8 #ifndef mx_astro_astroSpectrum_hpp
9 #define mx_astro_astroSpectrum_hpp
14 #include "../sys/environment.hpp"
15 #include "../ioutils/readColumns.hpp"
16 #include "../math/gslInterpolation.hpp"
17 #include "constants.hpp"
29 template<
typename realT>
65 template<
typename compSpectrumT>
103 template<
typename compSpectrumT>
104 realT
mean(
const compSpectrumT & T )
106 realT sum1 = 0, sum2 = 0;
137 std::vector<realT> & lambda
140 size_t N = lambda.size();
141 realT dl = lambda[1] - lambda[0];
142 realT half =
static_cast<realT
>(0.5);
145 for(
int i=0; i< N; ++i)
151 for(
int i=1; i< N-1; ++i) weff +=
_spectrum[i];
156 for(
int i=1; i< N-1; ++i) lambda0 += lambda[i]*
_spectrum[i];
157 lambda0 += half*lambda[N-1]*
_spectrum[N-1];
158 lambda0 *= dl/max/weff;
163 while(i < lambda.size())
196 template<
class filterT>
200 const realT & lambda_0,
201 const std::vector<realT> & lambda,
202 const filterT & trans
205 charFlux(flambda0, fnu0, fphot0, lambda_0, lambda, trans._spectrum);
223 const realT & lambda_0,
224 const std::vector<realT> & lambda,
225 const std::vector<realT> & trans
228 constexpr realT
h = constants::h<units::si<realT>>();
229 constexpr realT
c = constants::c<units::si<realT>>();
231 size_t N = lambda.size();
232 realT dl = lambda[1] - lambda[0];
233 realT half =
static_cast<realT
>(0.5);
237 realT denom = half*trans[0]*lambda[0];
238 for(
int i=1; i< N-1; ++i) denom += trans[i]*lambda[i];
239 denom += half*trans[N-1]*lambda[N-1];
242 realT num = half*
_spectrum[0]*trans[0]*lambda[0];
243 for(
int i=1; i< N-1; ++i) num +=
_spectrum[i]*trans[i]*lambda[i];
244 num += half*
_spectrum[N-1]*trans[N-1]*lambda[N-1];
247 flambda0 = num / denom;
251 denom = half*trans[0]/lambda[0];
252 for(
int i=1; i< N-1; ++i) denom += trans[i]/lambda[i];
253 denom += half*trans[N-1]/lambda[N-1];
260 fphot0 = flambda0 * lambda_0/(
h*
c);
278 template<
typename _spectrumT,
bool freq=false>
281 typedef _spectrumT spectrumT;
282 typedef typename spectrumT::units units;
283 typedef typename units::realT realT;
284 typedef typename spectrumT::paramsT paramsT;
301 const std::string & dataDir
312 if(spectrumT::dataDirEnvVar)
326 const std::string & dataDir
336 template<
typename gr
idT>
339 std::vector<realT> rawLambda;
340 std::vector<realT> rawSpectrum;
342 std::string fileName = spectrumT::fileName(
_params);
346 if(fileName.size() < 1)
348 mxError(
"astroSpectrum", MXE_PARAMNOTSET,
"fileName is empty");
352 if(
_dataDir ==
"" && fileName[0] ==
'/')
363 if( spectrumT::readSpectrum(rawLambda, rawSpectrum, path,
_params) < 0)
369 for(
int i=0; i < rawLambda.size(); ++i)
371 rawLambda[i] /= spectrumT::wavelengthUnits/(units::length);
372 rawSpectrum[i] /= spectrumT::fluxUnits/(units::energy/(units::time * units::length * units::length * units::length));
377 for(
int i=0; i < lambda.size(); ++i)
constexpr units::realT c()
The speed of light.
constexpr units::realT h()
Planck Constant.
int gsl_interpolate(const gsl_interp_type *interpT, realT *xin, realT *yin, size_t Nin, realT *xout, realT *yout, size_t Nout)
Interpolate a 1-D data X vs Y discrete function onto a new X axis.
std::string getEnv(const std::string &estr)
Return the value of an environment variable.
Class to manage an astronomical spectrum.
astroSpectrum()
Default c'tor.
paramsT _params
The parameters of the spectrum, e.g. its name or the numerical parameters needed to generate the name...
int setSpectrum(gridT &lambda)
Load the spectrum and interpolate it onto a wavelength scale.
int setParameters(const paramsT ¶ms)
Set the parameters of the spectrum, using the underlying spectrums parameter type.
int setParameters(const paramsT ¶ms, const std::string &dataDir)
Set the parameters of the spectrum, using the underlying spectrum's parameter type.
astroSpectrum(const paramsT ¶ms, const std::string &dataDir)
Constructor specifying name and data directory.
std::string _dataDir
The directory containing the spectrum.
astroSpectrum(const paramsT ¶ms)
Constructor specifying name, the enivronment will be queried for data directory.
Base spectrum class which provides manipulation and characterization functionality.
size_t size()
Get the current size of the spectrum.
const realT operator[](size_t i) const
Access a single point in the spectrum, specified by its vector index.
void charTrans(realT &lambda0, realT &weff, realT &max, realT &fwhm, std::vector< realT > &lambda)
Characterize the spectrum as a filter transmission curve.
realT & operator[](size_t i)
Access a single point in the spectrum, specified by its vector index.
baseSpectrum< realT > operator*(const compSpectrumT &spec)
Multiply two spectra together.
realT mean()
Calculate the mean value of the spectrum.
void charFlux(realT &flambda0, realT &fnu0, realT &fphot0, const realT &lambda_0, const std::vector< realT > &lambda, const std::vector< realT > &trans)
Characterize the flux densities of the spectrum w.r.t. a filter transmission curve.
void charFlux(realT &flambda0, realT &fnu0, realT &fphot0, const realT &lambda_0, const std::vector< realT > &lambda, const filterT &trans)
Characterize the flux densities of the spectrum w.r.t. a filter transmission curve.
std::vector< realT > _spectrum
Contains the spectrum after it is set.
realT mean(const compSpectrumT &T)
Calculate the mean value of the spectrum when mutiplied by another.
Unit specifications and conversions.