Skip to content

Commit 2310ef6

Browse files
committed
review actions
1 parent 1d8bf52 commit 2310ef6

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/iris/fileformats/grib/_load_convert.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,21 @@ def _unscale(v, f):
158158

159159
if isinstance(value, Iterable) or isinstance(factor, Iterable):
160160
def _masker(item):
161+
# This is a small work around for an edge case, which is not
162+
# evident in any of our sample GRIB2 messages, where an array
163+
# of header elements contains missing values.
164+
# iris.fileformats.grib.message returns these as None, but they
165+
# are wanted as a numerical masked array, so a temporary mdi
166+
# value is used, selected from a legacy implementation of iris,
167+
# to construct the masked array. The valure is transient, only in
168+
# scope for this function.
161169
numerical_mdi = 2 ** 32 - 1
162170
item = [numerical_mdi if i is None else i for i in item]
163171
result = ma.masked_equal(item, numerical_mdi)
164172
if ma.count_masked(result):
165173
# Circumvent downstream NumPy "RuntimeWarning"
166174
# of "overflow encountered in power" in _unscale
167-
# for data containing _MDI.
175+
# for data containing _MDI. Remove transient _MDI value.
168176
result.data[result.mask] = 0
169177
return result
170178
value = _masker(value)

lib/iris/fileformats/grib/message.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,9 @@ def _get_key_value(self, key):
458458
elif key in ('typeOfFirstFixedSurface', 'typeOfSecondFixedSurface'):
459459
# By default these values are returned as unhelpful strings but
460460
# we can use int representation to compare against instead.
461-
res = gribapi.grib_get(self._message_id, key, int)
462-
if gribapi.grib_is_missing(self._message_id, key) == 1:
463-
res = None
461+
res = _get_value_or_missing(self._message_id, key)
464462
else:
465-
res = gribapi.grib_get(self._message_id, key)
466-
if gribapi.grib_is_missing(self._message_id, key) == 1:
467-
res = None
463+
res = _get_value_or_missing(self._message_id, key)
468464
return res
469465

470466
def get_computed_key(self, key):
@@ -485,11 +481,21 @@ def get_computed_key(self, key):
485481
if key in vector_keys:
486482
res = gribapi.grib_get_array(self._message_id, key)
487483
else:
488-
res = gribapi.grib_get(self._message_id, key)
489-
if gribapi.grib_is_missing(self._message_id, key) == 1:
490-
res = None
484+
res = _get_value_or_missing(self._message_id, key)
491485
return res
492486

493487
def keys(self):
494488
"""Return coded keys available in this Section."""
495489
return self._keys
490+
491+
def _get_value_or_missing(message_id, key):
492+
"""
493+
Return value of header element, or None if value is encoded as missing.
494+
Implementation of Regulations 92.1.4 and 92.1.5 via ECCodes.
495+
496+
"""
497+
if gribapi.grib_is_missing(self._message_id, key) == 1:
498+
res = None
499+
else:
500+
res = gribapi.grib_get(self._message_id, key)
501+
return result

0 commit comments

Comments
 (0)