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