Flag Variables#
cf_xarray
has some support for flag variables.
import cf_xarray
import xarray as xr
da = xr.DataArray(
[1, 2, 1, 1, 2, 2, 3, 3, 3, 3],
dims=("time",),
attrs={
"flag_values": [1, 2, 3],
"flag_meanings": "atlantic_ocean pacific_ocean indian_ocean",
"standard_name": "region",
},
name="region",
)
da.cf
Flag Variable:
Flag Meanings: 1: atlantic_ocean
2: pacific_ocean
3: indian_ocean
Coordinates:
CF Axes: X, Y, Z, T: n/a
CF Coordinates: longitude, latitude, vertical, time: n/a
Cell Measures: area, volume: n/a
Standard Names: n/a
Bounds: n/a
Grid Mappings: n/a
Now you can perform meaningful boolean comparisons that take advantage of the flag_meanings
attribute:
# compare to da == 1
da.cf == "atlantic_ocean"
<xarray.DataArray 'region' (time: 10)> array([ True, False, True, True, False, False, False, False, False, False]) Dimensions without coordinates: time
Similarly with membership tests using DataArray.cf.isin()
# compare to da.isin([2, 3])
da.cf.isin(["indian_ocean", "pacific_ocean"])
<xarray.DataArray 'region' (time: 10)> array([False, True, False, False, True, True, True, True, True, True]) Dimensions without coordinates: time
You can also check whether a DataArray has the appropriate attributes to be recognized as a flag variable using DataArray.cf.is_flag_variable()
da.cf.is_flag_variable
True
Tip
cf_xarray
does not support flag masks yet but a Pull Request to add this functionality is very welcome!