Changeset 152 for trunk/src/SDMath.cc


Ignore:
Timestamp:
12/26/04 20:51:14 (19 years ago)
Author:
kil064
Message:

function 'add' and 'multiply' now take arg. doAll to
indicate whether operation to be applied to
all spectral or cursor selection only.

Combine functions 'localMultiply' and 'localAdd' into
'localOperate'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/SDMath.cc

    r146 r152  
    380380}
    381381
    382 
    383 
    384 void SDMath::multiplyInSitu(SDMemTable* pIn, Float factor)
    385 {
    386   SDMemTable* pOut = localMultiply (*pIn, factor);
     382void SDMath::multiplyInSitu(SDMemTable* pIn, Float factor, Bool doAll)
     383{
     384  const uInt what = 0;
     385  SDMemTable* pOut = localOperate (*pIn, factor, doAll, what);
    387386  *pIn = *pOut;
    388387   delete pOut;
     
    391390
    392391CountedPtr<SDMemTable>
    393 SDMath::multiply(const CountedPtr<SDMemTable>& in, Float factor)
    394 {
    395   return CountedPtr<SDMemTable>(localMultiply(*in,factor));
    396 }
     392SDMath::multiply(const CountedPtr<SDMemTable>& in, Float factor, Bool doAll)
     393{
     394  const uInt what = 0;
     395  return CountedPtr<SDMemTable>(localOperate (*in, factor, doAll, what));
     396}
     397
     398
     399void SDMath::addInSitu (SDMemTable* pIn, Float offset, Bool doAll)
     400{
     401  const uInt what = 1;
     402  SDMemTable* pOut = localOperate (*pIn, offset, doAll, what);
     403  *pIn = *pOut;
     404   delete pOut;
     405
     406
    397407
    398408CountedPtr<SDMemTable>
    399 SDMath::add(const CountedPtr<SDMemTable>& in, Float offset)
    400 //
    401 // Add offset to values
    402 //
    403 {
    404   SDMemTable* sdmt = new SDMemTable(*in,False);
    405   Table t = sdmt->table();
    406   ArrayColumn<Float> spec(t,"SPECTRA");
    407 
    408   for (uInt i=0; i < t.nrow(); i++) {
    409     MaskedArray<Float> marr(sdmt->rowAsMaskedArray(i));
    410     marr += offset;
    411     spec.put(i, marr.getArray());
    412   }
    413   return CountedPtr<SDMemTable>(sdmt);
     409SDMath::add(const CountedPtr<SDMemTable>& in, Float offset, Bool doAll)
     410{
     411  const uInt what = 1;
     412  return CountedPtr<SDMemTable>(localOperate(*in, offset, doAll, what));
    414413}
    415414
     
    657656// Specify cursor location
    658657
    659   uInt i = in->getBeam();
    660   uInt j = in->getIF();
    661   uInt k = in->getPol();
    662   IPosition start(4,i,j,k,0);
    663   IPosition end(4,i,j,k,in->nChan()-1);
     658  IPosition start, end;
     659  getCursorLocation (start, end, *in);
    664660
    665661// Loop over rows
     
    831827}
    832828
    833 SDMemTable* SDMath::localMultiply (const SDMemTable& in, Float factor)
    834 {
    835   SDMemTable* pOut = new SDMemTable(in,False);
    836   const Table& tOut = pOut->table();
    837   ArrayColumn<Float> spec(tOut,"SPECTRA"); 
    838 //
    839   for (uInt i=0; i < tOut.nrow(); i++) {
    840     MaskedArray<Float> marr(pOut->rowAsMaskedArray(i));
    841     marr *= factor;
    842     spec.put(i, marr.getArray());
    843   }
     829SDMemTable* SDMath::localOperate (const SDMemTable& in, Float val, Bool doAll,
     830                                  uInt what)
     831//
     832// what = 0   Multiply
     833//        1   Add
     834{
     835   SDMemTable* pOut = new SDMemTable(in,False);
     836   const Table& tOut = pOut->table();
     837   ArrayColumn<Float> spec(tOut,"SPECTRA"); 
     838//
     839   if (doAll) {
     840      for (uInt i=0; i < tOut.nrow(); i++) {
     841
     842// Get
     843
     844         MaskedArray<Float> marr(pOut->rowAsMaskedArray(i));
     845
     846// Operate
     847
     848         if (what==0) {
     849            marr *= val;
     850         } else if (what==1) {
     851            marr += val;
     852         }
     853
     854// Put
     855
     856         spec.put(i, marr.getArray());
     857      }
     858   } else {
     859
     860// Get cursor location
     861
     862      IPosition start, end;
     863      getCursorLocation (start, end, in);
     864//
     865      for (uInt i=0; i < tOut.nrow(); i++) {
     866
     867// Get
     868
     869         MaskedArray<Float> dataIn(pOut->rowAsMaskedArray(i));
     870
     871// Modify. More work than we would like to deal with the mask
     872
     873         Array<Float>& values = dataIn.getRWArray();
     874         Array<Bool> mask(dataIn.getMask());
     875//
     876         Array<Float> values2 = values(start,end);
     877         Array<Bool> mask2 = mask(start,end);
     878         MaskedArray<Float> t(values2,mask2);
     879         if (what==0) {
     880            t *= val;
     881         } else if (what==1) {
     882            t += val;
     883         }
     884         values(start, end) = t.getArray();     // Write back into 'dataIn'
     885
     886// Put
     887         spec.put(i, dataIn.getArray());
     888      }
     889   }
     890//
    844891   return pOut;
    845892}
    846893
    847894
     895
     896void SDMath::getCursorLocation (IPosition& start, IPosition& end,
     897                                const SDMemTable& in)
     898{
     899  const uInt nDim = 4;
     900  const uInt i = in.getBeam();
     901  const uInt j = in.getIF();
     902  const uInt k = in.getPol();
     903  const uInt n = in.nChan();
     904//
     905  IPosition s(nDim,i,j,k,0);
     906  IPosition e(nDim,i,j,k,n-1);
     907//
     908  start.resize(nDim);
     909  start = s;
     910  end.resize(nDim);
     911  end = e;
     912}
Note: See TracChangeset for help on using the changeset viewer.