Repository: ecmwf/earthkit-data
Pull request #477 (2024-10-08 10:11:23)

Fix unknown param in grib namespaces
With this PR when "param" or "shortName" in a namespace is "\~" in the GRIB header, `metadata()` returns the value of "paramId" as a str for both these keys in the relevant namespaces. Previously "~" was returned.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/477?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) report for BASE (`main@7849651`). [Learn more](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf#section-missing-base-commit) about missing BASE report.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #477   +/-   ##
=======================================
  Coverage        ?   90.57%           
=======================================
  Files           ?      123           
  Lines           ?     8340           
  Branches        ?      702           
=======================================
  Hits            ?     7554           
  Misses          ?      655           
  Partials        ?      131           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/477?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf).


Patch:
diff --git a/docs/release_notes/version_0.10_updates.rst b/docs/release_notes/version_0.10_updates.rst
index 778d12c..d085b94 100644
--- a/docs/release_notes/version_0.10_updates.rst
+++ b/docs/release_notes/version_0.10_updates.rst
@@ -1,6 +1,16 @@
 Version 0.10 Updates
 /////////////////////////
 
+Version 0.10.7
+===============
+
+Fixes
+++++++
+
+- When "param" or "shortName" in a namespace is "~" in the GRIB header :func:`metadata` now returns the value of "paramId" as a str for both these keys in the relevant namespaces. Previously "~" was returned.
+
+
+Version 0.9.0
 
 Version 0.10.4
 ===============
diff --git a/src/earthkit/data/readers/grib/metadata.py b/src/earthkit/data/readers/grib/metadata.py
index 297d719..b921c79 100644
--- a/src/earthkit/data/readers/grib/metadata.py
+++ b/src/earthkit/data/readers/grib/metadata.py
@@ -415,7 +415,14 @@ class GribMetadata(Metadata):
 
         if namespace == "default" or namespace == "":
             namespace = None
-        return self._handle.as_namespace(namespace)
+        r = self._handle.as_namespace(namespace)
+
+        # special case when  "param" is "~".
+        if r is not None:
+            for k in ("shortName", "param"):
+                if k in r and r[k] == "~":
+                    r[k] = self.get(k)
+        return r
 
     @property
     def geography(self):


Test patch:
diff --git a/tests/grib/test_grib_metadata.py b/tests/grib/test_grib_metadata.py
index 8a18cea..ed01662 100644
--- a/tests/grib/test_grib_metadata.py
+++ b/tests/grib/test_grib_metadata.py
@@ -558,6 +558,9 @@ def test_grib_tilde_shortname(fl_type, array_backend):
     assert f[0].metadata("paramId", astype=int) == 106
     assert f[0].metadata("param") == "106"
 
+    assert f[0].metadata(namespace="mars")["param"] == "106"
+    assert f[0].metadata(namespace="parameter")["shortName"] == "106"
+
 
 if __name__ == "__main__":
     from earthkit.data.testing import main