For our project we have ~20 independent flag_masks that can simultaneously be triggered (QA/QC flags). The current implementation of "flag_masks" works well with operations such as "==" and "isin" for integers larger than 8-bit:
import xarray as xr
import numpy as np
import cf_xarray
# Create example 32-bit flag_masks
flag_indep_uint32 = xr.DataArray(
2**np.arange(32, dtype=np.uint32),
dims=("time",),
attrs={
"flag_masks": [2**i for i in range(32)],
"flag_meanings": " ".join([f"flag_{i}" for i in range(32)]),
"standard_name": "flag_independent",
},
name="flag_var",
)
The one part that doesn't seem supported is just using the ".cf" to print the flags/bits:
flag_indep_uint32.cf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3.10/site-packages/cf_xarray/accessor.py", line 1543, in __repr__
return ("".join(self._generate_repr(rich=False))).rstrip()
File ".../lib/python3.10/site-packages/cf_xarray/accessor.py", line 1561, in _generate_repr
_format_flags(self, rich), title="Flag Variable", rich=rich
File ".../lib/python3.10/site-packages/cf_xarray/formatting.py", line 200, in _format_flags
bitstring[abs(b)] = _format_cf_name("1" if b >= 0 else "0", rich)
IndexError: list assignment index out of range
The _format_flags and find_set_bits have some uint8-dependent code. Internally, we made some small changes here:
This hasn't been thoroughly tested and there is probably a much more concise way of adding this support compared to what I have. For the same uint32 example earlier, the updated code displays as
For our project we have ~20 independent flag_masks that can simultaneously be triggered (QA/QC flags). The current implementation of "flag_masks" works well with operations such as "==" and "isin" for integers larger than 8-bit:
Find where flag_0 is True
Find where flag_0 or flag_1 or flag_31 are True
The one part that doesn't seem supported is just using the ".cf" to print the flags/bits:
The _format_flags and find_set_bits have some uint8-dependent code. Internally, we made some small changes here:
https://github.com/mps01060/cf-xarray/tree/morebits
This hasn't been thoroughly tested and there is probably a much more concise way of adding this support compared to what I have. For the same uint32 example earlier, the updated code displays as
Thank you for any help / suggestions!