Skip to content

Commit fa8e655

Browse files
authored
More flexible file loading in monitor/multi_datasets.py (#3728)
1 parent 351c4d3 commit fa8e655

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

esmvaltool/diag_scripts/monitor/multi_datasets.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@
612612
from iris.analysis.cartography import area_weights
613613
from iris.coord_categorisation import add_year
614614
from iris.coords import AuxCoord
615+
from iris.exceptions import ConstraintMismatchError
615616
from matplotlib.colors import CenteredNorm
616617
from matplotlib.gridspec import GridSpec
617618
from matplotlib.ticker import (
@@ -1107,7 +1108,22 @@ def _load_and_preprocess_data(self):
11071108
for dataset in input_data:
11081109
filename = dataset['filename']
11091110
logger.info("Loading %s", filename)
1110-
cube = iris.load_cube(filename)
1111+
cubes = iris.load(filename)
1112+
if len(cubes) == 1:
1113+
cube = cubes[0]
1114+
else:
1115+
var_name = dataset['short_name']
1116+
try:
1117+
cube = cubes.extract_cube(iris.NameConstraint(
1118+
var_name=var_name
1119+
))
1120+
except ConstraintMismatchError as exc:
1121+
var_names = [c.var_name for c in cubes]
1122+
raise ValueError(
1123+
f"Cannot load data: multiple variables ({var_names}) "
1124+
f"are available in file {filename}, but not the "
1125+
f"requested '{var_name}'"
1126+
) from exc
11111127

11121128
# Fix time coordinate if present
11131129
if cube.coords('time', dim_coords=True):

0 commit comments

Comments
 (0)