casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Deconvolver.h
Go to the documentation of this file.
00001 //# Deconvolver: defines classes for deconvolver.
00002 //# Copyright (C) 1996-2007
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 addressed 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: DOdeconvolver.h,v 19.7 2005/03/18 01:43:31 kgolap Exp $
00028 
00029 #ifndef SYNTHESIS_DECONVOLVER_H
00030 #define SYNTHESIS_DECONVOLVER_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/IPosition.h>
00034 #include <casa/Quanta/Quantum.h>
00035 #include <measures/Measures/MDirection.h>
00036 #include <measures/Measures/MPosition.h>
00037 #include <measures/Measures/MRadialVelocity.h>
00038 #include <lattices/Lattices/LatticeCleaner.h>
00039 #include <components/ComponentModels/GaussianBeam.h>
00040 
00041 #include <synthesis/MeasurementEquations/MultiTermMatrixCleaner.h>
00042 
00043 #include <casa/namespace.h>
00044 namespace casa { //# NAMESPACE CASA - BEGIN
00045 template<class T> class Lattice;
00046 template<class T> class PagedImage;
00047 template<class T> class TempImage;
00048 template<class T> class ImageInterface;
00049 template<class T> class LatticeConvolver;
00050 template<class T> class ResidualEquation;
00051 template<class T> class SubImage;
00052 
00053 
00054 class File;
00055 class CEMemModel;
00056 class ClarkCleanLatModel;
00057 class LatConvEquation;
00058 class ImageMSCleaner;
00059 
00060 // <summary> A simple deconvolver operating on images (no SkyEquation) </summary>
00061 
00062 // <use visibility=export>
00063 
00064 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00065 // </reviewed>
00066 
00067 // <prerequisite>
00068 //   <li> <linkto class="SkyEquation">SkyEquation</linkto>
00069 //   <li> <linkto class="SkyModel">SkyModel</linkto>
00070 // </prerequisite>
00071 //
00072 // <etymology>
00073 // Undo convolution or at least try to do it !
00074 // </etymology>
00075 //
00076 // <synopsis>
00077 // This class is a container that allows many SkyComponents to be grouped
00078 // together and manipulated as a group. In this respect this class is identical
00079 // to the <linkto class="ComponentList">ComponentList</linkto> class. The user
00080 // is encouraged to read the synopsis of that class for a general description
00081 // of the capabilities of this class.
00082 //
00083 // This class is differs from the ComponentList class in the following ways:
00084 // <ul>
00085 // <li> All components are indexed starting at one. This means the first
00086 //      component in this class is obtained by <src>component(1)</src> rather
00087 //      than <src>component(0)</src> in the ComponentList class.
00088 // <li> Copies of the components, rather than references, are returned to the
00089 //      user. This means that this class needs a replace function whereas
00090 //      ComponentList does not.
00091 // <li> Components that have been removed from the list are stored in a
00092 //      temporary place. In the ComponentList class once they are deleted they
00093 //      are gone.
00094 // <li> This class is derived from ApplicationObject and follows the AIPS++
00095 //      conventions for "distributed objects". Hence the fuunctions in this
00096 //      class can be made accessible from glish. 
00097 // <li> This class can generate simulated components and add them to the list.
00098 // </ul>
00099 //
00100 // There is a one-to-one correspondence between the functions in the glish
00101 // componentlist object (see the AIPS++ User Reference manual) and functions in
00102 // this class. This is make simplify the porting from glish to C++ of a glish
00103 // script using the componentlist distributed object.
00104 // </synopsis>
00105 //
00106 // <example>
00107 // These examples are coded in the tDOcomponentlist.h file.
00108 // <h4>Example 1:</h4>
00109 // In this example a ComponentList object is created and used to calculate the
00110 // ...
00111 // <srcblock>
00112 // </srcblock>
00113 // </example>
00114 //
00115 // <motivation> 
00116 // This class was written to make the componentlist classes usable from glish
00117 // </motivation>
00118 //
00119 // <thrown>
00120 // <li> AipsError - If an internal inconsistancy is detected, when compiled in 
00121 // debug mode only.
00122 // </thrown>
00123 //
00124 // <todo asof="1998/05/22">
00125 //   <li> Nothing I hope. But I expect users will disagree.
00126 // </todo>
00127 
00128 class Deconvolver 
00129 {
00130 public:
00131   // "deconvolver" ctor
00132   Deconvolver();
00133   
00134   Deconvolver(const String& dirty, const String& psf);
00135   
00136   Deconvolver(const Deconvolver &other);
00137   Deconvolver &operator=(const Deconvolver &other);
00138   ~Deconvolver();
00139 
00140   // Open the given dirty image and psf
00141   // If warn is true,  print warnings about there being
00142   // no psf if one is not supplied.
00143   Bool open(const String& dirty, const String& psf, Bool warn=True);
00144 
00145   // After some cleaning, the dirty image is replaced with the
00146   // residual image in the deconvolver tool.  reopen reinstates
00147   // that dirty image; cannot be invoked before open has been
00148   // invoked
00149   Bool reopen();
00150   
00151   // Flush the ms to disk and detach from the ms file. All function
00152   // calls after this will be a no-op.
00153   Bool close();
00154   
00155   String dirtyname() const;
00156   String psfname() const;
00157 
00158   // Output a summary of the state of the object
00159   Bool summary() const;
00160   
00161   // Return the state of the object as a string
00162   String state() const;
00163   
00164   // Return the image shape
00165   IPosition imageshape() const;
00166 
00167   // Restore
00168   Bool restore(const String& model,
00169                const String& image, GaussianBeam& mbeam);
00170 
00171   // Residual
00172   Bool residual(const String& model, 
00173                const String& image);
00174 
00175   // Smooth
00176   Bool smooth(const String& model, 
00177               const String& image,
00178               GaussianBeam& mbeam,
00179               Bool normalizeVolume);
00180 
00181   // Clean algorithm
00182   //maxResidual and iterationsDone are return values
00183   Bool clean(const String& algorithm,
00184              const Int niter, const Float gain, const Quantity& threshold, 
00185              const Bool displayProgress,
00186              const String& model, const String& mask, Float& maxResidual, 
00187              Int& iterationsDone );
00188 
00189   //Clark Clean but image, psf, mask has to be 4-axes in the canonical casa order.
00190   //Useful for cleaning dirty images made in CASA
00191   //if mask is larger than a quarter of the image it will do a full image clean ...unlike the one below
00192   Bool clarkclean(const Int niter, 
00193                   const Float gain, const Quantity& threshold, 
00194                   const String& model, const String& maskName,
00195                   Float& maxresid, Int& iterused,
00196                   Float cycleFactor=1.5);
00197 
00198   // Clark Clean algorithm
00199   Bool clarkclean(const Int niter, 
00200                   const Float gain, const Quantity& threshold, 
00201                   const Bool displayProgress, 
00202                   const String& model, const String& mask,
00203                   const Int histBins, 
00204                   const Vector<Int>& psfPatchSize, const Float maxExtPsf,
00205                   const Float speedUp, Int maxNumPix,
00206                   const Int maxNumMajorCycles,
00207                   const Int maxNumMinorIterations);
00208   
00209   
00210   // MEM algorithm    add other inputs as required
00211   Bool mem(const String& algorithm,
00212            const Int niter, const Quantity& sigma, 
00213            const Quantity& targetFlux, 
00214            Bool constrainTargetFlux, 
00215            Bool displayprogress, 
00216            const String& model, 
00217            const String& prior = "",
00218            const String& mask = "",
00219            const Bool imagePlane = False);
00220   
00221   // make a prior image
00222   Bool makeprior(const String& prior,
00223                  const String& templateImage,
00224                  const Quantity& lowClipfrom, 
00225                  const Quantity& lowClipto, 
00226                  const Quantity& highClipfrom, 
00227                  const Quantity& highClipto, 
00228                  const Vector<Int>& blc,
00229                  const Vector<Int>& trc);
00230   
00231   // Set up scales: based on scaleMethod = "nscales" or "uservector",
00232   // we will create the scale sizes in pixels via a power law or
00233   // use the user specified scale sizes.
00234   Bool setscales(const String& scaleMethod, const Int nscales, 
00235                  const Vector<Float>& userScaleSizes);
00236   
00237   // NNLS algorithm
00238   Bool nnls(const String& algorithm, const Int niter, const Float tolerance,
00239             const String& model, 
00240             const String& fluxMask, const String& dataMask);
00241 
00242   // Fourier transform the model and componentlist
00243   Bool ft(const String& model, const String& transform);
00244 
00245   // Make an empty image
00246   Bool make(const String& model);
00247 
00248   // Make an empty image with just one Stokes pixel (ie, for a mask)
00249   Bool make1(const String& imagename);
00250 
00251   // Make an empty image modeled after templateImage
00252   Bool make(const String& model, ImageInterface<Float>& templateImage);
00253 
00254   // Make a Box Mask
00255   Bool boxmask(const String& boxmask,
00256                const Vector<Int> blc,
00257                const Vector<Int> trc,
00258                const Quantity& fillValue=1.0,
00259                const Quantity& externalValue=0.0);
00260 
00261   //make a mask image from regions
00262   Bool regionmask(const String& maskimage, Record* imageRegRec, 
00263                   Matrix<Quantity>& blctrcs, const Float& value=1.0);
00264 
00265   // Clip an image below some Stokes I threshold
00266   Bool clipimage(const String& clippedImage, const String& inputImage,
00267                  const Quantity& threshold);
00268 
00269   // Fit the psf
00270   Bool fitpsf(const String& psf, GaussianBeam& beam);
00271 
00272   // Convolve one image with another
00273   Bool convolve(const String& convolvedmodel, 
00274                 const String& model);
00275 
00276   // Make a Gaussian -- you might want to use it for convolution, etc
00277   Bool makegaussian(const String& gaussianimage, GaussianBeam& mbeam, Bool normalizeVolume);
00278 
00279   // ------------------  Multi-Term Deconvolver functions - START ----------------------
00280   // Initialize the Multi-Term Matrix Cleaners and compute Hessian elements.
00281   Bool mtopen(const Int nTaylor,
00282               const Vector<Float>& userScaleSizes,
00283               const Vector<String>& psfs);
00284 
00285   // Do component-finding iterations
00286   Bool mtclean(const Vector<String>& residuals,
00287                const Vector<String>& models,
00288                const Int niter,
00289                const Float gain, 
00290                const Quantity& threshold, 
00291                const Bool displayProgress,
00292                const String& mask, 
00293                Float& maxResidual, Int& iterationsDone);
00294 
00295   // Restore the output images
00296   Bool mtrestore(const Vector<String>& models,
00297                  const Vector<String>& residuals,
00298                  const Vector<String>& images,
00299                  GaussianBeam& mbeam);
00300 
00301   // Calculate alpha and beta from restored images.
00302   Bool mtcalcpowerlaw(const Vector<String>& images,
00303                       const Vector<String>& residuals,
00304                       const String& alphaname,
00305                       const String& betaname,
00306                       const Quantity& threshold,
00307                       const Bool calcerror);
00308 
00309 
00310   // ------------------  Multi-Term Deconvolver functions - END ----------------------
00311 
00312 
00313 private:
00314   
00315   // Cut the inner quarter out of an image
00316   SubImage<Float>* innerQuarter(PagedImage<Float>& in);
00317 
00318   // Return full image as a SubImage
00319   SubImage<Float>* allQuarters(PagedImage<Float>& in);
00320 
00321   // Clone an image
00322   Bool clone(const String& imageName, const String& newImageName);
00323   
00324 
00325   //find which axes are the spectral and pol one
00326   void findAxes();
00327 
00328   //check mask
00329 
00330   void checkMask(ImageInterface<Float>& maskimage, Int& xbeg, Int& xend, 
00331                  Int& ybeg, Int& yend);
00332 
00333 
00334   // setup lattice cleaner
00335   Bool setupLatCleaner(const String& algorithm,
00336                        const Int niter, const Float gain, const Quantity& threshold, 
00337                        const Bool displayProgress);
00338 
00339   // Embed a mask into an image. A convenience function.
00340   Bool createMask(LatticeExpr<Bool> &lemask, ImageInterface<Float> &outimage);
00341 
00342   PagedImage<Float>* dirty_p;
00343   PagedImage<Float>* psf_p;
00344 
00345   LatticeConvolver<Float>* convolver_p;
00346   ResidualEquation<Lattice<Float> >* residEqn_p;
00347   LatConvEquation* latConvEqn_p;
00348   CountedPtr <ImageMSCleaner> cleaner_p;
00349 
00350   Bool scalesValid_p;
00351 
00352   Int nx_p, ny_p, npol_p, nchan_p;
00353   Int chanAxis_p, polAxis_p;
00354   String mode_p;
00355   GaussianBeam beam_p;
00356 //  Quantity bmaj_p, bmin_p, bpa_p;
00357   Bool beamValid_p;
00358   String dirtyName_p;
00359   String psfName_p;
00360   Bool fullPlane_p;
00361 
00362   Vector<Float> itsTotalFluxScale;
00363   Float itsTotalFlux;
00364   Vector<Float> scaleSizes_p;
00365 
00366   // Multi-Term private variables
00367   Int mt_nterms_p;
00368   MultiTermMatrixCleaner mt_cleaner_p;
00369   Bool mt_valid_p;
00370 
00371   // Set the defaults
00372   void defaults();
00373 
00374   Bool removeTable(const String& tablename);
00375   
00376   // Prints an error message if the deconvolver DO is detached and returns True.
00377   Bool detached() const;
00378 
00379   String imageName() const;
00380 
00381   Bool valid() const;
00382 
00383 };
00384 
00385 } //# NAMESPACE CASA - END
00386 
00387 #endif