36namespace h5::array_interface {
43 if (v.rank() == 0)
return H5Screate(H5S_SCALAR);
46 dataspace dspace = H5Screate_simple(v.slab.rank(), v.parent_shape.data(),
nullptr);
47 if (!dspace.is_valid())
throw std::runtime_error(
"Error in make_mem_dspace: Creating the dataspace for an array_view failed");
50 herr_t err = H5Sselect_hyperslab(dspace, H5S_SELECT_SET, v.slab.offset.data(), v.slab.stride.data(), v.slab.count.data(),
51 (v.slab.block.empty() ?
nullptr : v.slab.block.data()));
52 if (err < 0)
throw std::runtime_error(
"Error in make_mem_dspace: Selecting the hyperslab failed");
62 if (rank == 0)
return {};
65 auto const view_size = std::accumulate(view_shape, view_shape + rank, 1l, std::multiplies<>());
66 if (view_size == 0)
return {
v_t(rank, 0),
v_t(rank, 1)};
79 v_t parent_shape(rank), h5_strides(rank);
84 for (
int u = 0; u < rank; ++u) h5_strides[u] = np_strides[u];
85 for (
int u = rank - 2; u >= 0; --u) {
87 for (
int v = u - 1; v >= 0; --v) { gcd = std::gcd(gcd, h5_strides[v]); }
88 parent_shape[u + 1] = gcd;
89 for (
int v = u; v >= 0; --v) { h5_strides[v] /= gcd; }
94 parent_shape[0] = view_shape[0] * h5_strides[0];
96 return {parent_shape, h5_strides};
102 bool has_complex_attribute = H5LTfind_attribute(ds,
"__complex__");
104 int rank = H5Sget_simple_extent_ndims(dspace);
106 H5Sget_simple_extent_dims(dspace, dims_out.data(),
nullptr);
108 return {std::move(dims_out), ty, has_complex_attribute};
125 if (compress and (v.
rank() != 0)) {
126 int n_dims = v.
rank();
127 std::vector<hsize_t> chunk_dims(n_dims);
130 for (
int i = v.
rank() - 1; i >= 0; --i) {
131 H5_ASSERT(max_chunk_size >= chunk_size);
132 hsize_t max_dim = max_chunk_size / chunk_size;
133 chunk_dims[i] = std::clamp(hs_shape[i],
hsize_t{1}, max_dim);
134 chunk_size *= chunk_dims[i];
136 cparms = H5Pcreate(H5P_DATASET_CREATE);
137 H5Pset_chunk(cparms, n_dims, chunk_dims.data());
138 H5Pset_deflate(cparms, 1);
142 dataspace file_dspace = H5Screate_simple(v.
slab.
rank(), hs_shape.data(),
nullptr);
145 dataset ds = H5Dcreate2(g, name.c_str(), v.
ty, file_dspace, H5P_DEFAULT, cparms, H5P_DEFAULT);
147 throw std::runtime_error(
"Error in h5::array_interface::write: Creating the dataset " + name +
" in the group " + g.
name() +
" failed");
150 dataspace mem_dspace = make_mem_dspace(v);
153 if (H5Sget_simple_extent_npoints(mem_dspace) > 0) {
154 herr_t err = H5Dwrite(ds, v.
ty, mem_dspace, H5S_ALL, H5P_DEFAULT, v.
start);
156 throw std::runtime_error(
"Error in h5::array_interface::write: Writing to the dataset " + name +
" in the group" + g.
name() +
" failed");
165 if (sl.
empty())
return;
168 if (v.
slab.
size() != sl.
size())
throw std::runtime_error(
"Error in h5::array_interface::write_slice: Incompatible sizes");
172 throw std::runtime_error(
"Error in h5::array_interface::write_slice: Incompatible HDF5 types: " +
get_name_of_h5_type(v.
ty)
177 dataspace file_dspace = H5Dget_space(ds);
178 herr_t err = H5Sselect_hyperslab(file_dspace, H5S_SELECT_SET, sl.
offset.data(), sl.
stride.data(), sl.
count.data(),
179 (sl.
block.empty() ?
nullptr : sl.
block.data()));
180 if (err < 0)
throw std::runtime_error(
"Error in h5::array_interface::write_slice: Selecting the hyperslab failed");
183 dataspace mem_dspace = make_mem_dspace(v);
186 if (H5Sget_simple_extent_npoints(file_dspace) > 0) {
187 err = H5Dwrite(ds, v.
ty, mem_dspace, file_dspace, H5P_DEFAULT, v.
start);
189 throw std::runtime_error(
"Error in h5::array_interface::write_slice: Writing the dataset " + name +
" in the group " + g.
name() +
" failed");
195 if (H5LTfind_attribute(obj, name.c_str()) != 0)
196 throw std::runtime_error(
"Error in h5::array_interface::write_attribute: Attribute " + name +
" already exists");
199 dataspace mem_dspace = make_mem_dspace(v);
202 attribute attr = H5Acreate2(obj, name.c_str(), v.
ty, mem_dspace, H5P_DEFAULT, H5P_DEFAULT);
203 if (!attr.
is_valid())
throw std::runtime_error(
"Error in h5::array_interface::write_attribute: Creating the attribute " + name +
" failed");
206 herr_t err = H5Awrite(attr, v.
ty, v.
start);
207 if (err < 0)
throw std::runtime_error(
"Error in h5::array_interface::write_attribute: Writing to the attribute " + name +
" failed");
213 dataspace file_dspace = H5Dget_space(ds);
216 if (not sl.
empty()) {
217 herr_t err = H5Sselect_hyperslab(file_dspace, H5S_SELECT_SET, sl.
offset.data(), sl.
stride.data(), sl.
count.data(),
218 (sl.
block.empty() ?
nullptr : sl.
block.data()));
219 if (err < 0)
throw std::runtime_error(
"Error in h5::array_interface::read: selecting the hyperslab failed");
224 if (H5Tget_class(v.
ty) != H5Tget_class(ds_info.ty))
225 throw std::runtime_error(
"Error in h5::array_interface::read: Incompatible HDF5 types: " +
get_name_of_h5_type(v.
ty)
229 std::cerr <<
"WARNING: HDF5 type mismatch while reading into an array_view: " +
get_name_of_h5_type(v.
ty)
232 auto sl_size = sl.
size();
233 if (sl.
empty()) { sl_size = std::accumulate(ds_info.lengths.begin(), ds_info.lengths.end(), (
hsize_t)1, std::multiplies<>()); }
234 if (sl_size != v.
slab.
size())
throw std::runtime_error(
"Error in h5::array_interface::read: Incompatible sizes");
237 dataspace mem_dspace = make_mem_dspace(v);
240 if (H5Sget_simple_extent_npoints(file_dspace) > 0) {
241 herr_t err = H5Dread(ds, v.
ty, mem_dspace, file_dspace, H5P_DEFAULT, v.
start);
243 throw std::runtime_error(
"Error in h5::array_interface::read: Reading the dataset " + name +
" in the group " + g.
name() +
" failed");
249 attribute attr = H5Aopen(obj, name.c_str(), H5P_DEFAULT);
250 if (!attr.
is_valid())
throw std::runtime_error(
"Error in h5::array_interface::read_attribute: Opening the attribute " + name +
" failed");
254 int rank = H5Sget_simple_extent_ndims(space);
255 if (rank != 0)
throw std::runtime_error(
"Error in h5::array_interface::read_attribute: Attribute " + name +
" has a rank != 0");
258 auto eq = H5Tequal(H5Aget_type(attr), v.
ty);
259 if (eq < 0)
throw std::runtime_error(
"Error in h5::array_interface::read_attribute: H5Tequal call failed");
260 if (eq == 0)
throw std::runtime_error(
"Error in h5::array_interface::read_attribute: Incompatible HDF5 types");
263 auto err = H5Aread(attr, v.
ty, v.
start);
264 if (err < 0)
throw std::runtime_error(
"Error in h5::array_interface::read_attribute: Reading the attribute " + name +
" failed");
Provides a generic interface to read/write n-dimensional arrays from/to HDF5.
A handle to an HDF5 group.
dataset open_dataset(std::string const &key) const
Open a dataset with the given key in the group.
std::string name() const
Get the name of the group.
void unlink(std::string const &key, bool error_if_absent=false) const
Remove a link with the given key from the group.
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
object datatype
Type alias for an HDF5 datatype.
object attribute
Type alias for an HDF5 attribute.
object dataset
Type alias for an HDF5 dataset.
object dataspace
Type alias for an HDF5 dataspace.
object proplist
Type alias for an HDF5 property list.
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
std::string get_name_of_h5_type(datatype dt)
Get the name of an h5::datatype (for error messages).
void read_attribute(object obj, std::string const &name, array_view v)
Read from an HDF5 attribute into an array view.
void write_slice(group g, std::string const &name, array_view const &v, hyperslab sl)
Write an array view to a selected hyperslab of an existing HDF5 dataset.
void read(group g, std::string const &name, array_view v, hyperslab sl)
Read a given hyperslab from an HDF5 dataset into an array view.
void write_attribute(object obj, std::string const &name, array_view v)
Write an array view to an HDF5 attribute.
dataset_info get_dataset_info(dataset ds)
Retrieve the shape and the h5::datatype from a dataset.
void write(group g, std::string const &name, array_view const &v, bool compress)
Write an array view to an HDF5 dataset.
std::pair< v_t, v_t > get_parent_shape_and_h5_strides(long const *np_strides, int rank, long const *view_shape)
Given a view on an n-dimensional array (dataspace) by specifying its numpy/nda-style strides and its ...
void h5_write_attribute(object obj, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 attribute.
std::vector< hsize_t > v_t
Vector of h5::hsize_t used throughout the h5 library.
unsigned long long hsize_t
Size type used in HDF5.
Macros used in the h5 library.
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.
Struct representing a view on an n-dimensional array/dataspace.
void * start
Pointer to the data of the array.
datatype ty
h5::datatype stored in the array.
bool is_complex
Whether the data is complex valued.
int rank() const
Get the rank of the view (including the possible added imaginary dimension).
hyperslab slab
h5::array_interface::hyperslab specifying the selection of the view.
Simple struct to store basic information about an HDF5 dataset.
Struct representing an HDF5 hyperslab.
auto size() const
Get the total number of elements in the hyperslab.
v_t block
Shape of a single block selected from the dataspace.
bool empty() const
Check whether the hyperslab is empty (has been initialized).
v_t stride
Stride in each dimension (in the HDF5 sense).
v_t offset
Index offset for each dimension.
v_t count
Number of elements or blocks to select along each dimension.
v_t shape() const
Get the shape of the selected hyperslab.
int rank() const
Get the rank of the hyperslab (including the possible added imaginary dimension).