Skip to content

Commit c0461e7

Browse files
Up-to-date instructions for reading & writing to Zarr with Xarray (#10410)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f95bd2d commit c0461e7

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

doc/user-guide/io.rst

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -752,23 +752,57 @@ key ```storage_options``, part of ``backend_kwargs``.
752752
This also works with ``open_mfdataset``, allowing you to pass a list of paths or
753753
a URL to be interpreted as a glob string.
754754

755-
For writing, you must explicitly set up a ``MutableMapping``
756-
instance and pass this, as follows:
755+
For writing, you may either specify a bucket URL or explicitly set up a
756+
``zarr.abc.store.Store`` instance, as follows:
757757

758-
.. code:: python
758+
.. tab:: URL
759+
760+
.. code:: python
761+
762+
# write to the bucket via GCS URL
763+
ds.to_zarr("gs://<bucket/path/to/data.zarr>")
764+
# read it back
765+
ds_gcs = xr.open_zarr("gs://<bucket/path/to/data.zarr>")
766+
767+
.. tab:: fsspec
768+
769+
.. code:: python
770+
771+
import gcsfs
772+
import zarr
759773
760-
import gcsfs
774+
# manually manage the cloud filesystem connection -- useful, for example,
775+
# when you need to manage permissions to cloud resources
776+
fs = gcsfs.GCSFileSystem(project="<project-name>", token=None)
777+
zstore = zarr.storage.FsspecStore(fs, path="<bucket/path/to/data.zarr>")
778+
779+
# write to the bucket
780+
ds.to_zarr(store=zstore)
781+
# read it back
782+
ds_gcs = xr.open_zarr(zstore)
783+
784+
.. tab:: obstore
785+
786+
.. code:: python
787+
788+
import obstore
789+
import zarr
790+
791+
# alternatively, obstore offers a modern, performant interface for
792+
# cloud buckets
793+
gcsstore = obstore.store.GCSStore(
794+
"<bucket>", prefix="<path/to/data.zarr>", skip_signature=True
795+
)
796+
zstore = zarr.store.ObjectStore(gcsstore)
761797
762-
fs = gcsfs.GCSFileSystem(project="<project-name>", token=None)
763-
gcsmap = gcsfs.mapping.GCSMap("<bucket-name>", gcs=fs, check=True, create=False)
764-
# write to the bucket
765-
ds.to_zarr(store=gcsmap)
766-
# read it back
767-
ds_gcs = xr.open_zarr(gcsmap)
798+
# write to the bucket
799+
ds.to_zarr(store=zstore)
800+
# read it back
801+
ds_gcs = xr.open_zarr(zstore)
768802
769-
(or use the utility function ``fsspec.get_mapper()``).
770803
771804
.. _fsspec: https://filesystem-spec.readthedocs.io/en/latest/
805+
.. _obstore: https://developmentseed.org/obstore/latest/
772806
.. _Zarr: https://zarr.readthedocs.io/
773807
.. _Amazon S3: https://aws.amazon.com/s3/
774808
.. _Google Cloud Storage: https://cloud.google.com/storage/

0 commit comments

Comments
 (0)