source: XIOS/dev/XIOS_DEV_CMIP6/src/transformation/scalar_algorithm_reduce_axis.cpp @ 1260

Last change on this file since 1260 was 1260, checked in by ymipsl, 3 years ago

Buf fix in reduction. Missing value update was not set correctly

YM

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)
29{
30  std::vector<CScalar*> scalarListDestP = gridDst->getScalars();
31  std::vector<CAxis*> axisListSrcP  = gridSrc->getAxis();
32
33  CReduceAxisToScalar* reduceAxis = dynamic_cast<CReduceAxisToScalar*> (transformation);
34  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
35  int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
36
37  return (new CScalarAlgorithmReduceAxis(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis));
38}
39
40bool CScalarAlgorithmReduceAxis::registerTrans()
41{
42  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create);
43}
44
45CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)
46 : CScalarAlgorithmTransformation(scalarDestination, axisSource),
47   reduction_(0)
48{
49  if (algo->operation.isEmpty())
50    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
51           << "Operation must be defined."
52           << "Axis source " <<axisSource->getId() << std::endl
53           << "Scalar destination " << scalarDestination->getId());
54  StdString op;
55  switch (algo->operation)
56  {
57    case CReduceAxisToScalar::operation_attr::sum:
58      op = "sum";
59      break;
60    case CReduceAxisToScalar::operation_attr::min:
61      op = "min";
62      break;
63    case CReduceAxisToScalar::operation_attr::max:
64      op = "max";
65      break;
66    case CReduceAxisToScalar::operation_attr::average:
67      op = "average";
68      break;
69    default:
70        ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
71         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
72         << "Domain source " <<axisSource->getId() << std::endl
73         << "Scalar destination " << scalarDestination->getId());
74
75  }
76 
77  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
78    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
79       << "Operation '" << op << "' not found. Please make sure to use a supported one"
80       << "Axis source " <<axisSource->getId() << std::endl
81       << "Scalar destination " << scalarDestination->getId());
82
83  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
84}
85
86void CScalarAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut,
87                                         std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass)
88{
89  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
90}
91
92void CScalarAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut)
93{
94  reduction_->updateData(dataOut);
95}
96
97CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis()
98{
99  if (0 != reduction_) delete reduction_;
100}
101
102void CScalarAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
103{
104  this->transformationMapping_.resize(1);
105  this->transformationWeight_.resize(1);
106
107  TransformationIndexMap& transMap = this->transformationMapping_[0];
108  TransformationWeightMap& transWeight = this->transformationWeight_[0];
109
110  CArray<int,1>& axisSrcIndex = axisSrc_->index;
111  int globalIndexSize = axisSrcIndex.numElements();
112
113  for (int idx = 0; idx < globalIndexSize; ++idx)
114  {
115    transMap[0].push_back(axisSrcIndex(idx));
116    transWeight[0].push_back(1.0);
117  }
118}
119
120}
Note: See TracBrowser for help on using the repository browser.