1- from pathlib import Path
21from typing import Any , Dict , MutableMapping , Optional , Union
32
4- import fsspec
53import numcodecs
64import xarray as xr
75from xarray import Dataset
86
97from sgkit .typing import PathType
8+ from sgkit .utils import has_keyword
109
1110
1211def save_dataset (
1312 ds : Dataset ,
1413 store : Union [PathType , MutableMapping [str , bytes ]],
1514 storage_options : Optional [Dict [str , str ]] = None ,
1615 auto_rechunk : Optional [bool ] = None ,
16+ zarr_format : int = 2 ,
1717 ** kwargs : Any ,
1818) -> None :
1919 """Save a dataset to Zarr storage.
@@ -35,11 +35,6 @@ def save_dataset(
3535 kwargs
3636 Additional arguments to pass to :meth:`xarray.Dataset.to_zarr`.
3737 """
38- if isinstance (store , str ):
39- storage_options = storage_options or {}
40- store = fsspec .get_mapper (store , ** storage_options )
41- elif isinstance (store , Path ):
42- store = str (store )
4338 if auto_rechunk is None :
4439 auto_rechunk = False
4540 for v in ds :
@@ -71,7 +66,9 @@ def save_dataset(
7166
7267 # Catch unequal chunking errors to provide a more helpful error message
7368 try :
74- ds .to_zarr (store , ** kwargs )
69+ if has_keyword (ds .to_zarr , "zarr_format" ): # from xarray v2024.10.0
70+ kwargs ["zarr_format" ] = zarr_format
71+ ds .to_zarr (store , storage_options = storage_options , ** kwargs )
7572 except ValueError as e :
7673 if "Zarr requires uniform chunk sizes" in str (
7774 e
@@ -109,12 +106,7 @@ def load_dataset(
109106 Dataset
110107 The dataset loaded from the Zarr store or file system.
111108 """
112- if isinstance (store , str ):
113- storage_options = storage_options or {}
114- store = fsspec .get_mapper (store , ** storage_options )
115- elif isinstance (store , Path ):
116- store = str (store )
117- ds : Dataset = xr .open_zarr (store , concat_characters = False , ** kwargs ) # type: ignore[no-untyped-call]
109+ ds : Dataset = xr .open_zarr (store , storage_options = storage_options , concat_characters = False , ** kwargs ) # type: ignore[no-untyped-call]
118110 for v in ds :
119111 # Workaround for https://github.com/pydata/xarray/issues/4386
120112 if v .endswith ("_mask" ): # type: ignore
0 commit comments