【问题标题】:How can i extract a specific field of a compund datatype as an array如何将复合数据类型的特定字段提取为数组
【发布时间】:2019-07-01 13:33:39
【问题描述】:

我有一个 HDF5 文件,其中包含(以及其他元素)一组复合数据,例如:

    DATASET "AgentDataSet" {
        DATATYPE  H5T_COMPOUND {
           H5T_STD_I32LE "LifeState";
           H5T_STD_I32LE "CellIdx";
           H5T_STD_I32LE "CellID";
           H5T_STD_I64LE "AgentID";
           H5T_IEEE_F32LE "BirthTime";
           H5T_STD_U8LE "Gender";
           H5T_IEEE_F32LE "Age";
           H5T_IEEE_F32LE "LastBirth";
        }
        DATASPACE  SIMPLE { ( 2252984 ) / ( 2252984 ) }
}

复合数据的成员可能因文件而异,但我知道 LifeStateCellIdxCellIDAgentID 始终包含在复合数据类型中(即使在相同的位置)。

我的应用程序不知道复合数据的精确结构,因此无法定义适当的struct 以在H5Tread() 中使用。

有没有办法将字段AgentIDCellID从复合数据数组中提取到一个数组中

struct {
  int iAgendID;
  int iCellID;   
}

即忽略其余字段?

谢谢

【问题讨论】:

    标签: c++ c hdf5


    【解决方案1】:

    我发现了如何做到这一点: 我定义了这个结构:

    struct info_t {
        idtype   m_ulID;
        gridtype m_ulCellID;
    };
    

    然后我创建一个对应的HDF5数据类型:

        hid_t hInfoDataType = H5Tcreate (H5T_COMPOUND, sizeof (info_t));
    
        info_t ii;
        H5Tinsert(hInfoDataType, SPOP_DT_CELL_ID,  HOFFSET(info_t, m_ulCellID), H5T_NATIVE_INT);
        H5Tinsert(hInfoDataType, SPOP_DT_AGENT_ID, HOFFSET(info_t, m_ulID),     INT_NATIVE_LONG);
    

    然后,假设适当的数据空间(hDataSpace)已经打开,我读取并使用数据:

        hsize_t dims;
        H5Sget_simple_extent_dims(hDataSpace, &dims, NULL);
        info_t *pInfos = new info_t[dims];
    
        hid_t hMemSpace = H5Screate_simple (1, &dims, NULL); 
        herr_t status = H5Dread(hDataSet, hAgentDataType, hMemSpace, hDataSpace, H5P_DEFAULT, pInfos);
    
        for (int i = 0; i < 10; i++) {
            printf("%d; a %ld, c %d\n", i, pInfos[i].m_ulID, pInfos[i].m_ulCellID);
        }
        delete[] pInfos;
    

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 2019-05-30
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多