Blame view

src/ParamOutputImpl/Plot/InstantPlot/PlotFunction.cc 23.5 KB
99ff615c   Menouard AZIB   Change nbEchantil...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <boost/lexical_cast.hpp>
#include <fstream>
#include <iomanip>

#include "AxesNode.hh"
#include "AxisLegendManager.hh"
#include "ParamMgr.hh"
#include "ParamsNode.hh"
#include "PlPlotUtil.hh"
#include "PlotFunction.hh"
#include "PlotLogger.hh"
#include "PlotOutput.hh"
#include "TimeUtil.hh"
#include "fonctions/fourier/DiscreteFourierTransform.cc"
#include "fonctions/fourier/DiscreteFourierTransform.hh"
#include <algorithm>
3cf11811   Menouard AZIB   FFT Validated and...
17
#include <complex>
99ff615c   Menouard AZIB   Change nbEchantil...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <limits>
#include <numeric>

#include "DefaultTimeAxisDecorator.hh"

#include "TimeAxisDecorator.hh"

using namespace AMDA::Parameters;
using namespace AMDA::Info;

namespace plot
{
    QSASConfig *PlotFunction::qsasconfig = NULL;

    PlotFunction::PlotFunction(AMDA::Parameters::ParameterManager &manager,
                               boost::shared_ptr<Panel> panel, TimeAxisDecorator *timeAxisDecorator) : PanelPlotOutput(manager, panel), function(PlotFunction::Function::NONE), abscisse("Time. ", "(s)", Abscisse::Abscisse_Type::TIME)

    {
07bf1e7c   Menouard AZIB   EveryThing works ...
36
        LOG4CXX_DEBUG(gLogger, "PlotFunction::Function to apply " << function);
99ff615c   Menouard AZIB   Change nbEchantil...
37
38
39
40
41
42
43
44
45
46
47
48
        setTimeAxisDecorator(std::shared_ptr<TimeAxisDecorator>(timeAxisDecorator));
    }

    PlotFunction::~PlotFunction()
    {
        if (qsasconfig != NULL)
        {
            free(qsasconfig);
            qsasconfig = nullptr;
        }
    }

3cf11811   Menouard AZIB   FFT Validated and...
49
50
    void PlotFunction::createParameters(std::list<std::string> &usedParametersId_)
    {
07bf1e7c   Menouard AZIB   EveryThing works ...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        for (ParameterAxesList::iterator it = _parameterAxesList.begin();
             it != _parameterAxesList.end(); ++it)
        {
            AMDA::Parameters::ParameterSPtr originalParam = _parameterManager.getParameter(it->_originalParamId);
            // Time Sampling to resample the origin param
            const double timeSampling = originalParam->getDataWriterTemplate()->getMinSampling();
            // Use max Gap in order to interpolate and deleting holes
            const int gap = 1000000;

            // Resample the origin param to cearte a new one
            ParameterSPtr usedParam = createSampledParameter(originalParam, timeSampling, gap);
            // Add the created parameter to list of usedParametersId_
            if (std::find(usedParametersId_.begin(), usedParametersId_.end(), usedParam->getId()) == usedParametersId_.end())
                usedParametersId_.push_back(usedParam->getId());

            // Add the created parameter to Series and Spectro
            std::vector<SeriesProperties>::iterator ity;
            for (ity = it->getYSeriePropertiesList().begin(); ity != it->getYSeriePropertiesList().end();
                 ++ity)
            {
                ity->setParamId(usedParam->getId());
                if (!PlotFunction::isFourier())
                {
                    ity->setXAxisId(X_AXIS_ID1);
                }
                else
                {
                    ity->setXAxisId(X_AXIS_ID);
                }
            }

            if (it->getSpectroProperties() != nullptr)
            {
                PlotFunction::isSpectro = true;
                it->getSpectroProperties()->setParamId(usedParam->getId());
                it->getSpectroProperties()->setYAxisId(X_AXIS_ID);
            }
        }
3cf11811   Menouard AZIB   FFT Validated and...
89
90
    }

df056382   Benjamin Renard   Remove some logs
91
    void PlotFunction::writeContext(ContextFileWriter &writer, AMDA::Parameters::TimeIntervalList::iterator /*currentTimeInterval*/)
3cf11811   Menouard AZIB   FFT Validated and...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    {
        writer.startElement("panel");
        _panel->writeContext(_pls, writer);

        writer.startElement("parameters");

        for (ParameterAxesList::iterator it = _parameterAxesList.begin();
             it != _parameterAxesList.end(); ++it)
        {
            AMDA::Parameters::ParameterSPtr originalParam =
                _parameterManager.getParameter(it->_originalParamId);

            // Retrieve ParamInfo Manager
            ParamMgr *piMgr = ParamMgr::getInstance();
            ParamInfoSPtr paramInfo = piMgr->getParamInfoFromId(it->_originalParamId);

            double resol = originalParam->getDataWriterTemplate()->getMinSampling();
            std::string resolstr = std::to_string(resol);

            writer.startElement("parameter");
            writer.addAttribute("id", paramInfo->getName().c_str());
            writer.addAttribute("name", paramInfo->getName().c_str());
            writer.addAttribute("shortname", paramInfo->getShortName().c_str());
            writer.addAttribute("MinSampling", resolstr.c_str());
            writer.addAttribute("unit", paramInfo->getUnits().c_str());
3cf11811   Menouard AZIB   FFT Validated and...
117
118
119
120
121
122
123
        }
        writer.endElement();
        writer.endElement();
    }

