我正在寻找使用C ++从hdf5文件进行简单读取的方法。我将其分为4部分。 1文件的外观。第二个我的代码试图读取文件。第三错误消息。我的结论是第四。
1.File-可以在文件中找到数据集,如下所示:
$ h5ls -r myfile.h5
/ Group
/mydata Dataset {1200}
注意-数据集是1200个字符串的数组。注意,CTYPE是H5T_C_S1
,这是我将用来读取它的内容。
HDF5 "myfile.h5" {
GROUP "/" {
DATASET "mydata" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 1200 ) / ( 1200 ) }
DATA {
(0): "pxsntpfcnkeesswwpwopksu", "exsytafcbkecsswwpwopnng",
(2): "ebswtlfcbnecsswwpwopnnm", "pxywtpfcnneesswwpwopksu",
(4): "exsgfnfwbktesswwpwoenag", "exyytafcbnecsswwpwopkng",
2.The Code-我的代码尝试像这样读取数据集:
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME("myfile.h5");
const H5std_string DATASET_NAME("mydata");
// open file
H5File file(FILE_NAME, H5F_ACC_RDONLY);
// get dataset
DataSet dataset = file.openDataSet(DATASET_NAME);
// get src dataspace
DataSpace src = dataset.getSpace();
// get dimensions
int NUM_DIMS = src.getSimpleExtentNdims();
std::vector<hsize_t> dims(NUM_DIMS);
src.getSimpleExtentDims(&dims[0]);
hsize_t height = dims[0];
hsize_t width = 23;
// define src hyperslab
std::vector<hsize_t> count(NUM_DIMS, 1);
std::vector<hsize_t> offset(NUM_DIMS, 0);
src.selectHyperslab(H5S_SELECT_SET, &count[0], &offset[0]);
// define dst hyperslab
DataSpace dst(NUM_DIMS, dims);
dst.selectHyperslab(H5S_SELECT_SET, &count[0], &offset[0]);
// read data into memory, array of cstrings
std::vector<char*> data_out(height);
dataset.read(&data_out[0], H5T_C_S1, dst, src);
// print first line
std::cout << data_out[0] << std::endl;
3。错误-但是,即使我设计了{{1,但在src
和dst
超级平板之间似乎是类型不匹配,它仍然失败 }}和src
具有相同的尺寸。错误消息如下:
dst
4。我的结论-我尝试了许多变体,包括删除HDF5-DIAG: Error detected in HDF5 (1.10.3) thread 0:
#000: H5Dio.c line 199 in H5Dread(): can't read data
major: Dataset
minor: Read failed
#001: H5Dio.c line 467 in H5D__read(): unable to set up type info
major: Dataset
minor: Unable to initialize object
#002: H5Dio.c line 993 in H5D__typeinfo_init(): unable to convert between src and dest datatype
major: Dataset
minor: Feature is unsupported
#003: H5T.c line 4546 in H5T_path_find(): can't find datatype conversion path
major: Datatype
minor: Can't get value
#004: H5T.c line 4762 in H5T__path_find_real(): no appropriate function for conversion path
major: Datatype
minor: Unable to initialize object
HDF5-DIAG: Error detected in HDF5 (1.10.3) thread 0:
#000: H5T.c line 1756 in H5Tclose(): immutable datatype
major: Invalid arguments to routine
minor: Bad value
DataType::~DataType - H5Tclose failed
和dst
作为src
的参数,将dataset.read()
更改为H5T_C_S1
和{ {1}},但是相同的错误仍然存在。
如何简单地将数据集读入内存?数据类型确实不匹配吗?还是我没有定义其他内容?我在读取功能中仍然使用错误的数据类型吗?我是否对我的超级平板进行了不正确的定义,以至于实际上存在类型不匹配?
答案 0 :(得分:1)
也许您想尝试HDFql并从HDF5低级细节中抽象自己。在使用HDFql的C ++中,您可以像这样读取可变长度char数据集mydata
(包含在文件myfile.h5
中):
HDFql::execute("USE FILE myfile.h5"); // use (i.e. open) file "myfile.h5"
HDFql::execute("SELECT FROM mydata"); // select (i.e. read) dataset "mydata" and populate default cursor with it
while(HDFql::cursorNext() == HDFql::Success) // display content of default cursor
{
std::cout << HDFql::cursorGetChar() << std::endl;
}