Skip to content

Commit 582d3d2

Browse files
langmoreWeatherbench2 authors
authored and
Weatherbench2 authors
committed
Give better error message in weatherbench/metrics when climatology doesn't contain forecast keys. Previously, the KeyError raised by xr would indicate that "some_var_mean" was not found, but that "_mean" variable was likely just a "let's do a final attempt" at finding variables, and this confuses the user.
PiperOrigin-RevId: 590711498
1 parent acb211f commit 582d3d2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

weatherbench2/metrics.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,17 @@ def compute_chunk(
335335
time_dim = "time"
336336
try:
337337
climatology_chunk = self.climatology[list(forecast.keys())]
338-
except KeyError:
338+
except KeyError as e:
339+
not_found = set(forecast.keys()).difference(self.climatology.data_vars)
339340
clim_var_dict = {key + "_mean": key for key in forecast.keys()} # pytype: disable=unsupported-operands
341+
not_found_means = set(clim_var_dict).difference(
342+
self.climatology.data_vars
343+
)
344+
if not_found and not_found_means:
345+
raise KeyError(
346+
f"Did not find {not_found} forecast keys in climatology. Appending "
347+
"'mean' did not help"
348+
) from e
340349
climatology_chunk = self.climatology[list(clim_var_dict.keys())].rename(
341350
clim_var_dict
342351
)

0 commit comments

Comments
 (0)