| [2696] | 1 | //
 | 
|---|
 | 2 | // C++ Implementation: STCalTsys
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // Description:
 | 
|---|
 | 5 | //
 | 
|---|
 | 6 | //
 | 
|---|
| [2703] | 7 | // Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp> (C) 2012
 | 
|---|
| [2696] | 8 | //
 | 
|---|
 | 9 | // Copyright: See COPYING file that comes with this distribution
 | 
|---|
 | 10 | //
 | 
|---|
 | 11 | //
 | 
|---|
 | 12 | 
 | 
|---|
| [2703] | 13 | #include <vector>
 | 
|---|
 | 14 | #include "STSelector.h"
 | 
|---|
| [2696] | 15 | #include "STCalTsys.h"
 | 
|---|
| [2703] | 16 | #include "RowAccumulator.h"
 | 
|---|
 | 17 | #include "STIdxIter.h"
 | 
|---|
 | 18 | #include "STDefs.h"
 | 
|---|
 | 19 | #include <atnf/PKSIO/SrcType.h>
 | 
|---|
| [2696] | 20 | 
 | 
|---|
| [2703] | 21 | #include <casa/Arrays/ArrayMath.h>
 | 
|---|
| [2696] | 22 | 
 | 
|---|
| [2703] | 23 | using namespace std;
 | 
