使用Visual C ++将2-D数组int [n] [m]写入HDF5文件 [英] Writing 2-D array int[n][m] to HDF5 file using Visual C++

查看:184
本文介绍了使用Visual C ++将2-D数组int [n] [m]写入HDF5文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个2-d数组: data [] [] 传递到方法。方法如下:

  void WriteData(int data [48] [100],int sizes [48])

数据的大小实际上不是48 x 100,而是48 x [i]。也就是说每行可以有不同的长度!在一个简单的情况下,我处理,所有行是相同的大小(但不是100),所以你可以说数组是48 X sizes [0]。



如何最好地写这个HDF5?



我有一些工作代码,我循环从0到48,并为每一行创建一个新的数据集。



类似:

  for(int i = 0; i <48; i ++)
{
hsize_t dsSize [2];
dsSize [0] = 48;
dsSize [1] = sizes [0]; // use sizes [i] in most general case
//创建数据空间
DataSpace dataSpace = DataSpace(2,dsSize);
DataSet dataSet = group.createDataSet(dataSetName,intDataType,dataSpace);
dataSet.write(data [i],intDataType);
}

有没有办法在一个DataSet中同时写入数据?也许一个解决方案为所有行的更简单的情况相同的长度,另一个为粗糙的行?



我试过几个东西没有用。我调用dataSet.write(data,intDataType),即我把整个数组。我似乎在文件中得到垃圾,我怀疑,因为数组存储的数据实际上是48x100,我只需要一小部分。



我发现我可能可以使用double ptrs int **或vector>但我坚持。据我所知,写需要一个void * ptr。此外,我想让文件看起来正确。也就是说一个巨大的行与所有行的数据是不可取的,如果我必须走这条路线,有人会需要沟通一个光滑的方式来存储信息,这将允许我从文件读取数据(也许存储行长度作为属性?)。



也许我真正的问题是找到非平凡用例的C ++示例。



非常感谢。



Dave

解决方案

如果您的数据是向量向量,则使用可变长度数组这似乎对你的用例有意义):

  void WriteData(const std :: vector< std :: vector< int> ;>& data)
{
hsize_t dim(data.size());
H5 :: DataSpace dspace(1,& dim);
H5 :: VarLenType dtype(H5 :: PredType :: NATIVE_INT);
H5 :: DataSet dset(group.createDataSet(dataSetName,dtype,dspace));
hvl_t vl [dim];
for(hsize_t i = 0; i {
vl [i] .len = data [i] .size()
vl [i] .p =& data [i] [0];
}
dset.write(vl,dtype);
}


I'm just getting started with HDF5 and would appreciate some advice on the following.

I have a 2-d array: data[][] passed into a method. The method looks like:

void WriteData( int data[48][100], int sizes[48])

The size of the data is not actually 48 x 100 but rather 48 x sizes[i]. I.e. each row could be a different length! In one simple case I'm dealing with, all rows are the same size (but not 100), so you can say that the array is 48 X sizes[0].

How best to write this to HDF5?

I have some working code where I loop through 0 to 48 and create a new dataset for each row.

Something like:

for (int i = 0; i < 48; i++)
{
   hsize_t      dsSize[2];
   dsSize[0] = 48;
   dsSize[1] = sizes[0];  // use sizes[i] in most general case
   // Create the Data Space
   DataSpace dataSpace = DataSpace(2, dsSize);
   DataSet dataSet = group.createDataSet(dataSetName, intDataType, dataSpace);
   dataSet.write(data[i], intDataType);
}

Is there a way to write the data all at once in one DataSet? Perhaps one solution for the simpler case of all rows the same length, and another for the ragged rows?

I've tried a few things to no avail. I called dataSet.write(data, intDataType), i.e. I threw the whole array at it. I seemed to get garbage in the file, I suspect because the array the data is stored in is actually 48x100 and I only need a small part of that.

It occurred to me that I could maybe use double ptrs int** or vector> but I'm stuck on that. As far as I can tell, "write" need a void* ptr. Also, I'd like the file to "look correct". I.e. one giant row with all rows of data is not desirable, if I must go that route, someone would need to communicate a slick way to store the info that would allow me to read the data back in from file (perhaps store row lengths as attributes?).

Perhaps my real problem is finding C++ examples of non-trivial use cases.

Any help is much appreciated.

Dave

解决方案

Here is how you can do it using variable length arrays if your data is a vector of vectors (which seems to make sense for your use case):

void WriteData(const std::vector< std::vector<int> >& data)
{
    hsize_t dim(data.size());
    H5::DataSpace dspace(1, &dim);
    H5::VarLenType dtype(H5::PredType::NATIVE_INT);
    H5::DataSet dset(group.createDataSet(dataSetName, dtype, dspace));
    hvl_t vl[dim];
    for (hsize_t i = 0; i < dim; ++i)
    {
        vl[i].len = data[i].size();
        vl[i].p = &data[i][0];
    }
    dset.write(vl, dtype);
}

这篇关于使用Visual C ++将2-D数组int [n] [m]写入HDF5文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