From 60cfad3a39aa57ce16884b2dcc6e2a660a8e9dc3 Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Tue, 17 Sep 2024 08:16:31 +0100 Subject: [PATCH] Fix FieldCube crash when generating detailed error report (#458) --- docs/release_notes/version_0.10_updates.rst | 9 +++++++++ src/earthkit/data/indexing/cube.py | 4 ++-- tests/grib/test_grib_cube.py | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/release_notes/version_0.10_updates.rst b/docs/release_notes/version_0.10_updates.rst index 200a34ad..214f57ad 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 8601226d..cc34cb78 100644 --- a/src/earthkit/data/indexing/cube.py +++ b/src/earthkit/data/indexing/cube.py @@ -87,7 +87,7 @@ def __init__( # 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 @@ def __init__( 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 diff --git a/tests/grib/test_grib_cube.py b/tests/grib/test_grib_cube.py index 7f14ddcc..cb96439d 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