SumIntoTableIndexes.hh 12.9 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 = TableTools::tools<DataType>::sumAverage(inputElt, _minIndex, _maxIndex, _type == "average");
                        }else {
                            BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("SumIntoTableIndexes  not supported process " + _type));
                        }
                        
                        _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 = TableTools::tools<DataType>::sumAverage2D(inputElt, _minIndex, _maxIndex, _tableRelatedDim, _type == "average");
                        }else {
                            BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("SumIntoTableIndexes  not supported process " + _type));
                        }
                        _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 MixedTableIndexes2DTwoRanges : 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).
                 */
                MixedTableIndexes2DTwoRanges(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<std::vector<DataType>>()),
                _minIndex1(minIndex1), _maxIndex1(maxIndex1), _tableRelatedDim(tableRelatedDim), _type1(type1), _minIndex2(minIndex2), _maxIndex2(maxIndex2), _type2(type2) {
                    _paramDataOutput = _paramOutput;
                }

                virtual ~MixedTableIndexes2DTwoRanges() {
                }

                /**
                 * @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);
                        Tab2DData<DataType> firstOutput; 
                        std::vector<DataType> outputElt;
                        if (_type1  == "all" && (_type2 == "average" || _type2 == "sum")){
                            firstOutput = TableTools::tools<DataType>::allValues(inputElt, _minIndex1, _maxIndex1,0);
                            outputElt    = TableTools::tools<DataType>::sumAverage2D(firstOutput,_minIndex2,_maxIndex2, 1, _type2 == "average");
                        }else if(_type2  == "all" && (_type1 == "average" || _type1 == "sum")){
                            firstOutput = TableTools::tools<DataType>::allValues(inputElt, _minIndex2, _maxIndex2,1);
                            outputElt    = TableTools::tools<DataType>::sumAverage2D(firstOutput,_minIndex1,_maxIndex1, 0, _type1 == "average");
                        }else {
                            BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("SumIntoTableIndexes  not supported process " + _type1+" "+_type2));
                        }
                        _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 _minIndex1;

                int _maxIndex1;

                int _tableRelatedDim;

                std::string _type1;

                int _minIndex2;

                int _maxIndex2;

                std::string _type2;
            };
            
                        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 = TableTools::tools<DataType>::sumAverage2DFull(inputElt, _minIndex1, _maxIndex1,
                                _minIndex2, _maxIndex2, _tableRelatedDim, _type1 == "average");
                        }else if((_type1 == "average" && _type2 == "sum")||(_type1 == "sum"  && _type2 =="average" )){
                             std::vector<DataType> firstOutput = TableTools::tools<DataType>::sumAverage2D(inputElt, _minIndex1, _maxIndex1, 0, _type1 == "average");
                             outputElt = TableTools::tools<DataType>::sumAverage(firstOutput, _minIndex2, _maxIndex2, _type1 == "average");
                        }else {
                            BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("SumIntoTableIndexes  not supported process " + _type1+" "+_type2));
                        }
                        _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_ */