casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
NewMultiTermFT.h
Go to the documentation of this file.
00001 //# NewMultiTermFT.h: Definition for NewMultiTermFT
00002 //# Copyright (C) 1996,1997,1998,1999,2000,2002
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_NEWMULTITERMFT_H
00030 #define SYNTHESIS_NEWMULTITERMFT_H
00031 
00032 #include <synthesis/TransformMachines/FTMachine.h>
00033 #include <casa/Arrays/Matrix.h>
00034 #include <scimath/Mathematics/FFTServer.h>
00035 #include <synthesis/MSVis/VisBuffer.h>
00036 #include <images/Images/ImageInterface.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 <scimath/Mathematics/ConvolveGridder.h>
00043 #include <lattices/Lattices/LatticeCache.h>
00044 #include <lattices/Lattices/ArrayLattice.h>
00045 //#include <synthesis/MeasurementComponents/SynthesisPeek.h>
00046 #include <casa/OS/Timer.h>
00047 
00048 namespace casa { //# NAMESPACE CASA - BEGIN
00049 
00050 class UVWMachine;
00051 
00052 class NewMultiTermFT : public FTMachine {
00053 public:
00054 
00055   // Construct using an existing FT-Machine 
00056   NewMultiTermFT(FTMachine *subftm, Int nterms=1, Double reffreq=0.0);
00057 
00058   // Construct from a Record containing the NewMultiTermFT state
00059   NewMultiTermFT(const RecordInterface& stateRec);
00060 
00061   // Copy constructor. 
00062   // This first calls the default "=" operator, and then instantiates objects for member pointers.
00063   NewMultiTermFT(const NewMultiTermFT &other);
00064 
00065   // Assignment operator --- leave it as the default
00066   NewMultiTermFT &operator=(const NewMultiTermFT &other);
00067 
00068   // Destructor
00069   ~NewMultiTermFT();
00070 
00071   // Called at the start of de-gridding : subftm->initializeToVis()
00072   // Note : Pre-de-gridding model-image divisions by PBs will go here.
00073   void initializeToVis(ImageInterface<Complex>& /*image*/, 
00074                        const VisBuffer& /*vb*/)
00075   {throw(AipsError("NewMultiTermFT::initializeToVis called without vectors !"));};
00076 
00077    // Vectorized InitializeToVis
00078   void initializeToVis(Block<CountedPtr<ImageInterface<Complex> > > & compImageVec,PtrBlock<SubImage<Float> *> & modelImageVec, PtrBlock<SubImage<Float> *>& weightImageVec, PtrBlock<SubImage<Float> *>& fluxScaleVec, Block<Matrix<Float> >& weightsVec, const VisBuffer& vb);
00079 
00080   // Called at the end of de-gridding : subftm->finalizeToVis()
00081   void finalizeToVis();
00082 
00083   // Called at the start of gridding : subftm->initializeToSky()
00084   void initializeToSky(ImageInterface<Complex>& /*image*/,  
00085                        Matrix<Float>& /*weight*/, const VisBuffer& /*vb*/) 
00086    {throw(AipsError("NewMultiTermFT::initializeToSky() called without vectors!"));};
00087 
00088   void initializeToSky(Block<CountedPtr<ImageInterface<Complex> > > & compImageVec, Block<Matrix<Float> >& weightsVec, const VisBuffer& vb, const Bool dopsf);
00089 
00090 
00091   // Called at the end of gridding : subftm->finalizeToSky()
00092   void finalizeToSky(){throw(AipsError("NewMultiTermFT::finalizeToSky() called without arguments!"));};
00093 
00094   void finalizeToSky(Block<CountedPtr<ImageInterface<Complex> > > & compImageVec, PtrBlock<SubImage<Float> *> & resImageVec, PtrBlock<SubImage<Float> *>& weightImageVec, PtrBlock<SubImage<Float> *>& fluxScaleVec, Bool dopsf, Block<Matrix<Float> >& weightsVec);
00095 
00096   //  void normalizeToSky(ImageInterface<Complex>& compImage, ImageInterface<Float>& resImage, ImageInterface<Float>& weightImage, Bool dopsf, Matrix<Float>& weights)
00097   // {throw(AipsError("NewMultiTermFT::normalizeToSky should not get called !"));};
00098 
00099 
00100   // Do the degridding via subftm->get() and modify model-visibilities by Taylor-weights
00101   void get(VisBuffer& vb, Int row=-1);
00102 
00103   // Modify imaging weights with Taylor-weights and do gridding via subftm->put()
00104   void put(VisBuffer& vb, Int row=-1, Bool dopsf=False,
00105            FTMachine::Type type=FTMachine::OBSERVED);
00106 
00107   // Have a const version for compatibility with other FTMs.. Throw an exception if called.
00108   void put(const VisBuffer& /*vb*/, Int /*row*/=-1, Bool /*dopsf*/=False,
00109            FTMachine::Type /*type*/=FTMachine::OBSERVED)
00110   {throw(AipsError("NewMultiTermFT::put called with a const vb. This FTM needs to modify the vb."));};
00111 
00112   // Calculate residual visibilities if possible.
00113   // The purpose is to allow rGridFT to make this multi-threaded
00114   virtual void ComputeResiduals(VisBuffer&vb, Bool useCorrected); 
00115 
00116   // Make an image : subftm->makeImage()
00117   void makeImage(FTMachine::Type type,
00118                  VisSet& vs,
00119                  ImageInterface<Complex>& image,
00120                  Matrix<Float>& weight);
00121   
00122   // Get the final image: do the Fourier transform grid-correct, then 
00123   // optionally normalize by the summed weights
00124   // Note : Post-gridding residual-image divisions by PBs will go here.
00125   //           For now, it just calls subftm->getImage()
00126   //  ImageInterface<Complex>& getImage(Matrix<Float>& weights, Bool normalize=True)
00127   //{return getImage(weights,normalize,0);};
00128   //ImageInterface<Complex>& getImage(Matrix<Float>& weights, Bool normalize=True, 
00129   //                                                         const Int taylorindex=0);
00130   ImageInterface<Complex>& getImage(Matrix<Float>& /*weights*/, Bool /*normalize*/=True)
00131   {throw(AipsError("NewMultiTermFT::getImage() should not be called"));}
00132  
00133 
00134   // Place-holder for possible use with AWProject and AWProjectWB FTMs
00135   /*
00136   virtual void normalizeImage(Lattice<Complex>& skyImage,
00137                               const Matrix<Double>& sumOfWts,
00138                               Lattice<Float>& sensitivityImage,
00139                               Bool fftNorm)
00140     {throw(AipsError("NewMultiTermFT::normalizeImage() is not implemented"));}
00141   */
00142   // Get the final weights image - this will hold PB2
00143   //void getWeightImage(ImageInterface<Float>& weightImage, Matrix<Float>& weights)
00144   //{getWeightImage(weightImage, weights, 0);};
00145   //void getWeightImage(ImageInterface<Float>& weightImage, Matrix<Float>& weights, 
00146   //                                const Int taylorindex);
00147   void getWeightImage(ImageInterface<Float>& weightImage, Matrix<Float>& weights)
00148   {subftms_p[0]->getWeightImage(weightImage, weights);}
00149   //  {throw(AipsError("NewMultiTermFT::getWeightImage() should not be called"));}
00150 
00151   // Save and restore the NewMultiTermFT to and from a record
00152   virtual Bool toRecord(String& error, RecordInterface& outRec, Bool withImage=False);
00153   virtual Bool fromRecord(String& error, const RecordInterface& inRec);
00154 
00155   // Various small inline functions
00156   virtual Bool isFourier() {return True;}
00157   virtual void setNoPadding(Bool nopad){subftms_p[0]->setNoPadding(nopad);};
00158   virtual String name()const {return machineName_p;};
00159   virtual void setMiscInfo(const Int qualifier){(void)qualifier;};
00160 
00161   void printFTTypes()
00162   {
00163     cout << "** Number of FTs : " << subftms_p.nelements() << " -- " ;
00164     for(uInt tix=0; tix<(subftms_p).nelements(); tix++)
00165       cout << tix << " : " << (subftms_p[tix])->name() << "   " ;
00166     cout << endl;
00167   };
00168 
00169   virtual void setDOPBCorrection(Bool doit=True) {doWideBandPBCorrection_p=doit;};
00170   virtual Bool getDOPBCorrection() {return doWideBandPBCorrection_p;};
00171   virtual void setConjBeams(Bool useit=True) {useConjBeams_p=useit;};
00172   virtual Bool getConjBeams() {return useConjBeams_p;};
00173 
00174 
00175 protected:
00176 
00177   // Instantiate a new sub FTM
00178   FTMachine* getNewFTM(const FTMachine *ftm);
00179 
00180   // Multiply Imaging weights by Taylor-function weights - during "put"
00181   Bool modifyVisWeights(VisBuffer& vb, uInt thisterm);
00182   // Multiply model visibilities by Taylor-function weights - during "get"
00183   Bool modifyModelVis(VisBuffer &vb, uInt thisterm);
00184   // Restore vb.imagingweights to the original
00185   void restoreImagingWeights(VisBuffer &vb);
00186 
00187   // Use sumwts to make a Hessian, invert it, apply to weight images, fill in pbcoeffs_p
00188   void normAvgPBs(PtrBlock<SubImage<Float> *> & weightImageVec);
00189   void calculateTaylorPBs(PtrBlock<SubImage<Float> *> & weightImageVec);
00190   // Make pixel-by-pixel matrices from pbcoeffs, invert, apply to residuals
00191   //  void normalizeWideBandPB2(PtrBlock<SubImage<Float> *> & resImageVec);
00192   //  void normalizeWideBandPB(PtrBlock<SubImage<Float> *> & resImageVec, PtrBlock<SubImage<Float> *>& scratchImageVec);
00193   void applyWideBandPB(String action, PtrBlock<SubImage<Float> *> & imageVec);
00194 
00195   void multiplyHMatrix( Matrix<Double> &hmat, PtrBlock<SubImage<Float>* > &invec,
00196                         PtrBlock<SubImage<Float>* > &outvec, String saveImagePrefix );
00197 
00198   // Helper function to write ImageInterfaces to disk
00199   Bool storeAsImg(String fileName, ImageInterface<Float> & theImg);
00200 
00201 
00202   Cube<Complex> modviscube_p;
00203 
00205   uInt nterms_p;
00206   Bool donePSF_p, doingPSF_p;
00207   Double reffreq_p;
00208   Matrix<Float> imweights_p;
00209   String machineName_p;
00210   Float pblimit_p;
00211   Bool doWideBandPBCorrection_p;
00212   String cacheDir_p;
00213   Bool donePBTaylor_p;
00214   Bool useConjBeams_p;
00215 
00216   Block< CountedPtr<FTMachine> > subftms_p;
00217   Block<Matrix<Float> > sumweights_p;
00218 
00219   Double sumwt_p;
00220   Matrix<Double> hess_p, invhess_p; // Block is for the pol axis
00221 
00222   Block<CountedPtr<ImageInterface<Float> > > sensitivitymaps_p;
00223   PtrBlock<SubImage<Float>* > pbcoeffs_p;
00224 
00225   Bool dbg_p,dotime_p;
00226   Timer tmr_p;
00227   Double time_get, time_put, time_res;
00228 };
00229 
00230 } //# NAMESPACE CASA - END
00231 
00232 #endif