Created
December 10, 2022 19:11
-
-
Save scollis/5fddbe03dde7199aefe61ac6a29f41a9 to your computer and use it in GitHub Desktop.
quick ingest and plot of HALO LiDAR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from netCDF4 import Dataset | |
import os | |
import datetime | |
import xarray as xr | |
import pandas as pd | |
import matplotlib.dates as mdates | |
import matplotlib.dates as mdates | |
from matplotlib import pyplot as plt | |
from matplotlib.colors import LogNorm | |
import pyart | |
''' | |
Import of StreamLine .hpl (txt) files and save locally in directory. Therefore | |
the data is converted into matrices with dimension "number of range gates" x "time stamp/rays". | |
In newer versions of the StreamLine software, the spectral width can be | |
stored as additional parameter in the .hpl files. | |
ORIGINAL CODE: https://github.com/marenha/doppler_wind_lidar_toolbox | |
Adaptation by Max Grover and Scott Collis | |
''' | |
def convert_to_preferred_format(secs): | |
secs = secs % (24 * 3600) | |
hour = secs // 3600 | |
secs %= 3600 | |
mins = secs // 60 | |
secs %= 60 | |
#print("seconds value in hours:",hour) | |
#print("seconds value in minutes:",mins) | |
return "%02d:%02d:%02d" %(hour, mins, secs) | |
def hpl2dict(file_path): | |
#import hpl files into intercal storage | |
with open(file_path, 'r') as text_file: | |
lines=text_file.readlines() | |
#write lines into Dictionary | |
data_temp=dict() | |
header_n=17 #length of header | |
data_temp['filename']=lines[0].split()[-1] | |
data_temp['system_id']=int(lines[1].split()[-1]) | |
data_temp['number_of_gates']=int(lines[2].split()[-1]) | |
data_temp['range_gate_length_m']=float(lines[3].split()[-1]) | |
data_temp['gate_length_pts']=int(lines[4].split()[-1]) | |
data_temp['pulses_per_ray']=int(lines[5].split()[-1]) | |
data_temp['number_of_waypoints_in_file']=int(lines[6].split()[-1]) | |
rays_n=(len(lines)-header_n)/(data_temp['number_of_gates']+1) | |
''' | |
number of lines does not match expected format if the number of range gates | |
was changed in the measuring period of the data file (especially possible for stare data) | |
''' | |
if not rays_n.is_integer(): | |
print('Number of lines does not match expected format') | |
return np.nan | |
data_temp['no_of_rays_in_file']=int(rays_n) | |
data_temp['scan_type']=' '.join(lines[7].split()[2:]) | |
data_temp['focus_range']=lines[8].split()[-1] | |
data_temp['start_time']=pd.to_datetime(' '.join(lines[9].split()[-2:])) | |
data_temp['resolution']=('%s %s' % (lines[10].split()[-1],'m s-1')) | |
data_temp['range_gates']=np.arange(0,data_temp['number_of_gates']) | |
data_temp['center_of_gates']=(data_temp['range_gates']+0.5)*data_temp['range_gate_length_m'] | |
#dimensions of data set | |
gates_n=data_temp['number_of_gates'] | |
rays_n=data_temp['no_of_rays_in_file'] | |
# item of measurement variables are predefined as symetric numpy arrays filled with NaN values | |
data_temp['radial_velocity'] = np.full([gates_n,rays_n],np.nan) #m s-1 | |
data_temp['intensity'] = np.full([gates_n,rays_n],np.nan) #SNR+1 | |
data_temp['beta'] = np.full([gates_n,rays_n],np.nan) #m-1 sr-1 | |
data_temp['spectral_width'] = np.full([gates_n,rays_n],np.nan) | |
data_temp['elevation'] = np.full(rays_n,np.nan) #degrees | |
data_temp['azimuth'] = np.full(rays_n,np.nan) #degrees | |
data_temp['decimal_time'] = np.full(rays_n,np.nan) #hours | |
data_temp['pitch'] = np.full(rays_n,np.nan) #degrees | |
data_temp['roll'] = np.full(rays_n,np.nan) #degrees | |
for ri in range(0,rays_n): #loop rays | |
lines_temp = lines[header_n+(ri*gates_n)+ri+1:header_n+(ri*gates_n)+gates_n+ri+1] | |
header_temp = np.asarray(lines[header_n+(ri*gates_n)+ri].split(),dtype=float) | |
data_temp['decimal_time'][ri] = header_temp[0] | |
data_temp['azimuth'][ri] = header_temp[1] | |
data_temp['elevation'][ri] = header_temp[2] | |
data_temp['pitch'][ri] = header_temp[3] | |
data_temp['roll'][ri] = header_temp[4] | |
for gi in range(0,gates_n): #loop range gates | |
line_temp=np.asarray(lines_temp[gi].split(),dtype=float) | |
data_temp['radial_velocity'][gi,ri] = line_temp[1] | |
data_temp['intensity'][gi,ri] = line_temp[2] | |
data_temp['beta'][gi,ri] = line_temp[3] | |
if line_temp.size>4: | |
data_temp['spectral_width'][gi,ri] = line_temp[4] | |
return data_temp | |
def read_as_netcdf(file): | |
field_dict = hpl2dict(file) | |
initial_time = pd.to_datetime(field_dict['start_time']) | |
time = pd.to_datetime([convert_to_preferred_format(x*60.*60.) for x in field_dict['decimal_time']]) | |
ds = xr.Dataset(coords={'range':field_dict['center_of_gates'], | |
'time':time}, | |
data_vars={'radial_velocity':(['range', 'time'], | |
field_dict['radial_velocity']), | |
'beta': (('range', 'time'), | |
field_dict['beta']), | |
'intensity': (('range', 'time'), | |
field_dict['intensity']) | |
} | |
) | |
return ds | |
ndir = '/Users/scollis/DL/firstexp/Proc/2022/202212/20221209/' | |
files = os.listdir(indir) | |
targets = [] | |
for tf in files: | |
if 'Stare' in tf: | |
targets.append(os.path.join(indir,tf)) | |
targets.sort() | |
datasets = [read_as_netcdf(thisone) for thisone in targets] | |
mergeddata = xr.concat(datasets, 'time') | |
myf = plt.figure(figsize=[15,5]) | |
mergeddata.radial_velocity.plot.pcolormesh( cmap=pyart.graph.cm_colorblind.balance, vmin=-4, vmax=4) | |
plt.ylim([0,6000]) | |
plt.ylabel("Height") | |
myf = plt.figure(figsize=[15,5]) | |
(mergeddata.intensity-1.0).plot.pcolormesh( cmap=pyart.graph.cm_colorblind.ChaseSpectral, norm=LogNorm(vmin=.0001, vmax=6)) | |
plt.ylim([0,6000]) | |
plt.ylabel("Height") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment