Changes in Source/Python/Mods/tools.py [026b359:75db9b0] in flex_extract.git


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Source/Python/Mods/tools.py

    r026b359 r75db9b0  
    1818#        - added documentation
    1919#        - moved all non class methods from former file Flexparttools in here
    20 #        - seperated args and control interpretation
     20#        - separated args and control interpretation
    2121#        - added functions get_list_as_string, read_ecenv, send_mail, make_dir,
    2222#          put_file_to_ecserver, submit_job_to_ecserver, get_informations,
    2323#          get_dimensions, execute_subprocess, none_or_int, none_or_str
     24#
     25#    August 2020 - Leopold Haimberger (University of Vienna)
     26#        - added function to check if a specific string is in a file
     27#        - added function to overwrite lines in a file which contain specific string
    2428#
    2529# @License:
     
    118122
    119123def none_or_str(value):
    120     '''Converts the input string into pythons None-type if the string
    121     contains string "None".
     124    '''Converts the input string into Pythons None type if it
     125    contains the string "None".
    122126
    123127    Parameters
     
    130134    None or value:
    131135        Return depends on the content of the input value. If it was "None",
    132         then the python type None is returned. Otherwise the string itself.
     136        then the Python type None is returned, otherwise the string itself.
    133137    '''
    134138    if value == 'None':
     
    137141
    138142def none_or_int(value):
    139     '''Converts the input string into pythons None-type if the string
    140     contains string "None". Otherwise it is converted to an integer value.
     143    '''Converts the input string into Pythons None-type if it
     144    contains string "None"; otherwise it is converted to an integer value.
    141145
    142146    Parameters
     
    158162def get_cmdline_args():
    159163    '''Decomposes the command line arguments and assigns them to variables.
    160     Apply default values for non mentioned arguments.
     164    Apply default values for arguments not present.
    161165
    162166    Parameters
     
    166170    ------
    167171    args : Namespace
    168         Contains the commandline arguments from script/program call.
     172        Contains the command line arguments from the script / program call.
    169173    '''
    170174
     
    191195    parser.add_argument("--basetime", dest="basetime",
    192196                        type=none_or_int, default=None,
    193                         help="base such as 0 or 12 (for half day retrievals)")
     197                        help="base time such as 0 or 12 (for half day retrievals)")
    194198    parser.add_argument("--step", dest="step",
    195199                        type=none_or_str, default=None,
     
    200204    parser.add_argument("--area", dest="area",
    201205                        type=none_or_str, default=None,
    202                         help="area defined as north/west/south/east")
     206                        help="area, defined by north/west/south/east")
    203207
    204208    # some switches
    205209    parser.add_argument("--debug", dest="debug",
    206210                        type=none_or_int, default=None,
    207                         help="debug mode - leave temporary files intact")
     211                        help="debug mode - temporary files will be conserved")
    208212    parser.add_argument("--oper", dest="oper",
    209213                        type=none_or_int, default=None,
    210                         help='operational mode - prepares dates with '
     214                        help='operational mode - prepares dates from '
    211215                        'environment variables')
    212216    parser.add_argument("--request", dest="request",
    213217                        type=none_or_int, default=None,
    214                         help="list all mars requests in file mars_requests.dat")
     218                        help="list all MARS requests in file mars_requests.dat")
    215219    parser.add_argument("--public", dest="public",
    216220                        type=none_or_int, default=None,
    217                         help="public mode - retrieves the public datasets")
     221                        help="public mode - retrieves public datasets")
    218222    parser.add_argument("--rrint", dest="rrint",
    219223                        type=none_or_int, default=None,
    220                         help='Selection of old or new precipitation '
    221                         'interpolation:\n'
     224                        help='Selection of old or new '
     225                        'interpolation method for precipitation:\n'
    222226                        '     0 - old method\n'
    223227                        '     1 - new method (additional subgrid points)')
     
    226230    parser.add_argument("--inputdir", dest="inputdir",
    227231                        type=none_or_str, default=None,
    228                         help='Path to the temporary directory for the '
    229                         'retrieval grib files and other processing files.')
     232                        help='Path to temporary directory for '
     233                        'retrieved grib files and other processing files.')
    230234    parser.add_argument("--outputdir", dest="outputdir",
    231235                        type=none_or_str, default=None,
    232                         help='Path to the final directory where the final '
    233                         'FLEXPART ready input files are stored.')
     236                        help='Path to final directory where '
     237                        'FLEXPART input files will be stored.')
    234238
    235239    # this is only used by prepare_flexpart.py to rerun a postprocessing step
    236240    parser.add_argument("--ppid", dest="ppid",
    237241                        type=none_or_str, default=None,
    238                         help='This is the specify parent process id of a '
     242                        help='This is the specify the parent process id of a '
    239243                        'single flex_extract run to identify the files. '
    240244                        'It is the second number in the GRIB files.')
     
    243247    parser.add_argument("--job_template", dest='job_template',
    244248                        type=none_or_str, default="job.temp",
    245                         help='The job template file which are adapted to be '
    246                         'submitted to the batch system on ECMWF server.')
     249                        help='Job template file. Will be used for submission '
     250                        'to the batch system on the ECMWF server after '
     251                        'modification.')
    247252    parser.add_argument("--queue", dest="queue",
    248253                        type=none_or_str, default=None,
    249                         help='The ECMWF server name for submission of the '
    250                         'job script to the batch system '
     254                        help='The name of the ECMWF server name where the'
     255                        'job script is to be submitted '
    251256                        '(e.g. ecgate | cca | ccb)')
    252257
     
    289294
    290295    It keeps the final FLEXPART input files if program runs without
    291     ECMWF Api and keywords "ectrans" or "ecstorage" are set to "1".
     296    ECMWF API and keywords "ectrans" or "ecstorage" are set to "1".
    292297
    293298    Parameters
     
    410415
    411416    This method combines the single characters of the passed arguments
    412     with each other. So that each character of each argument value
     417    with each other in a way that each character of each argument value
    413418    will be combined with each character of the other arguments as a tuple.
    414419
     
    519524    pars : str
    520525        Addpar argument from CONTROL file in the format of
    521         parameter names instead of ids. The parameter short
    522         names are sepearted with "/" and they are passed as
    523         one single string.
     526        parameter names instead of IDs. The parameter short
     527        names are separated by "/" and passed as one single string.
    524528
    525529    table : dict
     
    555559
    556560def to_param_id_with_tablenumber(pars, table):
    557     '''Transform parameter names to parameter ids and add table id.
     561    '''Transform parameter names to parameter IDs and add table ID.
    558562
    559563    Conversion with ECMWF grib table 128.
     
    563567    pars : str
    564568        Addpar argument from CONTROL file in the format of
    565         parameter names instead of ids. The parameter short
    566         names are sepearted with "/" and they are passed as
    567         one single string.
     569        parameter names instead of ID. The parameter short
     570        names are separated by "/" and passed as one single string.
    568571
    569572    table : dict
     
    576579    spar : str
    577580        List of addpar parameters from CONTROL file transformed to
    578         parameter ids in the format of integer.
     581        parameter IDs in the format of integer.
    579582    '''
    580583    if not pars:
     
    598601
    599602def get_list_as_string(list_obj, concatenate_sign=', '):
    600     '''Converts a list of arbitrary content into a single string.
     603    '''Converts a list of arbitrary content into a single string using a given
     604    concatenation character.
    601605
    602606    Parameters
     
    624628    '''Creates a directory.
    625629
    626     It gives a warning if the directory already exists and skips process.
    627     The program stops only if there is another problem.
     630    If the directory already exists, an information is printed and the creation
     631    skipped. The program stops only if there is another problem.
    628632
    629633    Parameters
     
    641645        # errno.EEXIST = directory already exists
    642646        if e.errno == errno.EEXIST:
    643             print('WARNING: Directory {0} already exists!'.format(directory))
     647            print('INFORMATION: Directory {0} already exists!'.format(directory))
    644648        else:
    645649            raise # re-raise exception if a different error occured
     
    720724    ------
    721725    job_id : int
    722         The id number of the job as a reference at the ecmwf server.
     726        The id number of the job as a reference at the ECMWF server.
    723727    '''
    724728
     
    732736
    733737        print('\n... Do you have a valid ecaccess certification key?')
    734         sys.exit('... ECACCESS-JOB-SUBMIT FAILED!')
     738        sys.exit('... ecaccess-job-submit FAILED!')
    735739    except OSError as e:
    736740        print('... ERROR CODE: ' + str(e.errno))
     
    738742
    739743        print('\n... Most likely the ECACCESS library is not available!')
    740         sys.exit('... ECACCESS-JOB-SUBMIT FAILED!')
     744        sys.exit('... ecaccess-job-submit FAILED!')
    741745
    742746    return job_id.decode()
     
    744748
    745749def get_informations(filename):
    746     '''Gets basic information from an example grib file.
    747 
    748     These information are important for later use and the
    749     initialization of numpy arrays for data storing.
     750    '''Extracts basic information from a sample grib file.
     751
     752    This information is needed for later use and the
     753    initialization of numpy arrays where data are stored.
    750754
    751755    Parameters
     
    768772
    769773    # --- open file ---
    770     print("Opening file for getting information data --- %s" % filename)
     774    print("Opening grib file for extraction of information --- %s" % filename)
    771775    with open(filename, 'rb') as f:
    772776        # load first message from file
     
    785789               ]
    786790
    787         print('\nInformations are: ')
     791        print('\nInformation extracted: ')
    788792        for key in keys:
    789793            # Get the value of the key in a grib message.
     
    798802
    799803def get_dimensions(info, purefc, dtime, index_vals, start_date, end_date):
    800     '''This function specifies the correct dimensions for x, y and t.
     804    '''This function specifies the correct dimensions for x, y, and t.
    801805
    802806    Parameters
     
    817821    index_vals : list of list of str
    818822        Contains the values from the keys used for a distinct selection
    819         of grib messages in processing the grib files.
     823        of GRIB messages in processing the grib files.
    820824        Content looks like e.g.:
    821825        index_vals[0]: ('20171106', '20171107', '20171108') ; date
     
    849853
    850854def execute_subprocess(cmd_list, error_msg='SUBPROCESS FAILED!'):
    851     '''Executes a command line instruction via a subprocess.
     855    '''Executes a command via a subprocess.
    852856
    853857    Error handling is done if an error occures.
     
    856860    ----------
    857861    cmd_list : list of str
    858         A list of the components for the command line execution. Each
    859         list entry is a single part of the command which is seperated from
    860         the rest by a blank space.
    861         E.g. ['mv', file1, file2]
     862        A list of the components for the command line execution.
     863        They will be concatenated with blank space for the command
     864        to be submitted, like ['mv', file1, file2] for mv file1 file2.
    862865
    863866    Return
    864867    ------
    865868    error_msg : str, optional
    866         The possible error message if the subprocess failed.
    867         By default it will just tell "SUBPROCESS FAILED!".
     869        Error message if the subprocess fails.
     870        By default it will just say "SUBPROCESS FAILED!".
    868871    '''
    869872
     
    911914
    912915    return start_period, end_period
     916
     917
     918def check_for_string_in_file(filepath, search_string):
     919    """
     920    Search for a specific string in a file and return True if
     921    the string was found.
     922
     923    Parameters
     924    ----------
     925    filepath : str
     926        The full file path which is to be examined.
     927
     928    search_string : str
     929        The string which is looked up for in the file.
     930
     931    Return
     932    ------
     933    Boolean :
     934        True : String was found
     935        False : String was not found
     936    """
     937    with open(filepath, 'r') as fio:
     938        for line in fio:
     939            if search_string in line:
     940                return True
     941    return False
     942
     943
     944def overwrite_lines_in_file(filepath, search_string, sub_string):
     945    """
     946    Overwrites lines which contain the given search string with the
     947    substitution string.
     948
     949    Parameters
     950    ----------
     951    search_string : str
     952        The string which is looked up for in the file.
     953
     954    sub_string : str
     955        The string which overwrites the search string.
     956
     957    Return
     958    ------
     959    """
     960    with open(filepath, 'r') as fio:
     961        data = fio.readlines()
     962
     963    with open(filepath, 'w') as fio:
     964        for line in data:
     965            if search_string in line:
     966                fio.write(sub_string)
     967            else:
     968                fio.write(line)
     969
     970    return
     971
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG