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