1 | #include "average.hpp" |
---|
2 | #include "array_new.hpp" |
---|
3 | #include "utils.hpp" |
---|
4 | |
---|
5 | namespace xios |
---|
6 | { |
---|
7 | namespace func |
---|
8 | { |
---|
9 | /// ////////////////////// Définitions ////////////////////// /// |
---|
10 | |
---|
11 | CAverage::CAverage(CArray<double,1>& doutput) |
---|
12 | : SuperClass(StdString("average"), doutput) |
---|
13 | { /* Ne rien faire de plus */ } |
---|
14 | |
---|
15 | CAverage::CAverage(CArray<double,1>& doutput, double missingValue) |
---|
16 | : SuperClass(StdString("average"), doutput, missingValue) |
---|
17 | { /* Ne rien faire de plus */ } |
---|
18 | |
---|
19 | CAverage::~CAverage(void) |
---|
20 | { /* Ne rien faire de plus */ } |
---|
21 | |
---|
22 | //--------------------------------------------------------------- |
---|
23 | |
---|
24 | void CAverage::apply(const CArray<double,1>& _dinput, |
---|
25 | CArray<double,1>& _doutput) |
---|
26 | { |
---|
27 | if (hasMissingValue) |
---|
28 | { |
---|
29 | if (nbcalls.numElements()==0) |
---|
30 | { |
---|
31 | nbcalls.resize(_dinput.numElements()) ; |
---|
32 | nbcalls=0 ; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | if (this->nbcall == 1) |
---|
37 | { |
---|
38 | _doutput=_dinput ; |
---|
39 | if (hasMissingValue) |
---|
40 | { |
---|
41 | int i, n =_dinput.numElements() ; |
---|
42 | const double * in=_dinput.dataFirst() ; |
---|
43 | int* nc=nbcalls.dataFirst() ; |
---|
44 | for (i=0; i<n; ++i,++nc,++in) |
---|
45 | if (!NumTraits<double>::isNan(*in)) (*nc) ++; |
---|
46 | } |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | if (hasMissingValue) |
---|
51 | { |
---|
52 | int i, n =_dinput.numElements() ; |
---|
53 | const double * in=_dinput.dataFirst() ; |
---|
54 | double* out=_doutput.dataFirst(); |
---|
55 | int* nc=nbcalls.dataFirst() ; |
---|
56 | for (i=0; i<n; ++i,++in,++out,++nc) |
---|
57 | if (!NumTraits<double>::isNan(*in)) |
---|
58 | { |
---|
59 | if (*nc != 0) (*out) += *in; |
---|
60 | else *out = *in ; |
---|
61 | (*nc) ++; |
---|
62 | } |
---|
63 | } |
---|
64 | else _doutput+=_dinput ; |
---|
65 | } |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | void CAverage::final(void) |
---|
70 | { |
---|
71 | if (hasMissingValue) |
---|
72 | { |
---|
73 | int i, n = nbcalls.numElements() ; |
---|
74 | double* out= doutput.dataFirst(); |
---|
75 | int* nc=nbcalls.dataFirst() ; |
---|
76 | for (i=0; i<n; ++i, ++out,++nc) |
---|
77 | if (*nc!=0) |
---|
78 | { |
---|
79 | *out /= *nc; |
---|
80 | *nc = 0 ; |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|
84 | else doutput/=this->nbcall; |
---|
85 | this->nbcall = 0; |
---|
86 | |
---|
87 | } |
---|
88 | } // namespace func |
---|
89 | } // namespace xios |
---|