SumIntoTableIndexes.hh 12 KB
/*
 * SumIntoTableIndexes.hh
 *
 *  Created on: Mar 26, 2019
 *      Author: AKKA IS
 */

#ifndef SUMINTOTABLEINDEXES_HH_
#define SUMINTOTABLEINDEXES_HH_

#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"
#include "tools.hh"


namespace AMDA {
    namespace Parameters {
        namespace SumIntoTableIndexes {

            /**
             * @class SumIntoTableIndexes1D
             * @brief It is responsible to compute the sum of 1D parameter data into a table of indexes.
             * @details This class implement the interface Operation.
             */
            template<typename DataType>
            class SumIntoTableIndexes1D : public Operation {
            public:

                /**
                 * @brief Constructor.
                 * @details Create the ParamData type of the input ParamData.
                 */
                SumIntoTableIndexes1D(Process& pProcess, ParamDataSpec<std::vector<DataType> >& paramInput, int minIndex, int maxIndex, std::string type)
                : Operation(pProcess),
                _paramInput(paramInput),
                _paramOutput(new ParamDataSpec<DataType>()), _minIndex(minIndex), _maxIndex(maxIndex), _type(type) {
                    _paramDataOutput = _paramOutput;
                }

                virtual ~SumIntoTableIndexes1D() {
                }

                /**
                 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
                 */
                void write(ParamDataIndexInfo &pParamDataIndexInfo) {
                    for (unsigned int _index = pParamDataIndexInfo._startIndex;
                            _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
                            ++_index) {
                        double crtTime = _paramInput.getTime(_index);
                        std::vector<DataType> inputElt = _paramInput.get(_index);
                        DataType output;
                        if (_type == "average" || _type == "sum")
                            output = tools<DataType>::sumAverage(inputElt, _minIndex, _maxIndex, _type == "average");
                        /**
 output << NotANumber();

 for (int i = _minIndex; i <= _maxIndex; ++i) {
         if ((i >= (int)inputElt.size()) || isNAN(inputElt[i]))
                 continue;
         if (isNAN(output))
               output = inputElt[i];
         else
               output += inputElt[i];
 }
                         * */

                        _paramOutput->pushTime(crtTime);
                        _paramOutput->getDataList().push_back(output);
                    }
                }

            private:
                /**<
                 * @brief It is the channel of data derived
                 */
                ParamDataSpec<std::vector<DataType> > &_paramInput;

                /**<
                 * @brief It is the channel of the data derived
                 */
                ParamDataSpec<DataType> *_paramOutput;

                int _minIndex;

                int _maxIndex;
                std::string _type;
            };

            /**
             * @class SumIntoTableIndexes2DOneRange
             * @brief It is responsible to compute the sum of 2D parameter data into one table indexes.
             * @details This class implement the interface Operation.
             */
            template<typename DataType>
            class SumIntoTableIndexes2DOneRange : public Operation {
            public:

                /**
                 * @brief Constructor.
                 * @details Create the ParamData type of the input ParamData when only one range is defined for one dimension (the output data is a vector).
                 */
                SumIntoTableIndexes2DOneRange(Process& pProcess, ParamDataSpec<Tab2DData<DataType> >& paramInput, int minIndex, int maxIndex, int tableRelatedDim, std::string type)
                : Operation(pProcess),
                _paramInput(paramInput),
                _paramOutput(new ParamDataSpec<std::vector<DataType>>()),
                _tableRelatedDim(tableRelatedDim), _minIndex(minIndex), _maxIndex(maxIndex), _type(type) {
                    _paramDataOutput = _paramOutput;
                }

                virtual ~SumIntoTableIndexes2DOneRange() {
                }

                /**
                 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
                 */
                void write(ParamDataIndexInfo &pParamDataIndexInfo) {
                    for (unsigned int _index = pParamDataIndexInfo._startIndex;
                            _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
                            ++_index) {
                        double crtTime = _paramInput.getTime(_index);
                        Tab2DData<DataType> inputElt = _paramInput.get(_index);
                        std::vector<DataType> outputElt;
                        if (_type == "average" || _type == "sum")
                            outputElt = tools<DataType>::sumAverage2D(inputElt, _minIndex, _maxIndex, _tableRelatedDim, _type == "average");
                        /**	
                                        if (_tableRelatedDim == 0) {
                                        outputElt.resize(inputElt.getDim2Size(), 0);
                                        outputElt << NotANumber();
                                        for (int i = _minIndex; i <= _maxIndex; ++i) {
                                                for (int j = 0; j < inputElt.getDim2Size(); ++j) {
                                                        if (isNAN(inputElt[i][j]))
                                                                continue;
                                                        if (isNAN(outputElt[j]))
                                                                outputElt[j] = inputElt[i][j];
                                                        else
                                                                outputElt[j] += inputElt[i][j];
                                                }
                                        }
                                }
                                else {
                                        outputElt.resize(inputElt.getDim1Size(), 0);
                                        outputElt << NotANumber();
                                        for (int j = _minIndex; j <= _maxIndex; ++j) {
                                                for (int i = 0; i < inputElt.getDim1Size(); ++i) {
                                                        if (isNAN(inputElt[i][j]))
                                                                continue;
                                                        if (isNAN(outputElt[i]))
                                                                outputElt[i] = inputElt[i][j];
                                                        else
                                                                outputElt[i] += inputElt[i][j];
                                                }
                                        }
                                }
                         */
                        _paramOutput->pushTime(crtTime);
                        _paramOutput->getDataList().push_back(outputElt);
                    }
                }

