|
13 | 13 | from xarray.backends.writers import to_zarr as xr_to_zarr |
14 | 14 |
|
15 | 15 | from mdio.constants import ZarrFormat |
16 | | -from mdio.core.config import MDIOSettings |
17 | 16 | from mdio.core.zarr_io import zarr_warnings_suppress_unstable_structs_v3 |
18 | 17 |
|
19 | 18 | if TYPE_CHECKING: |
|
26 | 25 |
|
27 | 26 |
|
28 | 27 | def _normalize_path(path: UPath | Path | str) -> UPath: |
29 | | - """Normalize a path to a UPath.""" |
30 | 28 | return UPath(path) |
31 | 29 |
|
32 | 30 |
|
33 | 31 | def _normalize_storage_options(path: UPath) -> dict[str, Any] | None: |
34 | | - """Normalize storage options from UPath.""" |
35 | 32 | return None if len(path.storage_options) == 0 else path.storage_options |
36 | 33 |
|
37 | 34 |
|
38 | | -def _get_gcs_store(path: UPath) -> tuple[Any, dict[str, Any] | None]: |
39 | | - """Get store and storage options, using local fake GCS server if enabled. |
40 | | -
|
41 | | - Args: |
42 | | - path: UPath pointing to storage location. |
43 | | -
|
44 | | - Returns: |
45 | | - Tuple of (store, storage_options) where store is either a mapper or path string. |
46 | | - """ |
47 | | - settings = MDIOSettings() |
48 | | - |
49 | | - if settings.local_gcs_server and str(path).startswith("gs://"): |
50 | | - import gcsfs # noqa: PLC0415 |
51 | | - |
52 | | - fs = gcsfs.GCSFileSystem( |
53 | | - endpoint_url="http://localhost:4443", |
54 | | - token="anon", # noqa: S106 |
55 | | - ) |
56 | | - store = fs.get_mapper(path.as_posix().replace("gs://", "")) |
57 | | - return store, None |
58 | | - |
59 | | - return path.as_posix(), _normalize_storage_options(path) |
60 | | - |
61 | | - |
62 | 35 | def open_mdio(input_path: UPath | Path | str, chunks: T_Chunks = None) -> xr_Dataset: |
63 | 36 | """Open a Zarr dataset from the specified universal file path. |
64 | 37 |
|
@@ -114,22 +87,17 @@ def to_mdio( # noqa: PLR0913 |
114 | 87 | the region of existing MDIO array(s) in which to write this dataset's data. |
115 | 88 | """ |
116 | 89 | output_path = _normalize_path(output_path) |
| 90 | + storage_options = _normalize_storage_options(output_path) |
117 | 91 | zarr_format = zarr.config.get("default_zarr_format") |
118 | 92 |
|
119 | | - store, storage_options = _get_gcs_store(output_path) |
120 | | - |
121 | | - kwargs = { |
122 | | - "dataset": dataset, |
123 | | - "store": store, |
124 | | - "mode": mode, |
125 | | - "compute": compute, |
126 | | - "consolidated": zarr_format == ZarrFormat.V2, |
127 | | - "region": region, |
128 | | - "write_empty_chunks": False, |
129 | | - } |
130 | | - |
131 | | - if storage_options is not None and not isinstance(store, dict): |
132 | | - kwargs["storage_options"] = storage_options |
133 | | - |
134 | 93 | with zarr_warnings_suppress_unstable_structs_v3(): |
135 | | - xr_to_zarr(**kwargs) |
| 94 | + xr_to_zarr( |
| 95 | + dataset, |
| 96 | + store=output_path.as_posix(), # xarray doesn't like URI when file:// is protocol |
| 97 | + mode=mode, |
| 98 | + compute=compute, |
| 99 | + consolidated=zarr_format == ZarrFormat.V2, # on for v2, off for v3 |
| 100 | + region=region, |
| 101 | + storage_options=storage_options, |
| 102 | + write_empty_chunks=False, |
| 103 | + ) |
0 commit comments