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