    void PlotFunction::applyFunction()
    {
3cf11811   Menouard AZIB   FFT Validated and...
124
125
        for (auto parameter : _parameterAxesList)
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
126
            SeriesProperties lSeries;
3cf11811   Menouard AZIB   FFT Validated and...
127
128
            for (auto lSeries : parameter.getYSeriePropertiesList())
            {
07bf1e7c   Menouard AZIB   EveryThing works ...
129
                plot::ParameterData &data = (*_pParameterValues)[lSeries.getParamId()];
3cf11811   Menouard AZIB   FFT Validated and...
130
131
                for (auto lIndex : lSeries.getIndexList(_pParameterValues))
                {
3cf11811   Menouard AZIB   FFT Validated and...
132
133
134
                    PlotFunction::compute(lIndex, data, &lSeries, nullptr, parameter._originalParamId);
                }
            }
3cf11811   Menouard AZIB   FFT Validated and...
135

3cf11811   Menouard AZIB   FFT Validated and...
136
137
            if (parameter.getSpectroProperties() != nullptr)
            {
07bf1e7c   Menouard AZIB   EveryThing works ...
138
139
                plot::ParameterData &data = (*_pParameterValues)[parameter.getSpectroProperties()->getParamId()];
                PlotFunction::compute(AMDA::Common::ParameterIndexComponent(-1, -1), data, nullptr, parameter.getSpectroProperties().get(), parameter._originalParamId);
3cf11811   Menouard AZIB   FFT Validated and...
140
141
            }
        }
07bf1e7c   Menouard AZIB   EveryThing works ...
142
    }
3cf11811   Menouard AZIB   FFT Validated and...
143

07bf1e7c   Menouard AZIB   EveryThing works ...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    void PlotFunction::setAxisRange()
    {
        double minValue = 0;
        double maxValue = 0;
        /************************************** X AXIS RANGE *********************************************/
        PlotFunction::getMinMax(xValuesMap, &minValue, &maxValue);
        boost::shared_ptr<Axis> xAxis = nullptr;
        if (!PlotFunction::isFourier() && !PlotFunction::isSpectro)
        {
            xAxis = _panel->getAxis(X_AXIS_ID1);
        }
        else
        {
            xAxis = _panel->getAxis(X_AXIS_ID);
        }

9d60b18e   Menouard AZIB   Compute Periods c...
160
        Range lAxisRange = xAxis->Axis::getRange();
ec9b5ba2   Menouard AZIB   Everything is wor...
161
162
163
        /*
         * here we should use pow10 in case if min and max are computed as log values
         */
07bf1e7c   Menouard AZIB   EveryThing works ...
164
165
166
167
        if (abscisseScale == Axis::Scale::LOGARITHMIC)
        {
            minValue = pow10(minValue);
            maxValue = pow10(maxValue);
ac7eebb0   Menouard AZIB   Ignore zero point...
168
            lAxisRange._extend = false;
07bf1e7c   Menouard AZIB   EveryThing works ...
169
        }
9d60b18e   Menouard AZIB   Compute Periods c...
170

07bf1e7c   Menouard AZIB   EveryThing works ...
171
172
173
174
175
176
        lAxisRange.setMin(minValue);
        lAxisRange.setMax(maxValue);
        xAxis->setRange(lAxisRange);
        xAxis->_used = true;
        /************************************** Y AXIS RANGE *********************************************/
        PlotFunction::getMinMax(yValuesMap, &minValue, &maxValue);
9d60b18e   Menouard AZIB   Compute Periods c...
177
178
        boost::shared_ptr<Axis> lYAxis = _panel->getAxis(Y_AXIS_ID);
        lAxisRange = lYAxis->Axis::getRange();
07bf1e7c   Menouard AZIB   EveryThing works ...
179
180
181
182
        if (ordonneeScale == Axis::Scale::LOGARITHMIC)
        {
            minValue = pow10(minValue);
            maxValue = pow10(maxValue);
ac7eebb0   Menouard AZIB   Ignore zero point...
183
            lAxisRange._extend = false;
07bf1e7c   Menouard AZIB   EveryThing works ...
184
        }
07bf1e7c   Menouard AZIB   EveryThing works ...
185
186
187
188
        lAxisRange.setMin(minValue);
        lAxisRange.setMax(maxValue);
        lYAxis->setRange(lAxisRange);
        lYAxis->_used = true;
3cf11811   Menouard AZIB   FFT Validated and...
189
190
    }

