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, (
void const *)&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, (
void *)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, (
void *)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, (
void const *)&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, (
void *)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, (
void *)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");
166 H5E_BEGIN_TRY { exists = H5Aexists_by_name(g, key.c_str(), name.c_str(), H5P_DEFAULT); }
168 if (exists < 0)
throw std::runtime_error(
"Error in h5_read_attribute_from_key: " + key +
" cannot be resolved in the group " + g.
name());
171 if (exists == 0)
return;
174 attribute attr = H5Aopen_by_name(g, key.c_str(), name.c_str(), H5P_DEFAULT, H5P_DEFAULT);
176 int rank = H5Sget_simple_extent_ndims(dspace);
177 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");
180 H5_ASSERT(H5Tget_class(dt) == H5T_STRING);
183 if (H5Tis_variable_str(dt)) {
185 std::array<char *, 1> rd_ptr{
nullptr};
186 auto err = H5Aread(attr, dt, (
void *)rd_ptr.data());
187 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Reading a string from the attribute " + name +
" failed");
191 err = H5Dvlen_reclaim(dt, dspace, H5P_DEFAULT, (
void *)rd_ptr.data());
192 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Rreeing resources after reading a variable-length string failed");
194 std::vector<char> buf(H5Tget_size(dt) + 1, 0x00);
195 auto err = H5Aread(attr, dt, &buf[0]);
196 if (err < 0)
throw std::runtime_error(
"Error in h5_read_attribute_to_key: Reading a string from the attribute " + name +
" failed");
197 s.append(&buf.front());
213 auto dt = cb.
dtype();
214 auto dspace = cb.
dspace();
218 auto err = H5Dwrite(ds, dt, dspace, H5S_ALL, H5P_DEFAULT, (
void *)cb.
buffer.data());
219 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");
231 int dim = H5Sget_simple_extent_ndims(dspace);
233 H5Sget_simple_extent_dims(dspace, cb_out.
lengths.data(),
nullptr);
235 size_t size = H5Tget_size(ty);
236 cb_out.
lengths.push_back(size);
238 long ltot = std::accumulate(cb_out.
lengths.begin(), cb_out.
lengths.end(), 1, std::multiplies<>());
239 cb_out.
buffer.resize(std::max(ltot, 1l), 0x00);
243 auto err = H5Dread(ds, ty, cb_out.
dspace(), H5S_ALL, H5P_DEFAULT, (
void *)cb_out.
buffer.data());
244 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");
247 cb = std::move(cb_out);
252 auto dt = cb.
dtype();
253 auto dspace = cb.
dspace();
256 attribute attr = H5Acreate2(obj, name.c_str(), dt, dspace, H5P_DEFAULT, H5P_DEFAULT);
260 herr_t status = H5Awrite(attr, dt, (
void *)cb.
buffer.data());
261 if (status < 0)
throw make_runtime_error(
"Error in h5_write_attribute: Writing a char_buf to the attribute ", name,
" failed");
266 attribute attr = H5Aopen(obj, name.c_str(), H5P_DEFAULT);
275 int dim = H5Sget_simple_extent_ndims(d_space);
277 H5Sget_simple_extent_dims(d_space, cb_out.
lengths.data(),
nullptr);
279 size_t size = H5Tget_size(ty);
280 cb_out.
lengths.push_back(size);
282 long ltot = std::accumulate(cb_out.
lengths.begin(), cb_out.
lengths.end(), 1, std::multiplies<>());
283 cb_out.
buffer.resize(std::max(ltot, 1l), 0x00);
287 auto err = H5Aread(attr, ty, (
void *)cb_out.
buffer.data());
288 if (err < 0)
throw make_runtime_error(
"Error in h5_read_attribute: Reading a char_buf from the attribute ", name,
" failed");
291 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.
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.
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)
Write a scalar to an HDF5 dataset.
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.
void h5_write_attribute(object, std::string const &, std::string const &)
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.