1
- from pathlib import Path
2
1
from typing import Any , Dict , MutableMapping , Optional , Union
3
2
4
- import fsspec
5
3
import numcodecs
6
4
import xarray as xr
7
5
from xarray import Dataset
8
6
9
7
from sgkit .typing import PathType
8
+ from sgkit .utils import has_keyword
10
9
11
10
12
11
def save_dataset (
13
12
ds : Dataset ,
14
13
store : Union [PathType , MutableMapping [str , bytes ]],
15
14
storage_options : Optional [Dict [str , str ]] = None ,
16
15
auto_rechunk : Optional [bool ] = None ,
16
+ zarr_format : int = 2 ,
17
17
** kwargs : Any ,
18
18
) -> None :
19
19
"""Save a dataset to Zarr storage.
@@ -35,11 +35,6 @@ def save_dataset(
35
35
kwargs
36
36
Additional arguments to pass to :meth:`xarray.Dataset.to_zarr`.
37
37
"""
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 )
43
38
if auto_rechunk is None :
44
39
auto_rechunk = False
45
40
for v in ds :
@@ -71,7 +66,9 @@ def save_dataset(
71
66
72
67
# Catch unequal chunking errors to provide a more helpful error message
73
68
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 )
75
72
except ValueError as e :
76
73
if "Zarr requires uniform chunk sizes" in str (
77
74
e
@@ -109,12 +106,7 @@ def load_dataset(
109
106
Dataset
110
107
The dataset loaded from the Zarr store or file system.
111
108
"""
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]
118
110
for v in ds :
119
111
# Workaround for https://github.com/pydata/xarray/issues/4386
120
112
if v .endswith ("_mask" ): # type: ignore
0 commit comments