Changeset ff99eae in flex_extract.git for python/GribTools.py


Ignore:
Timestamp:
Jun 1, 2018, 8:34:59 PM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
e1228f3
Parents:
ccab809
Message:

completed application of pep8 style guide and pylint investigations. added documentation almost everywhere

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/GribTools.py

    rccab809 rff99eae  
    11#!/usr/bin/env python
    22# -*- coding: utf-8 -*-
    3 #************************************************************************
    4 # TODO AP
    5 # - GribTools name möglicherweise etwas verwirrend.
    6 # - change self.filename in self.filenames!!!
    7 # - bis auf --init-- und index wird keine Funktion verwendet!?
    8 #************************************************************************
    93#*******************************************************************************
    104# @Author: Anne Fouilloux (University of Oslo)
     
    3630# @Class Content:
    3731#    - __init__
    38 #    - getkeys
    39 #    - setkeys
     32#    - get_keys
     33#    - set_keys
    4034#    - copy
    4135#    - index
     36#
     37# @Class Attributes:
     38#    - filenames
    4239#
    4340#*******************************************************************************
     
    4744# ------------------------------------------------------------------------------
    4845import os
    49 from gribapi import *
     46from gribapi import grib_new_from_file, grib_is_defined, grib_get, \
     47                    grib_release, grib_set, grib_write, grib_index_read, \
     48                    grib_index_new_from_file, grib_index_add_file,  \
     49                    grib_index_write
    5050
    5151# ------------------------------------------------------------------------------
    5252# CLASS
    5353# ------------------------------------------------------------------------------
    54 class GribTools:
     54class Gribtools(object):
    5555    '''
    5656    Class for GRIB utilities (new methods) based on GRIB API
     
    6262        '''
    6363        @Description:
    64             Initialise an object of GribTools and assign a list
     64            Initialise an object of Gribtools and assign a list
    6565            of filenames.
    6666
     
    7373        '''
    7474
    75         self.filename = filenames
     75        self.filenames = filenames
    7676
    7777        return
    7878
    7979
    80     def getkeys(self, keynames, wherekeynames=[], wherekeyvalues=[]):
     80    def get_keys(self, keynames, wherekeynames=[], wherekeyvalues=[]):
    8181        '''
    8282        @Description:
     
    8888                List of keynames.
    8989
    90             wherekeynames: list of ???, optional
     90            wherekeynames: list of strings, optional
    9191                Default value is an empty list.
    9292
    93             wherekeyvalues: list of ???, optional
     93            wherekeyvalues: list of strings, optional
    9494                Default value is an empty list.
    9595
     
    9999        '''
    100100
    101         fileid = open(self.filename, 'r')
     101        fileid = open(self.filenames, 'r')
    102102
    103103        return_list = []
     
    136136
    137137
    138     def setkeys(self, fromfile, keynames, keyvalues, wherekeynames=[],
    139                 wherekeyvalues=[], strict=False, filemode='w'):
     138    def set_keys(self, fromfile, keynames, keyvalues, wherekeynames=[],
     139                 wherekeyvalues=[], strict=False, filemode='w'):
    140140        '''
    141141        @Description:
     
    150150                Filename of the input file to read the grib messages from.
    151151
    152             keynames: list of ???
     152            keynames: list of strings
    153153                List of keynames. Default is an empty list.
    154154
    155             keyvalues: list of ???
     155            keyvalues: list of strings
    156156                List of keynames. Default is an empty list.
    157157
    158             wherekeynames: list of ???, optional
     158            wherekeynames: list of strings, optional
    159159                Default value is an empty list.
    160160
    161             wherekeyvalues: list of ???, optional
     161            wherekeyvalues: list of strings, optional
    162162                Default value is an empty list.
    163163
     
    174174
    175175        '''
    176         fout = open(self.filename, filemode)
     176        fout = open(self.filenames, filemode)
    177177        fin = open(fromfile)
    178178
     
    196196                i += 1
    197197
    198 #AP is it secured that the order of keynames is equal to keyvalues?
    199198            if select:
    200199                i = 0
     
    203202                    i += 1
    204203
    205 #AP this is a redundant code section
    206 # delete the if/else :
    207 #
    208 #           grib_write(gid_in, fout)
    209 #
    210             if strict:
    211                 if select:
    212                     grib_write(gid_in, fout)
    213             else:
    214                 grib_write(gid_in, fout)
    215 #AP end
     204            grib_write(gid_in, fout)
     205
    216206            grib_release(gid_in)
    217207
     
    238228                function. Default is True.
    239229
    240             keynames: list of ???, optional
     230            keynames: list of strings, optional
    241231                List of keynames. Default is an empty list.
    242232
    243             keyvalues: list of ???, optional
     233            keyvalues: list of strings, optional
    244234                List of keynames. Default is an empty list.
    245235
     
    252242
    253243        fin = open(filename_in)
    254         fout = open(self.filename, filemode)
     244        fout = open(self.filenames, filemode)
    255245
    256246        while 1:
     
    307297                Grib index id.
    308298        '''
    309         print("... index will be done")
    310         self.iid = None
    311 
    312         if (os.path.exists(index_file)):
    313             self.iid = grib_index_read(index_file)
    314             print("Use existing index file: %s " % (index_file))
     299        print "... index will be done"
     300        iid = None
     301
     302        if os.path.exists(index_file):
     303            iid = grib_index_read(index_file)
     304            print "Use existing index file: %s " % (index_file)
    315305        else:
    316             for file in self.filename:
    317                 print("Inputfile: %s " % (file))
    318                 if self.iid is None:
    319                     self.iid = grib_index_new_from_file(file, index_keys)
     306            for filename in self.filenames:
     307                print "Inputfile: %s " % (filename)
     308                if iid is None:
     309                    iid = grib_index_new_from_file(filename, index_keys)
    320310                else:
    321311                    print 'in else zweig'
    322                     grib_index_add_file(self.iid, file)
    323 
    324             if self.iid is not None:
    325                 grib_index_write(self.iid, index_file)
    326 
    327         print('... index done')
    328 
    329         return self.iid
    330 
    331 
    332 
    333 
    334 
     312                    grib_index_add_file(iid, filename)
     313
     314            if iid is not None:
     315                grib_index_write(iid, index_file)
     316
     317        print '... index done'
     318
     319        return iid
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG