Skip to content

Commit c8ef7aa

Browse files
committed
small cleanup
1 parent 88651d5 commit c8ef7aa

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

cf_xarray/accessor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,15 @@ def get_associated_variable_names(self, name: Hashable) -> List[Hashable]:
882882
):
883883
anames = attrs_or_encoding["ancillary_variables"].split(" ")
884884
coords.extend(anames)
885+
886+
missing = set(coords) - set(self._maybe_to_dataset().variables)
887+
if missing:
888+
warnings.warn(
889+
f"Variables {missing!r} not found in object but are referred to in the CF attributes.",
890+
UserWarning,
891+
)
892+
for m in missing:
893+
coords.remove(m)
885894
return coords
886895

887896
def __getitem__(self, key: Union[str, List[str]]):
@@ -937,17 +946,8 @@ def __getitem__(self, key: Union[str, List[str]]):
937946
da: DataArray = ds.reset_coords()[allnames[0]] # type: ignore
938947
if allnames[0] in coords:
939948
coords.remove(allnames[0])
940-
failed = []
941949
for k1 in coords:
942-
if k1 not in ds.variables:
943-
failed.append(k1)
944-
else:
945-
da.coords[k1] = ds.variables[k1]
946-
if failed:
947-
warnings.warn(
948-
f"Variables {failed!r} not found in object but are referred to in the CF attributes.",
949-
UserWarning,
950-
)
950+
da.coords[k1] = ds.variables[k1]
951951
return da
952952

953953
ds = ds.reset_coords()[varnames + coords]

0 commit comments

Comments
 (0)