Skip to content

Commit fd6ecd1

Browse files
committed
add functions for easy read-only data access
1 parent a82d047 commit fd6ecd1

File tree

1 file changed

+138
-2
lines changed

1 file changed

+138
-2
lines changed

src/zarr/api/asynchronous.py

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ async def open(
275275
path : str or None, optional
276276
The path within the store to open.
277277
storage_options : dict
278-
If using an fsspec URL to create the store, these will be passed to
279-
the backend implementation. Ignored otherwise.
278+
If the store is backed by an fsspec-based implementation, then this dict will be passed to
279+
the Store constructor for that implementation. Ignored otherwise.
280280
**kwargs
281281
Additional parameters are passed through to :func:`zarr.creation.open_array` or
282282
:func:`zarr.hierarchy.open_group`.
@@ -313,6 +313,47 @@ async def open(
313313
return await open_group(store=store_path, zarr_format=zarr_format, **kwargs)
314314

315315

316+
async def read(
317+
*,
318+
store: StoreLike | None = None,
319+
zarr_format: ZarrFormat | None = None,
320+
path: str | None = None,
321+
storage_options: dict[str, Any] | None = None,
322+
**kwargs: Any,
323+
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | AsyncGroup:
324+
"""Convenience function to open a group or array for reading. This function
325+
wraps :func:`zarr.api.asynchronous.open` See the documentation of that function for details.
326+
327+
Parameters
328+
----------
329+
store : Store or str, optional
330+
Store or path to directory in file system or name of zip file.
331+
zarr_format : {2, 3, None}, optional
332+
The zarr format to require. The default value of None will first look for Zarr v3 data,
333+
then Zarr v2 data, then fail if neither format is found.
334+
path : str or None, optional
335+
The path within the store to open.
336+
storage_options : dict, optional
337+
If using an fsspec URL to create the store, this will be passed to
338+
the backend implementation. Ignored otherwise.
339+
**kwargs
340+
Additional parameters are passed through to :func:`zarr.creation.open`.
341+
342+
Returns
343+
-------
344+
z : array or group
345+
Return type depends on what exists in the given store.
346+
"""
347+
return await open(
348+
store=store,
349+
mode="r",
350+
zarr_format=zarr_format,
351+
path=path,
352+
storage_options=storage_options,
353+
**kwargs,
354+
)
355+
356+
316357
async def open_consolidated(
317358
*args: Any, use_consolidated: Literal[True] = True, **kwargs: Any
318359
) -> AsyncGroup:
@@ -709,6 +750,66 @@ async def open_group(
709750
)
710751

711752

753+
async def read_group(
754+
store: StoreLike,
755+
path: str | None = None,
756+
zarr_format: ZarrFormat | None = None,
757+
storage_options: dict[str, Any] | None = None,
758+
use_consolidated: bool | str | None = None,
759+
) -> AsyncGroup:
760+
"""Open a group for reading. This function wraps :func:`zarr.api.asynchronous.open_group` See
761+
the documentation of that function for details.
762+
763+
Parameters
764+
----------
765+
store : Store, str, or mapping, optional
766+
Store or path to directory in file system or name of zip file.
767+
768+
Strings are interpreted as paths on the local file system
769+
and used as the ``root`` argument to :class:`zarr.store.LocalStore`.
770+
771+
Dictionaries are used as the ``store_dict`` argument in
772+
:class:`zarr.store.MemoryStore``.
773+
path : str, optional
774+
Group path within store.
775+
zarr_format : {2, 3, None}, optional
776+
The zarr format to require. The default value of None will first look for Zarr v3 data,
777+
then Zarr v2 data, then fail if neither format is found.
778+
storage_options : dict
779+
If the store is backed by an fsspec-based implementation, then this dict will be passed to
780+
the Store constructor for that implementation. Ignored otherwise.
781+
use_consolidated : bool or str, default None
782+
Whether to use consolidated metadata.
783+
784+
By default, consolidated metadata is used if it's present in the
785+
store (in the ``zarr.json`` for Zarr v3 and in the ``.zmetadata`` file
786+
for Zarr v2).
787+
788+
To explicitly require consolidated metadata, set ``use_consolidated=True``,
789+
which will raise an exception if consolidated metadata is not found.
790+
791+
To explicitly *not* use consolidated metadata, set ``use_consolidated=False``,
792+
which will fall back to using the regular, non consolidated metadata.
793+
794+
Zarr v2 allowed configuring the key storing the consolidated metadata
795+
(``.zmetadata`` by default). Specify the custom key as ``use_consolidated``
796+
to load consolidated metadata from a non-default key.
797+
798+
Returns
799+
-------
800+
g : group
801+
The new group.
802+
"""
803+
return await open_group(
804+
store=store,
805+
mode="r",
806+
path=path,
807+
storage_options=storage_options,
808+
zarr_format=zarr_format,
809+
use_consolidated=use_consolidated,
810+
)
811+
812+
712813
async def create(
713814
shape: ChunkCoords,
714815
*, # Note: this is a change from v2
@@ -893,6 +994,40 @@ async def create(
893994
)
894995

895996

997+
async def read_array(
998+
store: StoreLike,
999+
path: str | None = None,
1000+
zarr_format: ZarrFormat | None = None,
1001+
storage_options: dict[str, Any] | None = None,
1002+
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
1003+
"""Create an array for reading. Wraps `:func:zarr.api.asynchronous.create`.
1004+
See the documentation of that function for details.
1005+
1006+
Parameters
1007+
----------
1008+
store : Store or str
1009+
Store or path to directory in file system or name of zip file.
1010+
path : str, optional
1011+
Path under which the array is stored.
1012+
zarr_format : {2, 3, None}, optional
1013+
The zarr format to require. The default value of ``None`` will first look for Zarr v3 data,
1014+
then Zarr v2 data, then fail if neither format is found.
1015+
storage_options : dict
1016+
If using an fsspec URL to create the store, these will be passed to
1017+
the backend implementation. Ignored otherwise.
1018+
1019+
Returns
1020+
-------
1021+
z : array
1022+
The array.
1023+
"""
1024+
store_path = await make_store_path(store, path=path, mode="r", storage_options=storage_options)
1025+
return await AsyncArray.open(
1026+
store=store_path,
1027+
zarr_format=zarr_format,
1028+
)
1029+
1030+
8961031
async def empty(
8971032
shape: ChunkCoords, **kwargs: Any
8981033
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
@@ -1070,6 +1205,7 @@ async def open_array(
10701205
store=store_path,
10711206
zarr_format=zarr_format or _default_zarr_version(),
10721207
overwrite=store_path.store.mode.overwrite,
1208+
storage_options=storage_options,
10731209
**kwargs,
10741210
)
10751211
raise

0 commit comments

Comments
 (0)