[8463d78] | 1 | #!/usr/bin/env python3 |
---|
[64cf353] | 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: |
---|
[44174de] | 15 | # (C) Copyright 2014-2019. |
---|
| 16 | # Anne Philipp, Leopold Haimberger |
---|
[991df6a] | 17 | # |
---|
[44174de] | 18 | # SPDX-License-Identifier: CC-BY-4.0 |
---|
[ff99eae] | 19 | # |
---|
[44174de] | 20 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 21 | # International License. To view a copy of this license, visit |
---|
| 22 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 23 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[991df6a] | 24 | #******************************************************************************* |
---|
[64cf353] | 25 | |
---|
| 26 | # ------------------------------------------------------------------------------ |
---|
| 27 | # MODULES |
---|
| 28 | # ------------------------------------------------------------------------------ |
---|
[4ef5ba9] | 29 | from __future__ import print_function |
---|
[aa275fc] | 30 | |
---|
[4ef5ba9] | 31 | import os |
---|
[991df6a] | 32 | |
---|
[64cf353] | 33 | # ------------------------------------------------------------------------------ |
---|
| 34 | # CLASS |
---|
| 35 | # ------------------------------------------------------------------------------ |
---|
[268ee86] | 36 | class GribUtil(object): |
---|
[64cf353] | 37 | ''' |
---|
| 38 | Class for GRIB utilities (new methods) based on GRIB API |
---|
[44174de] | 39 | |
---|
| 40 | The GRIB API provides all necessary tools to work directly with the |
---|
| 41 | grib files. Nevertheless, the GRIB API tools are very basic and are in |
---|
| 42 | direct connection with the grib files. This class provides some higher |
---|
| 43 | functions which apply a set of GRIB API tools together in the respective |
---|
| 44 | context. So, the class initially contains a list of grib files (their |
---|
| 45 | names) and the using program then applies the methods directly on the |
---|
| 46 | class objects without having to think about how the actual GRIB API |
---|
| 47 | tools have to be arranged. |
---|
[64cf353] | 48 | ''' |
---|
| 49 | # -------------------------------------------------------------------------- |
---|
[efdb01a] | 50 | # CLASS FUNCTIONS |
---|
[64cf353] | 51 | # -------------------------------------------------------------------------- |
---|
| 52 | def __init__(self, filenames): |
---|
[268ee86] | 53 | '''Initialise an object of GribUtil and assign a list of filenames. |
---|
[274f9ef] | 54 | |
---|
| 55 | Parameters |
---|
| 56 | ---------- |
---|
| 57 | filenames : :obj:`list` of :obj:`strings` |
---|
| 58 | A list of filenames. |
---|
[64cf353] | 59 | |
---|
[274f9ef] | 60 | Return |
---|
| 61 | ------ |
---|
[64cf353] | 62 | |
---|
| 63 | ''' |
---|
| 64 | |
---|
[ff99eae] | 65 | self.filenames = filenames |
---|
[64cf353] | 66 | |
---|
| 67 | return |
---|
| 68 | |
---|
| 69 | |
---|
[ff99eae] | 70 | def get_keys(self, keynames, wherekeynames=[], wherekeyvalues=[]): |
---|
[274f9ef] | 71 | '''Get keyvalues for a given list of keynames a where statement |
---|
| 72 | can be given (list of key and list of values) |
---|
[64cf353] | 73 | |
---|
[274f9ef] | 74 | Parameters |
---|
| 75 | ---------- |
---|
| 76 | keynames : :obj:`list` of :obj:`string` |
---|
| 77 | List of keynames. |
---|
[64cf353] | 78 | |
---|
[274f9ef] | 79 | wherekeynames : :obj:`list` of :obj:`string`, optional |
---|
| 80 | Default value is an empty list. |
---|
[64cf353] | 81 | |
---|
[274f9ef] | 82 | wherekeyvalues : :obj:`list` of :obj:`string`, optional |
---|
| 83 | Default value is an empty list. |
---|
[64cf353] | 84 | |
---|
[274f9ef] | 85 | Return |
---|
| 86 | ------ |
---|
| 87 | return_list : :obj:`list` of :obj:`string` |
---|
| 88 | List of keyvalues for given keynames. |
---|
[64cf353] | 89 | ''' |
---|
[4ef5ba9] | 90 | from eccodes import (codes_new_from_file, codes_is_defined, codes_get, |
---|
| 91 | codes_release) |
---|
[64cf353] | 92 | |
---|
| 93 | return_list = [] |
---|
| 94 | |
---|
[c77630a] | 95 | with open(self.filenames, 'rb') as fileid: |
---|
[d69b677] | 96 | |
---|
[c77630a] | 97 | gid = codes_new_from_file(fileid) |
---|
[64cf353] | 98 | |
---|
| 99 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 100 | raise Exception("Number of key values and key names must be \ |
---|
| 101 | the same. Give a value for each keyname!") |
---|
| 102 | |
---|
| 103 | select = True |
---|
| 104 | i = 0 |
---|
[d69b677] | 105 | for wherekey in wherekeynames: |
---|
[2e62398] | 106 | if not codes_is_defined(gid, wherekey): |
---|
[64cf353] | 107 | raise Exception("where key was not defined") |
---|
| 108 | |
---|
| 109 | select = (select and (str(wherekeyvalues[i]) == |
---|
[2e62398] | 110 | str(codes_get(gid, wherekey)))) |
---|
[64cf353] | 111 | i += 1 |
---|
| 112 | |
---|
[d69b677] | 113 | if select: |
---|
| 114 | llist = [] |
---|
| 115 | for key in keynames: |
---|
[2e62398] | 116 | llist.extend([str(codes_get(gid, key))]) |
---|
[d69b677] | 117 | return_list.append(llist) |
---|
[64cf353] | 118 | |
---|
[2e62398] | 119 | codes_release(gid) |
---|
[64cf353] | 120 | |
---|
[d69b677] | 121 | return return_list |
---|
[64cf353] | 122 | |
---|
| 123 | |
---|
[ff99eae] | 124 | def set_keys(self, fromfile, keynames, keyvalues, wherekeynames=[], |
---|
[c77630a] | 125 | wherekeyvalues=[], strict=False, filemode='wb'): |
---|
[274f9ef] | 126 | '''Opens the file to read the grib messages and then write |
---|
[268ee86] | 127 | the selected messages (with wherekeys) to a new output file. |
---|
| 128 | Also, the keyvalues of the passed list of keynames are set. |
---|
[64cf353] | 129 | |
---|
[274f9ef] | 130 | Parameters |
---|
| 131 | ---------- |
---|
| 132 | fromfile : :obj:`string` |
---|
| 133 | Filename of the input file to read the grib messages from. |
---|
[64cf353] | 134 | |
---|
[274f9ef] | 135 | keynames : :obj:`list` of :obj:`string` |
---|
[268ee86] | 136 | List of keynames to set in the selected messages. |
---|
| 137 | Default is an empty list. |
---|
[64cf353] | 138 | |
---|
[274f9ef] | 139 | keyvalues : :obj:`list` of :obj:`string` |
---|
[268ee86] | 140 | List of keyvalues to set in the selected messages. |
---|
| 141 | Default is an empty list. |
---|
[64cf353] | 142 | |
---|
[274f9ef] | 143 | wherekeynames : :obj:`list` of :obj:`string`, optional |
---|
[268ee86] | 144 | List of keynames to select correct message. |
---|
[274f9ef] | 145 | Default value is an empty list. |
---|
[64cf353] | 146 | |
---|
[274f9ef] | 147 | wherekeyvalues : :obj:`list` of :obj:`string`, optional |
---|
[268ee86] | 148 | List of keyvalues for keynames to select correct message. |
---|
[274f9ef] | 149 | Default value is an empty list. |
---|
[64cf353] | 150 | |
---|
[274f9ef] | 151 | strict : :obj:`boolean`, optional |
---|
| 152 | Decides if everything from keynames and keyvalues |
---|
| 153 | is written out the grib file (False) or only those |
---|
| 154 | meeting the where statement (True). Default is False. |
---|
[64cf353] | 155 | |
---|
[274f9ef] | 156 | filemode : :obj:`string`, optional |
---|
[c77630a] | 157 | Sets the mode for the output file. Default is "wb". |
---|
[64cf353] | 158 | |
---|
[274f9ef] | 159 | Return |
---|
| 160 | ------ |
---|
[64cf353] | 161 | |
---|
| 162 | ''' |
---|
[4ef5ba9] | 163 | from eccodes import (codes_grib_new_from_file, codes_is_defined, |
---|
| 164 | codes_get, codes_set, codes_write, |
---|
| 165 | codes_set_values, codes_release) |
---|
| 166 | |
---|
[268ee86] | 167 | if len(wherekeynames) != len(wherekeyvalues): |
---|
| 168 | raise Exception("Give a value for each keyname!") |
---|
| 169 | |
---|
[ff99eae] | 170 | fout = open(self.filenames, filemode) |
---|
[64cf353] | 171 | |
---|
[c77630a] | 172 | with open(fromfile, 'rb') as fin: |
---|
[268ee86] | 173 | gid = codes_grib_new_from_file(fin) |
---|
[64cf353] | 174 | |
---|
| 175 | select = True |
---|
| 176 | i = 0 |
---|
[d69b677] | 177 | for wherekey in wherekeynames: |
---|
[2e62398] | 178 | if not codes_is_defined(gid, wherekey): |
---|
[268ee86] | 179 | raise Exception("wherekey was not defined") |
---|
[64cf353] | 180 | |
---|
| 181 | select = (select and (str(wherekeyvalues[i]) == |
---|
[2e62398] | 182 | str(codes_get(gid, wherekey)))) |
---|
[64cf353] | 183 | i += 1 |
---|
| 184 | |
---|
[d69b677] | 185 | if select: |
---|
[64cf353] | 186 | i = 0 |
---|
[d69b677] | 187 | for key in keynames: |
---|
[268ee86] | 188 | if key == 'values': |
---|
| 189 | codes_set_values(gid, keyvalues[i]) |
---|
| 190 | else: |
---|
| 191 | codes_set(gid, key, keyvalues[i]) |
---|
[64cf353] | 192 | i += 1 |
---|
| 193 | |
---|
[268ee86] | 194 | codes_write(gid, fout) |
---|
[ff99eae] | 195 | |
---|
[2e62398] | 196 | codes_release(gid) |
---|
[64cf353] | 197 | |
---|
[d69b677] | 198 | fout.close() |
---|
| 199 | |
---|
[64cf353] | 200 | return |
---|
| 201 | |
---|
[268ee86] | 202 | def copy_dummy_msg(self, filename_in, selectWhere=True, |
---|
[c77630a] | 203 | keynames=[], keyvalues=[], filemode='wb'): |
---|
[274f9ef] | 204 | '''Add the content of another input grib file to the objects file but |
---|
[64cf353] | 205 | only messages corresponding to keys/values passed to the function. |
---|
| 206 | The selectWhere switch decides if to copy the keys equal to (True) or |
---|
| 207 | different to (False) the keynames/keyvalues list passed to the function. |
---|
| 208 | |
---|
[274f9ef] | 209 | Parameters |
---|
| 210 | ---------- |
---|
| 211 | filename_in : :obj:`string` |
---|
| 212 | Filename of the input file to read the grib messages from. |
---|
| 213 | |
---|
| 214 | selectWhere : :obj:`boolean`, optional |
---|
| 215 | Decides if to copy the keynames and values equal to (True) or |
---|
| 216 | different to (False) the keynames/keyvalues list passed to the |
---|
| 217 | function. Default is True. |
---|
[64cf353] | 218 | |
---|
[274f9ef] | 219 | keynames : :obj:`list` of :obj:`string`, optional |
---|
| 220 | List of keynames. Default is an empty list. |
---|
[64cf353] | 221 | |
---|
[274f9ef] | 222 | keyvalues : :obj:`list` of :obj:`string`, optional |
---|
[268ee86] | 223 | List of keyvalues. Default is an empty list. |
---|
[64cf353] | 224 | |
---|
[274f9ef] | 225 | filemode : :obj:`string`, optional |
---|
[c77630a] | 226 | Sets the mode for the output file. Default is "wb". |
---|
[64cf353] | 227 | |
---|
[274f9ef] | 228 | Return |
---|
| 229 | ------ |
---|
[64cf353] | 230 | |
---|
| 231 | ''' |
---|
[4ef5ba9] | 232 | from eccodes import (codes_grib_new_from_file, codes_is_defined, |
---|
| 233 | codes_get, codes_release, codes_write) |
---|
| 234 | |
---|
[268ee86] | 235 | if len(keynames) != len(keyvalues): |
---|
| 236 | raise Exception("Give a value for each keyname!") |
---|
[64cf353] | 237 | |
---|
[c77630a] | 238 | |
---|
[ff99eae] | 239 | fout = open(self.filenames, filemode) |
---|
[64cf353] | 240 | |
---|
[268ee86] | 241 | fields = 0 |
---|
| 242 | |
---|
[c77630a] | 243 | with open(filename_in, 'rb') as fin: |
---|
| 244 | if fields >= 1: |
---|
| 245 | fout.close() |
---|
| 246 | return |
---|
[d69b677] | 247 | |
---|
[c77630a] | 248 | gid = codes_grib_new_from_file(fin) |
---|
[64cf353] | 249 | |
---|
| 250 | select = True |
---|
| 251 | i = 0 |
---|
[d69b677] | 252 | for key in keynames: |
---|
[2e62398] | 253 | if not codes_is_defined(gid, key): |
---|
[64cf353] | 254 | raise Exception("Key was not defined") |
---|
| 255 | |
---|
[d69b677] | 256 | if selectWhere: |
---|
[64cf353] | 257 | select = (select and (str(keyvalues[i]) == |
---|
[2e62398] | 258 | str(codes_get(gid, key)))) |
---|
[d69b677] | 259 | else: |
---|
[64cf353] | 260 | select = (select and (str(keyvalues[i]) != |
---|
[2e62398] | 261 | str(codes_get(gid, key)))) |
---|
[64cf353] | 262 | i += 1 |
---|
| 263 | |
---|
[d69b677] | 264 | if select: |
---|
[268ee86] | 265 | fields = fields + 1 |
---|
[2e62398] | 266 | codes_write(gid, fout) |
---|
[64cf353] | 267 | |
---|
[2e62398] | 268 | codes_release(gid) |
---|
[64cf353] | 269 | |
---|
[d69b677] | 270 | fout.close() |
---|
| 271 | |
---|
[64cf353] | 272 | return |
---|
| 273 | |
---|
| 274 | def index(self, index_keys=["mars"], index_file="my.idx"): |
---|
[274f9ef] | 275 | '''Create index file from a list of files if it does not exist or |
---|
| 276 | read an index file. |
---|
| 277 | |
---|
| 278 | Parameters |
---|
| 279 | ---------- |
---|
| 280 | index_keys: :obj:`list` of :obj:`string`, optional |
---|
| 281 | Contains the list of key parameter names from |
---|
| 282 | which the index is to be created. |
---|
| 283 | Default is a list with a single entry string "mars". |
---|
| 284 | |
---|
| 285 | index_file: :obj:`string`, optional |
---|
| 286 | Filename where the indices are stored. |
---|
| 287 | Default is "my.idx". |
---|
| 288 | |
---|
| 289 | Return |
---|
| 290 | ------ |
---|
| 291 | iid: :obj:`integer` |
---|
| 292 | Grib index id. |
---|
[64cf353] | 293 | ''' |
---|
[4ef5ba9] | 294 | from eccodes import (codes_index_read, codes_index_new_from_file, |
---|
| 295 | codes_index_add_file, codes_index_write) |
---|
| 296 | |
---|
[2fb99de] | 297 | print("... index will be done") |
---|
[ff99eae] | 298 | iid = None |
---|
[64cf353] | 299 | |
---|
[ff99eae] | 300 | if os.path.exists(index_file): |
---|
[2e62398] | 301 | iid = codes_index_read(index_file) |
---|
[2fb99de] | 302 | print("Use existing index file: %s " % (index_file)) |
---|
[d69b677] | 303 | else: |
---|
[ff99eae] | 304 | for filename in self.filenames: |
---|
[2fb99de] | 305 | print("Inputfile: %s " % (filename)) |
---|
[ff99eae] | 306 | if iid is None: |
---|
[2e62398] | 307 | iid = codes_index_new_from_file(filename, index_keys) |
---|
[d69b677] | 308 | else: |
---|
[2e62398] | 309 | codes_index_add_file(iid, filename) |
---|
[d69b677] | 310 | |
---|
[ff99eae] | 311 | if iid is not None: |
---|
[2e62398] | 312 | codes_index_write(iid, index_file) |
---|
[d69b677] | 313 | |
---|
[2fb99de] | 314 | print('... index done') |
---|
[d69b677] | 315 | |
---|
[ff99eae] | 316 | return iid |
---|