1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # TODO AP |
---|
5 | #AP |
---|
6 | # - |
---|
7 | # - |
---|
8 | #************************************************************************ |
---|
9 | ## ------------------------------------------------------------------------------ |
---|
10 | # MODULES |
---|
11 | # ------------------------------------------------------------------------------ |
---|
12 | import subprocess |
---|
13 | import os |
---|
14 | |
---|
15 | ecapi = True |
---|
16 | try: |
---|
17 | import ecmwfapi |
---|
18 | except ImportError: |
---|
19 | ecapi = False |
---|
20 | #from gribapi import * |
---|
21 | # ------------------------------------------------------------------------------ |
---|
22 | # CLASS |
---|
23 | # ------------------------------------------------------------------------------ |
---|
24 | class MARSretrieval: |
---|
25 | ''' |
---|
26 | Class for submitting MARS retrievals. |
---|
27 | |
---|
28 | A description of MARS keywords/arguments and examples of their |
---|
29 | values can be found here: |
---|
30 | https://software.ecmwf.int/wiki/display/UDOC/\ |
---|
31 | Identification+keywords#Identificationkeywords-class |
---|
32 | |
---|
33 | ''' |
---|
34 | |
---|
35 | def __init__(self, server, marsclass = "ei", type = "", levtype = "", |
---|
36 | levelist = "", repres = "", date = "", resol = "", stream = "", |
---|
37 | area = "", time = "", step = "", expver = "1", number = "", |
---|
38 | accuracy = "", grid = "", gaussian = "", target = "", |
---|
39 | param = ""): |
---|
40 | ''' |
---|
41 | @Description: |
---|
42 | Initialises the instance of the MARSretrieval class and |
---|
43 | defines and assigns a set of the necessary retrieval parameters |
---|
44 | for the FLEXPART input data. |
---|
45 | A description of MARS keywords/arguments, their dependencies |
---|
46 | on each other and examples of their values can be found here: |
---|
47 | |
---|
48 | https://software.ecmwf.int/wiki/display/UDOC/MARS+keywords |
---|
49 | |
---|
50 | @Input: |
---|
51 | self: instance of MARSretrieval |
---|
52 | For description see class documentation. |
---|
53 | |
---|
54 | server: instance of ECMWFService (from ECMWF Web-API) |
---|
55 | This is the connection to the ECMWF data servers. |
---|
56 | It is needed for the pythonic access of ECMWF data. |
---|
57 | |
---|
58 | marsclass: string, optional |
---|
59 | Characterisation of dataset. E.g. EI (ERA-Interim), |
---|
60 | E4 (ERA40), OD (Operational archive), ea (ERA5). |
---|
61 | Default is the ERA-Interim dataset "ei". |
---|
62 | |
---|
63 | type: string, optional |
---|
64 | Determines the type of fields to be retrieved. |
---|
65 | Selects between observations, images or fields. |
---|
66 | Examples for fields: Analysis (an), Forecast (fc), |
---|
67 | Perturbed Forecast (pf), Control Forecast (cf) and so on. |
---|
68 | Default is an empty string. |
---|
69 | |
---|
70 | levtype: string, optional |
---|
71 | Denotes type of level. Has a direct implication on valid |
---|
72 | levelist values! |
---|
73 | E.g. model level (ml), pressure level (pl), surface (sfc), |
---|
74 | potential vorticity (pv), potential temperature (pt) |
---|
75 | and depth (dp). |
---|
76 | Default is an empty string. |
---|
77 | |
---|
78 | levelist: string, optional |
---|
79 | Specifies the required levels. It has to have a valid |
---|
80 | correspondence to the selected levtype. |
---|
81 | Examples: model level: 1/to/137, pressure levels: 500/to/1000 |
---|
82 | Default is an empty string. |
---|
83 | |
---|
84 | repres: string, optional |
---|
85 | Selects the representation of the archived data. |
---|
86 | E.g. sh - spherical harmonics, gg - Gaussian grid, |
---|
87 | ll - latitude/longitude, ... |
---|
88 | Default is an empty string. |
---|
89 | |
---|
90 | date: string, optional |
---|
91 | Specifies the Analysis date, the Forecast base date or |
---|
92 | Observations date. Valid formats are: |
---|
93 | Absolute as YYYY-MM-DD or YYYYMMDD. |
---|
94 | Default is an empty string. |
---|
95 | |
---|
96 | resol: string, optional |
---|
97 | Specifies the desired triangular truncation of retrieved data, |
---|
98 | before carrying out any other selected post-processing. |
---|
99 | The default is automatic truncation (auto), by which the lowest |
---|
100 | resolution compatible with the value specified in grid is |
---|
101 | automatically selected for the retrieval. |
---|
102 | Users wanting to perform post-processing from full spectral |
---|
103 | resolution should specify Archived Value (av). |
---|
104 | The following are examples of existing resolutions found in |
---|
105 | the archive: 63, 106, 159, 213, 255, 319, 399, 511, 799 or 1279. |
---|
106 | This keyword has no meaning/effect if the archived data is |
---|
107 | not in spherical harmonics representation. |
---|
108 | The best selection can be found here: |
---|
109 | https://software.ecmwf.int/wiki/display/UDOC/\ |
---|
110 | Retrieve#Retrieve-Truncationbeforeinterpolation |
---|
111 | Default is an empty string. |
---|
112 | |
---|
113 | stream: string, optional |
---|
114 | Identifies the forecasting system used to generate the data. |
---|
115 | E.g. oper (Atmospheric model), enfo (Ensemble forecats), ... |
---|
116 | Default is an empty string. |
---|
117 | |
---|
118 | area: string, optional |
---|
119 | Specifies the desired sub-area of data to be extracted. |
---|
120 | Areas can be defined to wrap around the globe. |
---|
121 | |
---|
122 | Latitude values must be given as signed numbers, with: |
---|
123 | north latitudes (i.e. north of the equator) |
---|
124 | being positive (e.g: 40.5) |
---|
125 | south latitutes (i.e. south of the equator) |
---|
126 | being negative (e.g: -50.5) |
---|
127 | Longtitude values must be given as signed numbers, with: |
---|
128 | east longitudes (i.e. east of the 0 degree meridian) |
---|
129 | being positive (e.g: 35.0) |
---|
130 | west longitudes (i.e. west of the 0 degree meridian) |
---|
131 | being negative (e.g: -20.5) |
---|
132 | |
---|
133 | E.g.: North/West/South/East |
---|
134 | Default is an empty string. |
---|
135 | |
---|
136 | time: string, optional |
---|
137 | Specifies the time of the data in hours and minutes. |
---|
138 | Valid values depend on the type of data: Analysis time, |
---|
139 | Forecast base time or First guess verification time |
---|
140 | (all usually at synoptic hours: 00, 06, 12 and 18 ). |
---|
141 | Observation time (any combination in hours and minutes is valid, |
---|
142 | subject to data availability in the archive). |
---|
143 | The syntax is HHMM or HH:MM. If MM is omitted it defaults to 00. |
---|
144 | Default is an empty string. |
---|
145 | |
---|
146 | step: string, optional |
---|
147 | Specifies the forecast time step from forecast base time. |
---|
148 | Valid values are hours (HH) from forecast base time. It also |
---|
149 | specifies the length of the forecast which verifies at |
---|
150 | First Guess time. |
---|
151 | E.g. 1/3/6-hourly |
---|
152 | Default is an empty string. |
---|
153 | |
---|
154 | expver: string, optional |
---|
155 | The version of the dataset. Each experiment is assigned a |
---|
156 | unique code (version). Production data is assigned 1 or 2, |
---|
157 | and experimental data in Operations 11, 12 ,... |
---|
158 | Research or Member State's experiments have a four letter |
---|
159 | experiment identifier. |
---|
160 | Default is "1". |
---|
161 | |
---|
162 | number: string, optional |
---|
163 | Selects the member in ensemble forecast run. (Only then it |
---|
164 | is necessary.) It has a different meaning depending on |
---|
165 | the type of data. |
---|
166 | E.g. Perturbed Forecasts: specifies the Ensemble forecast member |
---|
167 | Default is an empty string. |
---|
168 | |
---|
169 | accuracy: string, optional |
---|
170 | Specifies the number of bits per value to be used in the |
---|
171 | generated GRIB coded fields. |
---|
172 | A positive integer may be given to specify the preferred number |
---|
173 | of bits per packed value. This must not be greater than the |
---|
174 | number of bits normally used for a Fortran integer on the |
---|
175 | processor handling the request (typically 32 or 64 bit). |
---|
176 | Within a compute request the accuracy of the original fields |
---|
177 | can be passed to the result field by specifying accuracy=av. |
---|
178 | Default is an empty string. |
---|
179 | |
---|
180 | grid: string, optional |
---|
181 | Specifies the output grid which can be either a Gaussian grid |
---|
182 | or a Latitude/Longitude grid. MARS requests specifying |
---|
183 | grid=av will return the archived model grid. |
---|
184 | |
---|
185 | Lat/Lon grid: The grid spacing needs to be an integer |
---|
186 | fraction of 90 degrees e.g. grid = 0.5/0.5 |
---|
187 | |
---|
188 | Gaussian grid: specified by a letter denoting the type of |
---|
189 | Gaussian grid followed by an integer (the grid number) |
---|
190 | representing the number of lines between the Pole and Equator, |
---|
191 | e.g. |
---|
192 | grid = F160 - full (or regular) Gaussian grid with |
---|
193 | 160 latitude lines between the pole and equator |
---|
194 | grid = N320 - ECMWF original reduced Gaussian grid with |
---|
195 | 320 latitude lines between the pole and equator, |
---|
196 | see Reduced Gaussian Grids for grid numbers used at ECMWF |
---|
197 | grid = O640 - ECMWF octahedral (reduced) Gaussian grid with |
---|
198 | 640 latitude lines between the pole and equator |
---|
199 | Default is an empty string. |
---|
200 | |
---|
201 | gaussian: string, optional |
---|
202 | This parameter is deprecated and should no longer be used. |
---|
203 | Specifies the desired type of Gaussian grid for the output. |
---|
204 | Valid Gaussian grids are quasi-regular (reduced) or regular. |
---|
205 | Keyword gaussian can only be specified together with |
---|
206 | keyword grid. Gaussian without grid has no effect. |
---|
207 | Default is an empty string. |
---|
208 | |
---|
209 | target: string, optional |
---|
210 | Specifies a file into which data is to be written after |
---|
211 | retrieval or manipulation. Path names should always be |
---|
212 | enclosed in double quotes. The MARS client supports automatic |
---|
213 | generation of multiple target files using MARS keywords |
---|
214 | enclosed in square brackets [ ]. If the environment variable |
---|
215 | MARS_MULTITARGET_STRICT_FORMAT is set to 1 before calling mars, |
---|
216 | the keyword values will be used in the filename as shown by |
---|
217 | the ecCodes GRIB tool grib_ls -m, e.g. with |
---|
218 | MARS_MULTITARGET_STRICT_FORMAT set to 1 the keywords time, |
---|
219 | expver and param will be formatted as 0600, 0001 and 129.128 |
---|
220 | rather than 600, 1 and 129. |
---|
221 | Default is an empty string. |
---|
222 | |
---|
223 | param: string, optional |
---|
224 | Specifies the meteorological parameter. |
---|
225 | The list of meteorological parameters in MARS is extensive. |
---|
226 | Their availability is directly related to their meteorological |
---|
227 | meaning and, therefore, the rest of directives specified |
---|
228 | in the MARS request. |
---|
229 | Meteorological parameters can be specified by their |
---|
230 | GRIB code (param=130), their mnemonic (param=t) or |
---|
231 | full name (param=temperature). |
---|
232 | The list of parameter should be seperated by a "/"-sign. |
---|
233 | E.g. 130/131/133 |
---|
234 | Default is an empty string. |
---|
235 | |
---|
236 | @Return: |
---|
237 | <nothing> |
---|
238 | ''' |
---|
239 | |
---|
240 | self.server = server |
---|
241 | self.marsclass = marsclass |
---|
242 | self.type = type |
---|
243 | self.levtype = levtype |
---|
244 | self.levelist = levelist |
---|
245 | self.repres = repres |
---|
246 | self.date = date |
---|
247 | self.resol = resol |
---|
248 | self.stream = stream |
---|
249 | self.area = area |
---|
250 | self.time = time |
---|
251 | self.step = step |
---|
252 | self.expver = expver |
---|
253 | self.number = number |
---|
254 | self.accuracy = accuracy |
---|
255 | self.grid = grid |
---|
256 | self.gaussian = gaussian |
---|
257 | self.target = target |
---|
258 | self.param = param |
---|
259 | |
---|
260 | return |
---|
261 | |
---|
262 | |
---|
263 | def displayInfo(self): |
---|
264 | ''' |
---|
265 | @Description: |
---|
266 | Prints all class attributes and their values. |
---|
267 | |
---|
268 | @Input: |
---|
269 | self: instance of MARSretrieval |
---|
270 | For description see class documentation. |
---|
271 | |
---|
272 | @Return: |
---|
273 | <nothing> |
---|
274 | ''' |
---|
275 | # Get all class attributes and their values as a dictionary |
---|
276 | attrs = vars(self) |
---|
277 | |
---|
278 | # iterate through all attributes and print them |
---|
279 | # with their corresponding values |
---|
280 | for item in attrs.items(): |
---|
281 | if item[0] in ('server'): |
---|
282 | pass |
---|
283 | else: |
---|
284 | print(item[0] + ': ' + str(item[1])) |
---|
285 | |
---|
286 | return |
---|
287 | |
---|
288 | def dataRetrieve(self): |
---|
289 | ''' |
---|
290 | @Description: |
---|
291 | Submits a MARS retrieval. Depending on the existence of |
---|
292 | ECMWF Web-API it is submitted via Python or a |
---|
293 | subprocess in the Shell. The parameter for the mars retrieval |
---|
294 | are taken from the defined class attributes. |
---|
295 | |
---|
296 | @Input: |
---|
297 | self: instance of MARSretrieval |
---|
298 | For description see class documentation. |
---|
299 | |
---|
300 | @Return: |
---|
301 | <nothing> |
---|
302 | ''' |
---|
303 | # Get all class attributes and their values as a dictionary |
---|
304 | attrs = vars(self) |
---|
305 | |
---|
306 | # convert the dictionary of attributes into a comma |
---|
307 | # seperated list of attributes with their values |
---|
308 | # needed for the retrieval call |
---|
309 | s = 'ret' |
---|
310 | for k, v in attrs.iteritems(): |
---|
311 | if k in ('server'): |
---|
312 | continue |
---|
313 | if k == 'marsclass': |
---|
314 | k = 'class' |
---|
315 | if v == '': |
---|
316 | continue |
---|
317 | if k.lower() == 'target': |
---|
318 | target = v |
---|
319 | else: |
---|
320 | s = s + ',' + k + '=' + str(v) |
---|
321 | |
---|
322 | # MARS request via Python script |
---|
323 | if self.server is not False: |
---|
324 | try: |
---|
325 | self.server.execute(s, target) |
---|
326 | except: |
---|
327 | print('MARS Request failed, \ |
---|
328 | have you already registered at apps.ecmwf.int?') |
---|
329 | raise IOError |
---|
330 | if os.stat(target).st_size == 0: |
---|
331 | print('MARS Request returned no data - please check request') |
---|
332 | raise IOError |
---|
333 | # MARS request via extra process in shell |
---|
334 | else: |
---|
335 | s += ',target = "' + target + '"' |
---|
336 | p = subprocess.Popen(['mars'], stdin=subprocess.PIPE, |
---|
337 | stdout=subprocess.PIPE, |
---|
338 | stderr=subprocess.PIPE, bufsize=1) |
---|
339 | pout = p.communicate(input=s)[0] |
---|
340 | print(pout.decode()) |
---|
341 | |
---|
342 | if 'Some errors reported' in pout.decode(): |
---|
343 | print('MARS Request failed - please check request') |
---|
344 | raise IOError |
---|
345 | |
---|
346 | if os.stat(target).st_size == 0: |
---|
347 | print('MARS Request returned no data - please check request') |
---|
348 | raise IOError |
---|
349 | |
---|
350 | return |
---|