Changeset 268ee86 in flex_extract.git for source/python/classes/GribUtil.py


Ignore:
Timestamp:
Dec 13, 2018, 11:51:17 PM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
145fbe0
Parents:
fdfaf00
Message:

renamed class; added some docstring information for input parameters; added a function to read a dummy grib message

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/python/classes/GribUtil.py

    r702cbb7 r268ee86  
    3232#    - get_keys
    3333#    - set_keys
    34 #    - copy
     34#    - copy_dummy_msg
    3535#    - index
    3636#
     
    4848                     codes_release, codes_set, codes_write, codes_index_read,
    4949                     codes_index_new_from_file, codes_index_add_file,
    50                      codes_index_write)
     50                     codes_index_write, codes_set_values)
    5151
    5252# ------------------------------------------------------------------------------
    5353# CLASS
    5454# ------------------------------------------------------------------------------
    55 class GribTools(object):
     55class GribUtil(object):
    5656    '''
    5757    Class for GRIB utilities (new methods) based on GRIB API
     
    6161    # --------------------------------------------------------------------------
    6262    def __init__(self, filenames):
    63         '''Initialise an object of GribTools and assign a list of filenames.
     63        '''Initialise an object of GribUtil and assign a list of filenames.
    6464
    6565        Parameters
     
    139139                 wherekeyvalues=[], strict=False, filemode='w'):
    140140        '''Opens the file to read the grib messages and then write
    141         them to a new output file. By default all messages are
    142         written out. Also, the keyvalues of the passed list of
    143         keynames are set or only those meeting the where statement.
    144         (list of key and list of values).
     141        the selected messages (with wherekeys) to a new output file.
     142        Also, the keyvalues of the passed list of keynames are set.
    145143
    146144        Parameters
     
    150148
    151149        keynames : :obj:`list` of :obj:`string`
    152             List of keynames. Default is an empty list.
     150            List of keynames to set in the selected messages.
     151            Default is an empty list.
    153152
    154153        keyvalues : :obj:`list` of :obj:`string`
    155             List of keynames. Default is an empty list.
     154            List of keyvalues to set in the selected messages.
     155            Default is an empty list.
    156156
    157157        wherekeynames : :obj:`list` of :obj:`string`, optional
     158            List of keynames to select correct message.
    158159            Default value is an empty list.
    159160
    160161        wherekeyvalues : :obj:`list` of :obj:`string`, optional
     162            List of keyvalues for keynames to select correct message.
    161163            Default value is an empty list.
    162164
     
    173175
    174176        '''
     177        if len(wherekeynames) != len(wherekeyvalues):
     178            raise Exception("Give a value for each keyname!")
     179
    175180        fout = open(self.filenames, filemode)
    176181        fin = open(fromfile)
    177182
    178183        while 1:
    179             gid = codes_new_from_file(fin)
     184            gid = codes_grib_new_from_file(fin)
    180185
    181186            if gid is None:
    182187                break
    183 
    184             if len(wherekeynames) != len(wherekeyvalues):
    185                 raise Exception("Give a value for each keyname!")
    186188
    187189            select = True
     
    189191            for wherekey in wherekeynames:
    190192                if not codes_is_defined(gid, wherekey):
    191                     raise Exception("where Key was not defined")
     193                    raise Exception("wherekey was not defined")
    192194
    193195                select = (select and (str(wherekeyvalues[i]) ==
     
    198200                i = 0
    199201                for key in keynames:
    200                     codes_set(gid, key, keyvalues[i])
     202                    if key == 'values':
     203                        codes_set_values(gid, keyvalues[i])
     204                    else:
     205                        codes_set(gid, key, keyvalues[i])
    201206                    i += 1
    202207
    203             codes_write(gid, fout)
     208                codes_write(gid, fout)
    204209
    205210            codes_release(gid)
     
    210215        return
    211216
    212     def copy(self, filename_in, selectWhere=True,
    213              keynames=[], keyvalues=[], filemode='w'):
     217    def copy_dummy_msg(self, filename_in, selectWhere=True,
     218                 keynames=[], keyvalues=[], filemode='w'):
    214219        '''Add the content of another input grib file to the objects file but
    215220        only messages corresponding to keys/values passed to the function.
     
    231236
    232237        keyvalues : :obj:`list` of :obj:`string`, optional
    233             List of keynames. Default is an empty list.
     238            List of keyvalues. Default is an empty list.
    234239
    235240        filemode : :obj:`string`, optional
     
    240245
    241246        '''
     247        if len(keynames) != len(keyvalues):
     248            raise Exception("Give a value for each keyname!")
    242249
    243250        fin = open(filename_in)
    244251        fout = open(self.filenames, filemode)
    245252
    246         while 1:
    247             gid = codes_new_from_file(fin)
     253        fields = 0
     254
     255        while fields < 1:
     256            gid = codes_grib_new_from_file(fin)
    248257
    249258            if gid is None:
    250259                break
    251 
    252             if len(keynames) != len(keyvalues):
    253                 raise Exception("Give a value for each keyname!")
    254260
    255261            select = True
     
    268274
    269275            if select:
     276                fields = fields + 1
    270277                codes_write(gid, fout)
    271278
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG