SUP-1861: High level Python API for GRIB in ecCodes

This commit is contained in:
Shahram Najm 2016-12-19 14:53:34 +00:00
parent 6140e70a7b
commit 35d5bffa36
4 changed files with 9 additions and 8 deletions

View File

@ -79,12 +79,12 @@ class TestGribMessage(unittest.TestCase):
"""Metadata is read correctly from GribMessage."""
with GribFile(TESTGRIB) as grib:
msg = GribMessage(grib)
key_count = 251
key_count = 253
self.assertEqual(len(msg), key_count)
self.assertEqual(msg.size(), 160219)
self.assertEqual(len(msg.keys()), key_count)
def test_missing_message_behavior(self):
def test_missing_message_behaviour(self):
"""Missing messages are detected properly."""
with GribFile(TESTGRIB) as grib:
msg = GribMessage(grib)

View File

@ -30,7 +30,7 @@ class GribFile(file):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
def __exit__(self, exception_type, exception_value, traceback):
"""Close all open messages, release GRIB file handle and close file."""
while self.open_messages:
self.open_messages.pop().close()

View File

@ -34,7 +34,7 @@ class GribIndex(object):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
def __exit__(self, exception_type, exception_value, traceback):
"""Release GRIB message handle and inform file of release."""
while self.open_messages:
self.open_messages[0].close()
@ -79,9 +79,9 @@ class GribIndex(object):
"""Return number of distinct values for index key."""
return eccodes.codes_index_get_size(self.iid, key)
def values(self, key, type=str):
def values(self, key, ktype=str):
"""Return distinct values of index key."""
return eccodes.codes_index_get(self.iid, key, type)
return eccodes.codes_index_get(self.iid, key, ktype)
def add(self, filename):
"""Add ``filename`` to the ``GribIndex``."""

View File

@ -59,6 +59,7 @@ class GribMessage(object):
... # If desired, messages can be closed manually or used in with
... msg.close()
"""
def __enter__(self):
return self
@ -144,7 +145,7 @@ class GribMessage(object):
if grib_file is not None:
self.gid = eccodes.codes_grib_new_from_file(grib_file.file_handle)
if self.gid is None:
raise IOError("Grib file %s is exhausted" % grib_file.name)
raise IOError("Grib file %s is exhausted." % grib_file.name)
self.grib_file = grib_file
self.grib_file.message += 1
self.grib_file.open_messages.append(self)
@ -175,7 +176,7 @@ class GribMessage(object):
def get(self, key, ktype=None):
"""Get value of a given key as its native or specified type."""
if self.missing(key):
raise KeyError("Key is missing from message.")
raise KeyError("Value of key %s is MISSING." % key)
if eccodes.codes_get_size(self.gid, key) > 1:
ret = eccodes.codes_get_array(self.gid, key, ktype)
else: