HDF5 - 使用Hyperslab逐个写入/附加HDF5复合数据类型?

时间:2017-05-04 10:36:43

标签: c++ hdf5

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

我想逐个编写复合数据类型对象。我有几种复合类型,它们没有进入“堆叠”缓冲区,它们只是逐个“到达”。当我尝试写入选定的Hyperslab时,我的代码失败了。

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

我的数据空间是一维的:

  • Rank = 1
  • dim =数据对象的数量(例如:213)

我用来编写数据的代码:

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的变量,但我不确定要传递哪些参数?

1 个答案:

答案 0 :(得分:0)

在此期间我找到了解决方案:

//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