[64cf353] | 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!!! |
---|
[efdb01a] | 8 | # - add file description |
---|
| 9 | # - bis auf --init-- und index wird keine Funktion verwendet!? |
---|
[64cf353] | 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: |
---|
[efdb01a] | 23 | (C) Copyright 2014-2018. |
---|
[64cf353] | 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. |
---|
[d69b677] | 37 | |
---|
[efdb01a] | 38 | ... |
---|
[64cf353] | 39 | """ |
---|
| 40 | # ------------------------------------------------------------------------------ |
---|
| 41 | # MODULES |
---|
| 42 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 43 | from gribapi import * |
---|
| 44 | import traceback |
---|
[efdb01a] | 45 | import sys |
---|
| 46 | import os |
---|
[64cf353] | 47 | # ------------------------------------------------------------------------------ |
---|
| 48 | # CLASS |
---|
| 49 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 50 | class GribTools: |
---|
[64cf353] | 51 | ''' |
---|
| 52 | Class for GRIB utilities (new methods) based on GRIB API |
---|
| 53 | ''' |
---|
| 54 | # -------------------------------------------------------------------------- |
---|
[efdb01a] | 55 | # CLASS FUNCTIONS |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 86 | wherekeynames: list of ???, optional |
---|
[64cf353] | 87 | Default value is an empty list. |
---|
| 88 | |
---|
[02c8c50] | 89 | wherekeyvalues: list of ???, optional |
---|
[64cf353] | 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 | |
---|
[d69b677] | 101 | while 1: |
---|
[64cf353] | 102 | gid_in = grib_new_from_file(fileid) |
---|
[d69b677] | 103 | |
---|
[64cf353] | 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 |
---|
[d69b677] | 113 | for wherekey in wherekeynames: |
---|
[64cf353] | 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 | |
---|
[d69b677] | 121 | if select: |
---|
| 122 | llist = [] |
---|
| 123 | for key in keynames: |
---|
| 124 | llist.extend([str(grib_get(gid_in, key))]) |
---|
| 125 | return_list.append(llist) |
---|
[64cf353] | 126 | |
---|
[d69b677] | 127 | grib_release(gid_in) |
---|
[64cf353] | 128 | |
---|
[d69b677] | 129 | fileid.close() |
---|
[64cf353] | 130 | |
---|
[d69b677] | 131 | return return_list |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 154 | wherekeynames: list of ???, optional |
---|
[64cf353] | 155 | Default value is an empty list. |
---|
| 156 | |
---|
[02c8c50] | 157 | wherekeyvalues: list of ???, optional |
---|
[64cf353] | 158 | Default value is an empty list. |
---|
| 159 | |
---|
[02c8c50] | 160 | strict: boolean, optional |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 165 | filemode: string, optional |
---|
[64cf353] | 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 | |
---|
[d69b677] | 175 | while 1: |
---|
[64cf353] | 176 | gid_in = grib_new_from_file(fin) |
---|
| 177 | |
---|
| 178 | if gid_in is None: |
---|
| 179 | break |
---|
[d69b677] | 180 | |
---|
[64cf353] | 181 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 182 | raise Exception("Give a value for each keyname!") |
---|
| 183 | |
---|
| 184 | select = True |
---|
| 185 | i = 0 |
---|
[d69b677] | 186 | for wherekey in wherekeynames: |
---|
[64cf353] | 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? |
---|
[d69b677] | 195 | if select: |
---|
[64cf353] | 196 | i = 0 |
---|
[d69b677] | 197 | for key in keynames: |
---|
| 198 | grib_set(gid_in, key, keyvalues[i]) |
---|
[64cf353] | 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 | # |
---|
[d69b677] | 206 | if strict: |
---|
| 207 | if select: |
---|
[64cf353] | 208 | grib_write(gid_in, fout) |
---|
[d69b677] | 209 | else: |
---|
[64cf353] | 210 | grib_write(gid_in, fout) |
---|
| 211 | #AP end |
---|
[d69b677] | 212 | grib_release(gid_in) |
---|
[64cf353] | 213 | |
---|
[d69b677] | 214 | fin.close() |
---|
| 215 | fout.close() |
---|
| 216 | |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 231 | selectWhere: boolean, optional |
---|
[64cf353] | 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 | |
---|
[02c8c50] | 236 | keynames: list of ???, optional |
---|
[64cf353] | 237 | List of keynames. Default is an empty list. |
---|
| 238 | |
---|
[02c8c50] | 239 | keyvalues: list of ???, optional |
---|
[64cf353] | 240 | List of keynames. Default is an empty list. |
---|
| 241 | |
---|
[02c8c50] | 242 | filemode: string, optional |
---|
[64cf353] | 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 | |
---|
[d69b677] | 252 | while 1: |
---|
[64cf353] | 253 | gid_in = grib_new_from_file(fin) |
---|
[d69b677] | 254 | |
---|
[64cf353] | 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 |
---|
[d69b677] | 263 | for key in keynames: |
---|
[64cf353] | 264 | if not grib_is_defined(gid_in, key): |
---|
| 265 | raise Exception("Key was not defined") |
---|
| 266 | |
---|
[d69b677] | 267 | if selectWhere: |
---|
[64cf353] | 268 | select = (select and (str(keyvalues[i]) == |
---|
| 269 | str(grib_get(gid_in, key)))) |
---|
[d69b677] | 270 | else: |
---|
[64cf353] | 271 | select = (select and (str(keyvalues[i]) != |
---|
| 272 | str(grib_get(gid_in, key)))) |
---|
| 273 | i += 1 |
---|
| 274 | |
---|
[d69b677] | 275 | if select: |
---|
[64cf353] | 276 | grib_write(gid_in, fout) |
---|
| 277 | |
---|
[d69b677] | 278 | grib_release(gid_in) |
---|
[64cf353] | 279 | |
---|
[d69b677] | 280 | fin.close() |
---|
| 281 | fout.close() |
---|
| 282 | |
---|
[64cf353] | 283 | return |
---|
| 284 | |
---|
| 285 | def index(self, index_keys=["mars"], index_file="my.idx"): |
---|
| 286 | ''' |
---|
| 287 | @Description: |
---|
[02c8c50] | 288 | Create index file from a list of files if it does not exist or |
---|
[64cf353] | 289 | read an index file. |
---|
| 290 | |
---|
| 291 | @Input: |
---|
[02c8c50] | 292 | index_keys: list of strings, optional |
---|
| 293 | Contains the list of key parameter names from |
---|
| 294 | which the index is to be created. |
---|
[64cf353] | 295 | Default is a list with a single entry string "mars". |
---|
| 296 | |
---|
[02c8c50] | 297 | index_file: string, optional |
---|
[64cf353] | 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") |
---|
[d69b677] | 306 | self.iid = None |
---|
[64cf353] | 307 | |
---|
[d69b677] | 308 | if (os.path.exists(index_file)): |
---|
| 309 | self.iid = grib_index_read(index_file) |
---|
[64cf353] | 310 | print("Use existing index file: %s " % (index_file)) |
---|
[d69b677] | 311 | else: |
---|
| 312 | for file in self.filename: |
---|
[64cf353] | 313 | print("Inputfile: %s " % (file)) |
---|
| 314 | if self.iid is None: |
---|
| 315 | self.iid = grib_index_new_from_file(file, index_keys) |
---|
[d69b677] | 316 | else: |
---|
[64cf353] | 317 | grib_index_add_file(self.iid, file) |
---|
[02c8c50] | 318 | |
---|
[64cf353] | 319 | if self.iid is not None: |
---|
| 320 | grib_index_write(self.iid, index_file) |
---|
| 321 | |
---|
[02c8c50] | 322 | print('... index done') |
---|
| 323 | |
---|
[64cf353] | 324 | return self.iid |
---|
| 325 | |
---|
[d69b677] | 326 | |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|