source: flex_extract.git/python/GribTools.py @ ccab809

ctbtodev
Last change on this file since ccab809 was ccab809, checked in by Anne Philipp <anne.philipp@…>, 6 years ago

further improved plotting routine for date selection

  • Property mode set to 100644
File size: 10.3 KB
Line 
1#!/usr/bin/env python
2# -*- 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#************************************************************************
9#*******************************************************************************
10# @Author: Anne Fouilloux (University of Oslo)
11#
12# @Date: July 2014
13#
14# @Change History:
15#   February 2018 - Anne Philipp (University of Vienna):
16#        - applied PEP8 style guide
17#        - added documentation
18#        - changed some naming
19#
20# @License:
21#    (C) Copyright 2014-2018.
22#
23#    This software is licensed under the terms of the Apache Licence Version 2.0
24#    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
25#
26# @Class Description:
27#    The GRIB API provides all necessary tools to work directly with the
28#    grib files. Nevertheless, the GRIB API tools are very basic and are in
29#    direct connection with the grib files. This class provides some higher
30#    functions which apply a set of GRIB API tools together in the respective
31#    context. So, the class initially contains a list of grib files (their
32#    names) and the using program then applies the methods directly on the
33#    class objects without having to think about how the actual GRIB API
34#    tools have to be arranged.
35#
36# @Class Content:
37#    - __init__
38#    - getkeys
39#    - setkeys
40#    - copy
41#    - index
42#
43#*******************************************************************************
44
45# ------------------------------------------------------------------------------
46# MODULES
47# ------------------------------------------------------------------------------
48import os
49from gribapi import *
50
51# ------------------------------------------------------------------------------
52# CLASS
53# ------------------------------------------------------------------------------
54class GribTools:
55    '''
56    Class for GRIB utilities (new methods) based on GRIB API
57    '''
58    # --------------------------------------------------------------------------
59    # CLASS FUNCTIONS
60    # --------------------------------------------------------------------------
61    def __init__(self, filenames):
62        '''
63        @Description:
64            Initialise an object of GribTools and assign a list
65            of filenames.
66
67        @Input:
68            filenames: list of strings
69                A list of filenames.
70
71        @Return:
72            <nothing>
73        '''
74
75        self.filename = filenames
76
77        return
78
79
80    def getkeys(self, keynames, wherekeynames=[], wherekeyvalues=[]):
81        '''
82        @Description:
83            get keyvalues for a given list of keynames
84            a where statement can be given (list of key and list of values)
85
86        @Input:
87            keynames: list of strings
88                List of keynames.
89
90            wherekeynames: list of ???, optional
91                Default value is an empty list.
92
93            wherekeyvalues: list of ???, optional
94                Default value is an empty list.
95
96        @Return:
97            return_list: list of strings
98                List of keyvalues for given keynames.
99        '''
100
101        fileid = open(self.filename, 'r')
102
103        return_list = []
104
105        while 1:
106            gid_in = grib_new_from_file(fileid)
107
108            if gid_in is None:
109                break
110
111            if len(wherekeynames) != len(wherekeyvalues):
112                raise Exception("Number of key values and key names must be \
113                                 the same. Give a value for each keyname!")
114
115            select = True
116            i = 0
117            for wherekey in wherekeynames:
118                if not grib_is_defined(gid_in, wherekey):
119                    raise Exception("where key was not defined")
120
121                select = (select and (str(wherekeyvalues[i]) ==
122                                      str(grib_get(gid_in, wherekey))))
123                i += 1
124
125            if select:
126                llist = []
127                for key in keynames:
128                    llist.extend([str(grib_get(gid_in, key))])
129                return_list.append(llist)
130
131            grib_release(gid_in)
132
133        fileid.close()
134
135        return return_list
136
137
138    def setkeys(self, fromfile, keynames, keyvalues, wherekeynames=[],
139                wherekeyvalues=[], strict=False, filemode='w'):
140        '''
141        @Description:
142            Opens the file to read the grib messages and then write
143            them to a new output file. By default all messages are
144            written out. Also, the keyvalues of the passed list of
145            keynames are set or only those meeting the where statement.
146            (list of key and list of values).
147
148        @Input:
149            fromfile: string
150                Filename of the input file to read the grib messages from.
151
152            keynames: list of ???
153                List of keynames. Default is an empty list.
154
155            keyvalues: list of ???
156                List of keynames. Default is an empty list.
157
158            wherekeynames: list of ???, optional
159                Default value is an empty list.
160
161            wherekeyvalues: list of ???, optional
162                Default value is an empty list.
163
164            strict: boolean, optional
165                Decides if everything from keynames and keyvalues
166                is written out the grib file (False) or only those
167                meeting the where statement (True). Default is False.
168
169            filemode: string, optional
170                Sets the mode for the output file. Default is "w".
171
172        @Return:
173            <nothing>
174
175        '''
176        fout = open(self.filename, filemode)
177        fin = open(fromfile)
178
179        while 1:
180            gid_in = grib_new_from_file(fin)
181
182            if gid_in is None:
183                break
184
185            if len(wherekeynames) != len(wherekeyvalues):
186                raise Exception("Give a value for each keyname!")
187
188            select = True
189            i = 0
190            for wherekey in wherekeynames:
191                if not grib_is_defined(gid_in, wherekey):
192                    raise Exception("where Key was not defined")
193
194                select = (select and (str(wherekeyvalues[i]) ==
195                                      str(grib_get(gid_in, wherekey))))
196                i += 1
197
198#AP is it secured that the order of keynames is equal to keyvalues?
199            if select:
200                i = 0
201                for key in keynames:
202                    grib_set(gid_in, key, keyvalues[i])
203                    i += 1
204
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
216            grib_release(gid_in)
217
218        fin.close()
219        fout.close()
220
221        return
222
223    def copy(self, filename_in, selectWhere=True,
224             keynames=[], keyvalues=[], filemode='w'):
225        '''
226        Add the content of another input grib file to the objects file but
227        only messages corresponding to keys/values passed to the function.
228        The selectWhere switch decides if to copy the keys equal to (True) or
229        different to (False) the keynames/keyvalues list passed to the function.
230
231        @Input:
232            filename_in: string
233                Filename of the input file to read the grib messages from.
234
235            selectWhere: boolean, optional
236                Decides if to copy the keynames and values equal to (True) or
237                different to (False) the keynames/keyvalues list passed to the
238                function. Default is True.
239
240            keynames: list of ???, optional
241                List of keynames. Default is an empty list.
242
243            keyvalues: list of ???, optional
244                List of keynames. Default is an empty list.
245
246            filemode: string, optional
247                Sets the mode for the output file. Default is "w".
248
249        @Return:
250            <nothing>
251        '''
252
253        fin = open(filename_in)
254        fout = open(self.filename, filemode)
255
256        while 1:
257            gid_in = grib_new_from_file(fin)
258
259            if gid_in is None:
260                break
261
262            if len(keynames) != len(keyvalues):
263                raise Exception("Give a value for each keyname!")
264
265            select = True
266            i = 0
267            for key in keynames:
268                if not grib_is_defined(gid_in, key):
269                    raise Exception("Key was not defined")
270
271                if selectWhere:
272                    select = (select and (str(keyvalues[i]) ==
273                                          str(grib_get(gid_in, key))))
274                else:
275                    select = (select and (str(keyvalues[i]) !=
276                                          str(grib_get(gid_in, key))))
277                i += 1
278
279            if select:
280                grib_write(gid_in, fout)
281
282            grib_release(gid_in)
283
284        fin.close()
285        fout.close()
286
287        return
288
289    def index(self, index_keys=["mars"], index_file="my.idx"):
290        '''
291        @Description:
292            Create index file from a list of files if it does not exist or
293            read an index file.
294
295        @Input:
296            index_keys: list of strings, optional
297                Contains the list of key parameter names from
298                which the index is to be created.
299                Default is a list with a single entry string "mars".
300
301            index_file: string, optional
302                Filename where the indices are stored.
303                Default is "my.idx".
304
305        @Return:
306            iid: integer
307                Grib index id.
308        '''
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))
315        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)
320                else:
321                    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
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG