source: trunk/src/RowAccumulator.cpp@ 2138

Last change on this file since 2138 was 2135, checked in by WataruKawasaki, 13 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): SD

Description: bugfix for RowAccumulator


File size: 5.5 KB
RevLine 
[814]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//
[2125]12
[814]13#include <casa/iomanip.h>
14#include <casa/Arrays/MaskArrMath.h>
[1314]15#include <casa/Arrays/MaskArrLogi.h>
[814]16#include <casa/Arrays/ArrayMath.h>
[1314]17#include <casa/Arrays/ArrayLogical.h>
[814]18#include "RowAccumulator.h"
19
20using namespace casa;
21using namespace asap;
22
[2125]23RowAccumulator::RowAccumulator(WeightType wt) : weightType_(wt), initialized_(False)
[814]24{
25 reset();
26}
27
28RowAccumulator::~RowAccumulator()
29{
30}
31
32
[2125]33void RowAccumulator::reset(const uInt size, const uInt tsysSize)
[814]34{
[2125]35 Vector<Bool> m(size, True);
36
37 spectrum_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
38 spectrumNoMask_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
39
40 n_.setData(Vector<uInt>(size, 0), Vector<Bool>(size, True));
41 nNoMask_.setData(Vector<uInt>(size, 0), Vector<Bool>(size, True));
42
43 weightSum_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
44 weightSumNoMask_.setData(Vector<Float>(size, 0.0), Vector<Bool>(size, True));
45
46 tsysSum_.resize(tsysSize); tsysSum_=0.0;
47 tsysSumNoMask_.resize(tsysSize); tsysSumNoMask_=0.0;
48
49 intervalSum_ = 0.0;
50 intervalSumNoMask_ = 0.0;
51
52 timeSum_ = 0.0;
53 timeSumNoMask_ = 0.0;
[2135]54
55 initialized_ = False;
[2125]56}
57
58void RowAccumulator::initialize(const uInt size, const uInt tsysSize)
59{
[2135]60 reset(size, tsysSize);
61 initialized_ = True;
[2125]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;
[814]112 }
113}
114
[2125]115Float RowAccumulator::getTotalWeight(const MaskedArray<Float>& data,
116 const Vector<Float>& tsys,
117 const Double interval,
118 const Double time,
119 const Bool ignoreMask)
[814]120{
[2125]121 Float totalWeight = 1.0;
122
123 Vector<Bool> m = data.getMask();
124 if (!allEQ(m, False)) { // only add these if not everything masked
125 totalWeight *= addTsys(tsys, ignoreMask);
126 totalWeight *= addInterval(interval, ignoreMask);
127 addTime(time, ignoreMask);
[814]128 }
[2125]129
130 if (weightType_ == W_VAR) {
131 Float fac = 1.0/variance(data);
132 if (!ignoreMask && (m.nelements() == userMask_.nelements()))
133 fac = 1.0/variance(data(userMask_));
134
135 totalWeight *= fac;
136 }
137
138 return totalWeight;
[814]139}
140
[2125]141Float RowAccumulator::addTsys(const Vector<Float>& v, Bool ignoreMask)
[814]142{
143 // @fixme this assume tsys is the same for all channels
144
145 Float w = 1.0;
[2125]146 if (ignoreMask) {
147 tsysSumNoMask_ += v[0];
148 } else {
149 tsysSum_ += v[0];
150 }
151 if ( weightType_ == W_TSYS || weightType_ == W_TINTSYS ) {
[814]152 w /= (v[0]*v[0]);
153 }
154 return w;
155}
156
[2125]157void RowAccumulator::addTime(Double t, Bool ignoreMask)
[814]158{
[2125]159 if (ignoreMask) {
160 timeSumNoMask_ += t;
161 } else {
162 timeSum_ += t;
163 }
[814]164}
165
[2125]166Float RowAccumulator::addInterval(Double inter, Bool ignoreMask)
[814]167{
168 Float w = 1.0;
[2125]169 if (ignoreMask) {
170 intervalSumNoMask_ += inter;
171 } else {
172 intervalSum_ += inter;
173 }
174 if (weightType_ == W_TINT || weightType_ == W_TINTSYS) {
[814]175 w /= Float(inter);
176 }
177 return w;
178}
179
[2125]180Vector<Float> RowAccumulator::getSpectrum() const
[814]181{
182 return (spectrum_/weightSum_).getArray();
183}
184
[2125]185Double RowAccumulator::getTime() const
[814]186{
[2125]187 return timeSum_/Float(max(n_));
[814]188}
189
[2125]190Double RowAccumulator::getInterval() const
[814]191{
192 return intervalSum_;
193}
194
[2125]195Vector<Bool> RowAccumulator::getMask() const
[814]196{
[1352]197 // Return the "total" mask - False where no points have been accumulated.
[1398]198 return (n_.getArray() > uInt(0));
[814]199}
200
[2125]201Vector<Float> RowAccumulator::getTsys() const
[814]202{
[1314]203 // @fixme this assumes tsys.nelements() == 1
[1398]204 return tsysSum_/Float(max(n_));
[814]205}
206
[2125]207void RowAccumulator::setUserMask(const Vector<Bool>& m)
[814]208{
209 userMask_.resize();
210 userMask_ = m;
211}
[1819]212
213// Added by TT check the state of RowAccumulator
[2125]214Bool RowAccumulator::state() const
[1819]215{
216 return initialized_;
217}
218
[2125]219void RowAccumulator::replaceNaN()
220{
221 Vector<Float> v = spectrum_.getArray();
222 Vector<Float> w = weightSum_.getArray();
223 Vector<Float> vRef = spectrumNoMask_.getArray();
224 Vector<Float> wRef = weightSumNoMask_.getArray();
225
226 for (uInt i = 0; i < v.size(); ++i) {
227 if (w[i] == 0.0) {
228 v[i] = vRef[i];
229 w[i] = wRef[i];
230 }
231 }
232
233 spectrum_.setData(v, Vector<Bool>(v.nelements(), True));
234 weightSum_.setData(w, Vector<Bool>(w.nelements(), True));
235}
Note: See TracBrowser for help on using the repository browser.