source: XIOS/dev/dev_olga/src/transformation/Functions/average_reduction.cpp @ 1158

Last change on this file since 1158 was 1158, checked in by oabramkina, 7 years ago

Two server levels: merging with trunk r1137.
There are bugs.

File size: 2.4 KB
RevLine 
[979]1/*!
2   \file average.cpp
3   \author Ha NGUYEN
4   \since 8 Sep 2016
[1158]5   \date 9 Jan 2017
[979]6
7   \brief average reduction
8 */
9#include "average_reduction.hpp"
[1158]10#include "utils.hpp"
[979]11
12namespace xios {
13
14CAverageReductionAlgorithm::CAverageReductionAlgorithm()
15  : CReductionAlgorithm(), resetWeight_(true)
16{
17}
18
19CReductionAlgorithm* CAverageReductionAlgorithm::create()
20{
21  return (new CAverageReductionAlgorithm());
22}
23
24bool CAverageReductionAlgorithm::registerTrans()
25{
26  return registerOperation(TRANS_REDUCE_AVERAGE, CAverageReductionAlgorithm::create);
27}
28
29void CAverageReductionAlgorithm::apply(const std::vector<std::pair<int,double> >& localIndex,
30                                       const double* dataInput,
31                                       CArray<double,1>& dataOut,
[1158]32                                       std::vector<bool>& flagInitial,                     
33                                       bool ignoreMissingValue)
[979]34{
[1158]35  if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } 
[979]36
[1158]37  if (ignoreMissingValue)
[979]38  {
[1158]39    int nbLocalIndex = localIndex.size();
40    int currentlocalIndex = 0;
41    double currentWeight  = 0.0;
[979]42
[1158]43    dataOut=std::numeric_limits<double>::quiet_NaN();
44
45    for (int idx = 0; idx < nbLocalIndex; ++idx)
[979]46    {
[1158]47      currentlocalIndex = localIndex[idx].first;
48      currentWeight     = localIndex[idx].second;
49      if (!NumTraits<double>::isnan(*(dataInput + idx)))
50      {
51        if (flagInitial[currentlocalIndex])
52        {
53          dataOut(currentlocalIndex) = *(dataInput + idx);
54          flagInitial[currentlocalIndex] = false;
55        }
56        else
57        {
58          dataOut(currentlocalIndex)  += *(dataInput + idx);
59          weights_(currentlocalIndex) += 1.0;
60        }
61      }
[979]62    }
[1158]63  }
64  else
65  {
66    int nbLocalIndex = localIndex.size();
67    int currentlocalIndex = 0;
68    double currentWeight  = 0.0;
69    for (int idx = 0; idx < nbLocalIndex; ++idx)
[979]70    {
[1158]71      currentlocalIndex = localIndex[idx].first;
72      currentWeight     = localIndex[idx].second;
73
74      if (flagInitial[currentlocalIndex])
75      {
76        dataOut(currentlocalIndex) = *(dataInput + idx);
77        flagInitial[currentlocalIndex] = false;
78      }
79      else
80      {
81        dataOut(currentlocalIndex)  += *(dataInput + idx);
82        weights_(currentlocalIndex) += 1.0;
83      }
[979]84    }
85  }
86}
87
88void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut)
89{
90  dataOut /= weights_;
91  resetWeight_ = true;
92}
93
94}
Note: See TracBrowser for help on using the repository browser.