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> 110 #ifndef DOXYGEN_INTERNAL 129 size_t ch=(size_t)((1.0+1.0e-12)*
130 pow(10.0,floor(log10(((
double)n))+0.5)));
132 if (ch>1000000) ch=1000000;
169 int open(std::string fname,
bool write_access=
false,
170 bool err_on_fail=
true);
198 int getc(std::string name,
char &c);
201 int getd(std::string name,
double &d);
204 int getf(std::string name,
float &f);
207 int geti(std::string name,
int &i);
210 int get_szt(std::string name,
size_t &u);
220 int gets(std::string name, std::string &s);
224 int gets_var(std::string name, std::string &s);
228 int gets_fixed(std::string name, std::string &s);
233 int gets_def_fixed(std::string name, std::string def, std::string &s);
239 void setc(std::string name,
char c);
242 void setd(std::string name,
double d);
245 void setf(std::string name,
float f);
248 void seti(std::string name,
int i);
251 void set_szt(std::string name,
size_t u);
258 void sets(std::string name, std::string s);
267 void sets_fixed(std::string name, std::string s);
279 hid_t
open_group(hid_t init_id, std::string path);
291 return H5Gclose(group);
303 int getd_vec(std::string name, std::vector<double> &v);
318 template<
class vec_t>
320 std::vector<double> v2;
328 int geti_vec(std::string name, std::vector<int> &v);
334 template<
class vec_
int_t>
339 for(
size_t i=0;i<v2.size();i++) v[i]=v2[i];
343 int get_szt_vec(std::string name, std::vector<size_t> &v);
349 template<
class vec_
size_t>
354 for(
size_t i=0;i<v2.size();i++) v[i]=v2[i];
359 int gets_vec(std::string name, std::vector<std::string> &s);
370 int setd_vec(std::string name,
const std::vector<double> &v);
377 template<
class vec_t>
384 std::vector<double> v2(v.size());
387 return setd_arr(name,v2.size(),&v2[0]);
391 int seti_vec(std::string name,
const std::vector<int> &v);
397 template<
class vec_
int_t>
404 std::vector<int> v2(v.size());
407 return seti_arr(name,v2.size(),&v2[0]);
410 int set_szt_vec(std::string name,
const std::vector<size_t> &v);
416 template<
class vec_
size_t>
423 std::vector<size_t> v2(v.size());
437 int sets_vec(std::string name,
const std::vector<std::string> &s);
470 template<
class arr2d_t>
472 size_t c,
const arr2d_t &a2d) {
474 if (write_access==
false) {
475 O2SCL_ERR2(
"File not opened with write access ",
480 double *d=
new double[r*c];
481 for(
size_t i=0;i<r;i++) {
482 for(
size_t j=0;j<c;j++) {
487 hid_t dset, space, dcpl=0;
488 bool chunk_alloc=
false;
493 dset=H5Dopen(current,name.c_str(),H5P_DEFAULT);
496 #ifdef O2SCL_NEVER_DEFINED 505 hsize_t dims[2]={r,c};
506 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
507 space=H5Screate_simple(2,dims,max);
510 dcpl=H5Pcreate(H5P_DATASET_CREATE);
512 int status2=H5Pset_chunk(dcpl,2,chunk);
514 #ifdef O2SCL_HDF5_COMP 517 int status3=H5Pset_deflate(dcpl,6);
518 }
else if (compr_type==2) {
519 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
520 }
else if (compr_type!=0) {
527 dset=H5Dcreate(current,name.c_str(),H5T_IEEE_F64LE,space,H5P_DEFAULT,
534 space=H5Dget_space(dset);
536 int ndims=H5Sget_simple_extent_dims(space,dims,0);
540 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
541 "matrix in hdf_file::setd_arr2d_copy().",
546 if (r!=dims[0] || c!=dims[1]) {
547 hsize_t new_dims[2]={r,c};
548 int status3=H5Dset_extent(dset,new_dims);
555 status=H5Dwrite(dset,H5T_NATIVE_DOUBLE,H5S_ALL,
556 H5S_ALL,H5P_DEFAULT,d);
558 status=H5Dclose(dset);
559 status=H5Sclose(space);
561 status=H5Pclose(dcpl);
572 template<
class arr2d_t>
574 size_t c,
const arr2d_t &a2d) {
576 if (write_access==
false) {
577 O2SCL_ERR2(
"File not opened with write access ",
583 for(
size_t i=0;i<r;i++) {
584 for(
size_t j=0;j<c;j++) {
589 hid_t dset, space, dcpl=0;
590 bool chunk_alloc=
false;
595 dset=H5Dopen(current,name.c_str(),H5P_DEFAULT);
598 #ifdef O2SCL_NEVER_DEFINED 607 hsize_t dims[2]={r,c};
608 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
609 space=H5Screate_simple(2,dims,max);
612 dcpl=H5Pcreate(H5P_DATASET_CREATE);
614 int status2=H5Pset_chunk(dcpl,2,chunk);
616 #ifdef O2SCL_HDF5_COMP 619 int status3=H5Pset_deflate(dcpl,6);
620 }
else if (compr_type==2) {
621 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
622 }
else if (compr_type!=0) {
629 dset=H5Dcreate(current,name.c_str(),H5T_STD_I32LE,space,H5P_DEFAULT,
636 space=H5Dget_space(dset);
638 int ndims=H5Sget_simple_extent_dims(space,dims,0);
642 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
643 "matrix in hdf_file::seti_arr2d_copy().",
648 if (r!=dims[0] || c!=dims[1]) {
649 hsize_t new_dims[2]={r,c};
650 int status3=H5Dset_extent(dset,new_dims);
657 status=H5Dwrite(dset,H5T_NATIVE_INT,H5S_ALL,
658 H5S_ALL,H5P_DEFAULT,d);
660 status=H5Dclose(dset);
661 status=H5Sclose(space);
663 status=H5Pclose(dcpl);
674 template<
class arr2d_t>
676 size_t c,
const arr2d_t &a2d) {
678 if (write_access==
false) {
679 O2SCL_ERR2(
"File not opened with write access ",
684 size_t *d=
new size_t[r*c];
685 for(
size_t i=0;i<r;i++) {
686 for(
size_t j=0;j<c;j++) {
691 hid_t dset, space, dcpl=0;
692 bool chunk_alloc=
false;
697 dset=H5Dopen(current,name.c_str(),H5P_DEFAULT);
700 #ifdef O2SCL_NEVER_DEFINED 709 hsize_t dims[2]={r,c};
710 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
711 space=H5Screate_simple(2,dims,max);
714 dcpl=H5Pcreate(H5P_DATASET_CREATE);
716 int status2=H5Pset_chunk(dcpl,2,chunk);
718 #ifdef O2SCL_HDF5_COMP 721 int status3=H5Pset_deflate(dcpl,6);
722 }
else if (compr_type==2) {
723 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
724 }
else if (compr_type!=0) {
731 dset=H5Dcreate(current,name.c_str(),H5T_STD_U64LE,space,H5P_DEFAULT,
738 space=H5Dget_space(dset);
740 int ndims=H5Sget_simple_extent_dims(space,dims,0);
744 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
745 "matrix in hdf_file::set_szt_arr2d_copy().",
750 if (r!=dims[0] || c!=dims[1]) {
751 hsize_t new_dims[2]={r,c};
752 int status3=H5Dset_extent(dset,new_dims);
759 status=H5Dwrite(dset,H5T_NATIVE_HSIZE,H5S_ALL,
760 H5S_ALL,H5P_DEFAULT,d);
762 status=H5Dclose(dset);
763 status=H5Sclose(space);
765 status=H5Pclose(dcpl);
783 std::vector<size_t> > &t);
791 std::vector<size_t> > &t);
799 std::vector<size_t> > &t);
806 template<
class vec_t,
class vec_
size_t>
820 template<
class vec_t,
class vec_
size_t>
837 std::vector<size_t> > &t);
847 std::vector<size_t> > &t);
857 std::vector<size_t> > &t);
866 template<
class vec_t,
class vec_
size_t>
869 std::vector<size_t> > &t) {
883 template<
class vec_t,
class vec_
size_t>
886 std::vector<size_t> > &t) {
908 int getc_arr(std::string name,
size_t n,
char *c);
916 int getd_arr(std::string name,
size_t n,
double *d);
925 int getd_arr_compr(std::string name,
size_t n,
double *d,
int &compr);
933 int getf_arr(std::string name,
size_t n,
float *f);
941 int geti_arr(std::string name,
size_t n,
int *i);
967 int setc_arr(std::string name,
size_t n,
const char *c);
970 int setd_arr(std::string name,
size_t n,
const double *d);
973 int setf_arr(std::string name,
size_t n,
const float *f);
976 int seti_arr(std::string name,
size_t n,
const int *i);
979 int set_szt_arr(std::string name,
size_t n,
const size_t *u);
1010 int getc_def(std::string name,
char def,
char &c);
1013 int getd_def(std::string name,
double def,
double &d);
1016 int getf_def(std::string name,
float def,
float &f);
1019 int geti_def(std::string name,
int def,
int &i);
1022 int get_szt_def(std::string name,
size_t def,
size_t &i);
1025 int gets_def(std::string name, std::string def, std::string &s);
1028 int gets_var_def(std::string name, std::string def, std::string &s);
1057 std::string &name,
int verbose=0);
1066 std::string &type,
int verbose=0);
1069 void file_list(
int verbose);
1083 static const int ip_filelist=1;
1084 static const int ip_name_from_type=2;
1085 static const int ip_type_from_name=3;
1090 hsize_t dims[100], hsize_t max_dims[100],
1091 std::string base_type, std::string name);
1094 static herr_t
iterate_func(hid_t loc,
const char *name,
1095 const H5L_info_t *inf,
void *op_data);
1097 #ifndef DOXYGEN_INTERNAL int gets_var_def(std::string name, std::string def, std::string &s)
Get a variable length string named name.
int seti_ten_copy(std::string name, const o2scl::tensor< int, std::vector< int >, std::vector< size_t > > &t)
Write a tensor of integers to an HDF file.
int getd_ten(std::string name, o2scl::tensor< double, std::vector< double >, std::vector< size_t > > &t)
Get a tensor of double-precision numbers from an HDF file.
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 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 setd_ten(std::string name, const o2scl::tensor< double, std::vector< double >, std::vector< size_t > > &t)
Write a tensor of double-precision numbers to an HDF file.
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 set_szt_vec_copy(std::string name, const vec_size_t &v)
Set vector dataset named name with v.
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 getd_ten_copy(std::string name, o2scl::tensor< double, vec_t, vec_size_t > &t)
Get a tensor of double-precision numbers from an HDF file.
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.
Parameters for iterate_func()
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 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_ten(std::string name, o2scl::tensor< int, std::vector< int >, std::vector< size_t > > &t)
Get a tensor of integers from an HDF file.
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 geti_ten_copy(std::string name, o2scl::tensor< int, vec_t, vec_size_t > &t)
Get a tensor of integers from an HDF file.
int get_szt(std::string name, size_t &u)
Get an unsigned integer named name.
int get_szt_ten(std::string name, o2scl::tensor< size_t, std::vector< size_t >, std::vector< size_t > > &t)
Get a tensor of size_t from an HDF file.
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.
int set_szt_ten(std::string name, const o2scl::tensor< size_t, std::vector< size_t >, std::vector< size_t > > &t)
Write a tensor of integers to an HDF file.
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 close_group(hid_t group)
Close a previously created group.
int find_object_by_type(std::string type, std::string &name, int verbose=0)
Look in hdf_file hf for an O<span style='position: relative; top: 0.3em; font-size: 0...
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 seti_ten(std::string name, const o2scl::tensor< int, std::vector< int >, std::vector< size_t > > &t)
Write a tensor of integers to an HDF file.
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.
static herr_t iterate_func(hid_t loc, const char *name, const H5L_info_t *inf, void *op_data)
HDF object iteration function.
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.
static void type_process(iterate_parms &ip, int mode, size_t ndims, hsize_t dims[100], hsize_t max_dims[100], std::string base_type, std::string name)
Process a type for iterate_func()
#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_ten_copy(std::string name, const o2scl::tensor< double, std::vector< double >, std::vector< size_t > > &t)
Write a tensor of double-precision numbers to an HDF file.
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.
int find_object_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...
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 getd_arr_compr(std::string name, size_t n, double *d, int &compr)
Get a double array named name of size n and put the compression type in compr.
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.