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

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

Dev: adding exception handling.

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

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