From 7c3f7e7314704145c6e134e3ab9b6218c975295e Mon Sep 17 00:00:00 2001 From: Henry Wright Date: Wed, 5 Feb 2025 16:57:06 +0000 Subject: [PATCH 1/2] add warning and test --- lib/iris/coord_categorisation.py | 8 ++++++++ .../coord_categorisation/test_coord_categorisation.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/lib/iris/coord_categorisation.py b/lib/iris/coord_categorisation.py index 12ad93a9c3..2d4088768d 100644 --- a/lib/iris/coord_categorisation.py +++ b/lib/iris/coord_categorisation.py @@ -19,12 +19,14 @@ import collections import inspect from typing import Callable +import warnings import cftime import numpy as np import iris.coords import iris.cube +import iris.warnings def add_categorised_coord( @@ -433,6 +435,12 @@ def add_season_membership(cube, coord, season, name="season_membership"): months = _months_in_season(season) def _season_membership(_, value: cftime.datetime) -> bool: + warnings.warn( + "The 'season_membership' coordinate is a boolean and will not be" + "saveable to a NetCDF file. If you need to save the file you can" + "convert them to integers using coord.points = coord.points.astype(int)", + category=iris.warnings.IrisCfSaveWarning, + ) return value.month in months add_categorised_coord(cube, name, coord, _season_membership) diff --git a/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py b/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py index daf4b7e3f2..f91106b4e8 100644 --- a/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py +++ b/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py @@ -16,6 +16,7 @@ import iris.cube import iris.exceptions from iris.tests import IrisTest +from iris.warnings import IrisCfSaveWarning @pytest.fixture( @@ -240,3 +241,9 @@ def test_add_season_membership_invalid_spec(cube): season = "maj" # not a season! with pytest.raises(ValueError): ccat.add_season_membership(cube, "time", season, name="maj_season") + + +def test_season_membership_save_warning(cube): + season = "djf" + with pytest.warns(IrisCfSaveWarning): + ccat.add_season_membership(cube, "time", season, name="in_season") From ceae92e0ba189e1e08703a5b9609cdfb2bf73976 Mon Sep 17 00:00:00 2001 From: Henry Wright Date: Tue, 6 May 2025 09:40:19 +0100 Subject: [PATCH 2/2] update warning classification and add whatsnew --- docs/src/whatsnew/latest.rst | 3 ++- lib/iris/coord_categorisation.py | 2 +- .../unit/coord_categorisation/test_coord_categorisation.py | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 0c1b8292b5..83c17224e1 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -36,7 +36,8 @@ This document explains the changes made to Iris for this release 🐛 Bugs Fixed ============= -#. N/A +#. `@HGWright`_ added a new warning to inform users that the boolean coordinate generated by + :meth:`iris.coord_categorisation.add_season_membership` is not saveable to netcdf. (:pull:`6305`) 💣 Incompatible Changes diff --git a/lib/iris/coord_categorisation.py b/lib/iris/coord_categorisation.py index 2d4088768d..460a131a18 100644 --- a/lib/iris/coord_categorisation.py +++ b/lib/iris/coord_categorisation.py @@ -439,7 +439,7 @@ def _season_membership(_, value: cftime.datetime) -> bool: "The 'season_membership' coordinate is a boolean and will not be" "saveable to a NetCDF file. If you need to save the file you can" "convert them to integers using coord.points = coord.points.astype(int)", - category=iris.warnings.IrisCfSaveWarning, + category=iris.warnings.IrisSaveWarning, ) return value.month in months diff --git a/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py b/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py index f91106b4e8..42e8718ae6 100644 --- a/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py +++ b/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py @@ -16,7 +16,7 @@ import iris.cube import iris.exceptions from iris.tests import IrisTest -from iris.warnings import IrisCfSaveWarning +from iris.warnings import IrisSaveWarning @pytest.fixture( @@ -245,5 +245,5 @@ def test_add_season_membership_invalid_spec(cube): def test_season_membership_save_warning(cube): season = "djf" - with pytest.warns(IrisCfSaveWarning): + with pytest.warns(IrisSaveWarning): ccat.add_season_membership(cube, "time", season, name="in_season")