dongya6381 2019-02-21 15:07
浏览 376
已采纳

如何使用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?

  • 写回答

1条回答 默认 最新

  • dsceme82487 2019-02-21 21:30
    关注

    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...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。