casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ImageFitter.h
Go to the documentation of this file.
00001 //# tSubImage.cc: Test program for class SubImage
00002 //# Copyright (C) 1998,1999,2000,2001,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This program is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU General Public License as published by the Free
00007 //# Software Foundation; either version 2 of the License, or (at your option)
00008 //# any later version.
00009 //#
00010 //# This program is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013 //# more details.
00014 //#
00015 //# You should have received a copy of the GNU General Public License along
00016 //# with this program; if not, write to the Free Software Foundation, Inc.,
00017 //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: tSubImage.cc 20567 2009-04-09 23:12:39Z gervandiepen $
00027 
00028 #ifndef IMAGES_IMAGEFITTER_H
00029 #define IMAGES_IMAGEFITTER_H
00030 
00031 #include <measures/Measures/Stokes.h>
00032 #include <lattices/LatticeMath/Fit2D.h>
00033 #include <components/ComponentModels/ComponentList.h>
00034 #include <imageanalysis/ImageAnalysis/ImageTask.h>
00035 #include <images/Images/SubImage.h>
00036 
00037 #include <components/ComponentModels/ComponentType.h>
00038 #include <casa/namespace.h>
00039 
00040 namespace casa {
00041 
00042 class ImageFitter : public ImageTask {
00043         // <summary>
00044         // Top level interface to ImageAnalysis::fitsky to handle inputs, bookkeeping etc and
00045         // ultimately call fitsky to do fitting
00046         // </summary>
00047 
00048         // <reviewed reviewer="" date="" tests="" demos="">
00049         // </reviewed>
00050 
00051         // <prerequisite>
00052         // </prerequisite>
00053 
00054         // <etymology>
00055         // Fits components to sources in images (ImageSourceComponentFitter was deemed to be to long
00056         // of a name)
00057         // </etymology>
00058 
00059         // <synopsis>
00060         // ImageFitter is the top level interface for fitting image source components. It handles most
00061         // of the inputs, bookkeeping etc. It can be instantiated and its one public method, fit,
00062         // run from either a C++ app or python.
00063         // </synopsis>
00064 
00065         // <example>
00066         // <srcblock>
00067         // ImageFitter fitter(...)
00068         // fitter.fit()
00069         // </srcblock>
00070         // </example>
00071 
00072 public:
00073         enum CompListWriteControl {
00074                 NO_WRITE,
00075                 WRITE_NO_REPLACE,
00076                 OVERWRITE
00077         };
00078 
00079         // constructor approprate for API calls.
00080         // Parameters:
00081         // <ul>
00082         // <li>imagename - the name of the input image in which to fit the models</li>
00083         // <li>box - A 2-D rectangular box in which to use pixels for the fitting, eg box=100,120,200,230
00084         // In cases where both box and region are specified, box, not region, is used.</li>
00085         // <li>region - Named region to use for fitting</li>
00086         // <li>regionPtr - A pointer to a region. Note there are unfortunately several different types of
00087         // region records throughout CASA. In this case, it must be a Record produced by creating a
00088         // region via a RegionManager method.
00089         // <li>chanInp - Zero-based channel number on which to do the fit. Only a single channel can be
00090         // specified.</li>
00091         // <li>stokes - Stokes plane on which to do the fit. Only a single Stokes parameter can be
00092         // specified.</li>
00093         // <li> maskInp - Mask (as LEL) to use as a way to specify which pixels to use </li>
00094         // <li> includepix - Pixel value range to include in the fit. includepix and excludepix
00095         // cannot be specified simultaneously. </li>
00096         // <li> excludepix - Pixel value range to exclude from fit</li>
00097         // <li> residualInp - Name of residual image to save. Blank means do not save residual image</li>
00098         // <li> modelInp - Name of the model image to save. Blank means do not save model image</li>
00099 
00100         // use these constructors when you already have a pointer to a valid ImageInterface object
00101 
00102         ImageFitter(
00103                 const ImageInterface<Float>* const &image, const String& region,
00104                 const Record *const regionRec,
00105                 const String& box="",
00106                 const String& chanInp="", const String& stokes="",
00107                 const String& maskInp="",
00108                 const Vector<Float>& includepix = Vector<Float>(0),
00109                 const Vector<Float>& excludepix = Vector<Float>(0),
00110                 const String& residualInp="", const String& modelInp="",
00111                 const String& estiamtesFilename="",
00112                 const String& newEstimatesInp="", const String& compListName="",
00113                 const CompListWriteControl writeControl=NO_WRITE
00114         );
00115 
00116         // destructor
00117         ~ImageFitter();
00118 
00119         // Do the fit. If componentList is specified, store the fitted components in
00120         // that object.
00121         ComponentList fit();
00122 
00123         inline String getClass() const {return _class;}
00124 
00125         // Did the fit converge for the specified channel?
00126         // Throw AipsError if the fit has not yet been done.
00127         // <src>plane</src> is relative to the first plane in the image chosen to be fit.
00128         Bool converged(uInt plane) const;
00129 
00130         // Did the fit converge?
00131         // Throw AipsError if the fit has not yet been done.
00132         // <src>plane</src> is relative to the first plane in the image chosen to be fit.
00133         Vector<Bool> converged() const;
00134 
00135         // set the zero level estimate. Implies fitting of zero level should be done. Must be
00136         // called before fit() to have an effect.
00137         void setZeroLevelEstimate(const Double estimate, const Bool isFixed);
00138 
00139         // Unset zero level (resets to zero). Implies fitting of zero level should not be done.
00140         // Call prior to fit().
00141         void unsetZeroLevelEstimate();
00142 
00143         // get the fitted result and error. Throws
00144         // an exception if the zero level was not fit for.
00145         void getZeroLevelSolution(vector<Double>& solution, vector<Double>& error);
00146 
00147 protected:
00148     virtual inline Bool _supportsMultipleRegions() {return True;}
00149 
00150 
00151 private:
00152         String _regionString, _residual, _model,
00153                 _estimatesString, _newEstimatesFileName, _compListName, _bUnit;
00154         Vector<Float> _includePixelRange, _excludePixelRange;
00155         ComponentList _estimates, _curResults;
00156         Vector<String> _fixed;
00157         Bool _fitDone, _noBeam, _doZeroLevel, _zeroLevelIsFixed;
00158         Vector<Bool> _fitConverged;
00159         Vector<Quantity> _peakIntensities, _peakIntensityErrors, _fluxDensityErrors,
00160                 _fluxDensities, _majorAxes, _majorAxisErrors, _minorAxes, _minorAxisErrors,
00161                 _positionAngles, _positionAngleErrors;
00162         Record _residStats, inputStats;
00163         Double chiSquared;
00164         String _kludgedStokes;
00165         CompListWriteControl _writeControl;
00166         Vector<uInt> _chanVec;
00167         uInt _curChan;
00168         Double _zeroLevelOffsetEstimate;
00169         vector<Double> _zeroLevelOffsetSolution, _zeroLevelOffsetError;
00170         Int _stokesPixNumber, _chanPixNumber;
00171 
00172         const static String _class;
00173 
00174         vector<ImageInputProcessor::OutputStruct> _getOutputs();
00175 
00176         vector<Coordinate::Type> _getNecessaryCoordinates() const;
00177 
00178         CasacRegionManager::StokesControl _getStokesControl() const;
00179 
00180         void _finishConstruction(const String& estimatesFilename);
00181 
00182         String _resultsHeader() const;
00183 
00184 
00185         // summarize the results in a nicely formatted string
00186         String _resultsToString();
00187 
00188         //summarize the size details in a nicely formatted string
00189         String _sizeToString(const uInt compNumber) const;
00190 
00191         String _fluxToString(uInt compNumber) const;
00192 
00193 
00194         String _spectrumToString(uInt compNumber) const;
00195 
00196         // write output to log file
00197         // void _writeLogfile(const String& output) const;
00198 
00199         // Write the estimates file using this fit.
00200         void _writeNewEstimatesFile() const;
00201 
00202         // Set the flux densities and peak intensities of the fitted components.
00203         void _setFluxes();
00204 
00205         // Set the convolved sizes of the fitted components.
00206         void _setSizes();
00207 
00208         void _getStandardDeviations(Double& inputStdDev, Double& residStdDev) const;
00209 
00210         void _getRMSs(Double& inputRMS, Double& residRMS) const;
00211 
00212         Double _getStatistic(const String& type, const uInt index, const Record& stats) const;
00213 
00214         String _statisticsToString() const;
00215 
00216         void setErrors(const Record& residStats);
00217 
00218         SubImage<Float> _createImageTemplate() const;
00219 
00220         void _writeCompList(ComponentList& list) const;
00221 
00222         void _setIncludeExclude(
00223             Fit2D& fitter
00224         ) const;
00225 
00226         void _fitsky(
00227                 Fit2D& fitter, Array<Float>& pixels,
00228             Array<Bool>& pixelMask, Bool& converged,
00229             Double& zeroLevelOffsetSolution,
00230             Double& zeroLevelOffsetError,
00231             const uInt& chan,
00232                 const Vector<String>& models,
00233                 const Bool fitIt,
00234                 const Bool deconvolveIt, const Bool list,
00235                 const Double zeroLevelEstimate
00236         );
00237 
00238         Vector<Double> _singleParameterEstimate(
00239                 Fit2D& fitter, Fit2D::Types model,
00240                 const MaskedArray<Float>& pixels, Float minVal,
00241                 Float maxVal, const IPosition& minPos, const IPosition& maxPos
00242         ) const;
00243 
00244         ComponentType::Shape _convertModelType(Fit2D::Types typeIn) const;
00245 
00246         void _fitskyExtractBeam(
00247                 Vector<Double>& parameters, const ImageInfo& imageInfo,
00248                 const Bool xIsLong, const CoordinateSystem& cSys
00249         ) const;
00250 
00251         void _encodeSkyComponentError(
00252                 LogIO& os, SkyComponent& sky,
00253                 Double facToJy, const ImageInterface<Float>& subIm,
00254                 const Vector<Double>& parameters, const Vector<Double>& errors,
00255                 Stokes::StokesTypes stokes, Bool xIsLong
00256         ) const;
00257 
00258 };
00259 }
00260 
00261 #endif