source: trunk/src/RowAccumulator.cpp@ 2125

Last change on this file since 2125 was 2125, checked in by WataruKawasaki, 14 years ago

New Development: No

JIRA Issue: Yes CAS-2776

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): SD

Description: bugfix of STMath::smoothOther(), STMath::average(), and RowAccumulator (used in sdsmooth)


File size: 7.8 KB
Line 
1//
2// C++ Implementation: RowAccumulator
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <Malte.Marquarding@csiro.au>, (C) 2005
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13#include <iostream>
14
15#include <casa/iomanip.h>
16#include <casa/Arrays/MaskArrMath.h>
17#include <casa/Arrays/MaskArrLogi.h>
18#include <casa/Arrays/ArrayMath.h>
19#include <casa/Arrays/ArrayLogical.h>
20#include "RowAccumulator.h"
21
22using namespace casa;
23using namespace asap;
24
25RowAccumulator::RowAccumulator(WeightType wt) : weightType_(wt), initialized_(False)
26{
27 reset();
28}
29
30RowAccumulator::~RowAccumulator()
31{
32}
33
34
35void RowAccumulator::reset(const uInt size, const uInt tsysSize)
36{
37 Vector<Bool> m(size, True);
38
39 spectrum_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
40 spectrumNoMask_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
41
42 n_.setData(Vector<uInt>(size, 0), Vector<Bool>(size, True));
43 nNoMask_.setData(Vector<uInt>(size, 0), Vector<Bool>(size, True));
44
45 weightSum_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
46 weightSumNoMask_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
47
48 tsysSum_.resize(tsysSize); tsysSum_=0.0;
49 tsysSumNoMask_.resize(tsysSize); tsysSumNoMask_=0.0;
50
51 intervalSum_ = 0.0;
52 intervalSumNoMask_ = 0.0;
53
54 timeSum_ = 0.0;
55 timeSumNoMask_ = 0.0;
56}
57
58void RowAccumulator::initialize(const uInt size, const uInt tsysSize)
59{
60 reset(size, tsysSize);
61 initialized_ = True;
62}
63
64void RowAccumulator::add(const Vector<Float>& v,
65 const Vector<Bool>& m,
66 const Vector<Float>& tsys,
67 const Double interval,
68 const Double time)
69{
70 uInt size = v.nelements();
71 //if (size != m.nelements()) raiseError;
72 if (!initialized_) initialize(size, tsys.nelements());
73
74 addSpectrum(v, m, tsys, interval, time);
75}
76
77void RowAccumulator::addSpectrum(const Vector<Float>& v,
78 const Vector<Bool>& m,
79 const Vector<Float>& tsys,
80 const Double interval,
81 const Double time)
82{
83 doAddSpectrum(v, m, tsys, interval, time, False);
84 doAddSpectrum(v, m, tsys, interval, time, True); // CAS-2776
85}
86
87void RowAccumulator::doAddSpectrum(const Vector<Float>& v,
88 const Vector<Bool>& m,
89 const Vector<Float>& tsys,
90 const Double interval,
91 const Double time,
92 const Bool ignoreMask)
93{
94 Vector<Float> vUse = v.copy();
95 Vector<Bool> mUse = m.copy();
96 if (ignoreMask) mUse = !mUse;
97
98 MaskedArray<Float> vadd(vUse, mUse);
99 Float totalWeight = getTotalWeight(vadd, tsys, interval, time, ignoreMask);
100 vadd *= totalWeight;
101 MaskedArray<Float> wadd(Vector<Float>(mUse.nelements(), totalWeight), mUse);
102 MaskedArray<uInt> inc(Vector<uInt>(mUse.nelements(), 1), mUse);
103
104 if (ignoreMask) {
105 spectrumNoMask_ += vadd;
106 weightSumNoMask_ += wadd;
107 nNoMask_ += inc;
108 } else {
109 spectrum_ += vadd;
110 weightSum_ += wadd;
111 n_ += inc;
112 }
113
114 //
115 cout << "***" << endl;
116 Vector<Float> spe = spectrum_.getArray();
117 Vector<Float> spe0 = spectrumNoMask_.getArray();
118 Vector<Float> wei = weightSum_.getArray();
119 Vector<Float> wei0 = weightSumNoMask_.getArray();
120 Vector<uInt> n = n_.getArray();
121 Vector<uInt> n0 = nNoMask_.getArray();
122 cout << "S__" << "[" << spe[0] << "][" << spe[1] << "][" << spe[2] << "][" << spe[3] << "][" << spe[4] << "][" << spe[5] << "][" << spe[6] << "][" << spe[7] << "][" << spe[8] << "][" << spe[9] << "][" << spe[10] << "][" << spe[11] << "][" << spe[12] << "]" << endl;
123 cout << "S0_" << "[" << spe0[0] << "][" << spe0[1] << "][" << spe0[2] << "][" << spe0[3] << "][" << spe0[4] << "][" << spe0[5] << "][" << spe0[6] << "][" << spe0[7] << "][" << spe0[8] << "][" << spe0[9] << "][" << spe0[10] << "][" << spe0[11] << "][" << spe0[12] << "]" << endl;
124 cout << "W__" << "[" << wei[0] << "][" << wei[1] << "][" << wei[2] << "][" << wei[3] << "][" << wei[4] << "][" << wei[5] << "][" << wei[6] << "][" << wei[7] << "][" << wei[8] << "][" << wei[9] << "][" << wei[10] << "][" << wei[11] << "][" << wei[12] << "]" << endl;
125 cout << "W0_" << "[" << wei0[0] << "][" << wei0[1] << "][" << wei0[2] << "][" << wei0[3] << "][" << wei0[4] << "][" << wei0[5] << "][" << wei0[6] << "][" << wei0[7] << "][" << wei0[8] << "][" << wei0[9] << "][" << wei0[10] << "][" << wei0[11] << "][" << wei0[12] << "]" << endl;
126 cout << "N__" << "[" << n[0] << "][" << n[1] << "][" << n[2] << "][" << n[3] << "][" << n[4] << "][" << n[5] << "][" << n[6] << "][" << n[7] << "][" << n[8] << "][" << n[9] << "][" << n[10] << "][" << n[11] << "][" << n[12] << "]" << endl;
127 cout << "N0_" << "[" << n0[0] << "][" << n0[1] << "][" << n0[2] << "][" << n0[3] << "][" << n0[4] << "][" << n0[5] << "][" << n0[6] << "][" << n0[7] << "][" << n0[8] << "][" << n0[9] << "][" << n0[10] << "][" << n0[11] << "][" << n0[12] << "]" << endl;
128 cout << "***" << endl;
129 //
130}
131
132Float RowAccumulator::getTotalWeight(const MaskedArray<Float>& data,
133 const Vector<Float>& tsys,
134 const Double interval,
135 const Double time,
136 const Bool ignoreMask)
137{
138 Float totalWeight = 1.0;
139
140 Vector<Bool> m = data.getMask();
141 if (!allEQ(m, False)) { // only add these if not everything masked
142 totalWeight *= addTsys(tsys, ignoreMask);
143 totalWeight *= addInterval(interval, ignoreMask);
144 addTime(time, ignoreMask);
145 }
146
147 if (weightType_ == W_VAR) {
148 Float fac = 1.0/variance(data);
149 if (!ignoreMask && (m.nelements() == userMask_.nelements()))
150 fac = 1.0/variance(data(userMask_));
151
152 totalWeight *= fac;
153 }
154
155 return totalWeight;
156}
157
158Float RowAccumulator::addTsys(const Vector<Float>& v, Bool ignoreMask)
159{
160 // @fixme this assume tsys is the same for all channels
161
162 Float w = 1.0;
163 if (ignoreMask) {
164 tsysSumNoMask_ += v[0];
165 } else {
166 tsysSum_ += v[0];
167 }
168 if ( weightType_ == W_TSYS || weightType_ == W_TINTSYS ) {
169 w /= (v[0]*v[0]);
170 }
171 return w;
172}
173
174void RowAccumulator::addTime(Double t, Bool ignoreMask)
175{
176 if (ignoreMask) {
177 timeSumNoMask_ += t;
178 } else {
179 timeSum_ += t;
180 }
181}
182
183Float RowAccumulator::addInterval(Double inter, Bool ignoreMask)
184{
185 Float w = 1.0;
186 if (ignoreMask) {
187 intervalSumNoMask_ += inter;
188 } else {
189 intervalSum_ += inter;
190 }
191 if (weightType_ == W_TINT || weightType_ == W_TINTSYS) {
192 w /= Float(inter);
193 }
194 return w;
195}
196
197Vector<Float> RowAccumulator::getSpectrum() const
198{
199 return (spectrum_/weightSum_).getArray();
200}
201
202Double RowAccumulator::getTime() const
203{
204 return timeSum_/Float(max(n_));
205}
206
207Double RowAccumulator::getInterval() const
208{
209 return intervalSum_;
210}
211
212Vector<Bool> RowAccumulator::getMask() const
213{
214 // Return the "total" mask - False where no points have been accumulated.
215 return (n_.getArray() > uInt(0));
216}
217
218Vector<Float> RowAccumulator::getTsys() const
219{
220 // @fixme this assumes tsys.nelements() == 1
221 return tsysSum_/Float(max(n_));
222}
223
224void RowAccumulator::setUserMask(const Vector<Bool>& m)
225{
226 userMask_.resize();
227 userMask_ = m;
228}
229
230// Added by TT check the state of RowAccumulator
231Bool RowAccumulator::state() const
232{
233 return initialized_;
234}
235
236void RowAccumulator::replaceNaN()
237{
238 Vector<Float> v = spectrum_.getArray();
239 Vector<Float> w = weightSum_.getArray();
240 Vector<Float> vRef = spectrumNoMask_.getArray();
241 Vector<Float> wRef = weightSumNoMask_.getArray();
242
243 //-------
244 cout << "SB-";
245 for (uInt i=0; i<13; ++i) {
246 cout << "[" << v[i] << "]";
247 }
248 cout << endl;
249 cout << "WB-";
250 for (uInt i=0; i<13; ++i) {
251 cout << "[" << w[i] << "]";
252 }
253 cout << endl;
254 //-------
255
256 for (uInt i = 0; i < v.size(); ++i) {
257 if (w[i] == 0.0) {
258 v[i] = vRef[i];
259 w[i] = wRef[i];
260 }
261 }
262
263 spectrum_.setData(v, Vector<Bool>(v.nelements(), True));
264 weightSum_.setData(w, Vector<Bool>(w.nelements(), True));
265
266 //-------
267 cout << "SA-";
268 for (uInt i=0; i<13; ++i) {
269 cout << "[" << v[i] << "]";
270 }
271 cout << endl;
272 cout << "WA-";
273 for (uInt i=0; i<13; ++i) {
274 cout << "[" << w[i] << "]";
275 }
276 cout << endl;
277 //-------
278}
Note: See TracBrowser for help on using the repository browser.