casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SpectralFitter.h
Go to the documentation of this file.
00001 //# SpectralCollapser.h: Header file for class SpectralCollapser
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: $
00027 
00028 #ifndef IMAGEANALYSIS_SPECTRALFITTER_H
00029 #define IMAGEANALYSIS_SPECTRALFITTER_H
00030 
00031 #include <components/SpectralComponents/SpectralList.h>
00032 #include <components/SpectralComponents/ProfileFit1D.h>
00033 
00034 #include <casa/namespace.h>
00035 
00036 namespace casa {
00037 
00038 template <class T> class ProfileFit1D;
00039 
00040 //class SpectralCoordinate;
00041 class SpectralList;
00042 class LogIO;
00043 
00044 class SpectralFitter {
00045         // <summary>
00046         // Does a simple fit to data in vectors. It is possible to specify weights (or errors)
00047         // for each element. A single Gaussian and a polynimial of order N are the only
00048         // component that can be fitted.
00049         // </summary>
00050 
00051         // <reviewed reviewer="" date="" tests="" demos="">
00052         // </reviewed>
00053 
00054         // <prerequisite>
00055         //   <li> ProfileFit1D
00056         // </prerequisite>
00057 
00058         // <etymology>
00059         // Created to fit components to spectra from the spectral profiler, hence
00060         // SpectralFitter.
00061         // </etymology>
00062 
00063         // <synopsis>
00064         // </synopsis>
00065 
00066 public:
00067         enum FitStatus {// report the status of the last fit
00068                 UNKNOWN,     // mostly means not fit has yet been executed
00069                 FAILED,
00070                 SUCCESS,
00071         };
00072 
00073         // default constructor
00074         SpectralFitter();
00075 
00076         // destructor
00077         virtual ~SpectralFitter();
00078 
00079         // Parameters:
00080         // <src>spcVals</src>  - independent values
00081         // <src>yVals</src>    - dependent values
00082         // <src>eVals</src>    - error values
00083         // <src>startVal</src> - lower boundary for independent values to be included in the fit
00084         // <src>endVal</src>   - upper boundary for independent values to be included in the fit
00085         // <src>fitGauss</src> - fit Gaussian component
00086         // <src>fitPoly</src>  - fit polynomial
00087         // <src>nPoly</src>    - order of polynomial to be fitted
00088         // <src>msg</src>      - message back to the calling routine
00089         virtual Bool fit(const Vector<Float> &spcVals, const Vector<Float> &yVals, const Vector<Float> &eVals,
00090                         const Float startVal, const Float endVal, const Bool fitGauss, const Bool fitPoly, const uInt nPoly, String &msg);
00091 
00092         // get the status of the last fit
00093         const SpectralFitter::FitStatus &getStatus(){return _fitStatus;};
00094 
00095         // get Chi Squared of the last fit
00096         Double getChiSquared () const {return _fit.getChiSquared();}
00097 
00098         // get number of iterations for the last fit
00099         Double getNumberIterations() const {return _fit.getNumberIterations();}
00100 
00101         const SpectralList &getList() const {return _fit.getList();};
00102 
00103         // get all values for the last fit
00104         Vector<Double> getFit() const {return _fit.getFit();};
00105 
00106         // get the values in the specified data range for the last fit
00107         void getFit(const Vector<Float> &spcVals, Vector<Float> &spcFit, Vector<Float> &yFit) const;
00108 
00109         // get all residuals for the last fit
00110         Vector<Double> getResidual() const {return _fit.getResidual();};
00111 
00112         // report on on the last fit to the
00113         // internal LogIO
00114         void report() const;
00115 
00116         // report on the last fit to a stream
00117         String report(LogIO &os, const String &xUnit="", const String &yUnit="", const String &yPrefixUnit="") const;
00118 
00119 private:
00120    LogIO *_log;
00121 
00122    ProfileFit1D<Double> _fit;
00123 
00124         SpectralFitter::FitStatus _fitStatus;
00125 
00126         Double _startVal;
00127         Double _endVal;
00128         uInt   _startIndex;
00129         uInt   _endIndex;
00130 
00131         String _resultMsg;
00132 
00133         // do all necessary setup
00134    void _setUp();
00135 
00136    // prepare the data which means give all data (independent, dependent, weights)
00137    // to the fitting class
00138    Bool _prepareData(const Vector<Float> &xVals, const Vector<Float> &eVals,
00139                 const Int &startIndex, const Int &endIndex, Vector<Bool> &maskVals, Vector<Double> &weightVals) const;
00140 
00141    //Bool _prepareElems(const Bool fitGauss, const Vector<Double> gPars, const Bool fitPoly,
00142    //           const uInt nPoly, const Vector<Double> pPars, SpectralList& list);
00143 
00144    // prepare the components that shall be fitted; this includes the setting
00145    // of reasonable initial parameters
00146    Bool _prepareElems(const Bool fitGauss, const Bool fitPoly, const uInt nPoly, Vector<Double> &xVals,
00147                 Vector<Double> &yVals, SpectralList& list);
00148 
00149    //void _report(const ProfileFit1D<Double> &fit, const Bool print, String &report) const;
00150 
00151    // report on the last fit to a stream
00152    //String _report(LogIO &os) const;
00153 
00154    // report on a list of spectral elements to a stream
00155    String _report(LogIO &os, const SpectralList &list, const String &xUnit="", const String &yUnit="", const String &yPrefixUnit="") const;
00156 };
00157 }
00158 
00159 #endif