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