Skip to content
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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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.IrisCfSaveWarning,
)
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 IrisCfSaveWarning


@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(IrisCfSaveWarning):
ccat.add_season_membership(cube, "time", season, name="in_season")
Loading