casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
BeamSkyJones.h
Go to the documentation of this file.
00001 //# BeamSkyJones.h: Definitions of interface for BeamSkyJones 
00002 //# Copyright (C) 1996,1997,1998,1999,2000,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$
00028 
00029 #ifndef SYNTHESIS_BEAMSKYJONES_H
00030 #define SYNTHESIS_BEAMSKYJONES_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Containers/Block.h>
00034 #include <casa/Exceptions/Error.h>
00035 #include <measures/Measures/MDirection.h>
00036 #include <measures/Measures.h>
00037 #include <measures/Measures/Stokes.h>
00038 #include <synthesis/TransformMachines/SkyJones.h>
00039 #include <synthesis/TransformMachines/PBMath.h>
00040 
00041 
00042 namespace casa { //# NAMESPACE CASA - BEGIN
00043 
00044 //#forward
00045 class SkyModel;
00046       
00047 //# Need forward declaration for Solve in the Jones Matrices
00048 class SkyEquation;
00049 class ImageRegion;
00050 
00051 // <summary> beam-like sky-plane effects for the SkyEquation </summary>
00052 
00053 // <use visibility=export>
00054 
00055 // <reviewed reviewer="" date="" tests="" demos="">
00056 
00057 // <prerequisite>
00058 // <li> <linkto class="SkyEquation">SkyEquation</linkto> class
00059 // <li> <linkto class="SkyJones">SkyJones</linkto> class
00060 // <li> <linkto class="PBMathInterface">PBMathInterface</linkto> class
00061 // </prerequisite>
00062 //
00063 // <etymology>
00064 // BeamSkyJones, derived from SkyJones, describes an interface to
00065 // beam-based SkyJones objects.  Like SkyJones, it too is an Abstract
00066 // Base Class, but implements the beam-related methods.
00067 // </etymology>
00068 //
00069 // <synopsis> 
00070 //
00071 // <motivation>
00072 // The properties of sky-plane based calibration effects must be described
00073 // for the <linkto class="SkyEquation">SkyEquation</linkto>; this class
00074 // encapsulates the antenna beam-based aspects which are present in at least
00075 // a few other specific SkyJones (VPSkyJones and DBeamSkyJones).
00076 // </motivation>
00077 //
00078 // <todo asof="98/11/01">
00079 // <li> Solvable part needs implementation: we need to derive an
00080 // image of gradients of the elements of the Jones matrix. See VisJones
00081 // for how to do this.
00082 // <li> The MS, version II, will have a beam subtable, which will have PBMaths
00083 // for each antenna.  Until this becomes available, we need to do some
00084 // fudging; for example, we 
00085 // </todo>
00086 
00087 class BeamSkyJones : virtual public SkyJones {
00088 
00089 public:
00090 
00091   // Eventually, the MS will have all the beam information in its Beam Subtable.
00092   // Till then, we either guess the PB to use or explicitly define it upon construction.
00093   // Construct from a Measurement Set, figure out the most appropriate PBMath object
00094   // from MS information
00095   BeamSkyJones(const Quantity &parallacticAngleIncrement = Quantity(720.0, "deg"), // def= 1 PA interval
00096                BeamSquint::SquintType doSquint = BeamSquint::NONE,  // def= no beam squint offsets
00097                const Quantity &skyPositionThreshold = Quantity(180,"deg"));  // def= assume there is no change of
00098                                                              // this operator due to position offset
00099 
00100 
00101   // Virtual destructor (this is a virtual base class)
00102   virtual ~BeamSkyJones() = 0;
00103 
00104   // Print out information concerning the state of this object
00105   virtual void showState(LogIO& os);
00106 
00107   // Apply Jones matrix to an image (and adjoint)
00108   // No "applyInverse" is available from the SkyJones classes,
00109   // you can get them directly from PBMath or you can get
00110   // the equivalent effect by dividing by grad grad Chi^2
00111   // in ImageSkyModel.
00112   // <group>
00113   ImageInterface<Complex>& apply(const ImageInterface<Complex>& in,
00114                                  ImageInterface<Complex>& out,
00115                                  const VisBuffer& vb, Int row,
00116                                  Bool forward=True);
00117   ImageInterface<Float>& apply(const ImageInterface<Float>& in,
00118                                      ImageInterface<Float>& out,
00119                                      const VisBuffer& vb, Int row);
00120 
00121   ImageInterface<Float>& applySquare(const ImageInterface<Float>& in,
00122                                      ImageInterface<Float>& out,
00123                                      const VisBuffer& vb, Int row);
00124   // </group>
00125 
00126   // Apply Jones matrix to a sky component (and adjoint)  
00127   // <group>
00128   SkyComponent& apply(SkyComponent& in,
00129                       SkyComponent& out,
00130                       const VisBuffer& vb, Int row,
00131                       Bool forward = True);
00132   SkyComponent& applySquare(SkyComponent& in,
00133                             SkyComponent& out,
00134                             const VisBuffer& vb, Int row);
00135   // </group>
00136 
00137   // Understand if things have changed since last PB application
00138   // <group>
00139 
00140   // Reset to pristine state
00141   virtual void reset();
00142 
00143   // Has this operator changed since the last Application?
00144   // (or more properly, since the last update() ) 
00145   virtual Bool changed(const VisBuffer& vb, Int row);
00146 
00147   // Does the operator change in this visbuffer or since the last call?
00148   // May not be useful -- check it out:  m.a.h. Dec 30 1999
00149   virtual Bool change(const VisBuffer& vb);
00150 
00151   // Does this operator changed in this VisBuffer,
00152   // starting with row1?
00153   // If yes, we return in row2, the last row that has the
00154   // same SkyJones as row1.
00155   // NOTE: need to first call changed(const VisBuffer& vb, Int row) and
00156   // shield the user from the case where the fieldID has changed
00157   // (which only changes in blocks)
00158   virtual Bool changedBuffer(const VisBuffer& vb, Int row1, Int& row2);
00159 
00160   // Update the FieldID, Telescope, pointingDirection, Parallactic angle info
00161   void update(const VisBuffer& vb, Int row);
00162 
00163   // if (changed) {update()}
00164   virtual void assure (const VisBuffer& vb, Int row);
00165   // </group>
00166 
00167   // Return the type of this Jones matrix (actual type of derived class).
00168   virtual Type type() {return SkyJones::E;};
00169 
00170   // Apply gradient
00171   virtual ImageInterface<Complex>& 
00172   applyGradient(ImageInterface<Complex>& result, const VisBuffer& vb,
00173                 Int row);
00174 
00175   virtual SkyComponent&
00176   applyGradient(SkyComponent& result, const VisBuffer& vb,
00177                 Int row);
00178 
00179   // Is this solveable?
00180   virtual Bool isSolveable() {return False;};
00181 
00182   // Initialize for gradient search
00183   virtual void initializeGradients();
00184 
00185   // Finalize for gradient search
00186   virtual void finalizeGradients();
00187  
00188   // Add to Gradient Chisq
00189   virtual void addGradients(const VisBuffer& vb, Int row, const Float sumwt, 
00190                             const Float chisq, const Matrix<Complex>& c, 
00191                             const Matrix<Float>& f);
00192  
00193   // Solve
00194   virtual Bool solve (SkyEquation& se);
00195   
00196   // Manage the PBMath objects
00197   // <group>
00198   // set the PB based on telescope name, antennaID and feedID
00199   // If antennaID or feedID is -1, the PBMath object is set for
00200   // all antennae or feeds, respectively. These are the default
00201   // values to retain the previous interface.
00202   //
00203   // Note. It would be nice to change the interface and make antennaID
00204   // and feedID the second and the third parameter, respectively.
00205   void setPBMath(const String &telescope, PBMath &myPBmath,
00206                  const Int &antennaID = -1, const Int &feedID = -1);
00207   
00208   // get the PBMath object; returns False if that one doesn't exist,
00209   // True if it does exist and is OK
00210   // whichAnt is an index into an array of PBMaths, which is different
00211   // for all telescope/antenna/feed combinations
00212   // Not sure why we need such a low-level method declared as public,
00213   // retained to preserve old interface
00214   Bool getPBMath(uInt whichAnt, PBMath &myPBMath) const;
00215   
00216   // get the PBMath object; returns False if that one doesn't exist,
00217   // True if it does exist and is OK
00218   // antennaID and feedID default to -1 to preserve the old interface
00219   // TODO: change the interface and make antennaID and feedID the
00220   // second and third parameter respectively to have a better looking code
00221   Bool getPBMath(const String &telescope, PBMath &myPBMath,
00222                  const Int &antennaID = -1, const Int &feedID = -1) const;
00223 
00224   Quantity getPAIncrement() {return Quantity(parallacticAngleIncrement_p,"rad");}
00225 
00226   Quantity getSkyPositionThreshold() {return Quantity(skyPositionThreshold_p,"rad");}
00227   
00228   // Return true if all antennas share a common VP
00229   Bool isHomogeneous() const;
00230   //</group>
00231   
00232   // Get the ImageRegion of the primary beam on an Image for a given pointing
00233   // Note: ImageRegion is not necesarily constrained to lie within the
00234   // image region (for example, if the pointing center is near the edge of the
00235   // image).  fPad: extra padding over the primary beam supporrt, 
00236   // fractional (ie, 1.2 for 20% padding), in all directions.
00237   // (note: we do not properly treat squint yet, this will cover it for now)
00238   // iChan: frequency channel to take: lowest frequency channel is safe for all
00239   //
00240   // Potential problem: this ImageRegion includes all Stokes and Frequency Channels
00241   // present in the input image.
00242   ImageRegion*  extent (const ImageInterface<Complex>& im, 
00243                         const VisBuffer& vb,  
00244                         const Int irow=-1,                      
00245                         const Float fPad=1.2,  
00246                         const Int iChan=0, 
00247                         const SkyJones::SizeType sizeType=SkyJones::COMPOSITE);
00248 
00249   ImageRegion*  extent (const ImageInterface<Float>& im, 
00250                         const VisBuffer& vb,  const Int irow=-1,
00251                         const Float fPad=1.2,  const Int iChan=0, 
00252                         const SkyJones::SizeType sizeType=SkyJones::COMPOSITE);
00253 
00254   // summarize the PBMaths contained here.
00255   // n = -1 => terse table
00256   // n =  0 => table plus constructor values
00257   // n =  m => plot m samples of the PB profile
00258   virtual void summary(Int n=0);
00259 
00260   //return the telescope it is on at this state
00261   String telescope();
00262 
00263   //Get an idea of the support of the PB in number of pixels
00264   virtual Int support(const VisBuffer& vb, const CoordinateSystem& cs);
00265 
00266 private:  
00267 
00268 
00269   String telescope_p;
00270 
00271   Int lastFieldId_p;
00272 
00273   Int lastArrayId_p;
00274 
00275   Int lastMSId_p;
00276 
00277   BeamSquint::SquintType doSquint_p;
00278 
00279   Double  parallacticAngleIncrement_p; // a parallactic angle threshold
00280                         // beyond which the operator is considered to be
00281                         // changed (in radians)
00282   Double  skyPositionThreshold_p;     // a sky position threshold beyond
00283                         // which the operator is considered to be changed
00284                         // (in radians)
00285   Block<Double> lastParallacticAngles_p; // a cache of parallactic angles
00286                         // used when the operator was applied last time.
00287                         // One value in radians for each beam model in PBMaths.
00288                         // A zero-length block means that the operator
00289                         // has never been applied
00290   Block<MDirection> lastDirections_p; // a chache of directions
00291                         // used when the operator was applied last time.
00292                         // One element for each beam model in PBMaths.
00293                         // A zero-length block means that the operator
00294                         // has never been applied
00295 
00296   // One or more PBMaths (a common one for the
00297   // entire array, or one for each antenna)
00298   // This requires some sorting out for heterogeneous arrays!
00299   Block<PBMath> myPBMaths_p;  
00300   // Names of telescopes (parralel with PBMaths
00301   Block<String> myTelescopes_p;
00302 
00303   // Antenna IDs (parallel with PBMaths)
00304   Block<Int> myAntennaIDs_p;
00305   // Feed IDs (parallel with PBMaths)
00306   Block<Int> myFeedIDs_p;
00307 
00308   // cache of the indices to the PBMaths container for antenna/feed 1 and 2  
00309   mutable const VisBuffer *lastUpdateVisBuffer_p; // to ensure that the cache
00310                                           // is filled for the correct
00311                                           // VisBuffer. The value is used
00312                                           // for comparison only
00313   mutable Int lastUpdateRow_p;  // to ensure that the cache is filled for
00314                                 // correct row in the VisBuffer
00315   mutable Int lastUpdateIndex1_p; // index of the first antenna/feed
00316   mutable Int lastUpdateIndex2_p; // index of the second antenna/feed
00317   //
00318 
00319   mutable Bool hasBeenApplied;  // True if the operator has been applied at least once
00320 
00321   // update the indices cache. This method could be made protected in the
00322   // future (need access functions for lastUpdateIndex?_p to benefit from it)
00323   // Cache will be valid for a given VisBuffer and row
00324   void updatePBMathIndices(const VisBuffer &vb, Int row) const;
00325 
00326 protected:
00327   // return True if two directions are close enough to consider the
00328   // operator unchanged, False otherwise
00329   Bool directionsCloseEnough(const MDirection &dir1,
00330                              const MDirection &dir2) const throw(AipsError);
00331                              
00332   // return index of compareTelescope, compareAntenna and compareFeed in
00333   // myTelescopes_p, myAntennaIDs and myFeedIDs; -1 if not found
00334   // if compareAntenna or compareTelescope is -1, this means that the
00335   // PBMath class is the same for all antennae/feeds. An exception will
00336   // be raised, if separate PBMath objects have been assigned by setPBMath
00337   // for different feeds/antennae but -1 is used for query.
00338   //
00339   // It would be good to rename this function to indexBeams as this name
00340   // is more appropriate. 
00341   //
00342   Int indexTelescope(const String & compareTelescope,
00343                      const Int &compareAntenna=-1,
00344                      const Int &compareFeed=-1) const;
00345 
00346 };
00347  
00348 
00349 } //# NAMESPACE CASA - END
00350 
00351 #endif
00352