99ff615c   Menouard AZIB   Change nbEchantil...
191
192
193
194
195
196
    void PlotFunction::drawSeries(double startDate, double stopDate, int intervalIndex, std::string pParamId,
                                  SeriesProperties &pSerie,
                                  AMDA::Common::ParameterIndexComponent pParamIndex,
                                  ParameterAxes &param, bool moreThanOneSerieForAxis)
    {
        std::string id = std::to_string(pSerie.getId()) + "_" + pSerie.getParamId() + "_" + std::to_string(pParamIndex.getDim1Index());
07bf1e7c   Menouard AZIB   EveryThing works ...
197

99ff615c   Menouard AZIB   Change nbEchantil...
198
199
        std::vector<double> xValues = xValuesMap[id];
        std::vector<double> yValues = yValuesMap[id];
99ff615c   Menouard AZIB   Change nbEchantil...
200
201
202
203
204
205
206

        double *coloredComputedValues = NULL;
        int nbValues = yValues.size();

        double *yValuesTemp = &yValues[0];
        double *xValuesTemp = &xValues[0];

99ff615c   Menouard AZIB   Change nbEchantil...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        PanelPlotOutput::drawSeries(startDate, stopDate, intervalIndex, pParamId,
                                    pSerie, pParamIndex, param, moreThanOneSerieForAxis);
        // draw serie
        Color lineColor = getSerieLineColor(pSerie, moreThanOneSerieForAxis);
        Color symbolColor = getSerieSymbolColor(pSerie, lineColor);

        drawSymbols(
            pSerie.getSymbolProperties().getType(),
            pSerie.getSymbolProperties().getSize(), 1.,
            symbolColor,
            nbValues, xValuesTemp, yValuesTemp, coloredComputedValues);

        drawLines(
            pSerie.getLineProperties().getType(),
            pSerie.getLineProperties().getStyle(),
5a638eb1   Menouard AZIB   Adding comments
222
            pSerie.getLineProperties().getWidth(),
99ff615c   Menouard AZIB   Change nbEchantil...
223
224
            lineColor,
            nbValues, xValuesTemp, yValuesTemp, coloredComputedValues);
07bf1e7c   Menouard AZIB   EveryThing works ...
225
226

        PanelPlotOutput::addSerieToParamsLegend(pSerie, pParamIndex, pParamId, lineColor, symbolColor, startDate, stopDate, intervalIndex);
99ff615c   Menouard AZIB   Change nbEchantil...
227
228
    }

07bf1e7c   Menouard AZIB   EveryThing works ...
229
    void PlotFunction::drawSeriesForSpectro(double startDate, double stopDate, int intervalIndex)
