Changeset 221


Ignore:
Timestamp:
01/19/05 17:07:10 (20 years ago)
Author:
kil064
Message:

add function 'convertFlux'

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/SDMath.cc

    r209 r221  
    4242#include <casa/Arrays/MaskArrMath.h>
    4343#include <casa/Arrays/MaskArrLogi.h>
     44#include <casa/Containers/Block.h>
     45#include <casa/Quanta/QC.h>
    4446#include <casa/Utilities/Assert.h>
    4547#include <casa/Exceptions.h>
     
    5759#include <coordinates/Coordinates/CoordinateSystem.h>
    5860#include <coordinates/Coordinates/CoordinateUtil.h>
     61#include <coordinates/Coordinates/VelocityAligner.h>
    5962
    6063#include "MathUtils.h"
     
    9497                                       const Vector<Bool>& mask, Bool scanAv,
    9598                                       const std::string& weightStr)
     99//Bool alignVelocity)
    96100//
    97101// Weighted averaging of spectra from one or more Tables.
    98102//
    99103{
     104   Bool alignVelocity = False;
    100105
    101106// Convert weight type
     
    110115// Setup
    111116
    112   const uInt axis = asap::ChanAxis;                      // Spectral axis
    113117  IPosition shp = in[0]->rowAsMaskedArray(0).shape();      // Must not change
    114118  Array<Float> arr(shp);
    115119  Array<Bool> barr(shp);
    116   const Bool useMask = (mask.nelements() == shp(axis));
     120  const Bool useMask = (mask.nelements() == shp(asap::ChanAxis));
    117121
    118122// Columns from Tables
     
    149153  IPosition shp2(nAxesSub);
    150154  for (uInt i=0,j=0; i<(nAxesSub+1); i++) {
    151      if (i!=axis) {
     155     if (i!=asap::ChanAxis) {
    152156       shp2(j) = shp(i);
    153157       j++;
     
    192196  Vector<uInt> freqID, freqIDStart, oldFreqID;
    193197
     198// Velocity Aligner. We need an aligner for each Direction and FreqID
     199// combination.  I don't think there is anyway to know how many
     200// directions there are.
     201// For now, assume all Tables have the same Frequency Table
     202
     203/*
     204  {
     205     MEpoch::Ref timeRef(MEpoch::UTC);              // Should be in header
     206     MDirection::Types dirRef(MDirection::J2000);   // Should be in header
     207//
     208     SDHeader sh = in[0].getSDHeader();
     209     const uInt nChan = sh.nchan;
     210//
     211     const SDFrequencyTable freqTab = in[0]->getSDFreqTable();
     212     const uInt nFreqID = freqTab.length();
     213     PtrBlock<const VelocityAligner<Float>* > vA(nFreqID);
     214
     215// Get first time from first table
     216
     217     const Table& tabIn0 = in[0]->table();
     218     mjdCol.attach(tabIn0, "TIME");
     219     Double dTmp;
     220     mjdCol.get(0, dTmp);
     221     MVEpoch tmp2(Quantum<Double>(dTmp, Unit(String("d"))));
     222     MEpoch epoch(tmp2, timeRef);
     223//
     224     for (uInt freqID=0; freqID<nFreqID; freqID++) {
     225        SpectralCoordinate sC = in[0]->getCoordinate(freqID);
     226        vA[freqID] = new VelocityAligner<Float>(sC, nChan, epoch, const MDirection& dir,
     227                                                const MPosition& pos, const String& velUnit,
     228                                                MDoppler::Types velType, MFrequency::Types velFreqSystem)
     229     }
     230  }
     231*/
     232
    194233// Loop over tables
    195234
     
    197236  const uInt nTables = in.nelements();
    198237  for (uInt iTab=0; iTab<nTables; iTab++) {
     238
     239// Should check that the frequency tables don't change if doing VelocityAlignment
    199240
    200241// Attach columns to Table
     
    252293// Normalize data in 'sum' accumulation array according to weighting scheme
    253294
    254            normalize(sum, sumSq, nPts, wtType, axis, nAxesSub);
     295           normalize(sum, sumSq, nPts, wtType, asap::ChanAxis, nAxesSub);
    255296
    256297// Fill scan container. The source and freqID come from the
     
    275316           timeSum = 0.0;
    276317           intSum = 0.0;
    277            nPts = 0.0; // reset this too!!!
     318           nPts = 0.0;
    278319
    279320// Increment
     
    292333
    293334        accumulate(timeSum, intSum, nAccum, sum, sumSq, nPts, tSysSum,
    294                     tSys, nInc, mask, time, interval, in, iTab, iRow, axis,
     335                    tSys, nInc, mask, time, interval, in, iTab, iRow, asap::ChanAxis,
    295336                    nAxesSub, useMask, wtType);
    296337//
     
    306347//
    307348// Normalize data in 'sum' accumulation array according to weighting scheme
    308   normalize(sum, sumSq, nPts, wtType, axis, nAxesSub);
     349  normalize(sum, sumSq, nPts, wtType, asap::ChanAxis, nAxesSub);
    309350
    310351// Create and fill container.  The container we clone will be from
     
    316357  fillSDC(sc, sum.getMask(), sum.getArray(), tSysSum/nR, outScanID,
    317358           timeSum/nR, intSum, sourceNameStart, freqIDStart);
    318 //
    319359  pTabOut->putSDContainer(sc);
    320 /*
    321    cout << endl;
    322    cout << "Last accumulation for output scan ID " << outScanID << endl;
    323    cout << "   The first row in this accumulation is " << rowStart << endl;
    324    cout << "   The number of rows accumulated is " << nAccum << endl;
    325    cout << "   The first table in this accumulation is " << tableStart << endl;
    326    cout << "   The first source in this accumulation is " << sourceNameStart << endl;
    327    cout << "   The first freqID in this accumulation is " << freqIDStart << endl;
    328    cout  << "   Average time stamp = " << timeSum/nR << endl;
    329    cout << "   Integrated time = " << intSum << endl;
    330 */
     360//
    331361  return CountedPtr<SDMemTable>(pTabOut);
    332362}
     
    480510// Loop over rows and bin along channel axis
    481511 
    482   const uInt axis = asap::ChanAxis;
    483512  for (uInt i=0; i < in.nRow(); ++i) {
    484513    SDContainer sc = in.getSDContainer(i);
     
    490519    MaskedArray<Float> marr(in.rowAsMaskedArray(i));
    491520    MaskedArray<Float> marrout;
    492     LatticeUtilities::bin(marrout, marr, axis, width);
     521    LatticeUtilities::bin(marrout, marr, asap::ChanAxis, width);
    493522
    494523// Put back the binned data and flags
     
    505534//
    506535    MaskedArray<Float> tSysOut;   
    507     LatticeUtilities::bin(tSysOut, tSysIn, axis, width);
     536    LatticeUtilities::bin(tSysOut, tSysIn, asap::ChanAxis, width);
    508537    sc.putTsys(tSysOut.getArray());
    509538//
     
    766795
    767796    if (doAll) {
    768        uInt axis = 3;
     797       uInt axis = asap::ChanAxis;
    769798       VectorIterator<Float> itValues(valuesIn, axis);
    770799       VectorIterator<Bool> itMask(maskIn, axis);
     
    821850
    822851
     852SDMemTable* SDMath::convertFlux (const SDMemTable& in, Float a, Float eta, Bool doAll)
     853//
     854// As it is, this function could be implemented with 'simpleOperate'
     855// However, I anticipate that eventually we will look the conversion
     856// values up in a Table and apply them in a frequency dependent way,
     857// so I have implemented it fully here
     858//
     859{
     860  SDHeader sh = in.getSDHeader();
     861  SDMemTable* pTabOut = new SDMemTable(in, True);
     862
     863// FInd out how to convert values into Jy and K (e.g. units might be mJy or mK)
     864// Also automatically find out what we are converting to according to the
     865// flux unit
     866
     867  Unit fluxUnit(sh.fluxunit);
     868  Unit K(String("K"));
     869  Unit JY(String("Jy"));
     870//
     871  Bool toKelvin = True;
     872  Double inFac = 1.0;
     873  if (fluxUnit==JY) {
     874     cerr << "Converting to K" << endl;
     875//
     876     Quantum<Double> t(1.0,fluxUnit);
     877     Quantum<Double> t2 = t.get(JY);
     878     inFac = (t2 / t).getValue();
     879//
     880     toKelvin = True;
     881     sh.fluxunit = "K";
     882  } else if (fluxUnit==K) {
     883     cerr << "Converting to Jy" << endl;
     884//
     885     Quantum<Double> t(1.0,fluxUnit);
     886     Quantum<Double> t2 = t.get(K);
     887     inFac = (t2 / t).getValue();
     888//
     889     toKelvin = False;
     890     sh.fluxunit = "Jy";
     891  } else {
     892     throw AipsError("Unrecognized brightness units in Table - must be consistent with Jy or K");
     893  }
     894  pTabOut->putSDHeader(sh);
     895
     896// Compute conversion factor. 'a' and 'eta' are really frequency, time and 
     897// telescope dependent and should be looked// up in a table
     898
     899  Float factor = 2.0 * inFac * 1.0e-7 * 1.0e26 * QC::k.getValue(Unit(String("erg/K"))) / a / eta;
     900  if (toKelvin) {
     901    factor = 1.0 / factor;
     902  }
     903  cerr << "Applying conversion factor = " << factor << endl;
     904
     905// For operations only on specified cursor location
     906
     907  IPosition start, end;
     908  getCursorLocation(start, end, in);
     909
     910// Loop over rows and apply factor to spectra
     911 
     912  const uInt axis = asap::ChanAxis;
     913  for (uInt i=0; i < in.nRow(); ++i) {
     914
     915// Get data
     916
     917    MaskedArray<Float> dataIn(in.rowAsMaskedArray(i));
     918    Array<Float>& valuesIn = dataIn.getRWArray();              // writable reference
     919    const Array<Bool>& maskIn = dataIn.getMask(); 
     920
     921// Need to apply correct conversion factor (frequency and time dependent)
     922// which should be sourced from a Table. For now we just apply the given
     923// factor to everything
     924
     925    if (doAll) {
     926       VectorIterator<Float> itValues(valuesIn, asap::ChanAxis);
     927       while (!itValues.pastEnd()) {
     928          itValues.vector() *= factor;                            // Writes back into dataIn
     929//
     930          itValues.next();
     931       }
     932    } else {
     933       Array<Float> valuesIn2 = valuesIn(start,end);
     934       valuesIn2 *= factor;
     935       valuesIn(start,end) = valuesIn2;
     936    }
     937
     938// Write out
     939
     940    SDContainer sc = in.getSDContainer(i);
     941    putDataInSDC(sc, valuesIn, maskIn);
     942//
     943    pTabOut->putSDContainer(sc);
     944  }
     945  return pTabOut;
     946}
    823947
    824948
  • trunk/src/SDMath.h

    r183 r221  
    6565                                         const casa::Vector<casa::Bool>& mask,
    6666                                         casa::Bool scanAverage, const std::string& weightStr);
     67//                                         casa::Bool alignVelocity);
    6768
    6869// Statistics
     
    7677   SDMemTable* smooth (const SDMemTable& in, const casa::String& kernel,
    7778                       casa::Float width, casa::Bool doAll);
     79
     80// Flux conversion between Jansky and Kelvin
     81   SDMemTable* convertFlux (const SDMemTable& in, casa::Float area,
     82                            casa::Float eta, casa::Bool doAll);
    7883
    7984// Simple mathematical operations.  what=0 (mul) or 1 (add)
Note: See TracChangeset for help on using the changeset viewer.