Skip to content

Commit

Permalink
Include _FillValue encoding intergration tests for HDFVirtualBackend.
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkinsspatial committed Feb 27, 2025
1 parent 592586a commit f9eb58b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
8 changes: 2 additions & 6 deletions virtualizarr/readers/hdf/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,7 @@ def _extract_cf_fill_value(
fillvalue = v.item()
else:
fillvalue = v
if (
fillvalue is not None
and h5obj.dtype.kind not in "S"
and h5obj.dtype.fields is None
):
fillvalue = FillValueCoder.encode(fillvalue, h5obj.dtype)
fillvalue = FillValueCoder.encode(fillvalue, h5obj.dtype) # type: ignore[arg-type]
return fillvalue

@staticmethod
Expand Down Expand Up @@ -334,6 +329,7 @@ def _dataset_to_variable(
cfcodec = cfcodec_from_dataset(dataset)
attrs = HDFVirtualBackend._extract_attrs(dataset)
cf_fill_value = HDFVirtualBackend._extract_cf_fill_value(dataset)
attrs.pop("_FillValue", None)

if cfcodec:
codecs.insert(0, cfcodec["codec"])
Expand Down
14 changes: 8 additions & 6 deletions virtualizarr/tests/test_readers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ def scalar_fill_value_hdf5_file(tmpdir):
{"fill_value": -9999, "data": np.random.randint(0, 10, size=(5))},
{"fill_value": -9999.0, "data": np.random.random(5)},
{"fill_value": np.nan, "data": np.random.random(5)},
{"fill_value": True, "data": np.random.choice([True, False], size=(5))},
{"fill_value": "NA".encode("ascii"), "data": np.array(["one"], dtype="S")},
# {"fill_value": compound_fill, "data": compound_data},
{"fill_value": False, "data": np.array([True, False, False, True, True])},
{"fill_value": "NaN", "data": np.array(["three"], dtype="S10")},
{"fill_value": compound_fill, "data": compound_data},
]


Expand All @@ -386,9 +386,11 @@ def cf_fill_value_hdf5_file(tmpdir, request):
filepath = f"{tmpdir}/cf_fill_value.nc"
f = h5py.File(filepath, "w")
dset = f.create_dataset(name="data", data=request.param["data"], chunks=True)
wat = f.create_dataset(name="wat", data=request.param["data"], chunks=True)
wat.make_scale()
dset.dims[0].attach_scale(wat)
dim_scale = f.create_dataset(
name="dim_scale", data=request.param["data"], chunks=True
)
dim_scale.make_scale()
dset.dims[0].attach_scale(dim_scale)
dset.attrs["_FillValue"] = request.param["fill_value"]
return filepath

Expand Down
6 changes: 6 additions & 0 deletions virtualizarr/tests/test_readers/test_hdf/test_hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def test_scalar_fill_value(self, scalar_fill_value_hdf5_file):
def test_cf_fill_value(self, cf_fill_value_hdf5_file):
f = h5py.File(cf_fill_value_hdf5_file)
ds = f["data"]
if ds.dtype.kind in "S":
pytest.xfail("Investigate fixed-length binary encoding in Zarr v3")
if ds.dtype.names:
pytest.xfail(
"To fix, structured dtype fill value encoding for Zarr backend"
)
var = HDFVirtualBackend._dataset_to_variable(
cf_fill_value_hdf5_file, ds, group=""
)
Expand Down
12 changes: 9 additions & 3 deletions virtualizarr/tests/test_readers/test_hdf/test_hdf_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ def test_non_coord_dim_roundtrip(self, tmpdir, non_coord_dim):

@requires_icechunk
def test_cf_fill_value_roundtrip(self, tmpdir, cf_fill_value_hdf5_file):
# ds = xr.open_dataset(cf_fill_value_hdf5_file)
ds = xr.open_dataset(cf_fill_value_hdf5_file)
if ds["data"].dtype.names:
pytest.xfail(
"To fix, structured dtype fill value encoding for Zarr backend"
)
vds = virtualizarr.open_virtual_dataset(
cf_fill_value_hdf5_file, backend=HDFVirtualBackend
cf_fill_value_hdf5_file,
backend=HDFVirtualBackend,
)
roundtrip_as_in_memory_icechunk(vds, tmpdir, decode_times=False)
roundtrip = roundtrip_as_in_memory_icechunk(vds, tmpdir, decode_times=False)
xrt.assert_equal(ds, roundtrip)

0 comments on commit f9eb58b

Please sign in to comment.