Go to the documentation of this file.
23 #ifndef O2SCL_HDF_FILE_H
24 #define O2SCL_HDF_FILE_H
31 #ifdef O2SCL_PLAIN_HDF5_HEADER
35 #include <hdf5/serial/hdf5.h>
41 #include <boost/numeric/ublas/vector.hpp>
42 #include <boost/numeric/ublas/matrix.hpp>
44 #include <o2scl/vector.h>
45 #include <o2scl/tensor.h>
46 #include <o2scl/format_float.h>
114 #ifndef DOXYGEN_INTERNAL
133 size_t ch=(size_t)((1.0+1.0e-12)*
134 pow(10.0,floor(log10(((
double)n))+0.5)));
136 if (ch>1000000) ch=1000000;
174 bool err_on_fail=
true);
202 int getc(std::string name,
char &c);
205 int getd(std::string name,
double &d);
208 int getf(std::string name,
float &f);
211 int geti(std::string name,
int &i);
214 int get_szt(std::string name,
size_t &u);
224 int gets(std::string name, std::string &s);
228 int gets_var(std::string name, std::string &s);
232 int gets_fixed(std::string name, std::string &s);
237 int gets_def_fixed(std::string name, std::string def, std::string &s);
243 void setc(std::string name,
char c);
246 void setd(std::string name,
double d);
249 void setf(std::string name,
float f);
252 void seti(std::string name,
int i);
255 void set_szt(std::string name,
size_t u);
262 void sets(std::string name, std::string s);
271 void sets_fixed(std::string name, std::string s);
283 hid_t
open_group(hid_t init_id, std::string path);
295 return H5Gclose(group);
307 int getd_vec(std::string name, std::vector<double> &v);
322 template<
class vec_t>
324 std::vector<double> v2;
332 int geti_vec(std::string name, std::vector<int> &v);
338 template<
class vec_
int_t>
343 for(
size_t i=0;i<v2.size();i++) v[i]=v2[i];
347 int get_szt_vec(std::string name, std::vector<size_t> &v);
353 template<
class vec_
size_t>
358 for(
size_t i=0;i<v2.size();i++) v[i]=v2[i];
363 int gets_vec(std::string name, std::vector<std::string> &s);
374 int setd_vec(std::string name,
const std::vector<double> &v);
381 template<
class vec_t>
388 std::vector<double> v2(v.size());
391 return setd_arr(name,v2.size(),&v2[0]);
395 int seti_vec(std::string name,
const std::vector<int> &v);
401 template<
class vec_
int_t>
408 std::vector<int> v2(v.size());
411 return seti_arr(name,v2.size(),&v2[0]);
414 int set_szt_vec(std::string name,
const std::vector<size_t> &v);
420 template<
class vec_
size_t>
427 std::vector<size_t> v2(v.size());
441 int sets_vec(std::string name,
const std::vector<std::string> &s);
474 template<
class arr2d_t>
476 size_t c,
const arr2d_t &a2d) {
479 O2SCL_ERR2(
"File not opened with write access ",
484 double *d=
new double[r*c];
485 for(
size_t i=0;i<r;i++) {
486 for(
size_t j=0;j<c;j++) {
491 hid_t dset, space, dcpl=0;
492 bool chunk_alloc=
false;
497 dset=H5Dopen(
current,name.c_str(),H5P_DEFAULT);
500 #ifdef O2SCL_NEVER_DEFINED
509 hsize_t dims[2]={r,c};
510 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
511 space=H5Screate_simple(2,dims,max);
514 dcpl=H5Pcreate(H5P_DATASET_CREATE);
516 int status2=H5Pset_chunk(dcpl,2,chunk);
518 #ifdef O2SCL_HDF5_COMP
521 int status3=H5Pset_deflate(dcpl,6);
523 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
531 dset=H5Dcreate(
current,name.c_str(),H5T_IEEE_F64LE,space,H5P_DEFAULT,
538 space=H5Dget_space(dset);
540 int ndims=H5Sget_simple_extent_dims(space,dims,0);
544 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
545 "matrix in hdf_file::setd_arr2d_copy().",
550 if (r!=dims[0] || c!=dims[1]) {
551 hsize_t new_dims[2]={r,c};
552 int status3=H5Dset_extent(dset,new_dims);
559 status=H5Dwrite(dset,H5T_NATIVE_DOUBLE,H5S_ALL,
560 H5S_ALL,H5P_DEFAULT,d);
562 status=H5Dclose(dset);
563 status=H5Sclose(space);
565 status=H5Pclose(dcpl);
576 template<
class arr2d_t>
578 size_t c,
const arr2d_t &a2d) {
581 O2SCL_ERR2(
"File not opened with write access ",
587 for(
size_t i=0;i<r;i++) {
588 for(
size_t j=0;j<c;j++) {
593 hid_t dset, space, dcpl=0;
594 bool chunk_alloc=
false;
599 dset=H5Dopen(
current,name.c_str(),H5P_DEFAULT);
602 #ifdef O2SCL_NEVER_DEFINED
611 hsize_t dims[2]={r,c};
612 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
613 space=H5Screate_simple(2,dims,max);
616 dcpl=H5Pcreate(H5P_DATASET_CREATE);
618 int status2=H5Pset_chunk(dcpl,2,chunk);
620 #ifdef O2SCL_HDF5_COMP
623 int status3=H5Pset_deflate(dcpl,6);
625 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
633 dset=H5Dcreate(
current,name.c_str(),H5T_STD_I32LE,space,H5P_DEFAULT,
640 space=H5Dget_space(dset);
642 int ndims=H5Sget_simple_extent_dims(space,dims,0);
646 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
647 "matrix in hdf_file::seti_arr2d_copy().",
652 if (r!=dims[0] || c!=dims[1]) {
653 hsize_t new_dims[2]={r,c};
654 int status3=H5Dset_extent(dset,new_dims);
661 status=H5Dwrite(dset,H5T_NATIVE_INT,H5S_ALL,
662 H5S_ALL,H5P_DEFAULT,d);
664 status=H5Dclose(dset);
665 status=H5Sclose(space);
667 status=H5Pclose(dcpl);
678 template<
class arr2d_t>
680 size_t c,
const arr2d_t &a2d) {
683 O2SCL_ERR2(
"File not opened with write access ",
688 size_t *d=
new size_t[r*c];
689 for(
size_t i=0;i<r;i++) {
690 for(
size_t j=0;j<c;j++) {
695 hid_t dset, space, dcpl=0;
696 bool chunk_alloc=
false;
701 dset=H5Dopen(
current,name.c_str(),H5P_DEFAULT);
704 #ifdef O2SCL_NEVER_DEFINED
713 hsize_t dims[2]={r,c};
714 hsize_t max[2]={H5S_UNLIMITED,H5S_UNLIMITED};
715 space=H5Screate_simple(2,dims,max);
718 dcpl=H5Pcreate(H5P_DATASET_CREATE);
720 int status2=H5Pset_chunk(dcpl,2,chunk);
722 #ifdef O2SCL_HDF5_COMP
725 int status3=H5Pset_deflate(dcpl,6);
727 int status3=H5Pset_szip(dcpl,H5_SZIP_NN_OPTION_MASK,16);
735 dset=H5Dcreate(
current,name.c_str(),H5T_STD_U64LE,space,H5P_DEFAULT,
742 space=H5Dget_space(dset);
744 int ndims=H5Sget_simple_extent_dims(space,dims,0);
748 O2SCL_ERR2(
"Tried to set a non-matrix dataset with a ",
749 "matrix in hdf_file::set_szt_arr2d_copy().",
754 if (r!=dims[0] || c!=dims[1]) {
755 hsize_t new_dims[2]={r,c};
756 int status3=H5Dset_extent(dset,new_dims);
763 status=H5Dwrite(dset,H5T_NATIVE_HSIZE,H5S_ALL,
764 H5S_ALL,H5P_DEFAULT,d);
766 status=H5Dclose(dset);
767 status=H5Sclose(space);
769 status=H5Pclose(dcpl);
787 std::vector<size_t> > &t);
795 std::vector<size_t> > &t);
803 std::vector<size_t> > &t);
810 template<
class vec_t,
class vec_
size_t>
824 template<
class vec_t,
class vec_
size_t>
841 std::vector<size_t> > &t);
851 std::vector<size_t> > &t);
861 std::vector<size_t> > &t);
870 template<
class vec_t,
class vec_
size_t>
873 std::vector<size_t> > &t) {
887 template<
class vec_t,
class vec_
size_t>
890 std::vector<size_t> > &t) {
912 int getc_arr(std::string name,
size_t n,
char *c);
920 int getd_arr(std::string name,
size_t n,
double *d);
938 int getf_arr(std::string name,
size_t n,
float *f);
946 int geti_arr(std::string name,
size_t n,
int *i);
972 int setc_arr(std::string name,
size_t n,
const char *c);
975 int setd_arr(std::string name,
size_t n,
const double *d);
978 int setf_arr(std::string name,
size_t n,
const float *f);
981 int seti_arr(std::string name,
size_t n,
const int *i);
984 int set_szt_arr(std::string name,
size_t n,
const size_t *u);
1015 int getc_def(std::string name,
char def,
char &c);
1018 int getd_def(std::string name,
double def,
double &d);
1021 int getf_def(std::string name,
float def,
float &f);
1024 int geti_def(std::string name,
int def,
int &i);
1027 int get_szt_def(std::string name,
size_t def,
size_t &i);
1030 int gets_def(std::string name, std::string def, std::string &s);
1033 int gets_var_def(std::string name, std::string def, std::string &s);
1062 std::string &name,
int verbose=0);
1071 std::string &type,
int verbose=0);
1081 std::string &type,
int verbose=0);
1101 static const int ip_filelist=1;
1102 static const int ip_name_from_type=2;
1103 static const int ip_type_from_name=3;
1104 static const int ip_type_from_pattern=4;
1109 hsize_t dims[100], hsize_t max_dims[100],
1110 std::string base_type, std::string name);
1113 static herr_t
iterate_func(hid_t loc,
const char *name,
1114 const H5L_info_t *inf,
void *op_data);
1116 #ifndef DOXYGEN_INTERNAL
Tensor class with arbitrary dimensions.
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()
int get_szt(std::string name, size_t &u)
Get an unsigned integer named name.
int getc(std::string name, char &c)
Get a character named name.
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)
void sets(std::string name, std::string s)
Set a 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.
@ exc_efailed
generic failure
int getc_arr(std::string name, size_t n, char *c)
Get a character array named name of size n.
int gets_var_def(std::string name, std::string def, std::string &s)
Get a variable length string named name.
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 gets_var(std::string name, std::string &s)
Get a variable length string named name.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
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.
hid_t get_current_id()
Retrieve the current working id.
int getd_mat_copy(std::string name, ubmatrix &m)
Get matrix dataset and place data in m.
int geti_vec_prealloc(std::string name, size_t n, int *i)
Get an integer array i pre-allocated to have size n.
void vector_copy(const vec_t &src, vec2_t &dest)
Simple vector copy.
int sets_vec(std::string name, const std::vector< std::string > &s)
Set a vector of strings named name.
int getc_def(std::string name, char def, char &c)
Get a character named name.
int geti(std::string name, int &i)
Get a integer named name.
int getf_arr(std::string name, size_t n, float *f)
Get a float array named name of size n.
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.
int geti_vec(std::string name, std::vector< int > &v)
Get vector dataset and place data in v.
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.
void close()
Close the file.
int getd_vec(std::string name, std::vector< double > &v)
Get vector dataset and place data in v.
int getf(std::string name, float &f)
Get a float named name.
int seti_vec_copy(std::string name, vec_int_t &v)
Set vector dataset named name with v.
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.
void file_list(int verbose)
List datasets and O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$s...
int open(std::string fname, bool write_access=false, bool err_on_fail=true)
Open a file named fname.
int setd_vec_copy(std::string name, const vec_t &v)
Set vector dataset named name with v.
void sets_fixed(std::string name, std::string s)
Set a fixed-length string named name to value s.
int getd(std::string name, double &d)
Get a double named name.
int gets(std::string name, std::string &s)
Get a string 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 setd_arr_fixed(std::string name, size_t n, const double *c)
Set a double array named name of size n to value d.
int geti_mat_copy(std::string name, ubmatrix_int &m)
Get matrix dataset and place data in m.
@ exc_einval
invalid argument supplied by user
int getc_arr_alloc(std::string name, size_t &n, char *c)
Get a character array named name of size n.
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 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.
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 seti_vec(std::string name, const std::vector< int > &v)
Set vector dataset named name with v.
bool write_access
If true, then the file has read and write access.
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
int set_szt_vec_copy(std::string name, const vec_size_t &v)
Set vector dataset named name with v.
void set_szt(std::string name, size_t u)
Set an unsigned integer named name to value u.
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 get_szt_vec_copy(std::string name, vec_size_t &v)
Get vector dataset and place data in v.
bool file_open
True if a file has been opened.
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 setf_arr(std::string name, size_t n, const float *f)
Set a float array named name of size n to value f.
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.
virtual hsize_t def_chunk(size_t n)
Default chunk size.
void setc(std::string name, char c)
Set a character named name to value c.
int geti_arr(std::string name, size_t n, int *i)
Get an integer array named name of size n.
hid_t get_file_id()
Get the current file id.
void seti(std::string name, int i)
Set an integer named name to value i.
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....
int geti_def(std::string name, int def, int &i)
Get a integer named name.
int geti_arr_alloc(std::string name, size_t &n, int *i)
Get an integer array named name of size n.
hid_t current
Current file or group location.
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
void open_or_create(std::string fname)
Open a file named fname or create if it doesn't already exist.
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 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.
void setd(std::string name, double d)
Set a double named name to value d.
int gets_vec(std::string name, std::vector< std::string > &s)
Get a vector of strings named name and store it in s.
Parameters for iterate_func()
size_t min_compr_size
Minimum size to compress by default.
int getd_vec_prealloc(std::string name, size_t n, double *d)
Get a double array d pre-allocated to have size n.
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 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_arr(std::string name, size_t n, const int *i)
Set a integer array named name of size n to value i.
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 gets_def(std::string name, std::string def, std::string &s)
Get a string named name.
int getd_arr(std::string name, size_t n, double *d)
Get a double array named name of size n.
int get_szt_def(std::string name, size_t def, size_t &i)
Get a size_t named name.
int getf_def(std::string name, float def, float &f)
Get a float named name.
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)
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.
void setf(std::string name, float f)
Set a float named name to value f.
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.
int getd_arr_alloc(std::string name, size_t &n, double *d)
Get a double array named name of size n.
int getd_vec_copy(std::string name, vec_t &v)
Get vector dataset and place data in v.
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 close_group(hid_t group)
Close a previously created group.
int gets_fixed(std::string name, std::string &s)
Get a fixed-length string named name.
int setd_mat_copy(std::string name, const ubmatrix &m)
Set matrix dataset named name with m.
int set_szt_vec(std::string name, const std::vector< size_t > &v)
Set vector dataset named name with v.
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.
static herr_t iterate_func(hid_t loc, const char *name, const H5L_info_t *inf, void *op_data)
HDF object iteration function.
int setd_vec(std::string name, const std::vector< double > &v)
Set vector dataset named name with v.
void set_current_id(hid_t cur)
Set the current working id.
int getf_arr_alloc(std::string name, size_t &n, float *f)
Get a float array named name of size n.
int find_object_by_pattern(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 seti_mat_copy(std::string name, const ubmatrix_int &m)
Set matrix dataset named name with m.
bool has_write_access()
If true, then the file has read and write access.
hid_t open_group(hid_t init_id, std::string path)
Open a group relative to the location specified in init_id.
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 compr_type
Compression type (support experimental)
int get_szt_vec(std::string name, std::vector< size_t > &v)
Get vector dataset and place data in v.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).