23 #ifndef O2SCL_TABLE_UNITS_H 24 #define O2SCL_TABLE_UNITS_H 30 #include <o2scl/table.h> 31 #include <o2scl/lib_settings.h> 38 #ifndef DOXYGEN_NO_O2NS 70 #ifndef DOXYGEN_NO_O2NS 84 template<
class vec_t=std::vector<
double> >
89 #ifdef O2SCL_NEVER_DEFINED 116 this->maxlines=this->nlines;
124 s.
dat.resize(this->nlines);
125 s.
index=this->atree.size();
126 this->atree.insert(make_pair(cname,s));
130 this->alist.push_back(it);
133 utree.insert(make_pair(cname,t.
get_unit(cname)));
137 it->second.dat[j]=t.
get(cname,j);
142 this->intp_set=
false;
161 this->maxlines=this->nlines;
169 s.
dat.resize(this->nlines);
170 s.
index=this->atree.size();
171 this->atree.insert(make_pair(cname,s));
175 this->alist.push_back(it);
179 it->second.dat[j]=t.
get(cname,j);
184 this->intp_set=
false;
208 this->maxlines=this->nlines;
216 s.
dat.resize(this->nlines);
217 s.
index=this->atree.size();
218 this->atree.insert(make_pair(cname,s));
222 this->alist.push_back(it);
225 utree.insert(make_pair(cname,t.
get_unit(cname)));
229 it->second.dat[j]=t.
get(cname,j);
234 if (this->intp_set) {
235 this->intp_set=
false;
263 this->maxlines=this->nlines;
271 s.
dat=
new vec_t(this->nlines);
272 s.
index=this->atree.size();
273 this->atree.insert(make_pair(cname,s));
277 this->alist.push_back(it);
281 (*it->second.dat)[j]=t.
get(cname,j);
286 if (this->intp_set) {
287 this->intp_set=
false;
311 template<
class vec2_t>
315 for(
size_t i=0;i<this->get_ncolumns();i++) {
316 std::string cname=this->get_column_name(i);
320 dest.
set_unit(cname,get_unit(cname));
324 for(
size_t i=0;i<this->get_nlines();i++) {
325 double val=this->row_function(func,i);
327 this->set_nlines_auto(new_lines+1);
328 for(
size_t j=0;j<this->get_ncolumns();j++) {
329 std::string cname=this->get_column_name(j);
330 dest.
set(cname,new_lines,this->
get(cname,i));
344 uciter it=utree.find(scol);
345 if (it==utree.end()) {
348 if (at==this->atree.
end()) {
349 O2SCL_ERR((((std::string)
"Column '")+scol+
350 "' not found in table_units::get_unit().").c_str(),
365 std::istringstream is(unit_line);
367 while(is >> unitval) {
368 if (unitval!=std::string(
".")) {
369 this->set_unit(this->get_column_name(icol),unitval);
382 return get_unit(this->get_column_name(i));
388 uiter it=utree.find(scol);
389 if (it==utree.end()) {
390 O2SCL_ERR((((std::string)
"Column '")+scol+
391 "' not found in table_units::get_unit().").c_str(),
395 if (utree.size()==0) {
396 O2SCL_ERR(
"No units specified in table_units::remove_unit().",
405 void set_unit(std::string scol, std::string unit) {
407 uiter it=utree.find(scol);
408 if (it==utree.end()) {
410 if (at==this->atree.
end()) {
411 O2SCL_ERR((((std::string)
"Column '")+scol+
412 "' not found in table_units::set_unit().").c_str(),
415 utree.insert(make_pair(scol,unit));
425 bool err_on_fail=
true) {
428 uiter it=utree.find(scol);
429 if (it==utree.end()) {
431 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
432 "table_units::convert_to_unit().").c_str(),
440 if (it->second==unit)
return success;
444 if (at==this->atree.
end()) {
446 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
447 "table_units::convert_to_unit().").c_str(),
455 vec_t &vec=at->second.dat;
456 double conv=
cup->convert(it->second,unit,1.0);
458 for(
size_t i=0;i<this->get_nlines();i++) {
505 if (this->intp_set==
true) {
507 this->intp_set=
false;
517 if (it==this->atree.
end()) {
518 O2SCL_ERR((((std::string)
"Column '")+scol+
519 " not found in table_units::delete_column().").c_str(),
525 if (get_unit(scol).length()>0) remove_unit(scol);
529 vit+=it->second.index;
534 this->alist[this->alist.size()-1]->second.index=it->second.index;
537 this->atree.erase(it);
538 this->alist.erase(vit);
550 std::string unit=get_unit(src);
557 virtual void summary(std::ostream *out,
size_t ncol=79)
const {
559 if (this->constants.size()==1) {
560 (*out) <<
"1 constant:" << std::endl;
562 (*out) << this->constants.size() <<
" constants:" << std::endl;
564 std::map<std::string,double>::const_iterator mit;
565 for(mit=this->constants.begin();mit!=this->constants.end();mit++) {
566 (*out) << mit->first <<
" " << mit->second << std::endl;
570 size_t nh=this->get_ncolumns(), nh2;
574 (*out) <<
"No columns." << std::endl;
579 (*out) <<
"1 column: " << std::endl;
581 (*out) << nh <<
" columns: " << std::endl;
583 std::string *h=
new std::string[nh];
584 for(
size_t i=0;i<nh;i++) {
585 h[i]=
szttos(i)+
". "+this->get_column_name(i)+
" ["+
586 get_unit(this->get_column_name(i))+
"]";
589 std::vector<std::string> h2;
595 for(
size_t i=0;i<nh2;i++) {
596 (*out) << h2[i] << std::endl;
603 if (this->get_nlines()==0) {
604 (*out) <<
"No lines of data." << std::endl;
605 }
else if (this->get_nlines()==1) {
606 (*out) <<
"One line of data." << std::endl;
608 (*out) << this->get_nlines() <<
" lines of data." << std::endl;
616 virtual const char *
type() {
return "table_units"; }
627 if (!this->is_column(dest)) this->new_column(dest);
629 typedef typename std::map<std::string,
632 aiter2 its=this->atree.find(src);
633 if (its==this->atree.end()) {
634 O2SCL_ERR((((std::string)
"Column '")+src+
635 " not found in table_units::copy_column().").c_str(),
639 aiter2 itd=this->atree.find(dest);
640 if (itd==this->atree.end()) {
641 O2SCL_ERR((((std::string)
"Destination column '")+dest+
642 " not found in table_units::copy_column().").c_str(),
646 this->set_unit(dest,this->get_unit(src));
647 for(
size_t i=0;i<this->nlines;i++) {
648 itd->second.dat[i]=its->second.dat[i];
659 std::istringstream *is;
662 std::vector<std::string> onames, nnames;
664 is=
new std::istringstream(line);
665 while ((*is) >> stemp) {
666 onames.push_back(stemp);
668 std::cout <<
"Read possible column name: " << stemp << std::endl;
675 for(
size_t i=0;i<onames.size();i++) {
681 if (n_nums==onames.size()) {
684 std::cout <<
"First row looks like it contains numerical values." 686 std::cout <<
"Creating generic column names: ";
689 for(
size_t i=0;i<onames.size();i++) {
690 nnames.push_back(((std::string)
"c")+
szttos(i+1));
691 if (verbose>0) std::cout << nnames[i] <<
" ";
694 if (verbose>0) std::cout << std::endl;
697 for(
size_t i=0;i<nnames.size();i++) {
698 this->new_column(nnames[i]);
702 this->set_nlines_auto(irow+1);
703 for(
size_t i=0;i<onames.size();i++) {
711 for(
size_t i=0;i<onames.size();i++) {
712 std::string temps=onames[i];
713 this->make_fp_varname(temps);
714 this->make_unique_name(temps,nnames);
715 nnames.push_back(temps);
716 if (temps!=onames[i] && verbose>0) {
717 std::cout <<
"Converted column named '" << onames[i] <<
"' to '" 718 << temps <<
"'." << std::endl;
723 for(
size_t i=0;i<nnames.size();i++) {
724 this->new_column(nnames[i]);
728 std::vector<std::string> units;
730 is=
new std::istringstream(line);
732 while ((*is) >> stemp) {
733 units.push_back(stemp);
734 if (stemp[0]==
'[') num_units++;
736 std::cout <<
"Read word in second row: " << stemp << std::endl;
741 if (units.size()!=nnames.size()) {
742 std::cout <<
"Second row appears not to have same number of " 743 <<
"entries as the first." << std::endl;
744 std::cout <<
"Aborting." << std::endl;
748 if (num_units==((
int)units.size()) || num_units>2) {
750 std::cout <<
"Looks like second row contains units." << std::endl;
752 for(
size_t i=0;i<units.size();i++) {
755 if (stemp[0]==
'[') stemp=stemp.substr(1,stemp.length()-1);
756 if (stemp[stemp.length()-1]==
']') {
757 stemp=stemp.substr(0,stemp.length()-1);
760 set_unit(nnames[i],stemp);
762 std::cout <<
"Name,unit: " << nnames[i] <<
" [" << stemp <<
"]" 770 this->set_nlines_auto(1);
771 for(
size_t i=0;i<units.size();i++) {
781 while ((fin) >> data) {
782 this->set_nlines_auto(irow+1);
783 this->
set(0,irow,data);
784 for(
size_t i=1;i<this->get_ncolumns();i++) {
786 this->
set(i,irow,data);
797 template<
class vec2_t>
799 bool allow_extrap=
true, std::string dest_index=
"") {
801 if (dest_index==
"") dest_index=src_index;
804 double min=source.
min(src_index);
805 double max=source.
max(src_index);
806 if (allow_extrap==
false) {
807 if (!std::isfinite(min) || !std::isfinite(max)) {
808 O2SCL_ERR2(
"Minimum or maximum of source index not finite ",
809 "in table_units::insert_table().",
exc_einval);
814 std::vector<std::string> col_list;
817 if (col!=src_index && col!=dest_index &&
818 this->is_column(col)==
false) {
819 col_list.push_back(col);
824 for(
size_t i=0;i<col_list.size();i++) {
825 this->new_column(col_list[i]);
826 set_unit(col_list[i],source.
get_unit(col_list[i]));
827 for(
size_t j=0;j<this->get_nlines();j++) {
828 double val=this->
get(dest_index,j);
829 if (allow_extrap || (val>=min && val<=max)) {
830 this->
set(col_list[i],j,source.
interp(src_index,val,col_list[i]));
840 friend void o2scl_hdf::hdf_output
846 friend void o2scl_hdf::hdf_output_data
854 #ifndef DOXYGEN_INTERNAL 864 typedef std::map<std::string,std::string,
865 std::greater<std::string> >::iterator uiter;
866 typedef std::map<std::string,std::string,
867 std::greater<std::string> >::const_iterator uciter;
871 std::map<std::string,std::string,std::greater<std::string> >
utree;
882 template<
class vec_t>
887 std::vector<double> dbuffer;
901 dbuffer.push_back(val);
904 ibuffer=cbuffer.length();
907 MPI_Send(&ibuffer,1,MPI_INT,dest_rank,
910 MPI_Send(&(cbuffer[0]),cbuffer.length(),MPI_CHAR,dest_rank,
913 MPI_Send(&(dbuffer[0]),dbuffer.size(),MPI_DOUBLE,dest_rank,
929 ibuffer=cbuffer.length();
931 MPI_Send(&ibuffer,1,MPI_INT,dest_rank,
934 MPI_Send(&(cbuffer[0]),cbuffer.length(),MPI_CHAR,dest_rank,
949 ibuffer=cbuffer.length();
951 MPI_Send(&ibuffer,1,MPI_INT,dest_rank,
954 MPI_Send(&(cbuffer[0]),cbuffer.length(),MPI_CHAR,dest_rank,
963 MPI_Send(&ibuffer,1,MPI_INT,dest_rank,
971 MPI_Send(&ibuffer,1,MPI_INT,dest_rank,
976 MPI_Send(&(t[i][0]),t.
get_nlines(),MPI_DOUBLE,dest_rank,
986 template<
class vec_t>
987 void o2scl_table_mpi_recv(
size_t src_rank,
992 std::vector<char> cbuffer;
993 std::vector<double> dbuffer;
995 std::vector<std::string> names;
1003 MPI_Recv(&ibuffer,1,MPI_INT,src_rank,tag,MPI_COMM_WORLD,
1005 cbuffer.resize(ibuffer);
1007 MPI_Recv(&(cbuffer[0]),ibuffer,MPI_CHAR,src_rank,tag,MPI_COMM_WORLD,
1011 for(
size_t i=0;i<cbuffer.size();i++) {
1012 if (cbuffer[i]!=
' ') {
1015 if (stemp.size()>0) {
1016 names.push_back(stemp);
1021 if (stemp.size()>0) {
1022 names.push_back(stemp);
1027 dbuffer.resize(names.size());
1030 MPI_Recv(&(dbuffer[0]),dbuffer.size(),MPI_DOUBLE,
1031 src_rank,tag,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
1034 for(
size_t i=0;i<names.size();i++) {
1044 MPI_Recv(&ibuffer,1,MPI_INT,src_rank,tag,MPI_COMM_WORLD,
1046 cbuffer.resize(ibuffer);
1048 MPI_Recv(&(cbuffer[0]),ibuffer,MPI_CHAR,src_rank,tag,MPI_COMM_WORLD,
1052 for(
size_t i=0;i<cbuffer.size();i++) {
1053 if (cbuffer[i]!=
' ') {
1056 if (stemp.size()>0) {
1057 names.push_back(stemp);
1062 if (stemp.size()>0) {
1063 names.push_back(stemp);
1067 for(
size_t i=0;i<names.size();i++) {
1076 MPI_Recv(&ibuffer,1,MPI_INT,src_rank,tag,MPI_COMM_WORLD,
1078 cbuffer.resize(ibuffer);
1080 MPI_Recv(&(cbuffer[0]),ibuffer,MPI_CHAR,src_rank,tag,MPI_COMM_WORLD,
1084 for(
size_t i=0;i<cbuffer.size();i++) {
1085 if (cbuffer[i]!=
' ') {
1088 if (stemp.size()>0) {
1089 names.push_back(stemp);
1094 if (stemp.size()>0) {
1095 names.push_back(stemp);
1099 for(
size_t i=0;i<names.size();i++) {
1108 MPI_Recv(&ibuffer,1,MPI_INT,src_rank,tag,MPI_COMM_WORLD,
1116 MPI_Recv(&ibuffer,1,MPI_INT,src_rank,tag,MPI_COMM_WORLD,
1123 MPI_Recv(&(v[0]),ibuffer,MPI_DOUBLE,src_rank,
1124 tag,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
1133 #ifndef DOXYGEN_NO_O2NS table_units(int cmaxlines=0)
Create a new table_units with space for nlines<=cmaxlines.
double max(std::string scol) const
Return column maximum. Makes no assumptions about ordering, .
double get(std::string scol, size_t row) const
Get value from row row of column named col. .
void remove_unit(std::string scol)
Remove the unit for column scol.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
size_t get_nlines() const
Return the number of lines.
double min(std::string scol) const
Return column minimum. Makes no assumptions about ordering, .
virtual void clear()
Clear everything.
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
void hdf_input(hdf_file &hf, o2scl::table_units< vec_t > &t, std::string name)
Input a o2scl::table_units object from a hdf_file.
virtual int read_generic(std::istream &fin, int verbose=0)
Clear the current table and read from a generic data file.
void set_nlines(size_t il)
Set the number of lines.
virtual void delete_column(std::string scol)
Delete column named scol.
virtual const char * type()
Return the type, "table_units".
sanity check failed - shouldn't happen
lib_settings_class o2scl_settings
The global library settings object.
invalid argument supplied by user
virtual size_t get_nconsts() const
Get the number of constants.
std::map< std::string, col, std::greater< std::string > >::const_iterator aciter
Const map iterator type.
Generic "not found" result.
int convert_to_unit(std::string scol, std::string unit, bool err_on_fail=true)
Convert the units of column scol to unit.
size_t get_interp_type() const
Get the interpolation type.
void line_of_units(std::string unit_line)
Specify the units as a string separated by spaces.
virtual double get_constant(std::string name) const
Get a constant.
virtual void summary(std::ostream *out, size_t ncol=79) const
Output a summary of the information stored.
void insert_table(table_units< vec2_t > &source, std::string src_index, bool allow_extrap=true, std::string dest_index="")
Insert columns from a source table into the new table by interpolation (or extrapolation) ...
virtual void rename_column(std::string src, std::string dest)
Rename column named src to dest .
std::map< std::string, double > constants
The list of constants.
void set_unit(std::string scol, std::string unit)
Set the unit for column scol to unit.
size_t get_maxlines()
Return the maximum number of lines before a reallocation is required.
double interp(std::string sx, double x0, std::string sy)
Interpolate value x0 from column named sx into column named sy.
convert_units * cup
The pointer to the convert units object.
size_t get_nunits()
Return the number of columns with units.
table_units & operator=(const table_units &t)
Copy with operator= from table_units.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Column structure for table [protected].
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
void hdf_input_data(hdf_file &hf, o2scl::table_units< vec_t > &t)
Internal function for inputting a o2scl::table_units object.
virtual void clear_table()
Clear the table and the column names and units (but leave constants)
std::map< std::string, std::string, std::greater< std::string > > utree
Unit map.
convert_units & get_convert_units()
Get the global convert_units object.
void screenify(size_t nin, const string_arr_t &in_cols, std::vector< std::string > &out_cols, size_t max_size=80)
Reformat the columns for output of width size.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
void copy_rows(std::string func, table_units< vec2_t > &dest)
Copy all rows matching a particular condition to a new table.
virtual void add_constant(std::string name, double val)
Add a constant, or if the constant already exists, change its value.
void set_interp_type(size_t interp_type)
Set the base interpolation objects.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
size_t get_ncolumns() const
Return the number of columns.
double stod(std::string s)
Convert a string to a double.
Data table table class with units.
void new_column(std::string head)
Add a new column owned by the table table .
aiter end()
Return the end of the column tree.
bool is_column(std::string scol) const
Return true if scol is a column in the current table table .
table_units(const table< vec_t > &t)
Copy with constructor from table.
table_units & operator=(const table< vec_t > &t)
Copy with operator= from table.
std::map< std::string, col, std::greater< std::string > >::iterator aiter
Map iterator type.
virtual void copy_column(std::string src, std::string dest)
Copy data from column named src to column named dest, creating a new column if necessary ...
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
std::string get_unit(std::string scol) const
Get the unit for column scol.
void set(std::string scol, size_t row, double val)
Set row row of column named col to value val . .
std::string get_unit(size_t i) const
Get the unit for column with index i.
std::string get_column_name(size_t icol) const
Returns the name of column col .
std::string szttos(size_t x)
Convert a size_t to a string.
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
vec_t dat
Pointer to column.
std::vector< aiter >::iterator aviter
Vector iterator type.
virtual void swap_column_data(std::string scol, vec_t &v)
Swap the data in column scol with that in vector v.
table_units(const table_units &t)
Copy with constructor from table_units.
virtual void rename_column(std::string src, std::string dest)
Rename column named src to dest .