Roka545 February 2016

HDF5 - Creating attributes for groups via code

I'm currently learning HDF5 and need to figure out how to add attributes to an empty group with C++. I know how to do it via HDFView, but I can't seem to find any documentation about adding attributes to a simple group, only datasets (is this even possible?).

In HDFView, if you add a group then right-click that group and select "Show properties", a new window will pop up with tabs for 'General' and 'Attributes'. In the 'Attributes' tab you can add multiple attributes. I basically want to do this, but through code.

My code below adds a single group to a new H5 file:

//Create a new file using default properties
H5File file("NewH5.h5", H5F_ACC_TRUNC);

//Create PLATFORM_t and SONAR_t groups in the file
Group groupPlatform(file.createGroup("/PLATFORM_t"));

Answers


dee-kay February 2016

The operations on attributes attached to files, groups, datasets etc. are the member functions of the H5Location class. In order to write an attribute named "some_attribute" of type double in your group you simply call

double value=42;
DataSpace dspace(H5S_SCALAR);
Attribute att = groupPlatform.createAttribute("some_attribute",PredType::NATIVE_DOUBLE,dspace);
att.write(PredType::NATIVE_DOUBLE,&value);

More complex attributes are possible if you use more complex dataspaces.

Post Status

Asked in February 2016
Viewed 1,315 times
Voted 7
Answered 1 times

Search




Leave an answer