Skip to content

Commit d211c9f

Browse files
committed
Add ancillary variables to getitem
1 parent 50d96d8 commit d211c9f

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

cf_xarray/accessor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,10 @@ def __getitem__(self, key: Union[str, List[str]]):
642642
]
643643
coords.extend(_strip_none_list(measures))
644644

645-
varnames.extend(coords)
645+
if isinstance(self._obj, xr.Dataset) and "ancillary_variables" in attrs:
646+
anames = attrs["ancillary_variables"].split(" ")
647+
coords.extend(anames)
648+
646649
if isinstance(self._obj, xr.DataArray):
647650
ds = self._obj._to_temp_dataset()
648651
else:

cf_xarray/tests/datasets.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,26 @@
4747
np.ones((20, 30)) * 15,
4848
{"coordinates": "TLONG TLAT", "standard_name": "sea_water_potential_temperature"},
4949
)
50+
51+
52+
# This dataset has ancillary variables
53+
54+
anc = xr.Dataset()
55+
anc["q"] = (
56+
("x", "y"),
57+
np.random.randn(10, 20),
58+
dict(
59+
standard_name="specific_humidity",
60+
units="g/g",
61+
ancillary_variables="q_error_limit q_detection_limit",
62+
),
63+
)
64+
anc["q_error_limit"] = (
65+
("x", "y"),
66+
np.random.randn(10, 20),
67+
dict(standard_name="specific_humidity standard_error", units="g/g"),
68+
)
69+
anc["q_detection_limit"] = xr.DataArray(
70+
1e-3, attrs=dict(standard_name="specific_humidity detection_minimum", units="g/g"),
71+
)
72+
anc

cf_xarray/tests/test_accessor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import cf_xarray # noqa
99

1010
from . import raise_if_dask_computes
11-
from .datasets import airds, ds_no_attrs, popds
11+
from .datasets import airds, anc, ds_no_attrs, popds
1212

1313
mpl.use("Agg")
1414

@@ -45,6 +45,12 @@ def test_getitem_standard_name():
4545
ds.air.cf["air_temperature"]
4646

4747

48+
def test_getitem_ancillary_variables():
49+
expected = anc.set_coords(["q_error_limit", "q_detection_limit"])["q"]
50+
assert_identical(anc.cf["q"], expected)
51+
assert_identical(anc.cf["specific_humidity"], expected)
52+
53+
4854
@pytest.mark.parametrize("obj", objects)
4955
@pytest.mark.parametrize(
5056
"attr, xrkwargs, cfkwargs",

0 commit comments

Comments
 (0)