14 case APPEND:
return "APPEND";
27 if (test_access.good()) {
28 file = H5Fopen(
filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
30 file = H5Fcreate(
filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
34 file = H5Fcreate(
filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
37 assert_msg(test_access.good(),
"To use readonly access, file '%s' must exist "
39 file = H5Fopen(
filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
42 assert_msg(
file >= 0,
"Opening file %s with mode %s failed with status %d. Are you sure "
43 "all potential intermediate directories exist?",
49 assert_msg(H5Iis_valid(
file),
"File id %d invalid. Did you open a file before creating "
52 assert_msg(!group_name.empty(),
"Group name must not be empty.");
53 assert_msg(group_name.size() == 1 || group_name.back() !=
'/',
54 "Group name must not end with a '/' unless it's root, got '%s'.", group_name);
56 if (group_name[0] ==
'/') {
59 group_name =
"/" + group_name;
62 std::string::size_type idx = 0;
64 auto new_idx = group_name.find(
'/', idx + 1);
65 assert_msg(new_idx - idx > 1,
"Two consecutive '/' not allowed in group name '%s'.",
67 if (new_idx == std::string::npos) { new_idx = group_name.size(); }
68 group_name_ += group_name.substr(idx, new_idx - idx);
75 assert_msg(
group >= 0,
"Failed creating group '%s' in file '%s' with status '%d'.",
79 }
while (idx != group_name.size());
81 assert_msg(
group >= 0,
"Failed opening group '%s' in file '%s' with status '%d'.",
87 std::string HDF::readAttribute<std::string>(
const std::string& attr_name)
const {
88 assert_msg(H5Iis_valid(group),
"Group id %d invalid. Did you open a group before reading?",
90 hid_t attr = H5Aopen(group, attr_name.c_str(), H5P_DEFAULT);
91 assert_msg(attr >= 0,
"Attribute '%s' could not be accessed in group '%s' in file '%s'.",
92 attr_name, group_name_, filename_);
94 hid_t type = H5Aget_type(attr);
95 assert_msg(type >= 0,
"Failed getting type of attribute '%s' in group '%s' in file '%s'.",
96 attr_name, group_name_, filename_);
98 H5T_str_t
pad = H5Tget_strpad(type);
99 assert_msg(
pad >= 0,
"Attribute '%s' in group '%s' in file '%s' is not a string attribute.",
100 attr_name, group_name_, filename_);
101 std::size_t size = H5Tget_size(type);
102 std::vector<char>
str;
104 herr_t status = H5Aread(attr, type,
str.data());
105 assert_msg(status >= 0,
"Failed reading attribute '%s' from group '%s' in file '%s'.",
106 attr_name, groupName(), filename_);
110 return std::string(
str.data(), size);
115 return std::string(s.c_str());
119 return readAttribute<int>(attr_name);
123 return readAttribute<bool>(attr_name);
127 return readAttribute<double>(attr_name);
131 return readAttribute<float>(attr_name);
135 return readAttribute<std::string>(attr_name);
143 writeAttribute(attr_name,
static_cast<hbool_t
>(value), H5T_NATIVE_HBOOL, overwrite);
155 bool overwrite)
const {
156 assert_msg(H5Iis_valid(
group),
"Group id %d invalid. Did you open a group before writing?",
158 assert_msg(!value.empty(),
"Storing empty strings is not supported in HDF5.");
159 if (H5Aexists(
group, attr_name.c_str())) {
161 assert_msg(
false,
"Attribute '%s' in group '%s' in file '%s' already exists. To "
162 "overwrite its contents use parameter overwrite=true.",
165 herr_t status = H5Adelete(
group, attr_name.c_str());
166 assert_msg(status >= 0,
"Failed deleting existing attribute '%s' in group '%s' in "
167 "file '%s' before writing a new one.",
171 hid_t space = H5Screate(H5S_SCALAR);
172 hid_t type = H5Tcopy(H5T_C_S1);
173 herr_t err = H5Tset_size(type, value.size());
175 hid_t attr = H5Acreate2(
group, attr_name.c_str(), type, space, H5P_DEFAULT, H5P_DEFAULT);
176 assert_msg(attr >= 0,
"Failed creating attribute '%s' in group '%s' in file '%s'.",
179 herr_t status = H5Awrite(attr, type, value.data());
180 assert_msg(status >= 0,
"Failed writing attribute '%s' to group '%s' in file '%s'.",
182 err = H5Tclose(type);
184 err = H5Sclose(space);
185 assert_msg(err >= 0,
"Space closing failed.");
186 err = H5Aclose(attr);
187 assert_msg(err >= 0,
"Attribute closing failed.");
191 return read1DArray<int>(dataset_name);
195 return read1DArray<double>(dataset_name);
199 return read1DArray<float>(dataset_name);
203 return read2DArray<int>(dataset_name);
207 return read2DArray<double>(dataset_name);
211 return read2DArray<float>(dataset_name);
214 std::vector<std::vector<std::vector<int>>>
216 return read3DArray<int>(dataset_name);
219 std::vector<std::vector<std::vector<double>>>
221 return read3DArray<double>(dataset_name);
224 std::vector<std::vector<std::vector<float>>>
226 return read3DArray<float>(dataset_name);
230 if (!H5Iis_valid(
file))
return;
231 H5Fflush(
file, H5F_SCOPE_GLOBAL);
232 herr_t status = H5Fclose(
file);
237 if (!H5Iis_valid(
group))
return;
238 herr_t status = H5Gclose(
group);
239 assert_msg(status >= 0,
"Closing group '%s' in file '%s' failed with status %d.",
245 if (!H5Iis_valid(
file))
return;
246 herr_t status = H5Fclose(
file);
253 assert_msg(!H5Iis_valid(
group),
"Group '%s' already opened, using atomic makes no sense.",
255 assert_msg(!H5Iis_valid(
file),
"File '%s' already opened, using atomic makes no sense.",
264 assert_msg(!group_name.empty(),
"Group name must not be empty");
265 assert_msg(group_name.find(
"//") == std::string::npos,
"Group name '%s' should not contain "
266 "two consecutive '/'.", group_name);
267 if (group_name[0] ==
'/') {
272 if (group_name.length() >= 2 && group_name.back() ==
'/') {
278 auto* results =
static_cast<Members*
>(data);
280 herr_t status = H5Oget_info_by_name(loc_id, name, &infobuf, H5P_DEFAULT);
281 assert_msg(status >= 0,
"Could not get type of object '%s'.", name);
282 switch (infobuf.type) {
284 results->groups.emplace_back(name);
286 case H5O_TYPE_DATASET:
287 results->datasets.emplace_back(name);
289 case H5O_TYPE_NAMED_DATATYPE:
290 results->datatypes.emplace_back(name);
293 results->unknowns.emplace_back(name);
299 assert_msg(H5Iis_valid(
group),
"Group id %d invalid. Did you open a group before "
300 "iterating over it?",
group);
302 herr_t status = H5Literate(
group, H5_INDEX_NAME, H5_ITER_INC,
nullptr,