[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: |
---|
[54a8a01] | 34 | # - get_cmdline_arguments |
---|
[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 |
---|
| 56 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[efdb01a] | 57 | |
---|
| 58 | # ------------------------------------------------------------------------------ |
---|
| 59 | # FUNCTIONS |
---|
| 60 | # ------------------------------------------------------------------------------ |
---|
| 61 | |
---|
[54a8a01] | 62 | def get_cmdline_arguments(): |
---|
[efdb01a] | 63 | ''' |
---|
| 64 | @Description: |
---|
[54a8a01] | 65 | Decomposes the command line arguments and assigns them to variables. |
---|
| 66 | Apply default values for non mentioned arguments. |
---|
[efdb01a] | 67 | |
---|
| 68 | @Input: |
---|
| 69 | <nothing> |
---|
| 70 | |
---|
| 71 | @Return: |
---|
| 72 | args: instance of ArgumentParser |
---|
| 73 | Contains the commandline arguments from script/program call. |
---|
| 74 | ''' |
---|
[54a8a01] | 75 | |
---|
[efdb01a] | 76 | parser = ArgumentParser(description='Retrieve FLEXPART input from \ |
---|
[54a8a01] | 77 | ECMWF MARS archive', |
---|
[efdb01a] | 78 | formatter_class=ArgumentDefaultsHelpFormatter) |
---|
| 79 | |
---|
| 80 | # the most important arguments |
---|
[54a8a01] | 81 | parser.add_argument("--start_date", dest="start_date", default=None, |
---|
[efdb01a] | 82 | help="start date YYYYMMDD") |
---|
[54a8a01] | 83 | parser.add_argument("--end_date", dest="end_date", default=None, |
---|
[efdb01a] | 84 | help="end_date YYYYMMDD") |
---|
| 85 | parser.add_argument("--date_chunk", dest="date_chunk", default=None, |
---|
| 86 | help="# of days to be retrieved at once") |
---|
| 87 | |
---|
[991df6a] | 88 | # some arguments that override the default in the CONTROL file |
---|
[54a8a01] | 89 | parser.add_argument("--basetime", dest="basetime", default=None, |
---|
[efdb01a] | 90 | help="base such as 00/12 (for half day retrievals)") |
---|
[54a8a01] | 91 | parser.add_argument("--step", dest="step", default=None, |
---|
[efdb01a] | 92 | help="steps such as 00/to/48") |
---|
[54a8a01] | 93 | parser.add_argument("--levelist", dest="levelist", default=None, |
---|
[efdb01a] | 94 | help="Vertical levels to be retrieved, e.g. 30/to/60") |
---|
[54a8a01] | 95 | parser.add_argument("--area", dest="area", default=None, |
---|
[efdb01a] | 96 | help="area defined as north/west/south/east") |
---|
| 97 | |
---|
| 98 | # set the working directories |
---|
| 99 | parser.add_argument("--inputdir", dest="inputdir", default=None, |
---|
| 100 | help="root directory for storing intermediate files") |
---|
| 101 | parser.add_argument("--outputdir", dest="outputdir", default=None, |
---|
| 102 | help="root directory for storing output files") |
---|
| 103 | parser.add_argument("--flexpart_root_scripts", dest="flexpart_root_scripts", |
---|
[54a8a01] | 104 | default=None, |
---|
[efdb01a] | 105 | help="FLEXPART root directory (to find grib2flexpart \ |
---|
[54a8a01] | 106 | and COMMAND file)\n Normally flex_extract resides in \ |
---|
[efdb01a] | 107 | the scripts directory of the FLEXPART distribution") |
---|
| 108 | |
---|
[ff99eae] | 109 | # this is only used by prepare_flexpart.py to rerun a postprocessing step |
---|
[54a8a01] | 110 | parser.add_argument("--ppid", dest="ppid", default=None, |
---|
| 111 | help="specify parent process id for \ |
---|
[ff99eae] | 112 | rerun of prepare_flexpart") |
---|
[efdb01a] | 113 | |
---|
| 114 | # arguments for job submission to ECMWF, only needed by submit.py |
---|
| 115 | parser.add_argument("--job_template", dest='job_template', |
---|
| 116 | default="job.temp", |
---|
| 117 | help="job template file for submission to ECMWF") |
---|
[54a8a01] | 118 | parser.add_argument("--queue", dest="queue", default=None, |
---|
[efdb01a] | 119 | help="queue for submission to ECMWF \ |
---|
| 120 | (e.g. ecgate or cca )") |
---|
| 121 | parser.add_argument("--controlfile", dest="controlfile", |
---|
| 122 | default='CONTROL.temp', |
---|
[991df6a] | 123 | help="file with CONTROL parameters") |
---|
[54a8a01] | 124 | parser.add_argument("--debug", dest="debug", default=None, |
---|
| 125 | help="debug mode - leave temporary files intact") |
---|
[2fb99de] | 126 | parser.add_argument("--request", dest="request", default=None, |
---|
| 127 | help="list all mars request in file mars_requests.dat \ |
---|
| 128 | and skip submission to mars") |
---|
[efdb01a] | 129 | |
---|
| 130 | args = parser.parse_args() |
---|
| 131 | |
---|
[54a8a01] | 132 | return args |
---|
[efdb01a] | 133 | |
---|
[54a8a01] | 134 | def read_ecenv(filename): |
---|
| 135 | ''' |
---|
| 136 | @Description: |
---|
| 137 | Reads the file into a dictionary where the key values are the parameter |
---|
| 138 | names. |
---|
[efdb01a] | 139 | |
---|
[54a8a01] | 140 | @Input: |
---|
| 141 | filename: string |
---|
[2fb99de] | 142 | Path to file where the ECMWV environment parameters are stored. |
---|
[efdb01a] | 143 | |
---|
[54a8a01] | 144 | @Return: |
---|
| 145 | envs: dict |
---|
[2fb99de] | 146 | Contains the environment parameter ecuid, ecgid, gateway |
---|
| 147 | and destination for ECMWF server environments. |
---|
[54a8a01] | 148 | ''' |
---|
| 149 | envs= {} |
---|
[2fb99de] | 150 | |
---|
[54a8a01] | 151 | with open(filename, 'r') as f: |
---|
| 152 | for line in f: |
---|
| 153 | data = line.strip().split() |
---|
| 154 | envs[str(data[0])] = str(data[1]) |
---|
[efdb01a] | 155 | |
---|
[54a8a01] | 156 | return envs |
---|
[efdb01a] | 157 | |
---|
[ff99eae] | 158 | def clean_up(c): |
---|
[efdb01a] | 159 | ''' |
---|
| 160 | @Description: |
---|
| 161 | Remove all files from intermediate directory |
---|
[991df6a] | 162 | (inputdir from CONTROL file). |
---|
[efdb01a] | 163 | |
---|
| 164 | @Input: |
---|
[991df6a] | 165 | c: instance of class ControlFile |
---|
[27fe969] | 166 | Contains all the parameters of CONTROL file and |
---|
| 167 | command line. |
---|
[efdb01a] | 168 | For more information about format and content of the parameter |
---|
| 169 | see documentation. |
---|
| 170 | |
---|
| 171 | @Return: |
---|
| 172 | <nothing> |
---|
| 173 | ''' |
---|
| 174 | |
---|
[2fb99de] | 175 | print("clean_up") |
---|
[efdb01a] | 176 | |
---|
| 177 | cleanlist = glob.glob(c.inputdir + "/*") |
---|
[54a8a01] | 178 | for clist in cleanlist: |
---|
| 179 | if c.prefix not in clist: |
---|
| 180 | silent_remove(clist) |
---|
[efdb01a] | 181 | if c.ecapi is False and (c.ectrans == '1' or c.ecstorage == '1'): |
---|
[54a8a01] | 182 | silent_remove(clist) |
---|
[efdb01a] | 183 | |
---|
[2fb99de] | 184 | print("Done") |
---|
[efdb01a] | 185 | |
---|
| 186 | return |
---|
| 187 | |
---|
| 188 | |
---|
[54a8a01] | 189 | def my_error(users, message='ERROR'): |
---|
[efdb01a] | 190 | ''' |
---|
| 191 | @Description: |
---|
| 192 | Prints a specified error message which can be passed to the function |
---|
| 193 | before exiting the program. |
---|
| 194 | |
---|
| 195 | @Input: |
---|
[54a8a01] | 196 | user: list of strings |
---|
| 197 | Contains all email addresses which should be notified. |
---|
| 198 | It might also contain just the ecmwf user name which wil trigger |
---|
| 199 | mailing to the associated email address for this user. |
---|
[efdb01a] | 200 | |
---|
| 201 | message: string, optional |
---|
| 202 | Error message. Default value is "ERROR". |
---|
| 203 | |
---|
| 204 | @Return: |
---|
| 205 | <nothing> |
---|
| 206 | ''' |
---|
[ff99eae] | 207 | |
---|
[2fb99de] | 208 | print(message) |
---|
[ff99eae] | 209 | |
---|
| 210 | # comment if user does not want email notification directly from python |
---|
[54a8a01] | 211 | for user in users: |
---|
| 212 | if '${USER}' in user: |
---|
| 213 | user = os.getenv('USER') |
---|
| 214 | try: |
---|
| 215 | p = subprocess.Popen(['mail', '-s flex_extract_v7.1 ERROR', |
---|
| 216 | os.path.expandvars(user)], |
---|
| 217 | stdin=subprocess.PIPE, |
---|
| 218 | stdout=subprocess.PIPE, |
---|
| 219 | stderr=subprocess.PIPE, |
---|
| 220 | bufsize=1) |
---|
| 221 | trace = '\n'.join(traceback.format_stack()) |
---|
| 222 | pout = p.communicate(input=message + '\n\n' + trace)[0] |
---|
| 223 | except ValueError as e: |
---|
[2fb99de] | 224 | print('ERROR: ', e) |
---|
[54a8a01] | 225 | sys.exit('Email could not be sent!') |
---|
| 226 | else: |
---|
[2fb99de] | 227 | print('Email sent to ' + os.path.expandvars(user) + ' ' + |
---|
| 228 | pout.decode()) |
---|
[54a8a01] | 229 | |
---|
| 230 | sys.exit(1) |
---|
[efdb01a] | 231 | |
---|
| 232 | return |
---|
| 233 | |
---|
| 234 | |
---|
[54a8a01] | 235 | def normal_exit(users, message='Done!'): |
---|
[efdb01a] | 236 | ''' |
---|
| 237 | @Description: |
---|
| 238 | Prints a specific exit message which can be passed to the function. |
---|
| 239 | |
---|
| 240 | @Input: |
---|
[54a8a01] | 241 | user: list of strings |
---|
| 242 | Contains all email addresses which should be notified. |
---|
| 243 | It might also contain just the ecmwf user name which wil trigger |
---|
| 244 | mailing to the associated email address for this user. |
---|
[efdb01a] | 245 | |
---|
| 246 | message: string, optional |
---|
| 247 | Message for exiting program. Default value is "Done!". |
---|
| 248 | |
---|
| 249 | @Return: |
---|
| 250 | <nothing> |
---|
| 251 | |
---|
| 252 | ''' |
---|
[2fb99de] | 253 | print(message) |
---|
[ff99eae] | 254 | |
---|
| 255 | # comment if user does not want notification directly from python |
---|
[54a8a01] | 256 | for user in users: |
---|
| 257 | if '${USER}' in user: |
---|
| 258 | user = os.getenv('USER') |
---|
| 259 | try: |
---|
| 260 | p = subprocess.Popen(['mail', '-s flex_extract_v7.1 normal exit', |
---|
| 261 | os.path.expandvars(user)], |
---|
[ff99eae] | 262 | stdin=subprocess.PIPE, |
---|
| 263 | stdout=subprocess.PIPE, |
---|
| 264 | stderr=subprocess.PIPE, |
---|
| 265 | bufsize=1) |
---|
| 266 | pout = p.communicate(input=message+'\n\n')[0] |
---|
[54a8a01] | 267 | except ValueError as e: |
---|
[2fb99de] | 268 | print('ERROR: ', e) |
---|
| 269 | print('Email could not be sent!') |
---|
[54a8a01] | 270 | else: |
---|
[2fb99de] | 271 | print('Email sent to ' + os.path.expandvars(user) + ' ' + |
---|
| 272 | pout.decode()) |
---|
[efdb01a] | 273 | |
---|
| 274 | return |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | def product(*args, **kwds): |
---|
| 278 | ''' |
---|
| 279 | @Description: |
---|
| 280 | This method is taken from an example at the ECMWF wiki website. |
---|
| 281 | https://software.ecmwf.int/wiki/display/GRIB/index.py; 2018-03-16 |
---|
| 282 | |
---|
| 283 | This method combines the single characters of the passed arguments |
---|
| 284 | with each other. So that each character of each argument value |
---|
| 285 | will be combined with each character of the other arguments as a tuple. |
---|
| 286 | |
---|
| 287 | Example: |
---|
| 288 | product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy |
---|
| 289 | product(range(2), repeat = 3) --> 000 001 010 011 100 101 110 111 |
---|
| 290 | |
---|
| 291 | @Input: |
---|
| 292 | *args: tuple |
---|
| 293 | Positional arguments (arbitrary number). |
---|
| 294 | |
---|
| 295 | **kwds: dictionary |
---|
| 296 | Contains all the keyword arguments from *args. |
---|
| 297 | |
---|
| 298 | @Return: |
---|
| 299 | prod: tuple |
---|
| 300 | Return will be done with "yield". A tuple of combined arguments. |
---|
| 301 | See example in description above. |
---|
| 302 | ''' |
---|
| 303 | pools = map(tuple, args) * kwds.get('repeat', 1) |
---|
| 304 | result = [[]] |
---|
| 305 | for pool in pools: |
---|
| 306 | result = [x + [y] for x in result for y in pool] |
---|
| 307 | for prod in result: |
---|
| 308 | yield tuple(prod) |
---|
| 309 | |
---|
| 310 | return |
---|
| 311 | |
---|
| 312 | |
---|
[ff99eae] | 313 | def silent_remove(filename): |
---|
[efdb01a] | 314 | ''' |
---|
| 315 | @Description: |
---|
[efa05d7] | 316 | Remove file if it exists. |
---|
[991df6a] | 317 | The function does not fail if the file does not exist. |
---|
[efdb01a] | 318 | |
---|
| 319 | @Input: |
---|
| 320 | filename: string |
---|
| 321 | The name of the file to be removed without notification. |
---|
| 322 | |
---|
| 323 | @Return: |
---|
| 324 | <nothing> |
---|
| 325 | ''' |
---|
| 326 | try: |
---|
| 327 | os.remove(filename) |
---|
| 328 | except OSError as e: |
---|
[54a8a01] | 329 | if e.errno != errno.ENOENT: |
---|
[efdb01a] | 330 | # errno.ENOENT = no such file or directory |
---|
| 331 | raise # re-raise exception if a different error occured |
---|
| 332 | |
---|
| 333 | return |
---|
| 334 | |
---|
| 335 | |
---|
[ff99eae] | 336 | def init128(filepath): |
---|
[efdb01a] | 337 | ''' |
---|
| 338 | @Description: |
---|
| 339 | Opens and reads the grib file with table 128 information. |
---|
| 340 | |
---|
| 341 | @Input: |
---|
[ff99eae] | 342 | filepath: string |
---|
[efdb01a] | 343 | Path to file of ECMWF grib table number 128. |
---|
| 344 | |
---|
| 345 | @Return: |
---|
| 346 | table128: dictionary |
---|
| 347 | Contains the ECMWF grib table 128 information. |
---|
| 348 | The key is the parameter number and the value is the |
---|
| 349 | short name of the parameter. |
---|
| 350 | ''' |
---|
| 351 | table128 = dict() |
---|
[ff99eae] | 352 | with open(filepath) as f: |
---|
[efdb01a] | 353 | fdata = f.read().split('\n') |
---|
| 354 | for data in fdata: |
---|
| 355 | if data[0] != '!': |
---|
| 356 | table128[data[0:3]] = data[59:64].strip() |
---|
| 357 | |
---|
| 358 | return table128 |
---|
| 359 | |
---|
| 360 | |
---|
[ff99eae] | 361 | def to_param_id(pars, table): |
---|
[efdb01a] | 362 | ''' |
---|
| 363 | @Description: |
---|
| 364 | Transform parameter names to parameter ids |
---|
| 365 | with ECMWF grib table 128. |
---|
| 366 | |
---|
| 367 | @Input: |
---|
| 368 | pars: string |
---|
[991df6a] | 369 | Addpar argument from CONTROL file in the format of |
---|
[efdb01a] | 370 | parameter names instead of ids. The parameter short |
---|
| 371 | names are sepearted with "/" and they are passed as |
---|
| 372 | one single string. |
---|
| 373 | |
---|
| 374 | table: dictionary |
---|
| 375 | Contains the ECMWF grib table 128 information. |
---|
| 376 | The key is the parameter number and the value is the |
---|
| 377 | short name of the parameter. |
---|
| 378 | |
---|
| 379 | @Return: |
---|
| 380 | ipar: list of integer |
---|
[991df6a] | 381 | List of addpar parameters from CONTROL file transformed to |
---|
[efdb01a] | 382 | parameter ids in the format of integer. |
---|
| 383 | ''' |
---|
| 384 | cpar = pars.upper().split('/') |
---|
| 385 | ipar = [] |
---|
| 386 | for par in cpar: |
---|
| 387 | for k, v in table.iteritems(): |
---|
| 388 | if par == k or par == v: |
---|
| 389 | ipar.append(int(k)) |
---|
| 390 | break |
---|
[ff99eae] | 391 | else: |
---|
[2fb99de] | 392 | print('Warning: par ' + par + ' not found in table 128') |
---|
[efdb01a] | 393 | |
---|
| 394 | return ipar |
---|
| 395 | |
---|
[ff99eae] | 396 | def get_list_as_string(list_obj, concatenate_sign=', '): |
---|
[efdb01a] | 397 | ''' |
---|
| 398 | @Description: |
---|
[991df6a] | 399 | Converts a list of arbitrary content into a single string. |
---|
| 400 | |
---|
| 401 | @Input: |
---|
[ff99eae] | 402 | list_obj: list |
---|
[991df6a] | 403 | A list with arbitrary content. |
---|
| 404 | |
---|
[ff99eae] | 405 | concatenate_sign: string, optional |
---|
| 406 | A string which is used to concatenate the single |
---|
| 407 | list elements. Default value is ", ". |
---|
| 408 | |
---|
[991df6a] | 409 | @Return: |
---|
[ff99eae] | 410 | str_of_list: string |
---|
[991df6a] | 411 | The content of the list as a single string. |
---|
[efdb01a] | 412 | ''' |
---|
[991df6a] | 413 | |
---|
[ff99eae] | 414 | str_of_list = concatenate_sign.join(str(l) for l in list_obj) |
---|
[991df6a] | 415 | |
---|
[ff99eae] | 416 | return str_of_list |
---|
[54a8a01] | 417 | |
---|
| 418 | def make_dir(directory): |
---|
| 419 | ''' |
---|
| 420 | @Description: |
---|
| 421 | Creates a directory and gives a warning if the directory |
---|
| 422 | already exists. The program stops only if there is another problem. |
---|
| 423 | |
---|
| 424 | @Input: |
---|
| 425 | directory: string |
---|
[efa05d7] | 426 | The directory name including the path which should be created. |
---|
[54a8a01] | 427 | |
---|
| 428 | @Return: |
---|
| 429 | <nothing> |
---|
| 430 | ''' |
---|
| 431 | try: |
---|
| 432 | os.makedirs(directory) |
---|
| 433 | except OSError as e: |
---|
| 434 | if e.errno != errno.EEXIST: |
---|
| 435 | # errno.EEXIST = directory already exists |
---|
| 436 | raise # re-raise exception if a different error occured |
---|
| 437 | else: |
---|
[2fb99de] | 438 | print('WARNING: Directory {0} already exists!'.format(directory)) |
---|
[54a8a01] | 439 | |
---|
| 440 | return |
---|
| 441 | |
---|
| 442 | def put_file_to_ecserver(ecd, filename, target, ecuid, ecgid): |
---|
| 443 | ''' |
---|
| 444 | @Description: |
---|
[97e09f4] | 445 | Uses the ecaccess-file-put command to send a file to the ECMWF servers. |
---|
| 446 | |
---|
| 447 | NOTE: |
---|
| 448 | The return value is just for testing reasons. It does not have |
---|
| 449 | to be used from the calling function since the whole error handling |
---|
| 450 | is done in here. |
---|
[54a8a01] | 451 | |
---|
| 452 | @Input: |
---|
| 453 | ecd: string |
---|
[97e09f4] | 454 | The path were the file is stored. |
---|
[54a8a01] | 455 | |
---|
| 456 | filename: string |
---|
| 457 | The name of the file to send to the ECMWF server. |
---|
| 458 | |
---|
| 459 | target: string |
---|
[97e09f4] | 460 | The target queue where the file should be sent to. |
---|
[54a8a01] | 461 | |
---|
| 462 | ecuid: string |
---|
| 463 | The user id on ECMWF server. |
---|
| 464 | |
---|
| 465 | ecgid: string |
---|
| 466 | The group id on ECMWF server. |
---|
| 467 | |
---|
| 468 | @Return: |
---|
[97e09f4] | 469 | rcode: string |
---|
| 470 | Resulting code of command execution. If successful the string |
---|
| 471 | will be empty. |
---|
[54a8a01] | 472 | ''' |
---|
| 473 | |
---|
| 474 | try: |
---|
[97e09f4] | 475 | rcode = subprocess.check_output(['ecaccess-file-put', |
---|
| 476 | ecd + '/' + filename, |
---|
| 477 | target + ':/home/ms/' + |
---|
| 478 | ecgid + '/' + ecuid + |
---|
| 479 | '/' + filename], |
---|
| 480 | stderr=subprocess.STDOUT) |
---|
[54a8a01] | 481 | except subprocess.CalledProcessError as e: |
---|
[2fb99de] | 482 | print('... ERROR CODE:\n ... ' + str(e.returncode)) |
---|
| 483 | print('... ERROR MESSAGE:\n ... ' + str(e)) |
---|
[54a8a01] | 484 | |
---|
[2fb99de] | 485 | print('\n... Do you have a valid ecaccess certification key?') |
---|
[97e09f4] | 486 | sys.exit('... ECACCESS-FILE-PUT FAILED!') |
---|
| 487 | |
---|
| 488 | return rcode |
---|
[54a8a01] | 489 | |
---|
[b1d07c9] | 490 | def submit_job_to_ecserver(target, jobname): |
---|
[54a8a01] | 491 | ''' |
---|
| 492 | @Description: |
---|
[97e09f4] | 493 | Uses ecaccess-job-submit command to submit a job to the ECMWF server. |
---|
| 494 | |
---|
| 495 | NOTE: |
---|
| 496 | The return value is just for testing reasons. It does not have |
---|
| 497 | to be used from the calling function since the whole error handling |
---|
| 498 | is done in here. |
---|
[54a8a01] | 499 | |
---|
| 500 | @Input: |
---|
| 501 | target: string |
---|
| 502 | The target where the file should be sent to, e.g. the queue. |
---|
| 503 | |
---|
| 504 | jobname: string |
---|
| 505 | The name of the jobfile to be submitted to the ECMWF server. |
---|
| 506 | |
---|
| 507 | @Return: |
---|
[97e09f4] | 508 | rcode: string |
---|
| 509 | Resulting code of command execution. If successful the string |
---|
| 510 | will contain an integer number, representing the id of the job |
---|
| 511 | at the ecmwf server. |
---|
[54a8a01] | 512 | ''' |
---|
| 513 | |
---|
| 514 | try: |
---|
[97e09f4] | 515 | rcode = subprocess.check_output(['ecaccess-job-submit', |
---|
| 516 | '-queueName', target, |
---|
| 517 | jobname]) |
---|
[54a8a01] | 518 | except subprocess.CalledProcessError as e: |
---|
[2fb99de] | 519 | print('... ERROR CODE:\n ... ' + str(e.returncode)) |
---|
| 520 | print('... ERROR MESSAGE:\n ... ' + str(e)) |
---|
| 521 | |
---|
[97e09f4] | 522 | |
---|
[2fb99de] | 523 | print('\n... Do you have a valid ecaccess certification key?') |
---|
[54a8a01] | 524 | sys.exit('... ECACCESS-JOB-SUBMIT FAILED!') |
---|
| 525 | |
---|
[451bb19] | 526 | return rcode |
---|