Skip to content

Commit 533ed5c

Browse files
authored
Add option to disable warnings about missing variables (#304)
1 parent 5dd0772 commit 533ed5c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

cf_xarray/accessor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,10 +1526,11 @@ def get_associated_variable_names(
15261526
allvars = itertools.chain(*coords.values())
15271527
missing = set(allvars) - set(self._maybe_to_dataset().variables)
15281528
if missing:
1529-
warnings.warn(
1530-
f"Variables {missing!r} not found in object but are referred to in the CF attributes.",
1531-
UserWarning,
1532-
)
1529+
if OPTIONS["warn_on_missing_variables"]:
1530+
warnings.warn(
1531+
f"Variables {missing!r} not found in object but are referred to in the CF attributes.",
1532+
UserWarning,
1533+
)
15331534
for k, v in coords.items():
15341535
for m in missing:
15351536
if m in v:

cf_xarray/options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
OPTIONS: MutableMapping[str, Any] = {
1111
"custom_criteria": [],
12+
"warn_on_missing_variables": True,
1213
}
1314

1415

@@ -20,6 +21,9 @@ class set_options:
2021
custom_criteria : dict
2122
Translate from axis, coord, or custom name to
2223
variable name optionally using ``custom_criteria``. Default: [].
24+
warn_on_missing_variables : bool
25+
Whether to raise a warning when variables referred to in attributes
26+
are not present in the object.
2327
2428
Examples
2529
--------

cf_xarray/tests/test_accessor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ def test_getitem_ancillary_variables():
299299
with pytest.warns(UserWarning):
300300
anc[["q"]].cf["q"]
301301

302+
with pytest.warns(None) as record:
303+
with cf_xarray.set_options(warn_on_missing_variables=False):
304+
anc[["q"]].cf["q"]
305+
assert len(record) == 0
306+
302307
for k in ["ULONG", "ULAT"]:
303308
assert k not in popds.cf["TEMP"].coords
304309

0 commit comments

Comments
 (0)