Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Attributes on Datasets using HDF5 C++ api

Tags:

c++

hdf5

I'm using HDF5 C++ API in HDF5 1.8.7 and would like to use an H5::Attribute instance to set a couple of scalar attributes in an H5::DataSet instance, but cannot find any examples. It's pretty cut and dry using the C API:

/* Value of the scalar attribute */ 
int point = 1;                         

/*
 * Create scalar attribute for the dataset, my_dataset.
 */
aid2  = H5Screate(H5S_SCALAR);
attr2 = H5Acreate(my_dataset, "Integer attribute", H5T_NATIVE_INT, aid2,H5P_DEFAULT);

/*
 * Write scalar attribute to my_dataset.
 */
ret = H5Awrite(attr2, H5T_NATIVE_INT, &point); 

/*
 * Close attribute dataspace.
 */
ret = H5Sclose(aid2); 

/*
 * Close attribute.
 */
ret = H5Aclose(attr2); 

For some strange reason, the H5::Attribute and H5::DataSet classes in the C++ API seem to be missing the necessary methods. If anyone can come up with a concrete example using the C++ API, I'd be very appreciative.

like image 558
Marc Avatar asked May 13 '11 07:05

Marc


1 Answers

if you have a Dataset object ds...

adding a string attribute...

StrType str_type(0, H5T_VARIABLE);
DataSpace att_space(H5S_SCALAR);
Attribute att = ds.createAttribute( "myAttribute", str_type, att_space );
att.write( str_type, "myString" );

adding an int attribute...

IntType int_type(PredType::STD_I32LE);
DataSpace att_space(H5S_SCALAR);
Attribute att = ds.createAttribute(" myAttribute", int_type, att_space );
int data = 77;
att.write( int_type, &data );
like image 84
Sam Russell Avatar answered Sep 19 '22 06:09

Sam Russell