23 #ifndef O2SCL_TABLE_UNITS_H 24 #define O2SCL_TABLE_UNITS_H 30 #include <o2scl/table.h> 31 #include <o2scl/lib_settings.h> 33 #ifndef DOXYGEN_NO_O2NS 65 #ifndef DOXYGEN_NO_O2NS 79 template<
class vec_t=std::vector<
double> >
84 #ifdef O2SCL_NEVER_DEFINED 111 this->maxlines=this->nlines;
119 s.
dat.resize(this->nlines);
120 s.
index=this->atree.size();
121 this->atree.insert(make_pair(cname,s));
125 this->alist.push_back(it);
128 utree.insert(make_pair(cname,t.
get_unit(cname)));
132 it->second.dat[j]=t.
get(cname,j);
137 this->intp_set=
false;
148 for(
size_t i=0;i<nc;i++) {
152 this->constants.insert(make_pair(name,val));
160 this->maxlines=this->nlines;
168 s.
dat.resize(this->nlines);
169 s.
index=this->atree.size();
170 this->atree.insert(make_pair(cname,s));
174 this->alist.push_back(it);
178 it->second.dat[j]=t.
get(cname,j);
183 this->intp_set=
false;
196 this->constants.clear();
214 s.
dat.resize(this->nlines);
215 s.
index=this->atree.size();
216 this->atree.insert(make_pair(cname,s));
220 this->alist.push_back(it);
223 utree.insert(make_pair(cname,t.
get_unit(cname)));
227 it->second.dat[j]=t.
get(cname,j);
232 if (this->intp_set) {
233 this->intp_set=
false;
250 this->constants.clear();
255 for(
size_t i=0;i<nc;i++) {
259 this->constants.insert(make_pair(name,val));
274 s.
dat=
new vec_t(this->nlines);
275 s.
index=this->atree.size();
276 this->atree.insert(make_pair(cname,s));
280 this->alist.push_back(it);
284 (*it->second.dat)[j]=t.
get(cname,j);
289 if (this->intp_set) {
290 this->intp_set=
false;
312 template<
class vec2_t>
316 for(
size_t i=0;i<this->get_ncolumns();i++) {
317 std::string cname=this->get_column_name(i);
325 for(
size_t i=0;i<this->get_nlines();i++) {
326 double val=this->row_function(func,i);
328 this->set_nlines_auto(new_lines+1);
329 for(
size_t j=0;j<this->get_ncolumns();j++) {
330 std::string cname=this->get_column_name(j);
331 dest.
set(cname,new_lines);
345 uciter it=utree.find(scol);
346 if (it==utree.end()) {
349 if (at==this->atree.
end()) {
350 O2SCL_ERR((((std::string)
"Column '")+scol+
351 "' not found in table_units::get_unit().").c_str(),
367 return get_unit(this->get_column_name(i));
373 uiter it=utree.find(scol);
374 if (it==utree.end()) {
375 O2SCL_ERR((((std::string)
"Column '")+scol+
376 "' not found in table_units::get_unit().").c_str(),
380 if (utree.size()==0) {
381 O2SCL_ERR(
"No units specified in table_units::remove_unit().",
390 void set_unit(std::string scol, std::string unit) {
392 uiter it=utree.find(scol);
393 if (it==utree.end()) {
395 if (at==this->atree.
end()) {
396 O2SCL_ERR((((std::string)
"Column '")+scol+
397 "' not found in table_units::set_unit().").c_str(),
400 utree.insert(make_pair(scol,unit));
410 bool err_on_fail=
true) {
413 uiter it=utree.find(scol);
414 if (it==utree.end()) {
416 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
417 "table_units::convert_to_unit().").c_str(),
425 if (it->second==unit)
return success;
429 if (at==this->atree.
end()) {
431 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
432 "table_units::convert_to_unit().").c_str(),
440 vec_t &vec=at->second.dat;
441 double conv=
cup->convert(it->second,unit,1.0);
443 for(
size_t i=0;i<this->get_nlines();i++) {
486 if (it==this->atree.
end()) {
487 O2SCL_ERR((((std::string)
"Column '")+scol+
488 " not found in table_units::delete_column().").c_str(),
494 if (get_unit(scol).length()>0) remove_unit(scol);
498 vit+=it->second.index;
503 this->alist[this->alist.size()-1]->second.index=it->second.index;
506 this->atree.erase(it);
507 this->alist.erase(vit);
519 std::string unit=get_unit(src);
526 virtual void summary(std::ostream *out,
size_t ncol=79)
const {
528 if (this->constants.size()==1) {
529 (*out) <<
"1 constant:" << std::endl;
531 (*out) << this->constants.size() <<
" constants:" << std::endl;
533 std::map<std::string,double>::const_iterator mit;
534 for(mit=this->constants.begin();mit!=this->constants.end();mit++) {
535 (*out) << mit->first <<
" " << mit->second << std::endl;
539 size_t nh=this->get_ncolumns(), nh2;
543 (*out) <<
"No columns." << std::endl;
548 (*out) <<
"1 column: " << std::endl;
550 (*out) << nh <<
" columns: " << std::endl;
552 std::string *h=
new std::string[nh];
553 for(
size_t i=0;i<nh;i++) {
554 h[i]=
szttos(i)+
". "+this->get_column_name(i)+
" ["+
555 get_unit(this->get_column_name(i))+
"]";
558 std::vector<std::string> h2;
564 for(
size_t i=0;i<nh2;i++) {
565 (*out) << h2[i] << std::endl;
572 if (this->get_nlines()==0) {
573 (*out) <<
"No lines of data." << std::endl;
574 }
else if (this->get_nlines()==1) {
575 (*out) <<
"One line of data." << std::endl;
577 (*out) << this->get_nlines() <<
" lines of data." << std::endl;
585 virtual const char *
type() {
return "table_units"; }
596 if (!this->is_column(dest)) this->new_column(dest);
598 typedef typename std::map<std::string,
601 aiter its=this->atree.find(src);
602 if (its==this->atree.end()) {
603 O2SCL_ERR((((std::string)
"Column '")+src+
604 " not found in table_units::copy_column().").c_str(),
608 aiter itd=this->atree.find(dest);
609 if (itd==this->atree.end()) {
610 O2SCL_ERR((((std::string)
"Destination column '")+dest+
611 " not found in table_units::copy_column().").c_str(),
615 this->set_unit(dest,this->get_unit(src));
616 for(
size_t i=0;i<this->nlines;i++) {
617 itd->second.dat[i]=its->second.dat[i];
628 std::istringstream *is;
631 std::vector<std::string> onames, nnames;
633 is=
new std::istringstream(line);
634 while ((*is) >> stemp) {
635 onames.push_back(stemp);
637 std::cout <<
"Read possible column name: " << stemp << std::endl;
644 for(
size_t i=0;i<onames.size();i++) {
650 if (n_nums==onames.size()) {
653 std::cout <<
"First row looks like it contains numerical values." 655 std::cout <<
"Creating generic column names: ";
658 for(
size_t i=0;i<onames.size();i++) {
659 nnames.push_back(((std::string)
"c")+
szttos(i+1));
660 if (verbose>0) std::cout << nnames[i] <<
" ";
663 if (verbose>0) std::cout << std::endl;
666 for(
size_t i=0;i<nnames.size();i++) {
667 this->new_column(nnames[i]);
671 for(
size_t i=0;i<onames.size();i++) {
679 for(
size_t i=0;i<onames.size();i++) {
680 std::string temps=onames[i];
681 this->make_fp_varname(temps);
682 this->make_unique_name(temps,nnames);
683 nnames.push_back(temps);
684 if (temps!=onames[i] && verbose>0) {
685 std::cout <<
"Converted column named '" << onames[i] <<
"' to '" 686 << temps <<
"'." << std::endl;
691 for(
size_t i=0;i<nnames.size();i++) {
692 this->new_column(nnames[i]);
696 std::vector<std::string> units;
698 is=
new std::istringstream(line);
700 while ((*is) >> stemp) {
701 units.push_back(stemp);
702 if (stemp[0]==
'[') num_units++;
704 std::cout <<
"Read word in second row: " << stemp << std::endl;
709 if (units.size()!=nnames.size()) {
710 std::cout <<
"Second row appears not to have same number of " 711 <<
"entries as the first." << std::endl;
712 std::cout <<
"Aborting." << std::endl;
716 if (num_units==((
int)units.size()) || num_units>2) {
718 std::cout <<
"Looks like second row contains units." << std::endl;
720 for(
size_t i=0;i<units.size();i++) {
723 if (stemp[0]==
'[') stemp=stemp.substr(1,stemp.length()-1);
724 if (stemp[stemp.length()-1]==
']') {
725 stemp=stemp.substr(0,stemp.length()-1);
728 set_unit(nnames[i],stemp);
730 std::cout <<
"Name,unit: " << nnames[i] <<
" [" << stemp <<
"]" 738 for(
size_t i=0;i<units.size();i++) {
748 while ((fin) >> data) {
749 this->
set(0,irow,data);
750 for(
size_t i=1;i<this->get_ncolumns();i++) {
752 this->
set(i,irow,data);
763 template<
class vec2_t>
765 bool allow_extrap=
true, std::string dest_index=
"") {
767 if (dest_index==
"") dest_index=src_index;
770 double min=source.
min(src_index);
771 double max=source.
max(src_index);
772 if (allow_extrap==
false) {
773 if (!std::isfinite(min) || !std::isfinite(max)) {
774 O2SCL_ERR2(
"Minimum or maximum of source index not finite ",
775 "in table_units::insert_table().",
exc_einval);
780 std::vector<std::string> col_list;
783 if (col!=src_index && col!=dest_index &&
784 this->is_column(col)==
false) {
785 col_list.push_back(col);
790 for(
size_t i=0;i<col_list.size();i++) {
791 this->new_column(col_list[i]);
792 set_unit(col_list[i],source.
get_unit(col_list[i]));
793 for(
size_t j=0;j<this->get_nlines();j++) {
794 double val=this->
get(dest_index,j);
795 if (allow_extrap || (val>=min && val<=max)) {
796 this->
set(col_list[i],j,source.
interp(src_index,val,col_list[i]));
806 friend void o2scl_hdf::hdf_output
812 friend void o2scl_hdf::hdf_output_data
820 #ifndef DOXYGEN_INTERNAL 830 typedef std::map<std::string,std::string,
831 std::greater<std::string> >::iterator uiter;
832 typedef std::map<std::string,std::string,
833 std::greater<std::string> >::const_iterator uciter;
837 std::map<std::string,std::string,std::greater<std::string> >
utree;
843 #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 .
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.
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.
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.
double interp(std::string sx, double x0, std::string sy)
Interpolate x0 from sx into 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.
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.
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.
Data table table class with units.
void new_column(std::string head)
Add a new column owned by the table table .
void clear()
Clear everything.
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 . .
double stod(std::string s, bool err_on_fail=true)
Convert a string to a double.
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.
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 .