34 id = H5Gopen2(f,
"/", H5P_DEFAULT);
35 if (
id < 0)
throw std::runtime_error(
"Error in h5::group: Opening the root group / for the file " + f.
name() +
" failed");
41 ssize_t size = H5Iget_name(
id,
nullptr, 1);
44 std::vector<char> buf(size + 1, 0x00);
45 H5Iget_name(
id, buf.data(), size + 1);
49 res.append(&(buf.front()));
53 bool group::has_key(std::string
const &key)
const {
return H5Lexists(
id, key.c_str(), H5P_DEFAULT); }
57 if (!
has_key(key))
return false;
60 hid_t id_node = H5Oopen(
id, key.c_str(), H5P_DEFAULT);
61 if (id_node <= 0)
return false;
64 bool r = (H5Iget_type(id_node) == H5I_GROUP);
71 if (!
has_key(key))
return false;
74 hid_t id_node = H5Oopen(
id, key.c_str(), H5P_DEFAULT);
75 if (id_node <= 0)
return false;
78 bool r = (H5Iget_type(id_node) == H5I_DATASET);
83 void group::unlink(std::string
const &key,
bool error_if_absent)
const {
87 if (error_if_absent)
throw std::runtime_error(
"Error in h5::group: " + key +
" does not exist in the group " +
name());
92 auto err = H5Ldelete(
id, key.c_str(), H5P_DEFAULT);
93 if (err < 0)
throw std::runtime_error(
"Error in h5::group: Unlinking " + key +
" in the group " +
name() +
" failed");
98 if (key.empty())
return *
this;
101 if (!
has_key(key))
throw std::runtime_error(
"Error in h5::group: " + key +
" does not exist in the group " +
name());
104 object obj = H5Gopen2(
id, key.c_str(), H5P_DEFAULT);
105 if (obj < 0)
throw std::runtime_error(
"Error in h5::group: Opening the subgroup " + key +
" in the group " +
name() +
" failed");
106 return {obj, parent_file};
111 if (key.empty())
return *
this;
114 if (delete_if_exists)
unlink(key);
117 object obj = H5Gcreate2(
id, key.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
118 if (not obj.
is_valid())
throw std::runtime_error(
"Error in h5::group: Creating the subgroup " + key +
" in the group " +
name() +
" failed");
119 return {obj, parent_file};
124 if (target_key.empty() || key.empty())
return;
127 if (!
has_key(target_key))
throw std::runtime_error(
"Error in h5::group: " + target_key +
" does not exist in the group " +
name());
130 if (delete_if_exists)
133 throw std::runtime_error(
"Error in h5::group: " + key +
" already exists in the group " +
name());
136 auto const err = H5Lcreate_soft(target_key.c_str(),
id, key.c_str(), H5P_DEFAULT, H5P_DEFAULT);
137 if (err < 0)
throw std::runtime_error(
"Error in h5::group: Creating the softlink " + key +
" -> " + target_key +
" failed");
142 if (!
has_key(key))
throw std::runtime_error(
"Error in h5::group: " + key +
" does not exist in the group " +
name());
145 dataset ds = H5Dopen2(
id, key.c_str(), H5P_DEFAULT);
146 if (!ds.
is_valid())
throw std::runtime_error(
"Error in h5::group: Opening the dataset " + key +
" in the group " +
name() +
" failed");
155 dataset ds = H5Dcreate2(
id, key.c_str(), ty, sp, H5P_DEFAULT, pl, H5P_DEFAULT);
156 if (!ds.
is_valid())
throw std::runtime_error(
"Error in h5::group: Creating the dataset " + key +
" in the group " +
name() +
" failed");
165 herr_t get_group_elements_name_ds(
::hid_t loc_id,
const char *name,
const H5L_info_t *,
void *opdata) {
166 H5O_info_t object_info;
167 herr_t err = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);
168 if (err < 0)
throw std::runtime_error(
"Error in h5::get_group_elements_name_ds: H5Oget_info_by_name call failed");
169 if (object_info.type == H5O_TYPE_DATASET)
static_cast<std::vector<std::string> *
>(opdata)->push_back(name);
173 herr_t get_group_elements_name_grp(
::hid_t loc_id,
const char *name,
const H5L_info_t *,
void *opdata) {
174 H5O_info_t object_info;
175 herr_t err = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);
176 if (err < 0)
throw std::runtime_error(
"Error in h5::get_group_elements_name_grp: H5Oget_info_by_name call failed");
177 if (object_info.type == H5O_TYPE_GROUP)
static_cast<std::vector<std::string> *
>(opdata)->push_back(name);
181 herr_t get_group_elements_name_ds_grp(
::hid_t loc_id,
const char *name,
const H5L_info_t *,
void *opdata) {
182 H5O_info_t object_info;
183 herr_t err = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);
184 if (err < 0)
throw std::runtime_error(
"Error in h5::get_group_elements_name_ds_grp: H5Oget_info_by_name call failed");
185 if ((object_info.type == H5O_TYPE_GROUP) or (object_info.type == H5O_TYPE_DATASET))
186 static_cast<std::vector<std::string> *
>(opdata)->push_back(name);
193 std::vector<std::string> grp_name;
194 int r = H5Literate(
::hid_t(
id), H5_INDEX_NAME, H5_ITER_NATIVE,
nullptr, get_group_elements_name_grp,
static_cast<void *
>(&grp_name));
195 if (r != 0)
throw std::runtime_error(
"Error in h5::group: Iterating over subgroups of the group " +
name() +
"failed");
200 std::vector<std::string> ds_name;
201 int r = H5Literate(
::hid_t(
id), H5_INDEX_NAME, H5_ITER_NATIVE,
nullptr, get_group_elements_name_ds,
static_cast<void *
>(&ds_name));
202 if (r != 0)
throw std::runtime_error(
"Error in h5::group: Iterating over datasets of the group " +
name() +
"failed");
207 std::vector<std::string> ds_name;
208 int r = H5Literate(
::hid_t(
id), H5_INDEX_NAME, H5_ITER_NATIVE,
nullptr, get_group_elements_name_ds_grp,
static_cast<void *
>(&ds_name));
209 if (r != 0)
throw std::runtime_error(
"Error in h5::group: Iterating over datasets and subgroups of the group " +
name() +
"failed");
A handle to an HDF5 file.
std::string name() const
Get the name of the file.
A handle to an HDF5 group.
bool has_dataset(std::string const &key) const
Check if a dataset with the given key exists in the group and is accessible.
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 has_subgroup(std::string const &key) const
Check if a subgroup with the given key exists in the group and is accessible.
group create_group(std::string const &key, bool delete_if_exists=true) const
Create a subgroup with the given key in the group.
group open_group(std::string const &key) const
Open a subgroup with the given key in the group.
void create_softlink(std::string const &target_key, std::string const &key, bool delete_if_exists=true) const
Create a softlink with the given key to a target with a given target key in this group.
std::vector< std::string > get_all_subgroup_dataset_names() const
Get all the names of the subgroups and datasets in the current group.
bool has_key(std::string const &key) const
Check if a link with the given key exists in the group.
std::vector< std::string > get_all_dataset_names() const
Get all the names of the datasets in the current group.
void unlink(std::string const &key, bool error_if_absent=false) const
Remove a link with the given key from the group.
std::vector< std::string > get_all_subgroup_names() const
Get all the names of the subgroups in the current group.
group()=default
Default constructor (only necessary for the Python interface).
A generic handle for HDF5 objects.
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
int64_t hid_t
ID type used in HDF5.