Line data Source code
1 : // -*- C++ -*-
2 : //# ConvFuncDiskCache.cc: Definition of the ConvFuncDiskCache class
3 : //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003
4 : //# Associated Universities, Inc. Washington DC, USA.
5 : //#
6 : //# This library is free software; you can redistribute it and/or modify it
7 : //# under the terms of the GNU Library General Public License as published by
8 : //# the Free Software Foundation; either version 2 of the License, or (at your
9 : //# option) any later version.
10 : //#
11 : //# This library is distributed in the hope that it will be useful, but WITHOUT
12 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 : //# License for more details.
15 : //#
16 : //# You should have received a copy of the GNU Library General Public License
17 : //# along with this library; if not, write to the Free Software Foundation,
18 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19 : //#
20 : //# Correspondence concerning AIPS++ should be addressed as follows:
21 : //# Internet email: casa-feedback@nrao.edu.
22 : //# Postal address: AIPS++ Project Office
23 : //# National Radio Astronomy Observatory
24 : //# 520 Edgemont Road
25 : //# Charlottesville, VA 22903-2475 USA
26 : //#
27 : //# $Id$
28 : #ifndef SYNTHESIS_TRANSFORM2_CFCACHE_H
29 : #define SYNTHESIS_TRANSFORM2_CFCACHE_H
30 :
31 : #include <casacore/casa/Arrays/Matrix.h>
32 : #include <msvis/MSVis/VisBuffer2.h>
33 : #include <casacore/images/Images/ImageInterface.h>
34 : #include <casacore/images/Images/TempImage.h>
35 : #include <casacore/images/Images/PagedImage.h>
36 : #include <casacore/casa/Arrays/Array.h>
37 : #include <casacore/casa/Arrays/Vector.h>
38 : #include <casacore/casa/Logging/LogIO.h>
39 : #include <casacore/casa/OS/Directory.h>
40 : #include <casacore/casa/Logging/LogSink.h>
41 : #include <casacore/casa/Logging/LogMessage.h>
42 : #include <casacore/lattices/Lattices/LatticeCache.h>
43 : #include <casacore/lattices/Lattices/ArrayLattice.h>
44 : #include <casacore/coordinates/Coordinates/DirectionCoordinate.h>
45 : #include <synthesis/TransformMachines/VPSkyJones.h>
46 : #include <synthesis/TransformMachines2/CFStore.h>
47 : #include <synthesis/TransformMachines2/CFDefs.h>
48 : #include <synthesis/TransformMachines2/CFStore2.h>
49 : #include <synthesis/TransformMachines2/Utils.h>
50 : // #include <casa/Tables/Table.h>
51 :
52 : namespace casa { //# NAMESPACE CASA - BEGIN
53 : namespace refim{
54 : using namespace CFDefs;
55 :
56 : // <summary>
57 : //
58 : // An object to manage the caches of pre-computed convolution
59 : // functions on the disk and in the memory.
60 : //
61 : // </summary>
62 :
63 : // <use visibility=export>
64 :
65 : // <reviewed reviewer="" date="" tests="" demos="">
66 :
67 : // <prerequisite>
68 : // CFStore class
69 : // </prerequisite>
70 : //
71 : // <etymology>
72 : //
73 : // CFCache is an object, to write convolution
74 : // functions from the memory cache to the disk cache, and
75 : // search/load the disk cache for convolution functions for a give
76 : // Parallactic Angle.
77 : //
78 : //</etymology>
79 : //
80 : // <synopsis>
81 : //
82 : // FTMachines uses pre-computed convolution functions for gridding
83 : // (FTMachine::put()) and prediction (FTMachine::get()). For
84 : // <linkto class=PBWProjectFT>PBWProjectFT</linkto>, the convolution
85 : // function computation is expensive. Once computed, it is
86 : // efficient to cache these functions as a function of Parallactic
87 : // Angle and the value of the w-term (if significant).
88 : //
89 : // CFCache class provides interface to the disk cache and
90 : // is used in <linkto class=PBWProjectFT>PBWProjectFT</linkto> to
91 : // search and load convolution functions from the disk. If a new
92 : // convolution function is computed in <linkto
93 : // class=PBWProjectFT>PBWProjectFT</linkto>, the disk cache is
94 : // updated using the services of this class as well.
95 : //
96 : // </synopsis>
97 : //
98 : // <example>
99 : // </example>
100 : //
101 : // <motivation>
102 : //
103 : // Factor out the code for managing convolution function caches
104 : // (memory and disk caches) from the FTMachine code. This is a
105 : // specialized service, and has nothing to do with the details of
106 : // gridding and prediction of visibilities (which is the function of
107 : // FTMachines).
108 : //
109 : // </motivation>
110 : //
111 : // <todo asof="2005/07/21">
112 : //
113 : // <ul> Generalize to handle convolution functions for inhomogeneous
114 : // arrays and multi-feed antennas.
115 : //
116 : // </todo>
117 : //
118 : //----------------------------------------------------------------------
119 : class CFCacheTable
120 : {
121 : public:
122 0 : CFCacheTable(): freqList(), wList(), muellerList(), cfNameList() {};
123 0 : ~CFCacheTable() {};
124 :
125 0 : CFCacheTable& operator=(const CFCacheTable& other)
126 : {
127 : // if (other != *this)
128 : {
129 0 : freqList = other.freqList;
130 0 : wList = other.wList;
131 0 : muellerList = other.muellerList;
132 0 : cfNameList = other.cfNameList;
133 : }
134 0 : return *this;
135 : }
136 :
137 : void init()
138 : {freqList.resize(0); wList.resize(0); muellerList.resize(0); cfNameList.resize(0);}
139 :
140 : std::vector<casacore::Double> freqList, wList;
141 : std::vector<casacore::Int> muellerList;
142 : std::vector<casacore::String> cfNameList;
143 : };
144 : //
145 : //----------------------------------------------------------------------
146 : //
147 : class CFCache
148 : {
149 : public:
150 : typedef casacore::Vector< CFStore > CFStoreCacheType;
151 : typedef casacore::Vector< CFStore2 > CFStoreCacheType2;
152 : typedef std::vector<CFCacheTable> CFCacheTableType;
153 0 : CFCache(const char *cfDir="CF"):
154 0 : memCache2_p(), memCacheWt2_p(),memCache_p(), memCacheWt_p(),
155 0 : cfCacheTable_p(), XSup(), YSup(), paList(),
156 0 : paList_p(), key2IndexMap(),
157 0 : Dir(""), WtImagePrefix(""), cfPrefix(cfDir), aux("aux.dat"), paCD_p(), avgPBReady_p(false),
158 0 : avgPBReadyQualifier_p(""), OTODone_p(false), loadPixBuf_p(casacore::True)
159 0 : {};
160 : CFCache& operator=(const CFCache& other);
161 : ~CFCache();
162 : //
163 : // Method to set the disk cache directory name
164 : //
165 0 : void setCacheDir(const char *dir) {Dir = casacore::String(dir);}
166 0 : casacore::String getCacheDir(CFCDirType dirType=USERDIR) {(void)dirType; return Dir;};
167 :
168 0 : void setWtImagePrefix(const char *prefix) {WtImagePrefix = prefix;}
169 : casacore::String getWtImagePrefix() {return WtImagePrefix;};
170 :
171 : void setLazyFill(const casacore::Bool& val);
172 : casacore::Bool isLazyFillOn() {return loadPixBuf_p;};
173 : //
174 : // Method to initialize the internal memory cache.
175 : //
176 : void initCache();
177 : void initCache2(casacore::Bool verbose=false, casacore::Float selectedPA=400.0, casacore::Float dPA=-1.0,
178 : casacore::String prefix=casacore::String(""));
179 : void initCacheFromList2(const casacore::String& path,
180 : const casacore::Vector<casacore::String>& cfFileNames,
181 : const casacore::Vector<casacore::String>& cfWtFileNames,
182 : casacore::Float selectedPA, casacore::Float dPA,
183 : const casacore::Int verbose=1);
184 : void initPolMaps(PolMapType& polMap, PolMapType& conjPolMap);
185 0 : inline casacore::Bool OTODone() {return OTODone_p;}
186 : //
187 : // Compute the size of the memory cache in bytes
188 : //
189 : casacore::Long size();
190 : //
191 : // Method to set the class to caluclate the differential
192 : // Parallactic Angle. The ParAngleChangeDetector also holds the
193 : // delta PA value (user defined).
194 : //
195 0 : void setPAChangeDetector(const ParAngleChangeDetector& paCD) {paCD_p = paCD;};
196 : //
197 : // Methods to cache the convolution function.
198 : //
199 : // Top level method interfacing with the CFStore object
200 : //-------------------------------------------------------------------
201 : void cacheConvFunction(CFStore& cfs,
202 : casacore::String nameQualifier="",casacore::Bool savePA=true)
203 : {cacheConvFunction(cfs.pa, cfs,nameQualifier,savePA);}
204 : //-------------------------------------------------------------------
205 : // One level lower - the Parallactic angle can be separately
206 : // provided.
207 : void cacheConvFunction(const casacore::Quantity pa, CFStore& cfs,
208 : casacore::String nameQualifier="",casacore::Bool savePA=true)
209 : {cacheConvFunction(pa.getValue("rad"), cfs, nameQualifier,savePA);}
210 : //-------------------------------------------------------------------
211 : // The Parallactic angle as a floating point number in radians.
212 : void cacheConvFunction(const casacore::Float pa, CFStore& cfs,
213 : casacore::String nameQualifier="",casacore::Bool savePA=true);
214 : //-------------------------------------------------------------------
215 : // Lowest level - all information about CFStore is explicitly
216 : // provided as basic types
217 : casacore::Int cacheConvFunction(casacore::Int which, const casacore::Float& pa, CFType& cf,
218 : casacore::CoordinateSystem& coords, casacore::CoordinateSystem& ftcoords,
219 : casacore::Int& convSize,
220 : casacore::Vector<casacore::Int>& xConvSupport, casacore::Vector<casacore::Int>& yConvSupport,
221 : casacore::Float convSampling, casacore::String nameQualifier="",casacore::Bool savePA=true);
222 : //-------------------------------------------------------------------
223 : // Methods to sarch for a convolution function in the caches (disk
224 : // or memory) for the give Parallactic Angle value.
225 : //
226 0 : casacore::Bool searchConvFunction(casacore::Int& which, const casacore::Quantity pa, const casacore::Quantity dPA )
227 0 : {return searchConvFunction(which, pa.getValue("rad"), dPA.getValue("rad"));};
228 :
229 : casacore::Bool searchConvFunction(casacore::Int& which, const casacore::Float pa, const casacore::Float dPA );
230 : //
231 : // Lower level method to load a convolution function from the disk.
232 : //
233 : casacore::Int loadFromDisk(casacore::Int where, casacore::Float pa, casacore::Float dPA,
234 : casacore::Int Nx, CFStoreCacheType & convFuncCache,
235 : CFStore& cfs, casacore::String nameQualifier="");
236 : //
237 : // Method to locate a convolution function for the given w-term
238 : // index and PA value. This is the top level function that must
239 : // be used by the clients. This uses searchConvFunction() and
240 : // loadFromDisk() methods and the private methods to return a
241 : // convolution function.
242 : //
243 : // Returns CFDefs::NOTCACHED if the convolution function was not
244 : // found in the cache, CFDefs::MEMCACHE or CFDefs::DISKCACHE if
245 : // the function was found in memory or disk cache respectively.
246 : //
247 : casacore::Int locateConvFunction(CFStore& cfs, CFStore& cftws, const casacore::Int Nw,
248 : const casacore::Quantity pa, const casacore::Quantity dPA,
249 : const casacore::Int mosXPos=0, const casacore::Int mosYPos=0)
250 : {return locateConvFunction(cfs, cftws, Nw,pa.getValue("rad"), dPA.getValue("rad"),mosXPos,mosYPos);};
251 :
252 : casacore::Int locateConvFunction(CFStore& cfs, const casacore::Int Nw,
253 : const casacore::Quantity pa, const casacore::Quantity dPA,
254 : const casacore::String& nameQualifier="",
255 : const casacore::Int mosXPos=0, const casacore::Int mosYPos=0)
256 : {return locateConvFunction(cfs, Nw,pa.getValue("rad"), dPA.getValue("rad"),nameQualifier, mosXPos,mosYPos);};
257 :
258 : casacore::Int locateConvFunction(CFStore& cfs, CFStore& cfwts,
259 : const casacore::Int Nw, const casacore::Float pa, const casacore::Float dPA,
260 : const casacore::Int mosXPos=0, const casacore::Int mosYPos=0);
261 :
262 : casacore::Int locateConvFunction(CFStore& cfs, const casacore::Int Nw, const casacore::Float pa, const casacore::Float dPA,
263 : const casacore::String& nameQualifier="",
264 : const casacore::Int mosXPos=0, const casacore::Int mosYPos=0);
265 :
266 : casacore::TableRecord getCFParams(const casacore::String& fileName,
267 : casacore::Array<casacore::Complex>& pixelBuffer,
268 : casacore::CoordinateSystem& coordSys,
269 : casacore::Double& sampling,
270 : casacore::Double& paVal,
271 : casacore::Int& xSupport, casacore::Int& ySupport,
272 : casacore::Double& fVal, casacore::Double& wVal, casacore::Int& mVal,
273 : casacore::Double& conjFreq, casacore::Int& conjPoln,
274 : casacore::Bool loadPixels=true);
275 : //
276 : // Methods to write the auxillary information from the memory
277 : // cache to the disk cache. Without this call, the disk cache
278 : // might not be complete. It is safe to call this method at
279 : // anytime during the life of this object.
280 : //
281 : void flush();
282 : void flush(casacore::ImageInterface<casacore::Float>& avgPB, casacore::String qualifier=casacore::String(""));
283 : ///cubeinfo tuple contains nchan and frequency of first channel
284 : casacore::Int loadAvgPB(casacore::ImageInterface<casacore::Float>& avgPB, casacore::String qualifier=casacore::String(""), std::tuple<int, double> cubeinfo=std::tuple<int,double>(1,-1.0));
285 0 : casacore::Int loadAvgPB(casacore::CountedPtr<casacore::ImageInterface<casacore::Float> > & avgPB, casacore::String qualifier=casacore::String(""), std::tuple<int, double> cubeinfo=std::tuple<int,double>(1,-1.0))
286 0 : {if (avgPB.null()) avgPB = new casacore::TempImage<casacore::Float>(); return loadAvgPB(*avgPB,qualifier,cubeinfo);};
287 :
288 : // loadAvgPB calls the method below if WtImgPrefix was set.
289 : casacore::Int loadWtImage(casacore::ImageInterface<casacore::Float>& avgPB, casacore::String qualifier, std::tuple<int, double> cubeinfo=std::tuple<int,double>(1,-1.0));
290 :
291 0 : casacore::Bool avgPBReady(const casacore::String& qualifier=casacore::String(""))
292 0 : {return (avgPBReady_p && (avgPBReadyQualifier_p == qualifier));};
293 :
294 : void summarize(CFStoreCacheType2& memCache, const casacore::String& message, const casacore::Bool cfsInfo=true);
295 :
296 : CFStoreCacheType2 memCache2_p, memCacheWt2_p;
297 :
298 : private:
299 : CFStoreCacheType memCache_p, memCacheWt_p;
300 : CFCacheTableType cfCacheTable_p;
301 :
302 : casacore::Matrix<casacore::Int> XSup, YSup;
303 : casacore::Vector<casacore::Float> paList, Sampling;
304 : std::vector<casacore::Float> paList_p;
305 : casacore::Matrix<casacore::Float> key2IndexMap; // Nx2 [PAVal, Freq]
306 : casacore::String Dir, WtImagePrefix, cfPrefix, aux;
307 : ParAngleChangeDetector paCD_p;
308 : //
309 : // Internal method to convert the direction co-ordinates of the
310 : // given casacore::CoordinateSystem to its Fourier conjuguate co-ordinates.
311 : //
312 : void makeFTCoordSys(const casacore::CoordinateSystem& coords,
313 : const casacore::Int& convSize,
314 : const casacore::Vector<casacore::Double>& ftRef,
315 : casacore::CoordinateSystem& ftCoords);
316 : //
317 : // Internal method to add the given convolution function to the
318 : // memory cache.
319 : //
320 : casacore::Int addToMemCache(CFStoreCacheType& cfCache,
321 : casacore::Float pa, CFType* cf, casacore::CoordinateSystem& coords,
322 : casacore::Vector<casacore::Int>& xConvSupport,
323 : casacore::Vector<casacore::Int>& yConvSupport,
324 : casacore::Float convSampling);
325 : CFStoreCacheType& getMEMCacheObj(const casacore::String& nameQualifier);
326 :
327 : void fillCFSFromDisk(const casacore::Directory dirObj, const casacore::String& pattern,
328 : CFStoreCacheType2& memStore, casacore::Bool showInfo=false,
329 : casacore::Float selectPAVal=400.0, casacore::Float dPA=-1.0,
330 : const casacore::Int verbose=1);
331 : void fillCFListFromDisk(const casacore::Vector<casacore::String>& fileNames, const casacore::String& CFCDir,
332 : CFStoreCacheType2& memStore,
333 : casacore::Bool showInfo, casacore::Float selectPAVal, casacore::Float dPA,
334 : const casacore::Int verbose=1);
335 :
336 : casacore::Bool avgPBReady_p;
337 : casacore::String avgPBReadyQualifier_p;
338 : casacore::Bool OTODone_p, loadPixBuf_p;
339 : };
340 : }
341 : }
342 : #endif
|