|---|
| [2696] | 24 | using namespace casa;
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | namespace asap {
 | 
|---|
| [2703] | 27 |   STCalTsys::STCalTsys(CountedPtr<Scantable> &s, vector<int> &iflist)
 | 
|---|
 | 28 |     : STCalibration(s),
 | 
|---|
 | 29 |       iflist_(iflist)
 | 
|---|
| [2696] | 30 | {
 | 
|---|
| [2703] | 31 |   applytable_ = new STCalTsysTable(*s);
 | 
|---|
| [2696] | 32 | }
 | 
|---|
 | 33 | 
 | 
|---|
| [2703] | 34 | void STCalTsys::calibrate()
 | 
|---|
| [2696] | 35 | {
 | 
|---|
| [2703] | 36 |   STSelector selOrg = scantable_->getSelection();
 | 
|---|
 | 37 |   STSelector sel;
 | 
|---|
 | 38 |   sel.setIFs(iflist_);
 | 
|---|
 | 39 |   scantable_->setSelection(sel);
 | 
|---|
 | 40 |   
 | 
|---|
 | 41 |   fillCalTable();
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |   scantable_->setSelection(selOrg);
 | 
|---|
| [2696] | 44 | }
 | 
|---|
 | 45 | 
 | 
|---|
| [2703] | 46 | void STCalTsys::fillCalTable()
 | 
|---|
| [2696] | 47 | {
 | 
|---|
| [2703] | 48 |   RowAccumulator acc(W_TINT);
 | 
|---|
 | 49 |   
 | 
|---|
 | 50 |   vector<string> cols(3);
 | 
|---|
 | 51 |   cols[0] = "IFNO";
 | 
|---|
 | 52 |   cols[1] = "POLNO";
 | 
|---|
 | 53 |   cols[2] = "BEAMNO";
 | 
|---|
 | 54 |   STIdxIterAcc iter(scantable_, cols);
 | 
|---|
| [2696] | 55 | 
 | 
|---|
| [2703] | 56 |   ROScalarColumn<Double> *tcol = new ROScalarColumn<Double>(scantable_->table(), "TIME");
 | 
|---|
 | 57 |   Vector<Double> timeSec = tcol->getColumn() * 86400.0;
 | 
|---|
 | 58 |   tcol->attach(scantable_->table(), "INTERVAL");
 | 
|---|
 | 59 |   Vector<Double> intervalSec = tcol->getColumn();
 | 
|---|
 | 60 |   delete tcol;
 | 
|---|
 | 61 |   ROScalarColumn<Float> *ecol = new ROScalarColumn<Float>(scantable_->table(), "ELEVATION");
 | 
|---|
 | 62 |   Vector<Float> elevation = ecol->getColumn();
 | 
|---|
 | 63 |   delete ecol;
 | 
|---|
| [2696] | 64 | 
 | 
|---|
| [2703] | 65 |   ROArrayColumn<Float> specCol(scantable_->table(), "TSYS");
 | 
|---|
 | 66 |   ROArrayColumn<uChar> flagCol(scantable_->table(), "FLAGTRA");
 | 
|---|
| [2696] | 67 | 
 | 
|---|
| [2703] | 68 |   // dummy Tsys: the following process doesn't need Tsys but RowAccumulator 
 | 
|---|
 | 69 |   //             requires to set it with spectral data
 | 
|---|
 | 70 |   Vector<Float> tsys(1, 1.0);
 | 
|---|
| [2696] | 71 | 
 | 
|---|
| [2703] | 72 |   Double timeCen = 0.0;
 | 
|---|
 | 73 |   Float elCen = 0.0;
 | 
|---|
 | 74 |   uInt count = 0;
 | 
|---|
| [2696] | 75 | 
 | 
|---|
| [2703] | 76 |   while(!iter.pastEnd()) {
 | 
|---|
 | 77 |     Vector<uInt> rows = iter.getRows(SHARE);
 | 
|---|
 | 78 |     Vector<uInt> current = iter.current();
 | 
|---|
 | 79 |     uInt len = rows.nelements();
 | 
|---|
 | 80 |     if (len == 0) {
 | 
|---|
 | 81 |       iter.next();
 | 
|---|
 | 82 |       continue;
 | 
|---|
 | 83 |     }
 | 
|---|
 | 84 |     
 | 
|---|
 | 85 |     uInt nchan = scantable_->nchan(scantable_->getIF(rows[0]));
 | 
|---|
 | 86 |     Vector<uChar> flag(nchan);
 | 
|---|
 | 87 |     Vector<Bool> bflag(nchan);
 | 
|---|
 | 88 |     Vector<Float> spec(nchan);
 | 
|---|
| [2696] | 89 | 
 | 
|---|
| [2703] | 90 |     Vector<Double> timeSep(len); 
 | 
|---|
 | 91 |     for (uInt i = 0; i < len-1; i++) {
 | 
|---|
 | 92 |       timeSep[i] = timeSec[rows[i+1]] - timeSec[rows[i]] ;
 | 
|---|
 | 93 |     }
 | 
|---|
 | 94 |     Double tMedian = median(timeSep(IPosition(1,0), IPosition(1,len-2)));
 | 
|---|
 | 95 |     timeSep[len-1] = tMedian * 10000.0 ; // any large value
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 |     uInt irow ;
 | 
|---|
 | 98 |     uInt jrow ;
 | 
|---|
 | 99 |     for (uInt i = 0; i < len; i++) {
 | 
|---|
 | 100 |       irow = rows[i];
 | 
|---|
 | 101 |       jrow = (i < len-1) ? rows[i+1] : rows[i];
 | 
|---|
 | 102 |       // accumulate data
 | 
|---|
 | 103 |       flagCol.get(irow, flag);
 | 
|---|
 | 104 |       convertArray(bflag, flag);
 | 
|---|
 | 105 |       specCol.get(irow, spec);
 | 
|---|
 | 106 |       if ( !allEQ(bflag,True) ) 
 | 
|---|
 | 107 |         acc.add( spec, !bflag, tsys, intervalSec[irow], timeSec[irow] ) ;
 | 
|---|
 | 108 |       timeCen += timeSec[irow];
 | 
|---|
 | 109 |       elCen += elevation[irow];
 | 
|---|
 | 110 |       count++;
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 |       // check time gap
 | 
|---|
 | 113 |       double gap = 2.0 * timeSep[i] / (intervalSec[jrow] + intervalSec[irow]);
 | 
|---|
 | 114 |       if ( gap > 5.0 ) {
 | 
|---|
 | 115 |         if ( acc.state() ) {
 | 
|---|
 | 116 |           acc.replaceNaN() ;
 | 
|---|
 | 117 | //           const Vector<Bool> &msk = acc.getMask();
 | 
|---|
 | 118 | //           convertArray(flag, !msk);
 | 
|---|
 | 119 | //           for (uInt k = 0; k < nchan; ++k) {
 | 
|---|
 | 120 | //             uChar userFlag = 1 << 7;
 | 
|---|
 | 121 | //             if (msk[k]==True) userFlag = 0 << 7;
 | 
|---|
 | 122 | //             flag(k) = userFlag;
 | 
|---|
 | 123 | //           }
 | 
|---|
 | 124 |           timeCen /= (Double)count * 86400.0; // sec->day
 | 
|---|
 | 125 |           elCen /= (Float)count;
 | 
|---|
 | 126 |           STCalTsysTable *p = dynamic_cast<STCalTsysTable *>(&(*applytable_));
 | 
|---|
 | 127 |           p->appenddata(0, 0, current[2], current[0], current[1],
 | 
|---|
 | 128 |                         timeCen, elCen, acc.getSpectrum());
 | 
|---|
 | 129 |         }
 | 
|---|
 | 130 |         acc.reset() ;
 | 
|---|
 | 131 |         timeCen = 0.0;
 | 
|---|
 | 132 |         elCen = 0.0;
 | 
|---|
 | 133 |         count = 0;
 | 
|---|
 | 134 |       }
 | 
|---|
 | 135 |     }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 |     iter.next() ;
 | 
|---|
 | 138 |   }
 | 
|---|
| [2696] | 139 | }
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 | }
 | 
|---|