45 datatype str_dtype(
size_t size = H5T_VARIABLE) {
47 auto err = H5Tset_size(dt, size);
48 H5Tset_cset(dt, H5T_CSET_UTF8);
49 if (err < 0)
throw std::runtime_error(
"Error in str_dtype: H5Tset_size call failed");
62 auto *s_ptr = s.c_str();
63 auto err = H5Dwrite(ds, dt, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s_ptr);
64 if (err < 0)
throw std::runtime_error(
"Error in h5_write: Writing a string to the dataset " + name +
" in the group " + g.
name() +
" failed");
74 int rank = H5Sget_simple_extent_ndims(dspace);
75 if (rank != 0)
throw std::runtime_error(
"Error in h5_read: Reading a string from a dataspace with rank != 0 is not possible");
78 H5_ASSERT(H5Tget_class(dt) == H5T_STRING);
81 if (H5Tis_variable_str(dt)) {
83 std::array<char *, 1> rd_ptr{
nullptr};
84 auto err = H5Dread(ds, dt, H5S_ALL, H5S_ALL, H5P_DEFAULT, rd_ptr.data());
85 if (err < 0)
throw std::runtime_error(
"Error in h5_read: Reading a string from the dataset " + name +
" in the group " + g.
name() +
" failed");
89 err = H5Dvlen_reclaim(dt, dspace, H5P_DEFAULT, rd_ptr.data());
90 if (err < 0)
throw std::runtime_error(
"Error in h5_read: Freeing resources after reading a variable-length string failed");
92 std::vector<char> buf(H5Tget_size(dt) + 1, 0x00);
93 auto err = H5Dread(ds, dt, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buf[0]);
94 if (err < 0)
throw std::runtime_error(
"Error in h5_read: Reading a string from the dataset " + name +
" in the group " + g.
name() +
" failed");
95 s.append(&buf.front());
105 attribute attr = H5Acreate2(obj, name.c_str(), dt, space, H5P_DEFAULT, H5P_DEFAULT);
106 if (!attr.
is_valid())
throw std::runtime_error(
"Error in h5_write_attribute: Creating the attribute " + name +
" failed");
109 auto *s_ptr = s.c_str();
110 herr_t err = H5Awrite(attr, dt, &s_ptr);
111 if (err < 0)
throw std::runtime_error(
"Error in h5_write_attribute: Writing a string to the attribute " + name +
" failed");
117 if (H5LTfind_attribute(obj, name.c_str()) == 0)
return;
120 attribute attr = H5Aopen(obj, name.c_str(), H5P_DEFAULT);
122 int rank = H5Sget_simple_extent_ndims(dspace);
123 if (rank != 0)
throw std::runtime_error(
"Error in h5_read_attribute: Reading a string from a dataspace with rank != 0 is not possible");
126 H5_ASSERT(H5Tget_class(dt) == H5T_STRING);
129 if (H5Tis_variable_str(dt)) {
131 std::array<char *, 1> rd_ptr{
nullptr};
132 auto err = H5Aread(attr, dt, rd_ptr.data());
133 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute: Reading a string from the attribute " + name +
" failed");
137 err = H5Dvlen_reclaim(dt, dspace, H5P_DEFAULT, rd_ptr.data());
138 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute: Freeing resources after reading a variable-length string failed");
140 std::vector<char> buf(H5Tget_size(dt) + 1, 0x00);
141 auto err = H5Aread(attr, dt, (
void *)(&buf[0]));
142 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute: Reading a string from the attribute " + name +
" failed");
143 s.append(&buf.front());
150 dataspace dspace = H5Screate(H5S_SCALAR);
153 attribute attr = H5Acreate_by_name(g, key.c_str(), name.c_str(), dt, dspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
154 if (!attr.
is_valid())
throw std::runtime_error(
"Error in h5_write_attribute_to_key: Creating the attribute " + name +
" failed");
157 herr_t err = H5Awrite(attr, dt, (
void *)(s.c_str()));
158 if (err < 0)
throw std::runtime_error(
"Error in h5_write_attribute_to_key: Writing a string to the attribute " + name +
" failed");
164 if (H5Aexists_by_name(g, key.c_str(), name.c_str(), H5P_DEFAULT) == 0)
return;
167 attribute attr = H5Aopen_by_name(g, key.c_str(), name.c_str(), H5P_DEFAULT, H5P_DEFAULT);
169 int rank = H5Sget_simple_extent_ndims(dspace);
170 if (rank != 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Reading a string from a dataspace with rank != 0 is not possible");
173 H5_ASSERT(H5Tget_class(dt) == H5T_STRING);
176 if (H5Tis_variable_str(dt)) {
178 std::array<char *, 1> rd_ptr{
nullptr};
179 auto err = H5Aread(attr, dt, rd_ptr.data());
180 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Reading a string from the attribute " + name +
" failed");
184 err = H5Dvlen_reclaim(dt, dspace, H5P_DEFAULT, rd_ptr.data());
185 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Rreeing resources after reading a variable-length string failed");
187 std::vector<char> buf(H5Tget_size(dt) + 1, 0x00);
188 auto err = H5Aread(attr, dt, &buf[0]);
189 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Reading a string from the attribute " + name +
" failed");
190 s.append(&buf.front());
206 auto dt = cb.
dtype();
207 auto dspace = cb.
dspace();
211 auto err = H5Dwrite(ds, dt, dspace, H5S_ALL, H5P_DEFAULT, (
void *)cb.
buffer.data());
212 if (err < 0)
throw make_runtime_error(
"Error in h5_write: Writing a char_buf to the dataset ", name,
" in the group ", g.
name(),
" failed");
224 int dim = H5Sget_simple_extent_ndims(dspace);
226 H5Sget_simple_extent_dims(dspace, cb_out.
lengths.data(),
nullptr);
228 size_t size = H5Tget_size(ty);
229 cb_out.
lengths.push_back(size);
231 long ltot = std::accumulate(cb_out.
lengths.begin(), cb_out.
lengths.end(), 1, std::multiplies<>());
232 cb_out.
buffer.resize(std::max(ltot, 1l), 0x00);
236 auto err = H5Dread(ds, ty, cb_out.
dspace(), H5S_ALL, H5P_DEFAULT, (
void *)cb_out.
buffer.data());
237 if (err < 0)
throw make_runtime_error(
"Error in h5_read: Reading a char_buf from the dataset ", name,
" in the group ", g.
name(),
" failed");
240 cb = std::move(cb_out);
245 auto dt = cb.
dtype();
246 auto dspace = cb.
dspace();
249 attribute attr = H5Acreate2(obj, name.c_str(), dt, dspace, H5P_DEFAULT, H5P_DEFAULT);
253 herr_t status = H5Awrite(attr, dt, (
void *)cb.
buffer.data());
254 if (status < 0)
throw make_runtime_error(
"Error in h5_write_attribute: Writing a char_buf to the attribute ", name,
" failed");
259 attribute attr = H5Aopen(obj, name.c_str(), H5P_DEFAULT);
268 int dim = H5Sget_simple_extent_ndims(d_space);
270 H5Sget_simple_extent_dims(d_space, cb_out.
lengths.data(),
nullptr);
272 size_t size = H5Tget_size(ty);
273 cb_out.
lengths.push_back(size);
275 long ltot = std::accumulate(cb_out.
lengths.begin(), cb_out.
lengths.end(), 1, std::multiplies<>());
276 cb_out.
buffer.resize(std::max(ltot, 1l), 0x00);
280 auto err = H5Aread(attr, ty, (
void *)cb_out.
buffer.data());
281 if (err < 0)
throw make_runtime_error(
"Error in h5_read_attribute: Reading a char_buf from the attribute ", name,
" failed");
284 cb = std::move(cb_out);
A handle to an HDF5 group.
dataset create_dataset(std::string const &key, datatype ty, dataspace sp, hid_t pl) const
Create a dataset with the given key, datatype, dataspace and dataset creation property list in this g...
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.
A generic handle for HDF5 objects.
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
object datatype
Type alias for an HDF5 datatype.
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
T h5_read_attribute_from_key(group g, std::string const &key, std::string const &name)
Generic implementation for reading an HDF5 attribute.
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 dataset.
void h5_write_attribute(object obj, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 attribute.
void h5_write_attribute_to_key(group g, std::string const &key, std::string const &name, std::string const &s)
Write a std::string to an HDF5 attribute.
std::runtime_error make_runtime_error(Ts const &...ts)
Create a std::runtime_error with an error message constructed from the given arguments.
Macros used in the h5 library.
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.
Stores an arbitrary number of strings in a 1-dimensional std::vector<char>.
dataspace dspace() const
Get the HDF5 dataspace.
datatype dtype() const
Get the HDF5 datatype.
v_t lengths
Stores the number of strings in each dimension and the max. allowed length of the strings + 1.
std::vector< char > buffer
Stores strings in a 1-dimensional vector.
Provides some utility functions for h5.