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