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> >
80 class table_units :
public table<vec_t> {
84 #ifdef O2SCL_NEVER_DEFINED
104 this->constants=t.constants;
107 this->nlines=t.get_nlines();
108 this->maxlines=this->nlines;
109 for(
size_t i=0;i<t.get_ncolumns();i++) {
112 std::string cname=t.get_column_name(i);
116 s.
dat.resize(this->nlines);
117 s.
index=this->atree.size();
118 this->atree.insert(make_pair(cname,s));
122 this->alist.push_back(it);
125 utree.insert(make_pair(cname,t.get_unit(cname)));
128 for(
size_t j=0;j<t.get_nlines();j++) {
129 it->second.dat[j]=t.get(cname,j);
134 this->intp_set=
false;
145 for(
size_t i=0;i<nc;i++) {
149 this->constants.insert(make_pair(name,val));
154 this->maxlines=this->nlines;
162 s.
dat=
new vec_t(this->nlines);
163 s.
index=this->atree.size();
164 this->atree.insert(make_pair(cname,s));
168 this->alist.push_back(it);
172 (*it->second.dat)[j]=t.
get(cname,j);
177 this->intp_set=
false;
190 this->constants.clear();
194 this->constants=t.constants;
197 this->nlines=t.get_nlines();
198 for(
size_t i=0;i<t.get_ncolumns();i++) {
201 std::string cname=t.get_column_name(i);
205 s.
dat.resize(this->nlines);
206 s.
index=this->atree.size();
207 this->atree.insert(make_pair(cname,s));
211 this->alist.push_back(it);
214 utree.insert(make_pair(cname,t.get_unit(cname)));
217 for(
size_t j=0;j<t.get_nlines();j++) {
218 it->second.dat[j]=t.get(cname,j);
223 if (this->intp_set) {
224 this->intp_set=
false;
241 this->constants.clear();
246 for(
size_t i=0;i<nc;i++) {
250 this->constants.insert(make_pair(name,val));
262 s.
dat=
new vec_t(this->nlines);
263 s.
index=this->atree.size();
264 this->atree.insert(make_pair(cname,s));
268 this->alist.push_back(it);
272 (*it->second.dat)[j]=t.
get(cname,j);
277 if (this->intp_set) {
278 this->intp_set=
false;
295 uciter it=utree.find(scol);
296 if (it==utree.end()) {
299 if (at==this->atree.
end()) {
300 O2SCL_ERR((((std::string)
"Column '")+scol+
301 "' not found in table_units::get_unit().").c_str(),
314 uiter it=utree.find(scol);
315 if (it==utree.end()) {
316 O2SCL_ERR((((std::string)
"Column '")+scol+
317 "' not found in table_units::get_unit().").c_str(),
321 if (utree.size()==0) {
322 O2SCL_ERR(
"No units specified in table_units::remove_unit().",
331 void set_unit(std::string scol, std::string unit) {
333 uiter it=utree.find(scol);
334 if (it==utree.end()) {
336 if (at==this->atree.
end()) {
337 O2SCL_ERR((((std::string)
"Column '")+scol+
338 "' not found in table_units::set_unit().").c_str(),
341 utree.insert(make_pair(scol,unit));
351 bool err_on_fail=
true) {
354 uiter it=utree.find(scol);
355 if (it==utree.end()) {
357 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
358 "table_units::convert_to_unit().").c_str(),
366 if (it->second==unit)
return success;
370 if (at==this->atree.
end()) {
372 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
373 "table_units::convert_to_unit().").c_str(),
381 vec_t &vec=at->second.dat;
382 double conv=
cup->convert(it->second,unit,1.0);
384 for(
size_t i=0;i<this->get_nlines();i++) {
427 if (it==this->atree.
end()) {
428 O2SCL_ERR((((std::string)
"Column '")+scol+
429 " not found in table_units::delete_column().").c_str(),
435 if (get_unit(scol).length()>0) remove_unit(scol);
439 vit+=it->second.index;
444 this->alist[this->alist.size()-1]->second.index=it->second.index;
447 this->atree.erase(it);
448 this->alist.erase(vit);
457 virtual void summary(std::ostream *out,
size_t ncol=79)
const {
459 if (this->constants.size()==1) {
460 (*out) <<
"1 constant:" << std::endl;
462 (*out) << this->constants.size() <<
" constants:" << std::endl;
464 std::map<std::string,double>::const_iterator mit;
465 for(mit=this->constants.begin();mit!=this->constants.end();mit++) {
466 (*out) << mit->first <<
" " << mit->second << std::endl;
470 size_t nh=this->get_ncolumns(), nh2;
474 (*out) <<
"No columns." << std::endl;
479 (*out) <<
"1 column: " << std::endl;
481 (*out) << nh <<
" columns: " << std::endl;
483 std::string *h=
new std::string[nh];
484 for(
size_t i=0;i<nh;i++) {
485 h[i]=
szttos(i)+
". "+this->get_column_name(i)+
" ["+
486 get_unit(this->get_column_name(i))+
"]";
489 std::vector<std::string> h2;
495 for(
size_t i=0;i<nh2;i++) {
496 (*out) << h2[i] << std::endl;
503 if (this->get_nlines()==0) {
504 (*out) <<
"No lines of data." << std::endl;
505 }
else if (this->get_nlines()==1) {
506 (*out) <<
"One line of data." << std::endl;
508 (*out) << this->get_nlines() <<
" lines of data." << std::endl;
516 virtual const char *
type() {
return "table_units"; }
527 if (!this->is_column(dest)) this->new_column(dest);
529 typedef typename std::map<std::string,
532 aiter its=this->atree.find(src);
533 if (its==this->atree.end()) {
534 O2SCL_ERR((((std::string)
"Column '")+src+
535 " not found in table_units::copy_column().").c_str(),
539 aiter itd=this->atree.find(dest);
540 if (itd==this->atree.end()) {
541 O2SCL_ERR((((std::string)
"Destination column '")+dest+
542 " not found in table_units::copy_column().").c_str(),
546 this->set_unit(dest,this->get_unit(src));
547 for(
size_t i=0;i<this->nlines;i++) {
548 itd->second.dat[i]=its->second.dat[i];
559 std::istringstream *is;
562 std::vector<std::string> onames, nnames;
564 is=
new std::istringstream(line);
565 while ((*is) >> stemp) {
566 onames.push_back(stemp);
568 std::cout <<
"Read possible column name: " << stemp << std::endl;
575 for(
size_t i=0;i<onames.size();i++) {
581 if (n_nums==onames.size()) {
584 std::cout <<
"First row looks like it contains numerical values."
586 std::cout <<
"Creating generic column names: ";
589 for(
size_t i=0;i<onames.size();i++) {
590 nnames.push_back(((std::string)
"c")+
szttos(i+1));
591 if (verbose>0) std::cout << nnames[i] <<
" ";
594 if (verbose>0) std::cout << std::endl;
597 for(
size_t i=0;i<nnames.size();i++) {
598 this->new_column(nnames[i]);
602 for(
size_t i=0;i<onames.size();i++) {
610 for(
size_t i=0;i<onames.size();i++) {
611 std::string temps=onames[i];
612 this->make_fp_varname(temps);
613 this->make_unique_name(temps,nnames);
614 nnames.push_back(temps);
615 if (temps!=onames[i] && verbose>0) {
616 std::cout <<
"Converted column named '" << onames[i] <<
"' to '"
617 << temps <<
"'." << std::endl;
622 for(
size_t i=0;i<nnames.size();i++) {
623 this->new_column(nnames[i]);
627 std::vector<std::string> units;
629 is=
new std::istringstream(line);
631 while ((*is) >> stemp) {
632 units.push_back(stemp);
633 if (stemp[0]==
'[') num_units++;
635 std::cout <<
"Read word in second row: " << stemp << std::endl;
640 if (units.size()!=nnames.size()) {
641 std::cout <<
"Second row appears not to have same number of "
642 <<
"entries as the first." << std::endl;
643 std::cout <<
"Aborting." << std::endl;
647 if (num_units==((
int)units.size()) || num_units>2) {
649 std::cout <<
"Looks like second row contains units." << std::endl;
651 for(
size_t i=0;i<units.size();i++) {
654 if (stemp[0]==
'[') stemp=stemp.substr(1,stemp.length()-1);
655 if (stemp[stemp.length()-1]==
']') {
656 stemp=stemp.substr(0,stemp.length()-1);
659 set_unit(nnames[i],stemp);
661 std::cout <<
"Name,unit: " << nnames[i] <<
" [" << stemp <<
"]"
669 for(
size_t i=0;i<units.size();i++) {
679 while ((fin) >> data) {
680 this->set(0,irow,data);
681 for(
size_t i=1;i<this->get_ncolumns();i++) {
683 this->set(i,irow,data);
693 friend void o2scl_hdf::hdf_output
699 friend void o2scl_hdf::hdf_output_data
707 #ifndef DOXYGEN_INTERNAL
717 typedef std::map<std::string,std::string,
719 typedef std::map<std::string,std::string,
724 std::map<std::string,std::string,string_comp>
utree;
730 #ifndef DOXYGEN_NO_O2NS
table_units(int cmaxlines=0)
Create a new table_units with space for nlines<=cmaxlines.
std::map< std::string, col, string_comp >::iterator aiter
Map iterator type.
void remove_unit(std::string scol)
Remove the unit for column scol.
virtual size_t get_nconsts() const
Get the number of constants.
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
virtual int read_generic(std::istream &fin, int verbose=0)
Clear the current table and read from a generic data file.
size_t get_nlines() const
Return 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.
std::string get_unit(std::string scol) const
Get the unit for column scol.
virtual void summary(std::ostream *out, size_t ncol=79) const
Output a summary of the information stored.
Simple string comparison.
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.
virtual double get_constant(std::string name) const
Get a constant.
std::string get_column_name(size_t icol) const
Returns the name of column col .
void set_unit(std::string scol, std::string unit)
Set the unit for column scol to unit.
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.
Column structure for table [protected].
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.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
std::map< std::string, std::string, string_comp > utree
Unit map.
size_t get_ncolumns() const
Return the number of columns.
Data table table class with units.
aiter end()
Return the end of the column tree.
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.
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$scl...
std::map< std::string, col, string_comp >::const_iterator aciter
Const map iterator type.
double stod(std::string s, bool err_on_fail=true)
Convert a string to a double.
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.
double get(std::string scol, size_t row) const
Get value from row row of column named col. .
table_units(const table_units &t)
Copy with constructor from table_units.