Skip to content
Snippets Groups Projects
Commit 71e79149 authored by Pierre Paleo's avatar Pierre Paleo Committed by payno
Browse files

Add h5utils

parent 943832a0
No related branches found
No related tags found
1 merge request!168Add fscan
import numpy as np
from ...io import HDF5File
def get_first_hdf5_entry(fname):
with HDF5File(fname, "r") as fid:
entry = list(fid.keys())[0]
return entry
def get_h5_value(fname, h5_path, default_ret=None):
with HDF5File(fname, "r") as fid:
try:
val_ptr = fid[h5_path][()]
except KeyError:
val_ptr = default_ret
return val_ptr
def get_h5obj_value(h5_obj, name, default=None):
if name in h5_obj:
return h5_obj[name][()]
return default
def _get_3D_subregion(sub_region):
if sub_region is None:
xmin, xmax, ymin, ymax, zmin, zmax = None, None, None, None, None, None
elif len(sub_region) == 3:
first_part, second_part, third_part = sub_region
xmin, xmax = first_part
ymin, ymax = second_part
zmin, zmax = third_part
elif len(sub_region) == 6:
xmin, xmax, ymin, ymax, zmin, zmax = sub_region
else:
raise ValueError(
"Expected parameter in the form (xmin, xmax, ymin, ymax, zmin, zmax) or ((xmin, xmax), (ymin, ymax), (zmin, zmax))"
)
return xmin, xmax, ymin, ymax, zmin, zmax
def get_hdf5_dataset_shape(fname, h5_data_path, sub_region=None):
zmin, zmax, ymin, ymax, xmin, xmax = _get_3D_subregion(sub_region)
with HDF5File(fname, "r") as f:
d_ptr = f[h5_data_path]
shape = d_ptr.shape
n_z, n_y, n_x = shape
# perhaps there is more elegant
res_shape = []
for n, bounds in zip([n_z, n_y, n_x], ((zmin, zmax), (ymin, ymax), (xmin, xmax))):
res_shape.append(np.arange(n)[bounds[0] : bounds[1]].size)
return tuple(res_shape)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment