[efdb01a] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Philipp (University of Vienna) |
---|
| 5 | # |
---|
| 6 | # @Date: May 2018 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # October 2014 - Anne Fouilloux (University of Oslo) |
---|
[ff99eae] | 10 | # - created functions silent_remove and product (taken from ECMWF) |
---|
[991df6a] | 11 | # |
---|
| 12 | # November 2015 - Leopold Haimberger (University of Vienna) |
---|
[ff99eae] | 13 | # - created functions: interpret_args_and_control, clean_up |
---|
| 14 | # my_error, normal_exit, init128, to_param_id |
---|
[991df6a] | 15 | # |
---|
| 16 | # April 2018 - Anne Philipp (University of Vienna): |
---|
| 17 | # - applied PEP8 style guide |
---|
| 18 | # - added documentation |
---|
[ff99eae] | 19 | # - moved all functions from file Flexparttools to this file tools |
---|
| 20 | # - added function get_list_as_string |
---|
[54a8a01] | 21 | # - seperated args and control interpretation |
---|
[991df6a] | 22 | # |
---|
| 23 | # @License: |
---|
| 24 | # (C) Copyright 2014-2018. |
---|
| 25 | # |
---|
| 26 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 27 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 28 | # |
---|
| 29 | # @Modul Description: |
---|
| 30 | # This module contains a couple of helpful functions which are |
---|
| 31 | # used in different places in flex_extract. |
---|
| 32 | # |
---|
| 33 | # @Module Content: |
---|
[9aefaad] | 34 | # - get_cmdline_args |
---|
[ff99eae] | 35 | # - clean_up |
---|
| 36 | # - my_error |
---|
| 37 | # - normal_exit |
---|
[991df6a] | 38 | # - product |
---|
[ff99eae] | 39 | # - silent_remove |
---|
[991df6a] | 40 | # - init128 |
---|
[ff99eae] | 41 | # - to_param_id |
---|
| 42 | # - get_list_as_string |
---|
[54a8a01] | 43 | # - make_dir |
---|
[991df6a] | 44 | # |
---|
| 45 | #******************************************************************************* |
---|
[efdb01a] | 46 | |
---|
| 47 | # ------------------------------------------------------------------------------ |
---|
| 48 | # MODULES |
---|
| 49 | # ------------------------------------------------------------------------------ |
---|
| 50 | import os |
---|
| 51 | import errno |
---|
| 52 | import sys |
---|
| 53 | import glob |
---|
[ff99eae] | 54 | import subprocess |
---|
[991df6a] | 55 | import traceback |
---|
[96e1533] | 56 | import exceptions |
---|
[095dc73] | 57 | from datetime import datetime |
---|
[991df6a] | 58 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[efdb01a] | 59 | |
---|
| 60 | # ------------------------------------------------------------------------------ |
---|
| 61 | # FUNCTIONS |
---|
| 62 | # ------------------------------------------------------------------------------ |
---|
| 63 | |
---|
[ca867de] | 64 | def none_or_str(value): |
---|
[274f9ef] | 65 | '''Converts the input string into pythons None-type if the string |
---|
| 66 | contains string "None". |
---|
| 67 | |
---|
| 68 | Parameters |
---|
| 69 | ---------- |
---|
| 70 | value : :obj:`string` |
---|
| 71 | String to be checked for the "None" word. |
---|
| 72 | |
---|
| 73 | Return |
---|
| 74 | ------ |
---|
| 75 | None or value: |
---|
| 76 | Return depends on the content of the input value. If it was "None", |
---|
| 77 | then the python type None is returned. Otherwise the string itself. |
---|
[ca867de] | 78 | ''' |
---|
| 79 | if value == 'None': |
---|
| 80 | return None |
---|
| 81 | return value |
---|
| 82 | |
---|
| 83 | def none_or_int(value): |
---|
[274f9ef] | 84 | '''Converts the input string into pythons None-type if the string |
---|
| 85 | contains string "None". Otherwise it is converted to an integer value. |
---|
| 86 | |
---|
| 87 | Parameters |
---|
| 88 | ---------- |
---|
| 89 | value : :obj:`string` |
---|
| 90 | String to be checked for the "None" word. |
---|
| 91 | |
---|
| 92 | Return |
---|
| 93 | ------ |
---|
| 94 | None or int(value): |
---|
| 95 | Return depends on the content of the input value. If it was "None", |
---|
| 96 | then the python type None is returned. Otherwise the string is |
---|
| 97 | converted into an integer value. |
---|
[ca867de] | 98 | ''' |
---|
| 99 | if value == 'None': |
---|
| 100 | return None |
---|
| 101 | return int(value) |
---|
| 102 | |
---|
[9aefaad] | 103 | def get_cmdline_args(): |
---|
[274f9ef] | 104 | '''Decomposes the command line arguments and assigns them to variables. |
---|
| 105 | Apply default values for non mentioned arguments. |
---|
[efdb01a] | 106 | |
---|
[274f9ef] | 107 | Parameters |
---|
| 108 | ---------- |
---|
[efdb01a] | 109 | |
---|
[274f9ef] | 110 | Return |
---|
| 111 | ------ |
---|
| 112 | args : :obj:`Namespace` |
---|
| 113 | Contains the commandline arguments from script/program call. |
---|
[efdb01a] | 114 | ''' |
---|
[54a8a01] | 115 | |
---|
[efdb01a] | 116 | parser = ArgumentParser(description='Retrieve FLEXPART input from \ |
---|
[54a8a01] | 117 | ECMWF MARS archive', |
---|
[efdb01a] | 118 | formatter_class=ArgumentDefaultsHelpFormatter) |
---|
| 119 | |
---|
[095dc73] | 120 | # control parameters that override control file values |
---|
[ca867de] | 121 | parser.add_argument("--start_date", dest="start_date", |
---|
| 122 | type=none_or_str, default=None, |
---|
[efdb01a] | 123 | help="start date YYYYMMDD") |
---|
[ca867de] | 124 | parser.add_argument("--end_date", dest="end_date", |
---|
| 125 | type=none_or_str, default=None, |
---|
[efdb01a] | 126 | help="end_date YYYYMMDD") |
---|
[ca867de] | 127 | parser.add_argument("--date_chunk", dest="date_chunk", |
---|
| 128 | type=none_or_int, default=None, |
---|
[efdb01a] | 129 | help="# of days to be retrieved at once") |
---|
[f2616a3] | 130 | parser.add_argument("--job_chunk", dest="job_chunk", |
---|
| 131 | type=none_or_int, default=None, |
---|
| 132 | help="# of days to be retrieved within a single job") |
---|
[ca867de] | 133 | parser.add_argument("--controlfile", dest="controlfile", |
---|
| 134 | type=none_or_str, default='CONTROL.temp', |
---|
| 135 | help="file with CONTROL parameters") |
---|
| 136 | parser.add_argument("--basetime", dest="basetime", |
---|
| 137 | type=none_or_int, default=None, |
---|
| 138 | help="base such as 00 or 12 (for half day retrievals)") |
---|
| 139 | parser.add_argument("--step", dest="step", |
---|
| 140 | type=none_or_str, default=None, |
---|
[efdb01a] | 141 | help="steps such as 00/to/48") |
---|
[ca867de] | 142 | parser.add_argument("--levelist", dest="levelist", |
---|
| 143 | type=none_or_str, default=None, |
---|
[efdb01a] | 144 | help="Vertical levels to be retrieved, e.g. 30/to/60") |
---|
[ca867de] | 145 | parser.add_argument("--area", dest="area", |
---|
| 146 | type=none_or_str, default=None, |
---|
[efdb01a] | 147 | help="area defined as north/west/south/east") |
---|
| 148 | |
---|
[095dc73] | 149 | # some switches |
---|
| 150 | parser.add_argument("--debug", dest="debug", |
---|
| 151 | type=none_or_int, default=None, |
---|
| 152 | help="debug mode - leave temporary files intact") |
---|
| 153 | parser.add_argument("--request", dest="request", |
---|
| 154 | type=none_or_int, default=None, |
---|
| 155 | help="list all mars requests in file mars_requests.dat") |
---|
| 156 | parser.add_argument("--public", dest="public", |
---|
| 157 | type=none_or_int, default=None, |
---|
| 158 | help="public mode - retrieves the public datasets") |
---|
| 159 | parser.add_argument("--rrint", dest="rrint", |
---|
| 160 | type=none_or_int, default=None, |
---|
| 161 | help="select old or new precipitation interpolation \ |
---|
| 162 | 0 - old method\ |
---|
| 163 | 1 - new method (additional subgrid points)") |
---|
| 164 | |
---|
| 165 | # set directories |
---|
[ca867de] | 166 | parser.add_argument("--inputdir", dest="inputdir", |
---|
| 167 | type=none_or_str, default=None, |
---|
[efdb01a] | 168 | help="root directory for storing intermediate files") |
---|
[ca867de] | 169 | parser.add_argument("--outputdir", dest="outputdir", |
---|
| 170 | type=none_or_str, default=None, |
---|
[efdb01a] | 171 | help="root directory for storing output files") |
---|
[2fef1f2] | 172 | parser.add_argument("--flexpartdir", dest="flexpartdir", |
---|
[ca867de] | 173 | type=none_or_str, default=None, |
---|
[efdb01a] | 174 | help="FLEXPART root directory (to find grib2flexpart \ |
---|
[54a8a01] | 175 | and COMMAND file)\n Normally flex_extract resides in \ |
---|
[efdb01a] | 176 | the scripts directory of the FLEXPART distribution") |
---|
| 177 | |
---|
[ff99eae] | 178 | # this is only used by prepare_flexpart.py to rerun a postprocessing step |
---|
[ca867de] | 179 | parser.add_argument("--ppid", dest="ppid", |
---|
[786cfd6] | 180 | type=none_or_str, default=None, |
---|
[54a8a01] | 181 | help="specify parent process id for \ |
---|
[ff99eae] | 182 | rerun of prepare_flexpart") |
---|
[efdb01a] | 183 | |
---|
| 184 | # arguments for job submission to ECMWF, only needed by submit.py |
---|
| 185 | parser.add_argument("--job_template", dest='job_template', |
---|
[ca867de] | 186 | type=none_or_str, default="job.temp", |
---|
[efdb01a] | 187 | help="job template file for submission to ECMWF") |
---|
[ca867de] | 188 | parser.add_argument("--queue", dest="queue", |
---|
| 189 | type=none_or_str, default=None, |
---|
[efdb01a] | 190 | help="queue for submission to ECMWF \ |
---|
| 191 | (e.g. ecgate or cca )") |
---|
| 192 | |
---|
| 193 | args = parser.parse_args() |
---|
| 194 | |
---|
[54a8a01] | 195 | return args |
---|
[efdb01a] | 196 | |
---|
[96e1533] | 197 | def read_ecenv(filepath): |
---|
[274f9ef] | 198 | '''Reads the file into a dictionary where the key values are the parameter |
---|
| 199 | names. |
---|
| 200 | |
---|
| 201 | Parameters |
---|
| 202 | ---------- |
---|
[96e1533] | 203 | filepath : :obj:`string` |
---|
[274f9ef] | 204 | Path to file where the ECMWF environment parameters are stored. |
---|
| 205 | |
---|
| 206 | Return |
---|
| 207 | ------ |
---|
| 208 | envs : :obj:`dictionary` |
---|
| 209 | Contains the environment parameter ecuid, ecgid, gateway |
---|
| 210 | and destination for ECMWF server environments. |
---|
[54a8a01] | 211 | ''' |
---|
| 212 | envs= {} |
---|
[96e1533] | 213 | try: |
---|
| 214 | with open(filepath, 'r') as f: |
---|
| 215 | for line in f: |
---|
| 216 | data = line.strip().split() |
---|
| 217 | envs[str(data[0])] = str(data[1]) |
---|
| 218 | except OSError as e: |
---|
| 219 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 220 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
[2fb99de] | 221 | |
---|
[96e1533] | 222 | sys.exit('\n... Error occured while trying to read ECMWF_ENV ' |
---|
| 223 | 'file: ' + str(filepath)) |
---|
[efdb01a] | 224 | |
---|
[54a8a01] | 225 | return envs |
---|
[efdb01a] | 226 | |
---|
[ff99eae] | 227 | def clean_up(c): |
---|
[96e1533] | 228 | '''Remove files from the intermediate directory (inputdir). |
---|
| 229 | |
---|
| 230 | It keeps the final FLEXPART input files if program runs without |
---|
| 231 | ECMWF Api and keywords "ectrans" or "ecstorage" are set to "1". |
---|
[274f9ef] | 232 | |
---|
| 233 | Parameters |
---|
| 234 | ---------- |
---|
| 235 | c : :obj:`ControlFile` |
---|
| 236 | Contains all the parameters of CONTROL file and |
---|
| 237 | command line. |
---|
| 238 | |
---|
| 239 | Return |
---|
| 240 | ------ |
---|
| 241 | |
---|
[efdb01a] | 242 | ''' |
---|
| 243 | |
---|
[96e1533] | 244 | print("... clean inputdir!") |
---|
[efdb01a] | 245 | |
---|
[96e1533] | 246 | cleanlist = glob.glob(os.path.join(c.inputdir, "*")) |
---|
[efdb01a] | 247 | |
---|
[96e1533] | 248 | if cleanlist: |
---|
| 249 | for element in cleanlist: |
---|
| 250 | if c.prefix not in element: |
---|
| 251 | silent_remove(element) |
---|
| 252 | if c.ecapi is False and (c.ectrans == 1 or c.ecstorage == 1): |
---|
| 253 | silent_remove(element) |
---|
| 254 | print("... done!") |
---|
| 255 | else: |
---|
| 256 | print("... nothing to clean!") |
---|
[efdb01a] | 257 | |
---|
| 258 | return |
---|
| 259 | |
---|
| 260 | |
---|
[54a8a01] | 261 | def my_error(users, message='ERROR'): |
---|
[274f9ef] | 262 | '''Prints a specified error message which can be passed to the function |
---|
| 263 | before exiting the program. |
---|
| 264 | |
---|
| 265 | Parameters |
---|
| 266 | ---------- |
---|
| 267 | user : :obj:`list` of :obj:`string` |
---|
| 268 | Contains all email addresses which should be notified. |
---|
| 269 | It might also contain just the ecmwf user name which wil trigger |
---|
| 270 | mailing to the associated email address for this user. |
---|
[efdb01a] | 271 | |
---|
[274f9ef] | 272 | message : :obj:`string`, optional |
---|
| 273 | Error message. Default value is "ERROR". |
---|
[efdb01a] | 274 | |
---|
[274f9ef] | 275 | Return |
---|
| 276 | ------ |
---|
[efdb01a] | 277 | |
---|
| 278 | ''' |
---|
[ff99eae] | 279 | |
---|
[96e1533] | 280 | trace = '\n'.join(traceback.format_stack()) |
---|
| 281 | full_message = message + '\n\n' + trace |
---|
[ff99eae] | 282 | |
---|
[96e1533] | 283 | print(full_message) |
---|
| 284 | |
---|
| 285 | send_mail(users, 'ERROR', full_message) |
---|
[54a8a01] | 286 | |
---|
| 287 | sys.exit(1) |
---|
[efdb01a] | 288 | |
---|
| 289 | return |
---|
| 290 | |
---|
| 291 | |
---|
[96e1533] | 292 | def send_mail(users, success_mode, message): |
---|
[274f9ef] | 293 | '''Prints a specific exit message which can be passed to the function. |
---|
[efdb01a] | 294 | |
---|
[274f9ef] | 295 | Parameters |
---|
| 296 | ---------- |
---|
[96e1533] | 297 | users : :obj:`list` of :obj:`string` |
---|
[274f9ef] | 298 | Contains all email addresses which should be notified. |
---|
| 299 | It might also contain just the ecmwf user name which wil trigger |
---|
| 300 | mailing to the associated email address for this user. |
---|
[efdb01a] | 301 | |
---|
[96e1533] | 302 | success_mode : :obj:``string` |
---|
| 303 | States the exit mode of the program to put into |
---|
| 304 | the mail subject line. |
---|
| 305 | |
---|
[274f9ef] | 306 | message : :obj:`string`, optional |
---|
| 307 | Message for exiting program. Default value is "Done!". |
---|
[efdb01a] | 308 | |
---|
[274f9ef] | 309 | Return |
---|
| 310 | ------ |
---|
[efdb01a] | 311 | |
---|
| 312 | ''' |
---|
[ff99eae] | 313 | |
---|
[54a8a01] | 314 | for user in users: |
---|
| 315 | if '${USER}' in user: |
---|
| 316 | user = os.getenv('USER') |
---|
| 317 | try: |
---|
[96e1533] | 318 | p = subprocess.Popen(['mail', '-s flex_extract_v7.1 ' + |
---|
| 319 | success_mode, os.path.expandvars(user)], |
---|
[ff99eae] | 320 | stdin=subprocess.PIPE, |
---|
| 321 | stdout=subprocess.PIPE, |
---|
| 322 | stderr=subprocess.PIPE, |
---|
| 323 | bufsize=1) |
---|
[96e1533] | 324 | pout = p.communicate(input=message + '\n\n')[0] |
---|
[54a8a01] | 325 | except ValueError as e: |
---|
[96e1533] | 326 | print('... ERROR: ' + str(e)) |
---|
| 327 | sys.exit('... Email could not be sent!') |
---|
| 328 | except OSError as e: |
---|
| 329 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 330 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 331 | sys.exit('... Email could not be sent!') |
---|
[54a8a01] | 332 | else: |
---|
[96e1533] | 333 | print('Email sent to ' + os.path.expandvars(user)) |
---|
| 334 | |
---|
| 335 | return |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | def normal_exit(message='Done!'): |
---|
| 339 | '''Prints a specific exit message which can be passed to the function. |
---|
| 340 | |
---|
| 341 | Parameters |
---|
| 342 | ---------- |
---|
| 343 | message : :obj:`string`, optional |
---|
| 344 | Message for exiting program. Default value is "Done!". |
---|
| 345 | |
---|
| 346 | Return |
---|
| 347 | ------ |
---|
| 348 | |
---|
| 349 | ''' |
---|
| 350 | |
---|
| 351 | print(str(message)) |
---|
[efdb01a] | 352 | |
---|
| 353 | return |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | def product(*args, **kwds): |
---|
[96e1533] | 357 | '''Creates combinations of all passed arguments. |
---|
| 358 | |
---|
| 359 | This method combines the single characters of the passed arguments |
---|
[274f9ef] | 360 | with each other. So that each character of each argument value |
---|
| 361 | will be combined with each character of the other arguments as a tuple. |
---|
| 362 | |
---|
| 363 | Note |
---|
| 364 | ---- |
---|
| 365 | This method is taken from an example at the ECMWF wiki website. |
---|
| 366 | https://software.ecmwf.int/wiki/display/GRIB/index.py; 2018-03-16 |
---|
| 367 | |
---|
| 368 | Example |
---|
| 369 | ------- |
---|
| 370 | product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy |
---|
| 371 | |
---|
| 372 | product(range(2), repeat = 3) --> 000 001 010 011 100 101 110 111 |
---|
| 373 | |
---|
| 374 | Parameters |
---|
| 375 | ---------- |
---|
[96e1533] | 376 | \*args : :obj:`list` or :obj:`string` |
---|
[274f9ef] | 377 | Positional arguments (arbitrary number). |
---|
| 378 | |
---|
| 379 | \*\*kwds : :obj:`dictionary` |
---|
| 380 | Contains all the keyword arguments from \*args. |
---|
| 381 | |
---|
| 382 | Return |
---|
| 383 | ------ |
---|
| 384 | prod : :obj:`tuple` |
---|
| 385 | Return will be done with "yield". A tuple of combined arguments. |
---|
| 386 | See example in description above. |
---|
[efdb01a] | 387 | ''' |
---|
[96e1533] | 388 | try: |
---|
| 389 | pools = map(tuple, args) * kwds.get('repeat', 1) |
---|
| 390 | result = [[]] |
---|
| 391 | for pool in pools: |
---|
| 392 | result = [x + [y] for x in result for y in pool] |
---|
| 393 | for prod in result: |
---|
| 394 | yield tuple(prod) |
---|
| 395 | except TypeError as e: |
---|
| 396 | sys.exit('... PRODUCT GENERATION FAILED!') |
---|
[efdb01a] | 397 | |
---|
| 398 | return |
---|
| 399 | |
---|
| 400 | |
---|
[ff99eae] | 401 | def silent_remove(filename): |
---|
[274f9ef] | 402 | '''Remove file if it exists. |
---|
| 403 | The function does not fail if the file does not exist. |
---|
| 404 | |
---|
| 405 | Parameters |
---|
| 406 | ---------- |
---|
| 407 | filename : :obj:`string` |
---|
| 408 | The name of the file to be removed without notification. |
---|
[efdb01a] | 409 | |
---|
[274f9ef] | 410 | Return |
---|
| 411 | ------ |
---|
[efdb01a] | 412 | |
---|
| 413 | ''' |
---|
| 414 | try: |
---|
| 415 | os.remove(filename) |
---|
| 416 | except OSError as e: |
---|
[96e1533] | 417 | # errno.ENOENT = no such file or directory |
---|
| 418 | if e.errno == errno.ENOENT: |
---|
| 419 | pass |
---|
| 420 | else: |
---|
[efdb01a] | 421 | raise # re-raise exception if a different error occured |
---|
| 422 | |
---|
| 423 | return |
---|
| 424 | |
---|
| 425 | |
---|
[ff99eae] | 426 | def init128(filepath): |
---|
[274f9ef] | 427 | '''Opens and reads the grib file with table 128 information. |
---|
| 428 | |
---|
| 429 | Parameters |
---|
| 430 | ---------- |
---|
| 431 | filepath : :obj:`string` |
---|
| 432 | Path to file of ECMWF grib table number 128. |
---|
| 433 | |
---|
| 434 | Return |
---|
| 435 | ------ |
---|
| 436 | table128 : :obj:`dictionary` |
---|
| 437 | Contains the ECMWF grib table 128 information. |
---|
| 438 | The key is the parameter number and the value is the |
---|
| 439 | short name of the parameter. |
---|
[efdb01a] | 440 | ''' |
---|
| 441 | table128 = dict() |
---|
[96e1533] | 442 | try: |
---|
| 443 | with open(filepath) as f: |
---|
| 444 | fdata = f.read().split('\n') |
---|
| 445 | except OSError as e: |
---|
| 446 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 447 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 448 | |
---|
| 449 | sys.exit('\n... Error occured while trying to read parameter ' |
---|
| 450 | 'table file: ' + str(filepath)) |
---|
| 451 | else: |
---|
| 452 | for data in fdata: |
---|
| 453 | if data[0] != '!': |
---|
| 454 | table128[data[0:3]] = data[59:64].strip() |
---|
[efdb01a] | 455 | |
---|
| 456 | return table128 |
---|
| 457 | |
---|
| 458 | |
---|
[ff99eae] | 459 | def to_param_id(pars, table): |
---|
[274f9ef] | 460 | '''Transform parameter names to parameter ids with ECMWF grib table 128. |
---|
| 461 | |
---|
| 462 | Parameters |
---|
| 463 | ---------- |
---|
| 464 | pars : :obj:`string` |
---|
| 465 | Addpar argument from CONTROL file in the format of |
---|
| 466 | parameter names instead of ids. The parameter short |
---|
| 467 | names are sepearted with "/" and they are passed as |
---|
| 468 | one single string. |
---|
| 469 | |
---|
| 470 | table : :obj:`dictionary` |
---|
| 471 | Contains the ECMWF grib table 128 information. |
---|
| 472 | The key is the parameter number and the value is the |
---|
| 473 | short name of the parameter. |
---|
| 474 | |
---|
| 475 | Return |
---|
| 476 | ------ |
---|
| 477 | ipar : :obj:`list` of :obj:`integer` |
---|
| 478 | List of addpar parameters from CONTROL file transformed to |
---|
| 479 | parameter ids in the format of integer. |
---|
[efdb01a] | 480 | ''' |
---|
[96e1533] | 481 | if not pars: |
---|
| 482 | return [] |
---|
| 483 | if not isinstance(pars, str): |
---|
| 484 | pars=str(pars) |
---|
| 485 | |
---|
[efdb01a] | 486 | cpar = pars.upper().split('/') |
---|
| 487 | ipar = [] |
---|
| 488 | for par in cpar: |
---|
| 489 | for k, v in table.iteritems(): |
---|
| 490 | if par == k or par == v: |
---|
| 491 | ipar.append(int(k)) |
---|
| 492 | break |
---|
[ff99eae] | 493 | else: |
---|
[2fb99de] | 494 | print('Warning: par ' + par + ' not found in table 128') |
---|
[efdb01a] | 495 | |
---|
| 496 | return ipar |
---|
| 497 | |
---|
[ff99eae] | 498 | def get_list_as_string(list_obj, concatenate_sign=', '): |
---|
[274f9ef] | 499 | '''Converts a list of arbitrary content into a single string. |
---|
[991df6a] | 500 | |
---|
[274f9ef] | 501 | Parameters |
---|
| 502 | ---------- |
---|
| 503 | list_obj : :obj:`list` |
---|
| 504 | A list with arbitrary content. |
---|
[991df6a] | 505 | |
---|
[274f9ef] | 506 | concatenate_sign : :obj:`string`, optional |
---|
| 507 | A string which is used to concatenate the single |
---|
| 508 | list elements. Default value is ", ". |
---|
[ff99eae] | 509 | |
---|
[274f9ef] | 510 | Return |
---|
| 511 | ------ |
---|
| 512 | str_of_list : :obj:`string` |
---|
| 513 | The content of the list as a single string. |
---|
[efdb01a] | 514 | ''' |
---|
[991df6a] | 515 | |
---|
[96e1533] | 516 | if not isinstance(list_obj, list): |
---|
| 517 | list_obj = list(list_obj) |
---|
[ff99eae] | 518 | str_of_list = concatenate_sign.join(str(l) for l in list_obj) |
---|
[991df6a] | 519 | |
---|
[ff99eae] | 520 | return str_of_list |
---|
[54a8a01] | 521 | |
---|
| 522 | def make_dir(directory): |
---|
[96e1533] | 523 | '''Creates a directory. |
---|
| 524 | |
---|
| 525 | It gives a warning if the directory already exists and skips process. |
---|
| 526 | The program stops only if there is another problem. |
---|
[54a8a01] | 527 | |
---|
[274f9ef] | 528 | Parameters |
---|
| 529 | ---------- |
---|
| 530 | directory : :obj:`string` |
---|
[96e1533] | 531 | The path to directory which should be created. |
---|
[274f9ef] | 532 | |
---|
| 533 | Return |
---|
| 534 | ------ |
---|
[54a8a01] | 535 | |
---|
| 536 | ''' |
---|
| 537 | try: |
---|
| 538 | os.makedirs(directory) |
---|
| 539 | except OSError as e: |
---|
[96e1533] | 540 | # errno.EEXIST = directory already exists |
---|
| 541 | if e.errno == errno.EEXIST: |
---|
[2fb99de] | 542 | print('WARNING: Directory {0} already exists!'.format(directory)) |
---|
[96e1533] | 543 | else: |
---|
| 544 | raise # re-raise exception if a different error occured |
---|
[54a8a01] | 545 | |
---|
| 546 | return |
---|
| 547 | |
---|
| 548 | def put_file_to_ecserver(ecd, filename, target, ecuid, ecgid): |
---|
[274f9ef] | 549 | '''Uses the ecaccess-file-put command to send a file to the ECMWF servers. |
---|
[97e09f4] | 550 | |
---|
[274f9ef] | 551 | Note |
---|
| 552 | ---- |
---|
| 553 | The return value is just for testing reasons. It does not have |
---|
| 554 | to be used from the calling function since the whole error handling |
---|
| 555 | is done in here. |
---|
[54a8a01] | 556 | |
---|
[274f9ef] | 557 | Parameters |
---|
| 558 | ---------- |
---|
| 559 | ecd : :obj:`string` |
---|
| 560 | The path were the file is stored. |
---|
[54a8a01] | 561 | |
---|
[274f9ef] | 562 | filename : :obj:`string` |
---|
| 563 | The name of the file to send to the ECMWF server. |
---|
[54a8a01] | 564 | |
---|
[274f9ef] | 565 | target : :obj:`string` |
---|
| 566 | The target queue where the file should be sent to. |
---|
[54a8a01] | 567 | |
---|
[274f9ef] | 568 | ecuid : :obj:`string` |
---|
| 569 | The user id on ECMWF server. |
---|
[54a8a01] | 570 | |
---|
[274f9ef] | 571 | ecgid : :obj:`string` |
---|
| 572 | The group id on ECMWF server. |
---|
[54a8a01] | 573 | |
---|
[274f9ef] | 574 | Return |
---|
| 575 | ------ |
---|
[96e1533] | 576 | |
---|
[54a8a01] | 577 | ''' |
---|
| 578 | |
---|
| 579 | try: |
---|
[96e1533] | 580 | subprocess.check_output(['ecaccess-file-put', |
---|
| 581 | ecd + '/' + filename, |
---|
| 582 | target + ':/home/ms/' + |
---|
| 583 | ecgid + '/' + ecuid + |
---|
| 584 | '/' + filename], |
---|
| 585 | stderr=subprocess.STDOUT) |
---|
[54a8a01] | 586 | except subprocess.CalledProcessError as e: |
---|
[96e1533] | 587 | print('... ERROR CODE: ' + str(e.returncode)) |
---|
| 588 | print('... ERROR MESSAGE:\n \t ' + str(e)) |
---|
[54a8a01] | 589 | |
---|
[2fb99de] | 590 | print('\n... Do you have a valid ecaccess certification key?') |
---|
[97e09f4] | 591 | sys.exit('... ECACCESS-FILE-PUT FAILED!') |
---|
[96e1533] | 592 | except OSError as e: |
---|
| 593 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 594 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 595 | |
---|
| 596 | print('\n... Most likely the ECACCESS library is not available!') |
---|
| 597 | sys.exit('... ECACCESS-FILE-PUT FAILED!') |
---|
[97e09f4] | 598 | |
---|
[96e1533] | 599 | return |
---|
[54a8a01] | 600 | |
---|
[b1d07c9] | 601 | def submit_job_to_ecserver(target, jobname): |
---|
[274f9ef] | 602 | '''Uses ecaccess-job-submit command to submit a job to the ECMWF server. |
---|
| 603 | |
---|
| 604 | Note |
---|
| 605 | ---- |
---|
| 606 | The return value is just for testing reasons. It does not have |
---|
| 607 | to be used from the calling function since the whole error handling |
---|
| 608 | is done in here. |
---|
| 609 | |
---|
| 610 | Parameters |
---|
| 611 | ---------- |
---|
| 612 | target : :obj:`string` |
---|
| 613 | The target where the file should be sent to, e.g. the queue. |
---|
| 614 | |
---|
| 615 | jobname : :obj:`string` |
---|
| 616 | The name of the jobfile to be submitted to the ECMWF server. |
---|
| 617 | |
---|
| 618 | Return |
---|
| 619 | ------ |
---|
[96e1533] | 620 | job_id : :obj:`int` |
---|
| 621 | The id number of the job as a reference at the ecmwf server. |
---|
[54a8a01] | 622 | ''' |
---|
| 623 | |
---|
| 624 | try: |
---|
[96e1533] | 625 | job_id = subprocess.check_output(['ecaccess-job-submit', '-queueName', |
---|
| 626 | target, jobname]) |
---|
[2fb99de] | 627 | |
---|
[96e1533] | 628 | except subprocess.CalledProcessError as e: |
---|
| 629 | print('... ERROR CODE: ' + str(e.returncode)) |
---|
| 630 | print('... ERROR MESSAGE:\n \t ' + str(e)) |
---|
[97e09f4] | 631 | |
---|
[2fb99de] | 632 | print('\n... Do you have a valid ecaccess certification key?') |
---|
[54a8a01] | 633 | sys.exit('... ECACCESS-JOB-SUBMIT FAILED!') |
---|
[96e1533] | 634 | except OSError as e: |
---|
| 635 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 636 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 637 | |
---|
| 638 | print('\n... Most likely the ECACCESS library is not available!') |
---|
| 639 | sys.exit('... ECACCESS-JOB-SUBMIT FAILED!') |
---|
[54a8a01] | 640 | |
---|
[96e1533] | 641 | return job_id |
---|
[095dc73] | 642 | |
---|
| 643 | |
---|
| 644 | def get_informations(filename): |
---|
| 645 | '''Gets basic information from an example grib file. |
---|
| 646 | |
---|
| 647 | These information are important for later use and the |
---|
| 648 | initialization of numpy arrays for data storing. |
---|
| 649 | |
---|
| 650 | Parameters |
---|
| 651 | ---------- |
---|
| 652 | filename : :obj:`string` |
---|
| 653 | Name of the file which will be opened to extract basic information. |
---|
| 654 | |
---|
| 655 | Return |
---|
| 656 | ------ |
---|
| 657 | data : :obj:`dictionary` |
---|
| 658 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 659 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 660 | 'longitudeOfFirstGridPointInDegrees', 'latitudeOfLastGridPointInDegrees', |
---|
| 661 | 'longitudeOfLastGridPointInDegrees', 'jDirectionIncrementInDegrees', |
---|
| 662 | 'iDirectionIncrementInDegrees', 'missingValue' |
---|
| 663 | ''' |
---|
[2fef1f2] | 664 | from eccodes import codes_grib_new_from_file, codes_get, codes_release |
---|
[095dc73] | 665 | |
---|
| 666 | data = {} |
---|
| 667 | |
---|
| 668 | # --- open file --- |
---|
| 669 | print("Opening file for getting information data --- %s" % filename) |
---|
| 670 | with open(filename) as f: |
---|
| 671 | # load first message from file |
---|
| 672 | gid = codes_grib_new_from_file(f) |
---|
| 673 | |
---|
| 674 | # information needed from grib message |
---|
| 675 | keys = [ |
---|
| 676 | 'Ni', |
---|
| 677 | 'Nj', |
---|
| 678 | 'latitudeOfFirstGridPointInDegrees', |
---|
| 679 | 'longitudeOfFirstGridPointInDegrees', |
---|
| 680 | 'latitudeOfLastGridPointInDegrees', |
---|
| 681 | 'longitudeOfLastGridPointInDegrees', |
---|
| 682 | 'jDirectionIncrementInDegrees', |
---|
| 683 | 'iDirectionIncrementInDegrees', |
---|
| 684 | 'missingValue', |
---|
| 685 | ] |
---|
| 686 | |
---|
| 687 | print('\nInformations are: ') |
---|
| 688 | for key in keys: |
---|
| 689 | # Get the value of the key in a grib message. |
---|
| 690 | data[key] = codes_get(gid,key) |
---|
| 691 | print("%s = %s" % (key,data[key])) |
---|
| 692 | |
---|
| 693 | # Free the memory for the message referred as gribid. |
---|
| 694 | codes_release(gid) |
---|
| 695 | |
---|
| 696 | return data |
---|
| 697 | |
---|
| 698 | |
---|
[2fef1f2] | 699 | def get_dimensions(info, purefc, dtime, index_vals, start_date, end_date): |
---|
[095dc73] | 700 | '''This function specifies the correct dimensions for x, y and t. |
---|
| 701 | |
---|
| 702 | Parameters |
---|
| 703 | ---------- |
---|
| 704 | info : :obj:`dictionary` |
---|
| 705 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 706 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 707 | 'longitudeOfFirstGridPointInDegrees', 'latitudeOfLastGridPointInDegrees', |
---|
| 708 | 'longitudeOfLastGridPointInDegrees', 'jDirectionIncrementInDegrees', |
---|
| 709 | 'iDirectionIncrementInDegrees', 'missingValue' |
---|
| 710 | |
---|
[2fef1f2] | 711 | purefc : :obj:`integer` |
---|
| 712 | Switch for definition of pure forecast mode or not. |
---|
| 713 | |
---|
| 714 | dtime : :obj:`string` |
---|
| 715 | Time step in hours. |
---|
| 716 | |
---|
| 717 | index_vals : :obj:`list` |
---|
| 718 | Contains the values from the keys used for a distinct selection |
---|
| 719 | of grib messages in processing the grib files. |
---|
| 720 | Content looks like e.g.: |
---|
| 721 | index_vals[0]: ('20171106', '20171107', '20171108') ; date |
---|
| 722 | index_vals[1]: ('0', '1200', '1800', '600') ; time |
---|
| 723 | index_vals[2]: ('0', '12', '3', '6', '9') ; stepRange |
---|
| 724 | |
---|
| 725 | start_date : :obj:`string` |
---|
| 726 | The start date of the retrieval job. |
---|
| 727 | |
---|
| 728 | end_date : :obj:`string` |
---|
| 729 | The end date of the retrieval job. |
---|
| 730 | |
---|
[095dc73] | 731 | Return |
---|
| 732 | ------ |
---|
| 733 | (ix, jy, it) : :obj:`tuple` of :obj:`integer` |
---|
| 734 | Dimension in x-direction, y-direction and in time. |
---|
| 735 | ''' |
---|
| 736 | |
---|
| 737 | ix = info['Ni'] |
---|
| 738 | |
---|
| 739 | jy = info['Nj'] |
---|
| 740 | |
---|
[2fef1f2] | 741 | if not purefc: |
---|
| 742 | it = ((end_date - start_date).days + 1) * 24/int(dtime) |
---|
| 743 | else: |
---|
| 744 | # #no of step * #no of times * #no of days |
---|
| 745 | it = len(index_vals[2]) * len(index_vals[1]) * len(index_vals[0]) |
---|
[095dc73] | 746 | |
---|
| 747 | return (ix, jy, it) |
---|
[bf48c8a] | 748 | |
---|
| 749 | |
---|
| 750 | def execute_subprocess(cmd_list, error_msg='SUBPROCESS FAILED!'): |
---|
| 751 | '''Executes a command line instruction via a subprocess. |
---|
| 752 | |
---|
| 753 | Error handling is done if an error occures. |
---|
| 754 | |
---|
| 755 | Parameters |
---|
| 756 | ---------- |
---|
| 757 | cmd_list : :obj:`list` of `:obj:`string` |
---|
| 758 | A list of the components for the command line execution. Each |
---|
| 759 | list entry is a single part of the command which is seperated from |
---|
| 760 | the rest by a blank space. |
---|
| 761 | E.g. ['mv', file1, file2] |
---|
| 762 | |
---|
| 763 | Return |
---|
| 764 | ------ |
---|
| 765 | error_msg : :obj:`string`, optional |
---|
| 766 | The possible error message if the subprocess failed. |
---|
| 767 | By default it will just tell "SUBPROCESS FAILED!". |
---|
| 768 | ''' |
---|
| 769 | |
---|
| 770 | try: |
---|
| 771 | subprocess.check_call(cmd_list) |
---|
| 772 | except subprocess.CalledProcessError as e: |
---|
| 773 | print('... ERROR CODE: ' + str(e.returncode)) |
---|
| 774 | print('... ERROR MESSAGE:\n \t ' + str(e)) |
---|
| 775 | |
---|
| 776 | sys.exit('... ' + error_msg) |
---|
| 777 | except OSError as e: |
---|
| 778 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 779 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 780 | |
---|
| 781 | sys.exit('... ' + error_msg) |
---|
| 782 | |
---|
| 783 | return |
---|