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()));
57 H5E_BEGIN_TRY { res = H5Lexists(
id, key.c_str(), H5P_DEFAULT); }
64 if (!
has_key(key))
return false;
67 hid_t id_node = H5Oopen(
id, key.c_str(), H5P_DEFAULT);
68 if (id_node <= 0)
return false;
71 bool r = (H5Iget_type(id_node) == H5I_GROUP);
78 if (!
has_key(key))
return false;
81 hid_t id_node = H5Oopen(
id, key.c_str(), H5P_DEFAULT);
82 if (id_node <= 0)
return false;
85 bool r = (H5Iget_type(id_node) == H5I_DATASET);
90 void group::unlink(std::string
const &key,
bool error_if_absent)
const {
94 if (error_if_absent)
throw std::runtime_error(
"Error in h5::group: " + key +
" does not exist in the group " +
name());
99 auto err = H5Ldelete(
id, key.c_str(), H5P_DEFAULT);
100 if (err < 0)
throw std::runtime_error(
"Error in h5::group: Unlinking " + key +
" in the group " +
name() +
" failed");
105 if (key.empty())
return *
this;
108 if (!
has_key(key))
throw std::runtime_error(
"Error in h5::group: " + key +
" does not exist in the group " +
name());
111 object obj = H5Gopen2(
id, key.c_str(), H5P_DEFAULT);
112 if (obj < 0)
throw std::runtime_error(
"Error in h5::group: Opening the subgroup " + key +
" in the group " +
name() +
" failed");
113 return {obj, parent_file};
118 if (key.empty())
return *
this;
121 if (delete_if_exists)
unlink(key);
124 object obj = H5Gcreate2(
id, key.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
125 if (not obj.
is_valid())
throw std::runtime_error(
"Error in h5::group: Creating the subgroup " + key +
" in the group " +
name() +
" failed");
126 return {obj, parent_file};
131 if (target_key.empty() || key.empty())
return;
134 if (!
has_key(target_key))
throw std::runtime_error(
"Error in h5::group: " + target_key +
" does not exist in the group " +
name());
137 if (delete_if_exists)
140 throw std::runtime_error(
"Error in h5::group: " + key +
" already exists in the group " +
name());
143 auto const err = H5Lcreate_soft(target_key.c_str(),
id, key.c_str(), H5P_DEFAULT, H5P_DEFAULT);
144 if (err < 0)
throw std::runtime_error(
"Error in h5::group: Creating the softlink " + key +
" -> " + target_key +
" failed");
149 if (!
has_key(key))
throw std::runtime_error(
"Error in h5::group: " + key +
" does not exist in the group " +
name());
152 dataset ds = H5Dopen2(
id, key.c_str(), H5P_DEFAULT);
153 if (!ds.
is_valid())
throw std::runtime_error(
"Error in h5::group: Opening the dataset " + key +
" in the group " +
name() +
" failed");
162 dataset ds = H5Dcreate2(
id, key.c_str(), ty, sp, H5P_DEFAULT, pl, H5P_DEFAULT);
163 if (!ds.
is_valid())
throw std::runtime_error(
"Error in h5::group: Creating the dataset " + key +
" in the group " +
name() +
" failed");
172 herr_t get_group_elements_name_ds(
::hid_t loc_id,
const char *name,
const H5L_info_t *,
void *opdata) {
173 H5O_info_t object_info;
174 herr_t err = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);
175 if (err < 0)
throw std::runtime_error(
"Error in h5::get_group_elements_name_ds: H5Oget_info_by_name call failed");
176 if (object_info.type == H5O_TYPE_DATASET)
static_cast<std::vector<std::string> *
>(opdata)->push_back(name);
180 herr_t get_group_elements_name_grp(
::hid_t loc_id,
const char *name,
const H5L_info_t *,
void *opdata) {
181 H5O_info_t object_info;
182 herr_t err = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);
183 if (err < 0)
throw std::runtime_error(
"Error in h5::get_group_elements_name_grp: H5Oget_info_by_name call failed");
184 if (object_info.type == H5O_TYPE_GROUP)
static_cast<std::vector<std::string> *
>(opdata)->push_back(name);
188 herr_t get_group_elements_name_ds_grp(
::hid_t loc_id,
const char *name,
const H5L_info_t *,
void *opdata) {
189 H5O_info_t object_info;
190 herr_t err = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);
191 if (err < 0)
throw std::runtime_error(
"Error in h5::get_group_elements_name_ds_grp: H5Oget_info_by_name call failed");
192 if ((object_info.type == H5O_TYPE_GROUP) or (object_info.type == H5O_TYPE_DATASET))
193 static_cast<std::vector<std::string> *
>(opdata)->push_back(name);
200 std::vector<std::string> grp_name;
201 int r = H5Literate(
::hid_t(
id), H5_INDEX_NAME, H5_ITER_INC,
nullptr, get_group_elements_name_grp,
static_cast<void *
>(&grp_name));
202 if (r != 0)
throw std::runtime_error(
"Error in h5::group: Iterating over subgroups of the group " +
name() +
"failed");
207 std::vector<std::string> ds_name;
208 int r = H5Literate(
::hid_t(
id), H5_INDEX_NAME, H5_ITER_INC,
nullptr, get_group_elements_name_ds,
static_cast<void *
>(&ds_name));
209 if (r != 0)
throw std::runtime_error(
"Error in h5::group: Iterating over datasets of the group " +
name() +
"failed");
214 std::vector<std::string> ds_name;
215 int r = H5Literate(
::hid_t(
id), H5_INDEX_NAME, H5_ITER_INC,
nullptr, get_group_elements_name_ds_grp,
static_cast<void *
>(&ds_name));
216 if (r != 0)
throw std::runtime_error(
"Error in h5::group: Iterating over datasets and subgroups of the group " +
name() +
"failed");
228 std::string result{};
234 std::string result{};
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.
void write_attribute(std::string const &name, std::string const &s) const
Write a std::string HDF5 attribute with the given name to the 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::string read_attribute(std::string const &name) const
Read a string from an HDF5 attribute with the given name.
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).
std::string read_hdf5_format_from_key(std::string const &key) const
Read an hdf5_format tag from an HDF5 attribute with the name 'Format'.
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
object(hid_t id=0)
Construct a new h5::object for a given HDF5 ID by taking ownership, i.e. without increasing the refer...
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
object datatype
Type alias for an HDF5 datatype.
object dataset
Type alias for an HDF5 dataset.
object dataspace
Type alias for an HDF5 dataspace.
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
void h5_write_attribute(object, std::string const &, std::string const &)
Write a std::string to an HDF5 attribute.
int64_t hid_t
ID type used in HDF5.