binietoglou@0: import datetime binietoglou@0: import os binietoglou@0: import glob binietoglou@0: binietoglou@0: import numpy as np binietoglou@0: binietoglou@0: from generic import BaseLidarMeasurement, Lidar_channel binietoglou@0: from ciao import CiaoMixin binietoglou@0: binietoglou@0: import pearl_netcdf_parameters binietoglou@0: from report_file import Report_file binietoglou@0: binietoglou@0: binietoglou@0: repository = '/mnt/storage/lidar_data/pearl/' binietoglou@0: binietoglou@0: binietoglou@0: class PearlLidarMeasurement(CiaoMixin, BaseLidarMeasurement): binietoglou@0: binietoglou@0: extra_netcdf_parameters = pearl_netcdf_parameters binietoglou@0: binietoglou@0: def import_file(self,filename): binietoglou@0: ''' Import a pearl file. ''' binietoglou@0: binietoglou@0: if filename in self.files: binietoglou@0: print "File has been imported already:" + filename binietoglou@0: else: binietoglou@0: parameters, channels_dict = self.read_pearl_data(filename) binietoglou@0: start_time = self._gettime(parameters['Acq_date'],parameters['Acq_start_time']) binietoglou@0: binietoglou@0: for channel_info in channels_dict.itervalues(): binietoglou@0: binietoglou@0: if channel_info['name'] == '1064ALR': binietoglou@0: name = '1064' binietoglou@0: tm = start_time binietoglou@0: elif channel_info['name'] == '1064BLR': binietoglou@0: name = '1064' binietoglou@0: tm = start_time + datetime.timedelta(seconds = 30) binietoglou@0: else: binietoglou@0: name = channel_info['name'] binietoglou@0: tm = start_time binietoglou@0: if name not in self.channels: binietoglou@0: self.channels[name] = Lidar_channel(channel_info) binietoglou@0: self.channels[name].data[tm] = channel_info['data'] binietoglou@0: self.files.append(filename) binietoglou@0: binietoglou@0: def read_pearl_data(self, filename): binietoglou@0: ''' binietoglou@0: Reads a pearl file. binietoglou@0: binietoglou@0: Returns: binietoglou@0: parameters - a dictionary of general parameters binietoglou@0: channels - a dictionary with keys the channel number and values lists binietoglou@0: [channel name, channel bin width, channel data]. binietoglou@0: ''' binietoglou@0: f = open(filename,'r') # Open the file binietoglou@0: s = f.read(26) # Read the first 26 bytes binietoglou@0: binietoglou@0: #Get the values in a dictionary binietoglou@0: parameters = {} binietoglou@0: parameters['Acq_date'] = s[0:10] # First 10 bytes are the acquisition date. binietoglou@0: parameters['Acq_start_time'] = s[10:20].strip() # Next 10 bytes are start time. Strip from trailing spaces. binietoglou@0: parameters['Channel_no'] = np.fromstring(s[20:22], dtype = np.int16) # Next 2 bytes are the number of channels. Short integer. binietoglou@0: parameters['Point_no'] = np.fromstring(s[22:26], dtype = np.int32) # Next 4 bytes are the number of points. Integer. binietoglou@0: p = parameters # Just for less typing binietoglou@0: binietoglou@0: # Read the channel parameters binietoglou@0: len = 20*p['Channel_no'] binietoglou@0: s = f.read(len) binietoglou@0: channels = {} binietoglou@0: for (c1,n) in zip(range(0,len, 20),range(p['Channel_no'])): binietoglou@0: channels[str(n)] = {'name' : s[c1+10:c1+20].strip(), binietoglou@0: 'binwidth' : s[c1:c1+10].strip()} binietoglou@0: binietoglou@0: #Read the data binietoglou@0: data = np.fromfile(f,dtype = np.float32) binietoglou@0: #print filename + ': ' + str(data.size) +',' + str(p['Point_no']) +str(p['Channel_no']) binietoglou@0: data = data.reshape(p['Point_no'],p['Channel_no']) binietoglou@0: for ch in channels.iterkeys(): binietoglou@0: channels[ch]['data'] = data[:,int(ch)] binietoglou@0: #Close the file binietoglou@0: f.close() binietoglou@0: return parameters,channels binietoglou@0: binietoglou@0: binietoglou@0: def get_measurement_for_interval(start_time, stop_time): binietoglou@0: ''' Searches for a pearl measurement based on a time interval binietoglou@0: ''' binietoglou@0: binietoglou@0: correct_series = None binietoglou@0: day = datetime.timedelta(hours = 24) binietoglou@0: binietoglou@0: if start_time > stop_time: binietoglou@0: raise ValueError('Stop time should be after start time') binietoglou@0: binietoglou@0: binietoglou@0: binietoglou@0: #The list of directories based on the given time. Same, previous, Next day binietoglou@0: possible_paths = [get_path(t) for t in [start_time - day, start_time, start_time + day] binietoglou@0: if get_path(t) is not None] binietoglou@0: for path in possible_paths: binietoglou@0: try: binietoglou@0: rf = Report_file(path) binietoglou@0: except: binietoglou@0: rf = None binietoglou@0: binietoglou@0: if rf is not None: binietoglou@0: for serie in rf.series: binietoglou@0: if (start_time > serie.starttime) and (stop_time < serie.endtime): binietoglou@0: correct_series = serie binietoglou@0: binietoglou@0: if correct_series: binietoglou@0: files = correct_series.files.get('apd', []) + correct_series.files.get('mcb', []) binietoglou@0: m_series = PearlLidarMeasurement(files) binietoglou@0: m_subset = m_series.subset_by_time(start_time, stop_time) binietoglou@0: return m_subset binietoglou@0: else: binietoglou@0: return None binietoglou@0: binietoglou@0: binietoglou@0: def get_channel(tim, channel = '1064'): binietoglou@0: if channel =='1064': binietoglou@0: extension = '*.apd' binietoglou@0: else: binietoglou@0: extension = '*.mcb' binietoglou@0: binietoglou@0: dirstr = get_path(tim) binietoglou@0: binietoglou@0: if not os.path.isdir(dirstr): binietoglou@0: raise IOError('No measurement for that date (directory does not exist.).') binietoglou@0: #No measurement for that date (directory does not exist.). binietoglou@0: files = glob.glob(dirstr + extension) binietoglou@0: m = PearlLidarMeasurement(files) binietoglou@0: c = m.channels[channel] binietoglou@0: return c binietoglou@0: binietoglou@0: binietoglou@0: def get_path(tim): binietoglou@0: dirstr = repository + tim.strftime('%Y')+ '/' +tim.strftime('%d%m%Y') + '/' binietoglou@0: return dirstr