Skip to content

Commit 6b63246

Browse files
committed
Use DataArray.cf.is_flag_variable
1 parent 98d1ee5 commit 6b63246

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

cf_xarray/accessor.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -886,19 +886,8 @@ def __getattr__(self, attr):
886886
)
887887

888888

889-
def _is_cf_flag_variable(da):
890-
if (
891-
isinstance(da, xr.DataArray)
892-
and "flag_meanings" in da.attrs
893-
and "flag_values" in da.attrs
894-
):
895-
return True
896-
else:
897-
return False
898-
899-
900889
def create_flag_dict(da):
901-
if not _is_cf_flag_variable(da):
890+
if not da.cf.is_flag_variable:
902891
raise ValueError(
903892
"Comparisons are only supported for DataArrays that represent CF flag variables."
904893
".attrs must contain 'flag_values' and 'flag_meanings'"
@@ -1256,7 +1245,7 @@ def make_text_section(subtitle, attr, valid_values, default_keys=None):
12561245

12571246
return "\n".join(rows) + "\n"
12581247

1259-
if _is_cf_flag_variable(self._obj):
1248+
if self._obj.is_flag_variable(self._obj):
12601249
flag_dict = create_flag_dict(self._obj)
12611250
text = f"CF Flag variable with mapping:\n\t{flag_dict!r}\n\n"
12621251
else:
@@ -2188,4 +2177,18 @@ def __getitem__(self, key: Union[str, List[str]]) -> DataArray:
21882177

21892178
return _getitem(self, key)
21902179

2191-
pass
2180+
@property
2181+
def is_flag_variable(self):
2182+
"""
2183+
Returns True if the DataArray satisfies CF conventions for flag variables.
2184+
2185+
Flag masks are not supported yet.
2186+
"""
2187+
if (
2188+
isinstance(self._obj, xr.DataArray)
2189+
and "flag_meanings" in self._obj.attrs
2190+
and "flag_values" in self._obj.attrs
2191+
):
2192+
return True
2193+
else:
2194+
return False

doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Attributes
3131
DataArray.cf.cell_measures
3232
DataArray.cf.coordinates
3333
DataArray.cf.formula_terms
34+
DataArray.cf.is_flag_variable
3435
DataArray.cf.standard_names
3536
DataArray.cf.plot
3637

0 commit comments

Comments
 (0)