HDF5 C ++接口:编写动态2D数组 [英] HDF5 C++ interface: writing dynamic 2D arrays
问题描述
我正在使用 HDF5 C ++ API 编写2D数组数据集文件.HDF组具有要创建的示例来自静态定义的数组大小的HDF5文件,我已对其进行了修改以适应下面的需求.但是,我需要一个动态数组,其中 NX
和 NY
都是在运行时确定的.我找到了使用"new"关键字以帮助创建动态数组.这是我所拥有的:
I am using the HDF5 C++ API to write 2D array dataset files. The HDF Group has an example to create a HDF5 file from a statically defined array size, which I've modified to suite my needs below. However, I require a dynamic array, where both NX
and NY
are determined at runtime. I've found another solution to create 2D arrays using the "new" keyword to help create a dynamic array. Here is what I have:
#include "StdAfx.h"
#include "H5Cpp.h"
using namespace H5;
const H5std_string FILE_NAME("C:\\SDS.h5");
const H5std_string DATASET_NAME("FloatArray");
const int NX = 5; // dataset dimensions
const int NY = 6;
int main (void)
{
// Create a 2D array using "new" method
double **data = new double*[NX];
for (int j = 0; j < NX; j++) // 0 1 2 3 4 5
{ // 1 2 3 4 5 6
data[j] = new double[NY]; // 2 3 4 5 6 7
for (int i = 0; i < NY; i++) // 3 4 5 6 7 8
data[j][i] = (float)(i + j); // 4 5 6 7 8 9
}
// Create HDF5 file and dataset
H5File file(FILE_NAME, H5F_ACC_TRUNC);
hsize_t dimsf[2] = {NX, NY};
DataSpace dataspace(2, dimsf);
DataSet dataset = file.createDataSet(DATASET_NAME, PredType::NATIVE_DOUBLE,
dataspace);
// Attempt to write data to HDF5 file
dataset.write(data, PredType::NATIVE_DOUBLE);
// Clean up
for(int j = 0; j < NX; j++)
delete [] data[j];
delete [] data;
return 0;
}
The resulting file, however, is not as expected (output from hdf5dump
):
HDF5 "SDS.h5" {
GROUP "/" {
DATASET "FloatArray" {
DATATYPE H5T_IEEE_F64LE
DATASPACE SIMPLE { ( 5, 6 ) / ( 5, 6 ) }
DATA {
(0,0): 4.76465e-307, 4.76541e-307, -7.84591e+298, -2.53017e-098, 0,
(0,5): 3.8981e-308,
(1,0): 4.76454e-307, 0, 2.122e-314, -7.84591e+298, 0, 1,
(2,0): 2, 3, 4, 5, -2.53017e-098, -2.65698e+303,
(3,0): 0, 3.89814e-308, 4.76492e-307, 0, 2.122e-314, -7.84591e+298,
(4,0): 1, 2, 3, 4, 5, 6
}
}
}
}
问题出在2D数组的创建方式上(因为此示例在静态数组方法下工作正常).据我从此电子邮件线程了解:
The problem stems back to how the 2D array was created (since this example works fine with a static array method). As I understand from this email thread:
HDF5库期望包含连续的元素数组,而不是指向较小尺寸元素的指针
The HDF5 library expects to a contiguous array of elements, not pointers to elements in lower dimensions
由于我是C ++/HDF5的新手,所以我不确定如何在运行时创建动态大小的数组,该数组是元素的连续数组.我不想执行电子邮件线程中描述的更复杂的"hyperslab"方法,因为这看起来过于复杂.感谢您的帮助.
As I am rather new to C++/HDF5, I'm not sure how to create a dynamically sized array at runtime that is a contiguous array of elements. I do not want to do the more complicated "hyperslab" method described in the email thread, as this looks overly complicated. Any help is appreciated.
推荐答案
好吧,我对HDF5一无所知,但是可以使用大小为 code的一维数组来模拟C ++中具有连续缓冲区的动态2D数组.> NX * NY
.例如:
Well, I don't know anything about HDF5, but dynamic 2D arrays in C++ with a contiguous buffer can be simulated by using a 1D array of size NX * NY
. For example:
分配:
double *data = new double[NX*NY];
元素访问权限:
data[j*NY + i]
(而不是 data [j] [i]
)
这篇关于HDF5 C ++接口:编写动态2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!