H5py¶
Description:
- Thin, pythonic wrapper around HDF5
Resources:
- [GitHub] https://github.com/h5py/h5py
- [Examples] https://github.com/h5py/h5py/tree/master/examples
- [Website] https://www.h5py.org/
- [Docs] https://docs.h5py.org/en/stable/
Example 1¶
Create a local hdf5 file with a dataset in a context manager, then open it again and read the data back
In [ ]:
Copied!
import h5py
import numpy as np
from pathlib import Path
tmp = Path("/tmp/ganymede_demo")
tmp.mkdir(parents=True, exist_ok=True)
fp = tmp / "geospatial_data.h5"
# Mode "w" creates a new file (and truncates any existing one)
with h5py.File(fp, "w") as f:
dset = f.create_dataset("mydataset", (100,), dtype='i')
dset[:] = np.arange(100)
dset.attrs["units"] = "metres"
print(dset)
# Mode "r" opens the file read-only
with h5py.File(fp, "r") as f:
dset = f["mydataset"]
print(dset.attrs["units"], dset[:10])
import h5py
import numpy as np
from pathlib import Path
tmp = Path("/tmp/ganymede_demo")
tmp.mkdir(parents=True, exist_ok=True)
fp = tmp / "geospatial_data.h5"
# Mode "w" creates a new file (and truncates any existing one)
with h5py.File(fp, "w") as f:
dset = f.create_dataset("mydataset", (100,), dtype='i')
dset[:] = np.arange(100)
dset.attrs["units"] = "metres"
print(dset)
# Mode "r" opens the file read-only
with h5py.File(fp, "r") as f:
dset = f["mydataset"]
print(dset.attrs["units"], dset[:10])