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

Last change on this file since 1620 was 1612, checked in by oabramkina, 5 years ago

Dev: adding exception handling.

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

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