C++:犰狳不想与hdf5格式合作

C++: Armadillo does not want to cooperate with hdf5 format

本文关键字:hdf5 格式 不想 C++      更新时间:2023-10-16

最近我决定将我的数据存储到 hdf5 二进制文件中,而不是 ASCII 文件中。我想使用 hdf5 格式。基本上的想法是将标头和数据放在同一个文件中(标头ASCII不是二进制格式,然后是二进制格式(。像这样:

----------------------------------------
Dataname : testdata
ref_ell  : wgs84
bmin     : 
etc.

这是HDF5格式的数据


犰狳库(http://arma.sourceforge.net/docs.html#save_load_mat(确实具有将数据附加到现有文件(hdf5_opts::append(的功能。但是我已经更早地解决了这个问题。我遵循了手册,但显然我做错了什么。假设我有:

#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>
#define ARMA_USE_HDF5
#include <hdf5.h>
#include <armadillo>

// g++ -O3 -lhdf5 -larmadillo -DARMA_DONT_USE_WRAPPER -DARMA_USE_BLAS -DARMA_USE_LAPACK -DARMA_USE_HDF5 - hdf5.cpp -o hdf5.o
// g++ -O3 -lhdf5 -larmadillo hdf5.cpp -o hdf5.o
// g++ -O3 -larmadillo -lhdf5 hdf5.cpp -o hdf5.o

using namespace std;
int main() {
arma::mat amat = arma::randu<arma::mat>(5,6);
cout << amat << endl;

amat.save( arma::hdf5_name("A.h5", "my_data"));
arma::mat bmat;
bool t = bmat.load( arma::hdf5_name("A.h5", "my_data"));
cout << bmat << endl;
if(t == false)
cout << "problem with loading" << endl;
return 0;
}

我试图编译这个练习,但我只得到错误:

要么这个:

hdf5.cpp: In function ‘int main()’:
hdf5.cpp:28:43: error: ‘hdf5_name’ was not declared in this scope
amat.save( hdf5_name("A.h5", "my_data"));

或:

g++ -O3 -lhdf5 -larmadillo hdf5.cpp -o hdf5.o
hdf5.cpp: In function ‘int main()’:
hdf5.cpp:27:16: error: ‘hdf5_name’ is not a member of ‘arma’
amat.save( arma::hdf5_name("A.h5", "my_data"), arma::hdf5_binary);

我错过了什么?(已解决 - 需要更新犰狳库!

继续问题的第二部分:首先保存标头,然后以 hdf5 格式添加数据。这样它就可以工作了。但是标头是在存储矩阵之后添加的。

#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>

#define ARMA_USE_HDF5
#define ARMA_DONT_USE_WRAPPER
#include <hdf5.h>
#include <armadillo>
// g++ -O3 -larmadillo -lhdf5 hdf5.cpp -o hdf5.o
using namespace std;
int main() {
arma::mat amat = arma::randu<arma::mat>(5,6);
cout << amat << endl;
amat.save( arma::hdf5_name("A.hdf5", "gmodel", arma::hdf5_opts::append ) );
ofstream f_out; f_out.open( "A.hdf5", ios::app );
f_out << "nbegin_of_head ================================================n";
f_out << "model name : " << "model_name" << endl;
f_out << "model type : " << "model_type" << endl;
f_out << "units      : " << "units" << endl;
f_out << "ref_ell    : " << "ref_ell" << endl;
f_out << "ISG format = " << "isg_format" << endl;;
f_out << "end_of_head ==================================================n";
f_out.close();
return 0;
}

当我切换顺序时,amat.save(( 函数只是重写 A.hdf5 文件的内容。

对我来说,代码使用

g++ hdf5.cpp `pkg-config --cflags --libs hdf5`  -DARMA_DONT_USE_WRAPPER -I/home/claes/armadillo-8.500/include -o hdf5.o -lblas -llapack

哪里

`pkg-config --cflags --libs hdf5`

扩展到

-I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial -lhdf5