[64cf353] | 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!!! |
---|
[efdb01a] | 7 | # - bis auf --init-- und index wird keine Funktion verwendet!? |
---|
[64cf353] | 8 | #************************************************************************ |
---|
[991df6a] | 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 | #******************************************************************************* |
---|
[64cf353] | 44 | |
---|
| 45 | # ------------------------------------------------------------------------------ |
---|
| 46 | # MODULES |
---|
| 47 | # ------------------------------------------------------------------------------ |
---|
[efdb01a] | 48 | import os |
---|
[991df6a] | 49 | from gribapi import * |
---|
| 50 | |
---|
[64cf353] | 51 | # ------------------------------------------------------------------------------ |
---|
| 52 | # CLASS |
---|
| 53 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 54 | class GribTools: |
---|
[64cf353] | 55 | ''' |
---|
| 56 | Class for GRIB utilities (new methods) based on GRIB API |
---|
| 57 | ''' |
---|
| 58 | # -------------------------------------------------------------------------- |
---|
[efdb01a] | 59 | # CLASS FUNCTIONS |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 90 | wherekeynames: list of ???, optional |
---|
[64cf353] | 91 | Default value is an empty list. |
---|
| 92 | |
---|
[02c8c50] | 93 | wherekeyvalues: list of ???, optional |
---|
[64cf353] | 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 | |
---|
[d69b677] | 105 | while 1: |
---|
[64cf353] | 106 | gid_in = grib_new_from_file(fileid) |
---|
[d69b677] | 107 | |
---|
[64cf353] | 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 |
---|
[d69b677] | 117 | for wherekey in wherekeynames: |
---|
[64cf353] | 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 | |
---|
[d69b677] | 125 | if select: |
---|
| 126 | llist = [] |
---|
| 127 | for key in keynames: |
---|
| 128 | llist.extend([str(grib_get(gid_in, key))]) |
---|
| 129 | return_list.append(llist) |
---|
[64cf353] | 130 | |
---|
[d69b677] | 131 | grib_release(gid_in) |
---|
[64cf353] | 132 | |
---|
[d69b677] | 133 | fileid.close() |
---|
[64cf353] | 134 | |
---|
[d69b677] | 135 | return return_list |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 158 | wherekeynames: list of ???, optional |
---|
[64cf353] | 159 | Default value is an empty list. |
---|
| 160 | |
---|
[02c8c50] | 161 | wherekeyvalues: list of ???, optional |
---|
[64cf353] | 162 | Default value is an empty list. |
---|
| 163 | |
---|
[02c8c50] | 164 | strict: boolean, optional |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 169 | filemode: string, optional |
---|
[64cf353] | 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 | |
---|
[d69b677] | 179 | while 1: |
---|
[64cf353] | 180 | gid_in = grib_new_from_file(fin) |
---|
| 181 | |
---|
| 182 | if gid_in is None: |
---|
| 183 | break |
---|
[d69b677] | 184 | |
---|
[64cf353] | 185 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 186 | raise Exception("Give a value for each keyname!") |
---|
| 187 | |
---|
| 188 | select = True |
---|
| 189 | i = 0 |
---|
[d69b677] | 190 | for wherekey in wherekeynames: |
---|
[64cf353] | 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? |
---|
[d69b677] | 199 | if select: |
---|
[64cf353] | 200 | i = 0 |
---|
[d69b677] | 201 | for key in keynames: |
---|
| 202 | grib_set(gid_in, key, keyvalues[i]) |
---|
[64cf353] | 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 | # |
---|
[d69b677] | 210 | if strict: |
---|
| 211 | if select: |
---|
[64cf353] | 212 | grib_write(gid_in, fout) |
---|
[d69b677] | 213 | else: |
---|
[64cf353] | 214 | grib_write(gid_in, fout) |
---|
| 215 | #AP end |
---|
[d69b677] | 216 | grib_release(gid_in) |
---|
[64cf353] | 217 | |
---|
[d69b677] | 218 | fin.close() |
---|
| 219 | fout.close() |
---|
| 220 | |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 235 | selectWhere: boolean, optional |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 240 | keynames: list of ???, optional |
---|
[64cf353] | 241 | List of keynames. Default is an empty list. |
---|
| 242 | |
---|
[02c8c50] | 243 | keyvalues: list of ???, optional |
---|
[64cf353] | 244 | List of keynames. Default is an empty list. |
---|
| 245 | |
---|
[02c8c50] | 246 | filemode: string, optional |
---|
[64cf353] | 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 | |
---|
[d69b677] | 256 | while 1: |
---|
[64cf353] | 257 | gid_in = grib_new_from_file(fin) |
---|
[d69b677] | 258 | |
---|
[64cf353] | 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 |
---|
[d69b677] | 267 | for key in keynames: |
---|
[64cf353] | 268 | if not grib_is_defined(gid_in, key): |
---|
| 269 | raise Exception("Key was not defined") |
---|
| 270 | |
---|
[d69b677] | 271 | if selectWhere: |
---|
[64cf353] | 272 | select = (select and (str(keyvalues[i]) == |
---|
| 273 | str(grib_get(gid_in, key)))) |
---|
[d69b677] | 274 | else: |
---|
[64cf353] | 275 | select = (select and (str(keyvalues[i]) != |
---|
| 276 | str(grib_get(gid_in, key)))) |
---|
| 277 | i += 1 |
---|
| 278 | |
---|
[d69b677] | 279 | if select: |
---|
[64cf353] | 280 | grib_write(gid_in, fout) |
---|
| 281 | |
---|
[d69b677] | 282 | grib_release(gid_in) |
---|
[64cf353] | 283 | |
---|
[d69b677] | 284 | fin.close() |
---|
| 285 | fout.close() |
---|
| 286 | |
---|
[64cf353] | 287 | return |
---|
| 288 | |
---|
| 289 | def index(self, index_keys=["mars"], index_file="my.idx"): |
---|
| 290 | ''' |
---|
| 291 | @Description: |
---|
[02c8c50] | 292 | Create index file from a list of files if it does not exist or |
---|
[64cf353] | 293 | read an index file. |
---|
| 294 | |
---|
| 295 | @Input: |
---|
[02c8c50] | 296 | index_keys: list of strings, optional |
---|
| 297 | Contains the list of key parameter names from |
---|
| 298 | which the index is to be created. |
---|
[64cf353] | 299 | Default is a list with a single entry string "mars". |
---|
| 300 | |
---|
[02c8c50] | 301 | index_file: string, optional |
---|
[64cf353] | 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") |
---|
[d69b677] | 310 | self.iid = None |
---|
[64cf353] | 311 | |
---|
[d69b677] | 312 | if (os.path.exists(index_file)): |
---|
| 313 | self.iid = grib_index_read(index_file) |
---|
[64cf353] | 314 | print("Use existing index file: %s " % (index_file)) |
---|
[d69b677] | 315 | else: |
---|
| 316 | for file in self.filename: |
---|
[64cf353] | 317 | print("Inputfile: %s " % (file)) |
---|
| 318 | if self.iid is None: |
---|
| 319 | self.iid = grib_index_new_from_file(file, index_keys) |
---|
[d69b677] | 320 | else: |
---|
[64cf353] | 321 | grib_index_add_file(self.iid, file) |
---|
[02c8c50] | 322 | |
---|
[64cf353] | 323 | if self.iid is not None: |
---|
| 324 | grib_index_write(self.iid, index_file) |
---|
| 325 | |
---|
[02c8c50] | 326 | print('... index done') |
---|
| 327 | |
---|
[64cf353] | 328 | return self.iid |
---|
| 329 | |
---|
[d69b677] | 330 | |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | |
---|