1 # General imports |
1 # General imports |
2 import datetime |
2 import datetime |
3 from operator import itemgetter |
3 from operator import itemgetter |
4 |
4 import logging |
5 # Science imports |
5 |
|
6 import matplotlib as mpl |
|
7 import netCDF4 as netcdf |
6 import numpy as np |
8 import numpy as np |
7 import matplotlib as mpl |
9 from matplotlib import pyplot as plt |
8 from matplotlib.ticker import ScalarFormatter |
10 from matplotlib.ticker import ScalarFormatter |
9 from matplotlib import pyplot as plt |
|
10 import netCDF4 as netcdf |
|
11 |
11 |
12 netcdf_format = 'NETCDF3_CLASSIC' # choose one of 'NETCDF3_CLASSIC', 'NETCDF3_64BIT', 'NETCDF4_CLASSIC' and 'NETCDF4' |
12 netcdf_format = 'NETCDF3_CLASSIC' # choose one of 'NETCDF3_CLASSIC', 'NETCDF3_64BIT', 'NETCDF4_CLASSIC' and 'NETCDF4' |
13 |
13 |
14 |
14 |
15 class BaseLidarMeasurement(object): |
15 class BaseLidarMeasurement(object): |
141 |
141 |
142 def subset_by_scc_channels(self): |
142 def subset_by_scc_channels(self): |
143 """ |
143 """ |
144 Subset the measurement based on the channels provided in the extra_netecdf_parameter file. |
144 Subset the measurement based on the channels provided in the extra_netecdf_parameter file. |
145 """ |
145 """ |
146 extra_channels = self.extra_netcdf_parameters.channel_parameters.keys() |
146 scc_channels = self.extra_netcdf_parameters.channel_parameters.keys() |
147 return self.subset_by_channels(extra_channels) |
147 common_channels = list(set(scc_channels).intersection(self.channels.keys())) |
|
148 |
|
149 if not common_channels: |
|
150 logging.debug("Config channels: %s." % ','.join(scc_channels)) |
|
151 logging.debug("Licel channels: %s." % ','.join(self.channels.keys())) |
|
152 raise ValueError('No common channels between licel and configuration files.') |
|
153 |
|
154 return self.subset_by_channels(common_channels) |
148 |
155 |
149 def subset_by_time(self, start_time, stop_time): |
156 def subset_by_time(self, start_time, stop_time): |
150 |
157 |
151 if start_time > stop_time: |
158 if start_time > stop_time: |
152 raise ValueError('Stop time should be after start time') |
159 raise ValueError('Stop time should be after start time') |
322 channel_var = 'channel string ID' |
329 channel_var = 'channel string ID' |
323 variable_type = str |
330 variable_type = str |
324 else: |
331 else: |
325 raise ValueError('Channel parameters should define either "chanel_id" or "channel_string_ID".') |
332 raise ValueError('Channel parameters should define either "chanel_id" or "channel_string_ID".') |
326 |
333 |
327 temp_v = f.createVariable(channel_var, variable_type, ('channels', )) |
334 temp_v = f.createVariable(channel_var, variable_type, ('channels',)) |
328 for channel, n in enumerate(channels): |
335 for n, channel in enumerate(channels): |
329 temp_v[n] = params.channel_parameters[channel][channel_var] |
336 temp_v[n] = params.channel_parameters[channel][channel_var] |
330 |
337 |
331 # Write the values of fixed channel parameters |
338 # Write the values of fixed channel parameters |
332 for (var, t) in channel_variables.iteritems(): |
339 for (var, t) in channel_variables.iteritems(): |
333 temp_v = f.createVariable(var, t[1], t[0]) |
340 temp_v = f.createVariable(var, t[1], t[0]) |
487 self.matrix = np.array(map(itemgetter(1), sorted_data)) |
494 self.matrix = np.array(map(itemgetter(1), sorted_data)) |
488 |
495 |
489 def _nearest_dt(self, dtime): |
496 def _nearest_dt(self, dtime): |
490 margin = datetime.timedelta(seconds=300) |
497 margin = datetime.timedelta(seconds=300) |
491 if ((dtime + margin) < self.start_time) | ((dtime - margin) > self.stop_time): |
498 if ((dtime + margin) < self.start_time) | ((dtime - margin) > self.stop_time): |
492 print "Requested date not covered in this file" |
499 logging.error("Requested date not covered in this file") |
493 raise |
500 raise ValueError("Requested date not covered in this file") |
494 dt = abs(self.time - np.array(dtime)) |
501 dt = abs(self.time - np.array(dtime)) |
495 dtmin = min(dt) |
502 dtmin = min(dt) |
496 |
503 |
497 if dtmin > datetime.timedelta(seconds=60): |
504 if dtmin > datetime.timedelta(seconds=60): |
498 print "Nearest profile more than 60 seconds away. dt = %s." % dtmin |
505 logging.warning("Nearest profile more than 60 seconds away. dt = %s." % dtmin) |
499 ind_t = np.where(dt == dtmin) |
506 ind_t = np.where(dt == dtmin) |
500 ind_a = ind_t[0] |
507 ind_a = ind_t[0] |
501 if len(ind_a) > 1: |
508 if len(ind_a) > 1: |
502 ind_a = ind_a[0] |
509 ind_a = ind_a[0] |
503 chosen_time = self.time[ind_a] |
510 chosen_time = self.time[ind_a] |