casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
CFCache.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //# ConvFuncDiskCache.cc: Definition of the ConvFuncDiskCache class
00003 //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003
00004 //# Associated Universities, Inc. Washington DC, USA.
00005 //#
00006 //# This library is free software; you can redistribute it and/or modify it
00007 //# under the terms of the GNU Library General Public License as published by
00008 //# the Free Software Foundation; either version 2 of the License, or (at your
00009 //# option) any later version.
00010 //#
00011 //# This library is distributed in the hope that it will be useful, but WITHOUT
00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 //# License for more details.
00015 //#
00016 //# You should have received a copy of the GNU Library General Public License
00017 //# along with this library; if not, write to the Free Software Foundation,
00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00019 //#
00020 //# Correspondence concerning AIPS++ should be addressed as follows:
00021 //#        Internet email: aips2-request@nrao.edu.
00022 //#        Postal address: AIPS++ Project Office
00023 //#                        National Radio Astronomy Observatory
00024 //#                        520 Edgemont Road
00025 //#                        Charlottesville, VA 22903-2475 USA
00026 //#
00027 //# $Id$
00028 #ifndef SYNTHESIS_CFCACHE_H
00029 #define SYNTHESIS_CFCACHE_H
00030 
00031 #include <casa/Arrays/Matrix.h>
00032 #include <synthesis/MSVis/VisBuffer.h>
00033 #include <images/Images/ImageInterface.h>
00034 #include <images/Images/TempImage.h>
00035 #include <images/Images/PagedImage.h>
00036 #include <casa/Arrays/Array.h>
00037 #include <casa/Arrays/Vector.h>
00038 #include <casa/Logging/LogIO.h>
00039 #include <casa/Logging/LogSink.h>
00040 #include <casa/Logging/LogMessage.h>
00041 #include <lattices/Lattices/LatticeCache.h>
00042 #include <lattices/Lattices/ArrayLattice.h>
00043 #include <coordinates/Coordinates/DirectionCoordinate.h>
00044 #include <synthesis/TransformMachines/VPSkyJones.h>
00045 #include <synthesis/TransformMachines/CFStore.h>
00046 #include <synthesis/TransformMachines/CFDefs.h>
00047 #include <synthesis/TransformMachines/Utils.h>
00048 // #include <casa/Tables/Table.h>
00049 
00050 namespace casa { //# NAMESPACE CASA - BEGIN
00051   using namespace CFDefs;
00052   // <summary> 
00053   //
00054   // An object to manage the caches of pre-computed convolution
00055   // functions on the disk and in the memory.
00056   //
00057   // </summary>
00058   
00059   // <use visibility=export>
00060   
00061   // <reviewed reviewer="" date="" tests="" demos="">
00062   
00063   // <prerequisite>
00064   //  CFStore class
00065   // </prerequisite>
00066   //
00067   // <etymology> 
00068   //
00069   // CFCache is an object, to write convolution
00070   // functions from the memory cache to the disk cache, and
00071   // search/load the disk cache for convolution functions for a give
00072   // Parallactic Angle.  
00073   //
00074   //</etymology>
00075   //
00076   // <synopsis> 
00077   //
00078   // FTMachines uses pre-computed convolution functions for gridding
00079   // (FTMachine::put()) and prediction (FTMachine::get()).  For
00080   // <linkto class=PBWProjectFT>PBWProjectFT</linkto>, the convolution
00081   // function computation is expensive.  Once computed, it is
00082   // efficient to cache these functions as a function of Parallactic
00083   // Angle and the value of the w-term (if significant). 
00084   //
00085   // CFCache class provides interface to the disk cache and
00086   // is used in <linkto class=PBWProjectFT>PBWProjectFT</linkto> to
00087   // search and load convolution functions from the disk.  If a new
00088   // convolution function is computed in <linkto
00089   // class=PBWProjectFT>PBWProjectFT</linkto>, the disk cache is
00090   // updated using the services of this class as well.
00091   //
00092   // </synopsis> 
00093   //
00094   // <example>
00095   // </example>
00096   //
00097   // <motivation>
00098   //
00099   // Factor out the code for managing convolution function caches
00100   // (memory and disk caches) from the FTMachine code.  This is a
00101   // specialized service, and has nothing to do with the details of
00102   // gridding and prediction of visibilities (which is the function of
00103   // FTMachines).
00104   //
00105   // </motivation>
00106   //
00107   // <todo asof="2005/07/21">
00108   //
00109   // <ul> Generalize to handle convolution functions for inhomogeneous
00110   // arrays and multi-feed antennas.
00111   //
00112   // </todo>
00113   //
00114   //----------------------------------------------------------------------
00115   class CFCacheTable
00116   {
00117   public:
00118     CFCacheTable(): freqList(), wList(), muellerList(), cfNameList() {};
00119     ~CFCacheTable() {};
00120     
00121     CFCacheTable& operator=(const CFCacheTable& other)
00122     {
00123       //      if (other != *this)
00124         {
00125           freqList = other.freqList;
00126           wList = other.wList;
00127           muellerList = other.muellerList;
00128           cfNameList = other.cfNameList;
00129         }
00130       return *this;
00131     }
00132 
00133     vector<Double> freqList, wList;
00134     vector<Int> muellerList;
00135     vector<String> cfNameList;
00136   };
00137   //
00138   //----------------------------------------------------------------------
00139   //
00140   class CFCache
00141   {
00142   public:
00143     typedef Vector< CFStore > CFStoreCacheType;
00144     typedef vector<CFCacheTable> CFCacheTableType;
00145     CFCache(const char *cfDir="CF"):
00146       memCache_p(), memCacheWt_p(), cfCacheTable_p(), XSup(), YSup(), paList(), 
00147       paList_p(), key2IndexMap(),
00148       Dir(""), cfPrefix(cfDir), aux("aux.dat"), paCD_p(), avgPBReady_p(False),
00149       avgPBReadyQualifier_p("")
00150     {};
00151     CFCache& operator=(const CFCache& other);
00152     ~CFCache();
00153     //
00154     // Method to set the disk cache directory name
00155     //
00156     void setCacheDir(const char *dir) {Dir = dir;}
00157     String getCacheDir() {return Dir;};
00158     //
00159     // Method to initialize the internal memory cache.
00160     //
00161     void initCache();
00162     void initCache2();
00163     //
00164     // Compute the size of the memory cache in bytes
00165     //
00166     Long size();
00167     //
00168     // Method to set the class to caluclate the differential
00169     // Parallactic Angle.  The ParAngleChangeDetector also holds the
00170     // delta PA value (user defined).
00171     //
00172     void setPAChangeDetector(const ParAngleChangeDetector& paCD) {paCD_p = paCD;};
00173     //
00174     // Methods to cache the convolution function.
00175     //
00176     // Top level method interfacing with the CFStore object
00177     //-------------------------------------------------------------------
00178     void cacheConvFunction(CFStore& cfs, 
00179                            String nameQualifier="",Bool savePA=True)
00180     {cacheConvFunction(cfs.pa, cfs,nameQualifier,savePA);}
00181     //-------------------------------------------------------------------
00182     // One level lower - the Parallactic angle can be separately
00183     // provided.
00184     void cacheConvFunction(const Quantity pa, CFStore& cfs,
00185                            String nameQualifier="",Bool savePA=True)
00186     {cacheConvFunction(pa.getValue("rad"), cfs, nameQualifier,savePA);}
00187     //-------------------------------------------------------------------
00188     // The Parallactic angle as a floating point number in radians.
00189     void cacheConvFunction(const Float pa, CFStore& cfs, 
00190                            String nameQualifier="",Bool savePA=True);
00191     //-------------------------------------------------------------------
00192     // Lowest level - all information about CFStore is explicitly
00193     // provided as basic types
00194     Int cacheConvFunction(Int which, const Float& pa, CFType& cf, 
00195                           CoordinateSystem& coords, CoordinateSystem& ftcoords, 
00196                           Int& convSize, 
00197                           Vector<Int>& xConvSupport, Vector<Int>& yConvSupport, 
00198                           Float convSampling, String nameQualifier="",Bool savePA=True);
00199     //-------------------------------------------------------------------
00200     // Methods to sarch for a convolution function in the caches (disk
00201     // or memory) for the give Parallactic Angle value.
00202     //
00203     Bool searchConvFunction(Int& which, const Quantity pa, const Quantity dPA )
00204     {return searchConvFunction(which, pa.getValue("rad"), dPA.getValue("rad"));};
00205 
00206     Bool searchConvFunction(Int& which, const Float pa, const Float dPA );
00207     //
00208     // Lower level method to load a convolution function from the disk.
00209     //
00210     Int loadFromDisk(Int where, Float pa, Float dPA,
00211                      Int Nx, CFStoreCacheType & convFuncCache,
00212                      CFStore& cfs, String nameQualifier="");
00213     //
00214     // Method to locate a convolution function for the given w-term
00215     // index and PA value.  This is the top level function that must
00216     // be used by the clients.  This uses searchConvFunction() and
00217     // loadFromDisk() methods and the private methods to return a
00218     // convolution function.
00219     //
00220     // Returns CFDefs::NOTCACHED if the convolution function was not
00221     // found in the cache, CFDefs::MEMCACHE or CFDefs::DISKCACHE if
00222     // the function was found in memory or disk cache respectively.
00223     //
00224     Int locateConvFunction(CFStore& cfs, CFStore& cftws, const Int Nw, 
00225                            const Quantity pa, const Quantity dPA, 
00226                            const Int mosXPos=0, const Int mosYPos=0)
00227     {return locateConvFunction(cfs, cftws, Nw,pa.getValue("rad"), dPA.getValue("rad"),mosXPos,mosYPos);};
00228 
00229     Int locateConvFunction(CFStore& cfs, const Int Nw, 
00230                            const Quantity pa, const Quantity dPA, 
00231                            const String& nameQualifier="",
00232                            const Int mosXPos=0, const Int mosYPos=0)
00233     {return locateConvFunction(cfs, Nw,pa.getValue("rad"), dPA.getValue("rad"),nameQualifier, mosXPos,mosYPos);};
00234 
00235     Int locateConvFunction(CFStore& cfs, CFStore& cfwts,
00236                            const Int Nw, const Float pa, const Float dPA,
00237                            const Int mosXPos=0, const Int mosYPos=0);
00238 
00239     Int locateConvFunction(CFStore& cfs, const Int Nw, const Float pa, const Float dPA, 
00240                            const String& nameQualifier="",
00241                            const Int mosXPos=0, const Int mosYPos=0);
00242     //
00243     // Methods to write the auxillary information from the memory
00244     // cache to the disk cache.  Without this call, the disk cache
00245     // might not be complete.  It is safe to call this method at
00246     // anytime during the life of this object.
00247     //
00248     void flush();
00249     void flush(ImageInterface<Float>& avgPB, String qualifier=String(""));
00250     Int loadAvgPB(ImageInterface<Float>& avgPB, String qualifier=String(""));
00251     Int loadAvgPB(CountedPtr<ImageInterface<Float> > & avgPB, String qualifier=String(""))
00252     {if (avgPB.null()) avgPB = new TempImage<Float>(); return loadAvgPB(*avgPB,qualifier);};
00253 
00254     Bool avgPBReady(const String& qualifier=String("")) 
00255     {return (avgPBReady_p && (avgPBReadyQualifier_p == qualifier));};
00256 
00257   private:
00258     CFStoreCacheType memCache_p, memCacheWt_p;
00259     CFCacheTableType cfCacheTable_p;
00260 
00261     Matrix<Int> XSup, YSup;
00262     Vector<Float> paList, Sampling;
00263     vector<Float> paList_p;
00264     Matrix<Float> key2IndexMap; // Nx2 [PAVal, Freq]
00265     String Dir, cfPrefix, aux;
00266     ParAngleChangeDetector paCD_p;
00267     //
00268     // Internal method to convert the direction co-ordinates of the
00269     // given CoordinateSystem to its Fourier conjuguate co-ordinates.
00270     //
00271     void makeFTCoordSys(const CoordinateSystem& coords,
00272                         const Int& convSize,
00273                         const Vector<Double>& ftRef,
00274                         CoordinateSystem& ftCoords);
00275     //
00276     // Internal method to add the given convolution function to the
00277     // memory cache.
00278     //
00279     Int addToMemCache(CFStoreCacheType& cfCache, 
00280                       Float pa, CFType* cf, CoordinateSystem& coords,
00281                       Vector<Int>& xConvSupport,
00282                       Vector<Int>& yConvSupport,
00283                       Float convSampling);
00284     CFStoreCacheType& getMEMCacheObj(const String& nameQualifier);
00285 
00286     Bool avgPBReady_p;
00287     String avgPBReadyQualifier_p;
00288   };
00289 }
00290 
00291 #endif