iris icon indicating copy to clipboard operation
iris copied to clipboard

`iris.coord_categorisation.add_season_year_preceding()`

Open kaedonkers opened this issue 3 years ago • 3 comments

✨ Feature Request

Add an alternative to iris.coord_categorisation.add_season_year() which assigns seasons which span two years to the preceding year instead of the following year.

Motivation

iris.coord_categorisation.add_season_year() is an excellent feature and very handy for comparing multiple timeseries datasets. However, it has an inherent assumption that a season which spans two years (i.e. includes December and January) will be assigned to the later rather than the earlier year. We have had a usecase where we want to compare the effects of monthly climate indices to several months of meteorological data after the climate index month (since we know that specific climate indices affect the weather variables we are analysing).

Proposed change

Proposed change/addition to iris.coord_categorisation (which essentially just removes the use of _month_year_adjusts()):

def add_season_year_preceding(
    cube, coord, name="season_year", seasons=("djf", "mam", "jja", "son")
):
    """
    Add a categorical year-of-season coordinate, with user specified
    seasons. Any season spanning two years will be assigned the preceding year.
    Args:
    * cube (:class:`iris.cube.Cube`):
        The cube containing 'coord'. The new coord will be added into
        it.
    * coord (:class:`iris.coords.Coord` or string):
        Coordinate in 'cube', or its name, representing time.
    Kwargs:
    * name (string):
        Name of the created coordinate. Defaults to "season_year".
    * seasons (:class:`list` of strings):
        List of seasons defined by month abbreviations. Each month must
        appear once and only once. Defaults to standard meteorological
        seasons ('djf', 'mam', 'jja', 'son').
    """
    # Check that the seasons are valid.
    _validate_seasons(seasons)

    # Define a categorisation function.
    def _season_year(coord, value):
        dt = _pt_date(coord, value)
        year = dt.year
        return year

    # Apply the categorisation.
    add_categorised_coord(cube, name, coord, _season_year)

kaedonkers avatar Jul 26 '22 15:07 kaedonkers

I think you still need some form of _month_year_adjusts, otherwise it's the same as add_year.

https://github.com/SciTools/iris/blob/09fac5b0b8936a8f9a3ed705af422c442124729d/lib/iris/coord_categorisation.py#L118-L122

rcomer avatar Jul 26 '22 16:07 rcomer

@kaedonkers how would you feel about an example in the docs, rather than more API? I'm assuming that this could be achieved with a few more lines of user code, but do correct me if I'm wrong.

trexfeathers avatar Jul 27 '22 09:07 trexfeathers

Hmm, good spot @rcomer. I'll need to give that a little more testing then. @trexfeathers Yes we achieved it with user code. I'll add it to my todo list.

kaedonkers avatar Aug 03 '22 08:08 kaedonkers

@kaedonkers any thoughts on this? It's OK to not be ~~OK~~ assigned! ♥

trexfeathers avatar Sep 20 '23 09:09 trexfeathers

Sorry, I never picked this up again. Is this something someone else can make happen?

kaedonkers avatar Sep 20 '23 11:09 kaedonkers