如何使用GO语言读取HDF5属性,该属性可能是两种不同数据类型之一?

如何使用GO语言读取HDF5属性,该属性可能是两种不同数据类型之一?

问题描述:

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?

I have confirmed my suspicions and now have a proper answer. The essential problem is that there was an error in my use of the C++ API (which would have led to only writing 1/2 of a double in certain cases) and I was essentially trying to repeat that error in GO. In fact, the solution is very simple.

The attribute type that is passed into the attribute read method, is not the type of the attribute, it is the type that you want it converted to when stored in memory. That means that my C++ code should be much simpler as there is no need to check the attribute type, nor to static_cast it to the result. To read and store the attribute value, relying on HDF5 to perform the conversion and for a suitable exception to be thrown if the attribute is not convertible to a double, is as simple as

const auto att = dataSet.openAttribute("my attribute name");
att.read(H5::PredType::NATIVE_DOUBLE, &attributeValue);

The GO version is more work, since we have to manage the object life cycle and error conditions manually, but here it is. (Note that I'm assuming "...handle the error..." also involves an early exit, otherwise an additional if statement is needed to check that att is not nil.)

att, err := dataSet.OpenAttribute("my attribute name")
if err != nil {
    ...handle the error...
}

err = att.Read(&attributeValue, hdf5.T_NATIVE_DOUBLE)
if err != nil {
    ...handle the error...
}

err = att.Close()
if err != nil {
    ...handle the error...
}