Repository: ecmwf/earthkit-data
Pull request #456 (2024-09-16 12:47:22)

Fix cube detailed error report
This PR fixes the issue when the missing "number" metadata key caused a crash when generating a detailed report for a FieldCube.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/ecmwf/earthkit-data/pull/456?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 [(`fe1a294`)](https://app.codecov.io/gh/ecmwf/earthkit-data/commit/fe1a294e28d4e918d9e748ab7a9b48d0bee85675?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ecmwf) to head [(`342ac04`)](https://app.codecov.io/gh/ecmwf/earthkit-data/commit/342ac04b6cfbc05737bb46e83fb5aea416347733?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            @@
##           develop     #456   +/-   ##
========================================
  Coverage    90.56%   90.57%           
========================================
  Files          123      123           
  Lines         8333     8341    +8     
  Branches       701      702    +1     
========================================
+ Hits          7547     7555    +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/456?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/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