I am porting an existing C++ application to GO as part of an evaluation project. As part of this I need to read two Dataset attributes which in some files are stored as a double and in some as a float. The C++ code I use to handle this looks like the following (we are using the libhdf5-cpp-100 on Debian Linux).
const auto att = dataSet.openAttribute(attributeName);
if (att.getDataType() == H5::PredType::NATIVE_DOUBLE) {
att.read(att.getDataType(), &attributeValue);
}
else if (att.getDataType() == H5::PredType::NATIVE_FLOAT) {
float temp = 0.F;
att.read(att.getDataType(), &temp);
attributeValue = static_cast<double>(temp);
}
else {
// we throw an exception indicating we don't support the type
}
My problem is that I'm having trouble writing the equivalent in GO. (I'm using the package "gonum.org/v1/hdf5".) The read method seems simple enough:
func (s *Attribute) Read(data interface{}, dtype *Datatype) error
But I'm struggling to determine what to pass as the Datatype as the Attribute type does not seem to have a GetDataType method. The closest I see is the following:
func (s *Attribute) GetType() Identifier
But that doesn't return a Datatype, it returns an Identifier. I tried the following comparison on the assumption that given the Identifier I could determine the data type:
if attr.GetType().ID() == hdf5.T_NATIVE_DOUBLE.ID() {
// handle as a double
}
but that doesn't work. The ID returned from GetType() is not the same as the ID for either the double or the float.
I've been through the online docs at https://godoc.org/gonum.org/v1/hdf5
but have been unable to find the solution to my problem. (Or any example of reading HDF5 attributes using GO.)
Has anyone managed to do something like this? Or are most examples just assuming the type rather than querying it?