Changeset 268ee86 in flex_extract.git
- Timestamp:
- Dec 13, 2018, 11:51:17 PM (5 years ago)
- Branches:
- master, ctbto, dev
- Children:
- 145fbe0
- Parents:
- fdfaf00
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
source/python/classes/GribUtil.py
r702cbb7 r268ee86 32 32 # - get_keys 33 33 # - set_keys 34 # - copy 34 # - copy_dummy_msg 35 35 # - index 36 36 # … … 48 48 codes_release, codes_set, codes_write, codes_index_read, 49 49 codes_index_new_from_file, codes_index_add_file, 50 codes_index_write )50 codes_index_write, codes_set_values) 51 51 52 52 # ------------------------------------------------------------------------------ 53 53 # CLASS 54 54 # ------------------------------------------------------------------------------ 55 class Grib Tools(object):55 class GribUtil(object): 56 56 ''' 57 57 Class for GRIB utilities (new methods) based on GRIB API … … 61 61 # -------------------------------------------------------------------------- 62 62 def __init__(self, filenames): 63 '''Initialise an object of Grib Toolsand assign a list of filenames.63 '''Initialise an object of GribUtil and assign a list of filenames. 64 64 65 65 Parameters … … 139 139 wherekeyvalues=[], strict=False, filemode='w'): 140 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). 141 the selected messages (with wherekeys) to a new output file. 142 Also, the keyvalues of the passed list of keynames are set. 145 143 146 144 Parameters … … 150 148 151 149 keynames : :obj:`list` of :obj:`string` 152 List of keynames. Default is an empty list. 150 List of keynames to set in the selected messages. 151 Default is an empty list. 153 152 154 153 keyvalues : :obj:`list` of :obj:`string` 155 List of keynames. Default is an empty list. 154 List of keyvalues to set in the selected messages. 155 Default is an empty list. 156 156 157 157 wherekeynames : :obj:`list` of :obj:`string`, optional 158 List of keynames to select correct message. 158 159 Default value is an empty list. 159 160 160 161 wherekeyvalues : :obj:`list` of :obj:`string`, optional 162 List of keyvalues for keynames to select correct message. 161 163 Default value is an empty list. 162 164 … … 173 175 174 176 ''' 177 if len(wherekeynames) != len(wherekeyvalues): 178 raise Exception("Give a value for each keyname!") 179 175 180 fout = open(self.filenames, filemode) 176 181 fin = open(fromfile) 177 182 178 183 while 1: 179 gid = codes_ new_from_file(fin)184 gid = codes_grib_new_from_file(fin) 180 185 181 186 if gid is None: 182 187 break 183 184 if len(wherekeynames) != len(wherekeyvalues):185 raise Exception("Give a value for each keyname!")186 188 187 189 select = True … … 189 191 for wherekey in wherekeynames: 190 192 if not codes_is_defined(gid, wherekey): 191 raise Exception("where Key was not defined")193 raise Exception("wherekey was not defined") 192 194 193 195 select = (select and (str(wherekeyvalues[i]) == … … 198 200 i = 0 199 201 for key in keynames: 200 codes_set(gid, key, keyvalues[i]) 202 if key == 'values': 203 codes_set_values(gid, keyvalues[i]) 204 else: 205 codes_set(gid, key, keyvalues[i]) 201 206 i += 1 202 207 203 codes_write(gid, fout)208 codes_write(gid, fout) 204 209 205 210 codes_release(gid) … … 210 215 return 211 216 212 def copy (self, filename_in, selectWhere=True,213 keynames=[], keyvalues=[], filemode='w'):217 def copy_dummy_msg(self, filename_in, selectWhere=True, 218 keynames=[], keyvalues=[], filemode='w'): 214 219 '''Add the content of another input grib file to the objects file but 215 220 only messages corresponding to keys/values passed to the function. … … 231 236 232 237 keyvalues : :obj:`list` of :obj:`string`, optional 233 List of key names. Default is an empty list.238 List of keyvalues. Default is an empty list. 234 239 235 240 filemode : :obj:`string`, optional … … 240 245 241 246 ''' 247 if len(keynames) != len(keyvalues): 248 raise Exception("Give a value for each keyname!") 242 249 243 250 fin = open(filename_in) 244 251 fout = open(self.filenames, filemode) 245 252 246 while 1: 247 gid = codes_new_from_file(fin) 253 fields = 0 254 255 while fields < 1: 256 gid = codes_grib_new_from_file(fin) 248 257 249 258 if gid is None: 250 259 break 251 252 if len(keynames) != len(keyvalues):253 raise Exception("Give a value for each keyname!")254 260 255 261 select = True … … 268 274 269 275 if select: 276 fields = fields + 1 270 277 codes_write(gid, fout) 271 278
Note: See TracChangeset
for help on using the changeset viewer.