casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SDGrid.h
Go to the documentation of this file.
00001 //# SDGrid.h: Definition for SDGrid
00002 //# Copyright (C) 1996,1997,1998,1999,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$
00028 
00029 #ifndef SYNTHESIS_SDGRID_H
00030 #define SYNTHESIS_SDGRID_H
00031 
00032 #include <synthesis/TransformMachines/FTMachine.h>
00033 #include <synthesis/TransformMachines/SkyJones.h>
00034 #include <casa/Arrays/Matrix.h>
00035 #include <scimath/Mathematics/FFTServer.h>
00036 #include <synthesis/MSVis/VisBuffer.h>
00037 #include <images/Images/ImageInterface.h>
00038 #include <casa/Containers/Block.h>
00039 #include <casa/Arrays/Array.h>
00040 #include <casa/Arrays/Vector.h>
00041 #include <casa/Arrays/Matrix.h>
00042 #include <lattices/Lattices/LatticeCache.h>
00043 #include <lattices/Lattices/ArrayLattice.h>
00044 #include <ms/MeasurementSets/MSColumns.h>
00045 #include <measures/Measures/Measure.h>
00046 #include <measures/Measures/MDirection.h>
00047 #include <measures/Measures/MPosition.h>
00048 #include <coordinates/Coordinates/DirectionCoordinate.h>
00049 
00050 namespace casa { //# NAMESPACE CASA - BEGIN
00051 
00052 // <summary> An FTMachine for Gridding Single Dish data
00053 // </summary>
00054 
00055 // <use visibility=export>
00056 
00057 // <reviewed reviewer="" date="" tests="" demos="">
00058 
00059 // <prerequisite>
00060 //   <li> <linkto class=FTMachine>FTMachine</linkto> module
00061 //   <li> <linkto class=SkyEquation>SkyEquation</linkto> module
00062 //   <li> <linkto class=VisBuffer>VisBuffer</linkto> module
00063 // </prerequisite>
00064 //
00065 // <etymology>
00066 // FTMachine is a Machine for Fourier Transforms. SDGrid does
00067 // Single Dish gridding in a similar way
00068 // </etymology>
00069 //
00070 // <synopsis> 
00071 // The <linkto class=SkyEquation>SkyEquation</linkto> needs to be able
00072 // to perform Fourier transforms on visibility data and to grid
00073 // single dish data.
00074 // SDGrid allows efficient Single Dish processing using a 
00075 // <linkto class=VisBuffer>VisBuffer</linkto> which encapsulates
00076 // a chunk of visibility (typically all baselines for one time)
00077 // together with all the information needed for processing
00078 // (e.g. direction coordinates).
00079 //
00080 // Gridding and degridding in SDGrid are performed using a
00081 // novel sort-less algorithm. In this approach, the gridded plane is
00082 // divided into small patches, a cache of which is maintained in memory
00083 // using a general-purpose <linkto class=LatticeCache>LatticeCache</linkto> class. As the (time-sorted)
00084 // visibility data move around slowly in the image plane, patches are
00085 // swapped in and out as necessary. Thus, optimally, one would keep at
00086 // least one patch per scan line of data.
00087 //
00088 // A grid cache is defined on construction. If the gridded image plane is smaller
00089 // than this, it is kept entirely in memory and all gridding and
00090 // degridding is done entirely in memory. Otherwise a cache of tiles is
00091 // kept an paged in and out as necessary. Optimally the cache should be
00092 // big enough to hold all polarizations and frequencies for one
00093 // complete scan line.
00094 // The paging rate will then be small. As the cache size is
00095 // reduced below this critical value, paging increases. The algorithm will
00096 // work for only one patch but it will be very slow!
00097 //
00098 // The gridding and degridding steps are implemented in Fortran
00099 // for speed. In gridding, the visibilities are added onto the
00100 // grid points in the neighborhood using a weighting function.
00101 // In degridding, the value is derived by a weight summ of the
00102 // same points, using the same weighting function.
00103 // </synopsis> 
00104 //
00105 // <example>
00106 // See the example for <linkto class=SkyModel>SkyModel</linkto>.
00107 // </example>
00108 //
00109 // <motivation>
00110 // Define an interface to allow efficient processing of chunks of 
00111 // visibility data
00112 // </motivation>
00113 //
00114 // <todo asof="97/10/01">
00115 // <ul> Deal with large VLA spectral line case 
00116 // </todo>
00117 
00118 class SDGrid : public FTMachine {
00119 public:
00120 
00121   // Constructor: cachesize is the size of the cache in words
00122   // (e.g. a few million is a good number), tilesize is the
00123   // size of the tile used in gridding (cannot be less than
00124   // 12, 16 works in most cases), and convType is the type of
00125   // gridding used (SF is prolate spheriodal wavefunction,
00126   // and BOX is plain box-car summation). mLocation is
00127   // the position to be used in some phase rotations. If
00128   // mTangent is specified then the uvw rotation is done for
00129   // that location iso the image center. userSupport is to allow 
00130   // larger support for the convolution if the user wants it ..-1 will 
00131   // use the default  i.e 1 for BOX and 3 for others
00132   // <group>
00133   SDGrid(SkyJones& sj, Int cachesize, Int tilesize,
00134          String convType="BOX", Int userSupport=-1);
00135   SDGrid(MPosition& ml, SkyJones& sj, Int cachesize,
00136          Int tilesize, String convType="BOX", Int userSupport=-1);
00137   SDGrid(Int cachesize, Int tilesize,
00138          String convType="BOX", Int userSupport=-1);
00139   SDGrid(MPosition& ml, Int cachesize, Int tilesize,
00140          String convType="BOX", Int userSupport=-1);
00141   SDGrid(MPosition& ml, Int cachesize, Int tilesize,
00142          String convType="TGAUSS", Float truncate=-1.0, 
00143          Float gwidth=0.0, Float jwidth=0.0);
00144   // </group>
00145 
00146   // Copy constructor
00147   SDGrid(const SDGrid &other);
00148 
00149   // Assignment operator
00150   SDGrid &operator=(const SDGrid &other);
00151 
00152   ~SDGrid();
00153 
00154   // Initialize transform to Visibility plane using the image
00155   // as a template. The image is loaded and Fourier transformed.
00156   void initializeToVis(ImageInterface<Complex>& image,
00157                        const VisBuffer& vb);
00158 
00159   // Finalize transform to Visibility plane: flushes the image
00160   // cache and shows statistics if it is being used.
00161   void finalizeToVis();
00162 
00163   // Initialize transform to Sky plane: initializes the image
00164   void initializeToSky(ImageInterface<Complex>& image,  Matrix<Float>& weight,
00165                        const VisBuffer& vb);
00166 
00167   // Finalize transform to Sky plane: flushes the image
00168   // cache and shows statistics if it is being used. DOES NOT
00169   // DO THE FINAL TRANSFORM!
00170   void finalizeToSky();
00171 
00172   // Get actual coherence from grid by degridding
00173   void get(VisBuffer& vb, Int row=-1);
00174 
00175   // Put coherence to grid by gridding.
00176   void put(const VisBuffer& vb, Int row=-1, Bool dopsf=False,
00177            FTMachine::Type type=FTMachine::OBSERVED);
00178 
00179   // Get the final image: do the Fourier transform and
00180   // grid-correct, then optionally normalize by the summed weights
00181   ImageInterface<Complex>& getImage(Matrix<Float>&, Bool normalize=True);
00182   virtual void normalizeImage(Lattice<Complex>& /*skyImage*/,
00183                               const Matrix<Double>& /*sumOfWts*/,
00184                               Lattice<Float>& /*sensitivityImage*/,
00185                               Bool /*fftNorm*/)
00186     {throw(AipsError("SDGrid::normalizeImage() called"));}
00187 
00188   // Get the final weights image
00189   void getWeightImage(ImageInterface<Float>&, Matrix<Float>&);
00190 
00191   // Has this operator changed since the last application?
00192   virtual Bool changed(const VisBuffer& vb);
00193   virtual void setMiscInfo(const Int qualifier){(void)qualifier;};
00194   virtual void ComputeResiduals(VisBuffer& /*vb*/, Bool /*useCorrected*/) {};
00195 
00196   virtual String name() const;
00197 
00198 private:
00199 
00200   // Find the Primary beam and convert it into a convolution buffer
00201   void findPBAsConvFunction(const ImageInterface<Complex>& image,
00202                             const VisBuffer& vb);
00203 
00204   SkyJones* sj_p;
00205 
00206   // Get the appropriate data pointer
00207   Array<Complex>* getDataPointer(const IPosition&, Bool);
00208   Array<Float>* getWDataPointer(const IPosition&, Bool);
00209 
00210   void ok();
00211 
00212   void init();
00213 
00214   // Image cache
00215   LatticeCache<Complex> * imageCache;
00216   LatticeCache<Float> * wImageCache;
00217 
00218   // Sizes
00219   Int cachesize, tilesize;
00220 
00221   // Is this tiled?
00222   Bool isTiled;
00223 
00224   // Storage for weights
00225   ImageInterface<Float>* wImage;
00226 
00227   // Array lattice
00228   Lattice<Complex> * arrayLattice;
00229   Lattice<Float> * wArrayLattice;
00230 
00231   // Lattice. For non-tiled gridding, this will point to arrayLattice,
00232   //  whereas for tiled gridding, this points to the image
00233   Lattice<Complex>* lattice;
00234   Lattice<Float>* wLattice;
00235 
00236   String convType;
00237 
00238   // Useful IPositions
00239   IPosition centerLoc, offsetLoc;
00240 
00241   // Array for non-tiled gridding
00242   Array<Complex> griddedData;
00243   Array<Float> wGriddedData;
00244 
00245 
00246   DirectionCoordinate directionCoord;
00247 
00248   MDirection::Convert* pointingToImage;
00249 
00250   Vector<Double> xyPos;
00251   //Original xypos of moving source
00252   Vector<Double> xyPosMovingOrig_p;
00253 
00254   MDirection worldPosMeas;
00255 
00256   Cube<Int> flags;
00257 
00258   Vector<Float> convFunc;
00259   Int convSampling;
00260   Int convSize;
00261   Int convSupport;
00262   Int userSetSupport_p;
00263   
00264   Float truncate_p;
00265   Float gwidth_p;
00266   Float jwidth_p;
00267 
00268   Int lastIndex_p;
00269 
00270   Int getIndex(const ROMSPointingColumns& mspc, const Double& time,
00271                const Double& interval);
00272 
00273   Bool getXYPos(const VisBuffer& vb, Int row);
00274 
00275   //get the MDirection from a chosen column of pointing table
00276   MDirection directionMeas(const ROMSPointingColumns& mspc, const Int& index);
00277   MDirection directionMeas(const ROMSPointingColumns& mspc, const Int& index, const Double& time);
00278   MDirection interpolateDirectionMeas(const ROMSPointingColumns& mspc, const Double& time,
00279                                   const Int& index, const Int& index1, const Int& index2);
00280 
00281   //for debugging
00282   //FILE *pfile;
00283 };
00284 
00285 } //# NAMESPACE CASA - END
00286 
00287 #endif