Changeset ff99eae in flex_extract.git for python/GribTools.py
- Timestamp:
- Jun 1, 2018, 8:34:59 PM (6 years ago)
- Branches:
- master, ctbto, dev
- Children:
- e1228f3
- Parents:
- ccab809
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/GribTools.py
rccab809 rff99eae 1 1 #!/usr/bin/env python 2 2 # -*- coding: utf-8 -*- 3 #************************************************************************4 # TODO AP5 # - 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 3 #******************************************************************************* 10 4 # @Author: Anne Fouilloux (University of Oslo) … … 36 30 # @Class Content: 37 31 # - __init__ 38 # - get keys39 # - set keys32 # - get_keys 33 # - set_keys 40 34 # - copy 41 35 # - index 36 # 37 # @Class Attributes: 38 # - filenames 42 39 # 43 40 #******************************************************************************* … … 47 44 # ------------------------------------------------------------------------------ 48 45 import os 49 from gribapi import * 46 from 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 50 50 51 51 # ------------------------------------------------------------------------------ 52 52 # CLASS 53 53 # ------------------------------------------------------------------------------ 54 class Grib Tools:54 class Gribtools(object): 55 55 ''' 56 56 Class for GRIB utilities (new methods) based on GRIB API … … 62 62 ''' 63 63 @Description: 64 Initialise an object of Grib Tools and assign a list64 Initialise an object of Gribtools and assign a list 65 65 of filenames. 66 66 … … 73 73 ''' 74 74 75 self.filename = filenames75 self.filenames = filenames 76 76 77 77 return 78 78 79 79 80 def get keys(self, keynames, wherekeynames=[], wherekeyvalues=[]):80 def get_keys(self, keynames, wherekeynames=[], wherekeyvalues=[]): 81 81 ''' 82 82 @Description: … … 88 88 List of keynames. 89 89 90 wherekeynames: list of ???, optional90 wherekeynames: list of strings, optional 91 91 Default value is an empty list. 92 92 93 wherekeyvalues: list of ???, optional93 wherekeyvalues: list of strings, optional 94 94 Default value is an empty list. 95 95 … … 99 99 ''' 100 100 101 fileid = open(self.filename , 'r')101 fileid = open(self.filenames, 'r') 102 102 103 103 return_list = [] … … 136 136 137 137 138 def set keys(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'): 140 140 ''' 141 141 @Description: … … 150 150 Filename of the input file to read the grib messages from. 151 151 152 keynames: list of ???152 keynames: list of strings 153 153 List of keynames. Default is an empty list. 154 154 155 keyvalues: list of ???155 keyvalues: list of strings 156 156 List of keynames. Default is an empty list. 157 157 158 wherekeynames: list of ???, optional158 wherekeynames: list of strings, optional 159 159 Default value is an empty list. 160 160 161 wherekeyvalues: list of ???, optional161 wherekeyvalues: list of strings, optional 162 162 Default value is an empty list. 163 163 … … 174 174 175 175 ''' 176 fout = open(self.filename , filemode)176 fout = open(self.filenames, filemode) 177 177 fin = open(fromfile) 178 178 … … 196 196 i += 1 197 197 198 #AP is it secured that the order of keynames is equal to keyvalues?199 198 if select: 200 199 i = 0 … … 203 202 i += 1 204 203 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 216 206 grib_release(gid_in) 217 207 … … 238 228 function. Default is True. 239 229 240 keynames: list of ???, optional230 keynames: list of strings, optional 241 231 List of keynames. Default is an empty list. 242 232 243 keyvalues: list of ???, optional233 keyvalues: list of strings, optional 244 234 List of keynames. Default is an empty list. 245 235 … … 252 242 253 243 fin = open(filename_in) 254 fout = open(self.filename , filemode)244 fout = open(self.filenames, filemode) 255 245 256 246 while 1: … … 307 297 Grib index id. 308 298 ''' 309 print ("... index will be done")310 self.iid = None311 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) 315 305 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) 320 310 else: 321 311 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.