Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Commit 0db84ed

Browse files
Do not call __exit__ on Zarr store when opening (#90)
* Do not call __exit__ on Zarr store when opening The `with` context when opening the zarr group with result in calling __exit__ on the store when the function completes. This calls `.close()` on ZipStore's, which results in errors: ``` ValueError: Attempt to use ZIP archive that was already closed ``` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent badb7bc commit 0db84ed

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

datatree/io.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,24 @@ def _open_datatree_netcdf(filename: str, **kwargs) -> DataTree:
8484
def _open_datatree_zarr(store, **kwargs) -> DataTree:
8585
import zarr # type: ignore
8686

87-
with zarr.open_group(store, mode="r") as zds:
88-
ds = open_dataset(store, engine="zarr", **kwargs)
89-
tree_root = DataTree.from_dict({"/": ds})
90-
for path in _iter_zarr_groups(zds):
91-
try:
92-
subgroup_ds = open_dataset(store, engine="zarr", group=path, **kwargs)
93-
except zarr.errors.PathNotFoundError:
94-
subgroup_ds = Dataset()
95-
96-
# TODO refactor to use __setitem__ once creation of new nodes by assigning Dataset works again
97-
node_name = NodePath(path).name
98-
new_node: DataTree = DataTree(name=node_name, data=subgroup_ds)
99-
tree_root._set_item(
100-
path,
101-
new_node,
102-
allow_overwrite=False,
103-
new_nodes_along_path=True,
104-
)
87+
zds = zarr.open_group(store, mode="r")
88+
ds = open_dataset(store, engine="zarr", **kwargs)
89+
tree_root = DataTree.from_dict({"/": ds})
90+
for path in _iter_zarr_groups(zds):
91+
try:
92+
subgroup_ds = open_dataset(store, engine="zarr", group=path, **kwargs)
93+
except zarr.errors.PathNotFoundError:
94+
subgroup_ds = Dataset()
95+
96+
# TODO refactor to use __setitem__ once creation of new nodes by assigning Dataset works again
97+
node_name = NodePath(path).name
98+
new_node: DataTree = DataTree(name=node_name, data=subgroup_ds)
99+
tree_root._set_item(
100+
path,
101+
new_node,
102+
allow_overwrite=False,
103+
new_nodes_along_path=True,
104+
)
105105
return tree_root
106106

107107

datatree/tests/test_io.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def test_to_zarr(self, tmpdir):
4040
roundtrip_dt = open_datatree(filepath, engine="zarr")
4141
assert_equal(original_dt, roundtrip_dt)
4242

43+
@requires_zarr
44+
def test_to_zarr_zip_store(self, tmpdir):
45+
from zarr.storage import ZipStore
46+
47+
filepath = str(
48+
tmpdir / "test.zarr.zip"
49+
) # casting to str avoids a pathlib bug in xarray
50+
original_dt = create_test_datatree()
51+
store = ZipStore(filepath)
52+
original_dt.to_zarr(store)
53+
54+
roundtrip_dt = open_datatree(store, engine="zarr")
55+
assert_equal(original_dt, roundtrip_dt)
56+
4357
@requires_zarr
4458
def test_to_zarr_not_consolidated(self, tmpdir):
4559
filepath = tmpdir / "test.zarr"

0 commit comments

Comments
 (0)