Go to the documentation of this file.
23 #ifndef O2SCL_HDF_IO_H
24 #define O2SCL_HDF_IO_H
29 #include <boost/numeric/ublas/vector.hpp>
33 #include <o2scl/hdf_file.h>
34 #include <o2scl/table.h>
35 #include <o2scl/table_units.h>
36 #include <o2scl/hist.h>
37 #include <o2scl/hist_2d.h>
38 #include <o2scl/table3d.h>
39 #include <o2scl/tensor_grid.h>
40 #include <o2scl/expval.h>
41 #include <o2scl/contour.h>
42 #include <o2scl/uniform_grid.h>
43 #include <o2scl/prob_dens_mdim_amr.h>
52 template<
class vec_t,
class mat_t>
58 if (name.length()==0) {
60 if (name.length()==0) {
61 O2SCL_ERR2(
"No object of type prob_dens_mdim_amr found in ",
72 std::vector<double> data;
73 std::vector<size_t> insides;
95 template<
class vec_t,
class mat_t>
101 O2SCL_ERR2(
"File not opened with write access in hdf_output",
102 "(hdf_file,prob_dens_mdim_amr<>,string).",
112 hf.
sets_fixed(
"o2scl_type",
"prob_dens_mdim_amr");
115 std::vector<double> data;
116 std::vector<size_t> insides;
137 void hdf_output(hdf_file &hf,
o2scl::table<> &t, std::string name);
139 #ifndef O2SCL_NO_HDF_INPUT
147 template<
class vec_t>
151 if (name.length()==0) {
153 if (name.length()==0) {
154 O2SCL_ERR2(
"No object of type table found in ",
185 template<
class vec_t>
196 if (type2!=
"table") {
197 O2SCL_ERR2(
"Typename in HDF group does not match ",
202 std::vector<std::string> cnames, cols;
209 if (cnames.size()!=cvalues.size()) {
210 O2SCL_ERR2(
"Size mismatch between constant names and values ",
213 for(
size_t i=0;i<cnames.size();i++) {
219 for(
size_t i=0;i<cols.size();i++) {
225 hf.
geti(
"nlines",nlines2);
239 ubvector vtmp(nlines2);
241 for(
int j=0;j<nlines2;j++) {
271 template<
class vec_t>
276 if (name.length()==0) {
278 if (name.length()==0) {
279 O2SCL_ERR2(
"No object of type table found in ",
309 template<
class vec_t>
321 hf.
geti(
"unit_flag",uf);
325 std::vector<std::string> units;
327 for(
size_t i=0;i<units.size();i++) {
328 t.
set_unit(t.get_column_name(i),units[i]);
336 void hdf_output(hdf_file &hf,
o2scl::hist &h, std::string name);
340 void hdf_output(hdf_file &hf,
const o2scl::hist_2d &h, std::string name);
344 void hdf_output(hdf_file &hf,
const o2scl::table3d &h, std::string name);
352 std::string name=
"");
368 std::string name=
"");
370 void hdf_output(hdf_file &hf,
const std::vector<o2scl::contour_line> &cl,
373 void hdf_input(hdf_file &hf, std::vector<o2scl::contour_line> &cl,
374 std::string name=
"");
376 void hdf_output(hdf_file &hf,
const std::vector<o2scl::edge_crossings> &ec,
379 void hdf_input(hdf_file &hf, std::vector<o2scl::edge_crossings> &ec,
380 std::string name=
"");
383 std::vector<size_t> > &t, std::string name);
386 std::vector<size_t> > &t, std::string name=
"");
400 int verbose=0,
bool err_on_fail=
true);
421 bool err_on_fail=
true) {
424 std::cout <<
"Function vector_spec is parsing: " << spec << std::endl;
427 if (spec.find(
"val:")==0) {
429 std::string temp=spec.substr(4,spec.length()-4);
431 std::cout <<
"vector_spec(): single value " << temp
437 }
else if (spec.find(
"list:")==0) {
440 std::string list=spec.substr(5,spec.length()-5);
441 std::vector<std::string> sv;
446 O2SCL_ERR2(
"String split failed, spec empty? ",
453 std::cout <<
"vector_spec(): List " << list << std::endl;
454 std::cout << n <<
" " << sv[0] <<
" " << sv[n-1] << std::endl;
457 for(
size_t i=0;i<n;i++) {
461 }
else if (spec.find(
"func:")==0) {
465 std::cout <<
"vector_spec(): Function " << spec << std::endl;
467 std::string temp=spec.substr(5,spec.length()-5);
468 size_t ncolon=temp.find(
':');
469 if (ncolon==std::string::npos) {
471 O2SCL_ERR2(
"Function specified but no array length specified ",
488 std::cout <<
"Size " << n << std::endl;
490 if (temp.length()<ncolon+1) {
498 std::string func=temp.substr(ncolon+1,temp.length()-ncolon-1);
500 std::cout <<
"Function " << func << std::endl;
503 std::map<std::string,double> vars;
504 calc.
compile(func.c_str(),&vars);
506 for(
size_t i=0;i<n;i++) {
507 vars[
"i"]=((double)i);
508 v[i]=calc.
eval(&vars);
511 }
else if (spec.find(
"grid:")==0) {
515 std::cout <<
"vector_spec(): Grid " << spec << std::endl;
518 std::string temp=spec.substr(5,spec.length()-5);
520 std::vector<std::string> sv;
524 O2SCL_ERR2(
"Not enough information for grid ",
531 std::cout <<
"Begin,end,width "
532 << sv[0] <<
" " << sv[1] <<
" " << sv[2] << std::endl;
534 if (sv.size()>=4 && sv[3]==
"log") {
548 }
else if (spec.find(
"text:")==0) {
552 std::cout <<
"vector_spec(): Text " << spec << std::endl;
555 std::vector<std::string> sv;
560 O2SCL_ERR2(
"Not enough information for text file ",
570 std::cout <<
"Filename,column " << sv[1] <<
" " << col << std::endl;
574 O2SCL_ERR2(
"Column is negative for text file ",
583 fin.open(sv[1].c_str());
586 std::string line, word;
590 std::istringstream is(line);
592 for(;i<=col && (is >> word) && in_header;i++) {
596 if (i==col && verbose>2) {
598 std::cout <<
"Word: " << word <<
" header." << std::endl;
600 std::cout <<
"Word: " << word <<
" start of data."
605 }
while (in_header && !fin.eof());
611 O2SCL_ERR2(
"Couldn't find a number in text file ",
619 std::vector<double> tempv;
620 bool end_of_data=
false;
625 std::istringstream is(line);
626 for(
int i=0;i<=col;i++) {
634 }
while (!end_of_data && !fin.eof());
640 v.resize(tempv.size());
641 for(
size_t i=0;i<tempv.size();i++) {
645 }
else if (spec.find(
"hdf5:")==0) {
649 std::cout <<
"vector_spec(): HDF5 file " << spec << std::endl;
651 std::string temp=spec.substr(5,spec.length()-5);
652 size_t ncolon=temp.find(
':');
653 if (ncolon==std::string::npos) {
655 O2SCL_ERR2(
"No apparent object name specified ",
661 std::string fname=temp.substr(0,ncolon);
663 std::cout <<
"Filename " << fname << std::endl;
665 if (temp.length()<ncolon+1) {
667 O2SCL_ERR2(
"No apparent object name specified ",
673 std::string obj_name=temp.substr(ncolon+1,temp.length()-ncolon-1);
674 std::string addl_spec;
675 ncolon=obj_name.find(
':');
676 if (ncolon!=std::string::npos) {
677 addl_spec=obj_name.substr(ncolon+1,obj_name.length()-ncolon-1);
678 obj_name=obj_name.substr(0,ncolon);
681 std::cout <<
"Object name " << obj_name << std::endl;
682 std::cout <<
"Additional specification " << addl_spec << std::endl;
686 std::string fname_old=fname;
687 std::vector<std::string> matches;
689 if (matches.size()>1 || matches.size()==0 || wret!=0) {
691 O2SCL_ERR2(
"Function wordexp_wrapper() failed ",
699 std::cout <<
"Filename after wordexp() " << fname << std::endl;
714 std::cout <<
"Object type from file: " << type << std::endl;
718 if (addl_spec.length()==0) {
728 v.resize(t.get_nlines());
729 for(
size_t i=0;i<t.get_nlines();i++) {
730 v[i]=t.get(addl_spec,i);
732 }
else if (type==
"double[]") {
733 std::vector<double> vtemp;
735 v.resize(vtemp.size());
736 for(
size_t i=0;i<v.size();i++) {
739 }
else if (type==
"hist") {
744 v.resize(wgts.size());
745 for(
size_t i=0;i<v.size();i++) {
748 }
else if (type==
"int[]") {
749 std::vector<int> vtemp;
751 v.resize(vtemp.size());
752 for(
size_t i=0;i<v.size();i++) {
755 }
else if (type==
"size_t[]") {
756 std::vector<size_t> vtemp;
758 v.resize(vtemp.size());
759 for(
size_t i=0;i<v.size();i++) {
762 }
else if (type==
"uniform_grid<double>") {
765 std::vector<double> vtemp;
767 v.resize(vtemp.size());
768 for(
size_t i=0;i<v.size();i++) {
771 }
else if (type==
"int") {
773 hf.
geti(obj_name,itemp);
776 }
else if (type==
"double") {
778 hf.
getd(obj_name,dtemp);
781 }
else if (type==
"size_t") {
792 std::cout <<
"Could not understand prefix in vector_spec()."
811 bool err_on_fail=
true) {
814 std::cout <<
"Function strings_spec is parsing: " << spec << std::endl;
817 if (spec.find(
"list:")==0) {
820 std::string list=spec.substr(5,spec.length()-5);
821 std::vector<std::string> sv;
826 O2SCL_ERR2(
"String split failed, spec empty? ",
833 std::cout <<
"strings_spec(): List " << list << std::endl;
834 std::cout << n <<
" " << sv[0] <<
" " << sv[n-1] << std::endl;
837 for(
size_t i=0;i<n;i++) {
841 }
else if (spec.find(
"shell:")==0) {
847 std::string cmd=spec.substr(6,spec.length()-6);
848 std::cout <<
"Using shell command: " << cmd << std::endl;
849 FILE *ps_pipe=popen(cmd.c_str(),
"r");
853 "convert_units::convert_gnu_units().",
860 char *cret=fgets(line1,255,ps_pipe);
863 std::string sline1=line1;
864 if (sline1[sline1.length()-1]==
'\n') {
865 sline1=sline1.substr(0,sline1.length()-1);
868 std::cout <<
"Read line "
869 << sline1 << std::endl;
872 cret=fgets(line1,255,ps_pipe);
875 if (pclose(ps_pipe)!=0) {
887 }
else if (spec.find(
"hdf5:")==0) {
891 std::cout <<
"strings_spec(): HDF5 file " << spec << std::endl;
893 std::string temp=spec.substr(5,spec.length()-5);
894 size_t ncolon=temp.find(
':');
895 if (ncolon==std::string::npos) {
897 O2SCL_ERR2(
"No apparent object name specified ",
903 std::string fname=temp.substr(0,ncolon);
905 std::cout <<
"Filename " << fname << std::endl;
907 if (temp.length()<ncolon+1) {
909 O2SCL_ERR2(
"No apparent object name specified ",
915 std::string obj_name=temp.substr(ncolon+1,temp.length()-ncolon-1);
916 std::string addl_spec;
917 ncolon=obj_name.find(
':');
918 if (ncolon!=std::string::npos) {
919 addl_spec=obj_name.substr(ncolon+1,obj_name.length()-ncolon-1);
920 obj_name=obj_name.substr(0,ncolon);
923 std::cout <<
"Object name " << obj_name << std::endl;
924 std::cout <<
"Additional specification " << addl_spec << std::endl;
928 std::string fname_old=fname;
929 std::vector<std::string> matches;
931 if (matches.size()>1 || matches.size()==0 || wret!=0) {
933 O2SCL_ERR2(
"Function wordexp_wrapper() failed ",
941 std::cout <<
"Filename after wordexp() " << fname << std::endl;
956 std::cout <<
"Object type from file: " << type << std::endl;
960 if (addl_spec.length()==0) {
970 v.resize(t.get_nlines());
971 for(
size_t i=0;i<t.get_nlines();i++) {
972 v[i]=t.get(addl_spec,i);
1002 std::vector<vec_t> &v,
1004 bool err_on_fail=
true) {
1006 if (spec.find(
"func:")==0) {
1010 std::cout <<
"mult_vector_spec(): Function " << spec << std::endl;
1012 std::string temp=spec.substr(5,spec.length()-5);
1013 std::vector<std::string> sv;
1017 O2SCL_ERR2(
"Less than three parts for function type ",
1035 std::cout <<
"Size " << n << std::endl;
1040 for(
size_t i=0;i<n;i++) {
1053 std::cout <<
"Size of " << n <<
" is " << n2 << std::endl;
1057 std::cout <<
"Function " << sv[2] << std::endl;
1061 std::map<std::string,double> vars;
1062 calc.
compile(sv[2].c_str(),&vars);
1065 for(
size_t j=0;j<n2;j++) {
1066 vars[
"i"]=((double)i);
1067 vars[
"j"]=((double)j);
1068 v[i][j]=calc.
eval(&vars);
1073 }
else if (spec.find(
"text:")==0) {
1077 std::cout <<
"mult_vector_spec(): Text " << spec << std::endl;
1080 std::vector<std::string> sv;
1085 O2SCL_ERR2(
"Not enough information for text file ",
1092 std::string col_list=sv[2];
1094 std::cout <<
"Filename,column list: " << sv[1] <<
" "
1095 << col_list << std::endl;
1098 std::vector<size_t> col_list2;
1100 o2scl::vector_sort<std::vector<size_t>,
size_t>(col_list2.size(),
1103 if (col_list2.size()<1) {
1112 bool in_header=
true;
1114 std::string line, word;
1115 fin.open(sv[1].c_str());
1117 std::cout <<
"Unfinished in mult_vector_spec." << std::endl;
1175 }
else if (spec.find(
"hdf5:")==0) {
1179 std::cout <<
"mult_vector_spec(): HDF5 file " << spec << std::endl;
1181 std::string temp=spec.substr(5,spec.length()-5);
1182 size_t ncolon=temp.find(
':');
1183 if (ncolon==std::string::npos) {
1185 O2SCL_ERR2(
"No apparent file name specified ",
1191 std::string fname=temp.substr(0,ncolon);
1193 std::cout <<
"Filename " << fname << std::endl;
1195 if (temp.length()<ncolon+1) {
1197 O2SCL_ERR2(
"No apparent object name specified ",
1203 std::string obj_name=temp.substr(ncolon+1,temp.length()-ncolon-1);
1204 std::string addl_spec;
1205 ncolon=obj_name.find(
':');
1206 if (ncolon!=std::string::npos) {
1207 addl_spec=obj_name.substr(ncolon+1,obj_name.length()-ncolon-1);
1208 obj_name=obj_name.substr(0,ncolon);
1211 std::cout <<
"Object name " << obj_name << std::endl;
1212 std::cout <<
"Additional specification " << addl_spec << std::endl;
1216 std::string fname_old=fname;
1217 std::vector<std::string> matches;
1219 if (matches.size()==0 || wret!=0) {
1221 O2SCL_ERR2(
"Function wordexp_wrapper() failed ",
1228 size_t nfiles=matches.size();
1230 std::cout <<
"Wordexp() found " << nfiles <<
" files."
1234 for(
size_t ifile=0;ifile<nfiles;ifile++) {
1235 fname=matches[ifile];
1237 std::cout <<
"Filename for index " << ifile <<
" is " << fname
1253 std::cout <<
"Object type from file: " << type << std::endl;
1256 if (type==
"table") {
1257 if (addl_spec.length()==0) {
1259 O2SCL_ERR2(
"No table column name specified ",
1268 for(
size_t j=0;j<t.get_ncolumns();j++) {
1269 if (fnmatch(addl_spec.c_str(),
1270 t.get_column_name(j).c_str(),0)==0) {
1272 std::cout <<
"Column " << t.get_column_name(j)
1273 <<
" matches pattern " << addl_spec
1274 <<
"." << std::endl;
1276 vec_t vtemp(t.get_nlines());
1277 for(
size_t k=0;k<t.get_nlines();k++) {
1278 vtemp[k]=t.get(j,k);
1283 }
else if (type==
"double[]") {
1284 std::vector<double> vtemp;
A one-dimensional histogram class.
int vector_spec(std::string spec, vec_t &v, int verbose=0, bool err_on_fail=true)
A vector specified by a string.
int get_szt(std::string name, size_t &u)
Get an unsigned integer named name.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
int string_to_uint_list(const std::string &x, size_vec_t &list)
Convert a string-based list of unsigned integers to a list.
void set_nlines(size_t il)
Set the number of lines.
void new_column(std::string head)
Add a new column owned by the table table .
@ exc_efailed
generic failure
size_t itype
Current interpolation type.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
int stoi(std::string s)
Convert a string to an integer.
hid_t get_current_id()
Retrieve the current working id.
void set_unit(std::string scol, std::string unit)
Set the unit for column scol to unit.
int wordexp_wrapper(std::string word, std::vector< std::string > &matches)
Wrapper for the wordexp() function.
void set_from_vectors(size_t &nd, size_t &dc, size_t &ms, const std::vector< double > &data, const std::vector< size_t > &insides)
Set the object from data specified as three size_t numbers and a set of two vectors.
int mult_vector_spec(std::string spec, std::vector< vec_t > &v, int verbose=0, bool err_on_fail=true)
A list of vectors specified by a string.
double stod(std::string s)
Convert a string to a double.
int geti(std::string name, int &i)
Get a integer named name.
void set(std::string scol, size_t row, double val)
Set row row of column named col to value val . .
int geti_vec(std::string name, std::vector< int > &v)
Get vector dataset and place data in v.
void close()
Close the file.
int getd_vec(std::string name, std::vector< double > &v)
Get vector dataset and place data in v.
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
Scalar expectation value.
const ubvector & get_wgts() const
Get a reference to the full y vector.
void compile(const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
Compile expression expr using variables specified in vars and return an integer to indicate success o...
Data table table class with units.
int open(std::string fname, bool write_access=false, bool err_on_fail=true)
Open a file named fname.
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.
@ exc_einval
invalid argument supplied by user
Evaluate a mathematical expression in a string.
virtual void add_constant(std::string name, double val)
Add a constant, or if the constant already exists, change its value.
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....
Vector expectation value.
Matrix expectation value.
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
Tensor class with arbitrary dimensions with a grid.
void set_szt(std::string name, size_t u)
Set an unsigned integer named name to value u.
void copy_to_vectors(size_t &nd, size_t &dc, size_t &ms, std::vector< double > &data, std::vector< size_t > &insides)
Copy the object data to three size_t numbers and two vectors.
int stoszt_nothrow(std::string s, size_t &result)
Convert a string to a size_t without throwing an exception.
size_t get_ncolumns() const
Return the number of columns.
@ exc_efilenotfound
File not found.
Probability distribution from an adaptive mesh created using a matrix of points.
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
void check_synchro() const
Check if the tree and list are properly synchronized.
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 gets_vec(std::string name, std::vector< std::string > &s)
Get a vector of strings named name and store it in s.
@ itp_cspline
Cubic spline for natural boundary conditions.
int get_szt_def(std::string name, size_t def, size_t &i)
Get a size_t named name.
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
int getd_vec_copy(std::string name, vec_t &v)
Get vector dataset and place data in v.
double function_to_double(std::string s)
Convert a formula to a double.
int split_string_delim(std::string str, std::vector< std::string > &list, char delim)
Split a string into parts using a delimiter.
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 value_spec(std::string spec, double &d, int verbose=0, bool err_on_fail=true)
A value specified by a string.
int set_szt_vec(std::string name, const std::vector< size_t > &v)
Set vector dataset named name with v.
virtual void clear_table()
Clear the table and the column names (but leave constants)
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.
bool has_write_access()
If true, then the file has read and write access.
void clear_constants()
CLear all constants.
hid_t open_group(hid_t init_id, std::string path)
Open a group relative to the location specified in init_id.
int strings_spec(std::string spec, vec_t &v, int verbose=0, bool err_on_fail=true)
Desc.
A data structure containing one or more slices of two-dimensional data points defined on a grid.
std::string get_column_name(size_t icol) const
Returns the name of column col .
A two-dimensional histogram class.
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).