diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 6686d67ed4d..3b0335aa5a6 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -207,7 +207,7 @@ def _get_zarr_dims_and_attrs(zarr_obj, dimension_key, try_nczarr): "which are required for xarray to determine variable dimensions." ) from e - nc_attrs = [attr for attr in zarr_obj.attrs if attr.startswith("_NC")] + nc_attrs = [attr for attr in zarr_obj.attrs if attr.lower().startswith("_nc")] attributes = HiddenKeyDict(zarr_obj.attrs, [dimension_key] + nc_attrs) return dimensions, attributes @@ -495,7 +495,7 @@ def get_attrs(self): return { k: v for k, v in self.zarr_group.attrs.asdict().items() - if not k.startswith("_NC") + if not k.lower().startswith("_nc") } def get_dimensions(self): diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index a156b864315..91daabd12d5 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5663,12 +5663,14 @@ def test_write_file_from_np_str(str_type, tmpdir) -> None: @requires_zarr @requires_netCDF4 class TestNCZarr: - @staticmethod - def _create_nczarr(filename): - netcdfc_version = Version(nc4.getlibversion().split()[0]) - if netcdfc_version < Version("4.8.1"): + @property + def netcdfc_version(self): + return Version(nc4.getlibversion().split()[0]) + + def _create_nczarr(self, filename): + if self.netcdfc_version < Version("4.8.1"): pytest.skip("requires netcdf-c>=4.8.1") - if (platform.system() == "Windows") and (netcdfc_version == Version("4.8.1")): + if platform.system() == "Windows" and self.netcdfc_version == Version("4.8.1"): # Bug in netcdf-c==4.8.1 (typo: Nan instead of NaN) # https://github.com/Unidata/netcdf-c/issues/2265 pytest.skip("netcdf-c==4.8.1 has issues on Windows") @@ -5678,9 +5680,7 @@ def _create_nczarr(filename): # https://github.com/Unidata/netcdf-c/issues/2259 ds = ds.drop_vars("dim3") - # netcdf-c>4.8.1 will add _ARRAY_DIMENSIONS by default - mode = "nczarr" if netcdfc_version == Version("4.8.1") else "nczarr,noxarray" - ds.to_netcdf(f"file://{filename}#mode={mode}") + ds.to_netcdf(f"file://{filename}#mode=nczarr") return ds def test_open_nczarr(self) -> None: @@ -5700,6 +5700,9 @@ def test_overwriting_nczarr(self) -> None: @pytest.mark.parametrize("mode", ["a", "r+"]) @pytest.mark.filterwarnings("ignore:.*non-consolidated metadata.*") def test_raise_writing_to_nczarr(self, mode) -> None: + if self.netcdfc_version > Version("4.8.1"): + pytest.skip("netcdf-c>4.8.1 adds the _ARRAY_DIMENSIONS attribute") + with create_tmp_file(suffix=".zarr") as tmp: ds = self._create_nczarr(tmp) with pytest.raises(