23 #ifndef O2SCL_HDF_FILE_H 24 #define O2SCL_HDF_FILE_H 31 #ifdef O2SCL_UBUNTU_HDF5 32 #include <hdf5/serial/hdf5.h> 37 #include <boost/numeric/ublas/vector.hpp> 38 #include <boost/numeric/ublas/matrix.hpp> 40 #include <o2scl/vector.h> 41 #include <o2scl/tensor.h> 42 #include <o2scl/format_float.h> 109 #ifndef DOXYGEN_INTERNAL 128 size_t ch=(size_t)((1.0+1.0e-12)*
129 pow(10.0,floor(log10(((
double)n))+0.5)));
131 if (ch>1000000) ch=1000000;
168 int open(std::string fname,
bool write_access=
false,
169 bool err_on_fail=
true);
197 int getc(std::string name,
char &c);
200 int getd(std::string name,
double &d);
203 int getf(std::string name,
float &f);
206 int geti(std::string name,
int &i);
209 int get_szt(std::string name,
size_t &u);
219 int gets(std::string name, std::string &s);
223 int gets_var(std::string name, std::string &s);
227 int gets_fixed(std::string name, std::string &s);
232 int gets_def_fixed(std::string name, std::string def, std::string &s);
238 void setc(std::string name,
char c);
241 void setd(std::string name,
double d);
244 void setf(std::string name,
float f);
247 void seti(std::string name,
int i);
250 void set_szt(std::string name,
size_t u);
257 void sets(std::string name, std::string s);
266 void sets_fixed(std::string name, std::string s);
278 hid_t
open_group(hid_t init_id, std::string path);
290 return H5Gclose(group);
302 int getd_vec(std::string name, std::vector<double> &v);
317 template<
class vec_t>
319 std::vector<double> v2;
327 int geti_vec(std::string name, std::vector<int> &v);
333 template<
class vec_
int_t>
338 for(
size_t i=0;i<v2.size();i++) v[i]=v2[i];
342 int get_szt_vec(std::string name, std::vector<size_t> &v);
348 template<
class vec_
size_t>
353 for(
size_t i=0;i<v2.size();i++) v[i]=v2[i];
358 int gets_vec(std::string name, std::vector<std::string> &s);
369 int setd_vec(std::string name,
const std::vector<double> &v);
376 template<
class vec_t>
383 std::vector<double> v2(v.size());
386 return setd_arr(name,v2.size(),&v2[0]);
390 int seti_vec(std::string name,
const std::vector<int> &v);
396 template<
class vec_
int_t>
403 std::vector<int> v2(v.size());
406 return seti_arr(name,v2.size(),&v2[0]);
409 int set_szt_vec(std::string name,
const std::vector<size_t> &v);
415 template<
class vec_
size_t>
422 std::vector<size_t> v2(v.size());
436 int sets_vec(std::string name,
const std::vector<std::string> &s);
469 template<
class arr2d_t>
471 size_t c,
const arr2d_t &a2d) {
473 if (write_access==
false) {
474 O2SCL_ERR2(
"File not opened with write access ",
479 double *d=
new double[r*c];
480 for(
size_t i=0;i<r;i++) {
481 for(
size_t j=0;j<c;j++) {
486 hid_t dset, space, dcpl=0;
487 bool chunk_alloc=
false;
492 dset=H5Dopen(current,name.c_str(),H5P_DEFAULT);
495 #ifdef O2SCL_NEVER_DEFINED 504 hsize_t dims[2]={r,c};
505 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
506 space=H5Screate_simple(2,dims,max);
509 dcpl=H5Pcreate(H5P_DATASET_CREATE);
511 int status2=H5Pset_chunk(dcpl,2,chunk);
513 #ifdef O2SCL_HDF5_COMP 516 int status3=H5Pset_deflate(dcpl,6);
517 }
else if (compr_type==2) {
518 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
519 }
else if (compr_type!=0) {
526 dset=H5Dcreate(current,name.c_str(),H5T_IEEE_F64LE,space,H5P_DEFAULT,
533 space=H5Dget_space(dset);
535 int ndims=H5Sget_simple_extent_dims(space,dims,0);
539 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
540 "matrix in hdf_file::setd_arr2d_copy().",
545 if (r!=dims[0] || c!=dims[1]) {
546 hsize_t new_dims[2]={r,c};
547 int status3=H5Dset_extent(dset,new_dims);
554 status=H5Dwrite(dset,H5T_NATIVE_DOUBLE,H5S_ALL,
555 H5S_ALL,H5P_DEFAULT,d);
557 status=H5Dclose(dset);
558 status=H5Sclose(space);
560 status=H5Pclose(dcpl);
571 template<
class arr2d_t>
573 size_t c,
const arr2d_t &a2d) {
575 if (write_access==
false) {
576 O2SCL_ERR2(
"File not opened with write access ",
582 for(
size_t i=0;i<r;i++) {
583 for(
size_t j=0;j<c;j++) {
588 hid_t dset, space, dcpl=0;
589 bool chunk_alloc=
false;
594 dset=H5Dopen(current,name.c_str(),H5P_DEFAULT);
597 #ifdef O2SCL_NEVER_DEFINED 606 hsize_t dims[2]={r,c};
607 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
608 space=H5Screate_simple(2,dims,max);
611 dcpl=H5Pcreate(H5P_DATASET_CREATE);
613 int status2=H5Pset_chunk(dcpl,2,chunk);
615 #ifdef O2SCL_HDF5_COMP 618 int status3=H5Pset_deflate(dcpl,6);
619 }
else if (compr_type==2) {
620 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
621 }
else if (compr_type!=0) {
628 dset=H5Dcreate(current,name.c_str(),H5T_STD_I32LE,space,H5P_DEFAULT,
635 space=H5Dget_space(dset);
637 int ndims=H5Sget_simple_extent_dims(space,dims,0);
641 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
642 "matrix in hdf_file::seti_arr2d_copy().",
647 if (r!=dims[0] || c!=dims[1]) {
648 hsize_t new_dims[2]={r,c};
649 int status3=H5Dset_extent(dset,new_dims);
656 status=H5Dwrite(dset,H5T_NATIVE_INT,H5S_ALL,
657 H5S_ALL,H5P_DEFAULT,d);
659 status=H5Dclose(dset);
660 status=H5Sclose(space);
662 status=H5Pclose(dcpl);
673 template<
class arr2d_t>
675 size_t c,
const arr2d_t &a2d) {
677 if (write_access==
false) {
678 O2SCL_ERR2(
"File not opened with write access ",
683 size_t *d=
new size_t[r*c];
684 for(
size_t i=0;i<r;i++) {
685 for(
size_t j=0;j<c;j++) {
690 hid_t dset, space, dcpl=0;
691 bool chunk_alloc=
false;
696 dset=H5Dopen(current,name.c_str(),H5P_DEFAULT);
699 #ifdef O2SCL_NEVER_DEFINED 708 hsize_t dims[2]={r,c};
709 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
710 space=H5Screate_simple(2,dims,max);
713 dcpl=H5Pcreate(H5P_DATASET_CREATE);
715 int status2=H5Pset_chunk(dcpl,2,chunk);
717 #ifdef O2SCL_HDF5_COMP 720 int status3=H5Pset_deflate(dcpl,6);
721 }
else if (compr_type==2) {
722 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
723 }
else if (compr_type!=0) {
730 dset=H5Dcreate(current,name.c_str(),H5T_STD_U64LE,space,H5P_DEFAULT,
737 space=H5Dget_space(dset);
739 int ndims=H5Sget_simple_extent_dims(space,dims,0);
743 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
744 "matrix in hdf_file::set_szt_arr2d_copy().",
749 if (r!=dims[0] || c!=dims[1]) {
750 hsize_t new_dims[2]={r,c};
751 int status3=H5Dset_extent(dset,new_dims);
758 status=H5Dwrite(dset,H5T_NATIVE_HSIZE,H5S_ALL,
759 H5S_ALL,H5P_DEFAULT,d);
761 status=H5Dclose(dset);
762 status=H5Sclose(space);
764 status=H5Pclose(dcpl);
788 template<
class vec_t,
class vec_
size_t>
804 std::vector<size_t> > &t);
813 template<
class vec_t,
class vec_
size_t>
816 std::vector<size_t> > &t) {
838 int getc_arr(std::string name,
size_t n,
char *c);
846 int getd_arr(std::string name,
size_t n,
double *d);
847 int getd_arr(std::string name,
size_t n,
double *d,
int &compr);
855 int getf_arr(std::string name,
size_t n,
float *f);
863 int geti_arr(std::string name,
size_t n,
int *i);
889 int setc_arr(std::string name,
size_t n,
const char *c);
892 int setd_arr(std::string name,
size_t n,
const double *d);
895 int setf_arr(std::string name,
size_t n,
const float *f);
898 int seti_arr(std::string name,
size_t n,
const int *i);
901 int set_szt_arr(std::string name,
size_t n,
const size_t *u);
932 int getc_def(std::string name,
char def,
char &c);
935 int getd_def(std::string name,
double def,
double &d);
938 int getf_def(std::string name,
float def,
float &f);
941 int geti_def(std::string name,
int def,
int &i);
944 int get_szt_def(std::string name,
size_t def,
size_t &i);
947 int gets_def(std::string name, std::string def, std::string &s);
950 int gets_var_def(std::string name, std::string def, std::string &s);
979 std::string &group_name,
int verbose=0);
988 std::string &type,
int verbose=0);
991 #ifndef DOXYGEN_INTERNAL 1015 std::string group_name;
1029 const H5L_info_t *inf,
void *op_data);
1040 const H5L_info_t *inf,
void *op_data);
int gets_var_def(std::string name, std::string def, std::string &s)
Get a variable length string named name.
int getf_arr_alloc(std::string name, size_t &n, float *f)
Get a float array named name of size n.
int seti_vec_copy(std::string name, vec_int_t &v)
Set vector dataset named name with v.
int getd_ten_copy(std::string name, o2scl::tensor< vec_t, vec_size_t > &t)
Get a tensor from an HDF file.
int seti_mat_copy(std::string name, const ubmatrix_int &m)
Set matrix dataset named name with m.
hid_t current
Current file or group location.
int getd_vec(std::string name, std::vector< double > &v)
Get vector dataset and place data in v.
int setd_mat_copy(std::string name, const ubmatrix &m)
Set matrix dataset named name with m.
int setd_vec_copy(std::string name, const vec_t &v)
Set vector dataset named name with v.
int setd_vec(std::string name, const std::vector< double > &v)
Set vector dataset named name with v.
int setd_arr(std::string name, size_t n, const double *d)
Set a double array named name of size n to value d.
int geti(std::string name, int &i)
Get a integer named name.
int getd_arr(std::string name, size_t n, double *d)
Get a double array named name of size n.
int set_szt_vec(std::string name, const std::vector< size_t > &v)
Set vector dataset named name with v.
int get_szt_def(std::string name, size_t def, size_t &i)
Get a size_t named name.
int find_group_by_name(std::string name, std::string &type, int verbose=0)
Look in hdf_file hf for an O<span style='position: relative; top: 0.3em; font-size: 0...
int set_szt_vec_copy(std::string name, const vec_size_t &v)
Set vector dataset named name with v.
int find_group_by_type(std::string type, std::string &group_name, int verbose=0)
Look in hdf_file hf for an O<span style='position: relative; top: 0.3em; font-size: 0...
int getf_arr(std::string name, size_t n, float *f)
Get a float array named name of size n.
void close()
Close the file.
void set_szt(std::string name, size_t u)
Set an unsigned integer named name to value u.
int getd(std::string name, double &d)
Get a double named name.
void setd(std::string name, double d)
Set a double named name to value d.
int gets_def(std::string name, std::string def, std::string &s)
Get a string named name.
bool file_open
True if a file has been opened.
int setf_arr(std::string name, size_t n, const float *f)
Set a float array named name of size n to value f.
int iterate_match_name(hid_t loc, const char *name, const H5L_info_t *inf, void *op_data)
Look at location loc in an HDF file for an O<span style='position: relative; top: 0...
int set_szt_arr(std::string name, size_t n, const size_t *u)
Set a integer array named name of size n to value i.
invalid argument supplied by user
int getd_ten(std::string name, o2scl::tensor< std::vector< double >, std::vector< size_t > > &t)
Get a tensor from an HDF file.
int getc_arr(std::string name, size_t n, char *c)
Get a character array named name of size n.
int getc_arr_alloc(std::string name, size_t &n, char *c)
Get a character array named name of size n.
void seti(std::string name, int i)
Set an integer named name to value i.
int setd_arr_fixed(std::string name, size_t n, const double *c)
Set a double array named name of size n to value d.
void sets_fixed(std::string name, std::string s)
Set a fixed-length string named name to value s.
int geti_vec_copy(std::string name, vec_int_t &v)
Get vector dataset and place data in v.
int geti_arr_alloc(std::string name, size_t &n, int *i)
Get an integer array named name of size n.
int sets_vec(std::string name, const std::vector< std::string > &s)
Set a vector of strings named name.
int geti_arr(std::string name, size_t n, int *i)
Get an integer array named name of size n.
int iterate_match_type(hid_t loc, const char *name, const H5L_info_t *inf, void *op_data)
Look at location loc in an HDF file for an O<span style='position: relative; top: 0...
int get_szt(std::string name, size_t &u)
Get an unsigned integer named name.
hid_t get_current_id()
Retrieve the current working id.
int getf_def(std::string name, float def, float &f)
Get a float named name.
An internal structure to pass information to and from iterate_match_type() and iterate_match_name() ...
int getd_vec_prealloc(std::string name, size_t n, double *d)
Get a double array d pre-allocated to have size n.
int geti_vec(std::string name, std::vector< int > &v)
Get vector dataset and place data in v.
void sets(std::string name, std::string s)
Set a string named name to value s.
int setd_ten_copy(std::string name, const o2scl::tensor< std::vector< double >, std::vector< size_t > > &t)
Write a tensor to an HDF file.
int close_group(hid_t group)
Close a previously created group.
int getf(std::string name, float &f)
Get a float named name.
int gets_fixed(std::string name, std::string &s)
Get a fixed-length string named name.
int setc_arr(std::string name, size_t n, const char *c)
Set a character array named name of size n to value c.
int geti_def(std::string name, int def, int &i)
Get a integer named name.
void vector_copy(const vec_t &src, vec2_t &dest)
Simple vector copy.
void open_or_create(std::string fname)
Open a file named fname or create if it doesn't already exist.
int getd_mat_copy(std::string name, ubmatrix &m)
Get matrix dataset and place data in m.
int seti_vec(std::string name, const std::vector< int > &v)
Set vector dataset named name with v.
int setd_ten(std::string name, const o2scl::tensor< std::vector< double >, std::vector< size_t > > &t)
Write a tensor to an HDF file.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
bool has_write_access()
If true, then the file has read and write access.
void set_current_id(hid_t cur)
Set the current working id.
int set_szt_arr2d_copy(std::string name, size_t r, size_t c, const arr2d_t &a2d)
Set a two-dimensional array dataset named name with m.
int geti_vec_prealloc(std::string name, size_t n, int *i)
Get an integer array i pre-allocated to have size n.
int getd_def(std::string name, double def, double &d)
Get a double named name.
int setf_arr_fixed(std::string name, size_t n, const float *f)
Set a float array named name of size n to value f.
int compr_type
Compression type (support experimental)
int geti_mat_prealloc(std::string name, size_t n, size_t m, int *i)
Get an integer matrix i pre-allocated to have size (n,m)
int gets_var(std::string name, std::string &s)
Get a variable length string named name.
int geti_mat_copy(std::string name, ubmatrix_int &m)
Get matrix dataset and place data in m.
int setd_arr2d_copy(std::string name, size_t r, size_t c, const arr2d_t &a2d)
Set a two-dimensional array dataset named name with m.
int getd_mat_prealloc(std::string name, size_t n, size_t m, double *d)
Get a double matrix d pre-allocated to have size (n,m)
virtual hsize_t def_chunk(size_t n)
Default chunk size.
hid_t get_file_id()
Get the current file id.
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
int getc(std::string name, char &c)
Get a character named name.
int gets_vec(std::string name, std::vector< std::string > &s)
Get a vector of strings named name and store it in s.
int setc_arr_fixed(std::string name, size_t n, const char *c)
Set a character array named name of size n to value c.
void setc(std::string name, char c)
Set a character named name to value c.
int gets(std::string name, std::string &s)
Get a string named name.
size_t min_compr_size
Minimum size to compress by default.
int seti_arr_fixed(std::string name, size_t n, const int *i)
Set an integer array named name of size n to value i.
int getd_vec_copy(std::string name, vec_t &v)
Get vector dataset and place data in v.
int open(std::string fname, bool write_access=false, bool err_on_fail=true)
Open a file named fname.
int get_szt_vec_copy(std::string name, vec_size_t &v)
Get vector dataset and place data in v.
void setf(std::string name, float f)
Set a float named name to value f.
int gets_def_fixed(std::string name, std::string def, std::string &s)
Get a fixed-length string named name with default value s.
int seti_arr2d_copy(std::string name, size_t r, size_t c, const arr2d_t &a2d)
Set a two-dimensional array dataset named name with m.
bool write_access
If true, then the file has read and write access.
int get_szt_vec(std::string name, std::vector< size_t > &v)
Get vector dataset and place data in v.
int getd_arr_alloc(std::string name, size_t &n, double *d)
Get a double array named name of size n.
int seti_arr(std::string name, size_t n, const int *i)
Set a integer array named name of size n to value i.
Tensor class with arbitrary dimensions.
hid_t open_group(hid_t init_id, std::string path)
Open a group relative to the location specified in init_id.
int getc_def(std::string name, char def, char &c)
Get a character named name.