LCOV - code coverage report
Current view: top level - synthesis/ImagerObjects - SDAlgorithmAAspClean.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 65 66 98.5 %
Date: 2024-11-06 17:42:47 Functions: 6 6 100.0 %

          Line data    Source code
       1             : //# SDAlgorithmAAspClean.cc: Implementation of SDAlgorithmAAspClean classes
       2             : //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
       3             : //# Associated Universities, Inc. Washington DC, USA.
       4             : //#
       5             : //# This library is free software; you can redistribute it and/or modify it
       6             : //# under the terms of the GNU Library General Public License as published by
       7             : //# the Free Software Foundation; either version 2 of the License, or (at your
       8             : //# option) any later version.
       9             : //#
      10             : //# This library is distributed in the hope that it will be useful, but WITHOUT
      11             : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
      13             : //# License for more details.
      14             : //#
      15             : //# You should have received a copy of the GNU Library General Public License
      16             : //# along with this library; if not, write to the Free Software Foundation,
      17             : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      18             : //#
      19             : //# Correspondence concerning AIPS++ should be addressed as follows:
      20             : //#        Internet email: casa-feedback@nrao.edu.
      21             : //#        Postal address: AIPS++ Project Office
      22             : //#                        National Radio Astronomy Observatory
      23             : //#                        520 Edgemont Road
      24             : //#                        Charlottesville, VA 22903-2475 USA
      25             : //#
      26             : //# $Id$
      27             : 
      28             : #include <casacore/casa/Arrays/ArrayMath.h>
      29             : #include <casacore/casa/OS/HostInfo.h>
      30             : #include <synthesis/ImagerObjects/SDAlgorithmAAspClean.h>
      31             : 
      32             : #include <components/ComponentModels/SkyComponent.h>
      33             : #include <components/ComponentModels/ComponentList.h>
      34             : #include <casacore/images/Images/TempImage.h>
      35             : #include <casacore/images/Images/SubImage.h>
      36             : #include <casacore/images/Regions/ImageRegion.h>
      37             : #include <casacore/casa/OS/File.h>
      38             : #include <casacore/lattices/LEL/LatticeExpr.h>
      39             : #include <casacore/lattices/Lattices/TiledLineStepper.h>
      40             : #include <casacore/lattices/Lattices/LatticeStepper.h>
      41             : #include <casacore/lattices/Lattices/LatticeIterator.h>
      42             : #include <synthesis/TransformMachines/StokesImageUtil.h>
      43             : #include <casacore/coordinates/Coordinates/StokesCoordinate.h>
      44             : #include <casacore/casa/Exceptions/Error.h>
      45             : #include <casacore/casa/BasicSL/String.h>
      46             : #include <casacore/casa/Utilities/Assert.h>
      47             : #include <casacore/casa/OS/Directory.h>
      48             : #include <casacore/tables/Tables/TableLock.h>
      49             : 
      50             : #include<synthesis/ImagerObjects/SIMinorCycleController.h>
      51             : 
      52             : #include <sstream>
      53             : 
      54             : #include <casacore/casa/Logging/LogMessage.h>
      55             : #include <casacore/casa/Logging/LogIO.h>
      56             : #include <casacore/casa/Logging/LogSink.h>
      57             : 
      58             : #include <casacore/casa/System/Choice.h>
      59             : #include <msvis/MSVis/StokesVector.h>
      60             : 
      61             : using namespace casacore;
      62             : namespace casa { //# NAMESPACE CASA - BEGIN
      63             : 
      64          11 :   SDAlgorithmAAspClean::SDAlgorithmAAspClean(Float fusedThreshold, bool isSingle, Int largestScale, Int stoppointmode):
      65             :     SDAlgorithmBase(),
      66          11 :     itsMatPsf(), itsMatResidual(), itsMatModel(),
      67          11 :     itsCleaner(),
      68          11 :     itsStopPointMode(stoppointmode),
      69          11 :     itsMCsetup(true),
      70          11 :     itsFusedThreshold(fusedThreshold),
      71          11 :     itsPrevPsfWidth(0),
      72          11 :     itsIsSingle(isSingle),
      73          33 :     itsUserLargestScale(largestScale)
      74             :   {
      75          11 :     itsAlgorithmName = String("asp");
      76          11 :   }
      77             : 
      78          22 :   SDAlgorithmAAspClean::~SDAlgorithmAAspClean()
      79             :   {
      80             : 
      81          22 :   }
      82             : 
      83          35 :   void SDAlgorithmAAspClean::initializeDeconvolver()
      84             :   {
      85          70 :     LogIO os(LogOrigin("SDAlgorithmAAspClean", "initializeDeconvolver", WHERE));
      86          35 :     AlwaysAssert((bool)itsImages, AipsError);
      87             : 
      88          35 :     itsImages->residual()->get( itsMatResidual, true );
      89          35 :     itsImages->model()->get( itsMatModel, true );
      90          35 :     itsImages->psf()->get( itsMatPsf, true );
      91          35 :     itsImages->mask()->get( itsMatMask, true );
      92             : 
      93             :     // Initialize the AspMatrixCleaner.
      94             :     // If it's single channel, this only needs to be computed once.
      95             :     // Otherwise, it needs to be called repeatedly at each minor cycle start to
      96             :     // get psf for each channel
      97          35 :     if (itsMCsetup)
      98             :     {
      99          35 :       Matrix<Float> tempMat(itsMatPsf);
     100          35 :       itsCleaner.setPsf(tempMat);
     101             :       // Initial scales are unchanged and only need to be
     102             :       // computed when psf width is updated
     103          35 :       const Float width = itsCleaner.getPsfGaussianWidth(*(itsImages->psf()));
     104             :       //if user does not provide the largest scale, we calculate it internally.
     105          35 :       itsCleaner.setUserLargestScale(itsUserLargestScale);
     106             :       // we do not use the shortest baseline approach below b/c of reasons in CAS-940 dated around Jan 2022
     107             :       // itsCleaner.getLargestScaleSize(*(itsImages->psf()));
     108             : 
     109          35 :       if (itsPrevPsfWidth != width)
     110             :       {
     111          28 :         itsPrevPsfWidth = width;
     112          28 :         itsCleaner.setInitScaleXfrs(width);
     113             :       }
     114             : 
     115          35 :       itsCleaner.stopPointMode( itsStopPointMode );
     116          35 :       itsCleaner.ignoreCenterBox( true ); // Clean full image
     117             :       // If it's single channel, we do not do the expensive set up repeatedly
     118          35 :       if (itsIsSingle)
     119           0 :         itsMCsetup = false;
     120             :       // Not used. Kept for unit test
     121             :       //Matrix<Float> tempMat1(itsMatResidual);
     122             :       //itsCleaner.setOrigDirty( tempMat1 );
     123             : 
     124             : 
     125          35 :       itsCleaner.setFusedThreshold(itsFusedThreshold);
     126          35 :     }
     127             : 
     128             :     // Parts to be repeated at each minor cycle start....
     129          35 :     itsCleaner.setInitScaleMasks(itsMatMask);
     130          35 :     itsCleaner.setaspcontrol(0, 0, 0, Quantity(0.0, "%"));/// Needs to come before the rest
     131             : 
     132          35 :     Matrix<Float> tempMat1;
     133          35 :     tempMat1.reference(itsMatResidual);
     134          35 :     itsCleaner.setDirty( tempMat1 );
     135             :     // InitScaleXfrs and InitScaleMasks should already be set
     136          35 :     itsScaleSizes.clear();
     137          35 :     itsScaleSizes = itsCleaner.getActiveSetAspen();
     138          35 :     itsScaleSizes.push_back(0.0); // put 0 scale
     139          35 :     itsCleaner.defineAspScales(itsScaleSizes);
     140          35 :   }
     141             : 
     142             : 
     143          35 :   void SDAlgorithmAAspClean::takeOneStep( Float loopgain,
     144             :                                           Int cycleNiter,
     145             :                                           Float cycleThreshold,
     146             :                                           Float &peakresidual,
     147             :                                           Float &modelflux,
     148             :                                           Int &iterdone)
     149             :   {
     150          70 :     LogIO os( LogOrigin("SDAlgorithmAAspClean","takeOneStep", WHERE) );
     151             : 
     152          35 :     Quantity thresh(cycleThreshold, "Jy");
     153          35 :     itsCleaner.setaspcontrol(cycleNiter, loopgain, thresh, Quantity(0.0, "%"));
     154          35 :     Matrix<Float> tempModel;
     155          35 :     tempModel.reference( itsMatModel );
     156             :     //save the previous model
     157          35 :     Matrix<Float> prevModel;
     158          35 :     prevModel = itsMatModel;
     159             : 
     160             :     //cout << "AAspALMS,  matrix shape : " << tempModel.shape() << " array shape : " << itsMatModel.shape() << endl;
     161             : 
     162             :     // retval
     163             :     //  1 = converged
     164             :     //  0 = not converged but behaving normally
     165             :     // -1 = not converged and stopped on cleaning consecutive smallest scale
     166             :     // -2 = not converged and either large scale hit negative or diverging
     167             :     // -3 = clean is diverging rather than converging
     168          35 :     itsCleaner.startingIteration( 0 );
     169          35 :     Int retval = itsCleaner.aspclean( tempModel );
     170          35 :     iterdone = itsCleaner.numberIterations();
     171             : 
     172          35 :     if( retval==-1 ) {os << LogIO::WARN << "AspClean minor cycle stopped on cleaning consecutive smallest scale" << LogIO::POST; }
     173          35 :     if( retval==-2 ) {os << LogIO::WARN << "AspClean minor cycle stopped at large scale negative or diverging" << LogIO::POST;}
     174          35 :     if( retval==-3 ) {os << LogIO::WARN << "AspClean minor cycle stopped because it is diverging" << LogIO::POST; }
     175             : 
     176             :     // update residual - this is critical
     177          35 :     itsMatResidual = itsCleaner.getterResidual();
     178             : 
     179          35 :     peakresidual = itsCleaner.getterPeakResidual();
     180             :     //cout << "SDAlg: peakres " << peakresidual << endl;
     181          35 :     modelflux = sum( itsMatModel );
     182          35 :   }
     183             : 
     184          35 :   void SDAlgorithmAAspClean::finalizeDeconvolver()
     185             :   {
     186          35 :     (itsImages->residual())->put( itsMatResidual );
     187          35 :     (itsImages->model())->put( itsMatModel );
     188          35 :   }
     189             : 
     190             : } //# NAMESPACE CASA - END

Generated by: LCOV version 1.16