Changeset 163


Ignore:
Timestamp:
12/27/04 16:23:10 (19 years ago)
Author:
kil064
Message:

consolidate code in 'private' functions
rerwork function 'avergae_pol' to reshape the output
to 1 pol rather than replciating in all pols

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/SDMath.cc

    r155 r163  
    7070//
    7171{
    72   weightType wtType = NONE;
    73   String tStr(weightStr);
    74   tStr.upcase();
    75   if (tStr.contains(String("NONE"))) {
    76      wtType = NONE;
    77   } else if (tStr.contains(String("VAR"))) {
    78      wtType = VAR;
    79   } else if (tStr.contains(String("TSYS"))) {
    80      wtType = TSYS;
    81      throw (AipsError("T_sys weighting not yet implemented"));
    82   } else {
    83     throw (AipsError("Unrecognized weighting type"));
    84   }
     72
     73// Convert weight type
     74 
     75  WeightType wtType = NONE;
     76  convertWeightString (wtType, weightStr);
    8577
    8678// Create output Table by cloning from the first table
     
    359351     out /= moff;
    360352     out *= tsarr;
    361      Array<Bool> outflagsb = !(mon.getMask() && moff.getMask());
    362      Array<uChar> outflags(outflagsb.shape());
    363      convertArray(outflags,outflagsb);
     353     Array<Bool> outflagsb = mon.getMask() && moff.getMask();
    364354
    365355// Fill container for this row
    366356
    367357     SDContainer sc = on->getSDContainer();
     358//
     359     putDataInSDC (sc, out, outflagsb);
    368360     sc.putTsys(tsarr);
    369361     sc.scanid = 0;
    370      sc.putSpectrum(out);
    371      sc.putFlags(outflags);
    372362
    373363// Put new row in output Table
     
    450440// Create and put back
    451441
    452     Array<uChar> outflags(barr.shape());
    453     convertArray(outflags,!barr);
    454442    SDContainer sc = in->getSDContainer(ri);
    455     sc.putSpectrum(arr);
    456     sc.putFlags(outflags);
     443    putDataInSDC (sc, arr, barr);
     444//
    457445    sdmt->putSDContainer(sc);
    458446  }
     
    461449
    462450
    463 
    464 
    465451CountedPtr<SDMemTable>
    466452SDMath::averagePol(const CountedPtr<SDMemTable>& in,
    467                    const Vector<Bool>& mask)
    468 {
     453                   const Vector<Bool>& mask)
     454//
     455// Average all polarizations together, weighted by variance
     456//
     457{
     458//   WeightType wtType = NONE;
     459//   convertWeightString (wtType, weight);
     460
    469461   const uInt nRows = in->nRow();
    470    const uInt axis = 3;                        // Spectrum
    471    const IPosition axes(2, 2, 3);              // pol-channel plane
    472 
    473 // Create output Table
    474 
    475   SDMemTable* sdmt = new SDMemTable(*in, True);
     462   const uInt polAxis = 2;                     // Polarization axis
     463   const uInt chanAxis = 3;                    // Spectrum axis
     464
     465// Create output Table and reshape number of polarizations
     466
     467  Bool clear=True;
     468  SDMemTable* pTabOut = new SDMemTable(*in, clear);
     469  SDHeader header = pTabOut->getSDHeader();
     470  header.npol = 1;
     471  pTabOut->putSDHeader(header);
     472
     473// Shape of input and output data
     474
     475  const IPosition shapeIn = in->rowAsMaskedArray(0).shape();
     476  IPosition shapeOut(shapeIn);
     477  shapeOut(polAxis) = 1;                          // Average all polarizations
     478//
     479  const uInt nChan = shapeIn(chanAxis);
     480  const IPosition vecShapeOut(4,1,1,1,nChan);     // A multi-dim form of a Vector shape
     481  IPosition start(4), end(4);
     482
     483// Output arrays
     484
     485  Array<Float> outData(shapeOut, 0.0);
     486  Array<Bool> outMask(shapeOut, True);
     487  const IPosition axes(2, 2, 3);              // pol-channel plane
     488//
     489  const Bool useMask = (mask.nelements() == shapeIn(chanAxis));
    476490
    477491// Loop over rows
     
    484498      Array<Float>& arr = marr.getRWArray();
    485499      const Array<Bool>& barr = marr.getMask();
    486 //
    487       IPosition shp = marr.shape();
    488       const Bool useMask = (mask.nelements() == shp(axis));
    489       const uInt nChan = shp(axis);
    490500
    491501// Make iterators to iterate by pol-channel planes
    492502
    493      ArrayIterator<Float> itDataPlane(arr, axes);
    494      ReadOnlyArrayIterator<Bool> itMaskPlane(barr, axes);
     503      ReadOnlyArrayIterator<Float> itDataPlane(arr, axes);
     504      ReadOnlyArrayIterator<Bool> itMaskPlane(barr, axes);
    495505
    496506// Accumulations
    497507
    498      Float fac = 0.0;
    499      Vector<Float> vecSum(nChan,0.0);
    500 
    501 // Iterate by plane
    502 
    503      while (!itDataPlane.pastEnd()) {
    504 
    505 // Iterate through pol-channel plane by spectrum
     508      Float fac = 1.0;
     509      Vector<Float> vecSum(nChan,0.0);
     510
     511// Iterate through data by pol-channel planes
     512
     513      while (!itDataPlane.pastEnd()) {
     514
     515// Iterate through plane by polarization  and accumulate Vectors
    506516
    507517        Vector<Float> t1(nChan); t1 = 0.0;
     
    541551        vecSum /= varSum;
    542552
    543 // We have formed the weighted averaged spectrum from all polarizations
    544 // for this beam and IF.  Now replicate the spectrum to all polarizations
    545 
    546         {
    547            VectorIterator<Float> itDataVec(itDataPlane.array(), 1);  // Writes back into 'arr'
    548            const Vector<Float>& vecSumData = vecSum.getArray();      // It *is* a Vector
    549 //
    550            while (!itDataVec.pastEnd()) {     
    551               itDataVec.vector() = vecSumData;
    552               itDataVec.next();
    553            }
    554         }
     553// FInd position in input data array.  We are iterating by pol-channel
     554// plane so all that will change is beam and IF and that's what we want.
     555
     556        IPosition pos = itDataPlane.pos();
     557
     558// Write out data. This is a bit messy. We have to reform the Vector
     559// accumulator into an Array of shape (1,1,1,nChan)
     560
     561        start = pos;
     562        end = pos;
     563        end(chanAxis) = nChan-1;
     564        outData(start,end) = vecSum.getArray().reform(vecShapeOut);
     565        outMask(start,end) = vecSum.getMask().reform(vecShapeOut);
    555566
    556567// Step to next beam/IF combination
     
    558569        itDataPlane.next();
    559570        itMaskPlane.next();
    560      }
     571      }
    561572
    562573// Generate output container and write it to output table
    563574
    564      SDContainer sc = in->getSDContainer();
    565      Array<uChar> outflags(barr.shape());
    566      convertArray(outflags,!barr);
    567      sc.putSpectrum(arr);
    568      sc.putFlags(outflags);
    569      sdmt->putSDContainer(sc);
     575      SDContainer sc = in->getSDContainer();
     576      sc.resize(shapeOut);
     577//
     578      putDataInSDC (sc, outData, outMask);
     579      pTabOut->putSDContainer(sc);
    570580   }
    571581//
    572   return CountedPtr<SDMemTable>(sdmt);
     582  return CountedPtr<SDMemTable>(pTabOut);
    573583}
    574584
     
    620630    IPosition ip2 = marrout.shape();
    621631    sc.resize(ip2);
    622     sc.putSpectrum(marrout.getArray());
    623 //
    624     Array<uChar> outflags(ip2);
    625     convertArray(outflags,!(marrout.getMask()));
    626     sc.putFlags(outflags);
     632//
     633    putDataInSDC (sc, marrout.getArray(), marrout.getMask());
    627634
    628635// Bin up Tsys. 
     
    634641    LatticeUtilities::bin(tSysOut, tSysIn, axis, width);
    635642    sc.putTsys(tSysOut.getArray());
     643//
    636644    sdmt->putSDContainer(sc);
    637645  }
     
    703711                      const Vector<uInt>& freqID)
    704712{
    705   sc.putSpectrum(data);
    706 //
    707   Array<uChar> outflags(mask.shape());
    708   convertArray(outflags,!mask);
    709   sc.putFlags(outflags);
    710 //
     713// Data and mask
     714
     715  putDataInSDC (sc, data, mask);
     716
     717// TSys
     718
    711719  sc.putTsys(tSys);
    712720
     
    724732                        const Array<Float>& sumSq,
    725733                        const Array<Float>& nPts,
    726                         weightType wtType, Int axis,
     734                        WeightType wtType, Int axis,
    727735                        Int nAxesSub)
    728736{
     
    762770                         uInt iTab, uInt iRow, uInt axis,
    763771                         uInt nAxesSub, Bool useMask,
    764                          weightType wtType)
     772                         WeightType wtType)
    765773{
    766774
     
    902910  const uInt n = in.nChan();
    903911//
    904   IPosition s(nDim,i,j,k,0);
    905   IPosition e(nDim,i,j,k,n-1);
    906 //
    907912  start.resize(nDim);
    908   start = s;
     913  start(0) = i;
     914  start(1) = j;
     915  start(2) = k;
     916  start(3) = 0;
     917//
    909918  end.resize(nDim);
    910   end = e;
    911 }
     919  end(0) = i;
     920  end(1) = j;
     921  end(2) = k;
     922  end(3) = n-1;
     923}
     924
     925
     926void SDMath::convertWeightString (WeightType& wtType, const std::string& weightStr)
     927{
     928  String tStr(weightStr);
     929  tStr.upcase();
     930  if (tStr.contains(String("NONE"))) {
     931     wtType = NONE;
     932  } else if (tStr.contains(String("VAR"))) {
     933     wtType = VAR;
     934  } else if (tStr.contains(String("TSYS"))) {
     935     wtType = TSYS;
     936     throw (AipsError("T_sys weighting not yet implemented"));
     937  } else {
     938    throw (AipsError("Unrecognized weighting type"));
     939  }
     940}
     941
     942void SDMath::putDataInSDC (SDContainer& sc, const Array<Float>& data,
     943                           const Array<Bool>& mask)
     944{
     945    sc.putSpectrum(data);
     946//
     947    Array<uChar> outflags(data.shape());
     948    convertArray(outflags,!mask);
     949    sc.putFlags(outflags);
     950}
Note: See TracChangeset for help on using the changeset viewer.