Skip to content

Description:

  • A Python interface for the netCDF4 file-format that reads and writes local or remote HDF5 files directly via h5py or h5pyd, without relying on the Unidata netCDF library.

Resources:

Source and license:

Create a local netcdf file in a context manager, then open it again to work with the data

import h5netcdf
import numpy as np
from pathlib import Path
tmp = Path("/tmp/ganymede_demo")
tmp.mkdir(parents=True, exist_ok=True)
fp = tmp / "example.nc"
# Mode "w" creates a new file (and truncates any existing one)
with h5netcdf.File(fp, 'w') as f:
# set dimensions with a dictionary
f.dimensions = {'x': 5}
# and update them with a dict-like interface
# f.dimensions['x'] = 5
# f.dimensions.update({'x': 5})
v = f.create_variable('hello', ('x',), float)
v[:] = np.ones(5)
# you don't need to create groups first
# you also don't need to create dimensions first if you supply data
# with the new variable
v = f.create_variable('/grouped/data', ('y',), data=np.arange(10))
# access and modify attributes with a dict-like interface
v.attrs['foo'] = 'bar'
# you can access variables and groups directly using a hierarchical
# keys like h5py
print(f['/grouped/data'])
# add an unlimited dimension
f.dimensions['z'] = None
# explicitly resize a dimension and all variables using it
f.resize_dimension('z', 3)
# Mode "r" opens the file read-only
with h5netcdf.File(fp, 'r') as f:
print(dict(f.dimensions))
print(f['hello'][:])
print(f['/grouped/data'].attrs['foo'])

ganymede is made by Eugo