Set up a Zarr store for reading or writing, handling both Zarr v2 and v3. This function prepares a Zarr-compatible storage object (`store`) from a URL or existing store. It supports optional storage options for fsspec-based stores and automatically selects the appropriate store typ
(
url: str, storage_options: dict[str, object] | None = None, **kwargs: object
)
| 3888 | |
| 3889 | |
| 3890 | def _setup_zarr_store( |
| 3891 | url: str, storage_options: dict[str, object] | None = None, **kwargs: object |
| 3892 | ): |
| 3893 | """ |
| 3894 | Set up a Zarr store for reading or writing, handling both Zarr v2 and v3. |
| 3895 | |
| 3896 | This function prepares a Zarr-compatible storage object (`store`) from a URL or existing |
| 3897 | store. It supports optional storage options for fsspec-based stores and automatically |
| 3898 | selects the appropriate store type depending on the Zarr version. |
| 3899 | |
| 3900 | Parameters |
| 3901 | ---------- |
| 3902 | url: Zarr Array or str or MutableMapping |
| 3903 | Location of the data. A URL can include a protocol specifier like s3:// |
| 3904 | for remote data. Can also be any MutableMapping instance, which should |
| 3905 | be serializable if used in multiple processes. |
| 3906 | storage_options: dict | None, default = None |
| 3907 | Any additional parameters for the storage backend (ignored for local |
| 3908 | paths) |
| 3909 | **kwargs: |
| 3910 | Passed to determine whether the store should be readonly by evaluating the following: |
| 3911 | 'kwargs.pop("read_only", kwargs.pop("mode", "a") == "r")' |
| 3912 | |
| 3913 | Returns |
| 3914 | ------- |
| 3915 | store : zarr.store.Store or original url |
| 3916 | A Zarr-compatible store object. Can be: |
| 3917 | - `zarr.storage.FsspecStore` for Zarr v3 with storage options |
| 3918 | - `zarr.storage.FSStore` for Zarr v2 with storage options |
| 3919 | - The original URL/path if no storage options are provided |
| 3920 | """ |
| 3921 | # Cannot directly import FSStore from storage. |
| 3922 | from zarr import storage |
| 3923 | |
| 3924 | if storage_options is not None: |
| 3925 | if _zarr_v3(): |
| 3926 | read_only = kwargs.pop("read_only", kwargs.pop("mode", "a") == "r") |
| 3927 | store = storage.FsspecStore.from_url( |
| 3928 | url, read_only=read_only, storage_options=storage_options |
| 3929 | ) |
| 3930 | else: |
| 3931 | store = storage.FSStore(url, **storage_options) |
| 3932 | else: |
| 3933 | store = url |
| 3934 | return store |
| 3935 | |
| 3936 | |
| 3937 | def to_zarr( |