[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 | |
---|
| 13 | #include <iostream> |
---|
| 14 | |
---|
[814] | 15 | #include <casa/iomanip.h> |
---|
| 16 | #include <casa/Arrays/MaskArrMath.h> |
---|
[1314] | 17 | #include <casa/Arrays/MaskArrLogi.h> |
---|
[814] | 18 | #include <casa/Arrays/ArrayMath.h> |
---|
[1314] | 19 | #include <casa/Arrays/ArrayLogical.h> |
---|
[814] | 20 | #include "RowAccumulator.h" |
---|
| 21 | |
---|
| 22 | using namespace casa; |
---|
| 23 | using namespace asap; |
---|
| 24 | |
---|
[2125] | 25 | RowAccumulator::RowAccumulator(WeightType wt) : weightType_(wt), initialized_(False) |
---|
[814] | 26 | { |
---|
| 27 | reset(); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | RowAccumulator::~RowAccumulator() |
---|
| 31 | { |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
[2125] | 35 | void RowAccumulator::reset(const uInt size, const uInt tsysSize) |
---|
[814] | 36 | { |
---|
[2125] | 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 | |
---|
| 58 | void RowAccumulator::initialize(const uInt size, const uInt tsysSize) |
---|
| 59 | { |
---|
| 60 | reset(size, tsysSize); |
---|
| 61 | initialized_ = True; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void 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 | |
---|
| 77 | void 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 | |
---|
| 87 | void 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 | } |
---|
[1414] | 113 | |
---|
[2125] | 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 | // |
---|
[814] | 130 | } |
---|
| 131 | |
---|
[2125] | 132 | Float RowAccumulator::getTotalWeight(const MaskedArray<Float>& data, |
---|
| 133 | const Vector<Float>& tsys, |
---|
| 134 | const Double interval, |
---|
| 135 | const Double time, |
---|
| 136 | const Bool ignoreMask) |
---|
[814] | 137 | { |
---|
[2125] | 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); |
---|
[814] | 145 | } |
---|
[2125] | 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; |
---|
[814] | 156 | } |
---|
| 157 | |
---|
[2125] | 158 | Float RowAccumulator::addTsys(const Vector<Float>& v, Bool ignoreMask) |
---|
[814] | 159 | { |
---|
| 160 | // @fixme this assume tsys is the same for all channels |
---|
| 161 | |
---|
| 162 | Float w = 1.0; |
---|
[2125] | 163 | if (ignoreMask) { |
---|
| 164 | tsysSumNoMask_ += v[0]; |
---|
| 165 | } else { |
---|
| 166 | tsysSum_ += v[0]; |
---|
| 167 | } |
---|
| 168 | if ( weightType_ == W_TSYS || weightType_ == W_TINTSYS ) { |
---|
[814] | 169 | w /= (v[0]*v[0]); |
---|
| 170 | } |
---|
| 171 | return w; |
---|
| 172 | } |
---|
| 173 | |
---|
[2125] | 174 | void RowAccumulator::addTime(Double t, Bool ignoreMask) |
---|
[814] | 175 | { |
---|
[2125] | 176 | if (ignoreMask) { |
---|
| 177 | timeSumNoMask_ += t; |
---|
| 178 | } else { |
---|
| 179 | timeSum_ += t; |
---|
| 180 | } |
---|
[814] | 181 | } |
---|
| 182 | |
---|
[2125] | 183 | Float RowAccumulator::addInterval(Double inter, Bool ignoreMask) |
---|
[814] | 184 | { |
---|
| 185 | Float w = 1.0; |
---|
[2125] | 186 | if (ignoreMask) { |
---|
| 187 | intervalSumNoMask_ += inter; |
---|
| 188 | } else { |
---|
| 189 | intervalSum_ += inter; |
---|
| 190 | } |
---|
| 191 | if (weightType_ == W_TINT || weightType_ == W_TINTSYS) { |
---|
[814] | 192 | w /= Float(inter); |
---|
| 193 | } |
---|
| 194 | return w; |
---|
| 195 | } |
---|
| 196 | |
---|
[2125] | 197 | Vector<Float> RowAccumulator::getSpectrum() const |
---|
[814] | 198 | { |
---|
| 199 | return (spectrum_/weightSum_).getArray(); |
---|
| 200 | } |
---|
| 201 | |
---|
[2125] | 202 | Double RowAccumulator::getTime() const |
---|
[814] | 203 | { |
---|
[2125] | 204 | return timeSum_/Float(max(n_)); |
---|
[814] | 205 | } |
---|
| 206 | |
---|
[2125] | 207 | Double RowAccumulator::getInterval() const |
---|
[814] | 208 | { |
---|
| 209 | return intervalSum_; |
---|
| 210 | } |
---|
| 211 | |
---|
[2125] | 212 | Vector<Bool> RowAccumulator::getMask() const |
---|
[814] | 213 | { |
---|
[1352] | 214 | // Return the "total" mask - False where no points have been accumulated. |
---|
[1398] | 215 | return (n_.getArray() > uInt(0)); |
---|
[814] | 216 | } |
---|
| 217 | |
---|
[2125] | 218 | Vector<Float> RowAccumulator::getTsys() const |
---|
[814] | 219 | { |
---|
[1314] | 220 | // @fixme this assumes tsys.nelements() == 1 |
---|
[1398] | 221 | return tsysSum_/Float(max(n_)); |
---|
[814] | 222 | } |
---|
| 223 | |
---|
[2125] | 224 | void RowAccumulator::setUserMask(const Vector<Bool>& m) |
---|
[814] | 225 | { |
---|
| 226 | userMask_.resize(); |
---|
| 227 | userMask_ = m; |
---|
| 228 | } |
---|
[1819] | 229 | |
---|
| 230 | // Added by TT check the state of RowAccumulator |
---|
[2125] | 231 | Bool RowAccumulator::state() const |
---|
[1819] | 232 | { |
---|
| 233 | return initialized_; |
---|
| 234 | } |
---|
| 235 | |
---|
[2125] | 236 | void 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 | } |
---|