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: aips2-request@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 12 : SDAlgorithmAAspClean::SDAlgorithmAAspClean(Float fusedThreshold, bool isSingle, Int largestScale, Int stoppointmode):
65 : SDAlgorithmBase(),
66 : itsMatPsf(), itsMatResidual(), itsMatModel(),
67 : itsCleaner(),
68 : itsStopPointMode(stoppointmode),
69 : itsMCsetup(true),
70 : itsFusedThreshold(fusedThreshold),
71 : itsPrevPsfWidth(0),
72 : itsIsSingle(isSingle),
73 12 : itsUserLargestScale(largestScale)
74 : {
75 12 : itsAlgorithmName = String("asp");
76 12 : }
77 :
78 24 : SDAlgorithmAAspClean::~SDAlgorithmAAspClean()
79 : {
80 :
81 24 : }
82 :
83 40 : void SDAlgorithmAAspClean::initializeDeconvolver()
84 : {
85 120 : LogIO os(LogOrigin("SDAlgorithmAAspClean", "initializeDeconvolver", WHERE));
86 40 : AlwaysAssert((bool)itsImages, AipsError);
87 :
88 40 : itsImages->residual()->get( itsMatResidual, true );
89 40 : itsImages->model()->get( itsMatModel, true );
90 40 : itsImages->psf()->get( itsMatPsf, true );
91 40 : 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 40 : if (itsMCsetup)
98 : {
99 80 : Matrix<Float> tempMat(itsMatPsf);
100 40 : itsCleaner.setPsf(tempMat);
101 : // Initial scales are unchanged and only need to be
102 : // computed when psf width is updated
103 40 : const Float width = itsCleaner.getPsfGaussianWidth(*(itsImages->psf()));
104 : //if user does not provide the largest scale, we calculate it internally.
105 40 : 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 40 : if (itsPrevPsfWidth != width)
110 : {
111 32 : itsPrevPsfWidth = width;
112 32 : itsCleaner.setInitScaleXfrs(width);
113 : }
114 :
115 40 : itsCleaner.stopPointMode( itsStopPointMode );
116 40 : itsCleaner.ignoreCenterBox( true ); // Clean full image
117 : // If it's single channel, we do not do the expensive set up repeatedly
118 40 : if (itsIsSingle)
119 0 : itsMCsetup = false;
120 : // Not used. Kept for unit test
121 : //Matrix<Float> tempMat1(itsMatResidual);
122 : //itsCleaner.setOrigDirty( tempMat1 );
123 :
124 40 : if (itsFusedThreshold < 0)
125 : {
126 0 : os << LogIO::WARN << "Acceptable fusedthreshld values are >= 0. Changing fusedthreshold from " << itsFusedThreshold << " to 0." << LogIO::POST;
127 0 : itsFusedThreshold = 0.0;
128 : }
129 :
130 40 : itsCleaner.setFusedThreshold(itsFusedThreshold);
131 : }
132 :
133 : // Parts to be repeated at each minor cycle start....
134 40 : itsCleaner.setInitScaleMasks(itsMatMask);
135 40 : itsCleaner.setaspcontrol(0, 0, 0, Quantity(0.0, "%"));/// Needs to come before the rest
136 :
137 80 : Matrix<Float> tempMat1;
138 40 : tempMat1.reference(itsMatResidual);
139 40 : itsCleaner.setDirty( tempMat1 );
140 : // InitScaleXfrs and InitScaleMasks should already be set
141 40 : itsScaleSizes.clear();
142 40 : itsScaleSizes = itsCleaner.getActiveSetAspen();
143 40 : itsScaleSizes.push_back(0.0); // put 0 scale
144 40 : itsCleaner.defineAspScales(itsScaleSizes);
145 40 : }
146 :
147 :
148 40 : void SDAlgorithmAAspClean::takeOneStep( Float loopgain,
149 : Int cycleNiter,
150 : Float cycleThreshold,
151 : Float &peakresidual,
152 : Float &modelflux,
153 : Int &iterdone)
154 : {
155 120 : LogIO os( LogOrigin("SDAlgorithmAAspClean","takeOneStep", WHERE) );
156 :
157 80 : Quantity thresh(cycleThreshold, "Jy");
158 40 : itsCleaner.setaspcontrol(cycleNiter, loopgain, thresh, Quantity(0.0, "%"));
159 80 : Matrix<Float> tempModel;
160 40 : tempModel.reference( itsMatModel );
161 : //save the previous model
162 40 : Matrix<Float> prevModel;
163 40 : prevModel = itsMatModel;
164 :
165 : //cout << "AAspALMS, matrix shape : " << tempModel.shape() << " array shape : " << itsMatModel.shape() << endl;
166 :
167 : // retval
168 : // 1 = converged
169 : // 0 = not converged but behaving normally
170 : // -1 = not converged and stopped on cleaning consecutive smallest scale
171 : // -2 = not converged and either large scale hit negative or diverging
172 : // -3 = clean is diverging rather than converging
173 40 : itsCleaner.startingIteration( 0 );
174 40 : Int retval = itsCleaner.aspclean( tempModel );
175 40 : iterdone = itsCleaner.numberIterations();
176 :
177 40 : if( retval==-1 ) {os << LogIO::WARN << "AspClean minor cycle stopped on cleaning consecutive smallest scale" << LogIO::POST; }
178 40 : if( retval==-2 ) {os << LogIO::WARN << "AspClean minor cycle stopped at large scale negative or diverging" << LogIO::POST;}
179 40 : if( retval==-3 ) {os << LogIO::WARN << "AspClean minor cycle stopped because it is diverging" << LogIO::POST; }
180 :
181 : // update residual - this is critical
182 40 : itsMatResidual = itsCleaner.getterResidual();
183 :
184 40 : peakresidual = itsCleaner.getterPeakResidual();
185 : //cout << "SDAlg: peakres " << peakresidual << endl;
186 40 : modelflux = sum( itsMatModel );
187 40 : }
188 :
189 40 : void SDAlgorithmAAspClean::finalizeDeconvolver()
190 : {
191 40 : (itsImages->residual())->put( itsMatResidual );
192 40 : (itsImages->model())->put( itsMatModel );
193 40 : }
194 :
195 : } //# NAMESPACE CASA - END
|