source: XIOS/trunk/src/transformation/Functions/average_reduction.cpp @ 1018

Last change on this file since 1018 was 1018, checked in by mhnguyen, 7 years ago

Improving missing-value processing
If detect_missing_value is activated, then all missing value will be converted to
NaN (Not-a-number) in input of data flow then they will be reconverted to missing value on output

+) Update SourceFilter?, TemporalFilter? and SpatialTransformFilter? with new processing
+) Update all transformations with new processing

Test
+) On Curie
+) Work

File size: 2.4 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                                       const double& defaultValue)
34{
35  if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; }
36  bool hasMissingValue = NumTraits<double>::isnan(defaultValue);
37
38  if (hasMissingValue)
39  {
40    int nbLocalIndex = localIndex.size();
41    int currentlocalIndex = 0;
42    double currentWeight  = 0.0;
43    for (int idx = 0; idx < nbLocalIndex; ++idx)
44    {
45      currentlocalIndex = localIndex[idx].first;
46      currentWeight     = localIndex[idx].second;
47      if (!NumTraits<double>::isnan(*(dataInput + idx)))
48      {
49        if (flagInitial[currentlocalIndex])
50        {
51          dataOut(currentlocalIndex) = *(dataInput + idx);
52          flagInitial[currentlocalIndex] = false;
53        }
54        else
55        {
56          dataOut(currentlocalIndex)  += *(dataInput + idx);
57          weights_(currentlocalIndex) += 1.0;
58        }
59      }
60    }
61  }
62  else
63  {
64    int nbLocalIndex = localIndex.size();
65    int currentlocalIndex = 0;
66    double currentWeight  = 0.0;
67    for (int idx = 0; idx < nbLocalIndex; ++idx)
68    {
69      currentlocalIndex = localIndex[idx].first;
70      currentWeight     = localIndex[idx].second;
71
72      if (flagInitial[currentlocalIndex])
73      {
74        dataOut(currentlocalIndex) = *(dataInput + idx);
75        flagInitial[currentlocalIndex] = false;
76      }
77      else
78      {
79        dataOut(currentlocalIndex)  += *(dataInput + idx);
80        weights_(currentlocalIndex) += 1.0;
81      }
82    }
83  }
84}
85
86void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut)
87{
88  dataOut /= weights_;
89  resetWeight_ = true;
90}
91
92}
Note: See TracBrowser for help on using the repository browser.