99ff615c   Menouard AZIB   Change nbEchantil...
230
    {
99ff615c   Menouard AZIB   Change nbEchantil...
231
232
        for (auto parameter : _parameterAxesList)
        {
3cf11811   Menouard AZIB   FFT Validated and...
233
            if (parameter.getSpectroProperties() != nullptr)
99ff615c   Menouard AZIB   Change nbEchantil...
234
            {
3cf11811   Menouard AZIB   FFT Validated and...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
                std::string pramId = parameter.getSpectroProperties()->getParamId();
                std::vector<double> xValues = xValuesMap[pramId];
                std::vector<double> yValues = yValuesMap[pramId];
                plot::SeriesProperties *pSerie = new plot::SeriesProperties();

                double *coloredComputedValues = NULL;
                int nbValues = yValues.size();

                double *yValuesTemp = &yValues[0];
                double *xValuesTemp = &xValues[0];

                pSerie->setXAxisId(X_AXIS_ID);
                pSerie->setYAxisId(Y_AXIS_ID);

07bf1e7c   Menouard AZIB   EveryThing works ...
249
                PanelPlotOutput::drawSeries(startDate, stopDate, intervalIndex, "",
3cf11811   Menouard AZIB   FFT Validated and...
250
251
                                            *pSerie, AMDA::Common::ParameterIndexComponent(-1, -1), parameter, false);

07bf1e7c   Menouard AZIB   EveryThing works ...
252
253
                Color lineColor(0, 0, 255);

3cf11811   Menouard AZIB   FFT Validated and...
254
255
256
257
258
259
260
261
262
263
264
                Color symbolColor = getSerieSymbolColor(*pSerie, lineColor);

                drawSymbols(
                    pSerie->getSymbolProperties().getType(),
                    pSerie->getSymbolProperties().getSize(), 1.,
                    symbolColor,
                    nbValues, xValuesTemp, yValuesTemp, coloredComputedValues);

                drawLines(
                    pSerie->getLineProperties().getType(),
                    pSerie->getLineProperties().getStyle(),
07bf1e7c   Menouard AZIB   EveryThing works ...
265
                    3.0,
3cf11811   Menouard AZIB   FFT Validated and...
266
267
                    lineColor,
                    nbValues, xValuesTemp, yValuesTemp, coloredComputedValues);
99ff615c   Menouard AZIB   Change nbEchantil...
268
269
            }
        }
99ff615c   Menouard AZIB   Change nbEchantil...
270
271
    }

07bf1e7c   Menouard AZIB   EveryThing works ...
272
    void PlotFunction::configureAxisLegend()
99ff615c   Menouard AZIB   Change nbEchantil...
273
    {
07bf1e7c   Menouard AZIB   EveryThing works ...
274
        if (!PlotFunction::isFourier() && !PlotFunction::isSpectro)
3cf11811   Menouard AZIB   FFT Validated and...
275
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
276
            AxisLegendManager::configureYAxisLegendForSeries(this);
3cf11811   Menouard AZIB   FFT Validated and...
277
        }
07bf1e7c   Menouard AZIB   EveryThing works ...
278
        else if (!PlotFunction::isFourier() && PlotFunction::isSpectro)
3cf11811   Menouard AZIB   FFT Validated and...
279
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
            AxisLegendManager::configureYAxisLegendForSpectro(this);
            for (auto parameter : _parameterAxesList)
            {
                boost::shared_ptr<Axis> lYAxis = _panel->getAxis(Y_AXIS_ID);
                AxisLegendManager::setAxisLegendForSpectro(this, lYAxis, parameter.getSpectroProperties()->getParamId());
            }
        }
        else if (PlotFunction::isFourier())
        {
            boost::shared_ptr<Axis> lYAxis = _panel->getAxis(Y_AXIS_ID);
            std::vector<Color> colorsLegendsSeriesFFT;
            if (!PlotFunction::isSpectro)
            {
                AxisLegendManager::configureYAxisLegendForSeries(this);
                for (std::size_t u = 0; u < lYAxis->_legend.getLabels().size(); u++)
                    colorsLegendsSeriesFFT.push_back(lYAxis->_legend.getLabels()[u]._color);
                lYAxis->_legend.clearLabels();
            }
            for (std::size_t pIndex = 0; pIndex < _parameterAxesList.size(); pIndex++)
            {
                ParameterAxes parameter = _parameterAxesList[pIndex];
                AMDA::Parameters::ParameterSPtr originalParam =
                    _parameterManager.getParameter(parameter._originalParamId);

                // Retrieve ParamInfo Manager
                ParamMgr *piMgr = ParamMgr::getInstance();
                ParamInfoSPtr paramInfo = piMgr->getParamInfoFromId(parameter._originalParamId);

                std::string unit = AxisLegendManager::getTransformedUnits(paramInfo->getUnits());
9d60b18e   Menouard AZIB   Compute Periods c...
309
                unit = "[" + unit + "]#u2";
07bf1e7c   Menouard AZIB   EveryThing works ...
310
311
312
313
314
315
316
317
318
319
320
321
                if (PlotFunction::isSpectro)
                {
                    Label label(lYAxis->_legend.getFont(), lYAxis->_legend.getColor());
                    label._text = unit;
                    lYAxis->_legend.setLabel(label);
                }
                else
                {
                    Color compLegendColor = colorsLegendsSeriesFFT[pIndex];
                    Label label(lYAxis->_legend.getFont(), compLegendColor);
                    label._text = unit;
                    lYAxis->_legend.pushLabel(label);
07bf1e7c   Menouard AZIB   EveryThing works ...
322
323
                }
            }
3cf11811   Menouard AZIB   FFT Validated and...
324
        }
