Line data Source code
1 : //# SDAlgorithmMSClean.cc: Implementation of SDAlgorithmMSClean 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/SDAlgorithmMSClean.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 <casacore/lattices/Lattices/LatticeLocker.h>
43 : #include <synthesis/TransformMachines/StokesImageUtil.h>
44 : #include <casacore/coordinates/Coordinates/StokesCoordinate.h>
45 : #include <casacore/casa/Exceptions/Error.h>
46 : #include <casacore/casa/BasicSL/String.h>
47 : #include <casacore/casa/Utilities/Assert.h>
48 : #include <casacore/casa/OS/Directory.h>
49 : #include <casacore/tables/Tables/TableLock.h>
50 :
51 : #include<synthesis/ImagerObjects/SIMinorCycleController.h>
52 :
53 : #include <sstream>
54 :
55 : #include <casacore/casa/Logging/LogMessage.h>
56 : #include <casacore/casa/Logging/LogIO.h>
57 : #include <casacore/casa/Logging/LogSink.h>
58 :
59 : #include <casacore/casa/System/Choice.h>
60 : #include <msvis/MSVis/StokesVector.h>
61 :
62 :
63 : using namespace casacore;
64 : namespace casa { //# NAMESPACE CASA - BEGIN
65 :
66 :
67 0 : SDAlgorithmMSClean::SDAlgorithmMSClean( Vector<Float> scalesizes,
68 : Float smallscalebias,
69 : // Int stoplargenegatives,
70 0 : Int stoppointmode ):
71 : SDAlgorithmBase(),
72 0 : itsMatPsf(), itsMatResidual(), itsMatModel(),
73 0 : itsCleaner(),
74 0 : itsScaleSizes(scalesizes),
75 0 : itsSmallScaleBias(smallscalebias),
76 : // itsStopLargeNegatives(stoplargenegatives),
77 0 : itsStopPointMode(stoppointmode)
78 : {
79 0 : itsAlgorithmName=String("multiscale");
80 0 : if( itsScaleSizes.nelements()==0 ){ itsScaleSizes.resize(1); itsScaleSizes[0]=0.0; }
81 0 : }
82 :
83 :
84 0 : SDAlgorithmMSClean::~SDAlgorithmMSClean()
85 : {
86 :
87 0 : }
88 :
89 0 : Long SDAlgorithmMSClean::estimateRAM(const vector<int>& imsize){
90 0 : Long mem=0;
91 0 : IPosition shp;
92 0 : if(itsImages)
93 0 : shp=itsImages->getShape();
94 0 : else if(imsize.size() >1)
95 0 : shp=IPosition(imsize);
96 : else
97 0 : return 0;
98 : //throw(AipsError("Deconvolver cannot estimate the memory usage at this point"));
99 :
100 : //Number of planes in memory
101 : //npsf=nscales+1
102 : //nresidual=4+nscales
103 : //nmodel=1
104 : //nmasks=2+nscales
105 : //transfer functions=nscales*2
106 0 : Long nplanes=5*itsScaleSizes.nelements()+7;
107 : //in kB
108 0 : mem=sizeof(Float)*(shp(0))*(shp(1))*nplanes/1024;
109 :
110 0 : return mem;
111 0 : }
112 :
113 : // void SDAlgorithmMSClean::initializeDeconvolver( Float &peakresidual, Float &modelflux )
114 0 : void SDAlgorithmMSClean::initializeDeconvolver()
115 : {
116 0 : LogIO os( LogOrigin("SDAlgorithmMSClean","initializeDeconvolver",WHERE) );
117 :
118 0 : AlwaysAssert( (bool) itsImages, AipsError );
119 : {
120 0 : LatticeLocker lock1 (*(itsImages->residual()), FileLocker::Read);
121 0 : LatticeLocker lock2 (*(itsImages->model()), FileLocker::Read);
122 0 : LatticeLocker lock3 (*(itsImages->psf()), FileLocker::Read);
123 0 : LatticeLocker lock4 (*(itsImages->mask()), FileLocker::Read);
124 0 : (itsImages->residual())->get( itsMatResidual , true );
125 0 : (itsImages->model())->get( itsMatModel , true );
126 0 : (itsImages->psf())->get( itsMatPsf , true );
127 0 : itsImages->mask()->get( itsMatMask, true );
128 :
129 0 : }
130 : //// Initialize the MatrixCleaner.
131 : /// ----------- do once ----------
132 : {
133 0 : itsCleaner.defineScales( itsScaleSizes );
134 :
135 0 : if(itsSmallScaleBias > 1)
136 : {
137 0 : os << LogIO::WARN << "Acceptable smallscalebias values are [-1,1].Changing smallscalebias from " << itsSmallScaleBias <<" to 1." << LogIO::POST;
138 0 : itsSmallScaleBias = 1;
139 : }
140 :
141 0 : if(itsSmallScaleBias < -1)
142 : {
143 0 : os << LogIO::WARN << "Acceptable smallscalebias values are [-1,1].Changing smallscalebias from " << itsSmallScaleBias <<" to -1." << LogIO::POST;
144 0 : itsSmallScaleBias = -1;
145 : }
146 :
147 :
148 0 : itsCleaner.setSmallScaleBias( itsSmallScaleBias );
149 : //itsCleaner.stopAtLargeScaleNegative( itsStopLargeNegatives );// In MFMSCleanImageSkyModel.cc, this is only for the first two major cycles...
150 0 : itsCleaner.stopPointMode( itsStopPointMode );
151 0 : itsCleaner.ignoreCenterBox( true ); // Clean full image
152 :
153 0 : Matrix<Float> tempMat;
154 0 : tempMat.reference( itsMatPsf );
155 0 : itsCleaner.setPsf( tempMat );
156 0 : itsCleaner.makePsfScales();
157 0 : }
158 : /// -----------------------------------------
159 :
160 : /*
161 : /// Find initial max vals..
162 : findMaxAbsMask( itsMatResidual, itsMatMask, itsPeakResidual, itsMaxPos );
163 : itsModelFlux = sum( itsMatModel );
164 :
165 : peakresidual = itsPeakResidual;
166 : modelflux = itsModelFlux;
167 : */
168 :
169 : // Parts to be repeated at each minor cycle start....
170 :
171 0 : itsCleaner.setcontrol(CleanEnums::MULTISCALE,0,0,0);/// Needs to come before makeDirtyScales
172 :
173 0 : Matrix<Float> tempmask(itsMatMask);
174 0 : itsCleaner.setMask( tempmask );
175 :
176 0 : Matrix<Float> tempMat1;
177 0 : tempMat1.reference( itsMatResidual );
178 0 : itsCleaner.setDirty( tempMat1 );
179 0 : itsCleaner.makeDirtyScales();
180 :
181 0 : }
182 :
183 0 : void SDAlgorithmMSClean::takeOneStep( Float loopgain, Int cycleNiter, Float cycleThreshold, Float &peakresidual, Float &modelflux, Int &iterdone)
184 : {
185 0 : LogIO os( LogOrigin("SDAlgorithmMSClean","takeOneStep",WHERE) );
186 :
187 0 : Quantity thresh( cycleThreshold, "Jy" );
188 : // Quantity ftthresh( 100.0, "Jy" ); /// Look at MFMSCleanImageSkyModel.cc for more.
189 0 : itsCleaner.setcontrol(CleanEnums::MULTISCALE, cycleNiter, loopgain, thresh); //, ftthresh);
190 :
191 0 : Matrix<Float> tempModel;
192 0 : tempModel.reference( itsMatModel );
193 : //save the previous model
194 0 : Matrix<Float> prevModel;
195 0 : prevModel=itsMatModel;
196 :
197 : //cout << "SDALMS, matrix shape : " << tempModel.shape() << " array shape : " << itsMatModel.shape() << endl;
198 :
199 : // retval
200 : // 1 = converged
201 : // 0 = not converged but behaving normally
202 : // -1 = not converged and stopped on cleaning consecutive smallest scale
203 : // -2 = not converged and either large scale hit negative or diverging
204 : // -3 = clean is diverging rather than converging
205 0 : itsCleaner.startingIteration( 0 );
206 0 : Int retval = itsCleaner.clean( tempModel );
207 0 : iterdone = itsCleaner.numberIterations();
208 :
209 0 : if( retval==-1 ) {os << LogIO::WARN << "MSClean minor cycle stopped on cleaning consecutive smallest scale" << LogIO::POST; }
210 0 : if( retval==-2 ) {os << LogIO::WARN << "MSClean minor cycle stopped at large scale negative or diverging" << LogIO::POST;}
211 0 : if( retval==-3 ) {os << LogIO::WARN << "MSClean minor cycle stopped because it is diverging" << LogIO::POST; }
212 :
213 : // Retrieve residual to be saved to the .residual file in finalizeDeconvolver.
214 : ////This is going to be wrong if there is no 0 scale;
215 : ///Matrix<Float> residual(itsCleaner.residual());
216 : //Matrix<Float> residual(itsCleaner.residual(tempModel-prevModel));
217 : // cout << "Max tempModel : " << max(abs(tempModel)) << " Max prevModel : " << max(abs(prevModel)) << endl;
218 0 : itsMatResidual = itsCleaner.residual(tempModel-prevModel);
219 :
220 : // account for mask as well
221 : //peakresidual = max(abs(residual*itsMatMask));
222 0 : peakresidual = max(abs(itsMatResidual*itsMatMask));
223 0 : modelflux = sum( itsMatModel ); // Performance hog ?
224 0 : }
225 :
226 0 : void SDAlgorithmMSClean::finalizeDeconvolver()
227 : {
228 : ///MatrixCleaner does not modify the original residual image matrix
229 : ///so the first line is a dummy.
230 0 : LatticeLocker lock1 (*(itsImages->residual()), FileLocker::Write);
231 0 : LatticeLocker lock2 (*(itsImages->model()), FileLocker::Write);
232 0 : (itsImages->residual())->put( itsMatResidual );
233 0 : (itsImages->model())->put( itsMatModel );
234 0 : }
235 :
236 :
237 : } //# NAMESPACE CASA - END
238 :
|