mirror of
				https://github.com/aportelli/LatAnalyze.git
				synced 2025-10-31 14:54:33 +00:00 
			
		
		
		
	Plot: function to save plots
This commit is contained in:
		
							
								
								
									
										107
									
								
								lib/Plot.cpp
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								lib/Plot.cpp
									
									
									
									
									
								
							| @@ -195,9 +195,17 @@ void LogScale::operator()(PlotOptions &option) const | |||||||
|     option.scaleMode[static_cast<int>(axis_)] |= Plot::Scale::log; |     option.scaleMode[static_cast<int>(axis_)] |= Plot::Scale::log; | ||||||
| } | } | ||||||
|  |  | ||||||
| // PlotRange constructor //////////////////////////////////////////////////////// | // PlotRange constructors ////////////////////////////////////////////////////// | ||||||
|  | PlotRange::PlotRange(const Axis axis) | ||||||
|  | : axis_(axis) | ||||||
|  | , reset_(true) | ||||||
|  | , min_(0.) | ||||||
|  | , max_(0.) | ||||||
|  | {} | ||||||
|  |  | ||||||
| PlotRange::PlotRange(const Axis axis, const double min, const double max) | PlotRange::PlotRange(const Axis axis, const double min, const double max) | ||||||
| : axis_(axis) | : axis_(axis) | ||||||
|  | , reset_(false) | ||||||
| , min_(min) | , min_(min) | ||||||
| , max_(max) | , max_(max) | ||||||
| {} | {} | ||||||
| @@ -207,9 +215,16 @@ void PlotRange::operator()(PlotOptions &option) const | |||||||
| { | { | ||||||
|     int a = static_cast<int>(axis_); |     int a = static_cast<int>(axis_); | ||||||
|      |      | ||||||
|     option.scaleMode[a] |= Plot::Scale::manual; |     if (!reset_) | ||||||
|     option.scale[a].min  = min_; |     { | ||||||
|     option.scale[a].max  = max_; |         option.scaleMode[a] |= Plot::Scale::manual; | ||||||
|  |         option.scale[a].min  = min_; | ||||||
|  |         option.scale[a].max  = max_; | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         option.scaleMode[a] = Plot::Scale::reset; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| // Terminal constructor //////////////////////////////////////////////////////// | // Terminal constructor //////////////////////////////////////////////////////// | ||||||
| @@ -252,15 +267,15 @@ Plot::~Plot(void) | |||||||
| // clean temporary files /////////////////////////////////////////////////////// | // clean temporary files /////////////////////////////////////////////////////// | ||||||
| void Plot::cleanTmpFiles(void) | void Plot::cleanTmpFiles(void) | ||||||
| { | { | ||||||
|     while (!tmpFileName_.empty()) |     for (string &fileName: tmpFileName_) | ||||||
|     { |     { | ||||||
|         if (remove(tmpFileName_.top().c_str())) |         if (remove(fileName.c_str())) | ||||||
|         { |         { | ||||||
|             LATAN_ERROR(System, "impossible to remove temporary file '" + |             LATAN_ERROR(System, "impossible to remove temporary file '" + | ||||||
|                         tmpFileName_.top() + "'"); |                         fileName + "'"); | ||||||
|         } |         } | ||||||
|         tmpFileName_.pop(); |  | ||||||
|     } |     } | ||||||
|  |     tmpFileName_.clear(); | ||||||
| } | } | ||||||
|  |  | ||||||
| // default options ///////////////////////////////////////////////////////////// | // default options ///////////////////////////////////////////////////////////// | ||||||
| @@ -295,7 +310,8 @@ Plot & Plot::operator<<(PlotObject &&command) | |||||||
|      |      | ||||||
|     while (command.gotTmpFile()) |     while (command.gotTmpFile()) | ||||||
|     { |     { | ||||||
|         tmpFileName_.push(command.popTmpFile()); |         tmpFileName_.push_back(command.popTmpFile()); | ||||||
|  |         commandStr += "'" + tmpFileName_.back() + "' "; | ||||||
|     } |     } | ||||||
|     commandStr = command.getCommand(); |     commandStr = command.getCommand(); | ||||||
|     if (!options_.lineColor.empty()) |     if (!options_.lineColor.empty()) | ||||||
| @@ -390,7 +406,7 @@ void Plot::getProgramPath(void) | |||||||
| // plot parsing and output ///////////////////////////////////////////////////// | // plot parsing and output ///////////////////////////////////////////////////// | ||||||
| void Plot::display(void) | void Plot::display(void) | ||||||
| { | { | ||||||
|     std::string command; |     string command; | ||||||
|     FILE *gnuplotPipe; |     FILE *gnuplotPipe; | ||||||
|      |      | ||||||
|     if (!getenv("DISPLAY")) |     if (!getenv("DISPLAY")) | ||||||
| @@ -414,6 +430,75 @@ void Plot::display(void) | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void Plot::save(string dirName) | ||||||
|  | { | ||||||
|  |     vector<string> commandBack; | ||||||
|  |     string         path, terminalBack, outputBack, gpCommand, scriptName; | ||||||
|  |     mode_t         mode755; | ||||||
|  |     ofstream       script; | ||||||
|  |      | ||||||
|  |     mode755 = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; | ||||||
|  |      | ||||||
|  |     // backup I/O parameters | ||||||
|  |     terminalBack = options_.terminal; | ||||||
|  |     outputBack   = options_.output; | ||||||
|  |     commandBack  = plotCommand_; | ||||||
|  |  | ||||||
|  |     // generate directory | ||||||
|  |     if (access(dirName.c_str(), R_OK|W_OK|X_OK)) | ||||||
|  |     { | ||||||
|  |         if (mkdir(dirName.c_str(), mode755)) | ||||||
|  |         { | ||||||
|  |             LATAN_ERROR(Io, "impossible to create directory '" + dirName + "'"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     // save PDF | ||||||
|  |     options_.terminal = "pdf"; | ||||||
|  |     options_.output   = dirName + "/plot.pdf"; | ||||||
|  |     display(); | ||||||
|  |     options_.terminal = terminalBack; | ||||||
|  |     options_.output   = outputBack; | ||||||
|  |      | ||||||
|  |     // save script and datafiles | ||||||
|  |     for (unsigned int i = 0; i < tmpFileName_.size(); ++i) | ||||||
|  |     { | ||||||
|  |         ofstream dataFile; | ||||||
|  |         ifstream tmpFile; | ||||||
|  |         string   dataFileName = "points_" + strFrom(i) + ".dat"; | ||||||
|  |          | ||||||
|  |         dataFile.open(dirName + "/" + dataFileName); | ||||||
|  |         tmpFile.open(tmpFileName_[i]); | ||||||
|  |         dataFile << tmpFile.rdbuf(); | ||||||
|  |         dataFile.close(); | ||||||
|  |         tmpFile.close(); | ||||||
|  |         for (string &command: plotCommand_) | ||||||
|  |         { | ||||||
|  |             auto pos = command.find(tmpFileName_[i]); | ||||||
|  |              | ||||||
|  |             while (pos != string::npos) | ||||||
|  |             { | ||||||
|  |                 command.replace(pos, tmpFileName_[i].size(), dataFileName); | ||||||
|  |                 pos = command.find(tmpFileName_[i], pos + 1); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     scriptName = dirName + "/source.plt"; | ||||||
|  |     script.open(scriptName); | ||||||
|  |     getProgramPath(); | ||||||
|  |     gpCommand = gnuplotPath_ + "/" + gnuplotBin_ + " " + gnuplotArgs_; | ||||||
|  |     script << "#!/usr/bin/env " << gpCommand << "\n" << endl; | ||||||
|  |     script << "# script generated by " << Env::fullName << "\n" << endl; | ||||||
|  |     script << *this; | ||||||
|  |     script.close(); | ||||||
|  |     if (chmod(scriptName.c_str(), mode755)) | ||||||
|  |     { | ||||||
|  |         LATAN_ERROR(Io, "impossible to set file '" + scriptName + | ||||||
|  |                     "' in mode 755"); | ||||||
|  |     } | ||||||
|  |     plotCommand_ = commandBack; | ||||||
|  | } | ||||||
|  |  | ||||||
| ostream & Latan::operator<<(ostream &out, const Plot &plot) | ostream & Latan::operator<<(ostream &out, const Plot &plot) | ||||||
| { | { | ||||||
|     std::string begin, end; |     std::string begin, end; | ||||||
| @@ -425,7 +510,7 @@ ostream & Latan::operator<<(ostream &out, const Plot &plot) | |||||||
|     } |     } | ||||||
|     if (!plot.options_.output.empty()) |     if (!plot.options_.output.empty()) | ||||||
|     { |     { | ||||||
|         out << "set output '" << plot.options_.terminal << "'" << endl; |         out << "set output '" << plot.options_.output << "'" << endl; | ||||||
|     } |     } | ||||||
|     if (!plot.options_.caption.empty()) |     if (!plot.options_.caption.empty()) | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -24,7 +24,6 @@ | |||||||
| #include <LatAnalyze/Mat.hpp> | #include <LatAnalyze/Mat.hpp> | ||||||
| #include <LatAnalyze/XYStatData.hpp> | #include <LatAnalyze/XYStatData.hpp> | ||||||
| #include <sstream> | #include <sstream> | ||||||
| #include <stack> |  | ||||||
| #include <vector> | #include <vector> | ||||||
|  |  | ||||||
| // gnuplot default parameters | // gnuplot default parameters | ||||||
| @@ -176,7 +175,8 @@ private: | |||||||
| class PlotRange: public PlotModifier | class PlotRange: public PlotModifier | ||||||
| { | { | ||||||
| public: | public: | ||||||
|     // constructor |     // constructors | ||||||
|  |     PlotRange(const Axis axis); | ||||||
|     PlotRange(const Axis axis, const double min, const double max); |     PlotRange(const Axis axis, const double min, const double max); | ||||||
|     // destructor |     // destructor | ||||||
|     virtual ~PlotRange(void) = default; |     virtual ~PlotRange(void) = default; | ||||||
| @@ -184,6 +184,7 @@ public: | |||||||
|     virtual void operator()(PlotOptions &option) const; |     virtual void operator()(PlotOptions &option) const; | ||||||
| private: | private: | ||||||
|     const Axis   axis_; |     const Axis   axis_; | ||||||
|  |     const bool   reset_; | ||||||
|     const double min_, max_; |     const double min_, max_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -238,6 +239,7 @@ public: | |||||||
|     Plot & operator<<(PlotModifier &&modifier); |     Plot & operator<<(PlotModifier &&modifier); | ||||||
|     // plot parsing and output |     // plot parsing and output | ||||||
|     void display(void); |     void display(void); | ||||||
|  |     void save(std::string dirName); | ||||||
|     friend std::ostream & operator<<(std::ostream &out, const Plot &plot); |     friend std::ostream & operator<<(std::ostream &out, const Plot &plot); | ||||||
|     // plot reset |     // plot reset | ||||||
|     void reset(void); |     void reset(void); | ||||||
| @@ -256,7 +258,7 @@ private: | |||||||
|     // string buffer for commands |     // string buffer for commands | ||||||
|     std::ostringstream       commandBuffer_; |     std::ostringstream       commandBuffer_; | ||||||
|     // stack of created temporary files |     // stack of created temporary files | ||||||
|     std::stack<std::string>  tmpFileName_; |     std::vector<std::string> tmpFileName_; | ||||||
|     // plot content |     // plot content | ||||||
|     PlotOptions              options_; |     PlotOptions              options_; | ||||||
|     std::vector<std::string> headCommand_; |     std::vector<std::string> headCommand_; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user