Skip to content

Commit

Permalink
Added driver parameter for h5netcdf (#8360)
Browse files Browse the repository at this point in the history
* added driver for h5netcdf

* Update xarray/backends/h5netcdf_.py

Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>

* Update xarray/backends/h5netcdf_.py

Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>

* Update xarray/backends/h5netcdf_.py

Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>

* Update xarray/backends/h5netcdf_.py

Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>

* added test for h5netcdf ros3

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* check h5netcdf 1.3.0 available for test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updated doc/whats-new.rst

---------

Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 15, 2023
1 parent 59378cc commit 1411474
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ New Features
region to write in the original store. Also implement automatic transpose when dimension
order does not match the original store. (:issue:`7702`, :issue:`8421`, :pull:`8434`).
By `Sam Levang <https://github.com/slevang>`_.
- Allow the usage of h5py drivers (eg: ros3) via h5netcdf (:pull:`8360`).
By `Ezequiel Cimadevilla <https://github.com/zequihg50>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
9 changes: 9 additions & 0 deletions xarray/backends/h5netcdf_.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def open(
invalid_netcdf=None,
phony_dims=None,
decode_vlen_strings=True,
driver=None,
driver_kwds=None,
):
import h5netcdf

Expand All @@ -161,7 +163,10 @@ def open(
kwargs = {
"invalid_netcdf": invalid_netcdf,
"decode_vlen_strings": decode_vlen_strings,
"driver": driver,
}
if driver_kwds is not None:
kwargs.update(driver_kwds)
if phony_dims is not None:
kwargs["phony_dims"] = phony_dims

Expand Down Expand Up @@ -397,6 +402,8 @@ def open_dataset( # type: ignore[override] # allow LSP violation, not supporti
invalid_netcdf=None,
phony_dims=None,
decode_vlen_strings=True,
driver=None,
driver_kwds=None,
) -> Dataset:
filename_or_obj = _normalize_path(filename_or_obj)
store = H5NetCDFStore.open(
Expand All @@ -407,6 +414,8 @@ def open_dataset( # type: ignore[override] # allow LSP violation, not supporti
invalid_netcdf=invalid_netcdf,
phony_dims=phony_dims,
decode_vlen_strings=decode_vlen_strings,
driver=driver,
driver_kwds=driver_kwds,
)

store_entrypoint = StoreBackendEntrypoint()
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def _importorskip(
requires_pandas_version_two = pytest.mark.skipif(
not has_pandas_version_two, reason="requires pandas 2.0.0"
)
has_h5netcdf_ros3 = _importorskip("h5netcdf", "1.3.0")
requires_h5netcdf_ros3 = pytest.mark.skipif(
not has_h5netcdf_ros3[0], reason="requires h5netcdf 1.3.0"
)

# change some global options for tests
set_options(warn_for_unclosed_files=True)
Expand Down
29 changes: 29 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
requires_dask,
requires_fsspec,
requires_h5netcdf,
requires_h5netcdf_ros3,
requires_iris,
requires_netCDF4,
requires_pydap,
Expand Down Expand Up @@ -3448,6 +3449,34 @@ def test_write_inconsistent_chunks(self) -> None:
assert actual["y"].encoding["chunksizes"] == (100, 50)


@requires_h5netcdf_ros3
class TestH5NetCDFDataRos3Driver(TestCommon):
engine: T_NetcdfEngine = "h5netcdf"
test_remote_dataset: str = (
"https://www.unidata.ucar.edu/software/netcdf/examples/OMI-Aura_L2-example.nc"
)

def test_get_variable_list(self) -> None:
with open_dataset(
self.test_remote_dataset,
engine="h5netcdf",
backend_kwargs={"driver": "ros3"},
) as actual:
assert "Temperature" in list(actual)

def test_get_variable_list_empty_driver_kwds(self) -> None:
driver_kwds = {
"secret_id": b"",
"secret_key": b"",
}
backend_kwargs = {"driver": "ros3", "driver_kwds": driver_kwds}

with open_dataset(
self.test_remote_dataset, engine="h5netcdf", backend_kwargs=backend_kwargs
) as actual:
assert "Temperature" in list(actual)


@pytest.fixture(params=["scipy", "netcdf4", "h5netcdf", "pynio", "zarr"])
def readengine(request):
return request.param
Expand Down

0 comments on commit 1411474

Please sign in to comment.