Changeset 64cf353 in flex_extract.git for python/GribTools.py


Ignore:
Timestamp:
Feb 8, 2018, 9:54:05 PM (6 years ago)
Author:
Anne Philipp <bscannephilipp@…>
Branches:
master, ctbto, dev
Children:
02c8c50
Parents:
6180177
Message:

pep8 changes + documentation added + minor code style changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/GribTools.py

    rd69b677 r64cf353  
    1 #
    2 # (C) Copyright 2014 UIO.
    3 #
    4 # This software is licensed under the terms of the Apache Licence Version 2.0
    5 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
    6 #
    7 #
    8 # Creation: July 2014 - Anne Fouilloux - University of Oslo
    9 #
    10 #
    11 
     1#!/usr/bin/env python
     2# -*- coding: utf-8 -*-
     3#************************************************************************
     4# TODO AP
     5#AP
     6# - GribTools name möglicherweise etwas verwirrend.
     7# - change self.filename in self.filenames!!!
     8# -
     9#************************************************************************
     10"""
     11@Author: Anne Fouilloux (University of Oslo)
     12
     13@Date: July 2014
     14
     15@ChangeHistory:
     16   February 2018 - Anne Philipp (University of Vienna):
     17        - applied PEP8 style guide
     18        - added documentation
     19        - changed some naming
     20
     21@License:
     22    (C) Copyright 2014 UIO.
     23
     24    This software is licensed under the terms of the Apache Licence Version 2.0
     25    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
     26
     27@Requirements:
     28    - A standard python 2.6 or 2.7 installation
     29    - dateutils
     30    - matplotlib (optional, for debugging)
     31    - ECMWF specific packages, all available from https://software.ecmwf.int/
     32        ECMWF WebMARS, gribAPI with python enabled, emoslib and
     33        ecaccess web toolkit
     34
     35@Description:
     36    Further documentation may be obtained from www.flexpart.eu.
     37
     38    Functionality provided:
     39        Prepare input 3D-wind fields in hybrid coordinates +
     40        surface fields for FLEXPART runs
     41"""
     42# ------------------------------------------------------------------------------
     43# MODULES
     44# ------------------------------------------------------------------------------
    1245from gribapi import *
    1346import traceback
    14 import sys,os
    15 
    16 
    17 ##############################################################
    18 #  GRIB utilities
    19 ##############################################################
     47import sys, os
     48
     49# ------------------------------------------------------------------------------
     50# CLASS
     51# ------------------------------------------------------------------------------
    2052class GribTools:
    21     'class for GRIB API with new methods'
    22     def __init__(self,filename):
    23         self.filename=filename
    24    
    25 # get keyvalues for a given list of keynames
    26 # a where statment can be given (list of key and list of values)
    27 
    28     def getkeys(self,keynames,wherekeynames=[],wherekeyvalues=[]):
    29         fileid=open(self.filename,'r')
    30 
    31         return_list=[]
    32        
     53    '''
     54    Class for GRIB utilities (new methods) based on GRIB API
     55    '''
     56    # --------------------------------------------------------------------------
     57    # FUNCTIONS
     58    # --------------------------------------------------------------------------
     59    def __init__(self, filenames):
     60        '''
     61        @Description:
     62            Initialise an object of GribTools and assign a list
     63            of filenames.
     64
     65        @Input:
     66            filenames: list of strings
     67                A list of filenames.
     68
     69        @Return:
     70            <nothing>
     71        '''
     72
     73        self.filename = filenames
     74
     75        return
     76
     77
     78    def getkeys(self, keynames, wherekeynames=[], wherekeyvalues=[]):
     79        '''
     80        @Description:
     81            get keyvalues for a given list of keynames
     82            a where statement can be given (list of key and list of values)
     83
     84        @Input:
     85            keynames: list of strings
     86                List of keynames.
     87
     88            wherekeynames: list of ???
     89                Default value is an empty list.
     90
     91            wherekeyvalues: list of ???
     92                Default value is an empty list.
     93
     94        @Return:
     95            return_list: list of strings
     96                List of keyvalues for given keynames.
     97        '''
     98
     99        fileid = open(self.filename, 'r')
     100
     101        return_list = []
     102
    33103        while 1:
    34             gid_in = grib_new_from_file(fileid) 
    35             if gid_in is None: break
    36             select=True
    37             i=0
    38             if len(wherekeynames) != len(wherekeyvalues): raise Exception("Give a value for each keyname!")
    39 
     104            gid_in = grib_new_from_file(fileid)
     105
     106            if gid_in is None:
     107                break
     108
     109            if len(wherekeynames) != len(wherekeyvalues):
     110                raise Exception("Number of key values and key names must be \
     111                                 the same. Give a value for each keyname!")
     112
     113            select = True
     114            i = 0
    40115            for wherekey in wherekeynames:
    41                 if not grib_is_defined(gid_in, wherekey): raise Exception("where Key was not defined")
    42                 select=select and (str(wherekeyvalues[i])==str(grib_get(gid_in, wherekey)))
    43                 i=i+1
     116                if not grib_is_defined(gid_in, wherekey):
     117                    raise Exception("where key was not defined")
     118
     119                select = (select and (str(wherekeyvalues[i]) ==
     120                                      str(grib_get(gid_in, wherekey))))
     121                i += 1
     122
    44123            if select:
    45124                llist = []
     
    47126                    llist.extend([str(grib_get(gid_in, key))])
    48127                return_list.append(llist)
     128
    49129            grib_release(gid_in)
     130
    50131        fileid.close()
     132
    51133        return return_list
    52      
    53 # set keyvalues for a given list of keynames
    54 # a where statment can be given (list of key and list of values)
    55 # an input file must be given as an input for reading grib messages
    56 # note that by default all messages are written out
    57 # if you want to get only those meeting the where statement, use
    58 # strict=true
    59     def setkeys(self,fromfile,keynames,keyvalues, wherekeynames=[],wherekeyvalues=[], strict=False, filemode='w'):
    60         fout=open(self.filename,filemode)
    61         fin=open(fromfile)
    62        
     134
     135
     136    def setkeys(self, fromfile, keynames, keyvalues, wherekeynames=[],
     137                wherekeyvalues=[], strict=False, filemode='w'):
     138        '''
     139        @Description:
     140            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).
     145
     146        @Input:
     147            fromfile: string
     148                Filename of the input file to read the grib messages from.
     149
     150            keynames: list of ???
     151                List of keynames. Default is an empty list.
     152
     153            keyvalues: list of ???
     154                List of keynames. Default is an empty list.
     155
     156            wherekeynames: list of ???
     157                Default value is an empty list.
     158
     159            wherekeyvalues: list of ???
     160                Default value is an empty list.
     161
     162            strict: boolean
     163                Decides if everything from keynames and keyvalues
     164                is written out the grib file (False) or only those
     165                meeting the where statement (True). Default is False.
     166
     167            filemode:
     168                Sets the mode for the output file. Default is "w".
     169
     170        @Return:
     171            <nothing>
     172
     173        '''
     174        fout = open(self.filename, filemode)
     175        fin = open(fromfile)
     176
    63177        while 1:
    64             gid_in = grib_new_from_file(fin) 
    65             if gid_in is None: break
    66            
    67             select=True
    68             i=0
    69             if len(wherekeynames) != len(wherekeyvalues): raise Exception("Give a value for each keyname!")
    70 
     178            gid_in = grib_new_from_file(fin)
     179
     180            if gid_in is None:
     181                break
     182
     183            if len(wherekeynames) != len(wherekeyvalues):
     184                raise Exception("Give a value for each keyname!")
     185
     186            select = True
     187            i = 0
    71188            for wherekey in wherekeynames:
    72                 if not grib_is_defined(gid_in, wherekey): raise Exception("where Key was not defined")
    73                 select=select and (str(wherekeyvalues[i])==str(grib_get(gid_in, wherekey)))
    74                 i=i+1
     189                if not grib_is_defined(gid_in, wherekey):
     190                    raise Exception("where Key was not defined")
     191
     192                select = (select and (str(wherekeyvalues[i]) ==
     193                                      str(grib_get(gid_in, wherekey))))
     194                i += 1
     195
     196#AP is it secured that the order of keynames is equal to keyvalues?
    75197            if select:
    76                 i=0
     198                i = 0
    77199                for key in keynames:
    78200                    grib_set(gid_in, key, keyvalues[i])
    79                     i=i+1
     201                    i += 1
     202
     203#AP this is a redundant code section
     204# delete the if/else :
     205#
     206#           grib_write(gid_in, fout)
     207#
    80208            if strict:
    81209                if select:
    82                     grib_write(gid_in,fout)
     210                    grib_write(gid_in, fout)
    83211            else:
    84                 grib_write(gid_in,fout)
     212                grib_write(gid_in, fout)
     213#AP end
    85214            grib_release(gid_in)
     215
    86216        fin.close()
    87217        fout.close()
    88218
    89 # Add the content of a grib file but only messages
    90 # corresponding to keys/values       
    91 # if selectWhere is False select fields that are different from keynames/keyvalues
    92     def copy(self,filename_in, selectWhere=True, keynames=[], keyvalues=[],filemode='w'):
    93         fin=open(filename_in)
    94         fout=open(self.filename,filemode)
    95        
     219        return
     220
     221    def copy(self, filename_in, selectWhere=True,
     222             keynames=[], keyvalues=[], filemode='w'):
     223        '''
     224        Add the content of another input grib file to the objects file but
     225        only messages corresponding to keys/values passed to the function.
     226        The selectWhere switch decides if to copy the keys equal to (True) or
     227        different to (False) the keynames/keyvalues list passed to the function.
     228
     229        @Input:
     230            filename_in: string
     231                Filename of the input file to read the grib messages from.
     232
     233            selectWhere: boolean
     234                Decides if to copy the keynames and values equal to (True) or
     235                different to (False) the keynames/keyvalues list passed to the
     236                function. Default is True.
     237
     238            keynames: list of ???
     239                List of keynames. Default is an empty list.
     240
     241            keyvalues: list of ???
     242                List of keynames. Default is an empty list.
     243
     244            filemode:
     245                Sets the mode for the output file. Default is "w".
     246
     247        @Return:
     248            <nothing>
     249        '''
     250
     251        fin = open(filename_in)
     252        fout = open(self.filename, filemode)
     253
    96254        while 1:
    97             gid_in = grib_new_from_file(fin) 
    98             if gid_in is None: break
    99            
    100             select=True
    101             i=0
    102             if len(keynames) != len(keyvalues): raise Exception("Give a value for each keyname!")
    103 
     255            gid_in = grib_new_from_file(fin)
     256
     257            if gid_in is None:
     258                break
     259
     260            if len(keynames) != len(keyvalues):
     261                raise Exception("Give a value for each keyname!")
     262
     263            select = True
     264            i = 0
    104265            for key in keynames:
    105                 if not grib_is_defined(gid_in, key): raise Exception("Key was not defined")
    106                
     266                if not grib_is_defined(gid_in, key):
     267                    raise Exception("Key was not defined")
     268
    107269                if selectWhere:
    108                     select=select and (str(keyvalues[i])==str(grib_get(gid_in, key)))
     270                    select = (select and (str(keyvalues[i]) ==
     271                                          str(grib_get(gid_in, key))))
    109272                else:
    110                     select=select and (str(keyvalues[i])!=str(grib_get(gid_in, key)))
    111                 i=i+1
     273                    select = (select and (str(keyvalues[i]) !=
     274                                          str(grib_get(gid_in, key))))
     275                i += 1
     276
    112277            if select:
    113                 grib_write(gid_in,fout)
     278                grib_write(gid_in, fout)
     279
    114280            grib_release(gid_in)
     281
    115282        fin.close()
    116283        fout.close()
    117284
    118 # Create index from a list of files if it does not exist or read it
    119     def index(self,index_keys=["mars"], index_file = "my.idx"):
    120         print "index to be done"
     285        return
     286
     287    def index(self, index_keys=["mars"], index_file="my.idx"):
     288        '''
     289        @Description:
     290            Create index from a list of files if it does not exist or
     291            read an index file.
     292
     293        @Input:
     294            index_keys: list of ???
     295                Default is a list with a single entry string "mars".
     296
     297            index_file: string
     298                Filename where the indices are stored.
     299                Default is "my.idx".
     300
     301        @Return:
     302            iid: integer
     303                Grib index id.
     304        '''
     305        print("... index will be done")
    121306        self.iid = None
    122  
     307
    123308        if (os.path.exists(index_file)):
    124309            self.iid = grib_index_read(index_file)
    125             print "Use existing index file: %s "%(index_file)
     310            print("Use existing index file: %s " % (index_file))
    126311        else:
     312#AP does the for loop overwrite the iid all the time?
    127313            for file in self.filename:
    128                 print "Inputfile: %s "%(file)
    129                 if self.iid == None:
    130                     self.iid = grib_index_new_from_file(file,index_keys)
     314                print("Inputfile: %s " % (file))
     315                if self.iid is None:
     316                    self.iid = grib_index_new_from_file(file, index_keys)
    131317                else:
    132                      grib_index_add_file(self.iid,file)
    133 
    134             if self.iid != None:
    135               grib_index_write(self.iid,index_file)
    136         return self.iid
    137 
    138 
    139        
    140 
    141        
     318                    grib_index_add_file(self.iid, file)
     319#AP or does the if has to be in the for loop?
     320#AP would make more sense?
     321            if self.iid is not None:
     322                grib_index_write(self.iid, index_file)
     323
     324        return self.iid
     325
     326
     327
     328
     329
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG