Skip to content
30 changes: 29 additions & 1 deletion lib/iris/fileformats/netcdf/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def _translate_constraints_to_var_callback(constraints):

Notes
-----
For now, ONLY handles a single NameConstraint with no 'STASH' component.
For now, ONLY handles NameConstraints with no 'STASH' component.

"""
import iris._constraints
Expand Down Expand Up @@ -624,6 +624,34 @@ def inner(cf_datavar):
break
return match

result = inner
elif len(constraints) > 1:
if all(
isinstance(constraint, iris._constraints.NameConstraint)
and constraint.STASH == "none"
for constraint in constraints
):

def inner(cf_datavar):
match = False
for constraint in constraints:
match = True
for name in constraint._names:
expected = getattr(constraint, name)
if name != "STASH" and expected != "none":
attr_name = "cf_name" if name == "var_name" else name
# Fetch property : N.B. CFVariable caches the property values
# The use of a default here is the only difference from the code in NameConstraint.
if not hasattr(cf_datavar, attr_name):
continue
actual = getattr(cf_datavar, attr_name, "")
if actual != expected:
match = False
break
if match:
break
return match

result = inner
return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ class Test(tests.IrisTest):
def test_multiple_constraints(self):
constrs = [
iris.NameConstraint(standard_name="x_wind"),
iris.NameConstraint(var_name="var1"),
iris.NameConstraint(var_name="var2"),
]
callback = _translate_constraints_to_var_callback(constrs)
result = [callback(var) for var in self.data_variables]
self.assertArrayEqual(result, [True, True, False, True, False])

def test_multiple_constraints_invalid(self):
constrs = [
iris.NameConstraint(standard_name="x_wind"),
iris.NameConstraint(var_name="var1", STASH="m01s00i024"),
]
result = _translate_constraints_to_var_callback(constrs)
self.assertIsNone(result)
Expand Down
Loading