Roka545
Roka545

Reputation: 3636

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"));

Upvotes: 0

Views: 1354

Answers (1)

dee-kay
dee-kay

Reputation: 76

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.

Upvotes: 1

Related Questions