diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2b1e1c354eb..15cfb8e5d81 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -118,6 +118,8 @@ Bug Fixes By `Emmanuel Ferdman `_. - :func:`combine_by_coords` no longer returns an empty dataset when a generator is passed as ``data_objects`` (:issue:`10114`, :pull:`11265`). By `Amartya Anand `_. +- Fix h5netcdf backend module detection and ros3 tests (:issue:`11243`, :pull:`11274`). + By `Kai Mühlbauer `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/backends/h5netcdf_.py b/xarray/backends/h5netcdf_.py index 041aa49cf76..9b828c8e236 100644 --- a/xarray/backends/h5netcdf_.py +++ b/xarray/backends/h5netcdf_.py @@ -277,7 +277,6 @@ def ds(self): def open_store_variable(self, name, var): import h5netcdf.core - import h5py dimensions = var.dimensions data = indexing.LazilyIndexedArray(H5NetCDFArrayWrapper(name, self)) @@ -306,6 +305,7 @@ def open_store_variable(self, name, var): encoding["source"] = self._filename encoding["original_shape"] = data.shape + h5py = var._root._h5py vlen_dtype = h5py.check_dtype(vlen=var.dtype) if vlen_dtype is str: encoding["dtype"] = str diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 3abb29f74bf..70697cf68ce 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -215,9 +215,13 @@ def _importorskip_h5netcdf_ros3(has_h5netcdf: bool): not has_h5netcdf, reason="requires h5netcdf" ) - import h5py + has_h5py, _ = _importorskip("h5py") + if has_h5py: + import h5py - h5py_with_ros3 = h5py.get_config().ros3 + h5py_with_ros3 = h5py.get_config().ros3 + else: + h5py_with_ros3 = has_h5py return h5py_with_ros3, pytest.mark.skipif( not h5py_with_ros3, diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index f32d52ea3db..e5b4acc894c 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5422,16 +5422,28 @@ def test_memoryview_write_netcdf4_read_h5netcdf() -> None: @requires_h5netcdf_ros3 class TestH5NetCDFDataRos3Driver(TestCommon): engine: T_NetcdfEngine = "h5netcdf" - test_remote_dataset: str = "https://archive.unidata.ucar.edu/software/netcdf/examples/OMI-Aura_L2-example.nc" + test_remote_dataset: str = "https://dandiarchive.s3.amazonaws.com/ros3test.hdf5" + + @property + def ros3_kwargs(self) -> dict: + from h5py import version as h5ver + + return ( + {} if h5ver.hdf5_version_tuple < (2, 0, 0) else {"aws_region": b"us-east-2"} + ) @pytest.mark.filterwarnings("ignore:Duplicate dimension names") def test_get_variable_list(self) -> None: with open_dataset( self.test_remote_dataset, engine="h5netcdf", - backend_kwargs={"driver": "ros3"}, + backend_kwargs={ + "driver": "ros3", + "driver_kwds": self.ros3_kwargs, + "phony_dims": "access", + }, ) as actual: - assert "Temperature" in list(actual) + assert "mydataset" in list(actual) @pytest.mark.filterwarnings("ignore:Duplicate dimension names") def test_get_variable_list_empty_driver_kwds(self) -> None: @@ -5439,12 +5451,17 @@ def test_get_variable_list_empty_driver_kwds(self) -> None: "secret_id": b"", "secret_key": b"", } - backend_kwargs = {"driver": "ros3", "driver_kwds": driver_kwds} + driver_kwds.update(self.ros3_kwargs) + backend_kwargs = { + "driver": "ros3", + "driver_kwds": driver_kwds, + "phony_dims": "access", + } with open_dataset( self.test_remote_dataset, engine="h5netcdf", backend_kwargs=backend_kwargs ) as actual: - assert "Temperature" in list(actual) + assert "mydataset" in list(actual) @pytest.fixture(params=["scipy", "netcdf4", "h5netcdf", "zarr"])