Skip to content
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

FIX: h5py>=3 string decoding #4893

Merged
merged 8 commits into from
Feb 17, 2021
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
2 changes: 1 addition & 1 deletion ci/requirements/environment-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
- dask<2021.02.0
- distributed
- h5netcdf
- h5py=2
- h5py
- hdf5
- hypothesis
- iris
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
- dask<2021.02.0
- distributed
- h5netcdf
- h5py=2
- h5py
- hdf5
- hypothesis
- iris
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/py38-all-but-dask.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
- cftime
- coveralls
- h5netcdf
- h5py=2
- h5py
- hdf5
- hypothesis
- lxml # Optional dep of pydap
Expand Down
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Bug fixes
By `Leif Denby <https://github.com/leifdenby>`_.
- Fix time encoding bug associated with using cftime versions greater than
1.4.0 with xarray (:issue:`4870`, :pull:`4871`). By `Spencer Clark <https://github.com/spencerkclark>`_.
- Fix decoding of vlen strings using h5py versions greater than 3.0.0 with h5netcdf backend (:issue:`4570`, :pull:`4893`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions xarray/backends/h5netcdf_.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def open(
autoclose=False,
invalid_netcdf=None,
phony_dims=None,
decode_vlen_strings=True,
):

if isinstance(filename, bytes):
Expand All @@ -157,6 +158,10 @@ def open(
"h5netcdf backend keyword argument 'phony_dims' needs "
"h5netcdf >= 0.8.0."
)
if LooseVersion(h5netcdf.__version__) >= LooseVersion(
"0.10.0"
) and LooseVersion(h5netcdf.core.h5py.__version__) >= LooseVersion("3.0.0"):
kwargs["decode_vlen_strings"] = decode_vlen_strings

if lock is None:
if mode == "r":
Expand Down Expand Up @@ -358,6 +363,7 @@ def open_dataset(
lock=None,
invalid_netcdf=None,
phony_dims=None,
decode_vlen_strings=True,
):

store = H5NetCDFStore.open(
Expand All @@ -367,6 +373,7 @@ def open_dataset(
lock=lock,
invalid_netcdf=invalid_netcdf,
phony_dims=phony_dims,
decode_vlen_strings=decode_vlen_strings,
)

store_entrypoint = StoreBackendEntrypoint()
Expand Down
18 changes: 15 additions & 3 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2578,13 +2578,19 @@ def test_open_dataset_group(self):
v = group.createVariable("x", "int")
v[...] = 42

h5 = h5netcdf.File(tmp_file, mode="r")
kwargs = {}
if LooseVersion(h5netcdf.__version__) >= LooseVersion(
"0.10.0"
) and LooseVersion(h5netcdf.core.h5py.__version__) >= LooseVersion("3.0.0"):
kwargs = dict(decode_vlen_strings=True)

h5 = h5netcdf.File(tmp_file, mode="r", **kwargs)
store = backends.H5NetCDFStore(h5["g"])
with open_dataset(store) as ds:
expected = Dataset({"x": ((), 42)})
assert_identical(expected, ds)

h5 = h5netcdf.File(tmp_file, mode="r")
h5 = h5netcdf.File(tmp_file, mode="r", **kwargs)
store = backends.H5NetCDFStore(h5, group="g")
with open_dataset(store) as ds:
expected = Dataset({"x": ((), 42)})
Expand All @@ -2599,7 +2605,13 @@ def test_deepcopy(self):
v = nc.createVariable("y", np.int32, ("x",))
v[:] = np.arange(10)

h5 = h5netcdf.File(tmp_file, mode="r")
kwargs = {}
if LooseVersion(h5netcdf.__version__) >= LooseVersion(
"0.10.0"
) and LooseVersion(h5netcdf.core.h5py.__version__) >= LooseVersion("3.0.0"):
kwargs = dict(decode_vlen_strings=True)

h5 = h5netcdf.File(tmp_file, mode="r", **kwargs)
store = backends.H5NetCDFStore(h5)
with open_dataset(store) as ds:
copied = ds.copy(deep=True)
Expand Down