source: flexpart.git/preproc/python/GribTools.py @ 0e29ef4

FPv9.3.1FPv9.3.1b_testingFPv9.3.2devfp9.3.1-20161214-nc4grib2nc4_repairrelease-10univie
Last change on this file since 0e29ef4 was 0e29ef4, checked in by Anne Fouilloux <annefou@…>, 5 years ago

bug fix when looping over months/dates

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
12from gribapi import *
13import traceback
14import sys,os
15
16
17##############################################################
18#  GRIB utilities
19##############################################################
20class 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       
33        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
40            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
44            if select:
45                llist = []
46                for key in keynames:
47                    llist.extend([str(grib_get(gid_in, key))])
48                return_list.append(llist)
49            grib_release(gid_in)
50        fileid.close()
51        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       
63        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
71            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
75            if select:
76                i=0
77                for key in keynames:
78                    grib_set(gid_in, key, keyvalues[i])
79                    i=i+1
80            if strict:
81                if select:
82                    grib_write(gid_in,fout)
83            else:
84                grib_write(gid_in,fout)
85            grib_release(gid_in)
86        fin.close()
87        fout.close()
88
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       
96        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
104            for key in keynames:
105                if not grib_is_defined(gid_in, key): raise Exception("Key was not defined")
106               
107                if selectWhere:
108                    select=select and (str(keyvalues[i])==str(grib_get(gid_in, key)))
109                else:
110                    select=select and (str(keyvalues[i])!=str(grib_get(gid_in, key)))
111                i=i+1
112            if select:
113                grib_write(gid_in,fout)
114            grib_release(gid_in)
115        fin.close()
116        fout.close()
117
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" 
121        self.iid = None
122 
123        if (os.path.exists(index_file)):
124            self.iid = grib_index_read(index_file)
125            print "Use existing index file: %s "%(index_file)
126        else:
127            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)
131                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       
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG