Skip to content

Commit 7a3e3bc

Browse files
committed
Add glob pattern filtering to zarr backend
Use _resolve_group_and_filter in open_groups_as_dict to support glob patterns in the group parameter for selective group loading.
1 parent 6fe80b3 commit 7a3e3bc

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

xarray/backends/zarr.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,11 +1820,13 @@ def open_groups_as_dict(
18201820
zarr_version=None,
18211821
zarr_format=None,
18221822
) -> dict[str, Dataset]:
1823+
from xarray.backends.common import _is_glob_pattern, _resolve_group_and_filter
1824+
18231825
filename_or_obj = _normalize_path(filename_or_obj)
18241826

1825-
# Check for a group and make it a parent if it exists
1826-
if group:
1827-
parent = str(NodePath("/") / NodePath(group))
1827+
effective_group = None if (group and _is_glob_pattern(group)) else group
1828+
if effective_group:
1829+
parent = str(NodePath("/") / NodePath(effective_group))
18281830
else:
18291831
parent = str(NodePath("/"))
18301832

@@ -1841,8 +1843,11 @@ def open_groups_as_dict(
18411843
zarr_format=zarr_format,
18421844
)
18431845

1846+
_, filtered_paths = _resolve_group_and_filter(group, list(stores.keys()))
1847+
18441848
groups_dict = {}
1845-
for path_group, store in stores.items():
1849+
for path_group in filtered_paths:
1850+
store = stores[path_group]
18461851
store_entrypoint = StoreBackendEntrypoint()
18471852

18481853
with close_on_error(store):
@@ -1856,7 +1861,7 @@ def open_groups_as_dict(
18561861
use_cftime=use_cftime,
18571862
decode_timedelta=decode_timedelta,
18581863
)
1859-
if group:
1864+
if effective_group:
18601865
group_name = str(NodePath(path_group).relative_to(parent))
18611866
else:
18621867
group_name = str(NodePath(path_group))

0 commit comments

Comments
 (0)