source: trunk/external-alma/components/SpectralComponents/GaussianSpectralElement.cc @ 2980

Last change on this file since 2980 was 2980, checked in by Malte Marquarding, 10 years ago

Add a copy of casacore/components/SpectralComponents to external-alma directory to prepare for its removal from casacore-trunk. DOn;t activate in SConscript yet.

File size: 5.0 KB
Line 
1//# SpectralElement.cc: Describes (a set of related) spectral lines
2//# Copyright (C) 2001,2004
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: GaussianSpectralElement.cc 21451 2014-06-10 07:48:08Z gervandiepen $
27
28//# Includes
29#include <components/SpectralComponents/GaussianSpectralElement.h>
30
31#include <casa/BasicSL/Constants.h>
32
33#include <scimath/Functionals/Gaussian1D.h>
34
35#include <casa/iostream.h>
36
37namespace casa { //# NAMESPACE CASA - BEGIN
38
39//# Constants
40const Double GaussianSpectralElement::SigmaToFWHM = sqrt(8.0*C::ln2);
41
42GaussianSpectralElement::GaussianSpectralElement()
43: PCFSpectralElement(SpectralElement::GAUSSIAN, 1, 0, 2*sqrt(C::ln2)/C::pi) {
44        _setFunction(
45                std::tr1::shared_ptr<Gaussian1D<Double> >(
46                        new Gaussian1D<Double>(1, 0, 1)
47                )
48        );
49}
50
51
52GaussianSpectralElement::GaussianSpectralElement(
53        const Double ampl, const Double center, const Double sigma
54) : PCFSpectralElement(SpectralElement::GAUSSIAN, ampl, center, sigma) {
55        _setFunction(
56                std::tr1::shared_ptr<Gaussian1D<Double> >(
57                        new Gaussian1D<Double>(ampl, center, sigma*SigmaToFWHM)
58                )
59        );
60}
61
62GaussianSpectralElement::GaussianSpectralElement(
63        const Vector<Double> &param
64) : PCFSpectralElement(SpectralElement::GAUSSIAN, param) {
65        std::tr1::shared_ptr<Gaussian1D<Double> >(
66                new Gaussian1D<Double>(param[AMP], param[CENTER], param[WIDTH]*SigmaToFWHM)
67        );
68}
69
70GaussianSpectralElement::GaussianSpectralElement(
71        const GaussianSpectralElement &other
72) : PCFSpectralElement(other) {}
73
74GaussianSpectralElement::~GaussianSpectralElement() {}
75
76
77SpectralElement* GaussianSpectralElement::clone() const {
78        return new GaussianSpectralElement(*this);
79}
80
81Double GaussianSpectralElement::getSigma() const {
82        return get()[2];
83}
84
85Double GaussianSpectralElement::getFWHM() const {
86        return sigmaToFWHM(get()[WIDTH]);
87}
88
89Double GaussianSpectralElement::getSigmaErr() const {
90        return getError()[WIDTH];
91}
92
93Double GaussianSpectralElement::getFWHMErr() const {
94        return sigmaToFWHM(getError()[WIDTH]);
95}
96
97void GaussianSpectralElement::setSigma(Double sigma) {
98        if (sigma < 0) {
99                sigma *= -1;
100        }
101        Vector<Double> p = get();
102        p[WIDTH] = sigma;
103        _set(p);
104        Vector<Double> err = getError();
105        err[WIDTH] = 0;
106        setError(err);
107}
108
109void GaussianSpectralElement::set(const Vector<Double>& v) {
110        PCFSpectralElement::set(v);
111        (*_getFunction())[WIDTH] = sigmaToFWHM(v[WIDTH]);
112}
113
114void GaussianSpectralElement::_set(const Vector<Double>& v) {
115        PCFSpectralElement::_set(v);
116        (*_getFunction())[WIDTH] = sigmaToFWHM(v[WIDTH]);
117}
118
119void GaussianSpectralElement::setFWHM(Double fwhm) {
120        setSigma(sigmaFromFWHM(fwhm));
121}
122
123void GaussianSpectralElement::fixSigma(const Bool isFixed) {
124        Vector<Bool> myFixed = fixed();
125        myFixed[WIDTH] = isFixed;
126        fix(myFixed);
127}
128
129Bool GaussianSpectralElement::fixedSigma() const {
130        return fixed()[WIDTH];
131}
132
133Double GaussianSpectralElement::getIntegral() const {
134        static const Double c = sqrt(C::pi_4/C::ln2);
135        return c * getAmpl() * getFWHM();
136}
137
138Bool GaussianSpectralElement::toRecord(
139        RecordInterface& out
140) const {
141        PCFSpectralElement::toRecord(out);
142        Vector<Double> p = out.asArrayDouble("parameters");
143        Vector<Double> e = out.asArrayDouble("errors");
144        p[2] = getFWHM();
145        out.define("parameters", p);
146        e[2] = getFWHMErr();
147        out.define("errors", e);
148        return True;
149}
150
151ostream &operator<<(ostream &os, const GaussianSpectralElement &elem) {
152        os << SpectralElement::fromType((elem.getType())) << " element: " << endl;
153    os << "  Amplitude: " << elem.getAmpl() << ", " << elem.getAmplErr();
154    if (elem.fixedAmpl()) os << " (fixed)";
155    os << endl << "  Center:    " << elem.getCenter() << ", " << elem.getCenterErr();
156    if (elem.fixedCenter()) os << " (fixed)";
157    os << endl << "  Sigma:     " << elem.getSigma() << ", " << elem.getSigmaErr();
158    if (elem.fixedSigma()) os << " (fixed)";
159    os << endl;
160    return os;
161}
162
163Double GaussianSpectralElement::sigmaToFWHM (const Double sigma) {
164   return SigmaToFWHM * sigma;
165}
166
167Double GaussianSpectralElement::sigmaFromFWHM(const Double fwhm) {
168  return fwhm / SigmaToFWHM;
169}
170
171} //# NAMESPACE CASA - END
172
Note: See TracBrowser for help on using the repository browser.