Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
gnuplot.h
00001 /**************************************************************************
00002  * Copyright (c) 2004-2011 T. M. Murali                                   *
00003  *                                                                        *
00004  * This file is part of Biorithm.                                         *
00005  *                                                                        *
00006  * Biorithm is free software: you can redistribute it and/or modify       *
00007  * it under the terms of the GNU General Public License as published by   *
00008  * the Free Software Foundation, either version 3 of the License, or      *
00009  * (at your option) any later version.                                    *
00010  *                                                                        *
00011  * Biorithm is distributed in the hope that it will be useful,            *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00014  * GNU General Public License for more details.                           *
00015  *                                                                        *
00016  * You should have received a copy of the GNU General Public License      *
00017  * along with Biorithm.  If not, see <http://www.gnu.org/licenses/>.      *
00018  *                                                                        *
00019  **************************************************************************/
00020 
00027 #include <fstream>
00028 #include <iostream>
00029 #include <map>
00030 #include <set>
00031 #include <string>
00032 #include <vector>
00033 
00034 using namespace std;
00035 
00036 enum GnuplotLineStyleType { GNUPLOT_LINESTYLE_POINTS = 0,
00037                             GNUPLOT_LINESTYLE_LINES,
00038                             GNUPLOT_LINESTYLE_LINESPOINTS,
00039                             GNUPLOT_LINESTYLE_IMPULSES,
00040                             GNUPLOT_LINESTYLE_DOTS,
00041                             GNUPLOT_LINESTYLE_STEPS,
00042                             GNUPLOT_LINESTYLE_ERRORBARS,
00043                             GNUPLOT_LINESTYLE_BOXES,
00044                             GNUPLOT_LINESTYLE_BOXERRORBARS
00045 };
00046 
00047 enum GnuplotTerminalType { GNUPLOT_TERMINAL_FIG,
00048                            GNUPLOT_TERMINAL_PDF,
00049                            GNUPLOT_TERMINAL_PNG,
00050                            GNUPLOT_TERMINAL_POSTSCRIPT
00051 };
00052 
00053 class GnuplotFileGenerator;
00054 
00059 class GnuplotDataFile
00060 {
00061   friend class GnuplotFileGenerator;
00062   
00063 private:
00064   string dataFile;
00065   unsigned int index;
00066   string title;
00067   
00068 public:
00069   GnuplotDataFile(string f, unsigned int i = 0, string t = "")
00070       : dataFile(f), index(i), title(t)
00071     {}
00072   
00073 
00074   virtual ~GnuplotDataFile()
00075     {}
00076   
00077 };
00078 
00079 
00086 class GnuplotFileGenerator
00087 {
00088 private:
00089 
00090   // the file containing gnuplot commands.
00091   string _gnuplotCommandsFile;
00092   // _dataFileNames must be a vector because i may add the same file name but with different indices.
00093   vector< GnuplotDataFile > _dataFileNames;
00094   map< string, GnuplotLineStyleType >_dataFileLineStyles;
00095 //  map< string, string >_dataFileTitles;
00096   string plotTitle;
00097   bool time;
00098   bool logscale;
00099   
00100   set< string > _functions;
00101 
00102   GnuplotLineStyleType _lineStyle;
00103   
00104 //   bool lines;
00105 //   bool points;
00106 //   bool linespoints;
00107 //   bool impulses;
00108 //   bool dots;
00109 //   bool steps;
00110 //   bool errorbars;
00111 //   bool boxes;
00112 //   bool boxerrorbars;
00113 
00114   string xlabel;
00115   string ylabel;
00116   string zlabel;
00117   bool border;
00118   bool key;
00119   bool zeroaxis;
00120   bool grid;
00121   bool dgrid3d;
00122   bool xtics;
00123   bool ytics;
00124   bool color;
00125   bool solid;
00126 
00127   GnuplotTerminalType _terminal;
00128 //   bool terminal_postscript;
00129 //   bool terminal_png;
00130 //   bool terminal_gif;
00131   bool plot3d;
00132   bool contour_base;
00133   bool contour_surface;
00134   bool contour_both;
00135   bool cntrparam_linear;
00136   bool cntrparam_cubicspline;
00137   bool cntrparam_bspline;
00138   int x1;
00139   int x2;
00140   int y1;
00141   int y2;
00142   int use1;
00143   int use2;
00144   int use3;
00145   
00146 public:
00147 
00149   GnuplotFileGenerator()
00150     {
00151 //      _dataFileNames = set<string>();
00152       plotTitle = "";
00153       time = false;
00154       logscale = false;
00155       _lineStyle = GNUPLOT_LINESTYLE_POINTS;
00156 //       lines = true;
00157 //       points = false;
00158 //       linespoints = false;
00159 //       impulses = false;
00160 //       dots = false;
00161 //       steps = false;
00162 //       errorbars = false;
00163 //       boxes = false;
00164 //       boxerrorbars = false;
00165       xlabel = "";
00166       ylabel = "";
00167       zlabel = "";
00168       border = true;
00169       key = true;
00170       zeroaxis = true;
00171       grid = true;
00172       dgrid3d = false;
00173       xtics = true;
00174       ytics = true;
00175       color = true;
00176       solid = false;
00177 
00178       _terminal = GNUPLOT_TERMINAL_PNG;
00179 //       terminal_postscript = true;
00180 //       terminal_png = false;
00181 //       terminal_gif = false;
00182       plot3d = false;
00183       contour_base = false;
00184       contour_surface = false;
00185       contour_both = false;
00186       cntrparam_linear = false;
00187       cntrparam_cubicspline = false;
00188       cntrparam_bspline = false;
00189       x1 = 0;
00190       x2 = 0;
00191       y1 = 0;
00192       y2 = 0;
00193       use1 = 0;
00194       use2 = 0;
00195       use3 = 0;
00196 
00197     }
00198   ~GnuplotFileGenerator(){}
00199 
00207   void addDataFile(string file, unsigned int index = 0, string title = "")
00208     {
00209       _dataFileNames.push_back(GnuplotDataFile(file, index, title));
00210     }
00211   
00212         /*
00213          * Add a function.
00214          * 
00215          * \param[in] funciton, the function to plot.
00216          */
00217         void addFunction(string function)
00218         {
00219                 _functions.insert(function);
00220         }
00221   
00225   void setColumnsToPlot(int c1, int c2, int c3 = 0)
00226     {
00227       use1 = c1;
00228       use2 = c2;
00229       use3 = c3;
00230     }
00231 
00233   void setLabels(string xl = "", string yl = "",string zl = "")
00234     {
00235       xlabel = xl;
00236       ylabel = yl;
00237       zlabel = zl;
00238     }
00239 
00255   void setGnuplotFileName(string file)
00256     {
00257       _gnuplotCommandsFile = file;
00258     }
00259   
00260   
00264   void setLinestyle(GnuplotLineStyleType style, string fileName = "")
00265     {
00266       if ("" == fileName)
00267         _lineStyle = style;
00268       else
00269         _dataFileLineStyles[fileName] = style;
00270     }
00271   
00273   void setPlot3d(bool set)
00274     {
00275       plot3d = set;
00276     }
00277 
00279   void setPlotTitle(string t)
00280     {
00281       plotTitle = t;
00282     }
00283 
00285   void setXrange(int X1, int X2)
00286     {
00287       x1 = X1;
00288       x2 = X2;
00289     }
00291   void setYrange(int Y1, int Y2)
00292     {
00293       y1 = Y1;
00294       y2 = Y2;
00295     }
00296 
00297   void setTerminal(GnuplotTerminalType terminal)
00298     {
00299       _terminal = terminal;
00300     }
00301   
00302   void setContour_base(void)
00303     {
00304       if(contour_surface)contour_surface = false;
00305       if(contour_both)contour_both = false;
00306       
00307       contour_base = true;
00308     }
00309   void setContour_surface(void)
00310     {
00311       if(contour_base)contour_base = false;
00312       if(contour_both)contour_both = false;
00313       
00314       contour_surface = true;
00315     }
00316   void setContour_both(void)
00317     {
00318       if(contour_surface)contour_surface = false;
00319       if(contour_base)contour_base = false;
00320       
00321       contour_both = true;
00322     } 
00323   
00324   
00325   void setCntrparam_linear(void)
00326     {
00327       if(cntrparam_cubicspline)cntrparam_cubicspline = false;
00328       if(cntrparam_bspline)cntrparam_bspline = false;
00329       
00330       cntrparam_linear = true;
00331     }
00332   void setCntrparam_cubicspline(void)
00333     {
00334       if(cntrparam_linear)cntrparam_linear = false;
00335       if(cntrparam_bspline)cntrparam_bspline = false;
00336       
00337       cntrparam_cubicspline = true;
00338     }
00339   void setCntrparam_bspline(void)
00340     {
00341       if(cntrparam_cubicspline)cntrparam_cubicspline = false;
00342       if(cntrparam_linear)cntrparam_linear = false;
00343       
00344       cntrparam_bspline = true;
00345     }
00346 
00347   //turning on and off certain parameters.
00348   void turnon_grid(void)
00349     {
00350       grid = true;
00351     }
00352   void turnoff_grid(void)
00353     {
00354       grid = false;
00355     }
00356   void turnon_dgrid3d(void)
00357     {
00358       dgrid3d = true;
00359     }
00360   void turnoff_dgrid3d(void)
00361     {
00362       dgrid3d = false;
00363     }
00364   void turnon_key(void)
00365     {
00366       key = true;
00367     }
00368   void turnoff_key(void)
00369     {
00370       key = false;
00371         }
00372   void turnon_border(void)
00373     {
00374       border = true;
00375     }
00376   void turnoff_border(void)
00377     {
00378       border = false;
00379     }
00380   void turnon_xtics(void)
00381     {
00382       xtics = true;
00383     }
00384   void turnoff_xtics(void)
00385     {
00386       xtics = false;
00387     }
00388   void turnon_ytics(void)
00389     {
00390       ytics = true;
00391     }
00392   void turnoff_ytics(void)
00393     {
00394       ytics = false;
00395     }
00396   void turnon_logscale(void)
00397     {
00398       logscale = true;
00399     }
00400   void turnoff_logscale(void)
00401     {
00402       logscale = false;
00403     }
00404   void turnon_time(void)
00405     {
00406       time = true;
00407     }
00408   void turnoff_time(void)
00409     {
00410       time = false;
00411     }
00412   void turnon_zeroaxis(void)
00413     {
00414       zeroaxis = true;
00415     }
00416   void turnoff_zeroaxis(void)
00417     {
00418       zeroaxis = false;
00419     }
00420   void turnon_Color(void)
00421     {
00422       color = true;
00423     }
00424   void turnoff_Color(void)
00425     {
00426       color = false;
00427     }
00428   void turnon_Solid(void)
00429     {
00430       solid = true;
00431     }
00432   void turnoff_Solid(void)
00433     {
00434       solid = false;
00435     }   
00436     
00438   void generate()
00439     {
00440       generateFile();
00441       generatePlot();
00442     }
00443   
00444 
00446   void generateFile(void)
00447     {
00448       ofstream gfile;
00449       if (_dataFileNames.empty())
00450         {
00451           cerr << "No data files to plot! Did you forget to specify data files with the GnuplotFileGenerator::addDataFile() method?" << endl;
00452           return;
00453         }
00454       vector< GnuplotDataFile >::iterator fitr;
00455       map< string, GnuplotLineStyleType >::iterator sitr;
00456       if ("" == _gnuplotCommandsFile)
00457         {
00458           // use the concatenation of all the data file names.
00459           for (fitr = _dataFileNames.begin(); fitr != _dataFileNames.end(); fitr++)
00460             {
00461               if (fitr != _dataFileNames.begin())
00462                 _gnuplotCommandsFile += "-";
00463               _gnuplotCommandsFile += fitr->dataFile;
00464             }
00465         }
00466       string _gnuplotCommandsFileBase = _gnuplotCommandsFile;
00467       _gnuplotCommandsFile += ".gp";
00468       gfile.open(_gnuplotCommandsFile.c_str());
00469       
00470       if(!gfile.is_open())
00471         return;
00472       pair< string, string > terminal = _getTerminalString(_terminal);
00473 
00474       gfile<<"set output \""<< _gnuplotCommandsFileBase << terminal.second << "\"" << endl;
00475       gfile<<"set terminal " << terminal.first << endl;
00476               
00477 //       if(color) gfile<<" color\n";
00478 //       else {
00479         if(solid) gfile<<" solid\n";
00480         else gfile<<"\n";
00481 //      } 
00482       gfile<<"set autoscale x\n";
00483       gfile<<"set autoscale y\n";
00484       
00485       if(x1!=0||x2!=0)
00486         gfile<<"set xrange ["<<x1<<":"<<x2<<"]";
00487       if(y1!=0||y2!=0)
00488         gfile<<"set yrange ["<<y1<<":"<<y2<<"]";
00489       
00490       if(logscale)gfile<<"set logscale\n";
00491       else gfile<<"set nologscale\n";
00492       
00493       if(grid)gfile<<"set grid\n";
00494       else gfile<<"set nogrid\n";
00495       
00496       if(dgrid3d)gfile<<"set dgrid3d\n";
00497       else gfile<<"set nodgrid3d\n";
00498       
00499       if(border)gfile<<"set border\n";
00500       else gfile<<"set noborder\n";
00501       
00502       if(key)gfile<<"set key\n";
00503       else gfile<<"set nokey\n";
00504       
00505       if(xtics)gfile<<"set xtics\n";
00506       else gfile<<"set noxtics\n";
00507       
00508       if(ytics)gfile<<"set ytics\n";
00509       else gfile<<"set noytics\n";
00510       
00511       if(zeroaxis)gfile<<"set zeroaxis\n";
00512       else gfile<<"set nozeroaxis\n";
00513       
00514       if(time)gfile<<"set time\n";
00515       
00516       if(contour_base)gfile<<"set contour\n";
00517       if(contour_surface)gfile<<"set contour surface\n";
00518       if(contour_both)gfile<<"set contour both\n";
00519       
00520       if(cntrparam_cubicspline)gfile<<"set cntrparam cubicspline\n";
00521       if(cntrparam_bspline)gfile<<"set cntrparam bspline\n";
00522       if(cntrparam_linear)gfile<<"set cntrparam linear\n";
00523       
00524               
00525               
00526       if(plotTitle !="")gfile<<"set title \""<<plotTitle<<"\"\n";
00527       
00528       if(xlabel!="")gfile<<"set xlabel \""<<xlabel<<"\"\n";
00529       if(ylabel!="")gfile<<"set ylabel \""<<ylabel<<"\"\n";
00530       if(zlabel!="")gfile<<"set zlabel \""<<zlabel<<"\"\n";
00531 
00532       gfile <<"set data style "
00533             << _getLineStyleString(_lineStyle) <<"\n";
00534             
00535       string plotCommand;
00536       
00537       if(plot3d)
00538         {
00539           plotCommand = "splot ";
00540         }
00541       else
00542         {
00543           plotCommand = "plot ";
00544         }
00545       // print the plot command.
00546       gfile << plotCommand;
00547       map< string, string >::iterator titr;
00548       for (fitr = _dataFileNames.begin(); fitr != _dataFileNames.end(); fitr++)
00549         {
00550           if (fitr != _dataFileNames.begin())
00551             gfile << ", ";
00552           gfile << "\"" << fitr->dataFile <<"\"";
00553           gfile << " index " << fitr->index << " ";
00554           if(use1&&use2)gfile<<" using "<<use1<<":"<<use2;
00555           if(use3)gfile<<":"<<use3;
00556           gfile << " ";
00557           // print the title of this data file, if it exists. print after the using command.
00558 //           titr = _dataFileTitles.find(fitr->dataFile);
00559 //           if (_dataFileTitles.end() != titr)
00560           if ("" != fitr->title)
00561             gfile << " title \"" << fitr->title << "\" ";
00562           sitr = _dataFileLineStyles.find(fitr->dataFile);
00563           if (_dataFileLineStyles.end() != sitr)
00564             gfile << " with " << _getLineStyleString(sitr->second); 
00565        }
00566        
00567       // print plot commands for functions
00568       for (set< string >::iterator funitr = _functions.begin(); funitr != _functions.end(); funitr++)
00569       {
00570         if ((funitr != _functions.begin()) || (!_dataFileNames.empty()))
00571                 gfile << ", ";
00572         gfile << *funitr << " with lines"; // TODO add style control for functions
00573       }
00574       
00575       gfile << endl;
00576       gfile.close();
00577     }
00578 
00580   void generatePlot()
00581     {
00582       string gnuplotCommand;
00583       gnuplotCommand = "gnuplot " + _gnuplotCommandsFile;
00584       int returnValue = system(gnuplotCommand.c_str());
00585 
00586     }
00587   
00588 private:
00589   string _getLineStyleString(GnuplotLineStyleType style)
00590     {
00591       if (GNUPLOT_LINESTYLE_POINTS == style)
00592         return("points");
00593       if (GNUPLOT_LINESTYLE_LINES == style)
00594         return("lines");
00595       if (GNUPLOT_LINESTYLE_LINESPOINTS == style)
00596         return("linespoints");
00597       cerr << "Unknown Gnuplot line style " << style << endl;
00598       exit(-1);
00599     }
00600   
00601   pair< string, string > _getTerminalString(GnuplotTerminalType terminal)
00602     {
00603       if (GNUPLOT_TERMINAL_PDF == terminal)
00604         return(pair<string, string>(" pdf ", ".pdf"));
00605       if (GNUPLOT_TERMINAL_PNG == terminal)
00606         return(pair<string, string>(" png ", ".png"));
00607       if (GNUPLOT_TERMINAL_POSTSCRIPT == terminal)
00608 //        return(pair<string, string>(" postscript color landscape ", ".ps"));
00609         return(pair<string, string>(" postscript color ", ".ps"));
00610       cerr << "Unknown Gnuplot terminal type " << terminal << endl;
00611       exit(-1);
00612     }
00613   
00614 
00615 };
00616 
 All Classes Functions Variables Typedefs Friends