99ff615c   Menouard AZIB   Change nbEchantil...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
    }

    void PlotFunction::getMinMax(std::map<std::string, std::vector<double>> dataMap, double *minToFill, double *maxToFill)
    {
        double maxValue = INT_MIN;
        double minValue = INT_MAX;
        std::map<std::string, std::vector<double>>::iterator it;
        for (it = dataMap.begin(); it != dataMap.end(); it++)
        {
            std::vector<double> data = it->second;
            double max_x = *max_element(data.begin(), data.end());
            double min_x = *min_element(data.begin(), data.end());
            if (max_x > maxValue)
                maxValue = max_x;
            if (min_x < minValue)
                minValue = min_x;
        }
07bf1e7c   Menouard AZIB   EveryThing works ...
342

83cbfd9f   Menouard AZIB   Update Range Bounds
343
344
        minValue = minValue - abs(minValue) * 0.01;
        maxValue = maxValue + abs(maxValue) * 0.01;
07bf1e7c   Menouard AZIB   EveryThing works ...
345

99ff615c   Menouard AZIB   Change nbEchantil...
346
347
348
349
        *minToFill = minValue;
        *maxToFill = maxValue;
    }

3cf11811   Menouard AZIB   FFT Validated and...
350
    void PlotFunction::compute(AMDA::Common::ParameterIndexComponent pParamIndex, plot::ParameterData &data, SeriesProperties *pSerie, SpectroProperties *pSpectro, std::string paramOriginID)
99ff615c   Menouard AZIB   Change nbEchantil...
351
    {
3cf11811   Menouard AZIB   FFT Validated and...
352
353
        std::vector<double> xValues;
        std::vector<double> yValues;
99ff615c   Menouard AZIB   Change nbEchantil...
354

3cf11811   Menouard AZIB   FFT Validated and...
355
        if (pSpectro != nullptr)
3cf11811   Menouard AZIB   FFT Validated and...
356
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
357
358
359
360
361
            ParameterSPtr p = _parameterManager.getParameter(pSpectro->getParamId());
            boost::shared_ptr<AMDA::Info::ParamTable> rightTableSPtr;
            AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(p->getInfoId());
            if (paramInfo != nullptr)
                rightTableSPtr = paramInfo->getTable(pSpectro->getRelatedDim());
99ff615c   Menouard AZIB   Change nbEchantil...
362

07bf1e7c   Menouard AZIB   EveryThing works ...
363
            for (auto index : pSpectro->getIndexes())
3cf11811   Menouard AZIB   FFT Validated and...
364
            {
07bf1e7c   Menouard AZIB   EveryThing works ...
365
                AMDA::Info::t_TableBound crtBound;
3cf11811   Menouard AZIB   FFT Validated and...
366
                if (pSpectro->getRelatedDim() == 0)
07bf1e7c   Menouard AZIB   EveryThing works ...
367
368
369
370
                {
                    crtBound = rightTableSPtr->getBound(&_parameterManager, index.getDim1Index());
                    xValues.push_back((crtBound.min + crtBound.max) / 2.0);
                }
3cf11811   Menouard AZIB   FFT Validated and...
371
                else
07bf1e7c   Menouard AZIB   EveryThing works ...
372
373
374
375
                {
                    crtBound = rightTableSPtr->getBound(&_parameterManager, index.getDim2Index());
                    xValues.push_back((crtBound.min + crtBound.max) / 2.0);
                }
3cf11811   Menouard AZIB   FFT Validated and...
376

07bf1e7c   Menouard AZIB   EveryThing works ...
377
378
379
380
381
                double sum = 0.0;
                for (int i = 0; i < data.getSize(); ++i)
                {
                    sum += data.getValues(index, 0)[i];
                }
3cf11811   Menouard AZIB   FFT Validated and...
382

07bf1e7c   Menouard AZIB   EveryThing works ...
383
384
385
                if (function == PlotFunction::Function::AVG)
                    sum /= data.getSize();
                yValues.push_back(sum);
99ff615c   Menouard AZIB   Change nbEchantil...
386
387
            }
        }
07bf1e7c   Menouard AZIB   EveryThing works ...
388
        else
3cf11811   Menouard AZIB   FFT Validated and...
389
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
390
            for (int index_time = 0; index_time < data.getSize(); index_time++)
3cf11811   Menouard AZIB   FFT Validated and...
391
            {
07bf1e7c   Menouard AZIB   EveryThing works ...
392
393
                double time = data.getTimes()[index_time];
                xValues.push_back(time);
3cf11811   Menouard AZIB   FFT Validated and...
394

07bf1e7c   Menouard AZIB   EveryThing works ...
395
396
                double valueTemp = data.getValues(pParamIndex, 0)[index_time];
                yValues.push_back(valueTemp);
3cf11811   Menouard AZIB   FFT Validated and...
397
            }
