-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcardington.py
More file actions
259 lines (246 loc) · 10.9 KB
/
Copy pathcardington.py
File metadata and controls
259 lines (246 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# © Crown copyright, Met Office (2022-2025) and CSET contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Load cardington recipes."""
# long_name='VIS AT 1.5M (incl precip) M'
#'SURFACE SENSIBLE HEAT FLUX W/M2'
#'SURFACE LATENT HEAT FLUX W/M2'
#'DEWPOINT AT 1.5M (K)'
from CSET.recipes import Config, RawRecipe, get_models
def height_token(ht: float) -> str:
"""Convert height to the token used in Cardington variable names."""
# Cardington uses "1p2m" for 1.2 m
if abs(ht - 1.2) < 1e-9:
return "1p2m"
# Integers become "2m", "10m", etc.
if float(ht).is_integer():
return f"{int(ht)}m"
# Fallback for any other non-integer heights (e.g. 0.5 -> "0p5m")
s = str(ht).replace(".", "p")
return f"{s}m"
def load(conf: Config):
"""Yield recipes from the given workflow configuration."""
# Load a list of model detail dictionaries.
models = get_models(conf.asdict())
T30 = "30min"
T05 = "05min"
if conf.CARDINGTON_SINGLE_POINT_TIME_SERIES:
cardington_models = [m for m in models if "Cardington" in m["name"]]
um_model = next(m for m in models if "Cardington" not in m["name"])
fields = [("visibility", "m01s03i281", "visibility_belfort", [2])]
for plot_field, model_field, card_field, heights in fields:
for ht in heights:
card_label_30 = f"Cardington {T30} ({ht} m)"
card_label_05 = f"Cardington {T05} ({ht} m)"
varname = None
if isinstance(card_field, str):
varname = f"{card_field}_{height_token(ht)}"
yield RawRecipe(
recipe="cardington_visibility_single_point_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": varname,
"CARDINGTON_30_LABEL": card_label_30,
"CARDINGTON_05_LABEL": card_label_05,
"UM_VARNAME": model_field,
"HEIGHT": ht,
"PLOTNAME": plot_field,
},
model_ids=[
cardington_models[0]["id"],
cardington_models[1]["id"],
um_model["id"],
],
aggregation=False,
)
field = "surface_temperature"
card_label_30 = f"Cardington {T30}"
card_label_05 = f"Cardington {T05}"
yield RawRecipe(
recipe="cardington_surface_temperature_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": field,
"CARDINGTON_30_LABEL": card_label_30,
"CARDINGTON_05_LABEL": card_label_05,
"UM_VARNAME": "m01s00i024",
"PLOTNAME": field,
},
model_ids=[
cardington_models[0]["id"],
cardington_models[1]["id"],
um_model["id"],
],
aggregation=False,
)
fields = [("surface_air_pressure", "air_pressure")]
for model_field, card_field in fields:
card_label_30 = f"Cardington {T30}"
card_label_05 = f"Cardington {T05}"
yield RawRecipe(
recipe="cardington_surface_pressure_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": card_field,
"CARDINGTON_30_LABEL": card_label_30,
"CARDINGTON_05_LABEL": card_label_05,
"UM_VARNAME": model_field,
"PLOTNAME": card_field,
},
model_ids=[
cardington_models[0]["id"],
cardington_models[1]["id"],
um_model["id"],
],
aggregation=False,
)
fields = [
("wind_speed", "wind_speed_vector", [2, 10, 25, 50]),
("wind_direction", "wind_direction", [2, 10, 25, 50]),
]
for plot_field, card_field, heights in fields:
for ht in heights:
card_label_30 = f"Cardington {T30} ({ht} m)"
card_label_05 = f"Cardington {T05} ({ht} m)"
yield RawRecipe(
recipe="cardington_wind_single_point_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": f"{card_field}_{height_token(ht)}",
"CARDINGTON_30_LABEL": card_label_30,
"CARDINGTON_05_LABEL": card_label_05,
"UM_U_VARNAME": "x_wind",
"UM_V_VARNAME": "y_wind",
"PLOT_VARNAME": plot_field,
"HEIGHT": ht,
},
model_ids=[
cardington_models[0]["id"],
cardington_models[1]["id"],
um_model["id"],
],
aggregation=False,
)
fields = [
(
"air temperature",
"air_temperature",
"air_temperature_rtd",
[1.2, 10, 25, 50],
),
(
"relative humidity",
"relative_humidity",
"relative_humidity",
[1.2, 10, 25, 50],
),
("dew point", "m01s03i250", "dewpoint_temperature", [1.2, 10, 25, 50]),
("wind gust", "m01s03i463", "wind_speed_max", [2, 10, 25, 50]),
("latent heat flux", "m01s03i234", "wq_covariance", [10]),
(
"sensible heat flux",
"m01s03i217",
{
"wt_covariance": "HEIGHT",
"air_temperature_rtd": "HEIGHT_EXCEPT_2M",
"pressure_barometric": "FIXED",
},
[2, 10, 25, 50],
),
]
for plot_field, model_field, card_field, heights in fields:
for ht in heights:
card_label_30 = f"Cardington {T30} ({ht} m)"
card_label_05 = f"Cardington {T05} ({ht} m)"
varname = None
if isinstance(card_field, str):
varname = f"{card_field}_{height_token(ht)}"
if "latent" in plot_field:
yield RawRecipe(
recipe="cardington_latent_heat_single_point_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": varname,
"CARDINGTON_30_LABEL": card_label_30,
"UM_VARNAME": model_field,
"HEIGHT": ht,
"PLOTNAME": plot_field,
},
model_ids=[cardington_models[1]["id"], um_model["id"]],
aggregation=False,
)
elif "sensible" in plot_field and isinstance(card_field, dict):
card_varnames = []
for var, rule in card_field.items():
if rule == "HEIGHT":
card_varnames.append(f"{var}_{height_token(ht)}")
elif rule == "HEIGHT_EXCEPT_2M":
if ht == 2:
card_varnames.append(f"{var}_1p2m")
else:
card_varnames.append(f"{var}_{height_token(ht)}")
elif rule == "FIXED":
card_varnames.append(var)
else:
card_varnames.append(f"{var}_{rule}")
yield RawRecipe(
recipe="cardington_sensible_heat_single_point_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"CARDINGTON_30_LABEL": card_label_30,
"UM_VARNAME": model_field, # m01s03i217 (unchanged)
"HEIGHT": ht,
"PLOTNAME": plot_field,
"WT_VARNAMES": ",".join(card_varnames),
},
model_ids=[cardington_models[1]["id"], um_model["id"]],
aggregation=False,
)
elif "temperature" in model_field or "temperature" in card_field:
yield RawRecipe(
recipe="cardington_temperature_single_point_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": varname,
"CARDINGTON_30_LABEL": card_label_30,
"CARDINGTON_05_LABEL": card_label_05,
"UM_VARNAME": model_field,
"HEIGHT": ht,
"PLOTNAME": plot_field,
},
model_ids=[
cardington_models[0]["id"],
cardington_models[1]["id"],
um_model["id"],
],
aggregation=False,
)
elif "gust" in plot_field:
yield RawRecipe(
recipe="cardington_wind_gust_single_point_time_series.yaml",
variables={
"MODEL_NAME": um_model["name"],
"VARNAME": varname,
"CARDINGTON_30_LABEL": card_label_30,
"CARDINGTON_05_LABEL": card_label_05,
"UM_VARNAME": model_field,
"HEIGHT": ht,
"PLOTNAME": plot_field,
},
model_ids=[
cardington_models[0]["id"],
cardington_models[1]["id"],
um_model["id"],
],
aggregation=False,
)