【发布时间】:2019-01-17 09:07:46
【问题描述】:
最近我决定将我的数据存储到 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 格式的数据。这样就可以了。但是header是在矩阵存储后添加的。
#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 文件的内容。
【问题讨论】:
-
看起来您使用的是一个非常旧版本的犰狳。根据list of additions,您需要至少使用 8.300 版本才能获得所需的 hdf5 功能。
-
是的,你是对的,我已经将我的 Fedora 从 26 更新到 28。现在它可以工作了。所以我们可以继续解决问题的另一部分。