Skip to content

Commit 463bc28

Browse files
authored
fix nczarr when libnetcdf>4.8.1 (#7575)
1 parent 6531b57 commit 463bc28

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

xarray/backends/zarr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def _get_zarr_dims_and_attrs(zarr_obj, dimension_key, try_nczarr):
207207
"which are required for xarray to determine variable dimensions."
208208
) from e
209209

210-
nc_attrs = [attr for attr in zarr_obj.attrs if attr.startswith("_NC")]
210+
nc_attrs = [attr for attr in zarr_obj.attrs if attr.lower().startswith("_nc")]
211211
attributes = HiddenKeyDict(zarr_obj.attrs, [dimension_key] + nc_attrs)
212212
return dimensions, attributes
213213

@@ -495,7 +495,7 @@ def get_attrs(self):
495495
return {
496496
k: v
497497
for k, v in self.zarr_group.attrs.asdict().items()
498-
if not k.startswith("_NC")
498+
if not k.lower().startswith("_nc")
499499
}
500500

501501
def get_dimensions(self):

xarray/tests/test_backends.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5663,12 +5663,14 @@ def test_write_file_from_np_str(str_type, tmpdir) -> None:
56635663
@requires_zarr
56645664
@requires_netCDF4
56655665
class TestNCZarr:
5666-
@staticmethod
5667-
def _create_nczarr(filename):
5668-
netcdfc_version = Version(nc4.getlibversion().split()[0])
5669-
if netcdfc_version < Version("4.8.1"):
5666+
@property
5667+
def netcdfc_version(self):
5668+
return Version(nc4.getlibversion().split()[0])
5669+
5670+
def _create_nczarr(self, filename):
5671+
if self.netcdfc_version < Version("4.8.1"):
56705672
pytest.skip("requires netcdf-c>=4.8.1")
5671-
if (platform.system() == "Windows") and (netcdfc_version == Version("4.8.1")):
5673+
if platform.system() == "Windows" and self.netcdfc_version == Version("4.8.1"):
56725674
# Bug in netcdf-c==4.8.1 (typo: Nan instead of NaN)
56735675
# https://github.com/Unidata/netcdf-c/issues/2265
56745676
pytest.skip("netcdf-c==4.8.1 has issues on Windows")
@@ -5678,9 +5680,7 @@ def _create_nczarr(filename):
56785680
# https://github.com/Unidata/netcdf-c/issues/2259
56795681
ds = ds.drop_vars("dim3")
56805682

5681-
# netcdf-c>4.8.1 will add _ARRAY_DIMENSIONS by default
5682-
mode = "nczarr" if netcdfc_version == Version("4.8.1") else "nczarr,noxarray"
5683-
ds.to_netcdf(f"file://{filename}#mode={mode}")
5683+
ds.to_netcdf(f"file://{filename}#mode=nczarr")
56845684
return ds
56855685

56865686
def test_open_nczarr(self) -> None:
@@ -5700,6 +5700,9 @@ def test_overwriting_nczarr(self) -> None:
57005700
@pytest.mark.parametrize("mode", ["a", "r+"])
57015701
@pytest.mark.filterwarnings("ignore:.*non-consolidated metadata.*")
57025702
def test_raise_writing_to_nczarr(self, mode) -> None:
5703+
if self.netcdfc_version > Version("4.8.1"):
5704+
pytest.skip("netcdf-c>4.8.1 adds the _ARRAY_DIMENSIONS attribute")
5705+
57035706
with create_tmp_file(suffix=".zarr") as tmp:
57045707
ds = self._create_nczarr(tmp)
57055708
with pytest.raises(

0 commit comments

Comments
 (0)