Changeset f20af73 in flex_extract.git for source/python/mods/tools.py


Ignore:
Timestamp:
Mar 8, 2019, 10:05:20 AM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
82c2959
Parents:
b4a4777
Message:

added CDS API support for ERA5 instead of ECMWFAPI / refactored setup of CONTROL parameter because for local version it wanted to use not installed ECMWF_ENV file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/python/mods/tools.py

    r8778c5a rf20af73  
    6565import subprocess
    6666import traceback
    67 import exceptions
     67try:
     68    import exceptions
     69except ImportError:
     70    import builtins as exceptions
    6871from datetime import datetime
    6972from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
     73
     74
    7075
    7176# ------------------------------------------------------------------------------
    7277# METHODS
    7378# ------------------------------------------------------------------------------
     79
     80def setup_controldata():
     81    '''Collects, stores and checks controlling arguments from command line,
     82    CONTROL file and ECMWF_ENV file.
     83
     84    Parameters
     85    ----------
     86
     87    Return
     88    ------
     89    c : ControlFile
     90        Contains all the parameters of CONTROL file and
     91        command line.
     92
     93    ppid : str
     94        Parent process id.
     95
     96    queue : str
     97        Name of queue for submission to ECMWF (e.g. ecgate or cca )
     98
     99    job_template : str
     100        Name of the job template file for submission to ECMWF server.
     101    '''
     102    import _config
     103    from classes.ControlFile import ControlFile
     104
     105    args = get_cmdline_args()
     106    c = ControlFile(args.controlfile)
     107    c.assign_args_to_control(args)
     108    if os.path.isfile(_config.PATH_ECMWF_ENV):
     109        env_parameter = read_ecenv(_config.PATH_ECMWF_ENV)
     110        c.assign_envs_to_control(env_parameter)
     111    c.check_conditions(args.queue)
     112
     113    return c, args.ppid, args.queue, args.job_template
    74114
    75115def none_or_str(value):
     
    259299    print("... clean inputdir!")
    260300
    261     cleanlist = glob.glob(os.path.join(c.inputdir, "*"))
     301    cleanlist = [file for file in glob.glob(os.path.join(c.inputdir, "*"))
     302                 if not os.path.basename(file).startswith(c.prefix)]
    262303
    263304    if cleanlist:
    264305        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)
     306            silent_remove(element)
    269307        print("... done!")
    270308    else:
     
    274312
    275313
    276 def my_error(users, message='ERROR'):
     314def my_error(message='ERROR'):
    277315    '''Prints a specified error message which can be passed to the function
    278316    before exiting the program.
     
    280318    Parameters
    281319    ----------
    282     user : list of str
    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.
    286 
    287320    message : str, optional
    288321        Error message. Default value is "ERROR".
     
    464497    else:
    465498        for data in fdata:
    466             if data[0] != '!':
    467                 table128[data[0:3]] = data[59:64].strip()
     499            if data != '' and data[0] != '!':
     500                table128[data[0:3]] = data[59:65].strip()
    468501
    469502    return table128
     
    508541
    509542    return ipar
     543
     544def to_param_id_with_tablenumber(pars, table):
     545    '''Transform parameter names to parameter ids and add table id.
     546
     547    Conversion with ECMWF grib table 128.
     548
     549    Parameters
     550    ----------
     551    pars : str
     552        Addpar argument from CONTROL file in the format of
     553        parameter names instead of ids. The parameter short
     554        names are sepearted with "/" and they are passed as
     555        one single string.
     556
     557    table : dict
     558        Contains the ECMWF grib table 128 information.
     559        The key is the parameter number and the value is the
     560        short name of the parameter.
     561
     562    Return
     563    ------
     564    spar : str
     565        List of addpar parameters from CONTROL file transformed to
     566        parameter ids in the format of integer.
     567    '''
     568    if not pars:
     569        return []
     570    if not isinstance(pars, str):
     571        pars=str(pars)
     572
     573    cpar = pars.upper().split('/')
     574    spar = []
     575    for par in cpar:
     576        for k, v in table.iteritems():
     577            if par == k or par == v:
     578                spar.append(k + '.128')
     579                break
     580        else:
     581            print('\n\n\t\tWarning: par ' + par + ' not found in table 128\n\n')
     582
     583    return '/'.join(spar)
    510584
    511585def get_list_as_string(list_obj, concatenate_sign=', '):
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG