1 | #!/usr/bin/env python3 |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #******************************************************************************* |
---|
4 | # @Author: Anne Philipp (University of Vienna) |
---|
5 | # |
---|
6 | # @Date: November 2018 |
---|
7 | # |
---|
8 | # @Change History: |
---|
9 | # |
---|
10 | # @License: |
---|
11 | # (C) Copyright 2014-2019. |
---|
12 | # Anne Philipp, Leopold Haimberger |
---|
13 | # |
---|
14 | # SPDX-License-Identifier: CC-BY-4.0 |
---|
15 | # |
---|
16 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
17 | # International License. To view a copy of this license, visit |
---|
18 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
19 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
20 | #******************************************************************************* |
---|
21 | '''This module contains check methods for the CONTROL paramaters. |
---|
22 | ''' |
---|
23 | |
---|
24 | # ------------------------------------------------------------------------------ |
---|
25 | # MODULES |
---|
26 | # ------------------------------------------------------------------------------ |
---|
27 | |
---|
28 | from __future__ import print_function |
---|
29 | import os |
---|
30 | import sys |
---|
31 | |
---|
32 | import _config |
---|
33 | try: |
---|
34 | import exceptions |
---|
35 | except ImportError: |
---|
36 | import builtins as exceptions |
---|
37 | from datetime import datetime |
---|
38 | from Mods.tools import my_error, silent_remove |
---|
39 | # ------------------------------------------------------------------------------ |
---|
40 | # FUNCTIONS |
---|
41 | # ------------------------------------------------------------------------------ |
---|
42 | |
---|
43 | def check_logicals_type(c, logicals): |
---|
44 | '''Check that the logical variables have correct type integer. |
---|
45 | |
---|
46 | Parameters |
---|
47 | ---------- |
---|
48 | c : ControlFile |
---|
49 | Contains all the parameters of CONTROL file and |
---|
50 | command line. |
---|
51 | |
---|
52 | logicals : list of (str or int) |
---|
53 | Names of the switches that are used to control the flow of the |
---|
54 | program. |
---|
55 | |
---|
56 | Return |
---|
57 | ------ |
---|
58 | |
---|
59 | ''' |
---|
60 | |
---|
61 | for var in logicals: |
---|
62 | if not isinstance(getattr(c, var), int): |
---|
63 | setattr(c, var, int(getattr(c, var))) |
---|
64 | |
---|
65 | return |
---|
66 | |
---|
67 | def check_grid(grid): |
---|
68 | '''Convert grid into correct Lat/Lon format. E.g. '0.5/0.5' |
---|
69 | |
---|
70 | Checks on format of original grid. Wether it is in the order of 1000 or 1. |
---|
71 | Convert to correct grid format and substitute into "Lat/Lon" format string. |
---|
72 | |
---|
73 | Parameters |
---|
74 | ---------- |
---|
75 | grid : str |
---|
76 | Contains grid information |
---|
77 | |
---|
78 | Return |
---|
79 | ------ |
---|
80 | grid : str |
---|
81 | Contains grid in format Lat/lon. E.g. 0.1/0.1 |
---|
82 | ''' |
---|
83 | |
---|
84 | if 'N' in grid: |
---|
85 | return grid |
---|
86 | if '/' in grid: |
---|
87 | gridx, gridy = grid.split('/') |
---|
88 | if gridx == gridy: |
---|
89 | grid = gridx |
---|
90 | else: |
---|
91 | raise ValueError('GRID parameter contains two ' |
---|
92 | 'different values: %s' (grid)) |
---|
93 | # # determine grid format |
---|
94 | # if float(grid) / 100. >= 0.5: |
---|
95 | # # grid is defined in 1/1000 degrees; old format |
---|
96 | # grid = '{}/{}'.format(float(grid) / 1000., |
---|
97 | # float(grid) / 1000.) |
---|
98 | # elif float(grid) / 100. < 0.5: |
---|
99 | # # grid is defined in normal degree; new format |
---|
100 | # grid = '{}/{}'.format(float(grid), float(grid)) |
---|
101 | |
---|
102 | |
---|
103 | # determine grid format |
---|
104 | # assumes that nobody wants grid spacings of 20 deg or more |
---|
105 | if float(grid) >= 20.: |
---|
106 | # grid is defined in 1/1000 degree; old format |
---|
107 | grid = '{}/{}'.format(float(grid) / 1000., float(grid) / 1000.) |
---|
108 | else: |
---|
109 | # grid is defined in degree; new format |
---|
110 | grid = '{}/{}'.format(float(grid), float(grid)) |
---|
111 | |
---|
112 | |
---|
113 | return grid |
---|
114 | |
---|
115 | def check_area(grid, area, upper, lower, left , right): |
---|
116 | '''Defines the correct area string. |
---|
117 | |
---|
118 | Checks on the format of the four area components. Wether it is of |
---|
119 | the order of 1000 or 1. Also checks wether area was already set by command |
---|
120 | line, then the four components are overwritten. |
---|
121 | Convert to correct format of the order of magnitude "1" and sets the |
---|
122 | area parameter (North/West/South/East). |
---|
123 | E.g.: -5./20./10./10. |
---|
124 | |
---|
125 | Parameters |
---|
126 | ---------- |
---|
127 | grid : str |
---|
128 | Contains grid information. |
---|
129 | |
---|
130 | area : str |
---|
131 | Contains area informtion. |
---|
132 | |
---|
133 | upper : str |
---|
134 | The northern most latitude. |
---|
135 | |
---|
136 | lower : str |
---|
137 | The souther most latitude. |
---|
138 | |
---|
139 | left : str |
---|
140 | The western most longitude. |
---|
141 | |
---|
142 | right : str |
---|
143 | The eastern most longiude. |
---|
144 | |
---|
145 | Return |
---|
146 | ------ |
---|
147 | grid : str |
---|
148 | Contains grid in format Lat/lon. E.g. 0.1/0.1 |
---|
149 | ''' |
---|
150 | if 'N' in grid: # Gaussian output grid |
---|
151 | area = 'G' |
---|
152 | return area |
---|
153 | |
---|
154 | # if area was provided decompose area into its 4 components |
---|
155 | if area: |
---|
156 | components = area.split('/') |
---|
157 | upper, left, lower, right = components |
---|
158 | |
---|
159 | # determine area format |
---|
160 | if ((abs(float(upper) / 10000.) >= 0.01 or float(upper) / 1000. == 0. ) and |
---|
161 | (abs(float(lower) / 10000.) >= 0.01 or float(lower) / 1000. == 0. ) and |
---|
162 | (abs(float(left) / 10000.) >= 0.01 or float(left) / 1000. == 0. ) and |
---|
163 | (abs(float(right) / 10000.) >= 0.01 or float(right) / 1000. == 0.)): |
---|
164 | # area is defined in 1/1000 degrees; old format |
---|
165 | area = '{}/{}/{}/{}'.format(float(upper) / 1000., |
---|
166 | float(left) / 1000., |
---|
167 | float(lower) / 1000., |
---|
168 | float(right) / 1000.) |
---|
169 | elif (abs(float(upper) / 10000.) < 0.05 and |
---|
170 | abs(float(lower) / 10000.) < 0.05 and |
---|
171 | abs(float(left) / 10000.) < 0.05 and |
---|
172 | abs(float(right) / 10000.) < 0.05): |
---|
173 | # area is already in new format |
---|
174 | area = '{}/{}/{}/{}'.format(float(upper), |
---|
175 | float(left), |
---|
176 | float(lower), |
---|
177 | float(right)) |
---|
178 | else: |
---|
179 | raise ValueError('The area components have different ' |
---|
180 | 'formats (upper, lower, left, right): ' |
---|
181 | '{}/{}/{}/{}'.format(str(upper), str(lower), |
---|
182 | str(left) , str(right))) |
---|
183 | |
---|
184 | return area |
---|
185 | |
---|
186 | def check_levels(levelist, level): |
---|
187 | '''Defines correct level list and guarantees that the maximum level is |
---|
188 | one of the available maximum levels. |
---|
189 | |
---|
190 | Parameters |
---|
191 | ---------- |
---|
192 | levelist : str |
---|
193 | Specifies the level list. |
---|
194 | Examples: model level: 1/to/137, pressure levels: 500/to/1000 |
---|
195 | |
---|
196 | level : str |
---|
197 | Specifies the maximum level. |
---|
198 | |
---|
199 | Return |
---|
200 | ------ |
---|
201 | levelist : str |
---|
202 | Specifies the required levels. It has to have a valid |
---|
203 | correspondence to the selected levtype. |
---|
204 | Examples: model level: 1/to/137, pressure levels: 500/to/1000 |
---|
205 | |
---|
206 | level : str |
---|
207 | Specifies the maximum level. It has to be one of the |
---|
208 | available maximum level number as contained in the variable |
---|
209 | MAX_LEVEL_LIST in "_config". E.g. [16, 19, 31, 40, 50, 60, 62, 91, 137] |
---|
210 | |
---|
211 | ''' |
---|
212 | # assure consistency of levelist and level |
---|
213 | if not levelist and not level: |
---|
214 | raise ValueError('ERROR: neither levelist nor level ' |
---|
215 | 'specified in CONTROL file') |
---|
216 | elif not levelist and level: |
---|
217 | levelist = '1/to/' + level |
---|
218 | elif (levelist and not level) or \ |
---|
219 | (levelist[-1] != level[-1]): |
---|
220 | level = levelist.split('/')[-1] |
---|
221 | else: |
---|
222 | pass |
---|
223 | |
---|
224 | # check if max level is a valid level |
---|
225 | if int(level) not in _config.MAX_LEVEL_LIST: |
---|
226 | raise ValueError('ERROR: \n' |
---|
227 | 'LEVEL must be the maximum level of a specified ' |
---|
228 | 'level list from ECMWF, e.g. {} \n' |
---|
229 | 'Check parameter "LEVEL" or the max level of ' |
---|
230 | '"LEVELIST"!'.format(str(_config.MAX_LEVEL_LIST))) |
---|
231 | |
---|
232 | return levelist, level |
---|
233 | |
---|
234 | |
---|
235 | def check_ppid(c, ppid): |
---|
236 | '''Sets the current PPID. |
---|
237 | |
---|
238 | Parameters |
---|
239 | ---------- |
---|
240 | c : ControlFile |
---|
241 | Contains all the parameters of CONTROL file and |
---|
242 | command line. |
---|
243 | |
---|
244 | ppid : int or None |
---|
245 | Contains the ppid number provided by the command line parameter |
---|
246 | of is None otherwise. |
---|
247 | |
---|
248 | Return |
---|
249 | ------ |
---|
250 | |
---|
251 | ''' |
---|
252 | |
---|
253 | if not ppid: |
---|
254 | c.ppid = str(os.getppid()) |
---|
255 | else: |
---|
256 | c.ppid = ppid |
---|
257 | |
---|
258 | return |
---|
259 | |
---|
260 | |
---|
261 | def check_purefc(ftype): |
---|
262 | '''Check for a pure forecast mode. |
---|
263 | |
---|
264 | Parameters |
---|
265 | ---------- |
---|
266 | ftype : list of str |
---|
267 | List of field types. |
---|
268 | |
---|
269 | Return |
---|
270 | ------ |
---|
271 | True or False: |
---|
272 | True if pure forecasts are to be retrieved. False if there are |
---|
273 | analysis fields in between. |
---|
274 | ''' |
---|
275 | |
---|
276 | if 'AN' not in ftype and '4V' not in ftype: |
---|
277 | # pure forecast |
---|
278 | return 1 |
---|
279 | |
---|
280 | return 0 |
---|
281 | |
---|
282 | |
---|
283 | def check_step(step, mailfail): |
---|
284 | '''Checks on step format and convert into a list of steps. |
---|
285 | |
---|
286 | If the steps were defined with "to" and "by" they are converted into |
---|
287 | a list of steps. If the steps were set in a string, it is |
---|
288 | converted into a list. |
---|
289 | |
---|
290 | Parameters |
---|
291 | ---------- |
---|
292 | step : list of str or str |
---|
293 | Specifies the forecast time step from forecast base time. |
---|
294 | Valid values are hours (HH) from forecast base time. |
---|
295 | |
---|
296 | mailfail : list of str |
---|
297 | Contains all email addresses which should be notified. |
---|
298 | It might also contain just the ecmwf user name which will trigger |
---|
299 | mailing to the associated email address for this user. |
---|
300 | |
---|
301 | Return |
---|
302 | ------ |
---|
303 | step : list of str |
---|
304 | List of forecast steps in format e.g. [001, 002, ...] |
---|
305 | ''' |
---|
306 | import numpy as np |
---|
307 | |
---|
308 | if '/' in step: |
---|
309 | steps = step.split('/') |
---|
310 | if 'to' in step.lower() and 'by' in step.lower(): |
---|
311 | ilist = np.arange(int(steps[0]), |
---|
312 | int(steps[2]) + 1, |
---|
313 | int(steps[4])) |
---|
314 | step = ['{:0>3}'.format(i) for i in ilist] |
---|
315 | elif 'to' in step.lower() and 'by' not in step.lower(): |
---|
316 | my_error(step + ':\n' + |
---|
317 | 'if "to" is used in steps parameter, ' |
---|
318 | 'please use "by" as well') |
---|
319 | else: |
---|
320 | step = steps |
---|
321 | |
---|
322 | if not isinstance(step, list): |
---|
323 | step = [step] |
---|
324 | |
---|
325 | return step |
---|
326 | |
---|
327 | def check_type(ftype, steps): |
---|
328 | '''Check if type variable is of type list and if analysis field has |
---|
329 | forecast step 0. |
---|
330 | |
---|
331 | Parameters |
---|
332 | ---------- |
---|
333 | ftype : list of str or str |
---|
334 | List of field types. |
---|
335 | |
---|
336 | steps : str |
---|
337 | Specifies the forecast time step from forecast base time. |
---|
338 | Valid values are hours (HH) from forecast base time. |
---|
339 | |
---|
340 | Return |
---|
341 | ------ |
---|
342 | ftype : list of str |
---|
343 | List of field types. |
---|
344 | ''' |
---|
345 | if not isinstance(ftype, list): |
---|
346 | ftype = [ftype] |
---|
347 | |
---|
348 | for i, val in enumerate(ftype): |
---|
349 | if ftype[i] == 'AN' and int(steps[i]) != 0: |
---|
350 | print('Analysis retrievals must have STEP = 0 (now set to 0)') |
---|
351 | ftype[i] = 0 |
---|
352 | |
---|
353 | return ftype |
---|
354 | |
---|
355 | def check_time(ftime): |
---|
356 | '''Check if time variable is of type list. Otherwise convert to list. |
---|
357 | |
---|
358 | Parameters |
---|
359 | ---------- |
---|
360 | ftime : list of str or str |
---|
361 | The time in hours of the field. |
---|
362 | |
---|
363 | Return |
---|
364 | ------ |
---|
365 | ftime : list of str |
---|
366 | The time in hours of the field. |
---|
367 | ''' |
---|
368 | if not isinstance(ftime, list): |
---|
369 | ftime = [ftime] |
---|
370 | |
---|
371 | return ftime |
---|
372 | |
---|
373 | def check_len_type_time_step(ftype, ftime, steps, maxstep, purefc): |
---|
374 | '''Check if |
---|
375 | |
---|
376 | Parameters |
---|
377 | ---------- |
---|
378 | ftype : list of str |
---|
379 | List of field types. |
---|
380 | |
---|
381 | ftime : list of str or str |
---|
382 | The time in hours of the field. |
---|
383 | |
---|
384 | steps : str |
---|
385 | Specifies the forecast time step from forecast base time. |
---|
386 | Valid values are hours (HH) from forecast base time. |
---|
387 | |
---|
388 | maxstep : int |
---|
389 | The maximum forecast time step in hours from the forecast base time. |
---|
390 | This is the maximum step for non flux (accumulated) forecast data. |
---|
391 | |
---|
392 | purefc : int |
---|
393 | Switch for definition of pure forecast mode or not. |
---|
394 | |
---|
395 | Return |
---|
396 | ------ |
---|
397 | ftype : list of str |
---|
398 | List of field types. |
---|
399 | |
---|
400 | ftime : list of str |
---|
401 | The time in hours of the field. |
---|
402 | |
---|
403 | steps : str |
---|
404 | Specifies the forecast time step from forecast base time. |
---|
405 | Valid values are hours (HH) from forecast base time. |
---|
406 | ''' |
---|
407 | if not (len(ftype) == len(ftime) == len(steps)): |
---|
408 | raise ValueError('ERROR: The number of field types, times and steps ' |
---|
409 | 'are not the same! Please check the setting in the ' |
---|
410 | 'CONTROL file!') |
---|
411 | |
---|
412 | # if pure forecast is selected and only one field type/time is set |
---|
413 | # prepare a complete list of type/time/step combination upto maxstep |
---|
414 | if len(ftype) == 1 and purefc: |
---|
415 | nftype = [] |
---|
416 | nsteps = [] |
---|
417 | nftime = [] |
---|
418 | for i in range(0, maxstep + 1): |
---|
419 | nftype.append(ftype[0]) |
---|
420 | nsteps.append('{:0>3}'.format(i)) |
---|
421 | nftime.append(ftime[0]) |
---|
422 | return nftype, nftime, nsteps |
---|
423 | |
---|
424 | return ftype, ftime, steps |
---|
425 | |
---|
426 | def check_mail(mail): |
---|
427 | '''Check the string of mail addresses, seperate them and convert to a list. |
---|
428 | |
---|
429 | Parameters |
---|
430 | ---------- |
---|
431 | mail : list of str or str |
---|
432 | Contains email addresses for notifications. |
---|
433 | It might also contain just the ecmwf user name which will trigger |
---|
434 | mailing to the associated email address for this user. |
---|
435 | |
---|
436 | Return |
---|
437 | ------ |
---|
438 | mail : list of str |
---|
439 | Contains email addresses for notifications. |
---|
440 | It might also contain just the ecmwf user name which will trigger |
---|
441 | mailing to the associated email address for this user. |
---|
442 | |
---|
443 | ''' |
---|
444 | if not isinstance(mail, list): |
---|
445 | if ',' in mail: |
---|
446 | mail = mail.split(',') |
---|
447 | elif ' ' in mail: |
---|
448 | mail = mail.split() |
---|
449 | else: |
---|
450 | mail = [mail] |
---|
451 | |
---|
452 | return mail |
---|
453 | |
---|
454 | def check_queue(queue, gateway, destination, ecuid, ecgid): |
---|
455 | '''Check if the necessary ECMWF parameters are set if the queue is |
---|
456 | one of the QUEUES_LIST (in _config). |
---|
457 | |
---|
458 | Parameters |
---|
459 | ---------- |
---|
460 | queue : str |
---|
461 | Name of the queue if submitted to the ECMWF servers. |
---|
462 | Used to check if ecuid, ecgid, gateway and destination |
---|
463 | are set correctly and are not empty. |
---|
464 | |
---|
465 | gateway : str |
---|
466 | The address of the gateway server. |
---|
467 | |
---|
468 | destination : str |
---|
469 | The name of the destination of the gateway server for data |
---|
470 | transfer through ectrans. E.g. name@genericSftp |
---|
471 | |
---|
472 | ecuid : str |
---|
473 | ECMWF user id. |
---|
474 | |
---|
475 | ecgid : str |
---|
476 | ECMWF group id. |
---|
477 | |
---|
478 | Return |
---|
479 | ------ |
---|
480 | |
---|
481 | ''' |
---|
482 | if queue in _config.QUEUES_LIST and \ |
---|
483 | (not gateway or not destination or |
---|
484 | not ecuid or not ecgid): |
---|
485 | raise ValueError('\nEnvironment variables GATEWAY, DESTINATION, ECUID ' |
---|
486 | 'and ECGID were not set properly! \n ' |
---|
487 | 'Please check for existence of file "ECMWF_ENV" ' |
---|
488 | 'in the run directory!') |
---|
489 | return |
---|
490 | |
---|
491 | def check_pathes(idir, odir, fpdir, fedir): |
---|
492 | '''Check if output and flexpart pathes are set. |
---|
493 | |
---|
494 | Parameters |
---|
495 | ---------- |
---|
496 | idir : str |
---|
497 | Path to the temporary directory for MARS retrieval data. |
---|
498 | |
---|
499 | odir : str |
---|
500 | Path to the final output directory where the FLEXPART input files |
---|
501 | will be stored. |
---|
502 | |
---|
503 | fpdir : str |
---|
504 | Path to FLEXPART root directory. |
---|
505 | |
---|
506 | fedir : str |
---|
507 | Path to flex_extract root directory. |
---|
508 | |
---|
509 | Return |
---|
510 | ------ |
---|
511 | odir : str |
---|
512 | Path to the final output directory where the FLEXPART input files |
---|
513 | will be stored. |
---|
514 | |
---|
515 | fpdir : str |
---|
516 | Path to FLEXPART root directory. |
---|
517 | |
---|
518 | ''' |
---|
519 | if not fpdir: |
---|
520 | fpdir = fedir |
---|
521 | |
---|
522 | if not odir: |
---|
523 | odir = idir |
---|
524 | |
---|
525 | return odir, fpdir |
---|
526 | |
---|
527 | def check_dates(start, end): |
---|
528 | '''Checks if there is at least a start date for a one day retrieval. |
---|
529 | |
---|
530 | Checks if end date lies after start date and end date is set. |
---|
531 | |
---|
532 | Parameters |
---|
533 | ---------- |
---|
534 | start : str |
---|
535 | The start date of the retrieval job. |
---|
536 | |
---|
537 | end : str |
---|
538 | The end date of the retrieval job. |
---|
539 | |
---|
540 | Return |
---|
541 | ------ |
---|
542 | start : str |
---|
543 | The start date of the retrieval job. |
---|
544 | |
---|
545 | end : str |
---|
546 | The end date of the retrieval job. |
---|
547 | |
---|
548 | ''' |
---|
549 | # check for having at least a starting date |
---|
550 | # otherwise program is not allowed to run |
---|
551 | if not start: |
---|
552 | raise ValueError('start_date was neither specified in command line nor ' |
---|
553 | 'in CONTROL file.\n' |
---|
554 | 'Try "{} -h" to print usage information' |
---|
555 | .format(sys.argv[0].split('/')[-1]) ) |
---|
556 | |
---|
557 | # retrieve just one day if end_date isn't set |
---|
558 | if not end: |
---|
559 | end = start |
---|
560 | |
---|
561 | dstart = datetime.strptime(start, '%Y%m%d') |
---|
562 | dend = datetime.strptime(end, '%Y%m%d') |
---|
563 | if dstart > dend: |
---|
564 | raise ValueError('ERROR: Start date is after end date! \n' |
---|
565 | 'Please adapt the dates in CONTROL file or ' |
---|
566 | 'command line! (start={}; end={})'.format(start, end)) |
---|
567 | |
---|
568 | return start, end |
---|
569 | |
---|
570 | def check_maxstep(maxstep, steps): |
---|
571 | '''Convert maxstep into integer if it is already given. Otherwise, select |
---|
572 | maxstep by going through the steps list. |
---|
573 | |
---|
574 | Parameters |
---|
575 | ---------- |
---|
576 | maxstep : str |
---|
577 | The maximum forecast time step in hours from the forecast base time. |
---|
578 | This is the maximum step for non flux (accumulated) forecast data. |
---|
579 | |
---|
580 | steps : str |
---|
581 | Specifies the forecast time step from forecast base time. |
---|
582 | Valid values are hours (HH) from forecast base time. |
---|
583 | |
---|
584 | Return |
---|
585 | ------ |
---|
586 | maxstep : int |
---|
587 | The maximum forecast time step in hours from the forecast base time. |
---|
588 | This is the maximum step for non flux (accumulated) forecast data. |
---|
589 | |
---|
590 | ''' |
---|
591 | # if maxstep wasn't provided |
---|
592 | # search for it in the "step" parameter |
---|
593 | if not maxstep: |
---|
594 | maxstep = 0 |
---|
595 | for s in steps: |
---|
596 | if int(s) > maxstep: |
---|
597 | maxstep = int(s) |
---|
598 | else: |
---|
599 | maxstep = int(maxstep) |
---|
600 | |
---|
601 | return maxstep |
---|
602 | |
---|
603 | def check_basetime(basetime): |
---|
604 | '''Check if basetime is set and contains one of the two |
---|
605 | possible values (0, 12). |
---|
606 | |
---|
607 | Parameters |
---|
608 | ---------- |
---|
609 | basetime : int or str or None |
---|
610 | The time for a half day retrieval. The 12 hours upfront are to be |
---|
611 | retrieved. |
---|
612 | |
---|
613 | Return |
---|
614 | ------ |
---|
615 | basetime : int or None |
---|
616 | The time for a half day retrieval. The 12 hours upfront are to be |
---|
617 | retrieved. |
---|
618 | ''' |
---|
619 | if basetime is not None: |
---|
620 | basetime = int(basetime) |
---|
621 | if basetime != 0 and basetime != 12: |
---|
622 | raise ValueError('ERROR: Basetime has an invalid value ' |
---|
623 | '-> {}'.format(str(basetime))) |
---|
624 | return basetime |
---|
625 | |
---|
626 | def check_request(request, marsfile): |
---|
627 | '''Check if there is an old mars request file and remove it. |
---|
628 | |
---|
629 | Parameters |
---|
630 | ---------- |
---|
631 | request : int |
---|
632 | Selects the mode of retrieval. |
---|
633 | 0: Retrieves the data from ECMWF. |
---|
634 | 1: Prints the mars requests to an output file. |
---|
635 | 2: Retrieves the data and prints the mars request. |
---|
636 | |
---|
637 | marsfile : str |
---|
638 | Path to the mars request file. |
---|
639 | |
---|
640 | Return |
---|
641 | ------ |
---|
642 | |
---|
643 | ''' |
---|
644 | if request != 0: |
---|
645 | if os.path.isfile(marsfile): |
---|
646 | silent_remove(marsfile) |
---|
647 | return |
---|
648 | |
---|
649 | def check_public(public, dataset): |
---|
650 | '''Check wether the dataset parameter is set for a |
---|
651 | public data set retrieval. |
---|
652 | |
---|
653 | Parameters |
---|
654 | ---------- |
---|
655 | public : int |
---|
656 | Specifies if public data are to be retrieved or not. |
---|
657 | |
---|
658 | dataset : str |
---|
659 | Specific name which identifies the public dataset. |
---|
660 | |
---|
661 | Return |
---|
662 | ------ |
---|
663 | |
---|
664 | ''' |
---|
665 | if public and not dataset: |
---|
666 | raise ValueError('ERROR: If public mars data wants to be retrieved, ' |
---|
667 | 'the "dataset"-parameter has to be set too!') |
---|
668 | return |
---|
669 | |
---|
670 | def check_acctype(acctype, ftype): |
---|
671 | '''Guarantees that the accumulation field type is set. |
---|
672 | |
---|
673 | If not set, it is derivated as in the old method (TYPE[1]). |
---|
674 | |
---|
675 | Parameters |
---|
676 | ---------- |
---|
677 | acctype : str |
---|
678 | The field type for the accumulated forecast fields. |
---|
679 | |
---|
680 | ftype : list of str |
---|
681 | List of field types. |
---|
682 | |
---|
683 | Return |
---|
684 | ------ |
---|
685 | acctype : str |
---|
686 | The field type for the accumulated forecast fields. |
---|
687 | ''' |
---|
688 | if not acctype: |
---|
689 | print('... Control parameter ACCTYPE was not defined.') |
---|
690 | try: |
---|
691 | if len(ftype) == 1 and ftype[0] != 'AN': |
---|
692 | print('... Use same field type as for the non-flux fields.') |
---|
693 | acctype = ftype[0] |
---|
694 | elif len(ftype) > 1 and ftype[1] != 'AN': |
---|
695 | print('... Use old setting by using TYPE[1] for flux forecast!') |
---|
696 | acctype = ftype[1] |
---|
697 | except: |
---|
698 | raise ValueError('ERROR: Accumulation field type could not be set!') |
---|
699 | else: |
---|
700 | if acctype.upper() == 'AN': |
---|
701 | raise ValueError('ERROR: Accumulation forecast fields can not be ' |
---|
702 | 'of type "analysis"!') |
---|
703 | return acctype |
---|
704 | |
---|
705 | |
---|
706 | def check_acctime(acctime, marsclass, purefc, time): |
---|
707 | '''Guarantees that the accumulation forecast times were set. |
---|
708 | |
---|
709 | If it is not set, it tries to set the value for some of the |
---|
710 | most commonly used data sets. Otherwise it raises an error. |
---|
711 | |
---|
712 | Parameters |
---|
713 | ---------- |
---|
714 | acctime : str |
---|
715 | The starting time from the accumulated forecasts. |
---|
716 | |
---|
717 | marsclass : str |
---|
718 | ECMWF data classification identifier. |
---|
719 | |
---|
720 | purefc : int |
---|
721 | Switch for definition of pure forecast mode or not. |
---|
722 | |
---|
723 | Return |
---|
724 | ------ |
---|
725 | acctime : str |
---|
726 | The starting time from the accumulated forecasts. |
---|
727 | ''' |
---|
728 | |
---|
729 | if not acctime: |
---|
730 | print('... Control parameter ACCTIME was not defined.') |
---|
731 | print('... Value will be set depending on field type:\n ' |
---|
732 | '\t\t EA=06/18\n\t\t EI/OD=00/12\n\t\t EP=18') |
---|
733 | if marsclass.upper() == 'EA': # Era 5 |
---|
734 | acctime = '06/18' |
---|
735 | elif marsclass.upper() == 'EI': # Era-Interim |
---|
736 | acctime = '00/12' |
---|
737 | elif marsclass.upper() == 'EP': # CERA |
---|
738 | acctime = '18' |
---|
739 | elif marsclass.upper() == 'OD' and not purefc: # On-demand |
---|
740 | acctime = '00/12' |
---|
741 | elif marsclass.upper() == 'OD' and purefc: # On-demand |
---|
742 | acctime = time[0] |
---|
743 | else: |
---|
744 | raise ValueError('ERROR: Accumulation forecast time can not ' |
---|
745 | 'automatically be derived!') |
---|
746 | return acctime |
---|
747 | |
---|
748 | def check_accmaxstep(accmaxstep, marsclass, purefc, maxstep): |
---|
749 | '''Guarantees that the accumulation forecast step were set. |
---|
750 | |
---|
751 | Parameters |
---|
752 | ---------- |
---|
753 | accmaxstep : str |
---|
754 | The maximum forecast step for the accumulated forecast fields. |
---|
755 | |
---|
756 | marsclass : str |
---|
757 | ECMWF data classification identifier. |
---|
758 | |
---|
759 | purefc : int |
---|
760 | Switch for definition of pure forecast mode or not. |
---|
761 | |
---|
762 | maxstep : str |
---|
763 | The maximum forecast time step in hours from the forecast base time. |
---|
764 | This is the maximum step for non flux (accumulated) forecast data. |
---|
765 | |
---|
766 | Return |
---|
767 | ------ |
---|
768 | accmaxstep : str |
---|
769 | The maximum forecast step for the accumulated forecast fields. |
---|
770 | ''' |
---|
771 | if not accmaxstep: |
---|
772 | print('... Control parameter ACCMAXSTEP was not defined.') |
---|
773 | print('... Value will be set depending on field type/time: ' |
---|
774 | '\n\t\t EA/EI/OD=12\n\t\t EP=24') |
---|
775 | if marsclass.upper() in ['EA', 'EI', 'OD'] and not purefc: |
---|
776 | # Era 5, Era-Interim, On-demand operational |
---|
777 | accmaxstep = '12' |
---|
778 | elif marsclass.upper() == 'EP': # CERA |
---|
779 | accmaxstep = '24' |
---|
780 | elif purefc and accmaxstep != maxstep: |
---|
781 | accmaxstep = maxstep |
---|
782 | print('... For pure forecast mode, the accumulated forecast must ' |
---|
783 | 'have the same maxstep as the normal forecast fields!\n' |
---|
784 | '\t\t Accmaxstep was set to maxstep!') |
---|
785 | else: |
---|
786 | raise ValueError('ERROR: Accumulation forecast step can not ' |
---|
787 | 'automatically be derived!') |
---|
788 | else: |
---|
789 | if purefc and int(accmaxstep) != int(maxstep): |
---|
790 | accmaxstep = maxstep |
---|
791 | print('... For pure forecast mode, the accumulated forecast must ' |
---|
792 | 'have the same maxstep as the normal forecast fields!\n' |
---|
793 | '\t\t Accmaxstep was set to maxstep!') |
---|
794 | return accmaxstep |
---|
795 | |
---|
796 | def check_addpar(addpar): |
---|
797 | '''Check that addpar has correct format of additional parameters in |
---|
798 | a single string, so that it can be easily appended to the hard coded |
---|
799 | parameters that are retrieved in any case. |
---|
800 | |
---|
801 | Parameters |
---|
802 | ---------- |
---|
803 | addpar : str or list of str |
---|
804 | List of additional parameters to be retrieved. |
---|
805 | |
---|
806 | Return |
---|
807 | ------ |
---|
808 | addpar : str |
---|
809 | List of additional parameters to be retrieved. |
---|
810 | ''' |
---|
811 | |
---|
812 | if addpar and isinstance(addpar, str): |
---|
813 | if '/' in addpar: |
---|
814 | parlist = addpar.split('/') |
---|
815 | parlist = [p for p in parlist if p is not ''] |
---|
816 | else: |
---|
817 | parlist = [addpar] |
---|
818 | |
---|
819 | addpar = '/' + '/'.join(parlist) |
---|
820 | |
---|
821 | return addpar |
---|
822 | |
---|
823 | |
---|
824 | def check_job_chunk(job_chunk): |
---|
825 | '''Checks that if job chunk is set, the number is positive and non zero. |
---|
826 | |
---|
827 | Parameters |
---|
828 | ---------- |
---|
829 | job_chunk : int |
---|
830 | The number of days for a single job script. |
---|
831 | |
---|
832 | Return |
---|
833 | ------ |
---|
834 | job_chunk : int |
---|
835 | The number of days for a single job script. |
---|
836 | ''' |
---|
837 | if not job_chunk: |
---|
838 | return job_chunk |
---|
839 | else: |
---|
840 | job_chunk = int(job_chunk) |
---|
841 | |
---|
842 | if job_chunk < 0: |
---|
843 | raise ValueError('ERROR: The number of job chunk is negative!\n' |
---|
844 | 'It has to be a positive number!') |
---|
845 | elif job_chunk == 0: |
---|
846 | job_chunk = None |
---|
847 | else: |
---|
848 | pass |
---|
849 | |
---|
850 | return job_chunk |
---|
851 | |
---|
852 | |
---|
853 | def check_number(number, mailfail): |
---|
854 | '''Check for correct string format of ensemble member numbers. |
---|
855 | |
---|
856 | Parameters |
---|
857 | ---------- |
---|
858 | number : str |
---|
859 | List of ensemble member forecast runs. |
---|
860 | |
---|
861 | mailfail : list of str |
---|
862 | Contains all email addresses which should be notified. |
---|
863 | It might also contain just the ecmwf user name which will trigger |
---|
864 | mailing to the associated email address for this user. |
---|
865 | |
---|
866 | Return |
---|
867 | ------ |
---|
868 | number : str |
---|
869 | String with list of ensemble member forecast runs. E.g. '01/02/03/04' |
---|
870 | ''' |
---|
871 | |
---|
872 | if '/' in number: |
---|
873 | numbers = number.split('/') |
---|
874 | if 'to' in number.lower() and 'by' in number.lower(): |
---|
875 | number = '{:0>3}'.format(int(numbers[0])) + '/TO/' + \ |
---|
876 | '{:0>3}'.format(int(numbers[2])) + '/BY/' + \ |
---|
877 | '{:0>3}'.format(int(numbers[4])) |
---|
878 | elif 'to' in number.lower() and 'by' not in number.lower(): |
---|
879 | number = '{:0>3}'.format(int(numbers[0])) + '/TO/' + \ |
---|
880 | '{:0>3}'.format(int(numbers[2])) |
---|
881 | else: |
---|
882 | numbers = ['{:0>3}'.format(i) for i in numbers] |
---|
883 | number = '{:0>3}/'.join(numbers) |
---|
884 | elif number.isdigit(): |
---|
885 | number = '{:0>3}'.format(int(number)) |
---|
886 | else: |
---|
887 | pass |
---|
888 | |
---|
889 | return number |
---|