scipp.io.hdf5.save_hdf5#

scipp.io.hdf5.save_hdf5(obj, filename)#

Write an object out to file in HDF5 format.

Supported types include Variable, DataArray, Dataset, and DataGroup. Nested structures are supported.

Parameters:
  • obj (object) – The object to save. Can be a Variable, DataArray, Dataset, or DataGroup.

  • filename (str | PathLike[str] | StringIO | BytesIO | Group) – Path to the output file or an open HDF5 group.

Return type:

None

See also

scipp.io.load_hdf5

Load data from HDF5 files.

Examples

Save and load a Variable:

>>> import scipp as sc
>>> import tempfile
>>> var = sc.array(dims=['x'], values=[1.0, 2.0, 3.0], unit='m')
>>> with tempfile.NamedTemporaryFile(suffix='.h5') as f:
...     sc.io.save_hdf5(var, f.name)
...     loaded = sc.io.load_hdf5(f.name)
>>> loaded
<scipp.Variable> (x: 3)    float64              [m]  [1, 2, 3]

Save and load a DataArray with coordinates:

>>> da = sc.DataArray(
...     sc.array(dims=['x'], values=[10, 20, 30], unit='counts'),
...     coords={'x': sc.array(dims=['x'], values=[0.1, 0.2, 0.3], unit='m')}
... )
>>> with tempfile.NamedTemporaryFile(suffix='.h5') as f:
...     sc.io.save_hdf5(da, f.name)
...     loaded = sc.io.load_hdf5(f.name)
>>> loaded
<scipp.DataArray>
Dimensions: Sizes[x:3, ]
Coordinates:
* x                         float64              [m]  (x)  [0.1, 0.2, 0.3]
Data:
                              int64         [counts]  (x)  [10, 20, 30]