Skip to content

Commit f24cae3

Browse files
aladinorpre-commit-ci[bot]TomNicholaskeewis
authored
fixing behaviour for group parameter in open_datatree (#9666)
* adding draft for fixing behaviour for group parameter * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * new trial * new trial * fixing duplicate pahts and path in the root group * removing yield str(gpath) * implementing the proposed solution to hdf5 and netcdf backends * adding changes to whats-new.rst * removing encoding['source_group'] line to avoid conflicts with PR #9660 * adding test * adding test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * adding assert subgroup_tree.root.parent is None * modifying tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update xarray/tests/test_backends_datatree.py Co-authored-by: Justus Magin <[email protected]> * applying suggested changes * updating test * adding Justus and Alfonso to the list of contributors to the DataTree entry * adding Justus and Alfonso to the list of contributors to the DataTree entry --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tom Nicholas <[email protected]> Co-authored-by: Justus Magin <[email protected]>
1 parent 519f05e commit f24cae3

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

doc/whats-new.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ New Features
2828
By `Owen Littlejohns <https://github.com/owenlittlejohns>`_,
2929
`Eni Awowale <https://github.com/eni-awowale>`_,
3030
`Matt Savoie <https://github.com/flamingbear>`_,
31-
`Stephan Hoyer <https://github.com/shoyer>`_ and
32-
`Tom Nicholas <https://github.com/TomNicholas>`_.
31+
`Stephan Hoyer <https://github.com/shoyer>`_,
32+
`Tom Nicholas <https://github.com/TomNicholas>`_,
33+
`Justus Magin <https://github.com/keewis>`_, and
34+
`Alfonso Ladino <https://github.com/aladinor>`_.
3335
- A migration guide for users of the prototype `xarray-contrib/datatree repository <https://github.com/xarray-contrib/datatree>`_ has been added, and can be found in the `DATATREE_MIGRATION_GUIDE.md` file in the repository root.
3436
By `Tom Nicholas <https://github.com/TomNicholas>`_.
3537
- Added zarr backends for :py:func:`open_groups` (:issue:`9430`, :pull:`9469`).

xarray/backends/common.py

-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ def _iter_nc_groups(root, parent="/"):
135135
yield str(parent)
136136
for path, group in root.groups.items():
137137
gpath = parent / path
138-
yield str(gpath)
139138
yield from _iter_nc_groups(group, parent=gpath)
140139

141140

xarray/backends/h5netcdf_.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ def open_datatree(
494494
driver_kwds=driver_kwds,
495495
**kwargs,
496496
)
497+
497498
return datatree_from_dict_with_io_cleanup(groups_dict)
498499

499500
def open_groups_as_dict(
@@ -556,7 +557,10 @@ def open_groups_as_dict(
556557
decode_timedelta=decode_timedelta,
557558
)
558559

559-
group_name = str(NodePath(path_group))
560+
if group:
561+
group_name = str(NodePath(path_group).relative_to(parent))
562+
else:
563+
group_name = str(NodePath(path_group))
560564
groups_dict[group_name] = group_ds
561565

562566
return groups_dict

xarray/backends/netCDF4_.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ def open_datatree(
729729
autoclose=autoclose,
730730
**kwargs,
731731
)
732+
732733
return datatree_from_dict_with_io_cleanup(groups_dict)
733734

734735
def open_groups_as_dict(
@@ -789,7 +790,10 @@ def open_groups_as_dict(
789790
use_cftime=use_cftime,
790791
decode_timedelta=decode_timedelta,
791792
)
792-
group_name = str(NodePath(path_group))
793+
if group:
794+
group_name = str(NodePath(path_group).relative_to(parent))
795+
else:
796+
group_name = str(NodePath(path_group))
793797
groups_dict[group_name] = group_ds
794798

795799
return groups_dict

xarray/backends/zarr.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,7 @@ def open_datatree(
14791479
zarr_format=zarr_format,
14801480
**kwargs,
14811481
)
1482+
14821483
return datatree_from_dict_with_io_cleanup(groups_dict)
14831484

14841485
def open_groups_as_dict(
@@ -1543,9 +1544,11 @@ def open_groups_as_dict(
15431544
use_cftime=use_cftime,
15441545
decode_timedelta=decode_timedelta,
15451546
)
1546-
group_name = str(NodePath(path_group))
1547+
if group:
1548+
group_name = str(NodePath(path_group).relative_to(parent))
1549+
else:
1550+
group_name = str(NodePath(path_group))
15471551
groups_dict[group_name] = group_ds
1548-
15491552
return groups_dict
15501553

15511554

@@ -1554,7 +1557,6 @@ def _iter_zarr_groups(root: ZarrGroup, parent: str = "/") -> Iterable[str]:
15541557
yield str(parent_nodepath)
15551558
for path, group in root.groups():
15561559
gpath = parent_nodepath / path
1557-
yield str(gpath)
15581560
yield from _iter_zarr_groups(group, parent=str(gpath))
15591561

15601562

xarray/tests/test_backends_datatree.py

+24
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,18 @@ def test_open_groups_to_dict(self, tmpdir) -> None:
337337
for ds in aligned_dict_of_datasets.values():
338338
ds.close()
339339

340+
def test_open_datatree_specific_group(self, tmpdir, simple_datatree) -> None:
341+
"""Test opening a specific group within a NetCDF file using `open_datatree`."""
342+
filepath = tmpdir / "test.nc"
343+
group = "/set1"
344+
original_dt = simple_datatree
345+
original_dt.to_netcdf(filepath)
346+
expected_subtree = original_dt[group].copy()
347+
expected_subtree.orphan()
348+
with open_datatree(filepath, group=group, engine=self.engine) as subgroup_tree:
349+
assert subgroup_tree.root.parent is None
350+
assert_equal(subgroup_tree, expected_subtree)
351+
340352

341353
@requires_h5netcdf
342354
class TestH5NetCDFDatatreeIO(DatatreeIOBase):
@@ -502,6 +514,18 @@ def test_open_groups(self, unaligned_datatree_zarr) -> None:
502514
for ds in unaligned_dict_of_datasets.values():
503515
ds.close()
504516

517+
def test_open_datatree_specific_group(self, tmpdir, simple_datatree) -> None:
518+
"""Test opening a specific group within a Zarr store using `open_datatree`."""
519+
filepath = tmpdir / "test.zarr"
520+
group = "/set2"
521+
original_dt = simple_datatree
522+
original_dt.to_zarr(filepath)
523+
expected_subtree = original_dt[group].copy()
524+
expected_subtree.orphan()
525+
with open_datatree(filepath, group=group, engine=self.engine) as subgroup_tree:
526+
assert subgroup_tree.root.parent is None
527+
assert_equal(subgroup_tree, expected_subtree)
528+
505529
@requires_dask
506530
def test_open_groups_chunks(self, tmpdir) -> None:
507531
"""Test `open_groups` with chunks on a zarr store."""

0 commit comments

Comments
 (0)