12 import read_header
as rh
13 import read_grid
as rg
17 """High level interface to encapsulate and provide access to
18 the output of a specified FLEXPART simulation"""
20 def __init__(self, output_dir=None, nest=False):
23 output_dir: full path to directory of FLEXPART output
24 nest: Denotes whether this object is for the mother grid or a nest
35 raise IOError(
'Problem getting output data header')
37 raise IOError(
'Bad output data dir: ' + str(self.
_output_dir))
41 release=1, age_class=1):
43 """Extracts 2D horizontal slice. Note that level, species and
44 release index values for the user start at 1, but they are all
45 decremented internal to this method so they start at 0.
46 Returned 2D grid is a NumPy array
48 Note that if timestamp is not defined, then a timestamp "close to
49 the middle" will be selected.
51 If there is any kind of error, we return None
56 ntimes = len(self.
_Header[
'available_dates'])
58 timestamp = self.
_Header[
'available_dates'][tstamp_idx]
64 G = rg.read_grid(self.
_Header, date=timestamp,
71 logging.error(
'Problem getting G from read_grid()')
72 so_far_so_good =
False
77 the_grid = G[ (species-1, timestamp) ]
79 logging.error(
'Problem getting the_grid from G')
80 so_far_so_good =
False
85 the_slice = the_grid.grid[:, :, level-1, release-1]
88 logging.error(
'Problem getting the_slice from the_grid')
89 so_far_so_good =
False
98 release=1, age_class=1,
101 """Extracts 2D horizontal slice of deposition.
102 Note that species and
103 release index values for the user start at 1, but they are all
104 decremented internal to this method so they start at 0.
105 Returned 2D grid is a NumPy array
107 Note that if timestamp is not defined, then a timestamp "close to
108 the middle" will be selected.
110 If there is any kind of error, we return None
115 ntimes = len(self.
_Header[
'available_dates'])
116 tstamp_idx = ntimes/2
117 timestamp = self.
_Header[
'available_dates'][tstamp_idx]
120 so_far_so_good =
True
122 dry_flag=
False; wet_flag=
False
128 G = rg.read_grid(self.
_Header, date=timestamp,
142 logging.error(
'Problem getting G from read_grid()')
143 so_far_so_good =
False
148 the_grid = G[ (species-1, timestamp) ]
150 logging.error(
'Problem getting the_grid from G')
151 so_far_so_good =
False
172 if the_grid.dry.ndim == 2:
173 dry_grid = np.expand_dims(the_grid.dry, axis=2)
175 dry_grid = the_grid.dry
176 the_depo = dry_grid[:, :, release-1]
177 elif depo_type==
'wet':
179 if the_grid.wet.ndim == 2:
180 wet_grid = np.expand_dims(the_grid.wet, axis=2)
182 wet_grid = the_grid.wet
183 the_depo = wet_grid[:, :, release-1]
187 logging.error(
'Problem getting the_depo from the_grid')
188 so_far_so_good =
False
197 def get_volume(self, timestamp=None, level_list=None, species=1,
198 release=1, age_class=1):
200 """Extracts 3D volume. if level_list is not defined, we return the full
201 volume for the timestamp. If level_list is defined,then we extract the
202 specified levels and create a volume from them
204 Note that level, species and
205 release index values for the user start at 1, but they are all
206 decremented internal to this method so they start at 0.
207 Returned 2D grid is a NumPy array
209 If there is any kind of error, we return None
214 ntimes = len(self.
_Header[
'available_dates'])
215 tstamp_idx = ntimes/2
216 timestamp = self.
_Header[
'available_dates'][tstamp_idx]
219 so_far_so_good =
True
221 G = rg.read_grid(self.
_Header, date=timestamp,
228 logging.error(
'Problem getting G from read_grid()')
229 so_far_so_good =
False
234 the_grid = G[ (species-1, timestamp) ]
236 logging.error(
'Problem getting the_grid from G')
237 so_far_so_good =
False
242 level_indices = list(map(
lambda x: x-1, level_list))
245 the_volume = the_grid.grid[:, :, level_indices, release-1]
247 logging.error(
'Problem getting the_volume from the_grid')
248 so_far_so_good =
False
256 the_volume = the_grid.grid[:, :, :, release-1]
258 logging.error(
'Problem getting the_volume from the_grid')
259 so_far_so_good =
False
269 release=1, age_class=1):
271 """Extracts timeseries of 2D horizontal slices. If timestamp_list is
272 defined correctly, it extracts just the timestamps (in YYYYMMDDHHMMss
273 format) listed. Otherwise, it extracts all timestamps. The timeseries
274 is returned in a 3D NumPy array, indexed by (t, x, y)"""
277 if not timestamp_list:
278 timestamp_list = self.
_Header[
'available_dates']
282 slice_timeseries = np.ndarray( (len(timestamp_list), Nx, Ny),
289 for ts
in timestamp_list:
299 slice_timeseries[time_idx, :, :] = next_slice
303 return slice_timeseries
308 release=1, age_class=1,
311 """Extracts timeseries of 2D deposition. If timestamp_list is
312 defined correctly, it extracts just the timestamps (in YYYYMMDDHHMMss
313 format) listed. Otherwise, it extracts all timestamps. The timeseries
314 is returned in a 3D NumPy array, indexed by (t, x, y)"""
317 if not timestamp_list:
318 timestamp_list = self.
_Header[
'available_dates']
322 depo_timeseries = np.ndarray( (len(timestamp_list), Nx, Ny),
329 for ts
in timestamp_list:
342 depo_timeseries[time_idx, :, :] = next_depo
346 return depo_timeseries
350 level_list=
None, species=1,
351 release=1, age_class=1):
353 """Extracts timeseries of 2D horizontal slices. If timestamp_list is
354 defined correctly, it extracts just the timestamps (in YYYYMMDDHHMMss
355 format) listed. Otherwise, it extracts all timestamps. The timeseries
356 is returned in a 3D NumPy array, indexed by (t, x, y)"""
359 if not timestamp_list:
360 timestamp_list = self.
_Header[
'available_dates']
367 Nz = len( level_list )
369 volume_timeseries = np.ndarray( (len(timestamp_list), Nx, Ny, Nz),
376 for ts
in timestamp_list:
378 level_list=level_list,
386 volume_timeseries[time_idx, :, :] = next_volume
390 return volume_timeseries
397 Returns the list of timestamps as Python list
399 timestamp_list = self.
_Header[
'available_dates']
401 return timestamp_list
406 if __name__==
"_main__":
def get_deposition_timeseries
def get_volume_timeseries