Skip to content

Handle matching coordinates and dimensions with differing shapes #369

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

Merged
merged 5 commits into from
Oct 4, 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
6 changes: 3 additions & 3 deletions kerchunk/netCDF3.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ def translate(self):
out = self.out
z = zarr.open(out, mode="w")
for dim, var in self.variables.items():
if dim in self.dimensions:
shape = self.dimensions[dim]
elif dim in self.chunks:
if dim in self.chunks:
shape = self.chunks[dim][-1]
elif dim in self.dimensions:
shape = self.dimensions[dim]
else:
# defer record array
continue
Expand Down
49 changes: 49 additions & 0 deletions kerchunk/tests/test_netcdf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import os


import fsspec
import numpy as np
from packaging.version import Version
import pytest
from kerchunk import netCDF3

xr = pytest.importorskip("xarray")


has_xarray_2023_8_0 = Version(xr.__version__) >= Version("2023.8.0")


arr = np.random.rand(1, 10, 10)
static = xr.DataArray(data=np.random.rand(10, 10), dims=["x", "y"], name="static")
data = xr.DataArray(
Expand Down Expand Up @@ -89,3 +95,46 @@ def test_unlimited(unlimited_dataset):
assert (ds.lat.values == expected.lat.values).all()
assert (ds.lon.values == expected.lon.values).all()
assert (ds.temp.values == expected.temp.values).all()


@pytest.fixture()
def matching_coordinate_dimension_dataset(tmpdir):
"""Create a dataset with a coordinate dimension that matches the name of a
variable dimension."""
# https://unidata.github.io/netcdf4-python/#creatingopeningclosing-a-netcdf-file
from netCDF4 import Dataset

fn = os.path.join(tmpdir, "test.nc")
rootgrp = Dataset(fn, "w", format="NETCDF3_64BIT")
rootgrp.createDimension("node", 2)
rootgrp.createDimension("sigma", 2)

node = rootgrp.createVariable("node", "i4", ("node",))
sigma = rootgrp.createVariable("sigma", "f8", ("sigma", "node"))

node[:] = [0, 1]
for i in range(2):
sigma[i] = np.random.uniform(size=(2,))

rootgrp.close()
return fn


@pytest.mark.skipif(
not has_xarray_2023_8_0, reason="XArray 2023.08.0 is required for this behavior."
)
def test_matching_coordinate_dimension(matching_coordinate_dimension_dataset):
fn = matching_coordinate_dimension_dataset
expected = xr.open_dataset(fn, engine="scipy")
h = netCDF3.NetCDF3ToZarr(fn)
out = h.translate()
ds = xr.open_dataset(
"reference://",
engine="zarr",
backend_kwargs={
"consolidated": False,
"storage_options": {"fo": out},
},
)
assert (ds.node.values == expected.node.values).all()
assert (ds.sigma.values == expected.sigma.values).all()