            private:
                /**<
                 * @brief It is the channel of data derived
                 */
                ParamDataSpec<Tab2DData<DataType> > &_paramInput;

                ParamDataSpec<std::vector<DataType>> *_paramOutput;

                int _tableRelatedDim;

                int _minIndex;

                int _maxIndex;

                std::string _type;
            };

            /**
             * @class SumIntoIndexesTable2DTwoRanges
             * @brief It is responsible to compute the sum of 2D parameter data into two tables ranges of indexes.
             * @details This class implement the interface Operation.
             */
            template<typename DataType>
            class SumIntoTableIndexes2DTwoRanges : public Operation {
            public:

                /**
                 * @brief Constructor.
                 * @details Create the ParamData type of the input ParamData when a range is defined for each dimensions (the output data is a scalar).
                 */
                SumIntoTableIndexes2DTwoRanges(Process& pProcess, ParamDataSpec<Tab2DData<DataType> >& paramInput, int minIndex1, int maxIndex1, int tableRelatedDim, std::string type1, int minIndex2, int maxIndex2, std::string type2)
                : Operation(pProcess),
                _paramInput(paramInput),
                _paramOutput(new ParamDataSpec<DataType>()),
                _minIndex1(minIndex1), _maxIndex1(maxIndex1), _tableRelatedDim(tableRelatedDim), _type1(type1), _minIndex2(minIndex2), _maxIndex2(maxIndex2), _type2(type2) {
                    _paramDataOutput = _paramOutput;
                }

                virtual ~SumIntoTableIndexes2DTwoRanges() {
                }

                /**
                 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
                 */
                void write(ParamDataIndexInfo &pParamDataIndexInfo) {
                    for (unsigned int _index = pParamDataIndexInfo._startIndex;
                            _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
                            ++_index) {
                        double crtTime = _paramInput.getTime(_index);
                        Tab2DData<DataType> inputElt = _paramInput.get(_index);
                        DataType outputElt;
                        if (_type1  == _type2 && (_type1 == "average" || _type2 == "sum"))
                            outputElt = tools<DataType>::sumAverage2DFull(inputElt, _minIndex1, _maxIndex1,
                                _minIndex2, _maxIndex2, _tableRelatedDim, _type1 == "average");
                        /**
                        outputElt << NotANumber();

                        for (int i = _minIndex1; i <= _maxIndex1; ++i) {
                                for (int j = _minIndex2; j <= _maxIndex2; ++j) {
                                        if (tableRelatedDim == 0) {
                                                if (isNAN(inputElt[i][j]))
                                                        continue;
                                                if (isNAN(outputElt))
                                                        outputElt = inputElt[i][j];
                                                else
                                                        outputElt += inputElt[i][j];
                                        }
                                        else {
                                                if (isNAN(inputElt[j][i]))
                                                        continue;
                                                if (isNAN(outputElt))
                                                        outputElt = inputElt[j][i];
                                                else
                                                        outputElt += inputElt[j][i];
                                        }
                                }
                        }
                         */
                        _paramOutput->pushTime(crtTime);
                        _paramOutput->getDataList().push_back(outputElt);
                    }
                }

            private:
                /**<
                 * @brief It is the channel of data derived
                 */
                ParamDataSpec<Tab2DData<DataType> > &_paramInput;

                ParamDataSpec<DataType> *_paramOutput;


                int _minIndex1;

                int _maxIndex1;

                int _tableRelatedDim;

                std::string _type1;

                int _minIndex2;

                int _maxIndex2;

                std::string _type2;
            };


        } /* namespace SumIntoTableIndexes */
    } /* namespace Parameters */
} /* namespace AMDA */
#endif /* SUMINTOTABLEINDEXES_HH_ */