我有一个小问题将线性数组从 c++ 保存到 hdf5 文件中的三维数据集。
内存中的布局如下所示:
|---+---+---+---+---+---+---+---+---+---+----+----|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---+---+---+---+---+---+---+---+---+---+----+----|
虽然它被程序这样解释:
x
|---+---+---|
| 0 | 1 | 2 |
z=0 |---+---+---| y
| 3 | 4 | 5 |
|---+---+---|
x
|---+----+----|
| 6 | 7 | 8 |
z=1 |---+----+----| y
| 9 | 10 | 11 |
|---+----+----|
使用以下代码,将此数组保存到 HDF5 文件中的三维数据集
std::vector<int> buffer;
for(int i = 0; i < 12; ++i)
buffer.push_back(i);
hsize_t dims[3] = {2,3,2};
hisze_t mem_dim[1] = {12};
hid_t file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
hid_t space = H5Screate_simple (3, dims, NULL);
hid_t mem_space = H5Screate_simple(1,mem_dim,NULL);
hid_t dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT,
H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Dwrite (dset, H5T_NATIVE_INT, mem_space, space, H5P_DEFAULT,
&buffer[0]);
这导致这样的布局:
x
|---+---+----|
| 0 | 2 | 4 |
z=0|---+---+----| y
| 6 | 8 | 10 |
|---+---+----|
x
|---+----+----|
| 1 | 3 | 5 |
z=1|---+----+----| y
| 7 | 9 | 11 |
|---+----+----|
我猜这是因为行主要格式(z 是变化最快的索引)。无论如何,是否可以仅通过一次 H5DWrite 调用来强制 hdf5 写入预期格式?
我想出了这个想法,但它不起作用。我想我弄错了hyperslaps的功能。
... //Same as above, but before H5DWrite
hsize_t start[3];
hsize_t stride[3];
hsize_t count[3];
hsize_t block[3];
//Select two blocks from array. First for z=0, second for z=1
start[0] = 0;
stride[0] = 6;
count[0] = 2;
block[0] = 6;
status = H5Sselect_hyperslab (mem_space, H5S_SELECT_SET, start, stride, count,
block);
start[0] = 0;
start[1] = 0;
start[2] = 0;
stride[0] = 2;
stride[1] = 3;
stride[2] = 1;
count[0] = 1;
count[1] = 1;
count[2] = 2;
block[0] = 2;
block[1] = 3;
block[2] = 1;
status = H5Sselect_hyperslab (space, H5S_SELECT_SET, start, stride, count,
block);
status = H5Dwrite (dset, H5T_NATIVE_INT, mem_space, space, H5P_DEFAULT,
&buffer[0]);
在我对 H5Sselect_hyperslab 的解释中,为 mem_space 定义的两个块映射到文件空间的两个块。但实际上结果与上述相同。是否可以在不重新格式化数组且不循环调用 H5DWrite 的情况下实现预期的行为?
我