Repository: SciTools/iris
Pull request #6305 (2025-02-05 17:03:32)

Adds a warning for `iris.coord_categorisation.add_season_membership` that this is not saveable as a boolean coordinate.
## 🚀 Pull Request

### Description
<!-- Provide a clear description about your awesome pull request -->
<!-- Tell us all about your new feature, improvement, or bug fix -->


This addresses #5909, by raising a warning that since this adds a boolean, the result will not be saveable to a Netcdf file since it contains a boolean coordinate.

**Note for reviewer:**
I was not 100% sure on the type of error to raise either `iris.warnings.IrisSaveWarning` or `iris.warnings.IrisCfSaveWarning` since this a problem with Netcdf files.



Patch:
diff --git a/lib/iris/coord_categorisation.py b/lib/iris/coord_categorisation.py
index 12ad93a9c..460a131a1 100644
--- a/lib/iris/coord_categorisation.py
+++ b/lib/iris/coord_categorisation.py
@@ -19,12 +19,14 @@ import calendar
 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.IrisSaveWarning,
+        )
         return value.month in months
 
     add_categorised_coord(cube, name, coord, _season_membership)


Test patch:
diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst
index 32426623a..fcbf84364 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/tests/unit/coord_categorisation/test_coord_categorisation.py b/lib/iris/tests/unit/coord_categorisation/test_coord_categorisation.py
index daf4b7e3f..42e8718ae 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.coords
 import iris.cube
 import iris.exceptions
 from iris.tests import IrisTest
+from iris.warnings import IrisSaveWarning
 
 
 @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(IrisSaveWarning):
+        ccat.add_season_membership(cube, "time", season, name="in_season")