使用C API在嵌套for循环内创建HDF5组失败

时间:2018-10-26 16:31:10

标签: hdf5

我正在尝试在嵌套的for循环中通过HDF5Gcreate调用在HDF5文件中创建组:

#include "hdf5.h"
#include <string> 
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<string> firstIDs =  {"A", "B", "C"}; 
    vector<string> secondIDs =  {"1", "2", "3"}; 

    hid_t file_id; 
    file_id = H5Fcreate ("RBF_RANDOM_POINTS.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    for (const auto& firstID : firstIDs)
    {
        for (const auto& secondID : secondIDs)
        {
            // Create the HDF5 group for each RBF and random test size. 
            std::string groupName {
                "/FIRST/" + firstID + "/SECOND/" + secondID 
            };
            hid_t group_id; 
            cout << groupName << endl;
            group_id = H5Gcreate(file_id, groupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
            H5Gclose (group_id);
        }
    }

    H5Fclose(file_id); 
};

这是我得到的错误:

/FIRST/A/SECOND/1
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:
  #000: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5G.c line 323 in H5Gcreate2(): unable to create group
    major: Symbol table
    minor: Unable to initialize object
  #001: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5Gint.c line 157 in H5G__create_named(): unable to create and link to group
    major: Symbol table
    minor: Unable to initialize object
  #002: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5L.c line 1572 in H5L_link_object(): unable to create new link to object
    major: Links
    minor: Unable to initialize object
  #003: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5L.c line 1813 in H5L__create_real(): can't insert link
    major: Links
    minor: Unable to insert object
  #004: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5Gtraverse.c line 851 in H5G_traverse(): internal path traversal failed
    major: Symbol table
    minor: Object not found
  #005: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5Gtraverse.c line 741 in H5G__traverse_real(): component not found
    major: Symbol table
    minor: Object not found
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:
  #000: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5G.c line 682 in H5Gclose(): not a group
    major: Invalid arguments to routine
    minor: Inappropriate type
/FIRST/A/SECOND/2
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:

发生了什么事? for循环外的单个调用工作正常。

1 个答案:

答案 0 :(得分:1)

从HDF5用户指南:组

  

HDF5文件的结构构成文件中对象的名称空间。路径名称是一串由斜杠(/)分隔的组件。每个组件都是硬链接或软链接的名称,该链接指向文件中的对象。斜线不仅分隔了组件,还指示了它们的层次关系。用斜杠后面的链接名表示的组件始终是该斜杠前面的链接名所表示的组件的成员。

您正在尝试创建不存在的参考组的组,这就是为什么错误指出无法找到对象的原因。您可以通过确保group_id不是-1来验证是否已成功创建组。您需要先在层次结构中创建更高的组,然后再在其下面创建某些内容,如下面的更改。

int main(int argc, char* argv[])
{
  vector<string> firstIDs = { "A", "B", "C" };
  vector<string> secondIDs = { "1", "2", "3" };
  hid_t file_id = H5Fcreate("RBF_RANDOM_POINTS.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  hid_t group_id = H5Gcreate(file_id, "/FIRST", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  H5Gclose(group_id);
  for (const auto& firstID : firstIDs)
  {
    std::string parent{
      "/FIRST/" + firstID
    };
    std::string child1{
      "/FIRST/" + firstID + "/SECOND"
    };
    group_id = H5Gcreate(file_id, parent.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    H5Gclose(group_id);
    group_id = H5Gcreate(file_id, child1.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    H5Gclose(group_id);
    for (const auto& secondID : secondIDs)
    {
      // Create the HDF5 group for each RBF and random test size. 
      std::string whatYouWanted{
        "/FIRST/" + firstID + "/SECOND/" + secondID
      };
      cout << whatYouWanted << endl;
      group_id = H5Gcreate(file_id, whatYouWanted.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
      // if(-1 == group_id) something went wrong
      H5Gclose(group_id);
    }
  }
  H5Fclose(file_id);
}