更新:查看h5dump后,事实证明这是一种复合数据格式。我使用python创建该死的文件的错!
来自h5dump的代码段:
DATASET "dset1" {
DATATYPE H5T_COMPOUND {
16-bit little-endian floating-point "value";
我有一堆HDF5文件,可以在Matlab中读写,但是现在我想使用C ++。在遵循以下示例之后,我仍然无法破解它。
从运行时遇到的错误来看,我认为我没有正确理解或正确实现内存类型。您如何知道要使用哪种内存类型?
下面是我的代码,以及在运行时遇到的错误。它在dset.read行失败。(我在这里尝试了各种实现/内存类型)。
HDFView在Windows 64位计算机上报告的数据集属性是32位浮点。该代码在Mint 17 64bit上运行。
编译使用:
g++ importH5.cpp -o importH5 -std=c++11 -lhdf5_cpp -lhdf5
ImportH5.cpp:
#include <iostream>
#include <string>
#include <vector>
#include <H5Cpp.h>
using namespace std;
using namespace H5;
int main()
{
string ifn = "test.h5";
string datasetPath = "/grp1/grp2/grp3/dset1";
// Open HDF5 file handle, read only
H5File fp(ifn.c_str(),H5F_ACC_RDONLY);
// access the required dataset by path name
DataSet dset = fp.openDataSet(datasetPath.c_str());
// get the dataspace
DataSpace dspace = dset.getSpace();
// get the dataset type class
H5T_class_t type_class = dset.getTypeClass();
// According to HDFView, this is a 32-bit floating-point
// get the size of the dataset
hsize_t rank;
hsize_t dims[2];
rank = dspace.getSimpleExtentDims(dims, NULL); // rank = 1
cout<<"Datasize: "<<dims[0]<<endl; // this is the correct number of values
// Define the memory dataspace
hsize_t dimsm[1];
dimsm[0] = dims[0];
DataSpace memspace (1,dimsm);
// create a vector the same size as the dataset
vector<float> data;
data.resize(dims[0]);
cout<<"Vectsize: "<<data.size()<<endl;
float data_out[65341];
for (int i=0;i<65341;i++)
{
data_out[i]=0;
}
// pass pointer to the array (or vector) to read function, along with the data type and space.
dset.read(data_out, PredType::NATIVE_FLOAT, memspace, dspace); // FAILS
dset.read(data_out, PredType::NATIVE_FLOAT, dspace); // FAILS
dset.read(data.data(), PredType::NATIVE_FLOAT, memspace, dspace); // FAILS
// close the HDF5 file
fp.close();
return 0;
}
错误:
Datasize: 65341
Vectsize: 65341
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 139863993304960:
#000: ../../../src/H5Dio.c line 182 in H5Dread(): can't read data
major: Dataset
minor: Read failed
#001: ../../../src/H5Dio.c line 438 in H5D__read(): unable to set up type info
major: Dataset
minor: Unable to initialize object
#002: ../../../src/H5Dio.c line 939 in H5D__typeinfo_init(): unable to convert between src and dest datatype
major: Dataset
minor: Feature is unsupported
#003: ../../../src/H5T.c line 4525 in H5T_path_find(): no appropriate function for conversion path
major: Datatype
minor: Unable to initialize object
terminate called after throwing an instance of 'H5::DataSetIException'
Aborted
如果使用的话,这是指向HDF5文件的链接:https : //dl.dropboxusercontent.com/u/63051/test.h5
是的,您的问题是使用python创建它。正如您所提到的,它已将其放入复合数据类型中。
你快到了...
首先,您需要创建一个结构来保存数据。让我们称呼它吧pyf
(我想像不到-python float)。
struct pyf {
float x;
};
然后,您需要将其更改data_out
为这种类型。
struct pyf data_out[65341];
for (int i=0;i<65341;i++)
{
data_out[i].x=0;
}
最后,您需要创建一个HDF5复合类型,并告诉它要从文件中获取什么成员:
CompType mtype( sizeof(struct pyf) );
mtype.insertMember("value", HOFFSET(struct pyf, x), PredType::NATIVE_FLOAT);
dset.read(data_out, mtype);
最后进行健全性检查:
for (int i=0;i<65341;i++)
{
cout<<data_out[i].x << " ";
cout << endl;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句