Commit fffa50a2 authored by Klaus Zimmermann's avatar Klaus Zimmermann
Browse files

Fix seasonal period (backport of !168)

parent ff17c6cf
...@@ -40,7 +40,7 @@ class Monthly(Period): ...@@ -40,7 +40,7 @@ class Monthly(Period):
class Season(Period): class Season(Period):
YEAR = 'JFMAMJJASOND' YEAR = "jfmamjjasond" * 3
MONTHS = [ MONTHS = [
'January', 'January',
'February', 'February',
...@@ -56,29 +56,48 @@ class Season(Period): ...@@ -56,29 +56,48 @@ class Season(Period):
'December', 'December',
] ]
def __init__(self, specifier): def __init__(self, seasons=("djf", "mam", "jja", "son")):
super().__init__('season_year', 'sem') super().__init__(("season_year", "season"), "sem")
self.specifier = specifier.upper() if isinstance(seasons, str):
self.first_month_number = (self.YEAR*2).find(self.specifier)+1 season = seasons.lower()
self.length = len(self.specifier) self.first_month_number = self.YEAR.find(season) + 1
last_month_number = self.first_month_number + self.length - 1 self.length = len(season)
if last_month_number > 12: last_month_number = self.first_month_number + self.length - 1
last_month_number %= 12 if last_month_number > 12:
last_month_number %= 12
def selector(cell):
m = cell.point.month def selector(cell):
return (self.first_month_number <= m <= 12) | ( m = cell.point.month
1 <= m <= last_month_number return (self.first_month_number <= m <= 12) | (
) 1 <= m <= last_month_number
)
else:
def selector(cell):
m = cell.point.month
return self.first_month_number <= m <= last_month_number
self.constraint = iris.Constraint(time=selector)
self.last_month_number = last_month_number
complement_season = Season.season_complement(season)
self.seasons = (season, complement_season)
else: else:
self.seasons = seasons
def selector(cell):
m = cell.point.month @staticmethod
return self.first_month_number <= m <= last_month_number def season_complement(season):
season = season.lower()
self.constraint = iris.Constraint(time=selector) length = len(season)
self.last_month_number = last_month_number index = Season.YEAR.find(season)
if index < 0:
# Can't match the season, raise an error.
raise ValueError("unrecognised season: {!s}".format(season))
complement_length = 12 - length
complement_start = index + length
complement_end = complement_start + complement_length
complement_season = Season.YEAR[complement_start:complement_end]
return complement_season
def long_label(self): def long_label(self):
first_month = self.MONTHS[self.first_month_number-1] first_month = self.MONTHS[self.first_month_number-1]
...@@ -87,9 +106,18 @@ class Season(Period): ...@@ -87,9 +106,18 @@ class Season(Period):
return long_label return long_label
def add_coord_categorisation(self, cube): def add_coord_categorisation(self, cube):
iris.coord_categorisation.add_season_year(cube, iris.coord_categorisation.add_season_year(
self.input_coord, cube,
name=self.output_coord) self.input_coord,
name="season_year",
seasons=self.seasons,
)
iris.coord_categorisation.add_season(
cube,
self.input_coord,
name="season",
seasons=self.seasons,
)
return self.output_coord return self.output_coord
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment