Skip to content

Commit

Permalink
default type to float when fetching topLevel, bottomLevel, and level …
Browse files Browse the repository at this point in the history
…attributes

and if the level is actually an integer cast it back to integer

fixes ecmwf#195
  • Loading branch information
point9repeating committed Mar 4, 2021
1 parent caef214 commit 78306c0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cfgrib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def __del__(self) -> None:
def message_get(self, item, key_type=None, default=_MARKER):
# type: (str, T.Optional[type], T.Any) -> T.Any
"""Get value of a given key as its native or specified type."""
if key_type is None and item in ('level', 'topLevel', 'bottomLevel',):
key_type = float
try:
values = eccodes.codes_get_array(self.codes_id, item, key_type)
if values is None:
Expand All @@ -127,7 +129,10 @@ def message_get(self, item, key_type=None, default=_MARKER):
if len(values) == 1:
if isinstance(values, np.ndarray):
values = values.tolist()
return values[0]
val = values[0]
if key_type is float and val.is_integer():
val = int(val)
return val
return values

def message_set(self, item: str, value: T.Any) -> None:
Expand Down

0 comments on commit 78306c0

Please sign in to comment.