更新:在查看h5dump之后,结果发现它是一种复合数据格式。我想用python创建该死的文件是我的错!
来自h5dump的snippit:
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
答案 0 :(得分:1)
是的,您的问题是使用python来创建它。正如您所提到的,它已将其置于复合数据类型中。
你差不多......
首先,您需要创建一个结构来保存数据。让我们称之为pyf
(缺乏想象力的我 - 蟒蛇浮动)。
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;
}