【问题标题】:How to modify attributes of an existing object with the HDF5 lib?如何使用 HDF5 库修改现有对象的属性?
【发布时间】:2021-06-18 23:30:58
【问题描述】:

我正在尝试构建一个利用 HDF5 库的 C++ 应用程序。对于我的用例,我有一个初始化阶段,在该阶段我为文件创建结构(组、属性)并给它们一个默认值。稍后,我试图回到属性并修改它们。我目前的尝试如下:

#include <iostream>
#include <cstdint>
#include <string>
#include "H5Cpp.h"

const H5std_string FILE_NAME( "test.h5" );

int main (void)
{
  /*
   * Create a new file. If file exists its contents will be overwritten.
   */
  H5::H5File file(FILE_NAME, H5F_ACC_TRUNC);

  /*
   * Create Groups and Attributes
   */
  H5::Group g_data(file.createGroup( "/Data" ));
  H5::DataSpace dspace = H5::DataSpace(H5S_SCALAR);
  H5::Attribute version = g_data.createAttribute("version", H5::PredType::NATIVE_INT, dspace);
  int curVersion = {1};
  version.write(H5::PredType::NATIVE_INT, &curVersion);
  version.close();
  dspace.close();
  g_data.close();

  // WORK WITH DATASETS

  H5::Attribute new_version = file.openAttribute("/Data/version");
  int newVersion = {2};
  new_version.write(H5::PredType::NATIVE_INT, &newVersion);
  new_version.close();

  file.close();

  return 0;
}

但是,执行此操作时会出错:

HDF5-DIAG: Error detected in HDF5 (1.10.0-patch1) thread 140574792701760:
  #000: ../../../src/H5A.c line 438 in H5Aopen(): unable to load attribute info from object header for attribute: '/Data/version'
    major: Attribute
    minor: Unable to initialize object
  #001: ../../../src/H5Oattribute.c line 530 in H5O_attr_open_by_name(): can't locate attribute: '/Data/version'
    major: Attribute
    minor: Object not found
terminate called after throwing an instance of 'H5::AttributeIException'

这可能是因为我不能使用文件根目录的绝对路径来访问属性。修改时组对象已经关闭,但我想访问它们的属性。

有人知道这是如何正确完成的吗?

【问题讨论】:

    标签: c++ attributes hdf5


    【解决方案1】:

    不确定如何解决您正在使用的 HDF5 库的问题,但使用 C++ 中的 HDFql 可以按如下方式解决:

    // create HDF5 file 'test.h5' and use (i.e. open) it
    HDFql::execute("create and use file test.h5");
    
    // create group 'Data'
    HDFql::execute("create group Data");
    
    // create attribute 'version' (in group 'Data') of data type int with an initial value of '1'
    HDFql::execute("create attribute Data/version as int values(1)");
    
    // insert (i.e. write) value '2' into attribute 'version'
    HDFql::execute("insert into Data/version values(2)");
    

    可以在 HDFql reference manualexamples 中找到更多信息,说明如何使用它。

    【讨论】:

    • 感谢您的建议,我一定会调查并尝试使用 HDFql 库。我不会结束这个问题,以防有人用 HDF5 库解决了这个问题。
    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多