Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
climix
climix
Commits
e5308697
Commit
e5308697
authored
Jul 28, 2021
by
Klaus Zimmermann
Browse files
Fix seasonal period (fixes #243)
parent
6faa668f
Changes
1
Hide whitespace changes
Inline
Side-by-side
climix/period.py
View file @
e5308697
...
...
@@ -38,7 +38,7 @@ class Monthly(Period):
class
Season
(
Period
):
YEAR
=
"
JFMAMJJASOND"
YEAR
=
"
jfmamjjasond"
*
3
MONTHS
=
[
"January"
,
"February"
,
...
...
@@ -54,29 +54,48 @@ class Season(Period):
"December"
,
]
def
__init__
(
self
,
specifier
):
super
().
__init__
(
"season_year"
,
"sem"
)
self
.
specifier
=
specifier
.
upper
()
self
.
first_month_number
=
(
self
.
YEAR
*
2
).
find
(
self
.
specifier
)
+
1
self
.
length
=
len
(
self
.
specifier
)
last_month_number
=
self
.
first_month_number
+
self
.
length
-
1
if
last_month_number
>
12
:
last_month_number
%=
12
def
selector
(
cell
):
m
=
cell
.
point
.
month
return
(
self
.
first_month_number
<=
m
<=
12
)
|
(
1
<=
m
<=
last_month_number
)
def
__init__
(
self
,
seasons
=
(
"djf"
,
"mam"
,
"jja"
,
"son"
)):
super
().
__init__
((
"season_year"
,
"season"
),
"sem"
)
if
isinstance
(
seasons
,
str
):
season
=
seasons
.
lower
()
self
.
first_month_number
=
self
.
YEAR
.
find
(
season
)
+
1
self
.
length
=
len
(
season
)
last_month_number
=
self
.
first_month_number
+
self
.
length
-
1
if
last_month_number
>
12
:
last_month_number
%=
12
def
selector
(
cell
):
m
=
cell
.
point
.
month
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
:
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
self
.
seasons
=
seasons
@
staticmethod
def
season_complement
(
season
):
season
=
season
.
lower
()
length
=
len
(
season
)
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
):
first_month
=
self
.
MONTHS
[
self
.
first_month_number
-
1
]
...
...
@@ -86,7 +105,16 @@ class Season(Period):
def
add_coord_categorisation
(
self
,
cube
):
iris
.
coord_categorisation
.
add_season_year
(
cube
,
self
.
input_coord
,
name
=
self
.
output_coord
cube
,
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment