Skip to content

Commit 932d2b7

Browse files
kmuehlbauermaxrjones
authored andcommitted
Fix h5netcdf backend module detection and ros3 tests (pydata#11274)
* use var._root._h5py to get h5py module in h5netcdf backend instead of importing it * fix ros3 tests to use DANDI endpoint and include hdf5 version switch * fix phony_dims for ros3 test * fix import check for ros3 availability * try using property to get around import issue * add whats-new.rst entry
1 parent cd74393 commit 932d2b7

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Bug Fixes
118118
By `Emmanuel Ferdman <https://github.com/emmanuel-ferdman>`_.
119119
- :func:`combine_by_coords` no longer returns an empty dataset when a generator is passed as ``data_objects`` (:issue:`10114`, :pull:`11265`).
120120
By `Amartya Anand <https://github.com/SurfyPenguin>`_.
121+
- Fix h5netcdf backend module detection and ros3 tests (:issue:`11243`, :pull:`11274`).
122+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
121123

122124
Documentation
123125
~~~~~~~~~~~~~

xarray/backends/h5netcdf_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ def ds(self):
277277

278278
def open_store_variable(self, name, var):
279279
import h5netcdf.core
280-
import h5py
281280

282281
dimensions = var.dimensions
283282
data = indexing.LazilyIndexedArray(H5NetCDFArrayWrapper(name, self))
@@ -306,6 +305,7 @@ def open_store_variable(self, name, var):
306305
encoding["source"] = self._filename
307306
encoding["original_shape"] = data.shape
308307

308+
h5py = var._root._h5py
309309
vlen_dtype = h5py.check_dtype(vlen=var.dtype)
310310
if vlen_dtype is str:
311311
encoding["dtype"] = str

xarray/tests/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,13 @@ def _importorskip_h5netcdf_ros3(has_h5netcdf: bool):
215215
not has_h5netcdf, reason="requires h5netcdf"
216216
)
217217

218-
import h5py
218+
has_h5py, _ = _importorskip("h5py")
219+
if has_h5py:
220+
import h5py
219221

220-
h5py_with_ros3 = h5py.get_config().ros3
222+
h5py_with_ros3 = h5py.get_config().ros3
223+
else:
224+
h5py_with_ros3 = has_h5py
221225

222226
return h5py_with_ros3, pytest.mark.skipif(
223227
not h5py_with_ros3,

xarray/tests/test_backends.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5422,29 +5422,46 @@ def test_memoryview_write_netcdf4_read_h5netcdf() -> None:
54225422
@requires_h5netcdf_ros3
54235423
class TestH5NetCDFDataRos3Driver(TestCommon):
54245424
engine: T_NetcdfEngine = "h5netcdf"
5425-
test_remote_dataset: str = "https://archive.unidata.ucar.edu/software/netcdf/examples/OMI-Aura_L2-example.nc"
5425+
test_remote_dataset: str = "https://dandiarchive.s3.amazonaws.com/ros3test.hdf5"
5426+
5427+
@property
5428+
def ros3_kwargs(self) -> dict:
5429+
from h5py import version as h5ver
5430+
5431+
return (
5432+
{} if h5ver.hdf5_version_tuple < (2, 0, 0) else {"aws_region": b"us-east-2"}
5433+
)
54265434

54275435
@pytest.mark.filterwarnings("ignore:Duplicate dimension names")
54285436
def test_get_variable_list(self) -> None:
54295437
with open_dataset(
54305438
self.test_remote_dataset,
54315439
engine="h5netcdf",
5432-
backend_kwargs={"driver": "ros3"},
5440+
backend_kwargs={
5441+
"driver": "ros3",
5442+
"driver_kwds": self.ros3_kwargs,
5443+
"phony_dims": "access",
5444+
},
54335445
) as actual:
5434-
assert "Temperature" in list(actual)
5446+
assert "mydataset" in list(actual)
54355447

54365448
@pytest.mark.filterwarnings("ignore:Duplicate dimension names")
54375449
def test_get_variable_list_empty_driver_kwds(self) -> None:
54385450
driver_kwds = {
54395451
"secret_id": b"",
54405452
"secret_key": b"",
54415453
}
5442-
backend_kwargs = {"driver": "ros3", "driver_kwds": driver_kwds}
5454+
driver_kwds.update(self.ros3_kwargs)
5455+
backend_kwargs = {
5456+
"driver": "ros3",
5457+
"driver_kwds": driver_kwds,
5458+
"phony_dims": "access",
5459+
}
54435460

54445461
with open_dataset(
54455462
self.test_remote_dataset, engine="h5netcdf", backend_kwargs=backend_kwargs
54465463
) as actual:
5447-
assert "Temperature" in list(actual)
5464+
assert "mydataset" in list(actual)
54485465

54495466

54505467
@pytest.fixture(params=["scipy", "netcdf4", "h5netcdf", "zarr"])

0 commit comments

Comments
 (0)