LCOV - code coverage report
Current view: top level - synthesis/TransformMachines - CFCache.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 571 0.0 %
Date: 2024-10-10 11:40:37 Functions: 0 25 0.0 %

          Line data    Source code
       1             : // -*- C++ -*-
       2             : //# CFCache.cc: Implementation of the CFCache 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             : #include <synthesis/TransformMachines/SynthesisError.h>
      29             : #include <synthesis/TransformMachines/CFCache.h>
      30             : #include <synthesis/TransformMachines/Utils.h>
      31             : #include <casacore/lattices/LEL/LatticeExpr.h>
      32             : #include <casacore/casa/System/ProgressMeter.h>
      33             : #include <casacore/casa/Exceptions/Error.h>
      34             : #include <casacore/casa/Utilities/Regex.h>
      35             : #include <fstream>
      36             : #include <algorithm>
      37             : // #include <tables/Tables/TableDesc.h>
      38             : // #include <tables/Tables/SetupNewTab.h>
      39             : // #include <tables/Tables/Table.h>
      40             : 
      41             : using namespace casacore;
      42             : namespace casa{
      43           0 :   CFCache::~CFCache()  
      44             :   {
      45             :     //cerr << "#################" << "~CFCache() called" << endl;
      46           0 :   }
      47             :   //
      48             :   //-------------------------------------------------------------------------
      49             :   // Load just the axillary info. if found.  The convolution functions
      50             :   // are loaded on-demand.
      51             :   //
      52           0 :   void CFCache::initCache()
      53             :   {
      54           0 :     LogOrigin logOrigin("CFCache", "initCache");
      55           0 :     LogIO log_l(logOrigin);
      56             : 
      57           0 :     ostringstream name;
      58           0 :     String line;
      59           0 :     Directory dirObj(Dir);
      60             : 
      61           0 :     if (Dir.length() == 0) 
      62           0 :       throw(SynthesisFTMachineError(LogMessage("Got null string for disk cache dir. ",
      63           0 :                                                logOrigin).message()));
      64             :     //
      65             :     // If the directory does not exist, create it
      66             :     //
      67           0 :     if (!dirObj.exists()) dirObj.create();
      68           0 :     else if ((!dirObj.isWritable()) || (!dirObj.isReadable()))
      69             :       {
      70           0 :         throw(SynthesisFTMachineError(String("Directory \"")+Dir+String("\"")+
      71           0 :                                       String(" for convolution function cache"
      72           0 :                                              " exists but is unreadable/unwriteable")));
      73             :       }
      74             : 
      75             :     try
      76             :       {
      77           0 :         name << Dir << "/" << aux;
      78           0 :         File file(name);
      79           0 :         Int Npa=0,Nw=0;
      80           0 :         ifstream aux;
      81           0 :         Bool readFromFile=false;
      82           0 :         if (file.exists() && file.isRegular()) 
      83             :           {
      84           0 :             readFromFile=true;
      85           0 :             aux.open(name.str().c_str());
      86           0 :             if (readFromFile && aux.good()) aux >> Npa >> Nw;
      87             :             else
      88           0 :               throw(SynthesisFTMachineError(string("Error while reading convolution "
      89           0 :                                                    "function cache file ") + name.str( )));
      90             :           }
      91             : 
      92           0 :         if (Npa > 0)
      93             :           {
      94           0 :             paList.resize(Npa,true);
      95             : 
      96           0 :             IPosition s(2,Nw,Npa);
      97           0 :             XSup.resize(s,true);
      98           0 :             YSup.resize(s,true);
      99           0 :             Sampling.resize(Npa,true);
     100           0 :             for(Int i=0;i<Npa;i++)
     101             :               {
     102             :                 Float pa, S;
     103             :                 Int XS, YS;
     104           0 :                 s[2]=i;
     105           0 :                 aux >> pa;
     106           0 :                 for(Int iw=0;iw<Nw;iw++)
     107             :                   {
     108           0 :                     s[0]=iw;
     109           0 :                     aux >> XS >> YS;
     110           0 :                     YS = XS;
     111           0 :                     paList[i] = pa*M_PI/180.0;
     112           0 :                     XSup(iw,i)=XS;
     113           0 :                     YSup(iw,i)=YS;
     114             :                   }
     115           0 :                 aux >> S;
     116           0 :                 Sampling[i]=S;
     117             :               }
     118           0 :           }
     119           0 :       }
     120           0 :     catch(AipsError& x)
     121             :       {
     122           0 :         throw(SynthesisFTMachineError(String("Error while initializing CF disk cache: ")
     123           0 :                                       +x.getMesg()));
     124           0 :       }
     125           0 :   }
     126             :   //
     127             :   //-----------------------------------------------------------------------
     128             :   //
     129           0 :   void CFCache::initPolMaps(PolMapType& polMap, PolMapType& conjPolMap)
     130             :   {
     131           0 :     if (OTODone()==false)
     132             :       {
     133           0 :         for(Int i=0;i<(Int)memCache2_p.nelements();i++)
     134           0 :           memCache2_p[i].initPolMaps(polMap, conjPolMap);
     135           0 :         for(Int i=0;i<(Int)memCacheWt2_p.nelements();i++)
     136           0 :           memCacheWt2_p[i].initPolMaps(polMap, conjPolMap);
     137           0 :         OTODone_p=true;
     138             :       }
     139           0 :   }
     140             :   //
     141             :   //-----------------------------------------------------------------------
     142             :   //
     143           0 :   void CFCache::summarize(CFStoreCacheType2& memStore, const String& message, const Bool cfsInfo)
     144             :   {
     145           0 :     LogOrigin logOrigin("CFCache", "summarize");
     146           0 :     LogIO log_l(logOrigin);
     147             : 
     148           0 :     IPosition cfsShp=memStore[0].getShape();
     149           0 :     Int ipol=0;
     150             : 
     151           0 :     if (cfsInfo)
     152             :       {
     153           0 :         log_l << "PA: ";
     154           0 :         for (Int iBL=0; iBL<cfsShp(1); iBL++)
     155           0 :           for (Int iPA=0; iPA<cfsShp(0); iPA++)
     156             :             {
     157           0 :               Quantity pa; Int ant1, ant2;
     158           0 :               memStore[0].getParams(pa, ant1, ant2, iPA, iBL);
     159           0 :               log_l << pa.getValue("deg") << " ";
     160           0 :             }
     161           0 :         log_l << LogIO::POST;
     162             :       }
     163           0 :     log_l << message << LogIO::POST;
     164           0 :     for(Int iBL=0; iBL<cfsShp(1); iBL++)
     165           0 :       for(Int iPA=0; iPA<cfsShp(0); iPA++)
     166             :         {
     167           0 :           CFBuffer& cfb=memStore[0](iPA,iBL);
     168           0 :           IPosition cfbShp=cfb.getShape();
     169           0 :           for (Int iw=0; iw<cfbShp[1]; iw++)
     170             :             {
     171           0 :               log_l << "Support Size (w:"<< iw << ", PA:" << iPA << ", BL:" << iBL << ", C:*): ";
     172             :               {
     173           0 :                 for (Int inu=0; inu<cfbShp[0]; inu++)
     174             :                   {
     175           0 :                     CFCell& cc=cfb(inu,iw,ipol);
     176           0 :                     if (!cc.storage_p.null())
     177           0 :                       log_l << cfb(inu, iw, ipol).xSupport_p << " ";//<< "("<<inu<<") ";
     178             :                   }
     179           0 :                 log_l << LogIO::POST;
     180             :               }
     181             :             }
     182           0 :         }
     183             :     // for(Int iBL=0; iBL<cfsShp(1); iBL++)
     184             :     //   for(Int iPA=0; iPA<cfsShp(0); iPA++)
     185             :     //  {
     186             :     //    CFBuffer& cfb=memStore[0](iPA,iBL);
     187             :     //    IPosition cfbShp=cfb.getShape();
     188             :     //    for (Int iw=0; iw<cfbShp[1]; iw++)
     189             :     //      {
     190             :     //        log_l << "Support Size (w:"<< iw << ", PA:" << iPA << ", BL:" << iBL << ", C:*): ";
     191             :     //        {
     192             :     //          for (Int inu=0; inu<cfbShp[0]; inu++)
     193             :     //            {
     194             :     //              CFCell& cc=cfb(inu,iw,ipol);
     195             :     //              if (!cc.storage_p.null())
     196             :     //                {
     197             :     //                  log_l << cfb(inu, iw, ipol).cfShape_p[0] << "("<<inu<<") ";
     198             :     //                  log_l << cc.storage_p->shape()[0] << "(" <<inu<<") ";
     199             :     //                }
     200             :     //            }
     201             :     //          log_l << LogIO::POST;
     202             :     //        }
     203             :     //      }
     204             :     //  }
     205           0 :   }
     206             :   //
     207             :   //-----------------------------------------------------------------------
     208             :   //
     209             :   // By default (i.e., when called without any agruments), load all
     210             :   // the CFs found in the CF disk cache.
     211           0 :   void CFCache::initCacheFromList2(const String& path, 
     212             :                                    const Vector<String>& cfFileNames, 
     213             :                                    const Vector<String>& cfWtFileNames, 
     214             :                                    Float selectedPA, Float dPA,
     215             :                                    const Int verbose)
     216             :   {
     217           0 :     Vector<String> cf, wtcf;
     218           0 :     cf=cfFileNames;
     219           0 :     wtcf=cfWtFileNames;
     220           0 :     fillCFListFromDisk(cf, path, memCache2_p, true, selectedPA, dPA,verbose);
     221           0 :     fillCFListFromDisk(wtcf, path, memCacheWt2_p, false, selectedPA, dPA, verbose);
     222           0 :     memCache2_p[0].primeTheCFB();
     223           0 :     memCacheWt2_p[0].primeTheCFB();
     224           0 :     if (verbose > 0) summarize(memCache2_p,   "CFS",   true);
     225             :     //summarize(memCacheWt2_p, "WTCFS", false);
     226           0 :   }
     227             : 
     228           0 :   void CFCache::setLazyFill(const Bool& lazyFill)
     229             :   {
     230           0 :     loadPixBuf_p=!lazyFill;
     231           0 :     if (lazyFill)
     232             :       {
     233           0 :         LogIO os( LogOrigin("CFCache","setLazyFill",WHERE));
     234           0 :         os << "Lazy fill is On" << LogIO::POST;
     235           0 :       }
     236           0 :   }
     237             : 
     238           0 :   void CFCache::initCache2(Bool verbose, Float selectedPA, Float dPA)
     239             :   {
     240           0 :     LogOrigin logOrigin("CFCache", "initCache2");
     241           0 :     LogIO log_l(logOrigin);
     242             : 
     243           0 :     Directory dirObj(Dir);
     244             : 
     245           0 :     if (Dir.length() == 0) 
     246           0 :       throw(SynthesisFTMachineError(LogMessage("Got null string for disk cache dir. ",
     247           0 :                                                logOrigin).message()));
     248             :     //
     249             :     // If the directory does not exist, create it
     250             :     //
     251           0 :     if (!dirObj.exists()) dirObj.create();
     252           0 :     else if ((!dirObj.isWritable()) || (!dirObj.isReadable()))
     253             :       {
     254           0 :         throw(SynthesisFTMachineError(String("Directory \"")+Dir+String("\"")+
     255           0 :                                       String(" for convolution function cache"
     256           0 :                                              " exists but is unreadable/unwriteable")));
     257             :       }
     258             : 
     259           0 :     fillCFSFromDisk(dirObj,"CFS*", memCache2_p, true, selectedPA, dPA, verbose);
     260           0 :     fillCFSFromDisk(dirObj,"WTCFS*", memCacheWt2_p, false, selectedPA, dPA, verbose);
     261           0 :     memCache2_p[0].primeTheCFB();
     262           0 :     memCacheWt2_p[0].primeTheCFB();
     263             : 
     264             :     Double memUsed0,memUsed1;
     265           0 :     String memUnit="B";
     266           0 :     memUsed0=memCache2_p[0].memUsage();
     267           0 :     memUsed1=memCacheWt2_p[0].memUsage();
     268           0 :     if (memUsed0 > 1024.0)
     269             :       {
     270           0 :         memUsed0 /= 1024.0;
     271           0 :         memUsed1 /= 1024.0;
     272           0 :         memUnit="KB";
     273             :       }
     274             : 
     275           0 :     summarize(memCache2_p,   "CFS",   true);
     276           0 :     summarize(memCacheWt2_p, "WTCFS", false);
     277             : 
     278           0 :     if (memUsed0+memUsed1 > 0)
     279           0 :       log_l << "Total CF Cache memory footprint: " << (memUsed0+memUsed1) << " (" << memUsed0 << "," << memUsed1 << ") " << memUnit << LogIO::POST;
     280           0 :   }
     281             :   //
     282             :   //-----------------------------------------------------------------------
     283             :   //
     284           0 :   void CFCache::fillCFListFromDisk(const Vector<String>& fileNames, 
     285             :                                    const String& CFCDir, CFStoreCacheType2& memStore,
     286             :                                    Bool showInfo, Float selectPAVal, Float dPA,
     287             :                                    const Int verbose)
     288             :   {
     289           0 :     LogOrigin logOrigin("CFCache", "fillCFListFromDisk");
     290           0 :     LogIO log_l(logOrigin);
     291             :     (void)showInfo;
     292           0 :     Bool selectPA = (fabs(selectPAVal) <= 360.0);
     293             :     try
     294             :       {
     295           0 :         if (memStore.nelements() == 0) memStore.resize(1,true);
     296           0 :         memStore[0].setLazyFill(!loadPixBuf_p);
     297           0 :         memStore[0].setCFCacheDir(getCacheDir());
     298           0 :         CFCacheTableType cfCacheTable_l;
     299             : 
     300           0 :         if (fileNames.nelements() > 0)
     301             :           {
     302             :             //
     303             :             // Gather the list of PA values
     304             :             //
     305             :             {
     306           0 :               ProgressMeter pm(1.0, Double(fileNames.nelements()),
     307           0 :                                "Reading CFCache aux. info.", "","","",true);
     308           0 :               for (uInt i=0; i < fileNames.nelements(); i++)
     309             :                 {
     310           0 :                   PagedImage<Complex> thisCF(CFCDir+'/'+fileNames[i]);
     311           0 :                   TableRecord miscinfo = thisCF.miscInfo();
     312             :                   Double  paVal;
     313             :                   //UNUSED: Double  wVal; Int mVal;
     314           0 :                   miscinfo.get("ParallacticAngle",paVal);
     315           0 :                   paList_p.push_back(paVal);
     316           0 :                   pm.update(Double(i));
     317           0 :                 }
     318           0 :             }
     319             :             //
     320             :             // Make the PA-value list unique
     321             :             //
     322           0 :             sort( paList_p.begin(), paList_p.end() );
     323           0 :             paList_p.erase( unique( paList_p.begin(), paList_p.end() ), paList_p.end() );
     324           0 :             cfCacheTable_l.resize(paList_p.size());
     325             : 
     326             :             //      
     327             :             // For each CF, load the PA, Muelller element, WValue and
     328             :             // the Ref. Freq.  Insert these values in the lists in the
     329             :             // cfCacheTable
     330             :             //
     331           0 :             Array<Complex> pixBuf;
     332             : 
     333           0 :             TableRecord miscInfo;
     334             :             {
     335           0 :               ProgressMeter pm(1.0, Double(fileNames.nelements()),
     336           0 :                                "Loading CFs", "","","",true);
     337           0 :               for (uInt i=0; i < fileNames.nelements(); i++)
     338             :                 {
     339             :                   Double paVal, wVal, fVal, sampling, conjFreq; Int mVal, xSupport, ySupport, conjPoln;
     340           0 :                   CoordinateSystem coordSys;
     341             : 
     342           0 :                   miscInfo = SynthesisUtils::getCFParams(Dir,fileNames[i], pixBuf, coordSys,  sampling, paVal, 
     343           0 :                               xSupport, ySupport, fVal, wVal, mVal,conjFreq, conjPoln,false);
     344             :                 
     345           0 :                   Bool pickThisCF=true;
     346           0 :                   if (selectPA) pickThisCF = (fabs(paVal - selectPAVal) <= dPA);
     347           0 :                   if (pickThisCF)
     348             :                     {
     349           0 :                       Int ipos; SynthesisUtils::stdNearestValue(paList_p, (Float)paVal,ipos);
     350           0 :                       uInt paPos=ipos;
     351             :                   
     352           0 :                       if (paPos < paList_p.size())
     353             :                         {
     354           0 :                           cfCacheTable_l[paPos].freqList.push_back(fVal);
     355           0 :                           cfCacheTable_l[paPos].wList.push_back(wVal);
     356           0 :                           cfCacheTable_l[paPos].muellerList.push_back(mVal);
     357           0 :                           cfCacheTable_l[paPos].cfNameList.push_back(fileNames[i]);
     358             :                           //cerr << paPos << " " << fileNames[i] << endl;
     359             :                         }
     360             :                     }
     361           0 :                   pm.update(Double(fileNames.nelements()));
     362           0 :                 }
     363           0 :             }
     364           0 :             for (uInt ipa=0; ipa < cfCacheTable_l.size(); ipa++)
     365             :               {
     366             :                 //
     367             :                 // Resize the CFStore (poorly named private variable
     368             :                 // memCache2_p) to add CFBuffer for the each entry in
     369             :                 // the paList.
     370             :                 //
     371           0 :                 vector<String> fileNames(cfCacheTable_l[ipa].cfNameList);
     372             : 
     373           0 :                 Quantity paQuant(paList_p[ipa],"deg"), dPA(1.0,"deg");
     374           0 :                 memStore[0].resize(paQuant, dPA, 0,0);
     375           0 :                 CountedPtr<CFBuffer> cfb=memStore[0].getCFBuffer(paQuant, dPA, 0, 0);
     376             : 
     377             :                 //
     378             :                 // Get the list of f, w, mVals from cfCacheTable_l for
     379             :                 // the current ipa index.  Sort them.  And convert
     380             :                 // them into a list of unique entires.
     381             :                 //
     382           0 :                 vector<Double> fList(cfCacheTable_l[ipa].freqList), 
     383           0 :                   wList(cfCacheTable_l[ipa].wList);
     384           0 :                 vector<Int> mList(cfCacheTable_l[ipa].muellerList);
     385           0 :                 sort( fList.begin(), fList.end() );
     386           0 :                 sort( wList.begin(), wList.end() );
     387           0 :                 sort( mList.begin(), mList.end() );
     388           0 :                 fList.erase(SynthesisUtils::Unique(fList.begin(), fList.end()), fList.end());
     389           0 :                 wList.erase(SynthesisUtils::Unique(wList.begin(), wList.end()), wList.end());
     390           0 :                 mList.erase(SynthesisUtils::Unique(mList.begin(), mList.end()), mList.end());
     391           0 :                 PolMapType muellerElements;
     392           0 :                 Int npol=mList.size();
     393           0 :                 muellerElements.resize(npol);
     394           0 :                 for (Int ii=0;ii<npol;ii++)
     395             :                   {
     396           0 :                     muellerElements[ii].resize(1);
     397           0 :                     muellerElements[ii][0]=mList[ii];
     398             :                   }
     399             :                 
     400             :                 // In the absense of evidence, assume that the CFs are
     401             :                 // have no wterm.
     402             :                 //
     403             :                 //CFB::resize() below also sets the supplied value for all CFCells
     404           0 :                 Double wIncr=0;
     405           0 :                 if (miscInfo.isDefined("WIncr")) miscInfo.get("WIncr", wIncr);
     406           0 :         Vector<Double> const wListV(wList);
     407           0 :         Vector<Double> const fListV(fList);
     408           0 :                 cfb->resize(wIncr,0.0,wListV,fListV,
     409             :                             muellerElements,muellerElements,muellerElements,muellerElements);
     410           0 :                 cfb->setPA(paList_p[ipa]);
     411           0 :                 cfb->setDir(Dir);
     412             :                 //
     413             :                 // Now go over the list of fileNames corresponding to
     414             :                 // the current PA value and them the current CFBuffer.
     415             :                 //
     416           0 :                 for (uInt nf=0; nf<fileNames.size(); nf++)
     417             :                   {
     418             :                     Double paVal, wVal, fVal, sampling, conjFreq; 
     419             :                     Int mVal, xSupport, ySupport, conjPoln;
     420           0 :                     CoordinateSystem coordSys;
     421             :                     //
     422             :                     // Get the parameters from the CF file (including the pixel buffer this time)
     423             :                     //
     424             :                     //Bool loadPixBuf=False;
     425           0 :                     TableRecord miscInfo = SynthesisUtils::getCFParams(Dir,fileNames[nf], pixBuf, coordSys,  sampling, paVal, 
     426           0 :                                                                        xSupport, ySupport, fVal, wVal, mVal, conjFreq, conjPoln,loadPixBuf_p,True);
     427             :                     //
     428             :                     // Get the storage buffer from the CFBuffer and
     429             :                     // fill it in what we got from the getCFParams
     430             :                     // call above.
     431             :                     //
     432           0 :                     if (loadPixBuf_p)
     433             :                       {
     434           0 :                         Array<Complex> &cfBuf=(*(cfb->getCFCellPtr(fVal, wVal,mVal)->storage_p));
     435             :                         //
     436             :                         // Fill the cfBuf with the pixel array from the
     437             :                         // disk file.  Add it, along with the extracted CF
     438             :                         // parameters to the CFBuffer.
     439             :                         //
     440           0 :                         cfBuf.assign(pixBuf);
     441             :                       }
     442             : 
     443             : 
     444             :                     Int fndx,wndx, mndx;
     445           0 :                     SynthesisUtils::stdNearestValue(fList, fVal, fndx);
     446           0 :                     SynthesisUtils::stdNearestValue(wList, wVal, wndx);
     447           0 :                     SynthesisUtils::stdNearestValue(mList, mVal, mndx);
     448             :                     //
     449             :                     // The coordSys, sampling, xSupport, ySuport
     450             :                     // params are set for the CFCell at the location
     451             :                     // determined by fndx and wndx.  The mndx is
     452             :                     // determined inside using mVal (why this
     453             :                     // treatment for mndx, please don't ask.  Not just
     454             :                     // yet (SB)).
     455             :                     // Float fsampling=sampling;
     456             :                     // String telescopeName;miscInfo.get("TelescopeName",telescopeName);
     457             :                     // Float diameter; miscInfo.get("Diameter",diameter);
     458             :                     // cfb->setParams(fndx, wndx, 0,0, fVal, wVal, mVal, coordSys,miscInfo,
     459             :                     //             fsampling, xSupport, ySupport, 
     460             :                     //             fileNames[nf],conjFreq, conjPoln,
     461             :                     //             telescopeName, diameter);
     462             : 
     463           0 :                     cfb->setParams(fndx, wndx, 0,0, fVal, wVal, mVal, coordSys,miscInfo);
     464             :                     
     465           0 :                     if (verbose > 0) log_l << cfCacheTable_l[ipa].cfNameList[nf]
     466             :                                            << "[" << fndx << "," << wndx << "," << mndx << "] "
     467           0 :                                            << paList_p[ipa] << " " << xSupport << LogIO::POST;
     468           0 :                   }
     469           0 :               }
     470             : 
     471           0 :           }
     472           0 :       }
     473           0 :     catch(AipsError& x)
     474             :       {
     475           0 :         throw(SynthesisFTMachineError(String("Error while initializing CF disk cache: ")
     476           0 :                                       +x.getMesg()));
     477           0 :       }
     478           0 :   }
     479             :   //
     480             :   //-----------------------------------------------------------------------
     481             :   //
     482           0 :   void CFCache::fillCFSFromDisk(const Directory dirObj, const String& pattern, 
     483             :                                 CFStoreCacheType2& memStore,
     484             :                                 Bool showInfo, Float selectPAVal, Float dPA, 
     485             :                                 const Int verbose)
     486             :   {
     487           0 :     LogOrigin logOrigin("CFCache", "fillCFSFromDisk");
     488           0 :     LogIO log_l(logOrigin);
     489             :     try
     490             :       {
     491           0 :         Regex regex(Regex::fromPattern(pattern));
     492           0 :         Vector<String> fileNames(dirObj.find(regex));
     493           0 :         String CFCDir=dirObj.path().absoluteName();
     494           0 :         if (showInfo)
     495             :           log_l << "No. of " << pattern << " found in " 
     496           0 :                 << dirObj.path().originalName() << ": " 
     497           0 :                 << fileNames.nelements() << LogIO::POST;
     498             :         
     499           0 :         fillCFListFromDisk(fileNames, CFCDir, memStore, showInfo, selectPAVal, dPA, verbose);
     500           0 :       }
     501           0 :     catch(AipsError& x)
     502             :       {
     503           0 :         throw(SynthesisFTMachineError(String("Error while initializing CF disk cache: ")
     504           0 :                                       +x.getMesg()));
     505           0 :       }
     506           0 :   }
     507             :   //
     508             :   //-----------------------------------------------------------------------
     509             :   //
     510           0 :   TableRecord CFCache::getCFParams(const String& fileName,
     511             :                                    Array<Complex>& pixelBuffer,
     512             :                                    CoordinateSystem& coordSys, 
     513             :                                    Double& sampling,
     514             :                                    Double& paVal,
     515             :                                    Int& xSupport, Int& ySupport,
     516             :                                    Double& fVal, Double& wVal, Int& mVal,
     517             :                                    Double& conjFreq, Int& conjPoln,
     518             :                                    Bool loadPixels)
     519             :   {
     520             :     try
     521             :       {
     522           0 :         PagedImage<Complex> thisCF(Dir+'/'+fileName);
     523           0 :         TableRecord miscinfo = thisCF.miscInfo();
     524             : 
     525           0 :         if (loadPixels) pixelBuffer.assign(thisCF.get());
     526           0 :         miscinfo.get("ParallacticAngle", paVal);
     527           0 :         miscinfo.get("MuellerElement", mVal);
     528           0 :         miscinfo.get("WValue", wVal);
     529           0 :         miscinfo.get("Xsupport", xSupport);
     530           0 :         miscinfo.get("Ysupport", ySupport);
     531           0 :         miscinfo.get("Sampling", sampling);
     532           0 :         miscinfo.get("ConjFreq", conjFreq);
     533           0 :         miscinfo.get("ConjPoln", conjPoln);
     534           0 :         Int index= thisCF.coordinates().findCoordinate(Coordinate::SPECTRAL);
     535           0 :         coordSys = thisCF.coordinates();
     536           0 :         SpectralCoordinate spCS = coordSys.spectralCoordinate(index);
     537           0 :         fVal=static_cast<casacore::Float>(spCS.referenceValue()(0));
     538           0 :         return miscinfo;
     539           0 :       }
     540           0 :     catch(AipsError& x)
     541             :       {
     542           0 :         throw(SynthesisFTMachineError(String("Error in CFCache::getCFParams(): ")
     543           0 :                                       +x.getMesg()));
     544           0 :       }
     545             :   }
     546             :   //
     547             :   //-----------------------------------------------------------------------
     548             :   //
     549           0 :   CFCache& CFCache::operator=(const CFCache& other)
     550             :   {
     551             :     //    if (this != other)
     552             :       {
     553           0 :         paList = other.paList;
     554           0 :         Sampling = other.Sampling;
     555           0 :         XSup = other.XSup;
     556           0 :         YSup = other.YSup;
     557           0 :         Dir = other.Dir;
     558           0 :         cfPrefix = other.cfPrefix;
     559           0 :         WtImagePrefix = other.WtImagePrefix;
     560           0 :         aux = other.aux;
     561           0 :         paCD_p = other.paCD_p;
     562           0 :         memCache_p = other.memCache_p;
     563           0 :         memCacheWt_p = other.memCacheWt_p;
     564           0 :         cfCacheTable_p = other.cfCacheTable_p;
     565           0 :         OTODone_p = other.OTODone_p;
     566           0 :         loadPixBuf_p=other.loadPixBuf_p;
     567             :       }
     568           0 :     return *this;
     569             :   };
     570             :   //
     571             :   //-----------------------------------------------------------------------
     572             :   //
     573           0 :   Long CFCache::size()
     574             :   {
     575           0 :     Long s=0;
     576           0 :     for(uInt i=0;i<memCache_p.nelements();i++)
     577           0 :       s+=memCache_p[0].data->size();
     578           0 :     for(uInt i=0;i<memCacheWt_p.nelements();i++)
     579           0 :       s+=memCacheWt_p[0].data->size();
     580             : 
     581           0 :     return s*sizeof(Complex);
     582             :   }
     583             :   //
     584             :   //-----------------------------------------------------------------------
     585             :   //
     586           0 :   void CFCache::makeFTCoordSys(const CoordinateSystem& coords,
     587             :                                const Int& convSize,
     588             :                                const Vector<Double>& ftRef,
     589             :                                CoordinateSystem& ftCoords)
     590             :   {
     591             :     Int directionIndex;
     592             : 
     593           0 :     ftCoords = coords;
     594           0 :     directionIndex=ftCoords.findCoordinate(Coordinate::DIRECTION);
     595             :     //  The following line follows the (lame) logic that if a
     596             :     //  DIRECTION axis was not found, the coordinate system must be of
     597             :     //  the FT domain already
     598           0 :     if (directionIndex == -1) return;
     599             : 
     600           0 :     DirectionCoordinate dc;//=coords.directionCoordinate(directionIndex);
     601             :     //  AlwaysAssert(directionIndex>=0, AipsError);
     602           0 :     dc=coords.directionCoordinate(directionIndex);
     603           0 :     Vector<Bool> axes(2); axes(0)=axes(1)=true;//axes(2)=true;
     604           0 :     Vector<Int> shape(2,convSize);
     605             : 
     606             :     //cerr << "CFC: " << shape << endl;
     607             : 
     608           0 :     Vector<Double>ref(4);
     609           0 :     ref(0)=ref(1)=ref(2)=ref(3)=0;
     610           0 :     dc.setReferencePixel(ref);
     611           0 :     Coordinate* ftdc=dc.makeFourierCoordinate(axes,shape);
     612           0 :     Vector<Double> refVal;
     613           0 :     refVal=ftdc->referenceValue();
     614             :     // refVal(0)=refVal(1)=0;
     615             :     // ftdc->setReferenceValue(refVal);
     616           0 :     ref(0)=ftRef(0);
     617           0 :     ref(1)=ftRef(1);
     618           0 :     ftdc->setReferencePixel(ref);
     619             :     
     620           0 :     ftCoords.replaceCoordinate(*ftdc, directionIndex);
     621           0 :     delete ftdc; ftdc=0;
     622           0 :   }
     623             :   //
     624             :   //-------------------------------------------------------------------------
     625             :   //
     626           0 :   Int CFCache::addToMemCache(CFStoreCacheType& memCache_l, 
     627             :                              Float pa, CFType* cf, 
     628             :                              CoordinateSystem& coords,
     629             :                              Vector<Int>& xConvSupport,
     630             :                              Vector<Int>& yConvSupport,
     631             :                              Float convSampling)
     632             :   {
     633           0 :     Float dPA=paCD_p.getParAngleTolerance().getValue("rad");
     634             : 
     635           0 :     Int where=-1, wConvSize = cf->shape()(CFDefs::NWPOS);
     636           0 :     Bool found=searchConvFunction(where, pa, dPA);
     637             :     //
     638             :     // If the PA value was not found, the return value in "where" is
     639             :     // the negative of the location in which the PA value should be
     640             :     // found.  Convert it to positive value to be used for resizing
     641             :     // etc.
     642             :     //
     643           0 :     where=abs(where);
     644             :     //
     645             :     // Resize the arrays if the CF for the relevant PA was not in the
     646             :     // MEM cache.  Note that if the arrays are already of size
     647             :     // where+1, Array<>::resize() is a no-op.
     648             :     //
     649           0 :     Int N=memCache_l.nelements();
     650             : 
     651           0 :     memCache_l.resize(max(N,where+1), true);
     652           0 :     if ((Int)paList.nelements() <= where)
     653             :       {
     654           0 :         IPosition s(2,wConvSize,where+1);
     655           0 :         paList.resize(where+1,true);
     656           0 :         XSup.resize(s,true);    YSup.resize(s,true);
     657           0 :         Sampling.resize(where+1,true);
     658           0 :       }
     659             :     //
     660             :     // If the PA was not found, enter the aux. values in the internal
     661             :     // arrays.
     662             :     //
     663           0 :     if (!found)
     664             :       {
     665           0 :         paList[where] = pa;
     666           0 :         for(Int iw=0;iw<wConvSize;iw++)
     667             :           {
     668           0 :             YSup(iw,where) = xConvSupport(iw);
     669           0 :             XSup(iw,where) = yConvSupport(iw);
     670             :           }
     671           0 :         Sampling[where]=convSampling;
     672             :       }
     673             :     //
     674             :     // If the CF was not in the mem. cache, add it.
     675             :     //
     676           0 :     if (memCache_l[where].null())
     677             :       {
     678           0 :         Vector<Float> sampling(1);sampling[0]=convSampling;
     679             : 
     680           0 :         Int maxXSup=max(xConvSupport), maxYSup=max(yConvSupport);
     681           0 :         memCache_l[where] = CFStore(cf,coords,sampling,
     682             :                                     xConvSupport,yConvSupport,
     683           0 :                                     maxXSup,maxYSup,Quantity(pa,"rad"),
     684           0 :                                     0);
     685           0 :       }
     686             :     
     687           0 :     return where;
     688             :   }
     689             :   //-------------------------------------------------------------------------
     690             :   // Write the conv. functions from the mem. cache to the disk cache.
     691             :   //
     692           0 :   Int CFCache::cacheConvFunction(Int which,  const Float& pa, CFType& cf, 
     693             :                                  CoordinateSystem& coords,
     694             :                                  CoordinateSystem& ftCoords,
     695             :                                  Int &convSize,
     696             :                                  Vector<Int> &xConvSupport, 
     697             :                                  Vector<Int> &yConvSupport, 
     698             :                                  Float convSampling, 
     699             :                                  String nameQualifier,
     700             :                                  Bool savePA)
     701             :   {
     702           0 :     LogIO log_l(LogOrigin("CFCache","cacheConvFunction"));
     703           0 :     Int whereCached_l=-1;
     704           0 :     if (Dir.length() == 0) return whereCached_l;
     705           0 :     if (which < 0) 
     706             :       {
     707             :         Int i;
     708           0 :         searchConvFunction(i,pa,paCD_p.getParAngleTolerance().get("rad"));
     709             :         //      which = paList.nelements();
     710           0 :         which = abs(i);
     711             :       }
     712             :     
     713             :     try
     714             :       {
     715           0 :         IPosition newConvShape = cf.shape();
     716           0 :         Int wConvSize = newConvShape(CFDefs::NWPOS);
     717           0 :         for(Int iw=0;iw<wConvSize;iw++)
     718             :           {
     719           0 :             IPosition sliceStart(4,0,0,iw,0), 
     720           0 :               sliceLength(4,newConvShape(CFDefs::NXPOS),
     721           0 :                           newConvShape(CFDefs::NYPOS),
     722             :                           1,
     723           0 :                           newConvShape(CFDefs::NPOLPOS));
     724             : 
     725           0 :             Vector<Double> ftRef(2);
     726           0 :             ftRef(0)=newConvShape(CFDefs::NXPOS)/2-1;
     727           0 :             ftRef(1)=newConvShape(CFDefs::NYPOS)/2-1;
     728           0 :             makeFTCoordSys(coords, convSize, ftRef, ftCoords);
     729           0 :             ostringstream name;
     730           0 :             name << Dir << "/" << cfPrefix << nameQualifier << iw << "_" << which;
     731           0 :             const CFType tmpArr=cf(Slicer(sliceStart,sliceLength));
     732             : 
     733             :             //      storeArrayAsImage(name, ftCoords,tmpArr);
     734             : 
     735           0 :             IPosition screenShape(4,newConvShape(CFDefs::NXPOS),
     736           0 :                                   newConvShape(CFDefs::NYPOS),
     737           0 :                                   newConvShape(CFDefs::NPOLPOS),
     738           0 :                                   1);
     739           0 :             Record miscinfo;
     740           0 :             miscinfo.define("Xsupport",xConvSupport);
     741           0 :             miscinfo.define("Ysupport",yConvSupport);
     742           0 :             miscinfo.define("sampling", convSampling);
     743           0 :             miscinfo.define("ParallacticAngle",pa);
     744           0 :             PagedImage<Complex> thisScreen(screenShape, ftCoords, name);
     745           0 :             thisScreen.setMiscInfo(miscinfo);
     746           0 :             Array<Complex> buf;
     747           0 :             buf=((cf(Slicer(sliceStart,sliceLength)).nonDegenerate()));
     748           0 :             thisScreen.put(buf);
     749           0 :           }
     750           0 :         if (savePA)
     751             :           {
     752           0 :             CFStoreCacheType& memCacheObj = getMEMCacheObj(nameQualifier);
     753           0 :             whereCached_l=addToMemCache(memCacheObj, pa,&cf, ftCoords, 
     754             :                                         xConvSupport, yConvSupport, convSampling);
     755             :           }
     756           0 :       }
     757           0 :     catch (AipsError& x)
     758             :       {
     759           0 :         throw(SynthesisFTMachineError("Error while caching CF to disk in "+x.getMesg()));
     760           0 :       }
     761           0 :     return whereCached_l;
     762           0 :   }
     763             :   //
     764             :   //-------------------------------------------------------------------------
     765             :   //  
     766           0 :   void CFCache::cacheConvFunction(const Float pa, CFStore& cfs, 
     767             :                                   String nameQualifier,Bool savePA)
     768             :   {
     769           0 :     if (cfs.data.null())
     770           0 :       throw(SynthesisError(LogMessage("Won't cache a NULL CFStore",
     771           0 :                                       LogOrigin("CFCache::cacheConvFunction")).message()));
     772           0 :     CoordinateSystem ftcoords;
     773           0 :     Int which=-1, whereCached=-1;
     774           0 :     Int convSize=(Int)cfs.data->shape()(0);
     775             :       
     776           0 :     whereCached = cacheConvFunction(which, pa, *(cfs.data), cfs.coordSys, ftcoords, convSize,
     777           0 :                                     cfs.xSupport, cfs.ySupport, cfs.sampling[0],
     778             :                                     nameQualifier,savePA);
     779           0 :     cfs.coordSys = ftcoords;
     780           0 :     if (whereCached > -1)
     781             :       {
     782           0 :         CFStoreCacheType& memCacheObj=getMEMCacheObj(nameQualifier);
     783           0 :         cfs=memCacheObj[whereCached];
     784             :       }
     785           0 :   }
     786             :   //
     787             :   //-------------------------------------------------------------------------
     788             :   //  
     789           0 :   Bool CFCache::searchConvFunction(Int& which,
     790             :                                    const Float pa, const Float dPA)
     791             :   {
     792           0 :     if (paList.nelements()==0) initCache();
     793           0 :     Int i,NPA=paList.nelements(); Bool paFound=false;
     794             :     Float iPA;
     795             :     
     796           0 :     Float paDiff=2*dPA;
     797           0 :     Int saveNdx=-1;
     798             : 
     799           0 :     saveNdx = -1;
     800           0 :     for(i=0;i<NPA;i++)
     801             :       {
     802           0 :         iPA = paList[i];
     803           0 :         if (fabs(iPA - pa) < paDiff)
     804             :           {
     805           0 :             saveNdx = i;
     806           0 :             paDiff = fabs(iPA-pa);
     807             :           }
     808             :       }
     809           0 :     if (saveNdx > -1)
     810             :       {
     811           0 :         iPA = paList[saveNdx];
     812           0 :         if (fabs(iPA - pa) <= dPA)
     813             :           {
     814           0 :             i = saveNdx;
     815           0 :             paFound=true;
     816             :           }
     817             :       }
     818           0 :     if (paFound) which = i; 
     819           0 :     else which = -i;
     820           0 :     return paFound;
     821             :   }
     822             :   //
     823             :   //-------------------------------------------------------------------------
     824             :   //Write the aux. info. also in the disk cache (wonder if this should
     825             :   //be automatically be called from cacheCFtion() method).
     826             :   //
     827           0 :   void CFCache::flush()
     828             :   {
     829             :     // If WtImagePrefix is set, no need to save avgPB in the CFCache
     830           0 :     if (WtImagePrefix != "") return;
     831             : 
     832           0 :     LogIO log_l(LogOrigin("CFCache", "flush"));
     833             : 
     834           0 :     if (Dir.length() == 0) return;
     835           0 :     ostringstream name;
     836             :     
     837           0 :     name << Dir << "/aux.dat";
     838           0 :     Int n=memCache_p.nelements(),nw;
     839           0 :     if (n>0)
     840             :       try
     841             :         {
     842           0 :           nw=memCache_p[0].xSupport.nelements();
     843           0 :           ofstream aux(name.str().c_str());
     844           0 :           aux << n << " " << nw << endl;
     845           0 :           for(Int ipa=0;ipa<n;ipa++)
     846             :             {
     847           0 :               aux << paList[ipa]*180.0/M_PI << " ";
     848           0 :               for(int iw=0;iw<nw;iw++)
     849           0 :                 aux << memCache_p[ipa].xSupport(iw) << " " << memCache_p[ipa].ySupport(iw) << " ";
     850           0 :               aux << " " << Sampling[ipa] <<endl;
     851             :             }
     852           0 :         }
     853           0 :       catch(AipsError &x)
     854             :         {
     855           0 :           throw(SynthesisFTMachineError(string("Error while writing ")
     856           0 :                                         + name.str( ) + (string) x.getMesg()));
     857           0 :         }
     858           0 :   }
     859             :   //
     860             :   //-------------------------------------------------------------------------
     861             :   //Along with the aux. info., also save the average PB in the disk cache.
     862             :   //
     863           0 :   void CFCache::flush(ImageInterface<Float>& avgPB, String qualifier)
     864             :   {
     865             :     // If WtImagePrefix is set, no need to save avgPB in the CFCache
     866           0 :     if (WtImagePrefix != "") return;
     867             : 
     868           0 :     LogIO log_l(LogOrigin("CFCache", "flush"));
     869             : 
     870           0 :     if (Dir.length() == 0) return;
     871           0 :     flush();
     872           0 :     ostringstream Name;
     873           0 :     Name << Dir <<"/avgPB" << qualifier;
     874             :     try
     875             :       {
     876           0 :         storeImg(Name, avgPB);
     877           0 :         avgPBReady_p=true;
     878           0 :         avgPBReadyQualifier_p = qualifier;
     879             :       }
     880           0 :     catch(AipsError &x)
     881             :       {
     882           0 :         throw(SynthesisFTMachineError(string("Error while writing ")
     883           0 :                                       + Name.str( ) + (string) x.getMesg()));
     884           0 :       }
     885           0 :   }
     886             :   //
     887             :   //-------------------------------------------------------------------------
     888             :   //Load the average PB from the disk cache.
     889             :   //
     890           0 :   Int CFCache::loadWtImage(ImageInterface<Float>& avgPB, String qualifier)
     891             :   {
     892           0 :     LogIO log_l(LogOrigin("CFCache", "loadWtImage"));
     893           0 :     ostringstream name, sumWtName;
     894           0 :     name << WtImagePrefix << ".weight" << qualifier;
     895           0 :     if (qualifier != "") sumWtName << WtImagePrefix << ".sumwt.tt0";// << qualifier;
     896           0 :     else                 sumWtName << WtImagePrefix << ".sumwt";
     897             :       
     898             :     try
     899             :       {
     900             :         // First try to load .weight image.  If this fails, AipsError
     901             :         // will be caught and a NOTCACHED returned.
     902           0 :         PagedImage<Float> tmp(name.str().c_str());
     903             : 
     904             :         // Now try to load .sumwt.  If .sumwt is not found, this is a
     905             :         // fatal error (inconsistancy on the disk).  So this time
     906             :         // throw a SEVER exception.
     907           0 :         Float sumwt=1.0;
     908             :         try
     909             :           {
     910           0 :             PagedImage<Float> sumWtTmp(sumWtName.str().c_str());
     911           0 :             sumwt=max(sumWtTmp.get());
     912           0 :           }
     913           0 :         catch (AipsError& x)
     914             :           {
     915           0 :             log_l << "Sum-of-weights not found " << x.getMesg() << LogIO::SEVERE;
     916           0 :           }
     917             : 
     918           0 :         avgPB.resize(tmp.shape());
     919           0 :         avgPB.put(tmp.get()*sumwt);
     920             :         //cerr << "peak = " << max(tmp.get()*sumwt) << endl;
     921           0 :       }
     922           0 :     catch(AipsError& x) // Just rethrowing the exception for now.
     923             :                         // Ultimately, this should be used to make
     924             :                         // the state of this object consistant.
     925             :       {
     926           0 :         return NOTCACHED;
     927           0 :       }
     928             :     //log_l << "Loaded \"" << name.str() << "\"" << LogIO::POST;
     929           0 :     avgPBReady_p=true;
     930           0 :     return DISKCACHE;
     931             : 
     932           0 :   }  
     933             :   //
     934             :   //-------------------------------------------------------------------------
     935             :   //Load the average PB from the disk cache.
     936             :   //
     937           0 :   Int CFCache::loadAvgPB(ImageInterface<Float>& avgPB, String qualifier)
     938             :   {
     939           0 :     LogIO log_l(LogOrigin("CFCache", "loadAvgPB"));
     940             : 
     941           0 :     if (WtImagePrefix != "") 
     942             :       {
     943           0 :         return loadWtImage(avgPB, qualifier);
     944             :       }
     945             : 
     946           0 :     if (Dir.length() == 0) 
     947           0 :       throw(SynthesisFTMachineError("Cache dir. name null"));
     948             : 
     949             : 
     950           0 :     ostringstream name;
     951           0 :     name << Dir << "/avgPB" << qualifier;
     952             :     //    cout << name.str() << endl;
     953             :     try
     954             :       {
     955           0 :         PagedImage<Float> tmp(name.str().c_str());
     956           0 :         avgPB.resize(tmp.shape());
     957           0 :         avgPB.put(tmp.get());
     958           0 :       }
     959           0 :     catch(AipsError& x) // Just rethrowing the exception for now.
     960             :                         // Ultimately, this should be used to make
     961             :                         // the state of this object consistant.
     962             :       {
     963           0 :         return NOTCACHED;
     964           0 :       }
     965           0 :     log_l << "Loaded \"" << name.str() << "\"" << LogIO::POST;
     966           0 :     avgPBReady_p=true;
     967           0 :     return DISKCACHE;
     968           0 :   }
     969             :   //
     970             :   //-------------------------------------------------------------------------
     971             :   //Load a conv. func. from the disk.  This is non-optimal due to the
     972             :   //data structure used for the conv. func. in-memory cache (it's an
     973             :   //array of pointers where it should really be a List of pointers).
     974             :   //The conf. func. index, which is also used as a key to located them
     975             :   //in the mem. cache, are not assured to be contiguous.  As a result,
     976             :   //in the current implementation there can be gaps in the
     977             :   //convFuncCache array.  These gaps are initialized to NULL pointers.
     978             :   //It's not much of a memory waste, but still non-optimal!  Leaving
     979             :   //it like this for now.
     980             :   //
     981             :   // Return TRUE if loaded from disk and FLASE if found in the mem. cache.
     982             :   //
     983           0 :   Int CFCache::loadFromDisk(Int where, Float /*pa*/, Float /*dPA*/,
     984             :                             Int Nw, CFStoreCacheType &convFuncCache,
     985             :                             CFStore& cfs, String nameQualifier)
     986             :   {
     987           0 :     LogIO log_l(LogOrigin("CFCache", "loadFromDisk"));
     988             : 
     989           0 :     Vector<Int> xconvSupport,yconvSupport;;
     990           0 :     Vector<Float> convSampling;
     991             :     //Double cfRefFreq; 
     992           0 :     CoordinateSystem coordSys;
     993           0 :     Array<Complex> cfBuf;
     994             :     Float samplingFromMisc, paFromMisc;
     995           0 :     if (Dir.length() == 0) 
     996           0 :       throw(SynthesisFTMachineError("Cache dir. name not set"));
     997             :       
     998           0 :     if (where < (Int)convFuncCache.nelements() && (!convFuncCache[where].data.null())) 
     999           0 :       return MEMCACHE;
    1000             : 
    1001           0 :     Int wConvSize, polInUse=2;
    1002           0 :     Int N=convFuncCache.nelements();
    1003             : 
    1004             :     //
    1005             :     // Re-size the conv. func. memory cache if required, and set the
    1006             :     // new members of the resized cache to NULL.  This is used in the
    1007             :     // loop below to make a decision about allocating new memory or
    1008             :     // not.
    1009             :     //
    1010           0 :     convFuncCache.resize(max(where+1,N), true);
    1011             :     //    for(Int i=N;i<=where;i++) convFuncCache[i].data=NULL;
    1012             :     //
    1013             :     // Each w-plan is in a separate disk file.  Each file contains all
    1014             :     // polarization planes. Memory cache holds all w-planes and
    1015             :     // poln-planes in a single complex array.  The loop below read
    1016             :     // each w-plane image from the disk, and fills in the 3D
    1017             :     // mem. cache for each computed PA.
    1018             :     //
    1019           0 :     wConvSize = Nw;
    1020           0 :     for(Int iw=0;iw<Nw;iw++)
    1021             :       {
    1022           0 :         ostringstream name;
    1023           0 :         name << Dir << "/" << cfPrefix << nameQualifier << iw << "_" << where;
    1024             :         try
    1025             :           {
    1026           0 :             PagedImage<Complex> tmp(name.str().c_str());
    1027           0 :             Record miscInfo;
    1028           0 :             miscInfo = tmp.miscInfo();
    1029             : 
    1030           0 :             miscInfo.get("Xsupport", xconvSupport);
    1031           0 :             miscInfo.get("Ysupport", yconvSupport);
    1032           0 :             miscInfo.get("sampling", samplingFromMisc);
    1033           0 :             miscInfo.get("ParallacticAngle", paFromMisc);
    1034           0 :             convSampling = samplingFromMisc;
    1035             : 
    1036             : 
    1037           0 :             Int index= tmp.coordinates().findCoordinate(Coordinate::SPECTRAL);
    1038           0 :             coordSys = tmp.coordinates();
    1039           0 :             SpectralCoordinate spCS = coordSys.spectralCoordinate(index);
    1040             : 
    1041             :             //cfRefFreq=spCS.referenceValue()(0);
    1042             :         
    1043           0 :             polInUse = tmp.shape()(2);
    1044           0 :             IPosition ts=tmp.shape(),ndx(4,0,0,0,0),ts2(4,0,0,0,0);
    1045           0 :             Array<Complex> imBuf=tmp.get();
    1046           0 :             if (convFuncCache[where].data.null())
    1047           0 :               cfBuf.resize(IPosition(4,ts(0),ts(1), wConvSize,polInUse));
    1048             :             //        cfBuf = new CFType(IPosition(4,ts(0),ts(1), wConvSize,polInUse));
    1049             :         
    1050           0 :             ndx(CFDefs::NWPOS)=iw;                  // The w-axis
    1051           0 :             for(ndx(CFDefs::NPOLPOS)=0;ndx(CFDefs::NPOLPOS)<polInUse;ndx(CFDefs::NPOLPOS)++)  // The Poln. axis.
    1052           0 :               for(ndx(CFDefs::NXPOS)=0;ndx(CFDefs::NXPOS)<ts(CFDefs::NXPOS);ndx(CFDefs::NXPOS)++)   
    1053           0 :                 for(ndx(CFDefs::NYPOS)=0;ndx(CFDefs::NYPOS)<ts(CFDefs::NYPOS);ndx(CFDefs::NYPOS)++)
    1054             :                   {
    1055           0 :                     ts2(CFDefs::NXPOS)=ndx(CFDefs::NXPOS);
    1056           0 :                     ts2(CFDefs::NYPOS)=ndx(CFDefs::NYPOS);
    1057           0 :                     ts2(2)=ndx(CFDefs::NPOLPOS); // The Poln. axis of the disk-cache. The LHS index is different!
    1058           0 :                     ts2(3)=0;      // The freq. axis of the disk-cache
    1059           0 :                     (cfBuf)(ndx)=imBuf(ts2);
    1060             :                   }
    1061           0 :           }
    1062           0 :         catch(AipsError &x)
    1063             :           {
    1064           0 :             throw(SynthesisFTMachineError(string("Error while loading \"")+
    1065           0 :                                           name.str( ) + string("\": ") + (string) x.getMesg()));
    1066           0 :           }
    1067           0 :       }
    1068           0 :     where=addToMemCache(convFuncCache, paFromMisc, &cfBuf, coordSys, 
    1069             :                         xconvSupport, yconvSupport, samplingFromMisc);
    1070           0 :     cfs=convFuncCache[where];
    1071             :     //    convFuncCache[where].show("loadFromDisk: ");
    1072             : 
    1073           0 :     return DISKCACHE;
    1074           0 :   };
    1075             :   //
    1076             :   //-----------------------------------------------------------------------
    1077             :   //
    1078           0 :   Int CFCache::locateConvFunction(CFStore& cfs, CFStore& cfwts,
    1079             :                                   const Int Nw, const Float pa, const Float dPA,
    1080             :                                   const Int mosXPos, const Int mosYPos)
    1081             :   {
    1082             :     Int retVal;
    1083             :     // This assumes that the return state of locateConvFunction() for
    1084             :     // "CF" and "CFWT" will be the same.  I.e. if "CF" is found in the
    1085             :     // cache, "CFWT" will be found.  If "CF" is not found "CFWT" won't
    1086             :     // be found either.
    1087           0 :     retVal=locateConvFunction(cfs, Nw, pa, dPA,"",mosXPos,mosYPos);
    1088           0 :     locateConvFunction(cfwts, Nw, pa, dPA,"WT",mosXPos,mosYPos);
    1089             : 
    1090           0 :    return retVal;
    1091             :   }
    1092             :   //
    1093             :   //-----------------------------------------------------------------------
    1094             :   //
    1095             :   // Locate a convlution function in either mem. or disk cache.  
    1096             :   // Return CFCache::DISKCACHE (=1) if found in the disk cache.
    1097             :   //        CFCache::MEMCACHE (=2)  if found in the mem. cache.
    1098             :   //       <0 if not found in either cache.  In this case, absolute of
    1099             :   //          the return value corresponds to the index in the list of
    1100             :   //          conv. funcs. where this conv. func. should be filled
    1101             :   //
    1102           0 :   Int CFCache::locateConvFunction(CFStore& cfs, 
    1103             :                                   const Int Nw, const Float pa, const Float dPA,
    1104             :                                   const String& nameQualifier,
    1105             :                                   const Int /*mosXPos*/, const Int /*mosYPos*/)
    1106             :   {
    1107           0 :     LogIO log_l(LogOrigin("CFCache", "locatedConvFunction"));
    1108             : 
    1109           0 :     Int paKey,retVal=NOTCACHED; 
    1110             :     Bool found;
    1111             :     // Search for the PA corresponding to the supplied VB to find a
    1112             :     // paKey in memCache_p which has a Conv. Func. within dPA (dPA is
    1113             :     // given by paCD).  If found, return the key in paKey.
    1114           0 :     found = searchConvFunction(paKey,pa, dPA);
    1115             : 
    1116           0 :     if (found)
    1117             :       {
    1118           0 :         CFStoreCacheType &memCacheObj=getMEMCacheObj(nameQualifier);
    1119           0 :         retVal=loadFromDisk(paKey,pa,dPA,Nw,memCacheObj,cfs,nameQualifier);
    1120             :         
    1121           0 :         switch (retVal)
    1122             :           {
    1123           0 :           case DISKCACHE:
    1124             :             {
    1125           0 :               if (paKey < (Int)memCacheObj.nelements())
    1126             :                 log_l << "Loaded from disk cache: Conv. func. # "
    1127           0 :                       << paKey << LogIO::POST;
    1128             :               else
    1129           0 :                 throw(SynthesisFTMachineError("Internal error: paKey out of range"));
    1130             :             }
    1131             :           case MEMCACHE:  
    1132             :             {
    1133           0 :               cfs=(memCacheObj[paKey]);
    1134           0 :               break;
    1135             :             }
    1136           0 :           case NOTCACHED: {break;}
    1137             :           };
    1138             :       }
    1139           0 :     return retVal;
    1140           0 :   }
    1141             :   //
    1142             :   //-----------------------------------------------------------------------
    1143             :   //
    1144           0 :   CFCache::CFStoreCacheType& CFCache::getMEMCacheObj(const String& nameQualifier)
    1145             :   {
    1146           0 :     LogIO log_l(LogOrigin("CFCache::getMEMCacheObj"));
    1147           0 :     if (nameQualifier == "")           return memCache_p;
    1148           0 :     else if (nameQualifier == "WT")    return memCacheWt_p;
    1149             :     else
    1150           0 :       log_l << "Internal error. Unknown name qualifier '"+nameQualifier+"'."
    1151           0 :             << LogIO::EXCEPTION << endl;
    1152             :     // 
    1153             :     // Return to suppress compiler warning.  Control will never reach
    1154             :     // the following line.
    1155             :     //
    1156           0 :     return memCache_p;
    1157           0 :   }
    1158             :   //
    1159             :   //-----------------------------------------------------------------------
    1160             :   // This is for CUDA.  Not used otherwise.
    1161             :   // void CFCache::constructTable_p(CFCache::CFCacheTable& tab)
    1162             :   // {
    1163             :   //   TableDesc td("CFCache","0.0",TableDesc::Scratch);
    1164             :     
    1165             :   //   add PA and baseline type info.
    1166             : 
    1167             :   //   td.addColumn(ScalarColumnDesc<Double>("Ref. Frequency"));
    1168             :   //   td.addColumn(ScalarColumnDesc<Double>("W Value"));
    1169             :   //   td.addColumn(ScalarColumnDesc<uInt>("Mueller Index"));
    1170             :   //   td.addColumn(ScalarColumnDesc<String>("CF Disk File Name"));
    1171             :   //   SetupNewTable newTab("CFCache.dat",td,Table::Scratch);
    1172             :   //   tab = Table(newTab);
    1173             :   // }
    1174             : 
    1175             : } // end casa namespace

Generated by: LCOV version 1.16