Question

I am trying to write a comma separated string attribute to an HDF5 dataset. I create the data set using,

dset = H5Dcreate(file, "dset1", H5T_NATIVE_DOUBLE, file_space, H5P_DEFAULT, plist, H5P_DEFAULT);

The data is basically columnar, it has fields such as

Timestamp  Prop1   Prop2

Now I know that this is a hack, but it suffices for my purpose to somehow tag the dset with a string like "TimeStamp, Prop1, Prop2". I am looking to read the HDF5 file back in python and can easily read the string. I think one can use the H5AWrite method for this. But I am not sure if we can write strings with it, My question is

1) How to use the method to write the comma separated attribute

2) How to read it back while opening the file in Python.

I wasnt able to find any examples to do it in C++. Any pointers will be appreciated.

Was it helpful?

Solution

Here is how to write the attribute as a string like column1,column2,column3 (using the C API since you seem to be using it despite asking for C++):

hid_t atype = H5Tcopy(H5T_C_S1);
H5Tset_size(atype, H5T_VARIABLE);
hid_t attr = H5Acreate(dset, "columns", atype, H5S_SCALAR, H5P_DEFAULT);
H5Awrite(attr, atype, "column1,column2,column3");
H5Aclose(attr);

Then to read it in Python:

import h5py 
file =  h5py.File("my_file.h5", "r")
dset = file["/my_dset"]
columns = dset.attrs["columns"].split(",")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top