[c++][hdf5]HDF5格式dataset在c++中的读取
HDF5格式在存储数据集,使用很广泛。下述内容,主要集中于HDF5格式在c++中的使用。其中记录了源码安装、apt-get安装、c、c++读取等。推荐apt-get安装、c++接口。
参考
-
HDF5 C++ API:https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html
-
用户指南:https://portal.hdfgroup.org/display/HDF5/HDF5+User+Guides
-
样例:https://portal.hdfgroup.org/display/HDF5/Examples+in+the+Source+Code
HDF5安装
源码安装
cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS:BOOL=ON ~/hdf5-1.12.1
cmake --build . --config Release
make
sudo make install
或者也可以采用如下:https://blog.csdn.net/weixin_40255337/article/details/84256604
sudo ./configure --prefix=/usr/local/hdf5
sudo make
sudo make check
sudo make install
安装后的关键路径:
~/hdf5/HDF_Group/HDF5/1.12.1/
/usr/local/HDF_Group/HDF5/1.12.1/lib/
/usr/local/HDF_Group/HDF5/1.12.1/share/cmake/hdf5
/usr/local/HDF_Group/HDF5/1.12.1/include/
apt-get 安装:https://github.com/ros-industrial-consortium/reuleaux/issues/41
sudo apt-get install libhdf5-dev
# 查找头文件和库地址
sudo find /usr -name H5Cpp.h
sudo find /usr -name libhdf5.so
whereis hdf5
安装后的关键路径:
/usr/include/hdf5/serial
/usr/lib/x86_64-linux-gnu/hdf5/serial
cmake配置
下述cmake文件编写调试通过,其中动态链接库参考
/usr/local/HDF_Group/HDF5/1.12.1/lib
目录下的/pkgconfig
目录中的参考信息。
cmake_minimum_required(VERSION 3.5)
project(test1)
set(CMAKE_CXX_STANDARD 14)
include_directories(
SYSTEM
/usr/local/HDF_Group/HDF5/1.12.1/include
/usr/include/hdf5/serial
)
link_directories(
/usr/local/HDF_Group/HDF5/1.12.1/lib
/usr/lib/x86_64-linux-gnu/hdf5/serial
)
add_executable(test1 main.cpp)
target_link_libraries(test1 -lm -ldl -lhdf5 -lhdf5_hl)
c/c++ 读写
c++读取矩阵
//! 头文件
#include "H5Cpp.h"
//! 名称空间
using namespace H5;
//! 关闭打印
Exception::dontPrint();
//! 打开文件和数据集
const H5std_string FILE_NAME("SDSextendible.h5");
const H5std_string DATASET_NAME("ExtendibleArray");
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(DATASET_NAME);
//! Get filespace for rank and dimension
DataSpace filespace = dataset.getSpace();
//! Get number of dimensions in the file dataspace
int rank = filespace.getSimpleExtentNdims();
//! Get and print the dimension sizes of the file dataspace
hsize_t dims[2]; // dataset dimensions
rank = filespace.getSimpleExtentDims(dims);
DataSpace mspace1(RANK, dims);
//! 读取数据集矩阵
int data_out[NX][NY]; // buffer for dataset to be read
dataset.read(data_out, PredType::NATIVE_INT, mspace1, filespace);
或者采用这种比较简单的形式
hid_t file_id;
int data[6];
hsize_t dims[2];
size_t i, j, nrow, n_values;
/* open file from ex_lite1.c */
file_id = H5Fopen("ex_lite1.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
/* read dataset */
H5LTread_dataset_int(file_id, "/dset", data);
/* get the dimensions of the dataset */
H5LTget_dataset_info(file_id, "/dset", dims, NULL, NULL);
c读取属性
读取数据集的属性
#include "hdf5.h"
#include "hdf5_hl.h"
#include <stdlib.h>
#define ATTR_SIZE 5
/* get the attribute "attr1" from the dataset "dset" */
int data[ATTR_SIZE] = {1, 2, 3, 4, 5};
H5LTset_attribute_int(file_id, "dset", "attr1", data, ATTR_SIZE);
H5LTget_attribute_int(file_id, "dset", "attr1", data);
读取数据根层属性
#include <hdf5.h>
#include <hdf5_hl.h>
void hdf5_write_example() {
auto file_id = H5Fcreate("tmp.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
auto aid3 = H5Screate(H5S_SCALAR);
auto atype = H5Tcopy(H5T_C_S1);
std::string str_value = "HDF5_TEST_STR";
H5Tset_size(atype, str_value.length() + 1);
H5Tset_strpad(atype, H5T_STR_NULLTERM);
auto attr3 = H5Acreate2(file_id, "test_attr_str", atype, aid3, H5P_DEFAULT, H5P_DEFAULT);
H5Awrite(attr3, atype, str_value.c_str());
H5Aclose(attr3);
H5Sclose(aid3);
H5Tclose(atype);
H5Fclose(file_id);
}
void hdf5_read_example() {
char res[100] = "____________";
auto file_id = H5Fopen("tmp.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
auto atype = H5Tcopy(H5T_C_S1);
H5Tset_size(atype, 100);
H5Tset_strpad(atype, H5T_STR_NULLTERM);
auto attr = H5Aopen(file_id, "test_attr_str", H5P_DEFAULT);
H5Aread(attr, atype, res);
H5Aclose(attr);
H5Tclose(atype);
std::cout << "value of attribute test_attr_str: " << res << std::endl;
H5Fclose(file_id);
}
int main() {
hdf5_write_example();
hdf5_read_example();
return 0;
}
输出:
value of attribute test_attr_str: HDF5_TEST_STR
c++ 读取属性
void test_read_attribute_cpp() {
H5File file("tmp.h5", H5F_ACC_RDONLY);
Group what = file.openGroup("/");
Attribute attr = what.openAttribute("test_attr_str");
H5std_string test;
DataType type = attr.getDataType();
attr.read(type, test);
std::cout << "value of attribute test_attr_str (c++ api): " << test << std::endl;
}
int main() {
hdf5_write_example();
test_read_attribute_cpp();
return 0;
}
输出:
value of attribute test_attr_str (c++ api): HDF5_TEST_STR
参考
https://raw.githubusercontent.com/HDFGroup/hdf5/develop/c++/examples/chunks.cpp
https://raw.githubusercontent.com/HDFGroup/hdf5/develop/c++/examples/readdata.cpp
https://raw.githubusercontent.com/HDFGroup/hdf5/develop/hl/examples/ex_lite2.c
c的属性信息读取:google search: hdf5 read attribute c++
https://raw.githubusercontent.com/HDFGroup/hdf5/develop/hl/examples/ex_lite3.c
https://raw.githubusercontent.com/HDFGroup/hdf5/develop/examples/h5_attribute.c
https://fossies.org/linux/hdf5/examples/h5_attribute.c
https://support.hdfgroup.org/HDF5/doc1.6/UG/13_Attributes.html
c++接口
https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html
https://stackoverflow.com/questions/22842541/hdf5-c-read-the-contents-of-an-attribute/22847313
https://portal.hdfgroup.org/pages/viewpage.action?pageId=50073884
- 点赞
- 收藏
- 关注作者
评论(0)