Changeset 2126 for branches/casa-prerelease/pre-asap/src/RowAccumulator.cpp
- Timestamp:
- 04/08/11 21:06:20 (13 years ago)
- Location:
- branches/casa-prerelease/pre-asap
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/casa-prerelease/pre-asap
-
branches/casa-prerelease/pre-asap/src
- Property svn:mergeinfo changed
/trunk/src merged: 2125
- Property svn:mergeinfo changed
-
branches/casa-prerelease/pre-asap/src/RowAccumulator.cpp
r1819 r2126 10 10 // 11 11 // 12 13 #include <iostream> 14 12 15 #include <casa/iomanip.h> 13 14 16 #include <casa/Arrays/MaskArrMath.h> 15 17 #include <casa/Arrays/MaskArrLogi.h> … … 18 20 #include "RowAccumulator.h" 19 21 20 21 22 using namespace casa; 22 23 using namespace asap; 23 24 24 RowAccumulator::RowAccumulator(WeightType wt) : 25 weightType_(wt), 26 initialized_(False) 25 RowAccumulator::RowAccumulator(WeightType wt) : weightType_(wt), initialized_(False) 27 26 { 28 27 reset(); … … 34 33 35 34 36 void RowAccumulator::add( const Vector< Float >& v, 37 const Vector< Bool >& m, 38 const Vector< Float >& tsys, 39 Double interval, 40 Double time ) 41 { 42 if (!initialized_) { 43 Vector<Float> dummy(v.nelements(), 0.0); 44 Vector<Bool> dummymsk(m.nelements(), True); 45 spectrum_.setData(dummy, dummymsk); 46 n_.setData(Vector<uInt>(v.nelements(), 0), dummymsk); 47 weightSum_.setData(Vector<Float>(v.nelements(), 0.0), dummymsk); 48 tsysSum_.resize(tsys.nelements()); tsysSum_=0.0; 49 } 50 // add spectrum related weights, so far it is variance only. 51 Float totalweight = 1.0; 52 53 // only add these if not everything masked 54 if ( !allEQ(m, False) ) { 55 totalweight *= addTsys(tsys); 56 totalweight *= addInterval(interval); 57 addTime(time); 58 } 59 addSpectrum(v, m, totalweight); 60 initialized_ = True; 61 } 62 63 void RowAccumulator::addSpectrum( const Vector< Float >& v, 64 const Vector< Bool >& m, 65 Float weight) 66 { 67 Float totalweight = weight; 68 MaskedArray<Float> data(v,m); 69 if ( weightType_ == asap::W_VAR ) { 70 if (m.nelements() == userMask_.nelements()) { 71 Float fac = 1.0/variance(data(userMask_)); 72 totalweight *= fac; 73 } else { 74 Float fac = 1.0/variance(data); 75 totalweight *= fac; 76 } 77 } 78 data *= totalweight; 79 MaskedArray<Float> wadd(Vector<Float>(m.nelements(),totalweight), m); 80 weightSum_ += wadd; 81 spectrum_ += data; 82 const MaskedArray<uInt> inc(Vector<uInt>(m.nelements(),1), m); 83 n_ += inc; 84 } 85 86 Float RowAccumulator::addTsys( const casa::Vector< casa::Float > & v ) 35 void 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 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; 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 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) 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 158 Float RowAccumulator::addTsys(const Vector<Float>& v, Bool ignoreMask) 87 159 { 88 160 // @fixme this assume tsys is the same for all channels 89 161 90 162 Float w = 1.0; 91 tsysSum_ += v[0]; 92 if ( weightType_ == asap::W_TSYS || weightType_ == asap::W_TINTSYS ) { 163 if (ignoreMask) { 164 tsysSumNoMask_ += v[0]; 165 } else { 166 tsysSum_ += v[0]; 167 } 168 if ( weightType_ == W_TSYS || weightType_ == W_TINTSYS ) { 93 169 w /= (v[0]*v[0]); 94 170 } … … 96 172 } 97 173 98 void asap::RowAccumulator::addTime( casa::Double t ) 99 { 100 timeSum_ += t; 101 } 102 103 Float asap::RowAccumulator::addInterval( casa::Double inter ) 174 void RowAccumulator::addTime(Double t, Bool ignoreMask) 175 { 176 if (ignoreMask) { 177 timeSumNoMask_ += t; 178 } else { 179 timeSum_ += t; 180 } 181 } 182 183 Float RowAccumulator::addInterval(Double inter, Bool ignoreMask) 104 184 { 105 185 Float w = 1.0; 106 intervalSum_ += inter; 107 if ( weightType_ == asap::W_TINT || weightType_ == asap::W_TINTSYS ) { 186 if (ignoreMask) { 187 intervalSumNoMask_ += inter; 188 } else { 189 intervalSum_ += inter; 190 } 191 if (weightType_ == W_TINT || weightType_ == W_TINTSYS) { 108 192 w /= Float(inter); 109 193 } … … 111 195 } 112 196 113 void asap::RowAccumulator::reset( ) 114 { 115 initialized_ = False; 116 intervalSum_ = 0.0; 117 tsysSum_.resize(); 118 timeSum_ = 0.0; 119 } 120 121 casa::Vector< casa::Float > RowAccumulator::getSpectrum( ) const 197 Vector<Float> RowAccumulator::getSpectrum() const 122 198 { 123 199 return (spectrum_/weightSum_).getArray(); 124 200 } 125 201 126 casa::Double asap::RowAccumulator::getTime( ) const 127 { 128 uInt n = max(n_); 129 return timeSum_/Float(n); 130 } 131 132 casa::Double asap::RowAccumulator::getInterval( ) const 202 Double RowAccumulator::getTime() const 203 { 204 return timeSum_/Float(max(n_)); 205 } 206 207 Double RowAccumulator::getInterval() const 133 208 { 134 209 return intervalSum_; 135 210 } 136 211 137 casa::Vector< casa::Bool > RowAccumulator::getMask() const212 Vector<Bool> RowAccumulator::getMask() const 138 213 { 139 214 // Return the "total" mask - False where no points have been accumulated. … … 141 216 } 142 217 143 casa::Vector< casa::Float > asap::RowAccumulator::getTsys() const218 Vector<Float> RowAccumulator::getTsys() const 144 219 { 145 220 // @fixme this assumes tsys.nelements() == 1 … … 147 222 } 148 223 149 void asap::RowAccumulator::setUserMask( const casa::Vector< casa::Bool > & m)224 void RowAccumulator::setUserMask(const Vector<Bool>& m) 150 225 { 151 226 userMask_.resize(); … … 154 229 155 230 // Added by TT check the state of RowAccumulator 156 casa::Bool RowAccumulator::state() const231 Bool RowAccumulator::state() const 157 232 { 158 233 return initialized_; 159 234 } 160 235 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 }
Note: See TracChangeset
for help on using the changeset viewer.