Description
What happened?
Attempting to decode a masked int32 encoded array with CF-Conventions applied, specifically in the CFMaskCoder.decode function.
What did you expect to happen?
Expected behaviour is that masked int32 variables are decoded correctly, given the correct data type and fill value to replace with masks.
Issue is specific to CFMaskCoder.decode line 370, where for a masked array of int32 values, the following check gives an incorrect outcome:
data.dtype.kind in "iu"
should return False for signed integers, but instead returns True since "i" in "iu"
also returns True. The fix should then be to replace this check with data.dtype.kind in ["iu"]
to apply the dtype int64 to unsigned integer values, not signed 32bit integers.
Minimal Complete Verifiable Example
#Example dataset can be downloaded from https://data.ceda.ac.uk//neodc/esacci/sst/data/CDR_v2/ATSR/L3U/v2.1/AATSR/2002/07/24/20020724123321-ESACCI-L3U_GHRSST-SSTskin-AATSR-CDR2.1-v02.0-fv01.0.nc
import xarray as xr
ds = xr.open_dataset('20020724123321-ESACCI-L3U_GHRSST-SSTskin-AATSR-CDR2.1-v02.0-fv01.0.nc')
ds.sst_dtime.isel(time=0, lat=slice(0,5), lon=slice(0,5)).compute()
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Anything else we need to know?
This is the expected output, provided the CF-Conventions extend to int32 masked arrays. If this is not the case, the data can be decoded with decode_cf=False
correctly, however this should not be needed if the CF conventions should apply.
Environment
xarray: 2024.6.0
pandas: 2.2.3
numpy: 1.26.4
scipy: 1.15.2
netCDF4: 1.6.5
pydap: None
h5netcdf: None
h5py: 3.13.0
zarr: 2.18.7
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: None
dask: 2024.7.0
distributed: 2024.7.0
matplotlib: 3.9.2
cartopy: None
seaborn: None
numbagg: None
fsspec: 2024.9.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.9.1
pip: 24.0
conda: None
pytest: 8.3.5
mypy: None
IPython: 9.1.0
sphinx: 7.4.7