source: XIOS/dev/branch_yushan_merged/src/transformation/Functions/average_reduction.cpp @ 1157

Last change on this file since 1157 was 1157, checked in by yushan, 7 years ago

branch re-merged with trunk @1156

File size: 2.5 KB
Line 
1/*!
2   \file average.cpp
3   \author Ha NGUYEN
4   \since 8 Sep 2016
5   \date 9 Jan 2017
6
7   \brief average reduction
8 */
9#include "average_reduction.hpp"
10#include "utils.hpp"
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,
32                                       std::vector<bool>& flagInitial,                     
33                                       bool ignoreMissingValue)
34{
35  if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } 
36
37  if (ignoreMissingValue)
38  {
39    int nbLocalIndex = localIndex.size();
40    int currentlocalIndex = 0;
41    double currentWeight  = 0.0;   
42
43
44    for (int idx = 0; idx < nbLocalIndex; ++idx)
45    {
46      currentlocalIndex = localIndex[idx].first;
47      currentWeight     = localIndex[idx].second;
48      if (!NumTraits<double>::isnan(*(dataInput + idx)))
49      {
50        if (flagInitial[currentlocalIndex])
51        {
52          dataOut(currentlocalIndex) = *(dataInput + idx);
53          flagInitial[currentlocalIndex] = false;
54        }
55        else
56        {
57          dataOut(currentlocalIndex)  += *(dataInput + idx);
58          weights_(currentlocalIndex) += 1.0;
59        }
60      }
61      else
62      {
63        if (flagInitial[currentlocalIndex]) 
64          dataOut(currentlocalIndex) = std::numeric_limits<double>::quiet_NaN();
65      }
66    }
67  }
68  else
69  {
70    int nbLocalIndex = localIndex.size();
71    int currentlocalIndex = 0;
72    double currentWeight  = 0.0;
73    for (int idx = 0; idx < nbLocalIndex; ++idx)
74    {
75      currentlocalIndex = localIndex[idx].first;
76      currentWeight     = localIndex[idx].second;
77
78      if (flagInitial[currentlocalIndex])
79      {
80        dataOut(currentlocalIndex) = *(dataInput + idx);
81        flagInitial[currentlocalIndex] = false;
82      }
83      else
84      {
85        dataOut(currentlocalIndex)  += *(dataInput + idx);
86        weights_(currentlocalIndex) += 1.0;
87      }
88    }
89  }
90}
91
92void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut)
93{
94  dataOut /= weights_;
95  resetWeight_ = true;
96}
97
98}
Note: See TracBrowser for help on using the repository browser.