99ff615c   Menouard AZIB   Change nbEchantil...
398

b57f2949   Menouard AZIB   Add comments
399
400
401
402
403
            if (function == PlotFunction::Function::SUM)
            {
                double sum = std::accumulate(yValues.begin(), yValues.end(), 0);
                yValues.assign(yValues.size(), sum);
            }
3cf11811   Menouard AZIB   FFT Validated and...
404

07bf1e7c   Menouard AZIB   EveryThing works ...
405
            if (function == PlotFunction::Function::AVG)
3cf11811   Menouard AZIB   FFT Validated and...
406
            {
b57f2949   Menouard AZIB   Add comments
407
408
                double sum = std::accumulate(yValues.begin(), yValues.end(), 0);
                yValues.assign(yValues.size(), sum);
07bf1e7c   Menouard AZIB   EveryThing works ...
409
410
411
                const double taille = yValues.size();
                transform(yValues.begin(), yValues.end(), yValues.begin(), [taille](double &c)
                          { return c / taille; });
3cf11811   Menouard AZIB   FFT Validated and...
412
            }
07bf1e7c   Menouard AZIB   EveryThing works ...
413
414
415
416
417
418
        }

        if (function == PlotFunction::Function::FFT || function == PlotFunction::Function::DFT)
        {
            AMDA::Parameters::ParameterSPtr originalParam = _parameterManager.getParameter(paramOriginID);
            const double timeSampling = originalParam->getDataWriterTemplate()->getMinSampling();
3cf11811   Menouard AZIB   FFT Validated and...
419

ac7eebb0   Menouard AZIB   Ignore zero point...
420
421
            LOG4CXX_DEBUG(gLogger, "Fourier:: Signal Size = " << yValues.size());

07bf1e7c   Menouard AZIB   EveryThing works ...
422
423
424
425
426
427
            DiscreteFourierTransform<double, double> DFT(yValues, timeSampling);
            const bool computeFFT = (function == PlotFunction::Function::FFT);
            DFT.compute(computeFFT);

            std::vector<std::complex<double>> phasors = DFT.getPhasors();
            yValues = DFT.computeDSP(phasors);
ac7eebb0   Menouard AZIB   Ignore zero point...
428
429
            if (computeFFT)
                LOG4CXX_DEBUG(gLogger, "FFT Next Power of 2 = " << yValues.size() << " " << DFT.highestPowerof2(8211));
99ff615c   Menouard AZIB   Change nbEchantil...
430
431

            if (abscisse.getType() == Abscisse::Abscisse_Type::FREQUENCY)
07bf1e7c   Menouard AZIB   EveryThing works ...
432
                xValues = DFT.getFreq(phasors, 1.0 / timeSampling); // DFT.getFrequences();
ec9b5ba2   Menouard AZIB   Everything is wor...
433
434
            else
                xValues = DFT.getPeriods(phasors, 1.0 / timeSampling); // DFT.getFrequences();
99ff615c   Menouard AZIB   Change nbEchantil...
435
436
        }

ac7eebb0   Menouard AZIB   Ignore zero point...
437
        PlotFunction::applyScale(xValues, yValues);
07bf1e7c   Menouard AZIB   EveryThing works ...
438
439
440
441
442
443
444
445
446
447

        std::string id = "";
        if (pSerie != nullptr)
        {
            id = std::to_string(pSerie->getId()) + "_" + pSerie->getParamId() + "_" + std::to_string(pParamIndex.getDim1Index());
        }
        else
        {
            id = pSpectro->getParamId();
        }
99ff615c   Menouard AZIB   Change nbEchantil...
448
449
450
451
452

        PlotFunction::xValuesMap[id] = xValues;
        PlotFunction::yValuesMap[id] = yValues;
    }

ac7eebb0   Menouard AZIB   Ignore zero point...
453
    void PlotFunction::applyScale(std::vector<double> &vectX, std::vector<double> &vectY)
