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_TRANSFORM2_CFCACHE_H
00029 #define SYNTHESIS_TRANSFORM2_CFCACHE_H
00030 
00031 #include <casa/Arrays/Matrix.h>
00032 #include <msvis/MSVis/VisBuffer2.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/OS/Directory.h>
00040 #include <casa/Logging/LogSink.h>
00041 #include <casa/Logging/LogMessage.h>
00042 #include <lattices/Lattices/LatticeCache.h>
00043 #include <lattices/Lattices/ArrayLattice.h>
00044 #include <coordinates/Coordinates/DirectionCoordinate.h>
00045 #include <synthesis/TransformMachines/VPSkyJones.h>
00046 #include <synthesis/TransformMachines2/CFStore.h>
00047 #include <synthesis/TransformMachines2/CFDefs.h>
00048 #include <synthesis/TransformMachines2/CFStore2.h>
00049 #include <synthesis/TransformMachines2/Utils.h>
00050 // #include <casa/Tables/Table.h>
00051 
00052 namespace casa { //# NAMESPACE CASA - BEGIN
00053   namespace refim{
00054   using namespace CFDefs;
00055 
00056   // <summary> 
00057   //
00058   // An object to manage the caches of pre-computed convolution
00059   // functions on the disk and in the memory.
00060   //
00061   // </summary>
00062   
00063   // <use visibility=export>
00064   
00065   // <reviewed reviewer="" date="" tests="" demos="">
00066   
00067   // <prerequisite>
00068   //  CFStore class
00069   // </prerequisite>
00070   //
00071   // <etymology> 
00072   //
00073   // CFCache is an object, to write convolution
00074   // functions from the memory cache to the disk cache, and
00075   // search/load the disk cache for convolution functions for a give
00076   // Parallactic Angle.  
00077   //
00078   //</etymology>
00079   //
00080   // <synopsis> 
00081   //
00082   // FTMachines uses pre-computed convolution functions for gridding
00083   // (FTMachine::put()) and prediction (FTMachine::get()).  For
00084   // <linkto class=PBWProjectFT>PBWProjectFT</linkto>, the convolution
00085   // function computation is expensive.  Once computed, it is
00086   // efficient to cache these functions as a function of Parallactic
00087   // Angle and the value of the w-term (if significant). 
00088   //
00089   // CFCache class provides interface to the disk cache and
00090   // is used in <linkto class=PBWProjectFT>PBWProjectFT</linkto> to
00091   // search and load convolution functions from the disk.  If a new
00092   // convolution function is computed in <linkto
00093   // class=PBWProjectFT>PBWProjectFT</linkto>, the disk cache is
00094   // updated using the services of this class as well.
00095   //
00096   // </synopsis> 
00097   //
00098   // <example>
00099   // </example>
00100   //
00101   // <motivation>
00102   //
00103   // Factor out the code for managing convolution function caches
00104   // (memory and disk caches) from the FTMachine code.  This is a
00105   // specialized service, and has nothing to do with the details of
00106   // gridding and prediction of visibilities (which is the function of
00107   // FTMachines).
00108   //
00109   // </motivation>
00110   //
00111   // <todo asof="2005/07/21">
00112   //
00113   // <ul> Generalize to handle convolution functions for inhomogeneous
00114   // arrays and multi-feed antennas.
00115   //
00116   // </todo>
00117   //
00118   //----------------------------------------------------------------------
00119   class CFCacheTable
00120   {
00121   public:
00122     CFCacheTable(): freqList(), wList(), muellerList(), cfNameList() {};
00123     ~CFCacheTable() {};
00124     
00125     CFCacheTable& operator=(const CFCacheTable& other)
00126     {
00127       //      if (other != *this)
00128         {
00129           freqList = other.freqList;
00130           wList = other.wList;
00131           muellerList = other.muellerList;
00132           cfNameList = other.cfNameList;
00133         }
00134       return *this;
00135     }
00136 
00137     void init()
00138     {freqList.resize(0); wList.resize(0); muellerList.resize(0); cfNameList.resize(0);}
00139 
00140     vector<Double> freqList, wList;
00141     vector<Int> muellerList;
00142     vector<String> cfNameList;
00143   };
00144   //
00145   //----------------------------------------------------------------------
00146   //
00147   class CFCache
00148   {
00149   public:
00150     typedef Vector< CFStore > CFStoreCacheType;
00151     typedef Vector< CFStore2 > CFStoreCacheType2;
00152     typedef vector<CFCacheTable> CFCacheTableType;
00153     CFCache(const char *cfDir="CF"):
00154       memCache2_p(), memCacheWt2_p(),memCache_p(), memCacheWt_p(), 
00155       cfCacheTable_p(), XSup(), YSup(), paList(), 
00156       paList_p(), key2IndexMap(),
00157       Dir(""), WtImagePrefix(""), cfPrefix(cfDir), aux("aux.dat"), paCD_p(), avgPBReady_p(False),
00158       avgPBReadyQualifier_p(""), OTODone_p(False)
00159     {};
00160     CFCache& operator=(const CFCache& other);
00161     ~CFCache();
00162     //
00163     // Method to set the disk cache directory name
00164     //
00165     void setCacheDir(const char *dir) {Dir = String(dir);}
00166     String getCacheDir() {return Dir;};
00167 
00168     void setWtImagePrefix(const char *prefix) {WtImagePrefix = prefix;}
00169     String getWtImagePrefix() {return WtImagePrefix;};
00170     //
00171     // Method to initialize the internal memory cache.
00172     //
00173     void initCache();
00174     void initCache2(Bool verbose=False, Float selectedPA=400.0, Float dPA=-1.0);
00175     void initCacheFromList2(const String& path, 
00176                             const Vector<String>& cfFileNames, 
00177                             const Vector<String>& cfWtFileNames, 
00178                             Float selectedPA, Float dPA,
00179                             const Int verbose=1);
00180     void initPolMaps(PolMapType& polMap, PolMapType& conjPolMap);
00181     inline Bool OTODone() {return OTODone_p;}
00182     //
00183     // Compute the size of the memory cache in bytes
00184     //
00185     Long size();
00186     //
00187     // Method to set the class to caluclate the differential
00188     // Parallactic Angle.  The ParAngleChangeDetector also holds the
00189     // delta PA value (user defined).
00190     //
00191     void setPAChangeDetector(const ParAngleChangeDetector& paCD) {paCD_p = paCD;};
00192     //
00193     // Methods to cache the convolution function.
00194     //
00195     // Top level method interfacing with the CFStore object
00196     //-------------------------------------------------------------------
00197     void cacheConvFunction(CFStore& cfs, 
00198                            String nameQualifier="",Bool savePA=True)
00199     {cacheConvFunction(cfs.pa, cfs,nameQualifier,savePA);}
00200     //-------------------------------------------------------------------
00201     // One level lower - the Parallactic angle can be separately
00202     // provided.
00203     void cacheConvFunction(const Quantity pa, CFStore& cfs,
00204                            String nameQualifier="",Bool savePA=True)
00205     {cacheConvFunction(pa.getValue("rad"), cfs, nameQualifier,savePA);}
00206     //-------------------------------------------------------------------
00207     // The Parallactic angle as a floating point number in radians.
00208     void cacheConvFunction(const Float pa, CFStore& cfs, 
00209                            String nameQualifier="",Bool savePA=True);
00210     //-------------------------------------------------------------------
00211     // Lowest level - all information about CFStore is explicitly
00212     // provided as basic types
00213     Int cacheConvFunction(Int which, const Float& pa, CFType& cf, 
00214                           CoordinateSystem& coords, CoordinateSystem& ftcoords, 
00215                           Int& convSize, 
00216                           Vector<Int>& xConvSupport, Vector<Int>& yConvSupport, 
00217                           Float convSampling, String nameQualifier="",Bool savePA=True);
00218     //-------------------------------------------------------------------
00219     // Methods to sarch for a convolution function in the caches (disk
00220     // or memory) for the give Parallactic Angle value.
00221     //
00222     Bool searchConvFunction(Int& which, const Quantity pa, const Quantity dPA )
00223     {return searchConvFunction(which, pa.getValue("rad"), dPA.getValue("rad"));};
00224 
00225     Bool searchConvFunction(Int& which, const Float pa, const Float dPA );
00226     //
00227     // Lower level method to load a convolution function from the disk.
00228     //
00229     Int loadFromDisk(Int where, Float pa, Float dPA,
00230                      Int Nx, CFStoreCacheType & convFuncCache,
00231                      CFStore& cfs, String nameQualifier="");
00232     //
00233     // Method to locate a convolution function for the given w-term
00234     // index and PA value.  This is the top level function that must
00235     // be used by the clients.  This uses searchConvFunction() and
00236     // loadFromDisk() methods and the private methods to return a
00237     // convolution function.
00238     //
00239     // Returns CFDefs::NOTCACHED if the convolution function was not
00240     // found in the cache, CFDefs::MEMCACHE or CFDefs::DISKCACHE if
00241     // the function was found in memory or disk cache respectively.
00242     //
00243     Int locateConvFunction(CFStore& cfs, CFStore& cftws, const Int Nw, 
00244                            const Quantity pa, const Quantity dPA, 
00245                            const Int mosXPos=0, const Int mosYPos=0)
00246     {return locateConvFunction(cfs, cftws, Nw,pa.getValue("rad"), dPA.getValue("rad"),mosXPos,mosYPos);};
00247 
00248     Int locateConvFunction(CFStore& cfs, const Int Nw, 
00249                            const Quantity pa, const Quantity dPA, 
00250                            const String& nameQualifier="",
00251                            const Int mosXPos=0, const Int mosYPos=0)
00252     {return locateConvFunction(cfs, Nw,pa.getValue("rad"), dPA.getValue("rad"),nameQualifier, mosXPos,mosYPos);};
00253 
00254     Int locateConvFunction(CFStore& cfs, CFStore& cfwts,
00255                            const Int Nw, const Float pa, const Float dPA,
00256                            const Int mosXPos=0, const Int mosYPos=0);
00257 
00258     Int locateConvFunction(CFStore& cfs, const Int Nw, const Float pa, const Float dPA, 
00259                            const String& nameQualifier="",
00260                            const Int mosXPos=0, const Int mosYPos=0);
00261 
00262     TableRecord getCFParams(const String& fileName,
00263                      Array<Complex>& pixelBuffer,
00264                      CoordinateSystem& coordSys, 
00265                      Double& sampling,
00266                      Double& paVal,
00267                      Int& xSupport, Int& ySupport,
00268                      Double& fVal, Double& wVal, Int& mVal,
00269                      Double& conjFreq, Int& conjPoln,
00270                      Bool loadPixels=True);
00271     //
00272     // Methods to write the auxillary information from the memory
00273     // cache to the disk cache.  Without this call, the disk cache
00274     // might not be complete.  It is safe to call this method at
00275     // anytime during the life of this object.
00276     //
00277     void flush();
00278     void flush(ImageInterface<Float>& avgPB, String qualifier=String(""));
00279     Int loadAvgPB(ImageInterface<Float>& avgPB, String qualifier=String(""));
00280     Int loadAvgPB(CountedPtr<ImageInterface<Float> > & avgPB, String qualifier=String(""))
00281     {if (avgPB.null()) avgPB = new TempImage<Float>(); return loadAvgPB(*avgPB,qualifier);};
00282 
00283     // loadAvgPB calls the method below if WtImgPrefix was set.
00284     Int loadWtImage(ImageInterface<Float>& avgPB, String qualifier);
00285 
00286     Bool avgPBReady(const String& qualifier=String("")) 
00287     {return (avgPBReady_p && (avgPBReadyQualifier_p == qualifier));};
00288 
00289     void summarize(CFStoreCacheType2& memCache, const String& message, const Bool cfsInfo=True);
00290 
00291     CFStoreCacheType2 memCache2_p, memCacheWt2_p;
00292 
00293   private:
00294     CFStoreCacheType memCache_p, memCacheWt_p;
00295     CFCacheTableType cfCacheTable_p;
00296 
00297     Matrix<Int> XSup, YSup;
00298     Vector<Float> paList, Sampling;
00299     vector<Float> paList_p;
00300     Matrix<Float> key2IndexMap; // Nx2 [PAVal, Freq]
00301     String Dir, WtImagePrefix, cfPrefix, aux;
00302     ParAngleChangeDetector paCD_p;
00303     //
00304     // Internal method to convert the direction co-ordinates of the
00305     // given CoordinateSystem to its Fourier conjuguate co-ordinates.
00306     //
00307     void makeFTCoordSys(const CoordinateSystem& coords,
00308                         const Int& convSize,
00309                         const Vector<Double>& ftRef,
00310                         CoordinateSystem& ftCoords);
00311     //
00312     // Internal method to add the given convolution function to the
00313     // memory cache.
00314     //
00315     Int addToMemCache(CFStoreCacheType& cfCache, 
00316                       Float pa, CFType* cf, CoordinateSystem& coords,
00317                       Vector<Int>& xConvSupport,
00318                       Vector<Int>& yConvSupport,
00319                       Float convSampling);
00320     CFStoreCacheType& getMEMCacheObj(const String& nameQualifier);
00321 
00322     void fillCFSFromDisk(const Directory dirObj, const String& pattern, 
00323                          CFStoreCacheType2& memStore, Bool showInfo=False, 
00324                          Float selectPAVal=400.0, Float dPA=-1.0,
00325                          const Int verbose=1);
00326     void fillCFListFromDisk(const Vector<String>& fileNames, const String& CFCDir,
00327                             CFStoreCacheType2& memStore,
00328                             Bool showInfo, Float selectPAVal, Float dPA,
00329                             const Int verbose=1);
00330 
00331     Bool avgPBReady_p;
00332     String avgPBReadyQualifier_p;
00333     Bool OTODone_p;
00334   };
00335 }
00336 }
00337 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1