[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): |
---|
[274f9ef] | 68 | '''Initialise an object of GribTools and assign a list of filenames. |
---|
| 69 | |
---|
| 70 | Parameters |
---|
| 71 | ---------- |
---|
| 72 | filenames : :obj:`list` of :obj:`strings` |
---|
| 73 | A list of filenames. |
---|
[64cf353] | 74 | |
---|
[274f9ef] | 75 | Return |
---|
| 76 | ------ |
---|
[64cf353] | 77 | |
---|
| 78 | ''' |
---|
| 79 | |
---|
[ff99eae] | 80 | self.filenames = filenames |
---|
[64cf353] | 81 | |
---|
| 82 | return |
---|
| 83 | |
---|
| 84 | |
---|
[ff99eae] | 85 | def get_keys(self, keynames, wherekeynames=[], wherekeyvalues=[]): |
---|
[274f9ef] | 86 | '''Get keyvalues for a given list of keynames a where statement |
---|
| 87 | can be given (list of key and list of values) |
---|
[64cf353] | 88 | |
---|
[274f9ef] | 89 | Parameters |
---|
| 90 | ---------- |
---|
| 91 | keynames : :obj:`list` of :obj:`string` |
---|
| 92 | List of keynames. |
---|
[64cf353] | 93 | |
---|
[274f9ef] | 94 | wherekeynames : :obj:`list` of :obj:`string`, optional |
---|
| 95 | Default value is an empty list. |
---|
[64cf353] | 96 | |
---|
[274f9ef] | 97 | wherekeyvalues : :obj:`list` of :obj:`string`, optional |
---|
| 98 | Default value is an empty list. |
---|
[64cf353] | 99 | |
---|
[274f9ef] | 100 | Return |
---|
| 101 | ------ |
---|
| 102 | return_list : :obj:`list` of :obj:`string` |
---|
| 103 | List of keyvalues for given keynames. |
---|
[64cf353] | 104 | ''' |
---|
| 105 | |
---|
[ff99eae] | 106 | fileid = open(self.filenames, 'r') |
---|
[64cf353] | 107 | |
---|
| 108 | return_list = [] |
---|
| 109 | |
---|
[d69b677] | 110 | while 1: |
---|
[0934db1] | 111 | gid = grib_new_from_file(fileid) |
---|
[d69b677] | 112 | |
---|
[aa275fc] | 113 | if gid is None: |
---|
[64cf353] | 114 | break |
---|
| 115 | |
---|
| 116 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 117 | raise Exception("Number of key values and key names must be \ |
---|
| 118 | the same. Give a value for each keyname!") |
---|
| 119 | |
---|
| 120 | select = True |
---|
| 121 | i = 0 |
---|
[d69b677] | 122 | for wherekey in wherekeynames: |
---|
[0934db1] | 123 | if not grib_is_defined(gid, wherekey): |
---|
[64cf353] | 124 | raise Exception("where key was not defined") |
---|
| 125 | |
---|
| 126 | select = (select and (str(wherekeyvalues[i]) == |
---|
[0934db1] | 127 | str(grib_get(gid, wherekey)))) |
---|
[64cf353] | 128 | i += 1 |
---|
| 129 | |
---|
[d69b677] | 130 | if select: |
---|
| 131 | llist = [] |
---|
| 132 | for key in keynames: |
---|
[0934db1] | 133 | llist.extend([str(grib_get(gid, key))]) |
---|
[d69b677] | 134 | return_list.append(llist) |
---|
[64cf353] | 135 | |
---|
[0934db1] | 136 | grib_release(gid) |
---|
[64cf353] | 137 | |
---|
[d69b677] | 138 | fileid.close() |
---|
[64cf353] | 139 | |
---|
[d69b677] | 140 | return return_list |
---|
[64cf353] | 141 | |
---|
| 142 | |
---|
[ff99eae] | 143 | def set_keys(self, fromfile, keynames, keyvalues, wherekeynames=[], |
---|
| 144 | wherekeyvalues=[], strict=False, filemode='w'): |
---|
[274f9ef] | 145 | '''Opens the file to read the grib messages and then write |
---|
| 146 | them to a new output file. By default all messages are |
---|
| 147 | written out. Also, the keyvalues of the passed list of |
---|
| 148 | keynames are set or only those meeting the where statement. |
---|
| 149 | (list of key and list of values). |
---|
[64cf353] | 150 | |
---|
[274f9ef] | 151 | Parameters |
---|
| 152 | ---------- |
---|
| 153 | fromfile : :obj:`string` |
---|
| 154 | Filename of the input file to read the grib messages from. |
---|
[64cf353] | 155 | |
---|
[274f9ef] | 156 | keynames : :obj:`list` of :obj:`string` |
---|
| 157 | List of keynames. Default is an empty list. |
---|
[64cf353] | 158 | |
---|
[274f9ef] | 159 | keyvalues : :obj:`list` of :obj:`string` |
---|
| 160 | List of keynames. Default is an empty list. |
---|
[64cf353] | 161 | |
---|
[274f9ef] | 162 | wherekeynames : :obj:`list` of :obj:`string`, optional |
---|
| 163 | Default value is an empty list. |
---|
[64cf353] | 164 | |
---|
[274f9ef] | 165 | wherekeyvalues : :obj:`list` of :obj:`string`, optional |
---|
| 166 | Default value is an empty list. |
---|
[64cf353] | 167 | |
---|
[274f9ef] | 168 | strict : :obj:`boolean`, optional |
---|
| 169 | Decides if everything from keynames and keyvalues |
---|
| 170 | is written out the grib file (False) or only those |
---|
| 171 | meeting the where statement (True). Default is False. |
---|
[64cf353] | 172 | |
---|
[274f9ef] | 173 | filemode : :obj:`string`, optional |
---|
| 174 | Sets the mode for the output file. Default is "w". |
---|
[64cf353] | 175 | |
---|
[274f9ef] | 176 | Return |
---|
| 177 | ------ |
---|
[64cf353] | 178 | |
---|
| 179 | ''' |
---|
[ff99eae] | 180 | fout = open(self.filenames, filemode) |
---|
[64cf353] | 181 | fin = open(fromfile) |
---|
| 182 | |
---|
[d69b677] | 183 | while 1: |
---|
[0934db1] | 184 | gid = grib_new_from_file(fin) |
---|
[64cf353] | 185 | |
---|
[aa275fc] | 186 | if gid is None: |
---|
[64cf353] | 187 | break |
---|
[d69b677] | 188 | |
---|
[64cf353] | 189 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 190 | raise Exception("Give a value for each keyname!") |
---|
| 191 | |
---|
| 192 | select = True |
---|
| 193 | i = 0 |
---|
[d69b677] | 194 | for wherekey in wherekeynames: |
---|
[0934db1] | 195 | if not grib_is_defined(gid, wherekey): |
---|
[64cf353] | 196 | raise Exception("where Key was not defined") |
---|
| 197 | |
---|
| 198 | select = (select and (str(wherekeyvalues[i]) == |
---|
[0934db1] | 199 | str(grib_get(gid, wherekey)))) |
---|
[64cf353] | 200 | i += 1 |
---|
| 201 | |
---|
[d69b677] | 202 | if select: |
---|
[64cf353] | 203 | i = 0 |
---|
[d69b677] | 204 | for key in keynames: |
---|
[0934db1] | 205 | grib_set(gid, key, keyvalues[i]) |
---|
[64cf353] | 206 | i += 1 |
---|
| 207 | |
---|
[0934db1] | 208 | grib_write(gid, fout) |
---|
[ff99eae] | 209 | |
---|
[0934db1] | 210 | grib_release(gid) |
---|
[64cf353] | 211 | |
---|
[d69b677] | 212 | fin.close() |
---|
| 213 | fout.close() |
---|
| 214 | |
---|
[64cf353] | 215 | return |
---|
| 216 | |
---|
| 217 | def copy(self, filename_in, selectWhere=True, |
---|
| 218 | keynames=[], keyvalues=[], filemode='w'): |
---|
[274f9ef] | 219 | '''Add the content of another input grib file to the objects file but |
---|
[64cf353] | 220 | only messages corresponding to keys/values passed to the function. |
---|
| 221 | The selectWhere switch decides if to copy the keys equal to (True) or |
---|
| 222 | different to (False) the keynames/keyvalues list passed to the function. |
---|
| 223 | |
---|
[274f9ef] | 224 | Parameters |
---|
| 225 | ---------- |
---|
| 226 | filename_in : :obj:`string` |
---|
| 227 | Filename of the input file to read the grib messages from. |
---|
| 228 | |
---|
| 229 | selectWhere : :obj:`boolean`, optional |
---|
| 230 | Decides if to copy the keynames and values equal to (True) or |
---|
| 231 | different to (False) the keynames/keyvalues list passed to the |
---|
| 232 | function. Default is True. |
---|
[64cf353] | 233 | |
---|
[274f9ef] | 234 | keynames : :obj:`list` of :obj:`string`, optional |
---|
| 235 | List of keynames. Default is an empty list. |
---|
[64cf353] | 236 | |
---|
[274f9ef] | 237 | keyvalues : :obj:`list` of :obj:`string`, optional |
---|
| 238 | List of keynames. Default is an empty list. |
---|
[64cf353] | 239 | |
---|
[274f9ef] | 240 | filemode : :obj:`string`, optional |
---|
| 241 | Sets the mode for the output file. Default is "w". |
---|
[64cf353] | 242 | |
---|
[274f9ef] | 243 | Return |
---|
| 244 | ------ |
---|
[64cf353] | 245 | |
---|
| 246 | ''' |
---|
| 247 | |
---|
| 248 | fin = open(filename_in) |
---|
[ff99eae] | 249 | fout = open(self.filenames, filemode) |
---|
[64cf353] | 250 | |
---|
[d69b677] | 251 | while 1: |
---|
[0934db1] | 252 | gid = grib_new_from_file(fin) |
---|
[d69b677] | 253 | |
---|
[aa275fc] | 254 | if gid is None: |
---|
[64cf353] | 255 | break |
---|
| 256 | |
---|
| 257 | if len(keynames) != len(keyvalues): |
---|
| 258 | raise Exception("Give a value for each keyname!") |
---|
| 259 | |
---|
| 260 | select = True |
---|
| 261 | i = 0 |
---|
[d69b677] | 262 | for key in keynames: |
---|
[0934db1] | 263 | if not grib_is_defined(gid, key): |
---|
[64cf353] | 264 | raise Exception("Key was not defined") |
---|
| 265 | |
---|
[d69b677] | 266 | if selectWhere: |
---|
[64cf353] | 267 | select = (select and (str(keyvalues[i]) == |
---|
[0934db1] | 268 | str(grib_get(gid, key)))) |
---|
[d69b677] | 269 | else: |
---|
[64cf353] | 270 | select = (select and (str(keyvalues[i]) != |
---|
[0934db1] | 271 | str(grib_get(gid, key)))) |
---|
[64cf353] | 272 | i += 1 |
---|
| 273 | |
---|
[d69b677] | 274 | if select: |
---|
[0934db1] | 275 | grib_write(gid, fout) |
---|
[64cf353] | 276 | |
---|
[0934db1] | 277 | grib_release(gid) |
---|
[64cf353] | 278 | |
---|
[d69b677] | 279 | fin.close() |
---|
| 280 | fout.close() |
---|
| 281 | |
---|
[64cf353] | 282 | return |
---|
| 283 | |
---|
| 284 | def index(self, index_keys=["mars"], index_file="my.idx"): |
---|
[274f9ef] | 285 | '''Create index file from a list of files if it does not exist or |
---|
| 286 | read an index file. |
---|
| 287 | |
---|
| 288 | Parameters |
---|
| 289 | ---------- |
---|
| 290 | index_keys: :obj:`list` of :obj:`string`, optional |
---|
| 291 | Contains the list of key parameter names from |
---|
| 292 | which the index is to be created. |
---|
| 293 | Default is a list with a single entry string "mars". |
---|
| 294 | |
---|
| 295 | index_file: :obj:`string`, optional |
---|
| 296 | Filename where the indices are stored. |
---|
| 297 | Default is "my.idx". |
---|
| 298 | |
---|
| 299 | Return |
---|
| 300 | ------ |
---|
| 301 | iid: :obj:`integer` |
---|
| 302 | Grib index id. |
---|
[64cf353] | 303 | ''' |
---|
[2fb99de] | 304 | print("... index will be done") |
---|
[ff99eae] | 305 | iid = None |
---|
[64cf353] | 306 | |
---|
[ff99eae] | 307 | if os.path.exists(index_file): |
---|
[0934db1] | 308 | iid = grib_index_read(index_file) |
---|
[2fb99de] | 309 | print("Use existing index file: %s " % (index_file)) |
---|
[d69b677] | 310 | else: |
---|
[ff99eae] | 311 | for filename in self.filenames: |
---|
[2fb99de] | 312 | print("Inputfile: %s " % (filename)) |
---|
[ff99eae] | 313 | if iid is None: |
---|
[0934db1] | 314 | iid = grib_index_new_from_file(filename, index_keys) |
---|
[d69b677] | 315 | else: |
---|
[0934db1] | 316 | grib_index_add_file(iid, filename) |
---|
[d69b677] | 317 | |
---|
[ff99eae] | 318 | if iid is not None: |
---|
[0934db1] | 319 | grib_index_write(iid, index_file) |
---|
[d69b677] | 320 | |
---|
[2fb99de] | 321 | print('... index done') |
---|
[d69b677] | 322 | |
---|
[ff99eae] | 323 | return iid |
---|