07bf1e7c   Menouard AZIB   EveryThing works ...
454
    {
ac7eebb0   Menouard AZIB   Ignore zero point...
455
456
457
        std::vector<double> xtemp;
        std::vector<double> ytemp;
        for (std::size_t i = 0; i < vectX.size(); i++)
07bf1e7c   Menouard AZIB   EveryThing works ...
458
        {
1e90d1e7   Menouard AZIB   if Log scale is s...
459
460
461
462
            double xValue = vectX[i];
            double yValue = vectY[i];

            if (abscisseScale == Axis::Scale::LOGARITHMIC)
07bf1e7c   Menouard AZIB   EveryThing works ...
463
            {
1e90d1e7   Menouard AZIB   if Log scale is s...
464
465
466
467
468
469
470
                if (xValue <= 0)
                {
                    LOG4CXX_DEBUG(gLogger, xValue << " Negative value found, no log calculated");
                    xValue = NaN;
                }
                else
                    xValue = log10(xValue);
07bf1e7c   Menouard AZIB   EveryThing works ...
471
            }
1e90d1e7   Menouard AZIB   if Log scale is s...
472
473

            if (ordonneeScale == Axis::Scale::LOGARITHMIC)
07bf1e7c   Menouard AZIB   EveryThing works ...
474
            {
1e90d1e7   Menouard AZIB   if Log scale is s...
475
476
477
478
479
                if (yValue <= 0)
                {
                    LOG4CXX_DEBUG(gLogger, yValue << " Negative value found, no log calculated");
                    yValue = NaN;
                }
ac7eebb0   Menouard AZIB   Ignore zero point...
480
                else
1e90d1e7   Menouard AZIB   if Log scale is s...
481
                    yValue = log10(yValue);
07bf1e7c   Menouard AZIB   EveryThing works ...
482
            }
1e90d1e7   Menouard AZIB   if Log scale is s...
483
484
            xtemp.push_back(xValue);
            ytemp.push_back(yValue);
07bf1e7c   Menouard AZIB   EveryThing works ...
485
        }
ac7eebb0   Menouard AZIB   Ignore zero point...
486
487
        vectX = xtemp;
        vectY = ytemp;
07bf1e7c   Menouard AZIB   EveryThing works ...
488
489
    }

99ff615c   Menouard AZIB   Change nbEchantil...
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    void PlotFunction::drawStartDate(std::string _timeFormat, double startTime, double stopTime)
    {
        Font font(_panel->getFont());
        Bounds lPanelBounds(_panel->getBoundsInPlPage());

        PLFLT lXMin, lXMax, lYMin, lYMax;
        _pls->gvpd(lXMin, lXMax, lYMin, lYMax);

        float lPosition = 0.0;
        // display it one line above the main panel bottom border :
        float disp = -1;

        long int lTime = static_cast<long int>(startTime);
        tm *lTimeTm = gmtime(&lTime);
        char lTimeChr[80];

        // Format date.
        strftime(lTimeChr, 80,
                 getPlStartTimeFormat(_timeFormat,
                                      startTime,
                                      stopTime)
                     .c_str(),
                 lTimeTm);

        PLFLT mxmin, mxmax, mymin, mymax;
        plgspa(&mxmin, &mxmax, &mymin, &mymax);
        float x_subpage_per_mm = 1. / (mxmax - mxmin);

        LOG4CXX_DEBUG(gLogger, "Start date to draw : " << lTimeChr);

        // Set font
        PlPlotUtil::setPlFont(font);

        PLFLT dateWidthInMm;
        dateWidthInMm = plstrl(lTimeChr);

        // set viewport for start date :
        _pls->vpor(lXMin, lXMax, lPanelBounds._y, lPanelBounds._y + lPanelBounds._height);

        lPosition = 0.;
        if (dateWidthInMm * x_subpage_per_mm > lXMin - lPanelBounds._x)
            lPosition = (dateWidthInMm * x_subpage_per_mm - lXMin + lPanelBounds._x) / (lXMax - lXMin);
        _pls->mtex("b", disp, lPosition, 1., lTimeChr);

        // restore viewport :
        _pls->vpor(lXMin, lXMax, lYMin, lYMax);
    }

    void PlotFunction::preparePlotArea(double startTime, double stopTime, int intervalIndex)
    {
3cf11811   Menouard AZIB   FFT Validated and...
540
        PlotFunction::applyFunction();
07bf1e7c   Menouard AZIB   EveryThing works ...
541
        PlotFunction::setAxisRange();
99ff615c   Menouard AZIB   Change nbEchantil...
542

07bf1e7c   Menouard AZIB   EveryThing works ...
543
        if (!PlotFunction::isFourier())
99ff615c   Menouard AZIB   Change nbEchantil...
544
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
545
            boost::shared_ptr<Axis> xAxis = _panel->getAxis(X_AXIS_ID1);
99ff615c   Menouard AZIB   Change nbEchantil...
546
547
548
            getTimeAxisDecorator()->configure(this, dynamic_cast<TimeAxis *>(xAxis.get()), startTime, stopTime, _pParameterValues);
            _pls->timefmt(getTimeAxisDecorator()->getPlFormat().c_str());
        }
99ff615c   Menouard AZIB   Change nbEchantil...
549

