Skip to content

Adds a warning for iris.coord_categorisation.add_season_membership that this is not saveable as a boolean coordinate. #6305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/iris/coord_categorisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import iris.cube
import iris.exceptions
from iris.tests import IrisTest
from iris.warnings import IrisSaveWarning


@pytest.fixture(
Expand Down Expand Up @@ -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")
Loading