From 7e5911be6c3c2242471c73703cf3628d501b1a21 Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Fri, 6 Jun 2025 17:25:59 +0100 Subject: [PATCH 1/2] Fix crash when getting gridSpec GRIB metadata key --- docs/release_notes/version_0.14_updates.rst | 3 ++- src/earthkit/data/readers/grib/codes.py | 10 ++++++++++ tests/grib/test_grib_metadata.py | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/release_notes/version_0.14_updates.rst b/docs/release_notes/version_0.14_updates.rst index 1e644d71b..6d53f822f 100644 --- a/docs/release_notes/version_0.14_updates.rst +++ b/docs/release_notes/version_0.14_updates.rst @@ -8,7 +8,8 @@ Version 0.14.4 Fixes ++++++ -- Now, dependencies for GeoTIFF support are not installed when earthkit-data is installed with ``pip install earthkit-data[all]``. This step was necessary to make installation work when GDAL is not available. These dependencies need to be installed separately with ``pip install earthkit-data[geotiff]``. See :ref:`install`. +- Fixed issue when getting the "gridSpec" GRIB metadata key with a default value caused a crash when ecCodes 2.41.0 was used. (:pr:`720`). +- Now, dependencies for GeoTIFF support are not installed when earthkit-data is installed with ``pip install earthkit-data[all]``. This step was necessary to make installation work when GDAL is not available. These dependencies need to be installed separately with ``pip install earthkit-data[geotiff]``. See :ref:`install`. (:pr:`718`). Version 0.14.3 diff --git a/src/earthkit/data/readers/grib/codes.py b/src/earthkit/data/readers/grib/codes.py index 597f81916..4a6ba94e6 100644 --- a/src/earthkit/data/readers/grib/codes.py +++ b/src/earthkit/data/readers/grib/codes.py @@ -151,6 +151,16 @@ def get(self, name, ktype=None, **kwargs): return self.get_values() elif name == "md5GridSection": return self.get_md5GridSection() + elif name == "gridSpec": + # Temporary measure because from ecCodes 2.41.0, when we ask for the gridSpec + # ecCodes raises a gribapi.errors.FunctionNotImplementedError exception, which is + # not caught by the high level eccodes API even if a default value is provided. + if "default" in kwargs: + try: + return super().get(name, ktype, **kwargs) + except Exception as e: + if "FunctionNotImplementedError" in str(type(e)): + return kwargs.get("default", None) return super().get(name, ktype, **kwargs) diff --git a/tests/grib/test_grib_metadata.py b/tests/grib/test_grib_metadata.py index 9e7eaac55..6d5a5e7ce 100644 --- a/tests/grib/test_grib_metadata.py +++ b/tests/grib/test_grib_metadata.py @@ -544,6 +544,13 @@ def test_grib_tilde_shortname(fl_type): assert f[0].metadata(namespace="parameter")["shortName"] == "106" +def test_grib_gridspec_key(): + ds = from_source("file", earthkit_examples_file("test.grib")) + + ds[0].metadata("gridSpec", default=None) # Should not raise an error + ds.metadata("gridSpec", default=None) # Should not raise an error + + if __name__ == "__main__": from earthkit.data.testing import main From 8c1394d955a4f667461278ddc4ca85dee5df768b Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Fri, 6 Jun 2025 17:32:54 +0100 Subject: [PATCH 2/2] Fix crash when getting gridSpec GRIB metadata key --- docs/release_notes/version_0.14_updates.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/release_notes/version_0.14_updates.rst b/docs/release_notes/version_0.14_updates.rst index 6d53f822f..c9099834f 100644 --- a/docs/release_notes/version_0.14_updates.rst +++ b/docs/release_notes/version_0.14_updates.rst @@ -8,7 +8,7 @@ Version 0.14.4 Fixes ++++++ -- Fixed issue when getting the "gridSpec" GRIB metadata key with a default value caused a crash when ecCodes 2.41.0 was used. (:pr:`720`). +- Fixed issue when getting the "gridSpec" GRIB metadata key with a default value caused a crash when ecCodes 2.41.0 was used. (:pr:`719`). - Now, dependencies for GeoTIFF support are not installed when earthkit-data is installed with ``pip install earthkit-data[all]``. This step was necessary to make installation work when GDAL is not available. These dependencies need to be installed separately with ``pip install earthkit-data[geotiff]``. See :ref:`install`. (:pr:`718`).