Skip to content

Commit 8a460ba

Browse files
committed
Add convenience to_mdio function to write metadata
1 parent cfe234c commit 8a460ba

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/mdio/core/v1/builder.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,26 @@ def build(self) -> Dataset:
221221

222222
return make_dataset(all_variables, metadata)
223223

224+
def to_mdio(
225+
self,
226+
store: str,
227+
mode: str = "w",
228+
compute: bool = False,
229+
**kwargs: Mapping[str, str | int | float | bool],
230+
) -> Dataset:
231+
"""Write the dataset to a Zarr store and return the constructed mdio.Dataset.
232+
233+
This function constructs an mdio.Dataset from the MDIO dataset and writes its metadata
234+
to a Zarr store. The actual data is not written, only the metadata structure is created.
235+
"""
236+
return write_mdio_metadata(self.build(), store, mode, compute, **kwargs)
237+
224238

225239
def write_mdio_metadata(
226240
mdio_ds: Dataset,
227241
store: str,
242+
mode: str = "w",
243+
compute: bool = False,
228244
**kwargs: Mapping[str, str | int | float | bool],
229245
) -> mdio.Dataset:
230246
"""Write MDIO metadata to a Zarr store and return the constructed mdio.Dataset.
@@ -234,7 +250,9 @@ def write_mdio_metadata(
234250
235251
Args:
236252
mdio_ds: The MDIO dataset to serialize
237-
store: Path to the Zarr store
253+
store: Path to the Zarr or .mdio store
254+
mode: Write mode to pass to to_mdio(), e.g. 'w' or 'a'
255+
compute: Whether to compute (write) array chunks (True) or only metadata (False)
238256
**kwargs: Additional arguments to pass to to_mdio()
239257
240258
Returns:
@@ -270,11 +288,11 @@ def _generate_encodings() -> dict:
270288

271289
ds.to_mdio(
272290
store,
273-
mode="w",
291+
mode=mode,
274292
zarr_format=2,
275293
consolidated=True,
276294
safe_chunks=False,
277-
compute=False,
295+
compute=compute,
278296
encoding=_generate_encodings(),
279297
**kwargs,
280298
)

0 commit comments

Comments
 (0)