source: XIOS/dev/dev_olga/src/transformation/scalar_algorithm_reduce_axis.cpp @ 1620

Last change on this file since 1620 was 1612, checked in by oabramkina, 21 months ago

Dev: adding exception handling.

To activate it, compilation flag -DXIOS_EXCEPTION should be added.

File size: 5.1 KB
Line 
1/*!
2   \file scalar_algorithm_reduce_scalar.cpp
3   \author Ha NGUYEN
4   \since 23 June 2016
5   \date 23 June 2016
6
7   \brief Algorithm for reduce an axis to a scalar
8 */
9#include "scalar_algorithm_reduce_axis.hpp"
10#include "axis.hpp"
11#include "scalar.hpp"
12#include "reduce_axis_to_scalar.hpp"
13#include "grid.hpp"
14#include "grid_transformation_factory_impl.hpp"
15#include "reduction.hpp"
16
17#include "reduction.hpp"
18
19namespace xios {
20CGenericAlgorithmTransformation* CScalarAlgorithmReduceAxis::create(CGrid* gridDst, CGrid* gridSrc,
21                                                                     CTransformation<CScalar>* transformation,
22                                                                     int elementPositionInGrid,
23                                                                     std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
24                                                                     std::map<int, int>& elementPositionInGridSrc2AxisPosition,
25                                                                     std::map<int, int>& elementPositionInGridSrc2DomainPosition,
26                                                                     std::map<int, int>& elementPositionInGridDst2ScalarPosition,
27                                                                     std::map<int, int>& elementPositionInGridDst2AxisPosition,
28                                                                     std::map<int, int>& elementPositionInGridDst2DomainPosition)
29TRY
30{
31  std::vector<CScalar*> scalarListDestP = gridDst->getScalars();
32  std::vector<CAxis*> axisListSrcP  = gridSrc->getAxis();
33
34  CReduceAxisToScalar* reduceAxis = dynamic_cast<CReduceAxisToScalar*> (transformation);
35  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
36  int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
37
38  return (new CScalarAlgorithmReduceAxis(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis));
39}
40CATCH
41
42bool CScalarAlgorithmReduceAxis::registerTrans()
43TRY
44{
45  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create);
46}
47CATCH
48
49CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)
50 : CScalarAlgorithmTransformation(scalarDestination, axisSource),
51   reduction_(0)
52TRY
53{
54  if (algo->operation.isEmpty())
55    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
56           << "Operation must be defined."
57           << "Axis source " <<axisSource->getId() << std::endl
58           << "Scalar destination " << scalarDestination->getId());
59  StdString op;
60  switch (algo->operation)
61  {
62    case CReduceAxisToScalar::operation_attr::sum:
63      op = "sum";
64      break;
65    case CReduceAxisToScalar::operation_attr::min:
66      op = "min";
67      break;
68    case CReduceAxisToScalar::operation_attr::max:
69      op = "max";
70      break;
71    case CReduceAxisToScalar::operation_attr::average:
72      op = "average";
73      break;
74    default:
75        ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
76         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
77         << "Domain source " <<axisSource->getId() << std::endl
78         << "Scalar destination " << scalarDestination->getId());
79
80  }
81 
82  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
83    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
84       << "Operation '" << op << "' not found. Please make sure to use a supported one"
85       << "Axis source " <<axisSource->getId() << std::endl
86       << "Scalar destination " << scalarDestination->getId());
87
88  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
89}
90CATCH
91
92void CScalarAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut,
93                                         std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass)
94TRY
95{
96  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
97}
98CATCH
99
100void CScalarAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut)
101TRY
102{
103  reduction_->updateData(dataOut);
104}
105CATCH
106
107CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis()
108TRY
109{
110  if (0 != reduction_) delete reduction_;
111}
112CATCH
113
114void CScalarAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
115TRY
116{
117  this->transformationMapping_.resize(1);
118  this->transformationWeight_.resize(1);
119
120  TransformationIndexMap& transMap = this->transformationMapping_[0];
121  TransformationWeightMap& transWeight = this->transformationWeight_[0];
122
123  int globalIndexSize = axisSrc_-> n_glo;
124
125  for (int idx = 0; idx < globalIndexSize; ++idx)
126  {
127    transMap[0].push_back(idx);
128    transWeight[0].push_back(1.0);
129  }
130}
131CATCH
132
133}
Note: See TracBrowser for help on using the repository browser.