Repository: ecmwf/earthkit-data
Pull request #458 (2024-09-17 06:42:58)

Fix FieldCube crash when generating detailed error report

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/458?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:
> Project coverage is 90.57%. Comparing base [(`5fc1e6a`)](https://app.codecov.io/gh/ecmwf/earthkit-data/commit/5fc1e6a812787741bc23ef217a97814a49868f9e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf) to head [(`a436a98`)](https://app.codecov.io/gh/ecmwf/earthkit-data/commit/a436a98bf6cafd1775e14de8512e43ae7880c2a3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf).

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


```diff
@@           Coverage Diff           @@
##             main     #458   +/-   ##
=======================================
  Coverage   90.56%   90.57%           
=======================================
  Files         123      123           
  Lines        8329     8337    +8     
  Branches      701      702    +1     
=======================================
+ Hits         7543     7551    +8     
  Misses        655      655           
  Partials      131      131           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/458?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 200a34a..214f57a 100644
--- a/docs/release_notes/version_0.10_updates.rst
+++ b/docs/release_notes/version_0.10_updates.rst
@@ -2,6 +2,15 @@ Version 0.10 Updates
 /////////////////////////
 
 
+Version 0.10.3
+===============
+
+Fixes
+++++++
+
+- Fixed issue when building a FieldCube crashed while generating error message due to missing "number" metadata key in input GRIB data (:pr:`456`).
+
+
 Version 0.10.2
 ===============
 
diff --git a/src/earthkit/data/indexing/cube.py b/src/earthkit/data/indexing/cube.py
index 8601226..cc34cb7 100644
--- a/src/earthkit/data/indexing/cube.py
+++ b/src/earthkit/data/indexing/cube.py
@@ -87,7 +87,7 @@ class FieldCube:
         # print(self.source[3])
 
         # Get a mapping of user names to unique values
-        # With possible reduce dimentionality if the user use 'level+param'
+        # With possible reduce dimensionality if the user use 'level+param'
         self.user_coords = self.source.unique_values(*names, remapping=remapping, patches=patches)
 
         self.user_shape = tuple(len(v) for k, v in self.user_coords.items())
@@ -98,7 +98,7 @@ class FieldCube:
                 details.append(f"{key=} ({len(v)}) {v}")
             assert not isinstance(self.source, str), f"Not expecting a str here ({self.source})"
             for i, f in enumerate(self.source):
-                details.append(f"{i}={f} {f.metadata('number')}")
+                details.append(f"{i}={f} {f.metadata('number', default=None)}")
                 if i > 30:
                     details.append("...")
                     break


Test patch:
diff --git a/tests/grib/test_grib_cube.py b/tests/grib/test_grib_cube.py
index 7f14ddc..cb96439 100644
--- a/tests/grib/test_grib_cube.py
+++ b/tests/grib/test_grib_cube.py
@@ -10,9 +10,11 @@
 #
 
 import numpy as np
+import pytest
 
 from earthkit.data import from_source
 from earthkit.data.testing import earthkit_examples_file
+from earthkit.data.testing import earthkit_test_data_file
 
 
 def test_grib_cube():
@@ -124,6 +126,15 @@ def test_grib_cubelet():
         assert np.isclose(cb.to_numpy()[0, 0], ref[i])
 
 
+def test_grib_cube_non_hypercube():
+    ds = from_source("file", earthkit_examples_file("tuv_pl.grib"))
+    ds += from_source("file", earthkit_test_data_file("ml_data.grib"))[:2]
+    assert len(ds) == 18 + 2
+
+    with pytest.raises(ValueError):
+        ds.cube("param", "level")
+
+
 if __name__ == "__main__":
     from earthkit.data.testing import main