Skip to content

Commit 6fe80b3

Browse files
committed
Add glob pattern filtering to netCDF4 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 6d29b6d commit 6fe80b3

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

xarray/backends/netCDF4_.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,13 +859,19 @@ def open_groups_as_dict(
859859
autoclose=False,
860860
**kwargs,
861861
) -> dict[str, Dataset]:
862-
from xarray.backends.common import _iter_nc_groups
862+
from xarray.backends.common import (
863+
_is_glob_pattern,
864+
_iter_nc_groups,
865+
_resolve_group_and_filter,
866+
)
863867
from xarray.core.treenode import NodePath
864868

865869
filename_or_obj = _normalize_path(filename_or_obj)
870+
871+
effective_group = None if (group and _is_glob_pattern(group)) else group
866872
store = NetCDF4DataStore.open(
867873
filename_or_obj,
868-
group=group,
874+
group=effective_group,
869875
format=format,
870876
clobber=clobber,
871877
diskless=diskless,
@@ -875,15 +881,17 @@ def open_groups_as_dict(
875881
autoclose=autoclose,
876882
)
877883

878-
# Check for a group and make it a parent if it exists
879-
if group:
880-
parent = NodePath("/") / NodePath(group)
884+
if effective_group:
885+
parent = NodePath("/") / NodePath(effective_group)
881886
else:
882887
parent = NodePath("/")
883888

884889
manager = store._manager
890+
all_group_paths = list(_iter_nc_groups(store.ds, parent=parent))
891+
_, filtered_paths = _resolve_group_and_filter(group, all_group_paths)
892+
885893
groups_dict = {}
886-
for path_group in _iter_nc_groups(store.ds, parent=parent):
894+
for path_group in filtered_paths:
887895
group_store = NetCDF4DataStore(manager, group=path_group, **kwargs)
888896
store_entrypoint = StoreBackendEntrypoint()
889897
with close_on_error(group_store):
@@ -897,7 +905,7 @@ def open_groups_as_dict(
897905
use_cftime=use_cftime,
898906
decode_timedelta=decode_timedelta,
899907
)
900-
if group:
908+
if effective_group:
901909
group_name = str(NodePath(path_group).relative_to(parent))
902910
else:
903911
group_name = str(NodePath(path_group))

0 commit comments

Comments
 (0)