HDF5 - Write/append HDF5 复合数据类型与 Hyperslab 一一对应?

HDF5 - Write/append HDF5 Compound Datatype one by one with Hyperslab?

我正在尝试使用 C++ API 将我的复合数据类型一一写入我的文件中。我在 Win64 上使用 HDF5-1.8.18。

我想一个一个地写我的复合数据类型对象。我有几种复合类型,它们不在 "stacked" 缓冲区中,它们只是 "arrive" 一个接一个。当我尝试写入 selected Hyperslab 时,我的代码失败了。

我希望我的 Hyperslab 一次 select 正好是数据集中复合数据类型的一个元素。在这段测试代码中,我一直想写入索引 3。显然,稍后我会从静态索引切换到计数器变量。

我的数据空间是一维的:

我用来写入数据的代码:

H5::CompType compType = group->openCompType("tTestData");

H5::DataSpace fspace1 = dataset->getSpace();
hsize_t start[]  = {3};
hsize_t stride[] = {1};
hsize_t count[]  = {1};
hsize_t block[]  = {1};

fspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block );
dataset->write(&myTestData, compType, fspace1);

抛出的异常是:

HDF5-DIAG: Error detected in HDF5 (1.8.18) thread 0:
  #000: ..\..\hdf5-1.8.18\src\H5Dio.c line 271 in H5Dwrite(): can't prepare for
writing data
    major: Dataset
    minor: Write failed
  #001: ..\..\hdf5-1.8.18\src\H5Dio.c line 352 in H5D__pre_write(): can't write
data
    major: Dataset
    minor: Write failed
  #002: ..\..\hdf5-1.8.18\src\H5Dio.c line 697 in H5D__write(): src and dest dat
a spaces have different sizes
    major: Invalid arguments to routine
    minor: Bad value

我需要如何调整我的 Hyperslab 才能一次写入一个复合数据类型对象?似乎我需要调整传递给 selectHyperslab 的变量,但我不确定要传递哪些参数?

同时我找到了解决方法:

//h5 is a custom Adapter class, which allows me to reuse groups, datasets etc. 
//h5 also keeps track of the count of written data objects in a dataset (used below)  
//These functions are not included in HDF5. Just encapsulate the complexity of the HDF5 lib
H5::Group* group = h5->GetGroupPtr();
H5::CompType compType = group->openCompType("someCompType");    
H5::DataSet* dataset = h5->GetDataSet("someDataSet");


//get dataspace of our dataset
H5::DataSpace fspace1 = dataset->getSpace();

//define a 1x1 (one data row) dataspace
hsize_t dim[] = {1};
H5::DataSpace space( 1, dim );

//get current data offset (index) and set stride, count, block to 1
tInt64 currentCounter = h5->GetDataSetCounter("someDataSet");
hsize_t start[]  = {currentCounter};
hsize_t stride[] = {1};
hsize_t count[]  = {1};
hsize_t block[]  = {1};

//select the 1x1 hyperslab 
fspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block );
dataset->write(&data, compType, space, fspace1);

//clean up used resources
fspace1.close();
space.close();

有关详细信息,请参阅 Hyperslab documentation