Changeset 095dc73 in flex_extract.git


Ignore:
Timestamp:
Dec 7, 2018, 3:23:56 PM (10 months ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
dev
Children:
d2febd4
Parents:
b0cf1a6
Message:

added two new functions for grib information and updated commandline parameter for argparser

File:
1 edited

Legend:

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

    r97f4f4c r095dc73  
    5555import traceback
    5656import exceptions
     57from datetime import datetime
    5758from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    5859
     
    117118                            formatter_class=ArgumentDefaultsHelpFormatter)
    118119
    119     # the most important arguments
     120    # control parameters that override control file values
    120121    parser.add_argument("--start_date", dest="start_date",
    121122                        type=none_or_str, default=None,
     
    130131                        type=none_or_str, default='CONTROL.temp',
    131132                        help="file with CONTROL parameters")
    132 
    133     # parameter for extra output information
     133    parser.add_argument("--basetime", dest="basetime",
     134                        type=none_or_int, default=None,
     135                        help="base such as 00 or 12 (for half day retrievals)")
     136    parser.add_argument("--step", dest="step",
     137                        type=none_or_str, default=None,
     138                        help="steps such as 00/to/48")
     139    parser.add_argument("--levelist", dest="levelist",
     140                        type=none_or_str, default=None,
     141                        help="Vertical levels to be retrieved, e.g. 30/to/60")
     142    parser.add_argument("--area", dest="area",
     143                        type=none_or_str, default=None,
     144                        help="area defined as north/west/south/east")
     145
     146    # some switches
    134147    parser.add_argument("--debug", dest="debug",
    135148                        type=none_or_int, default=None,
     
    137150    parser.add_argument("--request", dest="request",
    138151                        type=none_or_int, default=None,
    139                         help="list all mars request in file mars_requests.dat \
    140                         and skip submission to mars")
     152                        help="list all mars requests in file mars_requests.dat")
    141153    parser.add_argument("--public", dest="public",
    142154                        type=none_or_int, default=None,
    143155                        help="public mode - retrieves the public datasets")
    144 
    145     # some arguments that override the default in the CONTROL file
    146     parser.add_argument("--basetime", dest="basetime",
     156    parser.add_argument("--rrint", dest="rrint",
    147157                        type=none_or_int, default=None,
    148                         help="base such as 00 or 12 (for half day retrievals)")
    149     parser.add_argument("--step", dest="step",
    150                         type=none_or_str, default=None,
    151                         help="steps such as 00/to/48")
    152     parser.add_argument("--levelist", dest="levelist",
    153                         type=none_or_str, default=None,
    154                         help="Vertical levels to be retrieved, e.g. 30/to/60")
    155     parser.add_argument("--area", dest="area",
    156                         type=none_or_str, default=None,
    157                         help="area defined as north/west/south/east")
    158 
    159     # set the working directories
     158                        help="select old or new precipitation interpolation \
     159                        0 - old method\
     160                        1 - new method (additional subgrid points)")
     161
     162    # set directories
    160163    parser.add_argument("--inputdir", dest="inputdir",
    161164                        type=none_or_str, default=None,
     
    634637
    635638    return job_id
     639
     640
     641def get_informations(filename):
     642    '''Gets basic information from an example grib file.
     643
     644    These information are important for later use and the
     645    initialization of numpy arrays for data storing.
     646
     647    Parameters
     648    ----------
     649    filename : :obj:`string`
     650            Name of the file which will be opened to extract basic information.
     651
     652    Return
     653    ------
     654    data : :obj:`dictionary`
     655        Contains basic informations of the ECMWF grib files, e.g.
     656        'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees',
     657        'longitudeOfFirstGridPointInDegrees', 'latitudeOfLastGridPointInDegrees',
     658        'longitudeOfLastGridPointInDegrees', 'jDirectionIncrementInDegrees',
     659        'iDirectionIncrementInDegrees', 'missingValue'
     660    '''
     661    from eccodes import *
     662
     663    data = {}
     664
     665    # --- open file ---
     666    print("Opening file for getting information data --- %s" % filename)
     667    with open(filename) as f:
     668        # load first message from file
     669        gid = codes_grib_new_from_file(f)
     670
     671        # information needed from grib message
     672        keys = [
     673                'Ni',
     674                'Nj',
     675                'latitudeOfFirstGridPointInDegrees',
     676                'longitudeOfFirstGridPointInDegrees',
     677                'latitudeOfLastGridPointInDegrees',
     678                'longitudeOfLastGridPointInDegrees',
     679                'jDirectionIncrementInDegrees',
     680                'iDirectionIncrementInDegrees',
     681                'missingValue',
     682               ]
     683
     684        print('\nInformations are: ')
     685        for key in keys:
     686            # Get the value of the key in a grib message.
     687            data[key] = codes_get(gid,key)
     688            print("%s = %s" % (key,data[key]))
     689
     690        # Free the memory for the message referred as gribid.
     691        codes_release(gid)
     692
     693    return data
     694
     695
     696def get_dimensions(c, info):
     697    '''This function specifies the correct dimensions for x, y and t.
     698
     699    Parameters
     700    ----------
     701    c : :obj:`ControlFile`
     702        Contains all the parameters of CONTROL file and
     703        command line.
     704
     705    info : :obj:`dictionary`
     706        Contains basic informations of the ECMWF grib files, e.g.
     707        'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees',
     708        'longitudeOfFirstGridPointInDegrees', 'latitudeOfLastGridPointInDegrees',
     709        'longitudeOfLastGridPointInDegrees', 'jDirectionIncrementInDegrees',
     710        'iDirectionIncrementInDegrees', 'missingValue'
     711
     712    Return
     713    ------
     714    (ix, jy, it) : :obj:`tuple` of :obj:`integer`
     715        Dimension in x-direction, y-direction and in time.
     716    '''
     717
     718    ix = info['Ni']
     719
     720    jy = info['Nj']
     721
     722    start = datetime.strptime(c.start_date, '%Y%m%d')
     723    end = datetime.strptime(c.end_date, '%Y%m%d')
     724    it = ((end - start).days + 1) * 24/int(c.dtime)
     725    print 'it', it, (end - start).days, 24/int(c.dtime)
     726
     727    return (ix, jy, it)
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG