mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
astroSpectra.hpp
Go to the documentation of this file.
1/** \file astroSpectra.hpp
2 * \author Jared R. Males
3 * \brief Type definitions for the astronomical spectral libraries.
4 * \ingroup astrophot
5 *
6 */
7
8#ifndef mx_astro_astroSpectra_hpp
9#define mx_astro_astroSpectra_hpp
10
11#include "units.hpp"
12#include "../ioutils/readColumns.hpp"
13
14namespace mx
15{
16namespace astro
17{
18
19/// A basic spectrum
20/**
21 * \ingroup astrophot_spectra
22 */
23template <typename _units>
25{
26 typedef _units units;
27 typedef typename units::realT realT;
28
29 static const bool freq = false;
30
31 typedef std::string paramsT; ///< The parameter is a string name.
32
33 /// Specify how to convert to SI wavelength units. No conversions are performed in the basic spectrum
34 static constexpr realT wavelengthUnits = static_cast<realT>( 1 );
35
36 /// Specify how to convert to SI flux units. No conversions are performed in the basic spectrum
37 static constexpr realT fluxUnits = static_cast<realT>( 1 );
38
39 /// The data directory environment variable name.
40 static constexpr const char *dataDirEnvVar = 0;
41
42 /// This function should calculate the file name (without the path) for the spectrum parameters.
43 static std::string
44 fileName( const paramsT &name /**< [in] The parameters of the spectrum, in this case just its file name.*/ )
45 {
46 return name;
47 }
48
49 /// This function reads the spectrum, returning its raw wavelength and spectrum points.
50 /** No unit conversions or interpolations should take place.
51 */
53 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
54 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
55 const std::string &path, ///< [in] the full path to the file.
56 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
57 )
58 {
59 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
60 }
61};
62
63/// A spectrum from the astroFilt filter library
64/**
65 * \ingroup astrophot_spectra
66 */
67template <typename _units, bool _rsr = true>
69{
70 typedef _units units;
71 typedef typename units::realT realT;
72
73 static const bool freq = false;
74
75 typedef std::string paramsT; ///< The astroFilters are parameterized by name.
76
77 /// Convert from um to SI m
78 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
79
80 /// No conversion is performed on filter transmission.
81 static constexpr realT fluxUnits = static_cast<realT>( 1 );
82
83 static constexpr const char *dataDirEnvVar = "ASTROFILT_DATADIR";
84
85 static std::string fileName( const std::string &name )
86 {
87 return name + ".dat";
88 }
89
91 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
92 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
93 const std::string &path, ///< [in] the full path to the file.
94 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
95 )
96 {
97 mx::error_t errc = mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
98 if(!!errc)
99 {
100 return errc;
101 }
102
103 realT max = 0;
104
105 for( int i = 0; i < rawLambda.size(); ++i )
106 {
107 // if(_rsr) rawSpectrum[i] = rawSpectrum[i]*rawLambda[i];
108 if( rawSpectrum[i] > max )
109 max = rawSpectrum[i];
110 }
111
112 for( int i = 0; i < rawLambda.size(); ++i )
113 {
114 rawSpectrum[i] /= max;
115 }
116
117 return error_t::noerror;
118 }
119};
120
121/// A square-wave filter spectrum
122/** Parameters specify the central wavelength, width, and sampling (all in microns) of a square-wave type filter.
123 *
124 * To create this filter:
125 * \code
126 * typedef double realT;
127 *
128 * realT lam0 = 0.5; //Central wavelength in microns
129 * realT fw = 0.05; //Full-width, in microns.
130 * realT dlam = 0.001; //Delta-lambda for specifying the defining points. See note.
131 *
132 * astroSpectrum<sqWaveFilter<units::si<realT>>> filt({ lam0, fw, dlam});
133 *
134 * filt.setSpectrum(grid_meters); // grid_meters is a vector<realT> specifying the wavelength grid in meters
135 *
136 * \endcode
137 *
138 * Note that dlam specifies how sharp the filter edges are when interpolated. Larger values will make the filter more
139 * trapezoidal.
140 *
141 * \ingroup astrophot_spectra
142 */
143template <typename _units, bool _rsr = true>
145{
146 typedef _units units;
147 typedef typename units::realT realT;
148
149 static const bool freq = false;
150
151 /// The square wave is parameterized by the central wavelength, width, and sampling (all in microns).
152 typedef struct
153 {
154 realT lam0; ///< The central Wavelength in microns
155 realT fw; ///< The full width of the filter in microns
156 realT dlam; ///< The wavelength sampling to use in microns.
157 } paramsT;
158
159 /// Convert from um to SI m
160 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
161
162 /// No conversion is performed on filter transmission.
163 static constexpr realT fluxUnits = static_cast<realT>( 1 );
164
165 static constexpr const char *dataDirEnvVar = 0;
166
167 static std::string fileName( const paramsT &params )
168 {
169 return " "; // must not be empty to avoid error
170 }
171
173 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
174 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
175 const std::string &path, ///< [in] the full path to the file.
176 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
177 )
178 {
179 rawLambda.resize( 4 );
180 rawSpectrum.resize( 4 );
181
182 rawLambda[0] = params.lam0 - 0.5 * params.fw - 0.5 * params.dlam;
183 rawSpectrum[0] = 0.0;
184
185 rawLambda[1] = params.lam0 - 0.5 * params.fw + 0.5 * params.dlam;
186 rawSpectrum[1] = 1.0;
187
188 rawLambda[2] = params.lam0 + 0.5 * params.fw - 0.5 * params.dlam;
189 rawSpectrum[2] = 1.0;
190
191 rawLambda[3] = params.lam0 + 0.5 * params.fw + 0.5 * params.dlam;
192 rawSpectrum[3] = 0.0;
193
194 return error_t::noerror;
195 }
196};
197
198/// A spectrum from the HST calspec library
199/** See http://www.stsci.edu/hst/observatory/crds/calspec.html
200 *
201 * \ingroup astrophot_spectra
202 */
203template <typename _units>
205{
206 typedef _units units;
207 typedef typename units::realT realT;
208
209 static const bool freq = false;
210
211 typedef std::string paramsT; ///< The calspec Spectra are parameterized by star name.
212
213 /// Convert from A to SI m
214 static constexpr realT wavelengthUnits = static_cast<realT>( 1e10 );
215
216 /// Convert from erg s-1 cm-2 A-1 to SI W m-3
217 static constexpr realT fluxUnits =
218 static_cast<realT>( 1e7 ) / ( static_cast<realT>( 1e4 ) * static_cast<realT>( 1e10 ) );
219
220 static constexpr const char *dataDirEnvVar = "CALSPEC_DATADIR";
221
222 /// The file name is found from the star's name.
223 static std::string fileName( const std::string &name )
224 {
225 if( name == "alpha_lyr" || name == "vega" )
226 return "alpha_lyr_stis_005.asc";
227 else if( name == "1740346" )
228 return "1740346_nic_002.ascii";
229 else if( name == "sun" || name == "sun_reference" )
230 return "sun_reference_stis.002.asc";
231 }
232
233 /// Read a CALSPEC spectrum, which is a simple two column ASCII format.
235 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
236 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
237 const std::string &path, ///< [in] the full path to the file.
238 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
239 )
240 {
241 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
242 }
243};
244
245/// A spectrum from the Pickles library
246/**
247 * \ingroup astrophot_spectra
248 */
249template <typename _units>
251{
252 typedef _units units;
253 typedef typename units::realT realT;
254
255 static const bool freq = false;
256
257 typedef std::string paramsT; ///< The Pickles spectra are parameterized by a spectral type string.
258
259 /// Convert from A to SI m
260 static constexpr realT wavelengthUnits = static_cast<realT>( 1e10 );
261
262 /// The Pickles spectra are dimensionless.
263 static constexpr realT fluxUnits = static_cast<realT>( 1 );
264
265 /// The Pickles spectra location is specified by the PICKLES_DATADIR environment variable.
266 static constexpr const char *dataDirEnvVar = "PICKLES_DATADIR";
267
268 /// The name of the datafile is constructed from its spectral type string.
269 static std::string fileName( const std::string &spt )
270 {
271 return "uk" + ioutils::toLower( spt ) + ".dat";
272 }
273
274 /// Read a Pickles spectrum, which for these purposes is a simple two column ASCII format.
276 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
277 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
278 const std::string &path, ///< [in] the full path to the file.
279 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
280 )
281 {
282 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
283 }
284};
285
286/// Earth Albedo Spectra
287/** The spectra can be one of:
288 * - "EPOXI" returns the apparent albedo spectrum from Cowan and Strait (2013) \cite cowan_2013.
289 * - "RawEarthshine" returns the unormalized albedo spectrum measured using Earthshine by Turnbull et al (2006) \cite
290 * turnbull_20016.
291 * - "Earthshine" returns the Earthshine spectrum normalized to match the EPOXI result of 0.27 in the 550 nm band.
292 *
293 * \ingroup astrophot_spectra
294 */
295template <typename _units>
297{
298 typedef _units units;
299 typedef typename units::realT realT;
300
301 static const bool freq = false;
302
303 typedef std::string paramsT; ///< The name of the spectrum can be "EPOXI", "Earthshine", or "RawEarthshine".
304
305 /// Convert from A to SI m
306 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
307
308 /// The Earthshine is a dimensionless albedo.
309 static constexpr realT fluxUnits = static_cast<realT>( 1 );
310
311 /// The location is specified by the EARTHSHINE_DATADIR environment variable.
312 static constexpr const char *dataDirEnvVar = "EARTHSHINE_DATADIR";
313
314 /// The name of the datafile is a constant.
315 static std::string fileName( const std::string &name )
316 {
317 if( name == "EPOXI" )
318 return "cowan_2013_EPOXI_albedo.dat";
319 if( name == "Earthshine" )
320 return "earthshine_epoxi_normalized.dat";
321 if( name == "RawEarthshine" )
322 return "Earthshine/F7_opt_NIR_ES_data.txt";
323
324 mxError( "earthAlbeo::fileName", MXE_INVALIDARG, "name not recognized." );
325
326 return "";
327 }
328
329 /// Read the Earthshine albedo spectrum, which is a simple two column ASCII format.
331 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
332 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
333 const std::string &path, ///< [in] the full path to the file.
334 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
335 )
336 {
337 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
338 }
339};
340
341/// Venus Spectra
342/**
343 *
344 * \ingroup astrophot_spectra
345 */
346template <typename _units>
348{
349 typedef _units units;
350 typedef typename units::realT realT;
351
352 static const bool freq = false;
353
354 typedef std::string paramsT; ///< The name of the spectrum can be "venus"
355
356 /// Convert from A to SI m
357 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
358
359 /// The Earthshine is a dimensionless albedo.
360 static constexpr realT fluxUnits = static_cast<realT>( 1 );
361
362 /// The location is specified by the EARTHSHINE_DATADIR environment variable.
363 static constexpr const char *dataDirEnvVar = "VENUS_DATADIR";
364
365 /// The name of the datafile is a constant.
366 static std::string fileName( const std::string &name )
367 {
368 if( name == "Venus" )
369 return "venus_combined_albedo.dat";
370
371 mxError( "venusAlbeo::fileName", MXE_INVALIDARG, "name not recognized." );
372
373 return "";
374 }
375
376 /// Read the Earthshine albedo spectrum, which is a simple two column ASCII format.
378 std::vector<realT> &rawLambda, ///< [out] the raw wavelength vector. This should be an empty vector on input.
379 std::vector<realT> &rawSpectrum, ///< [out] the raw spectrum. This should be an empty vector on input.
380 const std::string &path, ///< [in] the full path to the file.
381 const paramsT &params ///< [in] the parameters are passed in case needed to construct the spectrum
382 )
383 {
384 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
385 }
386};
387
388} // namespace astro
389
390} // namespace mx
391
392#endif // mx_astro_astroSpectra_hpp
error_t readColumns(const std::string &fname, arrTs &...arrays)
Read in columns from a text file.
error_t
The mxlib error codes.
Definition error_t.hpp:26
@ noerror
No error has occurred.
#define mxError(esrc, ecode, expl)
This reports an mxlib specific error.
#define MXE_INVALIDARG
An argument was invalid.
mx::error_t toLower(std::string &outstr, const std::string &instr)
Convert a string to all lower case.
The mxlib c++ namespace.
Definition mxlib.hpp:37
A spectrum from the astroFilt filter library.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
static constexpr realT fluxUnits
No conversion is performed on filter transmission.
static constexpr realT wavelengthUnits
Convert from um to SI m.
std::string paramsT
The astroFilters are parameterized by name.
static constexpr realT fluxUnits
Specify how to convert to SI flux units. No conversions are performed in the basic spectrum.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
This function reads the spectrum, returning its raw wavelength and spectrum points.
std::string paramsT
The parameter is a string name.
static constexpr realT wavelengthUnits
Specify how to convert to SI wavelength units. No conversions are performed in the basic spectrum.
static std::string fileName(const paramsT &name)
This function should calculate the file name (without the path) for the spectrum parameters.
static constexpr const char * dataDirEnvVar
The data directory environment variable name.
A spectrum from the HST calspec library.
static constexpr realT fluxUnits
Convert from erg s-1 cm-2 A-1 to SI W m-3.
static std::string fileName(const std::string &name)
The file name is found from the star's name.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
Read a CALSPEC spectrum, which is a simple two column ASCII format.
std::string paramsT
The calspec Spectra are parameterized by star name.
static constexpr realT wavelengthUnits
Convert from A to SI m.
Earth Albedo Spectra.
static constexpr realT wavelengthUnits
Convert from A to SI m.
static constexpr const char * dataDirEnvVar
The location is specified by the EARTHSHINE_DATADIR environment variable.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
Read the Earthshine albedo spectrum, which is a simple two column ASCII format.
std::string paramsT
The name of the spectrum can be "EPOXI", "Earthshine", or "RawEarthshine".
static constexpr realT fluxUnits
The Earthshine is a dimensionless albedo.
static std::string fileName(const std::string &name)
The name of the datafile is a constant.
A spectrum from the Pickles library.
static constexpr const char * dataDirEnvVar
The Pickles spectra location is specified by the PICKLES_DATADIR environment variable.
static constexpr realT fluxUnits
The Pickles spectra are dimensionless.
static constexpr realT wavelengthUnits
Convert from A to SI m.
std::string paramsT
The Pickles spectra are parameterized by a spectral type string.
static std::string fileName(const std::string &spt)
The name of the datafile is constructed from its spectral type string.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
Read a Pickles spectrum, which for these purposes is a simple two column ASCII format.
The square wave is parameterized by the central wavelength, width, and sampling (all in microns).
realT dlam
The wavelength sampling to use in microns.
realT lam0
The central Wavelength in microns.
realT fw
The full width of the filter in microns.
A square-wave filter spectrum.
static constexpr realT fluxUnits
No conversion is performed on filter transmission.
static constexpr realT wavelengthUnits
Convert from um to SI m.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
static std::string fileName(const std::string &name)
The name of the datafile is a constant.
static constexpr realT fluxUnits
The Earthshine is a dimensionless albedo.
std::string paramsT
The name of the spectrum can be "venus".
static constexpr const char * dataDirEnvVar
The location is specified by the EARTHSHINE_DATADIR environment variable.
static error_t readSpectrum(std::vector< realT > &rawLambda, std::vector< realT > &rawSpectrum, const std::string &path, const paramsT &params)
Read the Earthshine albedo spectrum, which is a simple two column ASCII format.
static constexpr realT wavelengthUnits
Convert from A to SI m.
Unit specifications and conversions.