Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nczarr when libnetcdf>4.8.1 #7575

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
19 changes: 11 additions & 8 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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(
Expand Down