Ignore:
Timestamp:
07/27/07 02:00:22 (17 years ago)
Author:
TakTsutsumi
Message:

merged from NRAO version of ASAP 2.1 with ALMA specific modifications

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/alma/src/STMath.cpp

    r1384 r1387  
    4848#include "STAttr.h"
    4949#include "STMath.h"
     50#include "STSelector.h"
    5051
    5152using namespace casa;
     
    536537}
    537538
     539// dototalpower (migration of GBTIDL procedure dototalpower.pro)
     540// calibrate the CAL on-off pair. It calculate Tsys and average CAL on-off subintegrations
     541// do it for each cycles in a specific scan.
     542CountedPtr< Scantable > STMath::dototalpower( const CountedPtr< Scantable >& calon,
     543                                              const CountedPtr< Scantable >& caloff, Float tcal )
     544{
     545if ( ! calon->conformant(*caloff) ) {
     546    throw(AipsError("'CAL on' and 'CAL off' scantables are not conformant."));
     547  }
     548  setInsitu(false);
     549  CountedPtr< Scantable > out = getScantable(caloff, false);
     550  Table& tout = out->table();
     551  const Table& tcon = calon->table();
     552  Vector<Float> tcalout;
     553  Vector<Float> tcalout2;  //debug
     554
     555  if ( tout.nrow() != tcon.nrow() ) {
     556    throw(AipsError("Mismatch in number of rows to form cal on - off pair."));
     557  }
     558  // iteration by scanno or cycle no.
     559  TableIterator sit(tout, "SCANNO");
     560  TableIterator s2it(tcon, "SCANNO");
     561  while ( !sit.pastEnd() ) {
     562    Table toff = sit.table();
     563    TableRow row(toff);
     564    Table t = s2it.table();
     565    ScalarColumn<Double> outintCol(toff, "INTERVAL");
     566    ArrayColumn<Float> outspecCol(toff, "SPECTRA");
     567    ArrayColumn<Float> outtsysCol(toff, "TSYS");
     568    ArrayColumn<uChar> outflagCol(toff, "FLAGTRA");
     569    ROScalarColumn<uInt> outtcalIdCol(toff, "TCAL_ID");
     570    ROScalarColumn<uInt> outpolCol(toff, "POLNO");
     571    ROScalarColumn<Double> onintCol(t, "INTERVAL");
     572    ROArrayColumn<Float> onspecCol(t, "SPECTRA");
     573    ROArrayColumn<Float> ontsysCol(t, "TSYS");
     574    ROArrayColumn<uChar> onflagCol(t, "FLAGTRA");
     575    //ROScalarColumn<uInt> ontcalIdCol(t, "TCAL_ID");
     576
     577    for (uInt i=0; i < toff.nrow(); ++i) {
     578      //skip these checks -> assumes the data order are the same between the cal on off pairs
     579      //
     580      Vector<Float> specCalon, specCaloff;
     581      // to store scalar (mean) tsys
     582      Vector<Float> tsysout(1);
     583      uInt tcalId, polno;
     584      Double offint, onint;
     585      outpolCol.get(i, polno);
     586      outspecCol.get(i, specCaloff);
     587      onspecCol.get(i, specCalon);
     588      Vector<uChar> flagCaloff, flagCalon;
     589      outflagCol.get(i, flagCaloff);
     590      onflagCol.get(i, flagCalon);
     591      outtcalIdCol.get(i, tcalId);
     592      outintCol.get(i, offint);
     593      onintCol.get(i, onint);
     594      // caluculate mean Tsys
     595      uInt nchan = specCaloff.nelements();
     596      // percentage of edge cut off
     597      uInt pc = 10;
     598      uInt bchan = nchan/pc;
     599      uInt echan = nchan-bchan;
     600
     601      Slicer chansl(IPosition(1,bchan-1), IPosition(1,echan-1), IPosition(1,1),Slicer::endIsLast);
     602      Vector<Float> testsubsp = specCaloff(chansl);
     603      MaskedArray<Float> spoff = maskedArray( specCaloff(chansl),flagCaloff(chansl) );
     604      MaskedArray<Float> spon = maskedArray( specCalon(chansl),flagCalon(chansl) );
     605      MaskedArray<Float> spdiff = spon-spoff;
     606      uInt noff = spoff.nelementsValid();
     607      //uInt non = spon.nelementsValid();
     608      uInt ndiff = spdiff.nelementsValid();
     609      Float meantsys;
     610
     611/**
     612      Double subspec, subdiff;
     613      uInt usednchan;
     614      subspec = 0;
     615      subdiff = 0;
     616      usednchan = 0;
     617      for(uInt k=(bchan-1); k<echan; k++) {
     618        subspec += specCaloff[k];
     619        subdiff += static_cast<Double>(specCalon[k]-specCaloff[k]);
     620        ++usednchan;
     621      }
     622**/
     623      // get tcal if input tcal <= 0
     624      String tcalt;
     625      Float tcalUsed;
     626      tcalUsed = tcal;
     627      if ( tcal <= 0.0 ) {
     628        caloff->tcal().getEntry(tcalt, tcalout, tcalId);
     629        if (polno<=3) {
     630          tcalUsed = tcalout[polno];
     631        }
     632        else {
     633          tcalUsed = tcalout[0];
     634        }
     635      }
     636
     637      Float meanoff;
     638      Float meandiff;
     639      if (noff && ndiff) {
     640         //Debug
     641         //if(noff!=ndiff) cerr<<"noff and ndiff is not equal"<<endl;
     642         meanoff = sum(spoff)/noff;
     643         meandiff = sum(spdiff)/ndiff;
     644         meantsys= (meanoff/meandiff )*tcalUsed + tcalUsed/2;
     645      }
     646      else {
     647         meantsys=1;
     648      }
     649
     650      tsysout[0] = Float(meantsys);
     651      MaskedArray<Float> mcaloff = maskedArray(specCaloff, flagCaloff);
     652      MaskedArray<Float> mcalon = maskedArray(specCalon, flagCalon);
     653      MaskedArray<Float> sig =   Float(0.5) * (mcaloff + mcalon);
     654      //uInt ncaloff = mcaloff.nelementsValid();
     655      //uInt ncalon = mcalon.nelementsValid();
     656
     657      outintCol.put(i, offint+onint);
     658      outspecCol.put(i, sig.getArray());
     659      outflagCol.put(i, flagsFromMA(sig));
     660      outtsysCol.put(i, tsysout);
     661    }
     662    ++sit;
     663    ++s2it;
     664  }
     665  return out;
     666}
     667
     668//dosigref - migrated from GBT IDL's dosigref.pro, do calibration of position switch
     669// observatiions.
     670// input: sig and ref scantables, and an optional boxcar smoothing width(default width=0,
     671//        no smoothing).
     672// output: resultant scantable [= (sig-ref/ref)*tsys]
     673CountedPtr< Scantable > STMath::dosigref( const CountedPtr < Scantable >& sig,
     674                                          const CountedPtr < Scantable >& ref,
     675                                          int smoothref,
     676                                          casa::Float tsysv,
     677                                          casa::Float tau )
     678{
     679if ( ! ref->conformant(*sig) ) {
     680    throw(AipsError("'sig' and 'ref' scantables are not conformant."));
     681  }
     682  setInsitu(false);
     683  CountedPtr< Scantable > out = getScantable(sig, false);
     684  CountedPtr< Scantable > smref;
     685  if ( smoothref > 1 ) {
     686    float fsmoothref = static_cast<float>(smoothref);
     687    std::string inkernel = "boxcar";
     688    smref = smooth(ref, inkernel, fsmoothref );
     689    ostringstream oss;
     690    oss<<"Applied smoothing of "<<fsmoothref<<" on the reference."<<endl;
     691    pushLog(String(oss));
     692  }
     693  else {
     694    smref = ref;
     695  }
     696  Table& tout = out->table();
     697  const Table& tref = smref->table();
     698  if ( tout.nrow() != tref.nrow() ) {
     699    throw(AipsError("Mismatch in number of rows to form on-source and reference pair."));
     700  }
     701  // iteration by scanno? or cycle no.
     702  TableIterator sit(tout, "SCANNO");
     703  TableIterator s2it(tref, "SCANNO");
     704  while ( !sit.pastEnd() ) {
     705    Table ton = sit.table();
     706    Table t = s2it.table();
     707    ScalarColumn<Double> outintCol(ton, "INTERVAL");
     708    ArrayColumn<Float> outspecCol(ton, "SPECTRA");
     709    ArrayColumn<Float> outtsysCol(ton, "TSYS");
     710    ArrayColumn<uChar> outflagCol(ton, "FLAGTRA");
     711    ArrayColumn<Float> refspecCol(t, "SPECTRA");
     712    ROScalarColumn<Double> refintCol(t, "INTERVAL");
     713    ROArrayColumn<Float> reftsysCol(t, "TSYS");
     714    ArrayColumn<uChar> refflagCol(t, "FLAGTRA");
     715    ROScalarColumn<Float> refelevCol(t, "ELEVATION");
     716    for (uInt i=0; i < ton.nrow(); ++i) {
     717
     718      Double onint, refint;
     719      Vector<Float> specon, specref;
     720      // to store scalar (mean) tsys
     721      Vector<Float> tsysref;
     722      outintCol.get(i, onint);
     723      refintCol.get(i, refint);
     724      outspecCol.get(i, specon);
     725      refspecCol.get(i, specref);
     726      Vector<uChar> flagref, flagon;
     727      outflagCol.get(i, flagon);
     728      refflagCol.get(i, flagref);
     729      reftsysCol.get(i, tsysref);
     730
     731      Float tsysrefscalar;
     732      if ( tsysv > 0.0 ) {
     733        ostringstream oss;
     734        Float elev;
     735        refelevCol.get(i, elev);
     736        oss << "user specified Tsys = " << tsysv;
     737        // do recalc elevation if EL = 0
     738        if ( elev == 0 ) {
     739          throw(AipsError("EL=0, elevation data is missing."));
     740        } else {
     741          if ( tau <= 0.0 ) {
     742            throw(AipsError("Valid tau is not supplied."));
     743          } else {
     744            tsysrefscalar = tsysv * exp(tau/elev);
     745          }
     746        }
     747        oss << ", corrected (for El) tsys= "<<tsysrefscalar;
     748        pushLog(String(oss));
     749      }
     750      else {
     751        tsysrefscalar = tsysref[0];
     752      }
     753      //get quotient spectrum
     754      MaskedArray<Float> mref = maskedArray(specref, flagref);
     755      MaskedArray<Float> mon = maskedArray(specon, flagon);
     756      MaskedArray<Float> specres =   tsysrefscalar*((mon - mref)/mref);
     757      Double resint = onint*refint*smoothref/(onint+refint*smoothref);
     758
     759      //Debug
     760      //cerr<<"Tsys used="<<tsysrefscalar<<endl;
     761      // fill the result, replay signal tsys by reference tsys
     762      outintCol.put(i, resint);
     763      outspecCol.put(i, specres.getArray());
     764      outflagCol.put(i, flagsFromMA(specres));
     765      outtsysCol.put(i, tsysref);
     766    }
     767    ++sit;
     768    ++s2it;
     769  }
     770  return out;
     771}
     772
     773CountedPtr< Scantable > STMath::donod(const casa::CountedPtr<Scantable>& s,
     774                                     const std::vector<int>& scans,
     775                                     int smoothref,
     776                                     casa::Float tsysv,
     777                                     casa::Float tau,
     778                                     casa::Float tcal )
     779
     780{
     781  setInsitu(false);
     782  STSelector sel;
     783  std::vector<int> scan1, scan2, beams;
     784  std::vector< vector<int> > scanpair;
     785  std::vector<string> calstate;
     786  String msg;
     787
     788  CountedPtr< Scantable > s1b1on, s1b1off, s1b2on, s1b2off;
     789  CountedPtr< Scantable > s2b1on, s2b1off, s2b2on, s2b2off;
     790
     791  std::vector< CountedPtr< Scantable > > sctables;
     792  sctables.push_back(s1b1on);
     793  sctables.push_back(s1b1off);
     794  sctables.push_back(s1b2on);
     795  sctables.push_back(s1b2off);
     796  sctables.push_back(s2b1on);
     797  sctables.push_back(s2b1off);
     798  sctables.push_back(s2b2on);
     799  sctables.push_back(s2b2off);
     800
     801  //check scanlist
     802  int n=s->checkScanInfo(scans);
     803  if (n==1) {
     804     throw(AipsError("Incorrect scan pairs. "));
     805  }
     806
     807  // Assume scans contain only a pair of consecutive scan numbers.
     808  // It is assumed that first beam, b1,  is on target.
     809  // There is no check if the first beam is on or not.
     810  if ( scans.size()==1 ) {
     811    scan1.push_back(scans[0]);
     812    scan2.push_back(scans[0]+1);
     813  } else if ( scans.size()==2 ) {
     814   scan1.push_back(scans[0]);
     815   scan2.push_back(scans[1]);
     816  } else {
     817    if ( scans.size()%2 == 0 ) {
     818      for (uInt i=0; i<scans.size(); i++) {
     819        if (i%2 == 0) {
     820          scan1.push_back(scans[i]);
     821        }
     822        else {
     823          scan2.push_back(scans[i]);
     824        }
     825      }
     826    } else {
     827      throw(AipsError("Odd numbers of scans, cannot form pairs."));
     828    }
     829  }
     830  scanpair.push_back(scan1);
     831  scanpair.push_back(scan2);
     832  calstate.push_back("*calon");
     833  calstate.push_back("*[^calon]");
     834  CountedPtr< Scantable > ws = getScantable(s, false);
     835  uInt l=0;
     836  while ( l < sctables.size() ) {
     837    for (uInt i=0; i < 2; i++) {
     838      for (uInt j=0; j < 2; j++) {
     839        for (uInt k=0; k < 2; k++) {
     840          sel.reset();
     841          sel.setScans(scanpair[i]);
     842          sel.setName(calstate[k]);
     843          beams.clear();
     844          beams.push_back(j);
     845          sel.setBeams(beams);
     846          ws->setSelection(sel);
     847          sctables[l]= getScantable(ws, false);
     848          l++;
     849        }
     850      }
     851    }
     852  }
     853
     854  // replace here by splitData or getData functionality
     855  CountedPtr< Scantable > sig1;
     856  CountedPtr< Scantable > ref1;
     857  CountedPtr< Scantable > sig2;
     858  CountedPtr< Scantable > ref2;
     859  CountedPtr< Scantable > calb1;
     860  CountedPtr< Scantable > calb2;
     861
     862  msg=String("Processing dototalpower for subset of the data");
     863  ostringstream oss1;
     864  oss1 << msg  << endl;
     865  pushLog(String(oss1));
     866  // Debug for IRC CS data
     867  //float tcal1=7.0;
     868  //float tcal2=4.0;
     869  sig1 = dototalpower(sctables[0], sctables[1], tcal=tcal);
     870  ref1 = dototalpower(sctables[2], sctables[3], tcal=tcal);
     871  ref2 = dototalpower(sctables[4], sctables[5], tcal=tcal);
     872  sig2 = dototalpower(sctables[6], sctables[7], tcal=tcal);
     873
     874  // correction of user-specified tsys for elevation here
     875
     876  // dosigref calibration
     877  msg=String("Processing dosigref for subset of the data");
     878  ostringstream oss2;
     879  oss2 << msg  << endl;
     880  pushLog(String(oss2));
     881  calb1=dosigref(sig1,ref2,smoothref,tsysv,tau);
     882  calb2=dosigref(sig2,ref1,smoothref,tsysv,tau);
     883
     884  // iteration by scanno or cycle no.
     885  Table& tcalb1 = calb1->table();
     886  Table& tcalb2 = calb2->table();
     887  TableIterator sit(tcalb1, "SCANNO");
     888  TableIterator s2it(tcalb2, "SCANNO");
     889  while ( !sit.pastEnd() ) {
     890    Table t1 = sit.table();
     891    Table t2= s2it.table();
     892    ArrayColumn<Float> outspecCol(t1, "SPECTRA");
     893    ArrayColumn<Float> outtsysCol(t1, "TSYS");
     894    ArrayColumn<uChar> outflagCol(t1, "FLAGTRA");
     895    ScalarColumn<Double> outintCol(t1, "INTERVAL");
     896    ArrayColumn<Float> t2specCol(t2, "SPECTRA");
     897    ROArrayColumn<Float> t2tsysCol(t2, "TSYS");
     898    ArrayColumn<uChar> t2flagCol(t2, "FLAGTRA");
     899    ROScalarColumn<Double> t2intCol(t2, "INTERVAL");
     900    for (uInt i=0; i < t1.nrow(); ++i) {
     901      Vector<Float> spec1, spec2;
     902      // to store scalar (mean) tsys
     903      Vector<Float> tsys1, tsys2;
     904      Vector<uChar> flag1, flag2;
     905      Double tint1, tint2;
     906      outspecCol.get(i, spec1);
     907      t2specCol.get(i, spec2);
     908      outflagCol.get(i, flag1);
     909      t2flagCol.get(i, flag2);
     910      outtsysCol.get(i, tsys1);
     911      t2tsysCol.get(i, tsys2);
     912      outintCol.get(i, tint1);
     913      t2intCol.get(i, tint2);
     914      // average
     915      // assume scalar tsys for weights
     916      Float wt1, wt2, tsyssq1, tsyssq2;
     917      tsyssq1 = tsys1[0]*tsys1[0];
     918      tsyssq2 = tsys2[0]*tsys2[0];
     919      wt1 = Float(tint1)/tsyssq1;
     920      wt2 = Float(tint2)/tsyssq2;
     921      Float invsumwt=1/(wt1+wt2);
     922      MaskedArray<Float> mspec1 = maskedArray(spec1, flag1);
     923      MaskedArray<Float> mspec2 = maskedArray(spec2, flag2);
     924      MaskedArray<Float> avspec =  invsumwt * (wt1*mspec1 + wt2*mspec2);
     925      //Array<Float> avtsys =  Float(0.5) * (tsys1 + tsys2);
     926      // cerr<< "Tsys1="<<tsys1<<" Tsys2="<<tsys2<<endl;
     927      tsys1[0] = sqrt(tsyssq1 + tsyssq2);
     928      Array<Float> avtsys =  tsys1;
     929
     930      outspecCol.put(i, avspec.getArray());
     931      outflagCol.put(i, flagsFromMA(avspec));
     932      outtsysCol.put(i, avtsys);
     933    }
     934    ++sit;
     935    ++s2it;
     936  }
     937  return calb1;
     938}
     939
     940//GBTIDL version of frequency switched data calibration
     941CountedPtr< Scantable > STMath::dofs( const CountedPtr< Scantable >& s,
     942                                      const std::vector<int>& scans,
     943                                      int smoothref,
     944                                      casa::Float tsysv,
     945                                      casa::Float tau,
     946                                      casa::Float tcal )
     947{
     948
     949
     950  STSelector sel;
     951  CountedPtr< Scantable > ws = getScantable(s, false);
     952  CountedPtr< Scantable > sig, sigwcal, ref, refwcal;
     953  CountedPtr< Scantable > calsig, calref, out;
     954
     955  //split the data
     956  sel.setName("*_fs");
     957  ws->setSelection(sel);
     958  sig = getScantable(ws,false);
     959  sel.reset();
     960  sel.setName("*_fs_calon");
     961  ws->setSelection(sel);
     962  sigwcal = getScantable(ws,false);
     963  sel.reset();
     964  sel.setName("*_fsr");
     965  ws->setSelection(sel);
     966  ref = getScantable(ws,false);
     967  sel.reset();
     968  sel.setName("*_fsr_calon");
     969  ws->setSelection(sel);
     970  refwcal = getScantable(ws,false);
     971
     972  calsig = dototalpower(sigwcal, sig, tcal=tcal);
     973  calref = dototalpower(refwcal, ref, tcal=tcal);
     974
     975  out=dosigref(calsig,calref,smoothref,tsysv,tau);
     976
     977  return out;
     978}
     979
    538980
    539981CountedPtr< Scantable > STMath::freqSwitch( const CountedPtr< Scantable >& in )
     
    12501692  vec = 0;
    12511693  pols->table_.rwKeywordSet().define("nPol", Int(1));
    1252   pols->table_.rwKeywordSet().define("POLTYPE", String("stokes"));
     1694  //pols->table_.rwKeywordSet().define("POLTYPE", String("stokes"));
     1695  pols->table_.rwKeywordSet().define("POLTYPE", in->getPolType());
    12531696  std::vector<CountedPtr<Scantable> > vpols;
    12541697  vpols.push_back(pols);
Note: See TracChangeset for help on using the changeset viewer.