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