casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
VisCal.h
Go to the documentation of this file.
00001 //# VisCal.h: Definitions of interface for VisCal 
00002 //# Copyright (C) 1996,1997,2000,2001,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be adressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: VisCal.h,v 1.10 2006/02/06 19:23:11 gmoellen Exp $
00028 
00029 #ifndef SYNTHESIS_VISCAL_H
00030 #define SYNTHESIS_VISCAL_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Containers/Record.h>
00034 #include <casa/BasicSL/Complex.h>
00035 #include <casa/BasicSL/Constants.h>
00036 #include <synthesis/MeasurementComponents/Mueller.h>
00037 #include <synthesis/MeasurementComponents/Jones.h>
00038 #include <synthesis/CalTables/VisCalEnum.h>
00039 #include <synthesis/MeasurementComponents/VisVector.h>
00040 #include <synthesis/MSVis/VisSet.h>
00041 
00042 #include <synthesis/MSVis/VisBuffGroupAcc.h>
00043 
00044 namespace casa { //# NAMESPACE CASA - BEGIN
00045 
00046 
00047 // **********************************************************
00048 //  VisCal
00049 //
00050 
00051 class VisCal {
00052 
00053   friend class SolvableVisJones;
00054 
00055 public:
00056  
00057   // Allowed types of VisCal matrices - 'correct' order
00058   //  enum Type{UVMOD,Mf,M,K,B,G,D,C,E,P,T,EP,F};
00059   enum Type{Test=0,ANoise,M,KAntPos,K,B,G,J,D,X,C,P,E,T,F,A,ALL};
00060 
00061   // Enumeration of parameter types (Complex, Real, or Both)
00062   enum ParType{Co,Re,CoRe};
00063 
00064   static String nameOfType(Type type) {
00065     switch (type) {
00066     case ANoise: return "ANoise";
00067     case M: return "M";
00068     case K: return "K";
00069     case B: return "B";
00070     case J: return "J";
00071     case D: return "D";
00072     case X: return "X";
00073     case C: return "C";
00074     case P: return "P";
00075     case E: return "E";
00076     case T: return "T";
00077     case F: return "F";
00078     case A: return "A";
00079     default: return "0";
00080     }
00081   }
00082 
00083   VisCal(VisSet& vs);
00084   
00085   VisCal(const Int& nAnt);
00086   
00087   virtual ~VisCal();
00088 
00089   // Return the type of this calibration matrix (actual type of derived class).
00090   //    (Must be implemented in specializations!)
00091   virtual Type type()=0;
00092 
00093   // Return type name as string
00094   virtual String typeName()     { return "Unknown VisCal"; };
00095   virtual String longTypeName() { return "Unknown VisCal"; };
00096 
00097   // Return Matrix type
00098   virtual VisCalEnum::MatrixType matrixType() { return VisCalEnum::GLOBAL; };
00099 
00100   // Return the parameter type (nominally complex)
00101   virtual VisCalEnum::VCParType parType() { return VisCalEnum::COMPLEX; };
00102 
00103   // Number of pars per ant/bln
00104   //    (Must be implemented in specializations!)
00105   virtual Int nPar()=0;
00106 
00107   // Report calibration availability per spw
00108   //  (always True for non-tabular?)
00109   virtual Vector<Bool> spwOK() { return Vector<Bool>(nSpw(),True); };
00110   virtual Bool spwOK(Int) { return True; };
00111 
00112   // Frequency-dependent Parameters?  Nominally not.
00113   virtual Bool freqDepPar() { return False; };
00114 
00115   // Number of par channels in current spw
00116   inline const Int& nChanPar() const { return nChanPar_[currSpw_]; };
00117   
00118   // Frequency-dependent Matrices?  Nominally same as freqDepPar.
00119   virtual Bool freqDepMat() { return freqDepPar(); };
00120 
00121   // Matrices time-dependent per parameter set (nominally no)
00122   virtual Bool timeDepMat() { return False; };
00123 
00124   // Is this calibration matrix to be applied?
00125   inline Bool isApplied() {return applied_;};
00126 
00127   // Is this solveable? (never via this interface)
00128   virtual Bool isSolvable() {return False;};
00129 
00130   // Return the time interval over which this calibration is constant
00131   inline virtual Double& interval() {return interval_;}
00132 
00133   // Set the application parameters
00134   virtual void setApply();
00135   virtual void setApply(const Record& apply);
00136 
00137   // Apply info/params, suitable for logging
00138   virtual String applyinfo();
00139 
00140   // simulation params - for a VC, sim is apply; for a SVC this will get 
00141   // overriden
00142   inline virtual String siminfo() { return applyinfo(); };
00143 
00144   // Trigger calibration of weights
00145   inline Bool& calWt() { return calWt_; };
00146 
00147   // Apply calibration to data in VisBuffer (correct Data or corrupt Model)
00148   //  (in-place versions)
00149   virtual void correct(VisBuffer& vb, Bool trial=False);
00150   //  virtual void corrupt(VisBuffer& vb);
00151   virtual void corrupt(VisBuffer& vb);
00152   // Apply calibration to data in VisBuffer; 
00153   //  (alternate output versions)
00154   virtual void correct(VisBuffer& vb, Cube<Complex>& Vout,Bool trial=False);
00155   //  virtual void corrupt(VisBuffer& vb, Cube<Complex>& Mout);
00156   virtual void corrupt(VisBuffer& vb, Cube<Complex>& Mout);
00157 
00158   // Flag counting
00159   virtual void initCalFlagCount();
00160   virtual Record actionRec();
00161 
00162   // Report the state
00163   virtual void state();
00164 
00165   virtual void currMetaNote();
00166 
00167   // Set the print level
00168   inline void setPrtlev(const Int& prtlev) { prtlev_=prtlev; };
00169 
00170   // Baseline index from antenna indices: (assumes a1<=a2 !!)
00171   inline Int blnidx(const Int& a1, 
00172                     const Int& a2) { return  a1*nAnt() - a1*(a1+1)/2 + a2; };
00173 
00174   inline String& extraTag() { return extratag_; };
00175  
00176 protected:
00177 
00178   // Set applied state flag
00179   inline void setApplied(const Bool& flag) {applied_=flag;};
00180 
00181   inline String& msName() { return msName_; };
00182 
00183   // General Shape Info 
00184   inline Int& nSpw() { return nSpw_; };
00185   inline Int& nAnt() { return nAnt_; };
00186   inline Int& nBln() { return nBln_; };
00187 
00188   // The number of sets of parameters under consideration
00189   virtual Int& nElem()=0;
00190 
00191   // Number of Calibration matrices on ant/bln axis
00192   virtual Int nCalMat()=0;
00193 
00194   // Access to current vb
00195   inline const VisBuffer& vb() { return *vb_; };
00196 
00197   // Current in-focus spw
00198   inline Int& currSpw() { return currSpw_; };
00199 
00200   // Current coords
00201   inline Double& lastTime()                { return lastTime_(currSpw()); };
00202   inline Double& currTime()                { return currTime_(currSpw()); };
00203   inline Int&    currScan()                { return currScan_(currSpw()); };
00204   inline Int&    currObs()                 { return currObs_(currSpw()); };
00205   inline Int&    currField()               { return currField_(currSpw()); };
00206   inline Vector<Double>& currFreq()        { return currFreq_; };
00207 
00208   inline Double& refTime()                 { return refTime_; };
00209   inline Double& refFreq()                 { return refFreq_; };
00210 
00211   // Current spectral shapes
00212   inline Int& nChanPar()              { return nChanPar_[currSpw_]; };
00213   inline Int& nChanMat()              { return nChanMat_[currSpw_]; };
00214   inline Int& startChan()             { return startChan_[currSpw_];};
00215   inline Vector<Int>& nChanParList()  { return nChanPar_; };
00216   inline Vector<Int>& nChanMatList()  { return nChanMat_; };
00217   inline Vector<Int>& startChanList() { return startChan_;};
00218 
00219   // Access to matrix renderings of Visibilities
00220   inline VisVector& V() { return (*V_[currSpw()]); };
00221 
00222   // Access to current solution parameters and matrices
00223   inline virtual Cube<Complex>& currCPar()  {return (*currCPar_[currSpw()]);};
00224   inline virtual Cube<Float>&   currRPar()  {return (*currRPar_[currSpw()]);};
00225   inline virtual Cube<Bool>&    currParOK() {return (*currParOK_[currSpw()]);};
00226 
00227   // Validation of calibration parameters
00228   inline void invalidateP() {PValid_(currSpw())=False;};
00229   inline void validateP()   {PValid_(currSpw())=True;};
00230   inline Bool PValid()      {return PValid_(currSpw());};
00231 
00232   // Invalidate cal matrices generically 
00233   virtual void invalidateCalMat()=0;
00234 
00235   // Access to weight-scaling factors
00236   inline Matrix<Float>& currWtScale() { return (*currWtScale_[currSpw()]); };
00237 
00238   // Flag counting
00239   virtual void countInFlag(const VisBuffer& vb);
00240   virtual void countOutFlag(const VisBuffer& vb);
00241 
00242   // Row-by-row apply to a Cube<Complex> (generic)
00243   virtual void applyCal(VisBuffer& vb, Cube<Complex>& Vout,Bool trial=False)=0;
00244 
00245   // Synchronize "gains" with a VisBuffer or another VisCal
00246   virtual void syncCal(const VisBuffer& vb,
00247                        const Bool& doInv=False);
00248   virtual void syncCal(VisCal& vc);
00249 
00250   // Set internal meta data from a VisBuffer or another VisCal
00251   void syncMeta(const VisBuffer& vb);
00252   void syncMeta(VisCal& vc);
00253 
00254   void syncMeta(const Int& spw,
00255                 const Double& time,
00256                 const Int& field,
00257                 const Vector<Double>& freq,
00258                 const Int& nchan);
00259 
00260   // Set the calibration matrix channelization
00261   void setCalChannelization(const Int& nChanDat);
00262 
00263   // Test for need of new calibration
00264   void checkCurrCal();
00265 
00266   // Synchronize "gains" with current meta-data
00267   virtual void syncCal(const Bool& doInv=False);
00268 
00269   // Sync parameters for current meta data
00270   virtual void syncPar();
00271 
00272   // Calculate Mueller parameters by some means 
00273   virtual void calcPar();
00274 
00275   // Sync matrices generically for current meta data 
00276   virtual void syncCalMat(const Bool& doInv=False)=0;
00277 
00278   // Return print (cout) level
00279   inline Int& prtlev() { return prtlev_; };
00280 
00281   // set current field index vector to given field id
00282   void setCurrField(const Int& ifld);
00283 
00284 private:
00285 
00286   // Defalt ctor is private
00287   VisCal();
00288 
00289   // Initialize pointers, etc.
00290   void initVisCal();
00291 
00292   // Delete pointers
00293   void deleteVisCal();
00294 
00295   // Associated MS name
00296   String msName_;
00297   
00298   // Number of Spectral windows
00299   Int nSpw_;
00300 
00301   // Number of antennas
00302   Int nAnt_;
00303 
00304   // Number of baselines
00305   Int nBln_;
00306 
00307   // Pointer to the current vb
00308   const VisBuffer* vb_;
00309 
00310   // Current synchronized spw
00311   Int currSpw_;
00312 
00313   // Current indices
00314   Vector<Double> currTime_;
00315   Vector<Int> currScan_;
00316   Vector<Int> currObs_;
00317   Vector<Int> currField_;
00318   Vector<Double> currFreq_;
00319   Vector<Double> lastTime_;
00320   Double refTime_;
00321   Double refFreq_;
00322 
00323   // Channel counts
00324   Vector<Int> nChanPar_, nChanMat_;
00325   Vector<Int> startChan_;
00326 
00327   // Solution timescale (context-dependent)
00328   Double interval_;
00329 
00330   // Application flag
00331   Bool applied_;
00332 
00333   // In-focus channel for single-chan solves on multi-chan data
00334   Int focusChan_;
00335 
00336   // VisVector wrapper (per Spw)
00337   PtrBlock<VisVector*> V_;
00338 
00339   // Current parameters
00340   PtrBlock<Cube<Complex>*>  currCPar_;   // [nSpw](nPar,nChanPar,nElm)
00341   PtrBlock<Cube<Float>*>    currRPar_;   // [nSpw](nPar,nChanPar,nElm)
00342   PtrBlock<Cube<Bool>*>     currParOK_;  // [nSpw](nPar,nChanPar,nElm)
00343 
00344   // Paremeter validity
00345   Vector<Bool> PValid_;
00346 
00347   // Trigger calibration of weights
00348   Bool calWt_;
00349 
00350   // Weight scale factors
00351   PtrBlock<Matrix<Float>*> currWtScale_;  // [nSpw](nPar,nElm)
00352 
00353   // Flag counting
00354   Int64 ndataIn_, nflagIn_, nflagOut_;
00355 
00356   // Print level
00357   Int prtlev_;
00358 
00359   String extratag_;  // e.g. to tag as noise scale
00360 
00361 
00362 };
00363 
00364 
00365 // **********************************************************
00366 //  VisMueller
00367 //
00368 
00369 class VisMueller : virtual public VisCal {
00370 
00371 public:
00372 
00373   // Constructor
00374   VisMueller(VisSet& vs);
00375 
00376   VisMueller(const Int& nAnt);
00377 
00378   virtual ~VisMueller();
00379 
00380   // Return Matrix type
00381   virtual VisCalEnum::MatrixType matrixType() { return VisCalEnum::MUELLER; };
00382 
00383   // Mueller matrix type (must be implemented in Mueller specializations!)
00384   virtual Mueller::MuellerType muellerType()=0;  
00385 
00386   // Report the state
00387   virtual void state();
00388 
00389 protected:
00390 
00391   // Total number of parameter sets required
00392   virtual Int& nElem() { return nBln(); };
00393 
00394   // Number of Cal Matrices to form on baseline axis
00395   //  (Mueller, apply context: nBln())
00396   virtual Int nCalMat() { return nBln(); };
00397 
00398   // Are the parameters the matrix elements? 
00399   //   (or is a non-trivial calculation required?)
00400   //    (Must be implemented in specializations!)
00401   virtual Bool trivialMuellerElem()=0;
00402 
00403   // Are we applying via Mueller multiplication?
00404   //   (necessarily True for native Muellers)
00405   virtual Bool applyByMueller() { return True; };
00406 
00407   // Access to matrix renderings of Muellers
00408   inline Mueller& M()   { return (*M_[currSpw()]); };
00409 
00410   // Access to current matrices
00411   inline Cube<Complex>& currMElem()   {return (*currMElem_[currSpw()]);};
00412   inline Cube<Bool>&    currMElemOK() {return (*currMElemOK_[currSpw()]);};
00413 
00414   // Invalidate cal matrices generically (at this level, just Mueller)
00415   inline virtual void invalidateCalMat() { invalidateM(); };
00416 
00417   // Validation of Mueller matrices (for currSpw)
00418   inline void invalidateM() {MValid_(currSpw())=False;};
00419   inline void validateM()   {MValid_(currSpw())=True;};
00420   inline Bool MValid()      {return MValid_(currSpw());};
00421 
00422   // Row-by-row apply to a Cube<Complex> (applyByMueller override)
00423   virtual void applyCal(VisBuffer& vb, Cube<Complex>& Vout,Bool trial=False);
00424 
00425   // Sync matrices for current meta data (Mueller override)
00426   virtual void syncCalMat(const Bool& doInv=False);
00427 
00428   // Sync Mueller matrix elements for current parameters
00429   virtual void syncMueller(const Bool& doInv=False);
00430 
00431   // Calculate an ensemble of Mueller matrices (all baselines, channels)
00432   virtual void calcAllMueller();
00433 
00434   // Calculate a single Mueller matrix by some means
00435   virtual void calcOneMueller(Vector<Complex>& mat, Vector<Bool>& mOk,
00436                               const Vector<Complex>& par, const Vector<Bool>& pOk);
00437 
00438   // Invert Mueller matrices
00439   virtual void invMueller();
00440 
00441   // Set matrix elements according to their ok flags
00442   //  (e.g., makes a unit matrix if everything flagged,
00443   //   so we don't have to make atomic ok checks in apply)
00444   virtual void setMatByOk();
00445 
00446   // Create Mueller matrix algebra interface
00447   void createMueller();
00448  
00449   // Synchronize weight scale factors
00450   virtual void syncWtScale();
00451 
00452   // Update the wt vector for a baseline
00453   virtual void updateWt(Vector<Float>& wt,const Int& a1,const Int& a2);
00454 
00455 
00456 private:
00457 
00458   // Default ctor is private
00459   VisMueller();
00460 
00461   // Init VisMueller pointers, etc.
00462   void initVisMueller();
00463 
00464   // Delete the this* caches
00465   void deleteVisMueller();
00466 
00467   // Mueller algebra wrapper (per Spw)
00468   PtrBlock<Mueller*> M_;                
00469 
00470   // Current Mueller matrix elements
00471   PtrBlock<Cube<Complex>*> currMElem_;    // [nSpw]([1,2,4,16],nChanMat,nBln)
00472   PtrBlock<Cube<Bool>*>    currMElemOK_;  // [nSpw]([1,2,4,16],nChanMat,nBln)
00473 
00474   // Mueller validity
00475   Vector<Bool> MValid_;
00476 
00477 };
00478 
00479 
00480 // **********************************************************
00481 //  VisJones
00482 //
00483 
00484 class VisJones : virtual public VisMueller {
00485 
00486   friend class SolvableVisJones;
00487 
00488 public:
00489 
00490   // Constructor
00491   VisJones(VisSet& vs);
00492 
00493   VisJones(const Int& nAnt);
00494 
00495   virtual ~VisJones();
00496 
00497   // Return Matrix type
00498   virtual VisCalEnum::MatrixType matrixType() { return VisCalEnum::JONES; };
00499 
00500   // What kind of Mueller matrices should we use?  
00501   //   (A function of the jonesType and target data shape)
00502   virtual Mueller::MuellerType muellerType();
00503 
00504   // What kind of Jones matrix are we?  
00505   //    (Must be implemented in specializations!)
00506   virtual Jones::JonesType jonesType()=0; 
00507 
00508   // Report the state
00509   virtual void state();
00510 
00511 protected:
00512 
00513   // Number of parameter sets is number of antennas
00514   inline virtual Int& nElem() { return nAnt(); };
00515 
00516   // Number of Cal Matrices to form on antenna axis
00517   //  (Jones, apply context: nAnt())
00518   virtual Int nCalMat() { return nAnt(); };
00519 
00520   // Jones matrices can never be trivial Muellers!
00521   virtual Bool trivialMuellerElem() { return False; };
00522 
00523   // Are the parameters the Jones matrix elements?
00524   //   (or is a non-trivial calculation required?)
00525   //    (Must be implemented in specializations!)
00526   virtual Bool trivialJonesElem()=0;
00527 
00528   // Are we applying via Mueller or Jones multiplication?
00529   //   (probably by Jones for native Jones?)
00530   virtual Bool applyByMueller() { return False; };
00531   virtual Bool applyByJones()   { return True; };
00532 
00533   // Access to matrix renderings of Jones matrices
00534   inline Jones& J1() { return *J1_[currSpw()]; };
00535   inline Jones& J2() { return *J2_[currSpw()]; };
00536 
00537   // Access to Jones matrix element array
00538   inline Cube<Complex>& currJElem() {return (*currJElem_[currSpw()]);};
00539   inline Cube<Bool>& currJElemOK()  {return (*currJElemOK_[currSpw()]);};
00540 
00541   // Invalidate cal matrices generically (at this level, both Mueller and Jones)
00542   inline virtual void invalidateCalMat() { invalidateM(); invalidateJ(); };
00543 
00544   // Validation of Jones matrices
00545   inline void invalidateJ() {JValid_(currSpw())=False;};
00546   inline void validateJ()   {JValid_(currSpw())=True;};
00547   inline Bool JValid()      {return JValid_(currSpw());};
00548 
00549   // Row-by-row apply to a Cube<Complex> (applyByJones override)
00550   virtual void applyCal(VisBuffer& vb, Cube<Complex>& Vout,Bool trial=False);
00551 
00552   // Sync matrices for current meta data (VisJones override)
00553   virtual void syncCalMat(const Bool& doInv=False);
00554 
00555   // Calculate an ensemble of Mueller matrices (all baselines, channels)
00556   //  (only meaningful if applyByMueller()=T)
00557   virtual void calcAllMueller();
00558 
00559   // Synchronize current Jones matrices
00560   virtual void syncJones(const Bool& doInv=False);
00561 
00562   // Calculate an ensemble of Jones matrices (all antennas, channels)
00563   virtual void calcAllJones();
00564 
00565   // Calculate a single Jones matrix by some means from parameters
00566   virtual void calcOneJones(Vector<Complex>& mat, Vector<Bool>& mOk, 
00567                             const Vector<Complex>& par, const Vector<Bool>& pOk );
00568 
00569   // Invert Jones matrices
00570   virtual void invJones();
00571 
00572   // Set matrix elements according to their ok flags
00573   //  (e.g., makes a unit matrix if everything flagged,
00574   //   so we don't have to make atomic ok checks in apply)
00575   virtual void setMatByOk();
00576 
00577   // Create Jones interface
00578   void createJones();
00579 
00580   // Synchronize weight scale factors
00581   virtual void syncWtScale();
00582 
00583   // Update the wt vector for a baseline
00584   virtual void updateWt(Vector<Float>& wt,const Int& a1,const Int& a2);
00585 
00586 private:
00587 
00588   // Default ctor is private
00589   VisJones();
00590 
00591   // Init VisJones pointers
00592   void initVisJones();
00593 
00594   // Delete the this* caches
00595   void deleteVisJones();
00596 
00597   // Jones algebra wrapper (per Spw)
00598   PtrBlock<Jones*> J1_;
00599   PtrBlock<Jones*> J2_;
00600 
00601   // Current Jones matrix-element arrays
00602   PtrBlock<Cube<Complex>*> currJElem_;    // [nSpw](nJElem,nChanMat,nAnt)
00603   PtrBlock<Cube<Bool>*>    currJElemOK_;  // [nSpw](nJElem,nChanMat,nAnt)
00604 
00605   // Jones validity, per spw
00606   Vector<Bool> JValid_;
00607 
00608 
00609 };
00610 
00611 } //# NAMESPACE CASA - END
00612 
00613 #endif