Skip to content

Commit 6b1fdca

Browse files
authored
Merge pull request #35 from znicholls/update-temporal-labels
Add support for tminavg and tmaxavg
2 parents 129009e + 1048e34 commit 6b1fdca

4 files changed

Lines changed: 92 additions & 11 deletions

File tree

changelog/35.breaking.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Added support for "tminavg" and "tmaxavg" in line with updates to Table F1 of [Taylor et al.](https://docs.google.com/document/d/19jzecgymgiiEsTDzaaqeLP6pTvLT-NzCMaq-wu-QoOc/edit?pli=1&tab=t.0) as at 22 September 2025
2+
3+
If you have a variable with "time4" as a dimension and "time: max" ("time: min") in the cell methods, the temporal label is now "tmaxavg" ("tminavg") rather than "ti" as it was previously.
4+
5+
In addition, if you have "time: max", "time: min" or "time: sum" in your cell methods
6+
but do not have a "time" dimension, your temporal label will now be "ti"
7+
rather than its previously value.

src/cmip_branded_variable_mapper/temporal_label.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
DimensionMapper,
1010
)
1111

12-
TEMPORAL_LABEL_CELL_METHODS_MAPPER = CellMethodsSubStringMapper(
12+
TEMPORAL_LABEL_CELL_METHODS_INITIAL_TESTS_MAPPER = CellMethodsSubStringMapper(
1313
sub_string_map={
1414
"time: max": "tmax",
1515
"time: min": "tmin",
@@ -18,6 +18,11 @@
1818
)
1919
"""
2020
Mapper from sub-strings of cell methods to the temporal label
21+
22+
These are for the 'initial tests'
23+
i.e. the cell methods which are checked first.
24+
There is a coupling with dimensions in the logic in
25+
[get_temporal_label][(m).], be careful!
2126
"""
2227

2328
TEMPORAL_LABEL_DIMENSIONS_MAPPER = DimensionMapper(
@@ -32,14 +37,30 @@
3237
Mapper from dimensions to the temporal label
3338
"""
3439

40+
TEMPORAL_LABEL_CELL_METHODS_TIME4_TESTS_MAPPER = CellMethodsSubStringMapper(
41+
sub_string_map={
42+
"time: max": "tmaxavg",
43+
"time: min": "tminavg",
44+
}
45+
)
46+
"""
47+
Mapper from sub-strings of cell methods to the temporal label for "time4" data
3548
36-
def get_temporal_label(
49+
I.e. for data which has "time4" as a dimension
50+
"""
51+
52+
53+
def get_temporal_label( # noqa: PLR0913
3754
cell_methods: str | None,
3855
dimensions: tuple[str, ...],
39-
cell_methods_mapper: CellMethodsSubStringMapper = (
40-
TEMPORAL_LABEL_CELL_METHODS_MAPPER
56+
cell_methods_initial_mapper: CellMethodsSubStringMapper = (
57+
TEMPORAL_LABEL_CELL_METHODS_INITIAL_TESTS_MAPPER
4158
),
59+
cell_methods_initial_required_dimension: str = "time",
4260
dimensions_mapper: DimensionMapper = TEMPORAL_LABEL_DIMENSIONS_MAPPER,
61+
cell_methods_time4_mapper: CellMethodsSubStringMapper = (
62+
TEMPORAL_LABEL_CELL_METHODS_TIME4_TESTS_MAPPER
63+
),
4364
fallback: str = "ti",
4465
) -> str:
4566
"""
@@ -59,12 +80,29 @@ def get_temporal_label(
5980
dimensions
6081
Dimensions of the variable
6182
62-
cell_methods_mapper
83+
cell_methods_initial_mapper
6384
Mapper to use to get values based on cell methods
6485
86+
This is for the 'initial' tests
87+
i.e. checks of cell methods which are performed
88+
before any other checks.
89+
90+
cell_methods_initial_required_dimension
91+
Dimension required for the result of using `cell_methods_initial_mapper`
92+
to be returned.
93+
94+
If this dimension is not found, then the other mapping checks are performed.
95+
6596
dimensions_mapper
6697
Mapper to use to get values based on dimensions
6798
99+
If you include "time4" as a mapping here,
100+
you can make a mess as you will effectively
101+
disable any use of `cell_methods_time4_mapper`.
102+
103+
cell_methods_time4_mapper
104+
Mapper to use to get values based on cell methods if "time4" is in dimensions
105+
68106
fallback
69107
Value to return if no other conditions are matched
70108
@@ -75,10 +113,18 @@ def get_temporal_label(
75113
"""
76114
if cell_methods is not None:
77115
# Check cell methods first
78-
if (match := cell_methods_mapper.get_value(cell_methods)) is not None:
79-
return match
116+
if (match := cell_methods_initial_mapper.get_value(cell_methods)) is not None:
117+
if cell_methods_initial_required_dimension in dimensions:
118+
return match
119+
120+
# We matched cell methods but not the required dimension,
121+
# fall through to other tests
80122

81123
if (match := dimensions_mapper.get_value(dimensions)) is not None:
82124
return match
83125

126+
if cell_methods is not None and "time4" in dimensions:
127+
if (match := cell_methods_time4_mapper.get_value(cell_methods)) is not None:
128+
return match
129+
84130
return fallback

tests/unit/test_map_to_cmip_branded_variable.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,39 @@
1515
pytest.param(
1616
"area: mean where land time: max",
1717
("time", "lat", "lon"),
18-
# cell methods wins out over dimension
1918
"tmax",
2019
id="tmax",
2120
),
21+
pytest.param(
22+
"area: mean where land time: max",
23+
("lat", "lon"),
24+
"ti",
25+
id="tmax-missing-dim",
26+
),
2227
pytest.param(
2328
"area: mean where land time: min",
2429
("time", "lat", "lon"),
25-
# cell methods wins out over dimension
2630
"tmin",
2731
id="tmin",
2832
),
33+
pytest.param(
34+
"area: mean where land time: min",
35+
("lat", "lon"),
36+
"ti",
37+
id="tmin-missing-dim",
38+
),
2939
pytest.param(
3040
"area: mean where land time: sum",
3141
("time", "lat", "lon"),
32-
# cell methods wins out over dimension
3342
"tsum",
3443
id="tsum",
3544
),
45+
pytest.param(
46+
"area: mean where land time: sum",
47+
("lat", "lon"),
48+
"ti",
49+
id="tsum-missing-dim",
50+
),
3651
pytest.param(
3752
"area: mean where land",
3853
("time", "lat", "lon"),
@@ -64,6 +79,19 @@
6479
id="ti",
6580
),
6681
pytest.param(
82+
"area: mean where land time: max",
83+
("time4", "lat", "lon"),
84+
"tmaxavg",
85+
id="tmaxavg",
86+
),
87+
pytest.param(
88+
"area: mean where land time: min",
89+
("time4", "lat", "lon"),
90+
"tminavg",
91+
id="tminavg",
92+
),
93+
pytest.param(
94+
# Nothing in cell methods therefore ti
6795
"area: mean where land",
6896
("time4", "lat", "lon"),
6997
"ti",

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)