Skip to content

Commit 9c85dd5

Browse files
ajelenakrabernatdcherian
authored
Allow chunk_store argument when opening Zarr datasets (#3804)
* Allow chunk store for Zarr datasets * Add test for open_zarr() chunk_store argument * Add "chunk_store" argument to to_zarr() * Simplify chunk_store argument handling * blacken * Add minimum zarr version requirement in docstring * Update xarray/tests/test_backends.py Co-authored-by: Ryan Abernathey <ryan.abernathey@gmail.com> Co-authored-by: dcherian <deepak@cherian.net> Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
1 parent a36d0a1 commit 9c85dd5

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

xarray/backends/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ def _validate_append_dim_and_encoding(
13071307
def to_zarr(
13081308
dataset,
13091309
store=None,
1310+
chunk_store=None,
13101311
mode=None,
13111312
synchronizer=None,
13121313
group=None,
@@ -1322,6 +1323,8 @@ def to_zarr(
13221323
"""
13231324
if isinstance(store, Path):
13241325
store = str(store)
1326+
if isinstance(chunk_store, Path):
1327+
chunk_store = str(store)
13251328
if encoding is None:
13261329
encoding = {}
13271330

@@ -1346,6 +1349,7 @@ def to_zarr(
13461349
synchronizer=synchronizer,
13471350
group=group,
13481351
consolidate_on_close=consolidated,
1352+
chunk_store=chunk_store,
13491353
)
13501354
zstore.append_dim = append_dim
13511355
writer = ArrayWriter()

xarray/backends/zarr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,14 @@ def open_group(
278278
group=None,
279279
consolidated=False,
280280
consolidate_on_close=False,
281+
chunk_store=None,
281282
):
282283
import zarr
283284

284285
open_kwargs = dict(mode=mode, synchronizer=synchronizer, path=group)
286+
if chunk_store:
287+
open_kwargs["chunk_store"] = chunk_store
288+
285289
if consolidated:
286290
# TODO: an option to pass the metadata_key keyword
287291
zarr_group = zarr.open_consolidated(store, **open_kwargs)
@@ -505,6 +509,7 @@ def open_zarr(
505509
drop_variables=None,
506510
consolidated=False,
507511
overwrite_encoded_chunks=False,
512+
chunk_store=None,
508513
decode_timedelta=None,
509514
**kwargs,
510515
):
@@ -565,6 +570,8 @@ def open_zarr(
565570
consolidated : bool, optional
566571
Whether to open the store using zarr's consolidated metadata
567572
capability. Only works for stores that have already been consolidated.
573+
chunk_store : MutableMapping, optional
574+
A separate Zarr store only for chunk data.
568575
decode_timedelta : bool, optional
569576
If True, decode variables and coordinates with time units in
570577
{'days', 'hours', 'minutes', 'seconds', 'milliseconds', 'microseconds'}
@@ -646,6 +653,7 @@ def maybe_decode_store(store, lock=False):
646653
synchronizer=synchronizer,
647654
group=group,
648655
consolidated=consolidated,
656+
chunk_store=chunk_store,
649657
)
650658
ds = maybe_decode_store(zarr_store)
651659

xarray/core/dataset.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ def to_netcdf(
15711571
def to_zarr(
15721572
self,
15731573
store: Union[MutableMapping, str, Path] = None,
1574+
chunk_store: Union[MutableMapping, str, Path] = None,
15741575
mode: str = None,
15751576
synchronizer=None,
15761577
group: str = None,
@@ -1589,6 +1590,9 @@ def to_zarr(
15891590
----------
15901591
store : MutableMapping, str or Path, optional
15911592
Store or path to directory in file system.
1593+
chunk_store : MutableMapping, str or Path, optional
1594+
Store or path to directory in file system only for Zarr array chunks.
1595+
Requires zarr-python v2.4.0 or later.
15921596
mode : {"w", "w-", "a", None}, optional
15931597
Persistence mode: "w" means create (overwrite if exists);
15941598
"w-" means create (fail if exists);
@@ -1649,6 +1653,7 @@ def to_zarr(
16491653
return to_zarr(
16501654
self,
16511655
store=store,
1656+
chunk_store=chunk_store,
16521657
mode=mode,
16531658
synchronizer=synchronizer,
16541659
group=group,

xarray/tests/test_backends.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,15 @@ def test_roundtrip_consolidated(self):
15641564
self.check_dtypes_roundtripped(expected, actual)
15651565
assert_identical(expected, actual)
15661566

1567+
def test_with_chunkstore(self):
1568+
expected = create_test_data()
1569+
with self.create_zarr_target() as store_target, self.create_zarr_target() as chunk_store:
1570+
save_kwargs = {"chunk_store": chunk_store}
1571+
self.save(expected, store_target, **save_kwargs)
1572+
open_kwargs = {"chunk_store": chunk_store}
1573+
with self.open(store_target, **open_kwargs) as ds:
1574+
assert_equal(ds, expected)
1575+
15671576
@requires_dask
15681577
def test_auto_chunk(self):
15691578
original = create_test_data().chunk()

0 commit comments

Comments
 (0)