2014-02-10 11:22:27 +00:00
|
|
|
/*
|
|
|
|
* Plot.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2016-04-06 20:11:23 +01:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-02-10 11:22:27 +00:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Plot.hpp>
|
|
|
|
#include <LatAnalyze/includes.hpp>
|
|
|
|
#include <LatAnalyze/Mat.hpp>
|
2014-02-10 11:22:27 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
2014-03-03 12:41:48 +00:00
|
|
|
* Plot objects *
|
2014-02-10 11:22:27 +00:00
|
|
|
******************************************************************************/
|
2014-03-03 12:41:48 +00:00
|
|
|
// PlotObject access ///////////////////////////////////////////////////////////
|
|
|
|
const string & PlotObject::getCommand(void) const
|
|
|
|
{
|
|
|
|
return command_;
|
|
|
|
}
|
|
|
|
|
2015-06-26 20:33:57 +01:00
|
|
|
const string & PlotObject::getHeadCommand(void) const
|
|
|
|
{
|
|
|
|
return headCommand_;
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
void PlotObject::setCommand(const string &command)
|
|
|
|
{
|
|
|
|
command_ = command;
|
|
|
|
}
|
2014-02-10 11:22:27 +00:00
|
|
|
|
2015-06-26 20:33:57 +01:00
|
|
|
void PlotObject::setHeadCommand(const string &command)
|
|
|
|
{
|
|
|
|
headCommand_ = command;
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
string PlotObject::popTmpFile(void)
|
|
|
|
{
|
|
|
|
string res = tmpFileName_.top();
|
|
|
|
|
|
|
|
tmpFileName_.pop();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlotObject::pushTmpFile(const std::string &fileName)
|
|
|
|
{
|
|
|
|
tmpFileName_.push(fileName);
|
|
|
|
}
|
|
|
|
|
2014-03-03 18:33:53 +00:00
|
|
|
// PlotObject dump a matrix to a temporary file ////////////////////////////////
|
|
|
|
string PlotObject::dumpToTmpFile(const DMat &m)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2015-12-09 18:12:39 +00:00
|
|
|
char tmpFileName[MAX_PATH_LENGTH];
|
|
|
|
int fd;
|
|
|
|
FILE *tmpFile;
|
2014-03-03 12:41:48 +00:00
|
|
|
|
2014-03-03 18:33:53 +00:00
|
|
|
for (Index j = 0; j < m.cols(); ++j)
|
|
|
|
{
|
|
|
|
}
|
2015-12-09 18:12:39 +00:00
|
|
|
sprintf(tmpFileName, "%s/latan_plot_tmp.XXXXXX.dat", P_tmpdir);
|
2014-03-03 12:41:48 +00:00
|
|
|
fd = mkstemps(tmpFileName, 4);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(System, "impossible to create a temporary file from template "
|
|
|
|
+ strFrom(tmpFileName));
|
|
|
|
}
|
|
|
|
tmpFile = fdopen(fd, "w");
|
2014-03-03 18:33:53 +00:00
|
|
|
for (Index i = 0; i < m.rows(); ++i)
|
2014-03-03 12:41:48 +00:00
|
|
|
{
|
2014-03-03 18:33:53 +00:00
|
|
|
for (Index j = 0; j < m.cols(); ++j)
|
|
|
|
{
|
|
|
|
fprintf(tmpFile, "%e ", m(i, j));
|
|
|
|
}
|
|
|
|
fprintf(tmpFile, "\n");
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
fclose(tmpFile);
|
2014-03-03 18:33:53 +00:00
|
|
|
|
|
|
|
return string(tmpFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlotObject test /////////////////////////////////////////////////////////////
|
|
|
|
bool PlotObject::gotTmpFile(void) const
|
|
|
|
{
|
|
|
|
return !tmpFileName_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlotCommand constructor /////////////////////////////////////////////////////
|
|
|
|
PlotCommand::PlotCommand(const string &command)
|
|
|
|
{
|
|
|
|
setCommand(command);
|
|
|
|
}
|
|
|
|
|
2015-06-26 20:33:57 +01:00
|
|
|
// PlotHeadCommand constructor /////////////////////////////////////////////////
|
|
|
|
PlotHeadCommand::PlotHeadCommand(const string &command)
|
|
|
|
{
|
|
|
|
setHeadCommand(command);
|
|
|
|
}
|
|
|
|
|
2014-03-03 18:33:53 +00:00
|
|
|
// PlotData constructor ////////////////////////////////////////////////////////
|
2015-07-07 18:50:36 +01:00
|
|
|
PlotData::PlotData(const DMatSample &x, const DMatSample &y)
|
|
|
|
{
|
|
|
|
if (x[central].rows() != y[central].rows())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "x and y vector does not have the same size");
|
|
|
|
}
|
|
|
|
|
|
|
|
DMat d(x[central].rows(), 4);
|
|
|
|
string usingCmd, tmpFileName;
|
|
|
|
|
|
|
|
d.col(0) = x[central];
|
|
|
|
d.col(2) = y[central];
|
|
|
|
d.col(1) = x.variance().cwiseSqrt();
|
|
|
|
d.col(3) = y.variance().cwiseSqrt();
|
|
|
|
tmpFileName = dumpToTmpFile(d);
|
|
|
|
pushTmpFile(tmpFileName);
|
|
|
|
setCommand("'" + tmpFileName + "' u 1:3:2:4 w xyerr");
|
|
|
|
}
|
|
|
|
|
|
|
|
PlotData::PlotData(const DVec &x, const DMatSample &y)
|
|
|
|
{
|
|
|
|
if (x.rows() != y[central].rows())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "x and y vector does not have the same size");
|
|
|
|
}
|
|
|
|
|
|
|
|
DMat d(x.rows(), 3);
|
|
|
|
string usingCmd, tmpFileName;
|
|
|
|
|
|
|
|
d.col(0) = x;
|
|
|
|
d.col(1) = y[central];
|
|
|
|
d.col(2) = y.variance().cwiseSqrt();
|
|
|
|
tmpFileName = dumpToTmpFile(d);
|
|
|
|
pushTmpFile(tmpFileName);
|
|
|
|
setCommand("'" + tmpFileName + "' u 1:2:3 w yerr");
|
|
|
|
}
|
|
|
|
|
|
|
|
PlotData::PlotData(const DMatSample &x, const DVec &y)
|
|
|
|
{
|
|
|
|
if (x[central].rows() != y.rows())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "x and y vector does not have the same size");
|
|
|
|
}
|
|
|
|
|
|
|
|
DMat d(x[central].rows(), 3), xerr, yerr;
|
|
|
|
string usingCmd, tmpFileName;
|
|
|
|
|
|
|
|
d.col(0) = x[central];
|
|
|
|
d.col(2) = y;
|
|
|
|
d.col(1) = x.variance().cwiseSqrt();
|
|
|
|
tmpFileName = dumpToTmpFile(d);
|
|
|
|
pushTmpFile(tmpFileName);
|
|
|
|
setCommand("'" + tmpFileName + "' u 1:3:2 w xerr");
|
|
|
|
}
|
|
|
|
|
2016-04-05 14:29:12 +01:00
|
|
|
PlotData::PlotData(const XYStatData &data, const Index i, const Index j)
|
2016-04-04 19:09:18 +01:00
|
|
|
{
|
|
|
|
string usingCmd, tmpFileName;
|
|
|
|
|
|
|
|
usingCmd = (data.isXExact(i)) ? "u 1:3:4 w yerr" : "u 1:3:2:4 w xyerr";
|
|
|
|
tmpFileName = dumpToTmpFile(data.getTable(i, j));
|
|
|
|
pushTmpFile(tmpFileName);
|
|
|
|
setCommand("'" + tmpFileName + "' " + usingCmd);
|
|
|
|
}
|
2014-03-03 18:33:53 +00:00
|
|
|
|
2015-02-13 15:26:00 +00:00
|
|
|
// PlotHLine constructor ///////////////////////////////////////////////////////
|
|
|
|
PlotHLine::PlotHLine(const double y)
|
|
|
|
{
|
|
|
|
setCommand(strFrom(y));
|
|
|
|
}
|
|
|
|
|
2016-06-14 12:33:27 +01:00
|
|
|
// PlotHBand constructor ///////////////////////////////////////////////////////
|
|
|
|
PlotBand::PlotBand(const double xMin, const double xMax, const double yMin,
|
|
|
|
const double yMax, const double opacity)
|
|
|
|
{
|
|
|
|
setCommand("'< printf \"%e %e\\n%e %e\\n%e %e\\n%e %e\\n%e %e\\n\" "
|
|
|
|
+ strFrom(xMin) + " " + strFrom(yMin) + " "
|
|
|
|
+ strFrom(xMax) + " " + strFrom(yMin) + " "
|
|
|
|
+ strFrom(xMax) + " " + strFrom(yMax) + " "
|
|
|
|
+ strFrom(xMin) + " " + strFrom(yMax) + " "
|
|
|
|
+ strFrom(xMin) + " " + strFrom(yMin)
|
|
|
|
+ "' u 1:2 w filledcurves closed fs solid " + strFrom(opacity)
|
|
|
|
+ " noborder");
|
|
|
|
}
|
|
|
|
|
2014-03-03 18:33:53 +00:00
|
|
|
// PlotFunction constructor ////////////////////////////////////////////////////
|
|
|
|
PlotFunction::PlotFunction(const DoubleFunction &function, const double xMin,
|
2014-04-17 12:19:13 +01:00
|
|
|
const double xMax, const unsigned int nPoint)
|
2014-03-03 18:33:53 +00:00
|
|
|
{
|
2014-04-17 12:19:13 +01:00
|
|
|
DMat d(nPoint, 2);
|
2014-03-03 18:33:53 +00:00
|
|
|
string tmpFileName;
|
2014-04-17 12:19:13 +01:00
|
|
|
double dx = (xMax - xMin)/static_cast<double>(nPoint - 1);
|
2014-03-03 18:33:53 +00:00
|
|
|
|
2014-04-17 12:19:13 +01:00
|
|
|
for (Index i = 0; i < nPoint; ++i)
|
2014-03-03 18:33:53 +00:00
|
|
|
{
|
|
|
|
d(i, 0) = xMin + i*dx;
|
|
|
|
d(i, 1) = function(d(i, 0));
|
|
|
|
}
|
|
|
|
tmpFileName = dumpToTmpFile(d);
|
|
|
|
pushTmpFile(tmpFileName);
|
|
|
|
setCommand("'" + tmpFileName + "' u 1:2 w lines");
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 11:29:17 +01:00
|
|
|
// PlotPredBand constructor ////////////////////////////////////////////////////
|
|
|
|
PlotPredBand::PlotPredBand(const DoubleFunctionSample &function,
|
|
|
|
const double xMin, const double xMax,
|
|
|
|
const unsigned int nPoint, const double opacity)
|
|
|
|
{
|
|
|
|
DMat dLow(nPoint, 2), dHigh(nPoint, 2);
|
|
|
|
DSample pred(function.size());
|
|
|
|
double dx = (xMax - xMin)/static_cast<double>(nPoint - 1);
|
|
|
|
string lowFileName, highFileName;
|
|
|
|
|
|
|
|
for (Index i = 0; i < nPoint; ++i)
|
|
|
|
{
|
|
|
|
double x = xMin + i*dx, err;
|
|
|
|
|
|
|
|
pred = function(x);
|
|
|
|
err = sqrt(pred.variance());
|
|
|
|
dLow(i, 0) = x;
|
|
|
|
dLow(i, 1) = pred[central] - err;
|
|
|
|
dHigh(i, 0) = x;
|
|
|
|
dHigh(i, 1) = pred[central] + err;
|
|
|
|
}
|
|
|
|
lowFileName = dumpToTmpFile(dLow);
|
|
|
|
highFileName = dumpToTmpFile(dHigh);
|
|
|
|
pushTmpFile(lowFileName);
|
|
|
|
pushTmpFile(highFileName);
|
|
|
|
setCommand("'< (cat " + lowFileName + "; tac " + highFileName +
|
|
|
|
"; head -n1 " + lowFileName + ")' u 1:2 w filledcurves closed" +
|
|
|
|
" fs solid " + strFrom(opacity) + " noborder");
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:18:54 +01:00
|
|
|
// PlotHistogram constructor ///////////////////////////////////////////////////
|
|
|
|
PlotHistogram::PlotHistogram(const Histogram &h)
|
|
|
|
{
|
|
|
|
DMat d(h.size(), 2);
|
|
|
|
string tmpFileName;
|
|
|
|
|
|
|
|
for (Index i = 0; i < h.size(); ++i)
|
|
|
|
{
|
|
|
|
d(i, 0) = h.getX(i);
|
|
|
|
d(i, 1) = h[i];
|
|
|
|
}
|
|
|
|
tmpFileName = dumpToTmpFile(d);
|
|
|
|
pushTmpFile(tmpFileName);
|
|
|
|
setCommand("'" + tmpFileName + "' u 1:2 w steps");
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:51:00 +01:00
|
|
|
// PlotMatrixNoRange constructor ///////////////////////////////////////////////
|
|
|
|
PlotMatrixNoRange::PlotMatrixNoRange(const DMat &m)
|
|
|
|
{
|
|
|
|
string tmpFileName = dumpToTmpFile(m);
|
|
|
|
|
|
|
|
setCommand("'" + tmpFileName + "' matrix w image");
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Plot modifiers *
|
|
|
|
******************************************************************************/
|
2014-06-02 16:04:20 +01:00
|
|
|
// Caption constructor /////////////////////////////////////////////////////////
|
|
|
|
Caption::Caption(const string &caption)
|
|
|
|
: caption_(caption)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Caption modifier ////////////////////////////////////////////////////////////
|
|
|
|
void Caption::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
option.caption = caption_;
|
|
|
|
}
|
|
|
|
|
2015-02-13 15:26:00 +00:00
|
|
|
// Label constructor ///////////////////////////////////////////////////////////
|
|
|
|
Label::Label(const string &label, const Axis axis)
|
|
|
|
: label_(label)
|
|
|
|
, axis_(axis)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Label modifier //////////////////////////////////////////////////////////////
|
|
|
|
void Label::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
option.label[static_cast<int>(axis_)] = label_;
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
// Color constructor ///////////////////////////////////////////////////////////
|
|
|
|
Color::Color(const string &color)
|
|
|
|
: color_(color)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Color modifier //////////////////////////////////////////////////////////////
|
|
|
|
void Color::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
option.lineColor = color_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogScale constructor ////////////////////////////////////////////////////////
|
|
|
|
LogScale::LogScale(const Axis axis)
|
|
|
|
: axis_(axis)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Logscale modifier ///////////////////////////////////////////////////////////
|
|
|
|
void LogScale::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
option.scaleMode[static_cast<int>(axis_)] |= Plot::Scale::log;
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:13:26 +01:00
|
|
|
// PlotRange constructors //////////////////////////////////////////////////////
|
|
|
|
PlotRange::PlotRange(const Axis axis)
|
|
|
|
: axis_(axis)
|
|
|
|
, reset_(true)
|
|
|
|
, min_(0.)
|
|
|
|
, max_(0.)
|
|
|
|
{}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
PlotRange::PlotRange(const Axis axis, const double min, const double max)
|
|
|
|
: axis_(axis)
|
2014-09-18 17:13:26 +01:00
|
|
|
, reset_(false)
|
2014-03-03 12:41:48 +00:00
|
|
|
, min_(min)
|
|
|
|
, max_(max)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// PlotRange modifier ///////////////////////////////////////////////////////////
|
|
|
|
void PlotRange::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
int a = static_cast<int>(axis_);
|
|
|
|
|
2014-09-18 17:13:26 +01:00
|
|
|
if (!reset_)
|
|
|
|
{
|
|
|
|
option.scaleMode[a] |= Plot::Scale::manual;
|
|
|
|
option.scale[a].min = min_;
|
|
|
|
option.scale[a].max = max_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
option.scaleMode[a] = Plot::Scale::reset;
|
|
|
|
}
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-02 16:04:20 +01:00
|
|
|
// Terminal constructor ////////////////////////////////////////////////////////
|
2014-04-18 11:29:17 +01:00
|
|
|
Terminal::Terminal(const string &terminal, const std::string &options)
|
|
|
|
: terminalCmd_(terminal + " " + options)
|
|
|
|
{}
|
|
|
|
|
2014-06-02 16:04:20 +01:00
|
|
|
// Terminal modifier ///////////////////////////////////////////////////////////
|
2014-04-18 11:29:17 +01:00
|
|
|
void Terminal::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
option.terminal = terminalCmd_;
|
|
|
|
}
|
|
|
|
|
2014-03-17 14:57:46 +00:00
|
|
|
// Title constructor ///////////////////////////////////////////////////////////
|
|
|
|
Title::Title(const string &title)
|
|
|
|
: title_(title)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Title modifier //////////////////////////////////////////////////////////////
|
|
|
|
void Title::operator()(PlotOptions &option) const
|
|
|
|
{
|
|
|
|
option.title = title_;
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:22:27 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Plot implementation *
|
|
|
|
******************************************************************************/
|
2014-04-03 09:00:53 +01:00
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
|
|
|
Plot::Plot(void)
|
|
|
|
{
|
|
|
|
initOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
// default options /////////////////////////////////////////////////////////////
|
|
|
|
void Plot::initOptions(void)
|
|
|
|
{
|
2015-05-13 17:38:47 +01:00
|
|
|
options_.terminal = "qt";
|
2014-04-03 09:00:53 +01:00
|
|
|
options_.output = "";
|
2014-06-02 16:04:20 +01:00
|
|
|
options_.caption = "";
|
2014-04-03 09:00:53 +01:00
|
|
|
options_.title = "";
|
|
|
|
options_.scaleMode[0] = Plot::Scale::reset;
|
|
|
|
options_.scaleMode[1] = Plot::Scale::reset;
|
|
|
|
options_.scale[0] = {0.0, 0.0};
|
|
|
|
options_.scale[1] = {0.0, 0.0};
|
|
|
|
options_.label[0] = "";
|
|
|
|
options_.label[1] = "";
|
|
|
|
options_.lineColor = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// plot reset //////////////////////////////////////////////////////////////////
|
|
|
|
void Plot::reset(void)
|
|
|
|
{
|
|
|
|
headCommand_.clear();
|
|
|
|
plotCommand_.clear();
|
2015-12-09 18:12:39 +00:00
|
|
|
tmpFileName_.clear();
|
2014-04-03 09:00:53 +01:00
|
|
|
initOptions();
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:22:27 +00:00
|
|
|
// plot objects ////////////////////////////////////////////////////////////////
|
2014-03-03 12:41:48 +00:00
|
|
|
Plot & Plot::operator<<(PlotObject &&command)
|
|
|
|
{
|
|
|
|
string commandStr;
|
|
|
|
|
|
|
|
while (command.gotTmpFile())
|
|
|
|
{
|
2014-09-18 17:13:26 +01:00
|
|
|
tmpFileName_.push_back(command.popTmpFile());
|
|
|
|
commandStr += "'" + tmpFileName_.back() + "' ";
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
commandStr = command.getCommand();
|
2015-06-26 20:33:57 +01:00
|
|
|
if (!commandStr.empty())
|
2014-03-03 12:41:48 +00:00
|
|
|
{
|
2015-06-26 20:33:57 +01:00
|
|
|
if (!options_.lineColor.empty())
|
|
|
|
{
|
|
|
|
commandStr += " lc " + options_.lineColor;
|
|
|
|
options_.lineColor = "";
|
|
|
|
}
|
|
|
|
if (options_.title.empty())
|
|
|
|
{
|
|
|
|
commandStr += " notitle";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandStr += " t '" + options_.title + "'";
|
|
|
|
options_.title = "";
|
|
|
|
}
|
|
|
|
plotCommand_.push_back(commandStr);
|
2014-03-03 18:33:53 +00:00
|
|
|
}
|
2015-06-26 20:33:57 +01:00
|
|
|
if (!command.getHeadCommand().empty())
|
2014-03-03 18:33:53 +00:00
|
|
|
{
|
2015-06-26 20:33:57 +01:00
|
|
|
headCommand_.push_back(command.getHeadCommand());
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plot & Plot::operator<<(PlotModifier &&modifier)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
modifier(options_);
|
2014-02-10 11:22:27 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find gnuplot ////////////////////////////////////////////////////////////////
|
|
|
|
void Plot::getProgramPath(void)
|
|
|
|
{
|
|
|
|
int i, j, lg;
|
|
|
|
char *path;
|
2014-02-12 18:33:33 +00:00
|
|
|
static char buf[MAX_PATH_LENGTH];
|
2014-02-10 11:22:27 +00:00
|
|
|
|
2014-04-17 12:19:13 +01:00
|
|
|
// trivial case: try in CWD
|
2014-02-10 11:22:27 +00:00
|
|
|
sprintf(buf,"./%s", gnuplotBin_.c_str()) ;
|
|
|
|
if (access(buf, X_OK) == 0)
|
|
|
|
{
|
|
|
|
sprintf(buf,".");
|
|
|
|
gnuplotPath_ = buf;
|
|
|
|
}
|
2014-04-17 12:19:13 +01:00
|
|
|
// try out in all paths given in the PATH variable
|
2014-02-10 11:22:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[0] = 0;
|
|
|
|
path = getenv("PATH") ;
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
for (i=0;path[i];)
|
|
|
|
{
|
2016-04-20 03:21:05 +01:00
|
|
|
for (j=i;(path[j]) and (path[j]!=':');j++);
|
2014-02-10 11:22:27 +00:00
|
|
|
lg = j - i;
|
|
|
|
strncpy(buf,path + i,(size_t)(lg));
|
|
|
|
if (lg == 0)
|
|
|
|
{
|
|
|
|
buf[lg++] = '.';
|
|
|
|
}
|
|
|
|
buf[lg++] = '/';
|
|
|
|
strcpy(buf + lg, gnuplotBin_.c_str());
|
|
|
|
if (access(buf, X_OK) == 0)
|
|
|
|
{
|
2014-04-17 12:19:13 +01:00
|
|
|
// found it!
|
2014-02-10 11:22:27 +00:00
|
|
|
break ;
|
|
|
|
}
|
|
|
|
buf[0] = 0;
|
|
|
|
i = j;
|
|
|
|
if (path[i] == ':') i++ ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LATAN_ERROR(System, "PATH variable not set");
|
|
|
|
}
|
2014-04-17 12:19:13 +01:00
|
|
|
// if the buffer is still empty, the command was not found
|
2014-02-10 11:22:27 +00:00
|
|
|
if (buf[0] == 0)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(System, "cannot find gnuplot in $PATH");
|
|
|
|
}
|
2014-04-17 12:19:13 +01:00
|
|
|
// otherwise truncate the command name to yield path only
|
2014-02-10 11:22:27 +00:00
|
|
|
lg = (int)(strlen(buf) - 1);
|
|
|
|
while (buf[lg]!='/')
|
|
|
|
{
|
|
|
|
buf[lg] = 0;
|
|
|
|
lg--;
|
|
|
|
}
|
|
|
|
buf[lg] = 0;
|
|
|
|
gnuplotPath_ = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plot parsing and output /////////////////////////////////////////////////////
|
|
|
|
void Plot::display(void)
|
|
|
|
{
|
2015-05-13 17:38:47 +01:00
|
|
|
int pid = fork();
|
|
|
|
|
|
|
|
if (pid == 0)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2015-05-13 17:38:47 +01:00
|
|
|
FILE *gnuplotPipe;
|
|
|
|
string command;
|
|
|
|
ostringstream scriptBuf;
|
|
|
|
|
|
|
|
if (!getenv("DISPLAY"))
|
|
|
|
{
|
|
|
|
LATAN_ERROR(System, "cannot find DISPLAY variable: is it set ?");
|
|
|
|
}
|
|
|
|
getProgramPath();
|
|
|
|
command = gnuplotPath_ + "/" + gnuplotBin_;
|
|
|
|
gnuplotPipe = popen(command.c_str(), "w");
|
|
|
|
if (!gnuplotPipe)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(System, "error starting gnuplot (command was '"
|
|
|
|
+ command + "')");
|
|
|
|
}
|
|
|
|
commandBuffer_.str("");
|
|
|
|
commandBuffer_ << *this << endl;
|
|
|
|
commandBuffer_ << "pause mouse close" << endl;
|
|
|
|
fprintf(gnuplotPipe, "%s", commandBuffer_.str().c_str());
|
|
|
|
if (pclose(gnuplotPipe) == -1)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(System, "problem closing communication to gnuplot");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2015-05-13 17:38:47 +01:00
|
|
|
else if (pid == -1)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2015-05-13 17:38:47 +01:00
|
|
|
perror("fork error");
|
|
|
|
LATAN_ERROR(System, "problem forking to the process handling gnuplot");
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:13:26 +01:00
|
|
|
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
|
2015-09-28 16:32:37 +01:00
|
|
|
if (mkdir(dirName))
|
2014-09-18 17:13:26 +01:00
|
|
|
{
|
2015-09-28 16:32:37 +01:00
|
|
|
LATAN_ERROR(Io, "impossible to create directory '" + dirName + "'");
|
2014-09-18 17:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:22:27 +00:00
|
|
|
ostream & Latan::operator<<(ostream &out, const Plot &plot)
|
|
|
|
{
|
|
|
|
std::string begin, end;
|
2014-03-03 12:41:48 +00:00
|
|
|
int x = static_cast<int>(Axis::x), y = static_cast<int>(Axis::y);
|
2014-02-10 11:22:27 +00:00
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
if (!plot.options_.terminal.empty())
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
out << "set term " << plot.options_.terminal << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (!plot.options_.output.empty())
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-09-18 17:13:26 +01:00
|
|
|
out << "set output '" << plot.options_.output << "'" << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2014-06-02 16:04:20 +01:00
|
|
|
if (!plot.options_.caption.empty())
|
|
|
|
{
|
|
|
|
out << "set title '" << plot.options_.caption << "'" << endl;
|
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (plot.options_.scaleMode[x] & Plot::Scale::manual)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
out << "xMin = " << plot.options_.scale[x].min << endl;
|
|
|
|
out << "xMax = " << plot.options_.scale[x].max << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (plot.options_.scaleMode[y] & Plot::Scale::manual)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
out << "yMin = " << plot.options_.scale[y].min << endl;
|
|
|
|
out << "yMax = " << plot.options_.scale[y].max << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (!plot.options_.title.empty())
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
out << "set title '" << plot.options_.title << "'" << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (plot.options_.scaleMode[x] & Plot::Scale::manual)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
|
|
|
out << "set xrange [xMin:xMax]" << endl;
|
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (plot.options_.scaleMode[y] & Plot::Scale::manual)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
|
|
|
out << "set yrange [yMin:yMax]" << endl;
|
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (plot.options_.scaleMode[x] & Plot::Scale::log)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
|
|
|
out << "set log x" << endl;
|
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (plot.options_.scaleMode[y] & Plot::Scale::log)
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
|
|
|
out << "set log y" << endl;
|
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (!plot.options_.label[x].empty())
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
out << "set xlabel '" << plot.options_.label[x] << "'" << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
2014-03-03 12:41:48 +00:00
|
|
|
if (!plot.options_.label[y].empty())
|
2014-02-10 11:22:27 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
out << "set ylabel '" << plot.options_.label[y] << "'" << endl;
|
2014-02-10 11:22:27 +00:00
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < plot.headCommand_.size(); ++i)
|
|
|
|
{
|
|
|
|
out << plot.headCommand_[i] << endl;
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < plot.plotCommand_.size(); ++i)
|
|
|
|
{
|
|
|
|
begin = (i == 0) ? "plot " : " ";
|
|
|
|
end = (i == plot.plotCommand_.size() - 1) ? "" : ",\\";
|
|
|
|
out << begin << plot.plotCommand_[i] << end << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|