1

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.

ganesh reddy
  • 1,842
  • 7
  • 23
  • 38

1 Answers1

1

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(",")
Simon
  • 31,675
  • 9
  • 80
  • 92
  • Thanks Simon, appreciate your help! To your point I am running this in VStudio c++ and it builds and runs fine. – ganesh reddy May 09 '14 at 18:28
  • Note that you could make it one step nicer by writing the attribute as an array of strings instead of a single string with comma-separated values… but this is a bit simpler. – Simon May 09 '14 at 18:33
  • Yes but this works for my purpose, but yes thanks for the advise, I see what you are saying. – ganesh reddy May 09 '14 at 18:35