Add open_object, close_object functions in HDF5 interface

This commit is contained in:
Paul Romano 2021-10-13 07:27:56 -05:00
parent d024a3e008
commit 8bb2002d8f
3 changed files with 22 additions and 8 deletions

View file

@ -61,6 +61,8 @@ void ensure_exists(hid_t obj_id, const char* name, bool attribute = false);
vector<std::string> group_names(hid_t group_id);
vector<hsize_t> object_shape(hid_t obj_id);
std::string object_name(hid_t obj_id);
hid_t open_object(hid_t group_id, const std::string& name);
void close_object(hid_t obj_id);
//==============================================================================
// Fortran compatibility functions

View file

@ -90,23 +90,23 @@ bool is_inelastic_scatter(int mt)
unique_ptr<Function1D> read_function(hid_t group, const char* name)
{
hid_t dset = open_dataset(group, name);
hid_t obj_id = open_object(group, name);
std::string func_type;
read_attribute(dset, "type", func_type);
read_attribute(obj_id, "type", func_type);
unique_ptr<Function1D> func;
if (func_type == "Tabulated1D") {
func = make_unique<Tabulated1D>(dset);
func = make_unique<Tabulated1D>(obj_id);
} else if (func_type == "Polynomial") {
func = make_unique<Polynomial>(dset);
func = make_unique<Polynomial>(obj_id);
} else if (func_type == "CoherentElastic") {
func = make_unique<CoherentElasticXS>(dset);
func = make_unique<CoherentElasticXS>(obj_id);
} else if (func_type == "IncoherentElastic") {
func = make_unique<IncoherentElasticXS>(dset);
func = make_unique<IncoherentElasticXS>(obj_id);
} else {
throw std::runtime_error {"Unknown function type " + func_type +
" for dataset " + object_name(dset)};
" for dataset " + object_name(obj_id)};
}
close_dataset(dset);
close_object(obj_id);
return func;
}

View file

@ -118,6 +118,12 @@ void close_group(hid_t group_id)
fatal_error("Failed to close group");
}
void close_object(hid_t obj_id)
{
if (H5Oclose(obj_id) < 0)
fatal_error("Failed to close object");
}
int dataset_ndims(hid_t dset)
{
hid_t dspace = H5Dget_space(dset);
@ -394,6 +400,12 @@ hid_t open_group(hid_t group_id, const char* name)
return H5Gopen(group_id, name, H5P_DEFAULT);
}
hid_t open_object(hid_t group_id, const std::string& name)
{
ensure_exists(group_id, name.c_str());
return H5Oopen(group_id, name.c_str(), H5P_DEFAULT);
}
void read_attr(hid_t obj_id, const char* name, hid_t mem_type_id, void* buffer)
{
hid_t attr = H5Aopen(obj_id, name, H5P_DEFAULT);