07bf1e7c   Menouard AZIB   EveryThing works ...
550
        PlotFunction::configureAxisLegend();
3cf11811   Menouard AZIB   FFT Validated and...
551
        PanelPlotOutput::preparePlotArea(startTime, stopTime, intervalIndex);
347ee32b   Menouard AZIB   Change ParamNbPoi...
552
    }
99ff615c   Menouard AZIB   Change nbEchantil...
553

347ee32b   Menouard AZIB   Change ParamNbPoi...
554
555
556
557
    /**
     * @brief draw the plot for the current time interval
     */
    bool PlotFunction::draw(double startTime, double stopTime, int intervalIndex,
df056382   Benjamin Renard   Remove some logs
558
                            bool /*isFirstInterval*/, bool /*isLastInterval*/)
347ee32b   Menouard AZIB   Change ParamNbPoi...
559
    {
dc7a34dc   Menouard AZIB   Utiliser PI de pl...
560
561
562
563
564

        // Sets panel plplot viewport, draw background & title for the panel
        _panel->draw(_pls);
        fillBackground(_pls);

dc7a34dc   Menouard AZIB   Utiliser PI de pl...
565
566
567
568
569
570
        // Compute nb series to draw by y axis
        std::map<std::string, int> nbSeriesByYAxisMap = getNbSeriesByYAxis();
        SeriesProperties lSeries;
        // Draw series for parameters.
        for (auto parameter : _parameterAxesList)
        {
dc7a34dc   Menouard AZIB   Utiliser PI de pl...
571
572
573
574
575
576
577
578
579
580
581
            for (auto lSeries : parameter.getYSeriePropertiesList())
            {
                for (auto lIndex : lSeries.getIndexList(_pParameterValues))
                {
                    bool moreThanOneSerieForYAxis = false;
                    if (lSeries.hasYAxis())
                        moreThanOneSerieForYAxis = (nbSeriesByYAxisMap[lSeries.getYAxisId()] > 1);

                    // Draw (configure) window for this series.
                    drawSeries(startTime, stopTime, intervalIndex, parameter._originalParamId,
                               lSeries, lIndex, parameter, moreThanOneSerieForYAxis);
dc7a34dc   Menouard AZIB   Utiliser PI de pl...
582
583
584
                }
            }
        }
99ff615c   Menouard AZIB   Change nbEchantil...
585

07bf1e7c   Menouard AZIB   EveryThing works ...
586
        if (!PlotFunction::isFourier() && !PlotFunction::isSpectro)
347ee32b   Menouard AZIB   Change ParamNbPoi...
587
        {
07bf1e7c   Menouard AZIB   EveryThing works ...
588
589
            TimeAxis *timeAxis = dynamic_cast<TimeAxis *>(_panel->getAxis(X_AXIS_ID1).get());
            PlotFunction::drawStartDate(timeAxis->_timeFormat, startTime, stopTime);
347ee32b   Menouard AZIB   Change ParamNbPoi...
590
        }
99ff615c   Menouard AZIB   Change nbEchantil...
591

07bf1e7c   Menouard AZIB   EveryThing works ...
592
        PlotFunction::drawSeriesForSpectro(startTime, stopTime, intervalIndex);
37f72164   Menouard AZIB   Indent the change...
593
        PlotFunction::writeToFileDebug();
7e3993f5   Menouard AZIB   Enable to downloa...
594

3cf11811   Menouard AZIB   FFT Validated and...
595
        return true;
99ff615c   Menouard AZIB   Change nbEchantil...
596
    }
7e3993f5   Menouard AZIB   Enable to downloa...
597
598
599
600
601
602
603
604

    void PlotFunction::writeToFileDebug()
    {
        std::ofstream file("plotFunction_results.txt");
        std::map<std::string, std::vector<double>> ::iterator it;

        for (it = xValuesMap.begin(); it != xValuesMap.end(); it++)
        {
275795d9   Menouard AZIB   Indent the change...
605
606
607
608
609
610
611
            file <<  it->first << std::endl;
            std::vector<double> tempY = yValuesMap[it->first];
            std::vector<double> tempX =  it->second;
            for (std::size_t index = 0; index < tempX.size(); index++)
            {
                double x = tempX[index];
                double y = tempY[index];
7e3993f5   Menouard AZIB   Enable to downloa...
612

275795d9   Menouard AZIB   Indent the change...
613
614
                file << std::fixed << x << " " << std::fixed << y << std::endl;
            }
7e3993f5   Menouard AZIB   Enable to downloa...
615
616
617
618
        }

        file.close();
    }
99ff615c   Menouard AZIB   Change nbEchantil...
619
}