source: branches/casa-release-4_3/external-alma/components/SpectralComponents/SpectralElement.cc@ 3118

Last change on this file since 3118 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: 6.4 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: SpectralElement.cc 21451 2014-06-10 07:48:08Z gervandiepen $
27
28#include <components/SpectralComponents/SpectralElement.h>
29
30#include <casa/BasicSL/Constants.h>
31#include <casa/BasicSL/String.h>
32#include <casa/Arrays/ArrayLogical.h>
33#include <casa/Exceptions/Error.h>
34#include <casa/Utilities/MUString.h>
35#include <scimath/Mathematics/AutoDiffMath.h>
36#include <scimath/Functionals/Function.h>
37
38//debug only
39#include <scimath/Functionals/CompiledFunction.h>
40#include <casa/Arrays/ArrayIO.h>
41
42#include <casa/iostream.h>
43
44namespace casa { //# NAMESPACE CASA - BEGIN
45
46SpectralElement::SpectralElement(SpectralElement::Types type, const Vector<Double>& parms)
47 : _type(type), _params(parms), _errors(parms.size(), 0),
48 _fixed(parms.size(), False) {}
49
50
51SpectralElement::SpectralElement(const SpectralElement &other)
52: _type(other._type), _params(other._params.copy()), _errors(other._errors.copy()),
53 _fixed(other._fixed.copy()),
54 _function(
55 std::tr1::shared_ptr<Function<Double, Double> >(
56 other._function->clone()
57 )
58 ) {}
59
60SpectralElement::~SpectralElement() {}
61
62SpectralElement &SpectralElement::operator=(
63 const SpectralElement &other
64) {
65 if (this != &other) {
66 _type = other._type;
67 uInt n = other._params.size();
68 _params.resize(n);
69 _params = other._params.copy();
70 _errors.resize(n);
71 _errors = other._errors.copy();
72 _fixed.resize(n);
73 _fixed = other._fixed.copy();
74 _function = std::tr1::shared_ptr<Function<Double, Double> >(
75 other._function->clone()
76 );
77 }
78 return *this;
79}
80
81Double SpectralElement::operator[](const uInt n) const {
82 if (n >= _params.size()) {
83 throw(AipsError("SpectralElement: Illegal index for parameter"));
84 }
85 return _params[n];
86}
87
88Bool SpectralElement::operator==(
89 const SpectralElement& other
90) const {
91 if (this == &other) {
92 return True;
93 }
94 return (
95 _type == other._type && allNear(_params, other._params, 1e-8)
96 && allNear(_errors, other._errors, 1e-8)
97 && allTrue(_fixed == other._fixed)
98 );
99}
100
101const String* SpectralElement::allTypes(
102 Int &nall, const SpectralElement::Types *&typ
103) {
104 static const String tname[SpectralElement::N_Types] = {
105 String("GAUSSIAN"),
106 String("POLYNOMIAL"),
107 String("COMPILED"),
108 String("GAUSSIAN MULTIPLET"),
109 String("LORENTZIAN"),
110 String("POWER LOGARITHMIC POLYNOMIAL"),
111 String("LOGARITHMIC TRANSFORMED POLYNOMIAL")
112
113 };
114
115 static const SpectralElement::Types oname[SpectralElement::N_Types] = {
116 SpectralElement::GAUSSIAN,
117 SpectralElement::POLYNOMIAL,
118 SpectralElement::COMPILED,
119 SpectralElement::GMULTIPLET,
120 SpectralElement::LORENTZIAN,
121 SpectralElement::POWERLOGPOLY,
122 SpectralElement::LOGTRANSPOLY
123
124 };
125
126 nall = SpectralElement::N_Types;
127 typ = oname;
128 return tname;
129}
130
131void SpectralElement::_set(const Vector<Double>& params) {
132 _params = params.copy();
133 for (uInt i=0; i<params.size(); i++) {
134 (*_function)[i] = params[i];
135 }
136}
137
138void SpectralElement::_setType(const SpectralElement::Types type) {
139 _type = type;
140}
141
142const String &SpectralElement::fromType(SpectralElement::Types tp) {
143 Int nall;
144 const SpectralElement::Types *typ;
145 const String *const tname = SpectralElement::allTypes(nall, typ);
146 return tname[tp];
147}
148
149Bool SpectralElement::toType(
150 SpectralElement::Types &tp, const String &typname
151) {
152 Int nall;
153 const SpectralElement::Types *typ;
154 const String *const tname = SpectralElement::allTypes(nall, typ);
155
156 // Make sure a value returned
157 tp = typ[0];
158 Int i = MUString::minimaxNC(typname, SpectralElement::N_Types, tname);
159 if (i >= nall) {
160 return False;
161 }
162 tp = typ[i];
163 return True;
164}
165
166void SpectralElement::_setFunction(
167 const std::tr1::shared_ptr<Function<Double, Double> >& f
168) {
169 _function = f;
170}
171
172Double SpectralElement::operator()(const Double x) const {
173 return (*_function)(x);
174}
175
176void SpectralElement::get(Vector<Double> &param) const {
177 param = _params.copy();
178}
179
180Vector<Double> SpectralElement::get() const {
181 return _params.copy();
182}
183
184void SpectralElement::getError(Vector<Double> &err) const {
185 err = _errors.copy();
186}
187
188Vector<Double> SpectralElement::getError() const {
189 return _errors.copy();
190}
191
192void SpectralElement::setError(const Vector<Double> &err) {
193 if (err.nelements() != _params.nelements()) {
194 throw(
195 AipsError(
196 "SpectralElement: setting incorrect "
197 "number of errors in the element"
198 )
199 );
200 }
201 _errors = err.copy();
202}
203
204void SpectralElement::fix(const Vector<Bool> &fix) {
205 if (fix.nelements() != _params.nelements()) {
206 throw(
207 AipsError(
208 "SpectralElement: setting incorrect number of fixed "
209 "in the element"
210 )
211 );
212 }
213 _fixed = fix.copy();
214 for (uInt i=0; i<_fixed.size(); i++) {
215 _function->mask(i) = fix[i];
216 }
217}
218
219const Vector<Bool>& SpectralElement::fixed() const {
220 return _fixed;
221}
222
223Bool SpectralElement::toRecord(RecordInterface &out) const {
224 out.define(RecordFieldId("type"), fromType(_type));
225 Vector<Double> ptmp(_params);
226 Vector<Double> etmp(_params);
227 out.define(RecordFieldId("parameters"), _params);
228 out.define(RecordFieldId("errors"), _errors);
229 out.define(RecordFieldId("fixed"), _fixed);
230 return True;
231}
232
233void SpectralElement::set(const Vector<Double>& params) {
234 if (params.size() != get().size()) {
235 throw AipsError(
236 "Input number of parameters does not match "
237 "the current number of parameters"
238 );
239 }
240 _set(params);
241}
242
243} //# NAMESPACE CASA - END
244
245
Note: See TracBrowser for help on using the repository browser.