00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef IMAGES_IMAGEFITTER_H
00027 #define IMAGES_IMAGEFITTER_H
00028
00029 #include <imageanalysis/ImageAnalysis/ImageTask.h>
00030
00031 #include <components/ComponentModels/ComponentList.h>
00032 #include <lattices/LatticeMath/Fit2D.h>
00033
00034 #include <imageanalysis/IO/ImageFitterResults.h>
00035
00036 namespace casa {
00037
00038 class ImageFitter : public ImageTask<Float> {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 public:
00069
00070 ImageFitter() = delete;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 ImageFitter(
00097 const SPCIIF image, const String& region,
00098 const Record *const ®ionRec,
00099 const String& box="",
00100 const String& chanInp="", const String& stokes="",
00101 const String& maskInp="",
00102 const String& estiamtesFilename="",
00103 const String& newEstimatesInp="", const String& compListName=""
00104 );
00105
00106 ImageFitter(const ImageFitter& other) = delete;
00107
00108
00109 ~ImageFitter();
00110
00111
00112
00113
00114
00115 std::pair<ComponentList, ComponentList> fit();
00116
00117 void setWriteControl(ImageFitterResults::CompListWriteControl x) { _writeControl = x; }
00118
00119 inline String getClass() const {return _class;}
00120
00121
00122
00123
00124 Bool converged(uInt plane) const;
00125
00126
00127
00128
00129 Vector<Bool> converged() const;
00130
00131
00132
00133 void setZeroLevelEstimate(Double estimate, Bool isFixed);
00134
00135
00136
00137 void unsetZeroLevelEstimate();
00138
00139
00140
00141 void getZeroLevelSolution(vector<Double>& solution, vector<Double>& error);
00142
00143
00144 void setRMS(const Quantity& rms);
00145
00146 void setIncludePixelRange(const std::pair<Float, Float>& r) {
00147 _includePixelRange.reset(new std::pair<Float, Float>(r));
00148 }
00149
00150 void setExcludePixelRange(const std::pair<Float, Float>& r) {
00151 _excludePixelRange.reset(new std::pair<Float, Float>(r));
00152 }
00153
00154
00155 void setModel(const String& m) { _model = m; }
00156
00157
00158 void setResidual(const String& r) { _residual = r; }
00159
00160
00161 void setNoiseFWHM(const Quantity& q);
00162
00163
00164 void setNoiseFWHM(Double d);
00165
00166
00167
00168
00169 void clearNoiseFWHM();
00170
00171
00172 Record getOutputRecord() const {return _output; }
00173
00174
00175 void setSummaryFile(const String& f) { _summary = f; }
00176
00177 protected:
00178
00179 virtual Bool _hasLogfileSupport() const { return True; }
00180
00181 virtual inline Bool _supportsMultipleRegions() const {return True;}
00182
00183 private:
00184
00185 using Angular2DGaussian = GaussianBeam;
00186
00187 String _regionString;
00188 String _residual{}, _model{}, _estimatesString{}, _summary{};
00189 String _newEstimatesFileName, _compListName, _bUnit;
00190 SHARED_PTR<std::pair<Float, Float> > _includePixelRange{}, _excludePixelRange{};
00191 ComponentList _estimates{}, _curConvolvedList, _curDeconvolvedList;
00192 Vector<String> _fixed{}, _deconvolvedMessages;
00193 Bool _fitDone{False}, _noBeam{False}, _doZeroLevel{}, _zeroLevelIsFixed{},
00194 _correlatedNoise, _useBeamForNoise;
00195 Vector<Bool> _fitConverged{};
00196 Vector<Quantity> _peakIntensities{}, _peakIntensityErrors, _fluxDensityErrors,
00197 _fluxDensities, _majorAxes, _majorAxisErrors, _minorAxes, _minorAxisErrors,
00198 _positionAngles, _positionAngleErrors;
00199 vector<Quantity> _allConvolvedPeakIntensities{}, _allConvolvedPeakIntensityErrors{}, _allSums{},
00200 _allFluxDensities{}, _allFluxDensityErrors{};
00201 vector<GaussianBeam> _allBeams;
00202 vector<Double> _allBeamsPix, _allBeamsSter;
00203 vector<uInt> _allChanNums;
00204 vector<Bool> _isPoint;
00205 Record _residStats, inputStats, _output;
00206 Double _rms = -1;
00207 String _kludgedStokes;
00208 ImageFitterResults::CompListWriteControl _writeControl = ImageFitterResults::NO_WRITE;
00209 Vector<uInt> _chanVec;
00210 uInt _curChan;
00211 Double _zeroLevelOffsetEstimate = 0;
00212 vector<Double> _zeroLevelOffsetSolution, _zeroLevelOffsetError;
00213 Int _stokesPixNumber = -1, _chanPixNumber = -1;
00214 ImageFitterResults _results;
00215 std::unique_ptr<Quantity> _noiseFWHM{};
00216 Quantity _pixWidth{0, "arcsec"};
00217
00218 const static String _class;
00219
00220 void _fitLoop(
00221 Bool& anyConverged, ComponentList& convolvedList,
00222 ComponentList& deconvolvedList, SPIIF templateImage,
00223 SPIIF residualImage, SPIIF modelImage,
00224 String& resultsString
00225 );
00226
00227 vector<OutputDestinationChecker::OutputStruct> _getOutputStruct();
00228
00229 vector<Coordinate::Type> _getNecessaryCoordinates() const;
00230
00231 CasacRegionManager::StokesControl _getStokesControl() const;
00232
00233 void _finishConstruction(const String& estimatesFilename);
00234
00235
00236
00237
00238 String _resultsToString(uInt nPixels) const;
00239
00240
00241 String _sizeToString(const uInt compNumber) const;
00242
00243 String _spectrumToString(uInt compNumber) const;
00244
00245 void _setDeconvolvedSizes();
00246
00247 void _getStandardDeviations(Double& inputStdDev, Double& residStdDev) const;
00248
00249 void _getRMSs(Double& inputRMS, Double& residRMS) const;
00250
00251 Double _getStatistic(const String& type, const uInt index, const Record& stats) const;
00252
00253 String _statisticsToString() const;
00254
00255 SPIIF _createImageTemplate() const;
00256
00257 void _writeCompList(ComponentList& list) const;
00258
00259 void _setIncludeExclude(
00260 Fit2D& fitter
00261 ) const;
00262
00263 void _fitsky(
00264 Fit2D& fitter, Array<Float>& pixels,
00265 Array<Bool>& pixelMask, Bool& converged,
00266 Double& zeroLevelOffsetSolution,
00267 Double& zeroLevelOffsetError,
00268 std::pair<Int, Int>& pixelOffsets,
00269 const Vector<String>& models, Bool fitIt,
00270 Bool deconvolveIt,
00271 Double zeroLevelEstimate
00272 );
00273
00274 Vector<Double> _singleParameterEstimate(
00275 Fit2D& fitter, Fit2D::Types model,
00276 const MaskedArray<Float>& pixels, Float minVal,
00277 Float maxVal, const IPosition& minPos, const IPosition& maxPos
00278 ) const;
00279
00280 ComponentType::Shape _convertModelType(Fit2D::Types typeIn) const;
00281
00282 void _fitskyExtractBeam(
00283 Vector<Double>& parameters, const ImageInfo& imageInfo,
00284 const Bool xIsLong, const CoordinateSystem& cSys
00285 ) const;
00286
00287 void _encodeSkyComponentError(
00288 SkyComponent& sky, Double facToJy, const CoordinateSystem& csys,
00289 const Vector<Double>& parameters, const Vector<Double>& errors,
00290 Stokes::StokesTypes stokes, Bool xIsLong
00291 ) const;
00292
00293 void _doConverged(
00294 ComponentList& convolvedList, ComponentList& deconvolvedList,
00295 Double& zeroLevelOffsetEstimate, std::pair<Int, Int>& pixelOffsets,
00296 SPIIF& residualImage, SPIIF& modelImage,
00297 SHARED_PTR<TempImage<Float> >& tImage,
00298 SHARED_PTR<ArrayLattice<Bool> >& initMask,
00299 Double zeroLevelOffsetSolution, Double zeroLevelOffsetError,
00300 Bool hasSpectralAxis, Int spectralAxisNumber, Bool outputImages, const IPosition& planeShape,
00301 const Array<Float>& pixels, const Array<Bool>& pixelMask, const Fit2D& fitter,
00302 SPIIF templateImage
00303 );
00304
00305 Quantity _pixelWidth();
00306
00307 void _calculateErrors();
00308
00309 Double _getRMS() const;
00310
00311 Double _correlatedOverallSNR(
00312 uInt comp, Double a, Double b, Double signalToNoise
00313 ) const;
00314
00315 GaussianBeam _getCurrentBeam() const;
00316
00317 void _createOutputRecord(const ComponentList& convolved, const ComponentList& decon);
00318
00319 void _setSum(const SkyComponent& comp, const SubImage<Float>& im, uInt compNum);
00320
00321 void _setBeam(GaussianBeam& beam, uInt ngauss);
00322 };
00323 }
00324
00325 #endif