[2707] | 1 | // C++ Interface: STSideBandSep
|
---|
| 2 | //
|
---|
| 3 | // Description:
|
---|
| 4 | // A class to invoke sideband separation of Scantable
|
---|
| 5 | //
|
---|
| 6 | // Author: Kana Sugimoto <kana.sugi@nao.ac.jp>, (C) 2012
|
---|
| 7 | //
|
---|
| 8 | // Copyright: See COPYING file that comes with this distribution
|
---|
| 9 | //
|
---|
| 10 | //
|
---|
| 11 |
|
---|
| 12 | // STL
|
---|
| 13 | #include <ctype.h>
|
---|
| 14 |
|
---|
| 15 | // cascore
|
---|
| 16 | #include <casa/OS/File.h>
|
---|
| 17 | #include <casa/Logging/LogIO.h>
|
---|
| 18 | #include <casa/Quanta/QuantumHolder.h>
|
---|
| 19 |
|
---|
| 20 | #include <measures/Measures/MFrequency.h>
|
---|
| 21 | #include <measures/Measures/MCFrequency.h>
|
---|
| 22 |
|
---|
[2711] | 23 | #include <tables/Tables/TableRow.h>
|
---|
[2707] | 24 | #include <tables/Tables/TableRecord.h>
|
---|
| 25 | #include <tables/Tables/TableVector.h>
|
---|
| 26 |
|
---|
| 27 | // asap
|
---|
[2794] | 28 | #include "STGrid.h"
|
---|
| 29 | #include "STMath.h"
|
---|
| 30 | #include "MathUtils.h"
|
---|
[2707] | 31 | #include "STSideBandSep.h"
|
---|
| 32 |
|
---|
| 33 | using namespace std ;
|
---|
| 34 | using namespace casa ;
|
---|
| 35 | using namespace asap ;
|
---|
| 36 |
|
---|
[2794] | 37 | // #ifndef KS_DEBUG
|
---|
| 38 | // #define KS_DEBUG
|
---|
| 39 | // #endif
|
---|
[2707] | 40 |
|
---|
| 41 | namespace asap {
|
---|
| 42 |
|
---|
| 43 | // constructors
|
---|
[2726] | 44 | STSideBandSep::STSideBandSep(const vector<string> &names)
|
---|
[2707] | 45 | {
|
---|
[2726] | 46 | LogIO os(LogOrigin("STSideBandSep","STSideBandSep()", WHERE));
|
---|
| 47 | os << "Setting scantable names to process." << LogIO::POST ;
|
---|
| 48 | // Set file names
|
---|
| 49 | ntable_ = names.size();
|
---|
| 50 | infileList_.resize(ntable_);
|
---|
| 51 | for (unsigned int i = 0; i < ntable_; i++){
|
---|
| 52 | if (!checkFile(names[i], "d"))
|
---|
| 53 | throw( AipsError("File does not exist") );
|
---|
| 54 | infileList_[i] = names[i];
|
---|
| 55 | }
|
---|
| 56 | intabList_.resize(0);
|
---|
| 57 |
|
---|
| 58 | init();
|
---|
| 59 |
|
---|
| 60 | {// Summary
|
---|
| 61 | os << ntable_ << " files are set: [";
|
---|
| 62 | for (unsigned int i = 0; i < ntable_; i++) {
|
---|
| 63 | os << " '" << infileList_[i] << "' ";
|
---|
| 64 | if (i != ntable_-1) os << ",";
|
---|
| 65 | }
|
---|
| 66 | os << "] " << LogIO::POST;
|
---|
| 67 | }
|
---|
[2707] | 68 | };
|
---|
| 69 |
|
---|
[2726] | 70 | STSideBandSep::STSideBandSep(const vector<ScantableWrapper> &tables)
|
---|
| 71 | {
|
---|
| 72 | LogIO os(LogOrigin("STSideBandSep","STSideBandSep()", WHERE));
|
---|
| 73 | os << "Setting list of scantables to process." << LogIO::POST ;
|
---|
| 74 | // Set file names
|
---|
| 75 | ntable_ = tables.size();
|
---|
| 76 | intabList_.resize(ntable_);
|
---|
| 77 | for (unsigned int i = 0; i < ntable_; i++){
|
---|
| 78 | intabList_[i] = tables[i].getCP();
|
---|
| 79 | }
|
---|
| 80 | infileList_.resize(0);
|
---|
| 81 |
|
---|
| 82 | init();
|
---|
[2794] | 83 | tp_ = intabList_[0]->table().tableType();
|
---|
[2726] | 84 |
|
---|
| 85 | os << ntable_ << " tables are set." << LogIO::POST;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
[2707] | 88 | STSideBandSep::~STSideBandSep()
|
---|
| 89 | {
|
---|
| 90 | };
|
---|
| 91 |
|
---|
[2726] | 92 | void STSideBandSep::init()
|
---|
| 93 | {
|
---|
| 94 | // frequency setup
|
---|
[2794] | 95 | sigIfno_= -1;
|
---|
[2726] | 96 | ftol_ = -1;
|
---|
| 97 | solFrame_ = MFrequency::N_Types;
|
---|
| 98 | // shifts
|
---|
| 99 | initshift();
|
---|
| 100 | // direction tolerance
|
---|
| 101 | xtol_ = ytol_ = 9.69627e-6; // 2arcsec
|
---|
| 102 | // solution parameters
|
---|
| 103 | otherside_ = false;
|
---|
| 104 | doboth_ = false;
|
---|
| 105 | rejlimit_ = 0.2;
|
---|
| 106 | // LO1 values
|
---|
| 107 | lo1Freq_ = -1;
|
---|
| 108 | loTime_ = -1;
|
---|
| 109 | loDir_ = "";
|
---|
| 110 | // Default LO frame is TOPO
|
---|
| 111 | loFrame_ = MFrequency::TOPO;
|
---|
[2794] | 112 | // scantable storage
|
---|
| 113 | tp_ = Table::Memory;
|
---|
[2726] | 114 | };
|
---|
[2707] | 115 |
|
---|
[2726] | 116 | void STSideBandSep::initshift()
|
---|
[2707] | 117 | {
|
---|
[2726] | 118 | // shifts
|
---|
| 119 | nshift_ = 0;
|
---|
| 120 | nchan_ = 0;
|
---|
| 121 | sigShift_.resize(0);
|
---|
| 122 | imgShift_.resize(0);
|
---|
| 123 | tableList_.resize(0);
|
---|
[2707] | 124 | };
|
---|
| 125 |
|
---|
[2726] | 126 | void STSideBandSep::setFrequency(const unsigned int ifno,
|
---|
| 127 | const string freqtol,
|
---|
| 128 | const string frame)
|
---|
| 129 | {
|
---|
| 130 | LogIO os(LogOrigin("STSideBandSep","setFrequency()", WHERE));
|
---|
| 131 |
|
---|
| 132 | initshift();
|
---|
| 133 |
|
---|
| 134 | // IFNO
|
---|
[2794] | 135 | sigIfno_ = (int) ifno;
|
---|
[2726] | 136 |
|
---|
| 137 | // Frequency tolerance
|
---|
| 138 | Quantum<Double> qftol;
|
---|
| 139 | readQuantity(qftol, String(freqtol));
|
---|
| 140 | if (!qftol.getUnit().empty()){
|
---|
| 141 | // make sure the quantity is frequency
|
---|
| 142 | if (qftol.getFullUnit().getValue() != Unit("Hz").getValue())
|
---|
| 143 | throw( AipsError("Invalid quantity for frequency tolerance.") );
|
---|
| 144 | qftol.convert("Hz");
|
---|
| 145 | }
|
---|
| 146 | ftol_ = qftol;
|
---|
| 147 |
|
---|
| 148 | // Frequency Frame
|
---|
| 149 | if (!frame.empty()){
|
---|
| 150 | MFrequency::Types mft;
|
---|
| 151 | if (!MFrequency::getType(mft, frame))
|
---|
| 152 | throw( AipsError("Invalid frame type.") );
|
---|
| 153 | solFrame_ = mft;
|
---|
| 154 | } else {
|
---|
| 155 | solFrame_ = MFrequency::N_Types;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | {// Summary
|
---|
| 159 | const String sframe = ( (solFrame_ == MFrequency::N_Types) ?
|
---|
| 160 | "table frame" :
|
---|
| 161 | MFrequency::showType(solFrame_) );
|
---|
| 162 | os << "Frequency setup to search IF group: "
|
---|
| 163 | << "IFNO of table[0] = " << sigIfno_
|
---|
| 164 | << " , Freq tolerance = " << ftol_.getValue() << " [ "
|
---|
| 165 | << (ftol_.getUnit().empty() ? "channel" : ftol_.getUnit() )
|
---|
| 166 | << " ] (in " << sframe <<")" << LogIO::POST;
|
---|
| 167 | }
|
---|
| 168 | };
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | void STSideBandSep::setDirTolerance(const vector<string> dirtol)
|
---|
| 172 | {
|
---|
| 173 | LogIO os(LogOrigin("STSideBandSep","setDirTolerance()", WHERE));
|
---|
| 174 | Quantum<Double> qcell;
|
---|
| 175 | if ( (dirtol.size() == 1) && !dirtol[0].empty() ) {
|
---|
| 176 | readQuantity(qcell, String(dirtol[0]));
|
---|
| 177 | if (qcell.getFullUnit().getValue() == Unit("rad").getValue())
|
---|
| 178 | xtol_ = ytol_ = qcell.getValue("rad");
|
---|
| 179 | else
|
---|
| 180 | throw( AipsError("Invalid unit for direction tolerance.") );
|
---|
| 181 | }
|
---|
| 182 | else if (dirtol.size() > 1) {
|
---|
| 183 | if ( dirtol[0].empty() && dirtol[1].empty() )
|
---|
| 184 | throw( AipsError("Direction tolerance is empty.") );
|
---|
| 185 | if ( !dirtol[0].empty() ) {
|
---|
| 186 | readQuantity(qcell, String(dirtol[0]));
|
---|
| 187 | if (qcell.getFullUnit().getValue() == Unit("rad").getValue())
|
---|
| 188 | xtol_ = qcell.getValue("rad");
|
---|
| 189 | else
|
---|
| 190 | throw( AipsError("Invalid unit for direction tolerance.") );
|
---|
| 191 | }
|
---|
| 192 | if ( !dirtol[1].empty() ) {
|
---|
| 193 | readQuantity(qcell, String(dirtol[1]));
|
---|
| 194 | if (qcell.getFullUnit().getValue() == Unit("rad").getValue())
|
---|
| 195 | ytol_ = qcell.getValue("rad");
|
---|
| 196 | else
|
---|
| 197 | throw( AipsError("Invalid unit for direction tolerance.") );
|
---|
| 198 | }
|
---|
| 199 | else {
|
---|
| 200 | ytol_ = xtol_;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | else throw( AipsError("Invalid direction tolerance.") );
|
---|
| 204 |
|
---|
| 205 | os << "Direction tolerance: ( "
|
---|
| 206 | << xtol_ << " , " << ytol_ << " ) [rad]" << LogIO::POST;
|
---|
| 207 | };
|
---|
| 208 |
|
---|
| 209 | void STSideBandSep::setShift(const vector<double> &shift)
|
---|
| 210 | {
|
---|
| 211 | LogIO os(LogOrigin("STSideBandSep","setShift()", WHERE));
|
---|
| 212 | imgShift_.resize(shift.size());
|
---|
| 213 | for (unsigned int i = 0; i < shift.size(); i++)
|
---|
| 214 | imgShift_[i] = shift[i];
|
---|
| 215 |
|
---|
| 216 | if (imgShift_.size() == 0) {
|
---|
| 217 | os << "Channel shifts are cleared." << LogIO::POST;
|
---|
| 218 | } else {
|
---|
| 219 | os << "Channel shifts of image sideband are set: ( ";
|
---|
| 220 | for (unsigned int i = 0; i < imgShift_.size(); i++) {
|
---|
| 221 | os << imgShift_[i];
|
---|
| 222 | if (i != imgShift_.size()-1) os << " , ";
|
---|
| 223 | }
|
---|
[2794] | 224 | os << " ) [channels]" << LogIO::POST;
|
---|
[2726] | 225 | }
|
---|
| 226 | };
|
---|
| 227 |
|
---|
| 228 | void STSideBandSep::setThreshold(const double limit)
|
---|
| 229 | {
|
---|
| 230 | LogIO os(LogOrigin("STSideBandSep","setThreshold()", WHERE));
|
---|
| 231 | if (limit < 0)
|
---|
[2794] | 232 | throw( AipsError("Rejection limit should be a positive number.") );
|
---|
[2726] | 233 |
|
---|
| 234 | rejlimit_ = limit;
|
---|
[2794] | 235 | os << "Rejection limit is set to " << rejlimit_ << LogIO::POST;
|
---|
[2726] | 236 | };
|
---|
| 237 |
|
---|
[2794] | 238 | void STSideBandSep::separate(string outname)
|
---|
[2707] | 239 | {
|
---|
[2794] | 240 | LogIO os(LogOrigin("STSideBandSep","separate()", WHERE));
|
---|
| 241 | if (outname.empty())
|
---|
| 242 | outname = "sbseparated.asap";
|
---|
[2707] | 243 |
|
---|
[2794] | 244 | // Set up a goup of IFNOs in the list of scantables within
|
---|
| 245 | // the frequency tolerance and make them a list.
|
---|
| 246 | nshift_ = setupShift();
|
---|
| 247 | if (nshift_ < 2)
|
---|
| 248 | throw( AipsError("At least 2 IFs are necessary for convolution.") );
|
---|
| 249 | // Grid scantable and generate output tables
|
---|
| 250 | ScantableWrapper gridst = gridTable();
|
---|
| 251 | sigTab_p = gridst.getCP();
|
---|
| 252 | if (doboth_)
|
---|
| 253 | imgTab_p = gridst.copy().getCP();
|
---|
| 254 | vector<unsigned int> remRowIds;
|
---|
| 255 | remRowIds.resize(0);
|
---|
| 256 | Matrix<float> specMat(nchan_, nshift_);
|
---|
[2852] | 257 | Matrix<bool> flagMat(nchan_, nshift_);
|
---|
[2794] | 258 | vector<float> sigSpec(nchan_), imgSpec(nchan_);
|
---|
[2852] | 259 | Vector<bool> flagVec(nchan_);
|
---|
[2794] | 260 | vector<uInt> tabIdvec;
|
---|
| 261 |
|
---|
| 262 | //Generate FFTServer
|
---|
| 263 | fftsf.resize(IPosition(1, nchan_), FFTEnums::REALTOCOMPLEX);
|
---|
| 264 | fftsi.resize(IPosition(1, nchan_), FFTEnums::COMPLEXTOREAL);
|
---|
| 265 |
|
---|
| 266 | /// Loop over sigTab_p and separate sideband
|
---|
| 267 | for (int irow = 0; irow < sigTab_p->nrow(); irow++){
|
---|
| 268 | tabIdvec.resize(0);
|
---|
| 269 | const int polId = sigTab_p->getPol(irow);
|
---|
| 270 | const int beamId = sigTab_p->getBeam(irow);
|
---|
| 271 | const vector<double> dir = sigTab_p->getDirectionVector(irow);
|
---|
| 272 | // Get a set of spectra to solve
|
---|
| 273 | if (!getSpectraToSolve(polId, beamId, dir[0], dir[1],
|
---|
[2852] | 274 | specMat, flagMat, tabIdvec)){
|
---|
[2794] | 275 | remRowIds.push_back(irow);
|
---|
| 276 | #ifdef KS_DEBUG
|
---|
| 277 | cout << "no matching row found. skipping row = " << irow << endl;
|
---|
| 278 | #endif
|
---|
| 279 | continue;
|
---|
| 280 | }
|
---|
| 281 | // Solve signal sideband
|
---|
| 282 | sigSpec = solve(specMat, tabIdvec, true);
|
---|
| 283 | sigTab_p->setSpectrum(sigSpec, irow);
|
---|
[2838] | 284 | if (sigTab_p->isAllChannelsFlagged(irow)){
|
---|
| 285 | // unflag the spectrum since there should be some valid data
|
---|
| 286 | sigTab_p->flagRow(vector<uInt>(irow), true);
|
---|
[2852] | 287 | // need to unflag whole channels anyway
|
---|
[2838] | 288 | sigTab_p->flag(irow, vector<bool>(), true);
|
---|
| 289 | }
|
---|
[2852] | 290 | // apply channel flag
|
---|
| 291 | flagVec = collapseFlag(flagMat, tabIdvec, true);
|
---|
| 292 | //boolVec = !boolVec; // flag
|
---|
[2856] | 293 | vector<bool> tmpflag;
|
---|
| 294 | flagVec.tovector(tmpflag);
|
---|
| 295 | sigTab_p->flag(irow, tmpflag, false);
|
---|
[2794] | 296 |
|
---|
| 297 | // Solve image sideband
|
---|
| 298 | if (doboth_) {
|
---|
| 299 | imgSpec = solve(specMat, tabIdvec, false);
|
---|
| 300 | imgTab_p->setSpectrum(imgSpec, irow);
|
---|
[2838] | 301 | if (imgTab_p->isAllChannelsFlagged(irow)){
|
---|
| 302 | // unflag the spectrum since there should be some valid data
|
---|
| 303 | imgTab_p->flagRow(vector<uInt>(irow), true);
|
---|
[2852] | 304 | // need to unflag whole channels anyway
|
---|
[2838] | 305 | imgTab_p->flag(irow, vector<bool>(), true);
|
---|
| 306 | }
|
---|
[2852] | 307 | // apply channel flag
|
---|
| 308 | flagVec = collapseFlag(flagMat, tabIdvec, false);
|
---|
| 309 | //boolVec = !boolVec; // flag
|
---|
[2856] | 310 | flagVec.tovector(tmpflag);
|
---|
[2857] | 311 | imgTab_p->flag(irow, tmpflag, false);
|
---|
[2794] | 312 | }
|
---|
| 313 | } // end of row loop
|
---|
| 314 |
|
---|
| 315 | // Remove or flag rows without relevant data from gridded tables
|
---|
| 316 | if (remRowIds.size() > 0) {
|
---|
| 317 | const size_t nrem = remRowIds.size();
|
---|
| 318 | if ( sigTab_p->table().canRemoveRow() ) {
|
---|
| 319 | sigTab_p->table().removeRow(remRowIds);
|
---|
| 320 | os << "Removing " << nrem << " rows from the signal band table"
|
---|
| 321 | << LogIO::POST;
|
---|
| 322 | } else {
|
---|
| 323 | sigTab_p->flagRow(remRowIds, false);
|
---|
| 324 | os << "Cannot remove rows from the signal band table. Flagging "
|
---|
| 325 | << nrem << " rows" << LogIO::POST;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | if (doboth_) {
|
---|
| 329 | if ( imgTab_p->table().canRemoveRow() ) {
|
---|
| 330 | imgTab_p->table().removeRow(remRowIds);
|
---|
| 331 | os << "Removing " << nrem << " rows from the image band table"
|
---|
| 332 | << LogIO::POST;
|
---|
| 333 | } else {
|
---|
| 334 | imgTab_p->flagRow(remRowIds, false);
|
---|
| 335 | os << "Cannot remove rows from the image band table. Flagging "
|
---|
| 336 | << nrem << " rows" << LogIO::POST;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | // Finally, save tables on disk
|
---|
| 342 | if (outname.size() ==0)
|
---|
| 343 | outname = "sbseparated.asap";
|
---|
| 344 | const string sigName = outname + ".signalband";
|
---|
| 345 | os << "Saving SIGNAL sideband table: " << sigName << LogIO::POST;
|
---|
| 346 | sigTab_p->makePersistent(sigName);
|
---|
| 347 | if (doboth_) {
|
---|
| 348 | solveImageFrequency();
|
---|
| 349 | const string imgName = outname + ".imageband";
|
---|
| 350 | os << "Saving IMAGE sideband table: " << sigName << LogIO::POST;
|
---|
| 351 | imgTab_p->makePersistent(imgName);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[2707] | 354 | };
|
---|
| 355 |
|
---|
[2794] | 356 | unsigned int STSideBandSep::setupShift()
|
---|
| 357 | {
|
---|
| 358 | LogIO os(LogOrigin("STSideBandSep","setupShift()", WHERE));
|
---|
| 359 | if (infileList_.size() == 0 && intabList_.size() == 0)
|
---|
| 360 | throw( AipsError("No scantable has been set. Set a list of scantables first.") );
|
---|
| 361 |
|
---|
| 362 | const bool byname = (intabList_.size() == 0);
|
---|
| 363 | // Make sure sigIfno_ exists in the first table.
|
---|
| 364 | CountedPtr<Scantable> stab;
|
---|
| 365 | vector<string> coordsav;
|
---|
| 366 | vector<string> coordtmp(3);
|
---|
| 367 | os << "Checking IFNO in the first table." << LogIO::POST;
|
---|
| 368 | if (byname) {
|
---|
| 369 | if (!checkFile(infileList_[0], "d"))
|
---|
| 370 | os << LogIO::SEVERE << "Could not find scantable '" << infileList_[0]
|
---|
| 371 | << "'" << LogIO::EXCEPTION;
|
---|
| 372 | stab = CountedPtr<Scantable>(new Scantable(infileList_[0]));
|
---|
| 373 | } else {
|
---|
| 374 | stab = intabList_[0];
|
---|
| 375 | }
|
---|
| 376 | if (sigIfno_ < 0) {
|
---|
| 377 | sigIfno_ = (int) stab->getIF(0);
|
---|
| 378 | os << "IFNO to process has not been set. Using the first IF = "
|
---|
| 379 | << sigIfno_ << LogIO::POST;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | unsigned int basench;
|
---|
| 383 | double basech0, baseinc, ftolval, inctolval;
|
---|
| 384 | coordsav = stab->getCoordInfo();
|
---|
| 385 | const string stfframe = coordsav[1];
|
---|
| 386 | coordtmp[0] = "Hz";
|
---|
| 387 | coordtmp[1] = ( (solFrame_ == MFrequency::N_Types) ?
|
---|
| 388 | stfframe :
|
---|
| 389 | MFrequency::showType(solFrame_) );
|
---|
| 390 | coordtmp[2] = coordsav[2];
|
---|
| 391 | stab->setCoordInfo(coordtmp);
|
---|
| 392 | if (!getFreqInfo(stab, (unsigned int) sigIfno_, basech0, baseinc, basench)) {
|
---|
| 393 | os << LogIO::SEVERE << "No data with IFNO=" << sigIfno_
|
---|
| 394 | << " found in the first table." << LogIO::EXCEPTION;
|
---|
| 395 | }
|
---|
| 396 | else {
|
---|
| 397 | os << "Found IFNO = " << sigIfno_
|
---|
| 398 | << " in the first table." << LogIO::POST;
|
---|
| 399 | }
|
---|
| 400 | if (ftol_.getUnit().empty()) {
|
---|
| 401 | // tolerance in unit of channels
|
---|
| 402 | ftolval = ftol_.getValue() * baseinc;
|
---|
| 403 | }
|
---|
| 404 | else {
|
---|
| 405 | ftolval = ftol_.getValue("Hz");
|
---|
| 406 | }
|
---|
| 407 | inctolval = abs(baseinc/(double) basench);
|
---|
| 408 | const string poltype0 = stab->getPolType();
|
---|
| 409 |
|
---|
| 410 | // Initialize shift values
|
---|
| 411 | initshift();
|
---|
| 412 |
|
---|
| 413 | const bool setImg = ( doboth_ && (imgShift_.size() == 0) );
|
---|
| 414 | // Select IFs
|
---|
| 415 | for (unsigned int itab = 0; itab < ntable_; itab++ ){
|
---|
| 416 | os << "Table " << itab << LogIO::POST;
|
---|
| 417 | if (itab > 0) {
|
---|
| 418 | if (byname) {
|
---|
| 419 | if (!checkFile(infileList_[itab], "d"))
|
---|
| 420 | os << LogIO::SEVERE << "Could not find scantable '"
|
---|
| 421 | << infileList_[itab] << "'" << LogIO::EXCEPTION;
|
---|
| 422 | stab = CountedPtr<Scantable>(new Scantable(infileList_[itab]));
|
---|
| 423 | } else {
|
---|
| 424 | stab = intabList_[itab];
|
---|
| 425 | }
|
---|
| 426 | //POLTYPE should be the same.
|
---|
| 427 | if (stab->getPolType() != poltype0 ) {
|
---|
| 428 | os << LogIO::WARN << "POLTYPE differs from the first table."
|
---|
| 429 | << " Skipping the table" << LogIO::POST;
|
---|
| 430 | continue;
|
---|
| 431 | }
|
---|
| 432 | // Multi beam data may not handled properly
|
---|
| 433 | if (stab->nbeam() > 1)
|
---|
| 434 | os << LogIO::WARN << "Table contains multiple beams. "
|
---|
| 435 | << "It may not be handled properly." << LogIO::POST;
|
---|
| 436 |
|
---|
| 437 | coordsav = stab->getCoordInfo();
|
---|
| 438 | coordtmp[2] = coordsav[2];
|
---|
| 439 | stab->setCoordInfo(coordtmp);
|
---|
| 440 | }
|
---|
| 441 | bool selected = false;
|
---|
| 442 | vector<uint> ifnos = stab->getIFNos();
|
---|
| 443 | vector<uint>::iterator iter;
|
---|
| 444 | const STSelector& basesel = stab->getSelection();
|
---|
| 445 | for (iter = ifnos.begin(); iter != ifnos.end(); iter++){
|
---|
| 446 | unsigned int nch;
|
---|
| 447 | double freq0, incr;
|
---|
| 448 | if ( getFreqInfo(stab, *iter, freq0, incr, nch) ){
|
---|
| 449 | if ( (nch == basench) && (abs(freq0-basech0) < ftolval)
|
---|
| 450 | && (abs(incr-baseinc) < inctolval) ){
|
---|
| 451 | //Found
|
---|
| 452 | STSelector sel(basesel);
|
---|
| 453 | sel.setIFs(vector<int>(1,(int) *iter));
|
---|
| 454 | stab->setSelection(sel);
|
---|
| 455 | CountedPtr<Scantable> seltab = ( new Scantable(*stab, false) );
|
---|
| 456 | stab->setSelection(basesel);
|
---|
| 457 | seltab->setCoordInfo(coordsav);
|
---|
| 458 | const double chShift = (freq0 - basech0) / baseinc;
|
---|
| 459 | tableList_.push_back(seltab);
|
---|
[2808] | 460 | sigShift_.push_back(-chShift);
|
---|
[2794] | 461 | if (setImg)
|
---|
[2808] | 462 | imgShift_.push_back(chShift);
|
---|
[2794] | 463 |
|
---|
| 464 | selected = true;
|
---|
| 465 | os << "- IF" << *iter << " selected: sideband shift = "
|
---|
| 466 | << chShift << " channels" << LogIO::POST;
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 | } // ifno loop
|
---|
| 470 | stab->setCoordInfo(coordsav);
|
---|
| 471 | if (!selected)
|
---|
| 472 | os << LogIO::WARN << "No data selected in Table "
|
---|
| 473 | << itab << LogIO::POST;
|
---|
| 474 | } // table loop
|
---|
| 475 | nchan_ = basench;
|
---|
| 476 |
|
---|
| 477 | os << "Total number of IFs selected = " << tableList_.size()
|
---|
| 478 | << LogIO::POST;
|
---|
| 479 | if ( setImg && (sigShift_.size() != imgShift_.size()) ){
|
---|
| 480 | os << LogIO::SEVERE
|
---|
| 481 | << "User defined channel shift of image sideband has "
|
---|
| 482 | << imgShift_.size()
|
---|
| 483 | << " elements, while selected IFNOs are " << sigShift_.size()
|
---|
| 484 | << "\nThe frequency tolerance (freqtol) may be too small."
|
---|
| 485 | << LogIO::EXCEPTION;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | return tableList_.size();
|
---|
| 489 | };
|
---|
| 490 |
|
---|
| 491 | bool STSideBandSep::getFreqInfo(const CountedPtr<Scantable> &stab,
|
---|
| 492 | const unsigned int &ifno,
|
---|
| 493 | double &freq0, double &incr,
|
---|
| 494 | unsigned int &nchan)
|
---|
| 495 | {
|
---|
| 496 | vector<uint> ifnos = stab->getIFNos();
|
---|
| 497 | bool found = false;
|
---|
| 498 | vector<uint>::iterator iter;
|
---|
| 499 | for (iter = ifnos.begin(); iter != ifnos.end(); iter++){
|
---|
| 500 | if (*iter == ifno) {
|
---|
| 501 | found = true;
|
---|
| 502 | break;
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
| 505 | if (!found)
|
---|
| 506 | return false;
|
---|
| 507 |
|
---|
| 508 | const STSelector& basesel = stab->getSelection();
|
---|
| 509 | STSelector sel(basesel);
|
---|
| 510 | sel.setIFs(vector<int>(1,(int) ifno));
|
---|
| 511 | stab->setSelection(sel);
|
---|
| 512 | vector<double> freqs;
|
---|
| 513 | freqs = stab->getAbcissa(0);
|
---|
| 514 | freq0 = freqs[0];
|
---|
| 515 | incr = freqs[1] - freqs[0];
|
---|
| 516 | nchan = freqs.size();
|
---|
| 517 | stab->setSelection(basesel);
|
---|
| 518 | return true;
|
---|
| 519 | };
|
---|
| 520 |
|
---|
| 521 | ScantableWrapper STSideBandSep::gridTable()
|
---|
| 522 | {
|
---|
| 523 | LogIO os(LogOrigin("STSideBandSep","gridTable()", WHERE));
|
---|
| 524 | if (tableList_.size() == 0)
|
---|
| 525 | throw( AipsError("Internal error. No scantable has been set to grid.") );
|
---|
| 526 | Double xmin, xmax, ymin, ymax;
|
---|
| 527 | mapExtent(tableList_, xmin, xmax, ymin, ymax);
|
---|
| 528 | const Double centx = 0.5 * (xmin + xmax);
|
---|
| 529 | const Double centy = 0.5 * (ymin + ymax);
|
---|
| 530 | const int nx = max(1, (int) ceil( (xmax - xmin) * cos(centy) /xtol_ ) );
|
---|
| 531 | const int ny = max(1, (int) ceil( (ymax - ymin) / ytol_ ) );
|
---|
| 532 |
|
---|
| 533 | string scellx, scelly;
|
---|
| 534 | {
|
---|
| 535 | ostringstream oss;
|
---|
| 536 | oss << xtol_ << "rad" ;
|
---|
| 537 | scellx = oss.str();
|
---|
| 538 | }
|
---|
| 539 | {
|
---|
| 540 | ostringstream oss;
|
---|
| 541 | oss << ytol_ << "rad" ;
|
---|
| 542 | scelly = oss.str();
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | ScantableWrapper stab0;
|
---|
| 546 | if (intabList_.size() > 0)
|
---|
| 547 | stab0 = ScantableWrapper(intabList_[0]);
|
---|
| 548 | else
|
---|
| 549 | stab0 = ScantableWrapper(infileList_[0]);
|
---|
| 550 |
|
---|
| 551 | string scenter;
|
---|
| 552 | {
|
---|
| 553 | ostringstream oss;
|
---|
| 554 | oss << stab0.getCP()->getDirectionRefString() << " "
|
---|
| 555 | << centx << "rad" << " " << centy << "rad";
|
---|
| 556 | scenter = oss.str();
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | STGrid2 gridder = STGrid2(stab0);
|
---|
| 560 | gridder.setIF(sigIfno_);
|
---|
| 561 | gridder.defineImage(nx, ny, scellx, scelly, scenter);
|
---|
[2835] | 562 | // gridder.setFunc("box", 1); // convsupport=1 fails
|
---|
| 563 | gridder.setFunc("box");
|
---|
[2794] | 564 | gridder.setWeight("uniform");
|
---|
| 565 | #ifdef KS_DEBUG
|
---|
| 566 | cout << "Grid parameter summary: " << endl;
|
---|
| 567 | cout << "- IF = " << sigIfno_ << endl;
|
---|
[2836] | 568 | cout << "- center = " << scenter << "\n"
|
---|
[2794] | 569 | << "- npix = (" << nx << ", " << ny << ")\n"
|
---|
[2836] | 570 | << "- cell = (" << scellx << ", " << scelly << ")" << endl;
|
---|
[2794] | 571 | #endif
|
---|
| 572 | gridder.grid();
|
---|
| 573 | const int itp = (tp_ == Table::Memory ? 0 : 1);
|
---|
| 574 | ScantableWrapper gtab = gridder.getResultAsScantable(itp);
|
---|
| 575 | return gtab;
|
---|
| 576 | };
|
---|
| 577 |
|
---|
| 578 | void STSideBandSep::mapExtent(vector< CountedPtr<Scantable> > &tablist,
|
---|
| 579 | Double &xmin, Double &xmax,
|
---|
| 580 | Double &ymin, Double &ymax)
|
---|
| 581 | {
|
---|
| 582 | ROArrayColumn<Double> dirCol_;
|
---|
| 583 | dirCol_.attach( tablist[0]->table(), "DIRECTION" );
|
---|
| 584 | Matrix<Double> direction = dirCol_.getColumn();
|
---|
| 585 | Vector<Double> ra( direction.row(0) );
|
---|
| 586 | mathutil::rotateRA(ra);
|
---|
| 587 | minMax( xmin, xmax, ra );
|
---|
| 588 | minMax( ymin, ymax, direction.row(1) );
|
---|
| 589 | Double amin, amax, bmin, bmax;
|
---|
| 590 | const uInt ntab = tablist.size();
|
---|
| 591 | for ( uInt i = 1 ; i < ntab ; i++ ) {
|
---|
| 592 | dirCol_.attach( tablist[i]->table(), "DIRECTION" );
|
---|
| 593 | direction.assign( dirCol_.getColumn() );
|
---|
| 594 | ra.assign( direction.row(0) );
|
---|
| 595 | mathutil::rotateRA(ra);
|
---|
| 596 | minMax( amin, amax, ra );
|
---|
| 597 | minMax( bmin, bmax, direction.row(1) );
|
---|
| 598 | xmin = min(xmin, amin);
|
---|
| 599 | xmax = max(xmax, amax);
|
---|
| 600 | ymin = min(ymin, bmin);
|
---|
| 601 | ymax = max(ymax, bmax);
|
---|
| 602 | }
|
---|
| 603 | };
|
---|
| 604 |
|
---|
[2852] | 605 | // bool STSideBandSep::getSpectraToSolve(const int polId, const int beamId,
|
---|
| 606 | // const double dirX, const double dirY,
|
---|
| 607 | // Matrix<float> &specMat, vector<uInt> &tabIdvec)
|
---|
[2794] | 608 | bool STSideBandSep::getSpectraToSolve(const int polId, const int beamId,
|
---|
| 609 | const double dirX, const double dirY,
|
---|
[2852] | 610 | Matrix<float> &specMat,
|
---|
| 611 | Matrix<bool> &flagMat,
|
---|
| 612 | vector<uInt> &tabIdvec)
|
---|
[2794] | 613 | {
|
---|
| 614 | LogIO os(LogOrigin("STSideBandSep","getSpectraToSolve()", WHERE));
|
---|
| 615 |
|
---|
| 616 | tabIdvec.resize(0);
|
---|
| 617 | specMat.resize(nchan_, nshift_);
|
---|
| 618 | Vector<float> spec;
|
---|
[2852] | 619 | Vector<bool> boolVec;
|
---|
[2794] | 620 | uInt nspec = 0;
|
---|
| 621 | STMath stm(false); // insitu has no effect for average.
|
---|
| 622 | for (uInt itab = 0 ; itab < nshift_ ; itab++) {
|
---|
| 623 | CountedPtr<Scantable> currtab_p = tableList_[itab];
|
---|
| 624 | // Selection by POLNO and BEAMNO
|
---|
| 625 | const STSelector& basesel = currtab_p->getSelection();
|
---|
| 626 | STSelector sel(basesel);
|
---|
| 627 | sel.setPolarizations(vector<int>(1, polId));
|
---|
| 628 | sel.setBeams(vector<int>(1, beamId));
|
---|
| 629 | try {
|
---|
| 630 | currtab_p->setSelection(sel);
|
---|
| 631 | } catch (...) {
|
---|
| 632 | #ifdef KS_DEBUG
|
---|
| 633 | cout << "Table " << itab << " - No spectrum found. skipping the table."
|
---|
| 634 | << endl;
|
---|
| 635 | #endif
|
---|
| 636 | continue;
|
---|
| 637 | }
|
---|
| 638 | // Selection by direction;
|
---|
| 639 | vector<int> selrow(0);
|
---|
| 640 | vector<double> currDir(2, 0.);
|
---|
| 641 | const int nselrow = currtab_p->nrow();
|
---|
| 642 | for (int irow = 0 ; irow < nselrow ; irow++) {
|
---|
| 643 | currDir = currtab_p->getDirectionVector(irow);
|
---|
| 644 | if ( (abs(currDir[0]-dirX) > xtol_) ||
|
---|
| 645 | (abs(currDir[1]-dirY) > ytol_) )
|
---|
| 646 | continue;
|
---|
| 647 | // within direction tolerance
|
---|
| 648 | selrow.push_back(irow);
|
---|
| 649 | } // end of row loop
|
---|
| 650 |
|
---|
| 651 | if (selrow.size() < 1) {
|
---|
| 652 | currtab_p->setSelection(basesel);
|
---|
| 653 |
|
---|
| 654 | #ifdef KS_DEBUG
|
---|
| 655 | cout << "Table " << itab << " - No spectrum found. skipping the table."
|
---|
| 656 | << endl;
|
---|
| 657 | #endif
|
---|
| 658 |
|
---|
| 659 | continue;
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | // At least a spectrum is selected in this table
|
---|
| 663 | CountedPtr<Scantable> seltab_p = ( new Scantable(*currtab_p, false) );
|
---|
| 664 | currtab_p->setSelection(basesel);
|
---|
| 665 | STSelector sel2(seltab_p->getSelection());
|
---|
| 666 | sel2.setRows(selrow);
|
---|
| 667 | seltab_p->setSelection(sel2);
|
---|
| 668 | CountedPtr<Scantable> avetab_p;
|
---|
| 669 | if (seltab_p->nrow() > 1) {
|
---|
[2836] | 670 | // STMath::average also merges FLAGTRA and FLAGROW
|
---|
[2794] | 671 | avetab_p = stm.average(vector< CountedPtr<Scantable> >(1, seltab_p),
|
---|
| 672 | vector<bool>(), "TINTSYS", "NONE");
|
---|
| 673 | #ifdef KS_DEBUG
|
---|
| 674 | cout << "Table " << itab << " - more than a spectrum is selected. averaging rows..."
|
---|
| 675 | << endl;
|
---|
| 676 | #endif
|
---|
| 677 | if (avetab_p->nrow() > 1)
|
---|
| 678 | throw( AipsError("Averaged table has more than a row. Somethigs went wrong.") );
|
---|
| 679 | } else {
|
---|
| 680 | avetab_p = seltab_p;
|
---|
| 681 | }
|
---|
[2836] | 682 | // Check FLAGTRA and FLAGROW if there's any valid channel in the spectrum
|
---|
| 683 | if (avetab_p->getFlagRow(0) || avetab_p->isAllChannelsFlagged(0)) {
|
---|
| 684 | #ifdef KS_DEBUG
|
---|
| 685 | cout << "Table " << itab << " - All data are flagged. skipping the table."
|
---|
| 686 | << endl;
|
---|
| 687 | #endif
|
---|
| 688 | continue;
|
---|
| 689 | }
|
---|
| 690 | // Interpolate flagged channels of the spectrum.
|
---|
| 691 | Vector<Float> tmpSpec = avetab_p->getSpectrum(0);
|
---|
[2852] | 692 | // Mask is true if the data is valid (!flag)
|
---|
[2836] | 693 | vector<bool> mask = avetab_p->getMask(0);
|
---|
| 694 | mathutil::doZeroOrderInterpolation(tmpSpec, mask);
|
---|
[2794] | 695 | spec.reference(specMat.column(nspec));
|
---|
[2836] | 696 | spec = tmpSpec;
|
---|
[2852] | 697 | boolVec.reference(flagMat.column(nspec));
|
---|
| 698 | boolVec = mask; // cast std::vector to casa::Vector
|
---|
| 699 | boolVec = !boolVec;
|
---|
[2794] | 700 | tabIdvec.push_back((uInt) itab);
|
---|
| 701 | nspec++;
|
---|
[2852] | 702 | //Liberate from reference
|
---|
| 703 | spec.unique();
|
---|
| 704 | boolVec.unique();
|
---|
[2794] | 705 | } // end of table loop
|
---|
[2836] | 706 | // Check the number of selected spectra and resize matrix.
|
---|
[2794] | 707 | if (nspec != nshift_){
|
---|
| 708 | //shiftSpecmat.resize(nchan_, nspec, true);
|
---|
[2836] | 709 | specMat.resize(nchan_, nspec, true);
|
---|
[2852] | 710 | flagMat.resize(nchan_, nspec, true);
|
---|
[2794] | 711 | #ifdef KS_DEBUG
|
---|
| 712 | cout << "Could not find corresponding rows in some tables."
|
---|
| 713 | << endl;
|
---|
| 714 | cout << "Number of spectra selected = " << nspec << endl;
|
---|
| 715 | #endif
|
---|
| 716 | }
|
---|
| 717 | if (nspec < 2) {
|
---|
| 718 | #ifdef KS_DEBUG
|
---|
| 719 | cout << "At least 2 spectra are necessary for convolution"
|
---|
| 720 | << endl;
|
---|
| 721 | #endif
|
---|
| 722 | return false;
|
---|
| 723 | }
|
---|
| 724 | return true;
|
---|
| 725 | };
|
---|
| 726 |
|
---|
[2852] | 727 |
|
---|
| 728 | Vector<bool> STSideBandSep::collapseFlag(const Matrix<bool> &flagMat,
|
---|
| 729 | const vector<uInt> &tabIdvec,
|
---|
| 730 | const bool signal)
|
---|
| 731 | {
|
---|
| 732 | LogIO os(LogOrigin("STSideBandSep","collapseFlag()", WHERE));
|
---|
| 733 | if (tabIdvec.size() == 0)
|
---|
| 734 | throw(AipsError("Internal error. Table index is not defined."));
|
---|
| 735 | if (flagMat.ncolumn() != tabIdvec.size())
|
---|
| 736 | throw(AipsError("Internal error. The row number of input matrix is not conformant."));
|
---|
| 737 | if (flagMat.nrow() != nchan_)
|
---|
| 738 | throw(AipsError("Internal error. The channel size of input matrix is not conformant."));
|
---|
| 739 |
|
---|
| 740 | const size_t nspec = tabIdvec.size();
|
---|
| 741 | vector<double> *thisShift;
|
---|
| 742 | if (signal == otherside_) {
|
---|
| 743 | // (solve signal && solveother = T) OR (solve image && solveother = F)
|
---|
| 744 | thisShift = &imgShift_;
|
---|
| 745 | } else {
|
---|
| 746 | // (solve signal && solveother = F) OR (solve image && solveother = T)
|
---|
| 747 | thisShift = &sigShift_;
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | Vector<bool> outflag(nchan_, false);
|
---|
| 751 | double tempshift;
|
---|
| 752 | Vector<bool> shiftvec(nchan_, false);
|
---|
| 753 | Vector<bool> accflag(nchan_, false);
|
---|
| 754 | uInt shiftId;
|
---|
| 755 | for (uInt i = 0 ; i < nspec; ++i) {
|
---|
| 756 | shiftId = tabIdvec[i];
|
---|
| 757 | tempshift = - thisShift->at(shiftId);
|
---|
| 758 | shiftFlag(flagMat.column(i), tempshift, shiftvec);
|
---|
| 759 | // Now accumulate Flag
|
---|
| 760 | for (uInt j = 0 ; j < nchan_ ; ++j)
|
---|
| 761 | accflag[j] |= shiftvec[j];
|
---|
| 762 | }
|
---|
[2858] | 763 | outflag = accflag;
|
---|
[2852] | 764 | // Shift back Flag
|
---|
[2858] | 765 | //cout << "Shifting FLAG back to " << thisShift->at(0) << " channels" << endl;
|
---|
| 766 | //shiftFlag(accflag, thisShift->at(0), outflag);
|
---|
[2852] | 767 |
|
---|
| 768 | return outflag;
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 |
|
---|
[2794] | 772 | vector<float> STSideBandSep::solve(const Matrix<float> &specmat,
|
---|
| 773 | const vector<uInt> &tabIdvec,
|
---|
| 774 | const bool signal)
|
---|
| 775 | {
|
---|
| 776 | LogIO os(LogOrigin("STSideBandSep","solve()", WHERE));
|
---|
| 777 | if (tabIdvec.size() == 0)
|
---|
| 778 | throw(AipsError("Internal error. Table index is not defined."));
|
---|
| 779 | if (specmat.ncolumn() != tabIdvec.size())
|
---|
| 780 | throw(AipsError("Internal error. The row number of input matrix is not conformant."));
|
---|
| 781 | if (specmat.nrow() != nchan_)
|
---|
| 782 | throw(AipsError("Internal error. The channel size of input matrix is not conformant."));
|
---|
| 783 |
|
---|
| 784 |
|
---|
| 785 | #ifdef KS_DEBUG
|
---|
| 786 | cout << "Solving " << (signal ? "SIGNAL" : "IMAGE") << " sideband."
|
---|
| 787 | << endl;
|
---|
| 788 | #endif
|
---|
| 789 |
|
---|
| 790 | const size_t nspec = tabIdvec.size();
|
---|
| 791 | vector<double> *thisShift, *otherShift;
|
---|
| 792 | if (signal == otherside_) {
|
---|
| 793 | // (solve signal && solveother = T) OR (solve image && solveother = F)
|
---|
| 794 | thisShift = &imgShift_;
|
---|
| 795 | otherShift = &sigShift_;
|
---|
| 796 | #ifdef KS_DEBUG
|
---|
| 797 | cout << "Image sideband will be deconvolved." << endl;
|
---|
| 798 | #endif
|
---|
| 799 | } else {
|
---|
| 800 | // (solve signal && solveother = F) OR (solve image && solveother = T)
|
---|
| 801 | thisShift = &sigShift_;
|
---|
[2838] | 802 | otherShift = &imgShift_;
|
---|
[2794] | 803 | #ifdef KS_DEBUG
|
---|
| 804 | cout << "Signal sideband will be deconvolved." << endl;
|
---|
| 805 | #endif
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 | vector<double> spshift(nspec);
|
---|
| 809 | Matrix<float> shiftSpecmat(nchan_, nspec, 0.);
|
---|
| 810 | double tempshift;
|
---|
| 811 | Vector<float> shiftspvec;
|
---|
[2838] | 812 | uInt shiftId;
|
---|
[2794] | 813 | for (uInt i = 0 ; i < nspec; i++) {
|
---|
[2838] | 814 | shiftId = tabIdvec[i];
|
---|
| 815 | spshift[i] = otherShift->at(shiftId) - thisShift->at(shiftId);
|
---|
| 816 | tempshift = - thisShift->at(shiftId);
|
---|
[2794] | 817 | shiftspvec.reference(shiftSpecmat.column(i));
|
---|
| 818 | shiftSpectrum(specmat.column(i), tempshift, shiftspvec);
|
---|
| 819 | }
|
---|
| 820 |
|
---|
| 821 | Matrix<float> convmat(nchan_, nspec*(nspec-1)/2, 0.);
|
---|
| 822 | vector<float> thisvec(nchan_, 0.);
|
---|
| 823 |
|
---|
| 824 | float minval, maxval;
|
---|
| 825 | minMax(minval, maxval, shiftSpecmat);
|
---|
| 826 | #ifdef KS_DEBUG
|
---|
| 827 | cout << "Max/Min of input Matrix = (max: " << maxval << ", min: " << minval << ")" << endl;
|
---|
| 828 | #endif
|
---|
| 829 |
|
---|
| 830 | #ifdef KS_DEBUG
|
---|
| 831 | cout << "starting deconvolution" << endl;
|
---|
| 832 | #endif
|
---|
| 833 | deconvolve(shiftSpecmat, spshift, rejlimit_, convmat);
|
---|
| 834 | #ifdef KS_DEBUG
|
---|
| 835 | cout << "finished deconvolution" << endl;
|
---|
| 836 | #endif
|
---|
| 837 |
|
---|
| 838 | minMax(minval, maxval, convmat);
|
---|
| 839 | #ifdef KS_DEBUG
|
---|
| 840 | cout << "Max/Min of output Matrix = (max: " << maxval << ", min: " << minval << ")" << endl;
|
---|
| 841 | #endif
|
---|
| 842 |
|
---|
| 843 | aggregateMat(convmat, thisvec);
|
---|
| 844 |
|
---|
| 845 | if (!otherside_) return thisvec;
|
---|
| 846 |
|
---|
| 847 | // subtract from the other side band.
|
---|
| 848 | vector<float> othervec(nchan_, 0.);
|
---|
| 849 | subtractFromOther(shiftSpecmat, thisvec, spshift, othervec);
|
---|
| 850 | return othervec;
|
---|
| 851 | };
|
---|
| 852 |
|
---|
| 853 |
|
---|
| 854 | void STSideBandSep::shiftSpectrum(const Vector<float> &invec,
|
---|
| 855 | double shift,
|
---|
| 856 | Vector<float> &outvec)
|
---|
| 857 | {
|
---|
| 858 | LogIO os(LogOrigin("STSideBandSep","shiftSpectrum()", WHERE));
|
---|
| 859 | if (invec.size() != nchan_)
|
---|
| 860 | throw(AipsError("Internal error. The length of input vector differs from nchan_"));
|
---|
| 861 | if (outvec.size() != nchan_)
|
---|
| 862 | throw(AipsError("Internal error. The length of output vector differs from nchan_"));
|
---|
| 863 |
|
---|
| 864 | #ifdef KS_DEBUG
|
---|
[2858] | 865 | cout << "Start shifting spectrum for " << shift << " channels" << endl;
|
---|
[2794] | 866 | #endif
|
---|
| 867 |
|
---|
| 868 | // tweak shift to be in 0 ~ nchan_-1
|
---|
| 869 | if ( fabs(shift) > nchan_ ) shift = fmod(shift, nchan_);
|
---|
| 870 | if (shift < 0.) shift += nchan_;
|
---|
| 871 | double rweight = fmod(shift, 1.);
|
---|
| 872 | if (rweight < 0.) rweight += 1.;
|
---|
| 873 | double lweight = 1. - rweight;
|
---|
| 874 | uInt lchan, rchan;
|
---|
| 875 |
|
---|
| 876 | outvec = 0;
|
---|
| 877 | for (uInt i = 0 ; i < nchan_ ; i++) {
|
---|
| 878 | lchan = uInt( floor( fmod( (i + shift), nchan_ ) ) );
|
---|
| 879 | if (lchan < 0.) lchan += nchan_;
|
---|
| 880 | rchan = ( (lchan + 1) % nchan_ );
|
---|
| 881 | outvec(lchan) += invec(i) * lweight;
|
---|
| 882 | outvec(rchan) += invec(i) * rweight;
|
---|
[2858] | 883 | #ifdef KS_DEBUG
|
---|
| 884 | if (i == 2350 || i== 2930) {
|
---|
| 885 | cout << "Channel= " << i << " of input vector: " << endl;
|
---|
| 886 | cout << "L channel = " << lchan << endl;
|
---|
| 887 | cout << "R channel = " << rchan << endl;
|
---|
| 888 | cout << "L weight = " << lweight << endl;
|
---|
| 889 | cout << "R weight = " << rweight << endl;
|
---|
| 890 | }
|
---|
| 891 | #endif
|
---|
[2794] | 892 | }
|
---|
| 893 | };
|
---|
| 894 |
|
---|
[2852] | 895 |
|
---|
| 896 | void STSideBandSep::shiftFlag(const Vector<bool> &invec,
|
---|
| 897 | double shift,
|
---|
| 898 | Vector<bool> &outvec)
|
---|
| 899 | {
|
---|
| 900 | LogIO os(LogOrigin("STSideBandSep","shiftFlag()", WHERE));
|
---|
| 901 | if (invec.size() != nchan_)
|
---|
| 902 | throw(AipsError("Internal error. The length of input vector differs from nchan_"));
|
---|
| 903 | if (outvec.size() != nchan_)
|
---|
| 904 | throw(AipsError("Internal error. The length of output vector differs from nchan_"));
|
---|
| 905 |
|
---|
| 906 | #ifdef KS_DEBUG
|
---|
| 907 | cout << "Start shifting flag for " << shift << "channels" << endl;
|
---|
| 908 | #endif
|
---|
| 909 |
|
---|
| 910 | // shift is almost integer think it as int.
|
---|
| 911 | // tolerance should be in 0 - 1
|
---|
| 912 | double tolerance = 0.01;
|
---|
| 913 | // tweak shift to be in 0 ~ nchan_-1
|
---|
| 914 | if ( fabs(shift) > nchan_ ) shift = fmod(shift, nchan_);
|
---|
| 915 | if (shift < 0.) shift += nchan_;
|
---|
| 916 | double rweight = fmod(shift, 1.);
|
---|
| 917 | bool ruse(true), luse(true);
|
---|
| 918 | if (rweight < 0.) rweight += 1.;
|
---|
| 919 | if (rweight < tolerance){
|
---|
[2858] | 920 | // the shift is almost lchan
|
---|
| 921 | ruse = false;
|
---|
| 922 | luse = true;
|
---|
| 923 | }
|
---|
| 924 | if (rweight > 1-tolerance){
|
---|
| 925 | // the shift is almost rchan
|
---|
[2852] | 926 | ruse = true;
|
---|
| 927 | luse = false;
|
---|
| 928 | }
|
---|
| 929 | uInt lchan, rchan;
|
---|
| 930 |
|
---|
| 931 | outvec = false;
|
---|
| 932 | for (uInt i = 0 ; i < nchan_ ; i++) {
|
---|
| 933 | lchan = uInt( floor( fmod( (i + shift), nchan_ ) ) );
|
---|
| 934 | if (lchan < 0.) lchan += nchan_;
|
---|
| 935 | rchan = ( (lchan + 1) % nchan_ );
|
---|
| 936 | outvec(lchan) |= (invec(i) && luse);
|
---|
| 937 | outvec(rchan) |= (invec(i) && ruse);
|
---|
[2858] | 938 | #ifdef KS_DEBUG
|
---|
| 939 | if (i == 2350 || i == 2930) {
|
---|
| 940 | cout << "Channel= " << i << " of input vector: " << endl;
|
---|
| 941 | cout << "L channel = " << lchan << endl;
|
---|
| 942 | cout << "R channel = " << rchan << endl;
|
---|
| 943 | cout << "L channel will be " << (luse ? "used" : "ignored") << endl;
|
---|
| 944 | cout << "R channel will be " << (ruse ? "used" : "ignored") << endl;
|
---|
| 945 | }
|
---|
| 946 | #endif
|
---|
[2852] | 947 | }
|
---|
| 948 | };
|
---|
| 949 |
|
---|
| 950 |
|
---|
[2794] | 951 | void STSideBandSep::deconvolve(Matrix<float> &specmat,
|
---|
| 952 | const vector<double> shiftvec,
|
---|
| 953 | const double threshold,
|
---|
| 954 | Matrix<float> &outmat)
|
---|
| 955 | {
|
---|
| 956 | LogIO os(LogOrigin("STSideBandSep","deconvolve()", WHERE));
|
---|
| 957 | if (specmat.nrow() != nchan_)
|
---|
| 958 | throw(AipsError("Internal error. The length of input matrix differs from nchan_"));
|
---|
| 959 | if (specmat.ncolumn() != shiftvec.size())
|
---|
| 960 | throw(AipsError("Internal error. The number of input shifts and spectrum differs."));
|
---|
| 961 |
|
---|
[2836] | 962 | #ifdef KS_DEBUG
|
---|
[2794] | 963 | float minval, maxval;
|
---|
[2836] | 964 | #endif
|
---|
[2794] | 965 | #ifdef KS_DEBUG
|
---|
| 966 | minMax(minval, maxval, specmat);
|
---|
| 967 | cout << "Max/Min of input Matrix = (max: " << maxval << ", min: " << minval << ")" << endl;
|
---|
| 968 | #endif
|
---|
| 969 |
|
---|
| 970 | uInt ninsp = shiftvec.size();
|
---|
| 971 | outmat.resize(nchan_, ninsp*(ninsp-1)/2, 0.);
|
---|
| 972 | Matrix<Complex> fftspmat(nchan_/2+1, ninsp, 0.);
|
---|
| 973 | Vector<float> rvecref(nchan_, 0.);
|
---|
| 974 | Vector<Complex> cvecref(nchan_/2+1, 0.);
|
---|
| 975 | uInt icol = 0;
|
---|
| 976 | unsigned int nreject = 0;
|
---|
| 977 |
|
---|
| 978 | #ifdef KS_DEBUG
|
---|
| 979 | cout << "Starting initial FFT. The number of input spectra = " << ninsp << endl;
|
---|
| 980 | cout << "out matrix has ncolumn = " << outmat.ncolumn() << endl;
|
---|
| 981 | #endif
|
---|
| 982 |
|
---|
| 983 | for (uInt isp = 0 ; isp < ninsp ; isp++) {
|
---|
| 984 | rvecref.reference( specmat.column(isp) );
|
---|
| 985 | cvecref.reference( fftspmat.column(isp) );
|
---|
| 986 |
|
---|
| 987 | #ifdef KS_DEBUG
|
---|
| 988 | minMax(minval, maxval, rvecref);
|
---|
| 989 | cout << "Max/Min of inv FFTed Matrix = (max: " << maxval << ", min: " << minval << ")" << endl;
|
---|
| 990 | #endif
|
---|
| 991 |
|
---|
| 992 | fftsf.fft0(cvecref, rvecref, true);
|
---|
| 993 |
|
---|
| 994 | #ifdef KS_DEBUG
|
---|
| 995 | double maxr=cvecref[0].real(), minr=cvecref[0].real(),
|
---|
| 996 | maxi=cvecref[0].imag(), mini=cvecref[0].imag();
|
---|
| 997 | for (uInt i = 1; i<cvecref.size();i++){
|
---|
| 998 | maxr = max(maxr, cvecref[i].real());
|
---|
| 999 | maxi = max(maxi, cvecref[i].imag());
|
---|
| 1000 | minr = min(minr, cvecref[i].real());
|
---|
| 1001 | mini = min(mini, cvecref[i].imag());
|
---|
| 1002 | }
|
---|
| 1003 | cout << "Max/Min of inv FFTed Matrix (size=" << cvecref.size() << ") = (max: " << maxr << " + " << maxi << "j , min: " << minr << " + " << mini << "j)" << endl;
|
---|
| 1004 | #endif
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | //Liberate from reference
|
---|
| 1008 | rvecref.unique();
|
---|
| 1009 |
|
---|
| 1010 | Vector<Complex> cspec(nchan_/2+1, 0.);
|
---|
| 1011 | const double PI = 6.0 * asin(0.5);
|
---|
| 1012 | const double nchani = 1. / (float) nchan_;
|
---|
| 1013 | const Complex trans(0., 1.);
|
---|
| 1014 | #ifdef KS_DEBUG
|
---|
| 1015 | cout << "starting actual deconvolution" << endl;
|
---|
| 1016 | #endif
|
---|
| 1017 | for (uInt j = 0 ; j < ninsp ; j++) {
|
---|
| 1018 | for (uInt k = j+1 ; k < ninsp ; k++) {
|
---|
| 1019 | const double dx = (shiftvec[k] - shiftvec[j]) * 2. * PI * nchani;
|
---|
| 1020 |
|
---|
| 1021 | #ifdef KS_DEBUG
|
---|
| 1022 | cout << "icol = " << icol << endl;
|
---|
| 1023 | #endif
|
---|
| 1024 |
|
---|
| 1025 | for (uInt ichan = 0 ; ichan < cspec.size() ; ichan++){
|
---|
| 1026 | cspec[ichan] = ( fftspmat(ichan, j) + fftspmat(ichan, k) )*0.5;
|
---|
| 1027 | double phase = dx*ichan;
|
---|
| 1028 | if ( fabs( sin(phase) ) > threshold){
|
---|
| 1029 | cspec[ichan] += ( fftspmat(ichan, j) - fftspmat(ichan, k) ) * 0.5
|
---|
| 1030 | * trans * sin(phase) / ( 1. - cos(phase) );
|
---|
| 1031 | } else {
|
---|
| 1032 | nreject++;
|
---|
| 1033 | }
|
---|
| 1034 | } // end of channel loop
|
---|
| 1035 |
|
---|
| 1036 | #ifdef KS_DEBUG
|
---|
| 1037 | cout << "done calculation of cspec" << endl;
|
---|
| 1038 | #endif
|
---|
| 1039 |
|
---|
| 1040 | Vector<Float> rspec;
|
---|
| 1041 | rspec.reference( outmat.column(icol) );
|
---|
| 1042 |
|
---|
| 1043 | #ifdef KS_DEBUG
|
---|
| 1044 | cout << "Starting inverse FFT. icol = " << icol << endl;
|
---|
| 1045 | //cout << "- size of complex vector = " << cspec.size() << endl;
|
---|
| 1046 | double maxr=cspec[0].real(), minr=cspec[0].real(),
|
---|
| 1047 | maxi=cspec[0].imag(), mini=cspec[0].imag();
|
---|
| 1048 | for (uInt i = 1; i<cspec.size();i++){
|
---|
| 1049 | maxr = max(maxr, cspec[i].real());
|
---|
| 1050 | maxi = max(maxi, cspec[i].imag());
|
---|
| 1051 | minr = min(minr, cspec[i].real());
|
---|
| 1052 | mini = min(mini, cspec[i].imag());
|
---|
| 1053 | }
|
---|
| 1054 | cout << "Max/Min of conv vector (size=" << cspec.size() << ") = (max: " << maxr << " + " << maxi << "j , min: " << minr << " + " << mini << "j)" << endl;
|
---|
| 1055 | #endif
|
---|
| 1056 |
|
---|
| 1057 | fftsi.fft0(rspec, cspec, false);
|
---|
| 1058 |
|
---|
| 1059 | #ifdef KS_DEBUG
|
---|
| 1060 | //cout << "- size of inversed real vector = " << rspec.size() << endl;
|
---|
| 1061 | minMax(minval, maxval, rspec);
|
---|
| 1062 | cout << "Max/Min of inv FFTed Vector (size=" << rspec.size() << ") = (max: " << maxval << ", min: " << minval << ")" << endl;
|
---|
| 1063 | //cout << "Done inverse FFT. icol = " << icol << endl;
|
---|
| 1064 | #endif
|
---|
| 1065 |
|
---|
| 1066 | icol++;
|
---|
| 1067 | }
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | #ifdef KS_DEBUG
|
---|
| 1071 | minMax(minval, maxval, outmat);
|
---|
| 1072 | cout << "Max/Min of inv FFTed Matrix = (max: " << maxval << ", min: " << minval << ")" << endl;
|
---|
| 1073 | #endif
|
---|
| 1074 |
|
---|
| 1075 | os << "Threshold = " << threshold << ", Rejected channels = " << nreject << endl;
|
---|
| 1076 | };
|
---|
| 1077 |
|
---|
| 1078 |
|
---|
| 1079 | void STSideBandSep::aggregateMat(Matrix<float> &inmat,
|
---|
| 1080 | vector<float> &outvec)
|
---|
| 1081 | {
|
---|
| 1082 | LogIO os(LogOrigin("STSideBandSep","aggregateMat()", WHERE));
|
---|
| 1083 | if (inmat.nrow() != nchan_)
|
---|
| 1084 | throw(AipsError("Internal error. The row numbers of input matrix differs from nchan_"));
|
---|
| 1085 | // if (outvec.size() != nchan_)
|
---|
| 1086 | // throw(AipsError("Internal error. The size of output vector should be equal to nchan_"));
|
---|
| 1087 |
|
---|
| 1088 | os << "Averaging " << inmat.ncolumn() << " spectra in the input matrix."
|
---|
| 1089 | << LogIO::POST;
|
---|
| 1090 |
|
---|
| 1091 | const uInt nspec = inmat.ncolumn();
|
---|
| 1092 | const double scale = 1./( (double) nspec );
|
---|
| 1093 | // initialize values with 0
|
---|
| 1094 | outvec.assign(nchan_, 0);
|
---|
| 1095 | for (uInt isp = 0 ; isp < nspec ; isp++) {
|
---|
| 1096 | for (uInt ich = 0 ; ich < nchan_ ; ich++) {
|
---|
| 1097 | outvec[ich] += inmat(ich, isp);
|
---|
| 1098 | }
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | vector<float>::iterator iter;
|
---|
| 1102 | for (iter = outvec.begin(); iter != outvec.end(); iter++){
|
---|
| 1103 | *iter *= scale;
|
---|
| 1104 | }
|
---|
| 1105 | };
|
---|
| 1106 |
|
---|
| 1107 | void STSideBandSep::subtractFromOther(const Matrix<float> &shiftmat,
|
---|
| 1108 | const vector<float> &invec,
|
---|
| 1109 | const vector<double> &shift,
|
---|
| 1110 | vector<float> &outvec)
|
---|
| 1111 | {
|
---|
| 1112 | LogIO os(LogOrigin("STSideBandSep","subtractFromOther()", WHERE));
|
---|
| 1113 | if (shiftmat.nrow() != nchan_)
|
---|
| 1114 | throw(AipsError("Internal error. The row numbers of input matrix differs from nchan_"));
|
---|
| 1115 | if (invec.size() != nchan_)
|
---|
| 1116 | throw(AipsError("Internal error. The length of input vector should be nchan_"));
|
---|
| 1117 | if (shift.size() != shiftmat.ncolumn())
|
---|
| 1118 | throw(AipsError("Internal error. The column numbers of input matrix != the number of elements in shift"));
|
---|
| 1119 |
|
---|
| 1120 | const uInt nspec = shiftmat.ncolumn();
|
---|
| 1121 | Vector<float> subsp(nchan_, 0.), shiftsub;
|
---|
| 1122 | Matrix<float> submat(nchan_, nspec, 0.);
|
---|
| 1123 | vector<float>::iterator iter;
|
---|
| 1124 | for (uInt isp = 0 ; isp < nspec ; isp++) {
|
---|
| 1125 | for (uInt ich = 0; ich < nchan_ ; ich++) {
|
---|
| 1126 | subsp(ich) = shiftmat(ich, isp) - invec[ich];
|
---|
| 1127 | }
|
---|
| 1128 | shiftsub.reference(submat.column(isp));
|
---|
| 1129 | shiftSpectrum(subsp, shift[isp], shiftsub);
|
---|
| 1130 | }
|
---|
| 1131 |
|
---|
| 1132 | aggregateMat(submat, outvec);
|
---|
| 1133 | };
|
---|
| 1134 |
|
---|
| 1135 |
|
---|
| 1136 | void STSideBandSep::setLO1(const string lo1, const string frame,
|
---|
[2726] | 1137 | const double reftime, const string refdir)
|
---|
[2707] | 1138 | {
|
---|
[2794] | 1139 | Quantum<Double> qfreq;
|
---|
| 1140 | readQuantity(qfreq, String(lo1));
|
---|
| 1141 | lo1Freq_ = qfreq.getValue("Hz");
|
---|
[2707] | 1142 | MFrequency::getType(loFrame_, frame);
|
---|
| 1143 | loTime_ = reftime;
|
---|
| 1144 | loDir_ = refdir;
|
---|
| 1145 |
|
---|
| 1146 | #ifdef KS_DEBUG
|
---|
| 1147 | cout << "STSideBandSep::setLO1" << endl;
|
---|
| 1148 | if (lo1Freq_ > 0.)
|
---|
| 1149 | cout << "lo1 = " << lo1Freq_ << " [Hz] (" << frame << ")" << endl;
|
---|
| 1150 | if (loTime_ > 0.)
|
---|
| 1151 | cout << "ref time = " << loTime_ << " [day]" << endl;
|
---|
| 1152 | if (!loDir_.empty())
|
---|
| 1153 | cout << "ref direction = " << loDir_ << " [day]" << endl;
|
---|
| 1154 | #endif
|
---|
| 1155 | };
|
---|
| 1156 |
|
---|
[2711] | 1157 | void STSideBandSep::setLO1Root(string name)
|
---|
[2707] | 1158 | {
|
---|
[2711] | 1159 | LogIO os(LogOrigin("STSideBandSep","setLO1Root()", WHERE));
|
---|
| 1160 | os << "Searching for '" << name << "'..." << LogIO::POST;
|
---|
| 1161 | // Check for existance of the file
|
---|
| 1162 | if (!checkFile(name)) {
|
---|
[2707] | 1163 | throw(AipsError("File does not exist"));
|
---|
| 1164 | }
|
---|
[2711] | 1165 | if (name[(name.size()-1)] == '/')
|
---|
| 1166 | name = name.substr(0,(name.size()-2));
|
---|
[2707] | 1167 |
|
---|
[2711] | 1168 | if (checkFile(name+"/Receiver.xml", "file") &&
|
---|
| 1169 | checkFile(name+"/SpectralWindow.xml", "file")){
|
---|
| 1170 | os << "Found '" << name << "/Receiver.xml' ... got an ASDM name." << LogIO::POST;
|
---|
| 1171 | asdmName_ = name;
|
---|
| 1172 | } else if (checkFile(name+"/ASDM_RECEIVER") &&
|
---|
| 1173 | checkFile(name+"/ASDM_SPECTRALWINDOW")){
|
---|
| 1174 | os << "Found '" << name << "/ASDM_RECEIVER' ... got a Table name." << LogIO::POST;
|
---|
| 1175 | asisName_ = name;
|
---|
| 1176 | } else {
|
---|
| 1177 | throw(AipsError("Invalid file name. Set an MS or ASDM name."));
|
---|
| 1178 | }
|
---|
[2707] | 1179 |
|
---|
| 1180 | #ifdef KS_DEBUG
|
---|
[2711] | 1181 | cout << "STSideBandSep::setLO1Root" << endl;
|
---|
| 1182 | if (!asdmName_.empty())
|
---|
| 1183 | cout << "asdm name = " << asdmName_ << endl;
|
---|
| 1184 | if (!asisName_.empty())
|
---|
| 1185 | cout << "MS name = " << asisName_ << endl;
|
---|
[2707] | 1186 | #endif
|
---|
| 1187 | };
|
---|
| 1188 |
|
---|
[2712] | 1189 |
|
---|
[2794] | 1190 | void STSideBandSep::solveImageFrequency()
|
---|
[2707] | 1191 | {
|
---|
| 1192 | LogIO os(LogOrigin("STSideBandSep","solveImageFreqency()", WHERE));
|
---|
| 1193 | os << "Start calculating frequencies of image side band" << LogIO::POST;
|
---|
| 1194 |
|
---|
| 1195 | if (imgTab_p.null())
|
---|
| 1196 | throw AipsError("STSideBandSep::solveImageFreqency - an image side band scantable should be set first");
|
---|
| 1197 |
|
---|
[2711] | 1198 | // Convert frequency REFVAL to the value in frame of LO.
|
---|
[2707] | 1199 | // The code assumes that imgTab_p has only an IF and only a FREQ_ID
|
---|
| 1200 | // is associated to an IFNO
|
---|
| 1201 | // TODO: More complete Procedure would be
|
---|
| 1202 | // 1. Get freq IDs associated to sigIfno_
|
---|
| 1203 | // 2. Get freq information of the freq IDs
|
---|
| 1204 | // 3. For each freqIDs, get freq infromation in TOPO and an LO1
|
---|
| 1205 | // frequency and calculate image band frequencies.
|
---|
| 1206 | STFrequencies freqTab_ = imgTab_p->frequencies();
|
---|
| 1207 | // get the base frame of table
|
---|
| 1208 | const MFrequency::Types tabframe = freqTab_.getFrame(true);
|
---|
| 1209 | TableVector<uInt> freqIdVec( imgTab_p->table(), "FREQ_ID" );
|
---|
| 1210 | // assuming single freqID per IFNO
|
---|
| 1211 | uInt freqid = freqIdVec(0);
|
---|
[2711] | 1212 | int nChan = imgTab_p->nchan(imgTab_p->getIF(0));
|
---|
[2707] | 1213 | double refpix, refval, increment ;
|
---|
| 1214 | freqTab_.getEntry(refpix, refval, increment, freqid);
|
---|
| 1215 | //MFrequency sigrefval = MFrequency(MVFrequency(refval),tabframe);
|
---|
| 1216 | // get freq infromation of sigIfno_ in loFrame_
|
---|
| 1217 | const MPosition mp = imgTab_p->getAntennaPosition();
|
---|
| 1218 | MEpoch me;
|
---|
| 1219 | MDirection md;
|
---|
| 1220 | if (loTime_ < 0.)
|
---|
| 1221 | me = imgTab_p->getEpoch(-1);
|
---|
| 1222 | else
|
---|
| 1223 | me = MEpoch(MVEpoch(loTime_));
|
---|
| 1224 | if (loDir_.empty()) {
|
---|
| 1225 | ArrayColumn<Double> srcdirCol_;
|
---|
| 1226 | srcdirCol_.attach(imgTab_p->table(), "SRCDIRECTION");
|
---|
| 1227 | // Assuming J2000 and SRCDIRECTION in unit of rad
|
---|
| 1228 | Quantum<Double> srcra = Quantum<Double>(srcdirCol_(0)(IPosition(1,0)), "rad");
|
---|
| 1229 | Quantum<Double> srcdec = Quantum<Double>(srcdirCol_(0)(IPosition(1,1)), "rad");
|
---|
| 1230 | md = MDirection(srcra, srcdec, MDirection::J2000);
|
---|
| 1231 | //imgTab_p->getDirection(0);
|
---|
| 1232 | } else {
|
---|
| 1233 | // parse direction string
|
---|
| 1234 | string::size_type pos0 = loDir_.find(" ");
|
---|
| 1235 |
|
---|
| 1236 | if (pos0 == string::npos) {
|
---|
| 1237 | throw AipsError("bad string format in LO1 direction");
|
---|
| 1238 | }
|
---|
| 1239 | string::size_type pos1 = loDir_.find(" ", pos0+1);
|
---|
| 1240 | String sepoch, sra, sdec;
|
---|
| 1241 | if (pos1 != string::npos) {
|
---|
| 1242 | sepoch = loDir_.substr(0, pos0);
|
---|
| 1243 | sra = loDir_.substr(pos0+1, pos1-pos0);
|
---|
| 1244 | sdec = loDir_.substr(pos1+1);
|
---|
| 1245 | }
|
---|
| 1246 | MDirection::Types epoch;
|
---|
| 1247 | MDirection::getType(epoch, sepoch);
|
---|
| 1248 | QuantumHolder qh ;
|
---|
| 1249 | String err ;
|
---|
| 1250 | qh.fromString( err, sra);
|
---|
| 1251 | Quantum<Double> ra = qh.asQuantumDouble() ;
|
---|
| 1252 | qh.fromString( err, sdec ) ;
|
---|
| 1253 | Quantum<Double> dec = qh.asQuantumDouble() ;
|
---|
[2716] | 1254 | //md = MDirection(ra.getValue("rad"), dec.getValue("rad"),epoch);
|
---|
| 1255 | md = MDirection(ra, dec, epoch);
|
---|
[2707] | 1256 | }
|
---|
| 1257 | MeasFrame mframe( me, mp, md );
|
---|
[2709] | 1258 | MFrequency::Convert tobframe(loFrame_, MFrequency::Ref(tabframe, mframe));
|
---|
| 1259 | MFrequency::Convert toloframe(tabframe, MFrequency::Ref(loFrame_, mframe));
|
---|
[2711] | 1260 | // Convert refval to loFrame_
|
---|
[2707] | 1261 | double sigrefval;
|
---|
| 1262 | if (tabframe == loFrame_)
|
---|
| 1263 | sigrefval = refval;
|
---|
| 1264 | else
|
---|
| 1265 | sigrefval = toloframe(Quantum<Double>(refval, "Hz")).get("Hz").getValue();
|
---|
| 1266 |
|
---|
[2711] | 1267 | // Check for the availability of LO1
|
---|
| 1268 | if (lo1Freq_ > 0.) {
|
---|
| 1269 | os << "Using user defined LO1 frequency." << LogIO::POST;
|
---|
| 1270 | } else if (!asisName_.empty()) {
|
---|
| 1271 | // MS name is set.
|
---|
| 1272 | os << "Using user defined MS (asis): " << asisName_ << LogIO::POST;
|
---|
| 1273 | if (!getLo1FromAsisTab(asisName_, sigrefval, refpix, increment, nChan)) {
|
---|
| 1274 | throw AipsError("Failed to get LO1 frequency from MS");
|
---|
| 1275 | }
|
---|
| 1276 | } else if (!asdmName_.empty()) {
|
---|
| 1277 | // ASDM name is set.
|
---|
| 1278 | os << "Using user defined ASDM: " << asdmName_ << LogIO::POST;
|
---|
| 1279 | if (!getLo1FromAsdm(asdmName_, sigrefval, refpix, increment, nChan)) {
|
---|
| 1280 | throw AipsError("Failed to get LO1 frequency from ASDM");
|
---|
| 1281 | }
|
---|
| 1282 | } else {
|
---|
| 1283 | // Try getting ASDM name from scantable header
|
---|
| 1284 | os << "Try getting information from scantable header" << LogIO::POST;
|
---|
[2794] | 1285 | if (!getLo1FromScanTab(tableList_[0], sigrefval, refpix, increment, nChan)) {
|
---|
[2711] | 1286 | //throw AipsError("Failed to get LO1 frequency from asis table");
|
---|
| 1287 | os << LogIO::WARN << "Failed to get LO1 frequency using information in scantable." << LogIO::POST;
|
---|
| 1288 | os << LogIO::WARN << "Could not fill frequency information of IMAGE sideband properly." << LogIO::POST;
|
---|
| 1289 | os << LogIO::WARN << "Storing values of SIGNAL sideband in FREQUENCIES table" << LogIO::POST;
|
---|
| 1290 | return;
|
---|
| 1291 | }
|
---|
| 1292 | }
|
---|
[2794] | 1293 |
|
---|
[2711] | 1294 | // LO1 should now be ready.
|
---|
| 1295 | if (lo1Freq_ < 0.)
|
---|
| 1296 | throw(AipsError("Got negative LO1 Frequency"));
|
---|
| 1297 |
|
---|
[2794] | 1298 | // Print summary
|
---|
| 1299 | {
|
---|
| 1300 | // LO1
|
---|
| 1301 | Vector<Double> dirvec = md.getAngle(Unit(String("rad"))).getValue();
|
---|
| 1302 | os << "[LO1 settings]" << LogIO::POST;
|
---|
| 1303 | os << "- Frequency: " << lo1Freq_ << " [Hz] ("
|
---|
| 1304 | << MFrequency::showType(loFrame_) << ")" << LogIO::POST;
|
---|
| 1305 | os << "- Reference time: " << me.get(Unit(String("d"))).getValue()
|
---|
| 1306 | << " [day]" << LogIO::POST;
|
---|
| 1307 | os << "- Reference direction: [" << dirvec[0] << ", " << dirvec[1]
|
---|
| 1308 | << "] (" << md.getRefString() << ") " << LogIO::POST;
|
---|
[2711] | 1309 |
|
---|
[2794] | 1310 | // signal sideband
|
---|
| 1311 | os << "[Signal side band]" << LogIO::POST;
|
---|
| 1312 | os << "- IFNO: " << imgTab_p->getIF(0) << " (FREQ_ID = " << freqid << ")"
|
---|
| 1313 | << LogIO::POST;
|
---|
| 1314 | os << "- Reference value: " << refval << " [Hz] ("
|
---|
| 1315 | << MFrequency::showType(tabframe) << ") = "
|
---|
| 1316 | << sigrefval << " [Hz] (" << MFrequency::showType(loFrame_)
|
---|
| 1317 | << ")" << LogIO::POST;
|
---|
| 1318 | os << "- Reference pixel: " << refpix << LogIO::POST;
|
---|
| 1319 | os << "- Increment: " << increment << " [Hz]" << LogIO::POST;
|
---|
| 1320 | }
|
---|
[2711] | 1321 |
|
---|
| 1322 | // Calculate image band incr and refval in loFrame_
|
---|
[2707] | 1323 | Double imgincr = -increment;
|
---|
| 1324 | Double imgrefval = 2 * lo1Freq_ - sigrefval;
|
---|
| 1325 | Double imgrefval_tab = imgrefval;
|
---|
[2711] | 1326 | // Convert imgrefval back to table base frame
|
---|
[2707] | 1327 | if (tabframe != loFrame_)
|
---|
| 1328 | imgrefval = tobframe(Quantum<Double>(imgrefval, "Hz")).get("Hz").getValue();
|
---|
[2711] | 1329 | // Set new frequencies table
|
---|
[2707] | 1330 | uInt fIDnew = freqTab_.addEntry(refpix, imgrefval, imgincr);
|
---|
[2711] | 1331 | // Update FREQ_ID in table.
|
---|
[2707] | 1332 | freqIdVec = fIDnew;
|
---|
[2711] | 1333 |
|
---|
[2794] | 1334 | // Print summary (Image sideband)
|
---|
| 1335 | {
|
---|
| 1336 | os << "[Image side band]" << LogIO::POST;
|
---|
| 1337 | os << "- IFNO: " << imgTab_p->getIF(0) << " (FREQ_ID = " << freqIdVec(0)
|
---|
| 1338 | << ")" << LogIO::POST;
|
---|
| 1339 | os << "- Reference value: " << imgrefval << " [Hz] ("
|
---|
| 1340 | << MFrequency::showType(tabframe) << ") = "
|
---|
| 1341 | << imgrefval_tab << " [Hz] (" << MFrequency::showType(loFrame_)
|
---|
| 1342 | << ")" << LogIO::POST;
|
---|
| 1343 | os << "- Reference pixel: " << refpix << LogIO::POST;
|
---|
| 1344 | os << "- Increment: " << imgincr << " [Hz]" << LogIO::POST;
|
---|
| 1345 | }
|
---|
[2707] | 1346 | };
|
---|
| 1347 |
|
---|
| 1348 | Bool STSideBandSep::checkFile(const string name, string type)
|
---|
| 1349 | {
|
---|
| 1350 | File file(name);
|
---|
| 1351 | if (!file.exists()){
|
---|
| 1352 | return false;
|
---|
| 1353 | } else if (type.empty()) {
|
---|
| 1354 | return true;
|
---|
| 1355 | } else {
|
---|
| 1356 | // Check for file type
|
---|
| 1357 | switch (tolower(type[0])) {
|
---|
| 1358 | case 'f':
|
---|
| 1359 | return file.isRegular(True);
|
---|
| 1360 | case 'd':
|
---|
| 1361 | return file.isDirectory(True);
|
---|
| 1362 | case 's':
|
---|
| 1363 | return file.isSymLink();
|
---|
| 1364 | default:
|
---|
| 1365 | throw AipsError("Invalid file type. Available types are 'file', 'directory', and 'symlink'.");
|
---|
| 1366 | }
|
---|
| 1367 | }
|
---|
| 1368 | };
|
---|
| 1369 |
|
---|
[2711] | 1370 | bool STSideBandSep::getLo1FromAsdm(const string asdmname,
|
---|
[2836] | 1371 | const double /*refval*/,
|
---|
| 1372 | const double /*refpix*/,
|
---|
| 1373 | const double /*increment*/,
|
---|
| 1374 | const int /*nChan*/)
|
---|
[2707] | 1375 | {
|
---|
| 1376 | // Check for relevant tables.
|
---|
| 1377 | string spwname = asdmname + "/SpectralWindow.xml";
|
---|
| 1378 | string recname = asdmname + "/Receiver.xml";
|
---|
| 1379 | if (!checkFile(spwname) || !checkFile(recname)) {
|
---|
| 1380 | throw(AipsError("Could not find subtables in ASDM"));
|
---|
| 1381 | }
|
---|
| 1382 |
|
---|
| 1383 | return false;
|
---|
| 1384 |
|
---|
| 1385 | };
|
---|
| 1386 |
|
---|
| 1387 |
|
---|
[2711] | 1388 | bool STSideBandSep::getLo1FromScanTab(CountedPtr< Scantable > &scantab,
|
---|
| 1389 | const double refval,
|
---|
| 1390 | const double refpix,
|
---|
| 1391 | const double increment,
|
---|
| 1392 | const int nChan)
|
---|
[2707] | 1393 | {
|
---|
[2711] | 1394 | LogIO os(LogOrigin("STSideBandSep","getLo1FromScanTab()", WHERE));
|
---|
[2707] | 1395 | // Check for relevant tables.
|
---|
[2712] | 1396 | const TableRecord &rec = scantab->table().keywordSet() ;
|
---|
[2711] | 1397 | String spwname, recname;
|
---|
[2712] | 1398 | if (rec.isDefined("ASDM_SPECTRALWINDOW") && rec.isDefined("ASDM_RECEIVER")){
|
---|
| 1399 | spwname = rec.asString("ASDM_SPECTRALWINDOW");
|
---|
| 1400 | recname = rec.asString("ASDM_RECEIVER");
|
---|
| 1401 | }
|
---|
| 1402 | else {
|
---|
[2711] | 1403 | // keywords are not there
|
---|
| 1404 | os << LogIO::WARN
|
---|
| 1405 | << "Could not find necessary table names in scantable header."
|
---|
| 1406 | << LogIO::POST;
|
---|
| 1407 | return false;
|
---|
| 1408 | }
|
---|
[2707] | 1409 | if (!checkFile(spwname,"directory") || !checkFile(recname,"directory")) {
|
---|
[2711] | 1410 | throw(AipsError("Could not find relevant subtables in MS"));
|
---|
[2707] | 1411 | }
|
---|
[2711] | 1412 | // Get root MS name
|
---|
| 1413 | string msname;
|
---|
| 1414 | const String recsuff = "/ASDM_RECEIVER";
|
---|
| 1415 | String::size_type pos;
|
---|
[2712] | 1416 | pos = recname.size()-recsuff.size();
|
---|
[2711] | 1417 | if (recname.substr(pos) == recsuff)
|
---|
[2712] | 1418 | msname = recname.substr(0, pos);
|
---|
[2711] | 1419 | else
|
---|
| 1420 | throw(AipsError("Internal error in parsing table name from a scantable keyword."));
|
---|
[2707] | 1421 |
|
---|
[2711] | 1422 | if (!checkFile(msname))
|
---|
| 1423 | throw(AipsError("Internal error in parsing MS name from a scantable keyword."));
|
---|
[2707] | 1424 |
|
---|
[2711] | 1425 | return getLo1FromAsisTab(msname, refval, refpix, increment, nChan);
|
---|
| 1426 |
|
---|
[2707] | 1427 | };
|
---|
| 1428 |
|
---|
[2711] | 1429 | bool STSideBandSep::getLo1FromAsisTab(const string msname,
|
---|
| 1430 | const double refval,
|
---|
| 1431 | const double refpix,
|
---|
| 1432 | const double increment,
|
---|
| 1433 | const int nChan)
|
---|
| 1434 | {
|
---|
| 1435 | LogIO os(LogOrigin("STSideBandSep","getLo1FromAsisTab()", WHERE));
|
---|
| 1436 | os << "Searching an LO1 frequency in '" << msname << "'" << LogIO::POST;
|
---|
| 1437 | // Check for relevant tables.
|
---|
| 1438 | const string spwname = msname + "/ASDM_SPECTRALWINDOW";
|
---|
| 1439 | const string recname = msname + "/ASDM_RECEIVER";
|
---|
| 1440 | if (!checkFile(spwname,"directory") || !checkFile(recname,"directory")) {
|
---|
| 1441 | throw(AipsError("Could not find relevant tables in MS"));
|
---|
| 1442 | }
|
---|
[2707] | 1443 |
|
---|
[2711] | 1444 | Table spwtab_ = Table(spwname);
|
---|
| 1445 | String asdmSpw;
|
---|
| 1446 | ROTableRow spwrow(spwtab_);
|
---|
| 1447 | const Double rtol = 0.01;
|
---|
| 1448 | for (uInt idx = 0; idx < spwtab_.nrow(); idx++){
|
---|
| 1449 | const TableRecord& rec = spwrow.get(idx);
|
---|
| 1450 | // Compare nchan
|
---|
| 1451 | if (rec.asInt("numChan") != (Int) nChan)
|
---|
| 1452 | continue;
|
---|
| 1453 | // Compare increment
|
---|
| 1454 | Double asdminc;
|
---|
| 1455 | Array<Double> incarr = rec.asArrayDouble("chanWidthArray");
|
---|
| 1456 | if (incarr.size() > 0)
|
---|
| 1457 | asdminc = incarr(IPosition(1, (uInt) refpix));
|
---|
| 1458 | else
|
---|
| 1459 | asdminc = rec.asDouble("chanWidth");
|
---|
| 1460 | if (abs(asdminc - abs(increment)) > rtol * abs(increment))
|
---|
| 1461 | continue;
|
---|
| 1462 | // Compare refval
|
---|
| 1463 | Double asdmrefv;
|
---|
| 1464 | Array<Double> refvarr = rec.asArrayDouble("chanFreqArray");
|
---|
| 1465 | if (refvarr.size() > 0){
|
---|
| 1466 | const uInt iref = (uInt) refpix;
|
---|
| 1467 | const Double ratio = refpix - (Double) iref;
|
---|
| 1468 | asdmrefv = refvarr(IPosition(1, iref))*(1.-ratio)
|
---|
| 1469 | + refvarr(IPosition(1,iref+1))*ratio;
|
---|
| 1470 | }
|
---|
| 1471 | else {
|
---|
| 1472 | const Double ch0 = rec.asDouble("chanFreqStart");
|
---|
| 1473 | const Double chstep = rec.asDouble("chanFreqStep");
|
---|
| 1474 | asdmrefv = ch0 + chstep * refpix;
|
---|
| 1475 | }
|
---|
| 1476 | if (abs(asdmrefv - refval) < 0.5*abs(asdminc)){
|
---|
| 1477 | asdmSpw = rec.asString("spectralWindowId");
|
---|
| 1478 | break;
|
---|
| 1479 | }
|
---|
| 1480 | }
|
---|
| 1481 |
|
---|
| 1482 | if (asdmSpw.empty()){
|
---|
| 1483 | os << LogIO::WARN << "Could not find relevant SPW ID in " << spwname << LogIO::POST;
|
---|
| 1484 | return false;
|
---|
| 1485 | }
|
---|
| 1486 | else {
|
---|
| 1487 | os << asdmSpw << " in " << spwname
|
---|
| 1488 | << " matches the freqeuncies of signal side band." << LogIO::POST;
|
---|
| 1489 | }
|
---|
| 1490 |
|
---|
| 1491 | Table rectab_ = Table(recname);
|
---|
| 1492 | ROTableRow recrow(rectab_);
|
---|
| 1493 | for (uInt idx = 0; idx < rectab_.nrow(); idx++){
|
---|
| 1494 | const TableRecord& rec = recrow.get(idx);
|
---|
| 1495 | if (rec.asString("spectralWindowId") == asdmSpw){
|
---|
| 1496 | const Array<Double> loarr = rec.asArrayDouble("freqLO");
|
---|
| 1497 | lo1Freq_ = loarr(IPosition(1,0));
|
---|
| 1498 | os << "Found LO1 Frequency in " << recname << ": "
|
---|
| 1499 | << lo1Freq_ << " [Hz]" << LogIO::POST;
|
---|
| 1500 | return true;
|
---|
| 1501 | }
|
---|
| 1502 | }
|
---|
| 1503 | os << LogIO::WARN << "Could not find " << asdmSpw << " in " << recname
|
---|
| 1504 | << LogIO::POST;
|
---|
| 1505 | return false;
|
---|
| 1506 | };
|
---|
| 1507 |
|
---|
[2707] | 1508 | } //namespace asap
|
---|