Skip to content

Commit 2af0045

Browse files
committed
add StoreV3 support to creation routines
1 parent d3c523d commit 2af0045

File tree

2 files changed

+253
-104
lines changed

2 files changed

+253
-104
lines changed

zarr/creation.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def create(shape, chunks=True, dtype=None, compressor='default',
1919
fill_value=0, order='C', store=None, synchronizer=None,
2020
overwrite=False, path=None, chunk_store=None, filters=None,
2121
cache_metadata=True, cache_attrs=True, read_only=False,
22-
object_codec=None, dimension_separator=None, write_empty_chunks=True, **kwargs):
22+
object_codec=None, dimension_separator=None, write_empty_chunks=True, *,
23+
zarr_version=None, **kwargs):
2324
"""Create an array.
2425
2526
Parameters
@@ -77,7 +78,10 @@ def create(shape, chunks=True, dtype=None, compressor='default',
7778
that chunk's key is deleted. This setting enables sparser storage,
7879
as only chunks with non-fill-value data are stored, at the expense
7980
of overhead associated with checking the data of each chunk.
80-
81+
zarr_version : {None, 2, 3}, optional
82+
The zarr protocol version of the created array. If None, it will be
83+
inferred from ``store`` or ``chunk_store`` if they are provided,
84+
otherwise defaulting to 2.
8185
8286
Returns
8387
-------
@@ -122,9 +126,12 @@ def create(shape, chunks=True, dtype=None, compressor='default',
122126
<zarr.core.Array (10000, 10000) float64>
123127
124128
"""
129+
if zarr_version is None and store is None:
130+
zarr_version = getattr(chunk_store, '_store_version', 2)
125131

126132
# handle polymorphic store arg
127-
store = normalize_store_arg(store)
133+
store = normalize_store_arg(store, zarr_version=zarr_version)
134+
zarr_version = getattr(store, '_store_version', 2)
128135

129136
# API compatibility with h5py
130137
compressor, fill_value = _kwargs_compat(compressor, fill_value, kwargs)
@@ -141,6 +148,9 @@ def create(shape, chunks=True, dtype=None, compressor='default',
141148
f"{store_separator}")
142149
dimension_separator = normalize_dimension_separator(dimension_separator)
143150

151+
if zarr_version > 2 and path is None:
152+
raise ValueError("path must be supplied to initialize a zarr v3 array")
153+
144154
# initialize array metadata
145155
init_array(store, shape=shape, chunks=chunks, dtype=dtype, compressor=compressor,
146156
fill_value=fill_value, order=order, overwrite=overwrite, path=path,
@@ -388,6 +398,8 @@ def open_array(
388398
storage_options=None,
389399
partial_decompress=False,
390400
write_empty_chunks=True,
401+
*,
402+
zarr_version=None,
391403
**kwargs
392404
):
393405
"""Open an array using file-mode-like semantics.
@@ -450,6 +462,10 @@ def open_array(
450462
that chunk's key is deleted. This setting enables sparser storage,
451463
as only chunks with non-fill-value data are stored, at the expense
452464
of overhead associated with checking the data of each chunk.
465+
zarr_version : {None, 2, 3}, optional
466+
The zarr protocol version of the array to be opened. If None, it will
467+
be inferred from ``store`` or ``chunk_store`` if they are provided,
468+
otherwise defaulting to 2.
453469
454470
Returns
455471
-------
@@ -484,12 +500,21 @@ def open_array(
484500
# w- or x : create, fail if exists
485501
# a : read/write if exists, create otherwise (default)
486502

503+
if zarr_version is None and store is None:
504+
zarr_version = getattr(chunk_store, '_store_version', 2)
505+
487506
# handle polymorphic store arg
488507
clobber = (mode == 'w')
489-
store = normalize_store_arg(store, clobber=clobber, storage_options=storage_options, mode=mode)
508+
store = normalize_store_arg(store, clobber=clobber, storage_options=storage_options,
509+
mode=mode, zarr_version=zarr_version)
510+
zarr_version = getattr(store, '_store_version', 2)
490511
if chunk_store is not None:
491512
chunk_store = normalize_store_arg(chunk_store, clobber=clobber,
492-
storage_options=storage_options)
513+
storage_options=storage_options,
514+
zarr_version=zarr_version)
515+
516+
if zarr_version == 3 and path is None:
517+
path = 'array' # TODO: raise ValueError instead?
493518
path = normalize_storage_path(path)
494519

495520
# API compatibility with h5py
@@ -559,6 +584,7 @@ def _like_args(a, kwargs):
559584
kwargs.setdefault('compressor', a.compressor)
560585
kwargs.setdefault('order', a.order)
561586
kwargs.setdefault('filters', a.filters)
587+
kwargs.setdefault('zarr_version', a._version)
562588
else:
563589
kwargs.setdefault('compressor', 'default')
564590
kwargs.setdefault('order', 'C')

0 commit comments

Comments
 (0)