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

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

whole bunch of modifications due to new structure of ECMWFDATA, added basics of documentation, minor programming corrections

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