Skip to content

Commit

Permalink
review actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marqh committed Jul 25, 2017
1 parent 1d8bf52 commit 2310ef6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
10 changes: 9 additions & 1 deletion lib/iris/fileformats/grib/_load_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,21 @@ def _unscale(v, f):

if isinstance(value, Iterable) or isinstance(factor, Iterable):
def _masker(item):
# This is a small work around for an edge case, which is not
# evident in any of our sample GRIB2 messages, where an array
# of header elements contains missing values.
# iris.fileformats.grib.message returns these as None, but they
# are wanted as a numerical masked array, so a temporary mdi
# value is used, selected from a legacy implementation of iris,
# to construct the masked array. The valure is transient, only in
# scope for this function.
numerical_mdi = 2 ** 32 - 1
item = [numerical_mdi if i is None else i for i in item]
result = ma.masked_equal(item, numerical_mdi)
if ma.count_masked(result):
# Circumvent downstream NumPy "RuntimeWarning"
# of "overflow encountered in power" in _unscale
# for data containing _MDI.
# for data containing _MDI. Remove transient _MDI value.
result.data[result.mask] = 0
return result
value = _masker(value)
Expand Down
24 changes: 15 additions & 9 deletions lib/iris/fileformats/grib/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,9 @@ def _get_key_value(self, key):
elif key in ('typeOfFirstFixedSurface', 'typeOfSecondFixedSurface'):
# By default these values are returned as unhelpful strings but
# we can use int representation to compare against instead.
res = gribapi.grib_get(self._message_id, key, int)
if gribapi.grib_is_missing(self._message_id, key) == 1:
res = None
res = _get_value_or_missing(self._message_id, key)
else:
res = gribapi.grib_get(self._message_id, key)
if gribapi.grib_is_missing(self._message_id, key) == 1:
res = None
res = _get_value_or_missing(self._message_id, key)
return res

def get_computed_key(self, key):
Expand All @@ -485,11 +481,21 @@ def get_computed_key(self, key):
if key in vector_keys:
res = gribapi.grib_get_array(self._message_id, key)
else:
res = gribapi.grib_get(self._message_id, key)
if gribapi.grib_is_missing(self._message_id, key) == 1:
res = None
res = _get_value_or_missing(self._message_id, key)
return res

def keys(self):
"""Return coded keys available in this Section."""
return self._keys

def _get_value_or_missing(message_id, key):
"""
Return value of header element, or None if value is encoded as missing.
Implementation of Regulations 92.1.4 and 92.1.5 via ECCodes.
"""
if gribapi.grib_is_missing(self._message_id, key) == 1:
res = None
else:
res = gribapi.grib_get(self._message_id, key)
return result

0 comments on commit 2310ef6

Please sign in to comment.