[64cf353] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
| 5 | # |
---|
| 6 | # @Date: July 2014 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 10 | # - applied PEP8 style guide |
---|
| 11 | # - added documentation |
---|
| 12 | # - changed some naming |
---|
| 13 | # |
---|
| 14 | # @License: |
---|
| 15 | # (C) Copyright 2014-2018. |
---|
| 16 | # |
---|
| 17 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 18 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 19 | # |
---|
| 20 | # @Class Description: |
---|
| 21 | # The GRIB API provides all necessary tools to work directly with the |
---|
| 22 | # grib files. Nevertheless, the GRIB API tools are very basic and are in |
---|
| 23 | # direct connection with the grib files. This class provides some higher |
---|
| 24 | # functions which apply a set of GRIB API tools together in the respective |
---|
| 25 | # context. So, the class initially contains a list of grib files (their |
---|
| 26 | # names) and the using program then applies the methods directly on the |
---|
| 27 | # class objects without having to think about how the actual GRIB API |
---|
| 28 | # tools have to be arranged. |
---|
| 29 | # |
---|
| 30 | # @Class Content: |
---|
| 31 | # - __init__ |
---|
[ff99eae] | 32 | # - get_keys |
---|
| 33 | # - set_keys |
---|
[991df6a] | 34 | # - copy |
---|
| 35 | # - index |
---|
| 36 | # |
---|
[ff99eae] | 37 | # @Class Attributes: |
---|
| 38 | # - filenames |
---|
| 39 | # |
---|
[991df6a] | 40 | #******************************************************************************* |
---|
[64cf353] | 41 | |
---|
| 42 | # ------------------------------------------------------------------------------ |
---|
| 43 | # MODULES |
---|
| 44 | # ------------------------------------------------------------------------------ |
---|
[efdb01a] | 45 | import os |
---|
[0934db1] | 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 |
---|
[aa275fc] | 50 | |
---|
[0934db1] | 51 | |
---|
| 52 | # from eccodes import (codes_grib_new_from_file, codes_is_defined, codes_get, |
---|
| 53 | # codes_release, codes_set, codes_write, codes_index_read, |
---|
| 54 | # codes_index_new_from_file, codes_index_add_file, |
---|
| 55 | # codes_index_write) |
---|
[991df6a] | 56 | |
---|
[64cf353] | 57 | # ------------------------------------------------------------------------------ |
---|
| 58 | # CLASS |
---|
| 59 | # ------------------------------------------------------------------------------ |
---|
[e1228f3] | 60 | class GribTools(object): |
---|
[64cf353] | 61 | ''' |
---|
| 62 | Class for GRIB utilities (new methods) based on GRIB API |
---|
| 63 | ''' |
---|
| 64 | # -------------------------------------------------------------------------- |
---|
[efdb01a] | 65 | # CLASS FUNCTIONS |
---|
[64cf353] | 66 | # -------------------------------------------------------------------------- |
---|
| 67 | def __init__(self, filenames): |
---|
| 68 | ''' |
---|
| 69 | @Description: |
---|
[e1228f3] | 70 | Initialise an object of GribTools and assign a list |
---|
[64cf353] | 71 | of filenames. |
---|
| 72 | |
---|
| 73 | @Input: |
---|
| 74 | filenames: list of strings |
---|
| 75 | A list of filenames. |
---|
| 76 | |
---|
| 77 | @Return: |
---|
| 78 | <nothing> |
---|
| 79 | ''' |
---|
| 80 | |
---|
[ff99eae] | 81 | self.filenames = filenames |
---|
[64cf353] | 82 | |
---|
| 83 | return |
---|
| 84 | |
---|
| 85 | |
---|
[ff99eae] | 86 | def get_keys(self, keynames, wherekeynames=[], wherekeyvalues=[]): |
---|
[64cf353] | 87 | ''' |
---|
| 88 | @Description: |
---|
| 89 | get keyvalues for a given list of keynames |
---|
| 90 | a where statement can be given (list of key and list of values) |
---|
| 91 | |
---|
| 92 | @Input: |
---|
| 93 | keynames: list of strings |
---|
| 94 | List of keynames. |
---|
| 95 | |
---|
[ff99eae] | 96 | wherekeynames: list of strings, optional |
---|
[64cf353] | 97 | Default value is an empty list. |
---|
| 98 | |
---|
[ff99eae] | 99 | wherekeyvalues: list of strings, optional |
---|
[64cf353] | 100 | Default value is an empty list. |
---|
| 101 | |
---|
| 102 | @Return: |
---|
| 103 | return_list: list of strings |
---|
| 104 | List of keyvalues for given keynames. |
---|
| 105 | ''' |
---|
| 106 | |
---|
[ff99eae] | 107 | fileid = open(self.filenames, 'r') |
---|
[64cf353] | 108 | |
---|
| 109 | return_list = [] |
---|
| 110 | |
---|
[d69b677] | 111 | while 1: |
---|
[0934db1] | 112 | gid = grib_new_from_file(fileid) |
---|
[d69b677] | 113 | |
---|
[aa275fc] | 114 | if gid is None: |
---|
[64cf353] | 115 | break |
---|
| 116 | |
---|
| 117 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 118 | raise Exception("Number of key values and key names must be \ |
---|
| 119 | the same. Give a value for each keyname!") |
---|
| 120 | |
---|
| 121 | select = True |
---|
| 122 | i = 0 |
---|
[d69b677] | 123 | for wherekey in wherekeynames: |
---|
[0934db1] | 124 | if not grib_is_defined(gid, wherekey): |
---|
[64cf353] | 125 | raise Exception("where key was not defined") |
---|
| 126 | |
---|
| 127 | select = (select and (str(wherekeyvalues[i]) == |
---|
[0934db1] | 128 | str(grib_get(gid, wherekey)))) |
---|
[64cf353] | 129 | i += 1 |
---|
| 130 | |
---|
[d69b677] | 131 | if select: |
---|
| 132 | llist = [] |
---|
| 133 | for key in keynames: |
---|
[0934db1] | 134 | llist.extend([str(grib_get(gid, key))]) |
---|
[d69b677] | 135 | return_list.append(llist) |
---|
[64cf353] | 136 | |
---|
[0934db1] | 137 | grib_release(gid) |
---|
[64cf353] | 138 | |
---|
[d69b677] | 139 | fileid.close() |
---|
[64cf353] | 140 | |
---|
[d69b677] | 141 | return return_list |
---|
[64cf353] | 142 | |
---|
| 143 | |
---|
[ff99eae] | 144 | def set_keys(self, fromfile, keynames, keyvalues, wherekeynames=[], |
---|
| 145 | wherekeyvalues=[], strict=False, filemode='w'): |
---|
[64cf353] | 146 | ''' |
---|
| 147 | @Description: |
---|
| 148 | Opens the file to read the grib messages and then write |
---|
| 149 | them to a new output file. By default all messages are |
---|
| 150 | written out. Also, the keyvalues of the passed list of |
---|
| 151 | keynames are set or only those meeting the where statement. |
---|
| 152 | (list of key and list of values). |
---|
| 153 | |
---|
| 154 | @Input: |
---|
| 155 | fromfile: string |
---|
| 156 | Filename of the input file to read the grib messages from. |
---|
| 157 | |
---|
[ff99eae] | 158 | keynames: list of strings |
---|
[64cf353] | 159 | List of keynames. Default is an empty list. |
---|
| 160 | |
---|
[ff99eae] | 161 | keyvalues: list of strings |
---|
[64cf353] | 162 | List of keynames. Default is an empty list. |
---|
| 163 | |
---|
[ff99eae] | 164 | wherekeynames: list of strings, optional |
---|
[64cf353] | 165 | Default value is an empty list. |
---|
| 166 | |
---|
[ff99eae] | 167 | wherekeyvalues: list of strings, optional |
---|
[64cf353] | 168 | Default value is an empty list. |
---|
| 169 | |
---|
[02c8c50] | 170 | strict: boolean, optional |
---|
[64cf353] | 171 | Decides if everything from keynames and keyvalues |
---|
| 172 | is written out the grib file (False) or only those |
---|
| 173 | meeting the where statement (True). Default is False. |
---|
| 174 | |
---|
[02c8c50] | 175 | filemode: string, optional |
---|
[64cf353] | 176 | Sets the mode for the output file. Default is "w". |
---|
| 177 | |
---|
| 178 | @Return: |
---|
| 179 | <nothing> |
---|
| 180 | |
---|
| 181 | ''' |
---|
[ff99eae] | 182 | fout = open(self.filenames, filemode) |
---|
[64cf353] | 183 | fin = open(fromfile) |
---|
| 184 | |
---|
[d69b677] | 185 | while 1: |
---|
[0934db1] | 186 | gid = grib_new_from_file(fin) |
---|
[64cf353] | 187 | |
---|
[aa275fc] | 188 | if gid is None: |
---|
[64cf353] | 189 | break |
---|
[d69b677] | 190 | |
---|
[64cf353] | 191 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 192 | raise Exception("Give a value for each keyname!") |
---|
| 193 | |
---|
| 194 | select = True |
---|
| 195 | i = 0 |
---|
[d69b677] | 196 | for wherekey in wherekeynames: |
---|
[0934db1] | 197 | if not grib_is_defined(gid, wherekey): |
---|
[64cf353] | 198 | raise Exception("where Key was not defined") |
---|
| 199 | |
---|
| 200 | select = (select and (str(wherekeyvalues[i]) == |
---|
[0934db1] | 201 | str(grib_get(gid, wherekey)))) |
---|
[64cf353] | 202 | i += 1 |
---|
| 203 | |
---|
[d69b677] | 204 | if select: |
---|
[64cf353] | 205 | i = 0 |
---|
[d69b677] | 206 | for key in keynames: |
---|
[0934db1] | 207 | grib_set(gid, key, keyvalues[i]) |
---|
[64cf353] | 208 | i += 1 |
---|
| 209 | |
---|
[0934db1] | 210 | grib_write(gid, fout) |
---|
[ff99eae] | 211 | |
---|
[0934db1] | 212 | grib_release(gid) |
---|
[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 | |
---|
[ff99eae] | 236 | keynames: list of strings, optional |
---|
[64cf353] | 237 | List of keynames. Default is an empty list. |
---|
| 238 | |
---|
[ff99eae] | 239 | keyvalues: list of strings, 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) |
---|
[ff99eae] | 250 | fout = open(self.filenames, filemode) |
---|
[64cf353] | 251 | |
---|
[d69b677] | 252 | while 1: |
---|
[0934db1] | 253 | gid = grib_new_from_file(fin) |
---|
[d69b677] | 254 | |
---|
[aa275fc] | 255 | if gid is None: |
---|
[64cf353] | 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: |
---|
[0934db1] | 264 | if not grib_is_defined(gid, key): |
---|
[64cf353] | 265 | raise Exception("Key was not defined") |
---|
| 266 | |
---|
[d69b677] | 267 | if selectWhere: |
---|
[64cf353] | 268 | select = (select and (str(keyvalues[i]) == |
---|
[0934db1] | 269 | str(grib_get(gid, key)))) |
---|
[d69b677] | 270 | else: |
---|
[64cf353] | 271 | select = (select and (str(keyvalues[i]) != |
---|
[0934db1] | 272 | str(grib_get(gid, key)))) |
---|
[64cf353] | 273 | i += 1 |
---|
| 274 | |
---|
[d69b677] | 275 | if select: |
---|
[0934db1] | 276 | grib_write(gid, fout) |
---|
[64cf353] | 277 | |
---|
[0934db1] | 278 | grib_release(gid) |
---|
[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 | ''' |
---|
[2fb99de] | 305 | print("... index will be done") |
---|
[ff99eae] | 306 | iid = None |
---|
[64cf353] | 307 | |
---|
[ff99eae] | 308 | if os.path.exists(index_file): |
---|
[0934db1] | 309 | iid = grib_index_read(index_file) |
---|
[2fb99de] | 310 | print("Use existing index file: %s " % (index_file)) |
---|
[d69b677] | 311 | else: |
---|
[ff99eae] | 312 | for filename in self.filenames: |
---|
[2fb99de] | 313 | print("Inputfile: %s " % (filename)) |
---|
[ff99eae] | 314 | if iid is None: |
---|
[0934db1] | 315 | iid = grib_index_new_from_file(filename, index_keys) |
---|
[d69b677] | 316 | else: |
---|
[0934db1] | 317 | grib_index_add_file(iid, filename) |
---|
[d69b677] | 318 | |
---|
[ff99eae] | 319 | if iid is not None: |
---|
[0934db1] | 320 | grib_index_write(iid, index_file) |
---|
[d69b677] | 321 | |
---|
[2fb99de] | 322 | print('... index done') |
---|
[d69b677] | 323 | |
---|
[ff99eae] | 324 | return iid |
---|