eccodes/examples/python/grib_index.py

92 lines
2.3 KiB
Python
Raw Normal View History

2013-03-25 14:23:07 +00:00
#
2020-01-28 14:32:34 +00:00
# (C) Copyright 2005- ECMWF.
2013-03-25 14:23:07 +00:00
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
2015-11-12 15:15:50 +00:00
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
2013-03-25 14:23:07 +00:00
#
2015-11-12 15:15:50 +00:00
# Description: How to create and use an index to access GRIB messages from
# a file
2015-11-11 17:53:52 +00:00
2015-11-11 18:20:31 +00:00
import os
2021-09-15 11:28:05 +00:00
import sys
import traceback
2013-03-25 12:04:10 +00:00
from eccodes import *
2013-03-25 12:04:10 +00:00
2021-09-15 19:45:34 +00:00
INPUT = "../../data/index.grib"
2015-11-11 18:20:31 +00:00
VERBOSE = 1 # verbose error reporting
2013-03-25 12:04:10 +00:00
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
2021-09-15 19:45:34 +00:00
pools = list(map(tuple, args)) * kwds.get("repeat", 1)
2013-03-25 12:04:10 +00:00
result = [[]]
for pool in pools:
2015-11-11 18:20:31 +00:00
result = [x + [y] for x in result for y in pool]
2013-03-25 12:04:10 +00:00
for prod in result:
yield tuple(prod)
2015-11-11 18:20:31 +00:00
2013-03-25 12:04:10 +00:00
def example():
2015-11-11 18:20:31 +00:00
index_keys = ["shortName", "level", "number", "step"]
2013-03-25 12:04:10 +00:00
index_file = "my.idx"
iid = None
if os.path.exists(index_file):
iid = codes_index_read(index_file)
2013-03-25 12:04:10 +00:00
else:
2015-11-11 18:20:31 +00:00
iid = codes_index_new_from_file(INPUT, index_keys)
2013-03-25 12:04:10 +00:00
# multiple files can be added to an index:
# codes_index_add_file(iid,"grib file to add")
2013-03-25 12:04:10 +00:00
2015-11-11 18:20:31 +00:00
codes_index_write(iid, index_file)
2013-03-25 12:04:10 +00:00
index_vals = []
for key in index_keys:
2021-09-15 19:45:34 +00:00
print("%sSize=%d" % (key, codes_index_get_size(iid, key)))
2013-03-25 12:04:10 +00:00
2015-11-11 18:20:31 +00:00
key_vals = codes_index_get(iid, key)
print(" ".join(key_vals))
2013-03-25 12:04:10 +00:00
index_vals.append(key_vals)
for prod in product(*index_vals):
for i in range(len(index_keys)):
2015-11-11 18:20:31 +00:00
codes_index_select(iid, index_keys[i], prod[i])
2013-03-25 12:04:10 +00:00
while 1:
gid = codes_new_from_index(iid)
2015-11-11 18:20:31 +00:00
if gid is None:
break
2021-09-15 19:45:34 +00:00
print(
" ".join(["%s=%s" % (key, codes_get(gid, key)) for key in index_keys])
)
codes_release(gid)
2013-03-25 12:04:10 +00:00
codes_index_release(iid)
2015-11-11 18:20:31 +00:00
2013-03-25 12:04:10 +00:00
def main():
try:
example()
except CodesInternalError as err:
2013-03-25 12:04:10 +00:00
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
2021-09-15 19:45:34 +00:00
sys.stderr.write(err.msg + "\n")
2013-03-25 12:04:10 +00:00
return 1
2013-03-25 12:04:10 +00:00
if __name__ == "__main__":
sys.exit(main())