Skip to content

Commit

Permalink
Use paramId for param when shortName is tilde in GRIB data (#417)
Browse files Browse the repository at this point in the history
* Use paramId for param when shortName is tilde in GRIB data
  • Loading branch information
sandorkertesz authored Jul 17, 2024
1 parent f8c23b4 commit c22cf8d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/release_notes/version_0.9_updates.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
Version 0.9 Updates
/////////////////////////


Version 0.9.1
===============

Fixes
++++++

- When "shortName" is "~" in the GRIB header, :func:`metadata` now returns the value of "paramId" as a str for both the "param" and "shortName" keys. Previously "~" was returned for both these keys.


Version 0.9.0
===============

New features
++++++++++++++++

Expand Down
8 changes: 7 additions & 1 deletion src/earthkit/data/readers/grib/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,13 @@ def _key_name(key):
if not raise_on_missing:
_kwargs["default"] = default

return self._handle.get(_key_name(key), ktype=astype, **_kwargs)
key = _key_name(key)

# special case when "shortName" is "~".
v = self._handle.get(key, ktype=astype, **_kwargs)
if key == "shortName" and v == "~":
v = self._handle.get("paramId", ktype=str, **_kwargs)
return v

def _is_custom_key(self, key):
return key in self.CUSTOM_KEYS
Expand Down
Binary file added tests/data/tilde_shortname.grib
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/grib/test_grib_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,18 @@ def test_message(fl_type, array_backend):
assert v[:4] == b"GRIB"


@pytest.mark.parametrize("fl_type", ["file"])
@pytest.mark.parametrize("array_backend", [None])
def test_grib_tilde_shortname(fl_type, array_backend):
f = load_grib_data("tilde_shortname.grib", fl_type, array_backend, folder="data")

assert f[0].metadata("shortName") == "106"
assert f[0].metadata("shortName", astype=int) == 0
assert f[0].metadata("paramId") == 106
assert f[0].metadata("paramId", astype=int) == 106
assert f[0].metadata("param") == "106"


if __name__ == "__main__":
from earthkit.data.testing import main

Expand Down

0 comments on commit c22cf8d

Please sign in to comment.