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"
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 */
52 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
53 input. */
54 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
55 input. */
56 const std::string &path, /**< [in] the full path to the file. */
57 const paramsT &params /**< [in] the parameters needed to construct the
58 spectrum */
59 )
60 {
61 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
62 }
63};
64
65/// A spectrum from the astroFilt filter library
66/**
67 * \ingroup astrophot_spectra
68 */
69template <typename _units, bool _rsr = true>
71{
72 typedef _units units;
73 typedef typename units::realT realT;
74
75 static const bool freq = false;
76
77 typedef std::string paramsT; ///< The astroFilters are parameterized by name.
78
79 /// Convert from um to SI m
80 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
81
82 /// No conversion is performed on filter transmission.
83 static constexpr realT fluxUnits = static_cast<realT>( 1 );
84
85 static constexpr const char *dataDirEnvVar = "ASTROFILT_DATADIR";
86
87 static std::string fileName( const std::string &name )
88 {
89 return name + ".dat";
90 }
91
92 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
93 input. */
94 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
95 input. */
96 const std::string &path, /**< [in] the full path to the file. */
97 const paramsT &params /**< [in] the parameters needed to construct the
98 spectrum */
99 )
100 {
101 mx::error_t errc = mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
102 if( !!errc )
103 {
104 return errc;
105 }
106
107 realT max = 0;
108
109 for( int i = 0; i < rawLambda.size(); ++i )
110 {
111 // if(_rsr) rawSpectrum[i] = rawSpectrum[i]*rawLambda[i];
112 if( rawSpectrum[i] > max )
113 max = rawSpectrum[i];
114 }
115
116 for( int i = 0; i < rawLambda.size(); ++i )
117 {
118 rawSpectrum[i] /= max;
119 }
120
121 return error_t::noerror;
122 }
123};
124
125/// A square-wave filter spectrum
126/** Parameters specify the central wavelength, width, and sampling (all in microns) of a square-wave type filter.
127 *
128 * To create this filter:
129 * \code
130 * typedef double realT;
131 *
132 * realT lam0 = 0.5; //Central wavelength in microns
133 * realT fw = 0.05; //Full-width, in microns.
134 * realT dlam = 0.001; //Delta-lambda for specifying the defining points. See note.
135 *
136 * astroSpectrum<sqWaveFilter<units::si<realT>>> filt({ lam0, fw, dlam});
137 *
138 * filt.setSpectrum(grid_meters); // grid_meters is a vector<realT> specifying the wavelength grid in meters
139 *
140 * \endcode
141 *
142 * Note that dlam specifies how sharp the filter edges are when interpolated. Larger values will make the filter more
143 * trapezoidal.
144 *
145 * \ingroup astrophot_spectra
146 */
147template <typename _units, bool _rsr = true>
149{
150 typedef _units units;
151 typedef typename units::realT realT;
152
153 static const bool freq = false;
154
155 /// The square wave is parameterized by the central wavelength, width, and sampling (all in microns).
156 typedef struct
157 {
158 realT lam0; ///< The central Wavelength in microns
159 realT fw; ///< The full width of the filter in microns
160 realT dlam; ///< The wavelength sampling to use in microns.
161 } paramsT;
162
163 /// Convert from um to SI m
164 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
165
166 /// No conversion is performed on filter transmission.
167 static constexpr realT fluxUnits = static_cast<realT>( 1 );
168
169 static constexpr const char *dataDirEnvVar = 0;
170
171 static std::string fileName( const paramsT &params )
172 {
173 return " "; // must not be empty to avoid error
174 }
175
176 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
177 input. */
178 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
179 input. */
180 const std::string &path, /**< [in] the full path to the file. */
181 const paramsT &params /**< [in] the parameters needed to construct the
182 spectrum */
183 )
184 {
185 rawLambda.resize( 4 );
186 rawSpectrum.resize( 4 );
187
188 rawLambda[0] = params.lam0 - 0.5 * params.fw - 0.5 * params.dlam;
189 rawSpectrum[0] = 0.0;
190
191 rawLambda[1] = params.lam0 - 0.5 * params.fw + 0.5 * params.dlam;
192 rawSpectrum[1] = 1.0;
193
194 rawLambda[2] = params.lam0 + 0.5 * params.fw - 0.5 * params.dlam;
195 rawSpectrum[2] = 1.0;
196
197 rawLambda[3] = params.lam0 + 0.5 * params.fw + 0.5 * params.dlam;
198 rawSpectrum[3] = 0.0;
199
200 return error_t::noerror;
201 }
202};
203
204/// A spectrum from the HST calspec library
205/** See http://www.stsci.edu/hst/observatory/crds/calspec.html
206 *
207 * \ingroup astrophot_spectra
208 */
209template <typename _units>
211{
212 typedef _units units;
213 typedef typename units::realT realT;
214
215 static const bool freq = false;
216
217 typedef std::string paramsT; ///< The calspec Spectra are parameterized by star name.
218
219 /// Convert from A to SI m
220 static constexpr realT wavelengthUnits = static_cast<realT>( 1e10 );
221
222 /// Convert from erg s-1 cm-2 A-1 to SI W m-3
223 static constexpr realT fluxUnits =
224 static_cast<realT>( 1e7 ) / ( static_cast<realT>( 1e4 ) * static_cast<realT>( 1e10 ) );
225
226 static constexpr const char *dataDirEnvVar = "CALSPEC_DATADIR";
227
228 /// The file name is found from the star's name.
229 static std::string fileName( const std::string &name )
230 {
231 if( name == "alpha_lyr" || name == "vega" )
232 return "alpha_lyr_stis_005.asc";
233 else if( name == "1740346" )
234 return "1740346_nic_002.ascii";
235 else if( name == "sun" || name == "sun_reference" )
236 return "sun_reference_stis.002.asc";
237
238 return "";
239 }
240
241 /// Read a CALSPEC spectrum, which is a simple two column ASCII format.
242 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
243 input. */
244 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
245 input. */
246 const std::string &path, /**< [in] the full path to the file. */
247 const paramsT &params /**< [in] the parameters needed to construct the
248 spectrum */
249 )
250 {
251 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
252 }
253};
254
255/// A spectrum from the Pickles library
256/**
257 * \ingroup astrophot_spectra
258 */
259template <typename _units>
261{
262 typedef _units units;
263 typedef typename units::realT realT;
264
265 static const bool freq = false;
266
267 typedef std::string paramsT; ///< The Pickles spectra are parameterized by a spectral type string.
268
269 /// Convert from A to SI m
270 static constexpr realT wavelengthUnits = static_cast<realT>( 1e10 );
271
272 /// The Pickles spectra are dimensionless.
273 static constexpr realT fluxUnits = static_cast<realT>( 1 );
274
275 /// The Pickles spectra location is specified by the PICKLES_DATADIR environment variable.
276 static constexpr const char *dataDirEnvVar = "PICKLES_DATADIR";
277
278 /// The name of the datafile is constructed from its spectral type string.
279 static std::string fileName( const std::string &spt )
280 {
281 return "uk" + ioutils::toLower( spt ) + ".dat";
282 }
283
284 /// Read a Pickles spectrum, which for these purposes is a simple two column ASCII format.
285 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
286 input. */
287 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
288 input. */
289 const std::string &path, /**< [in] the full path to the file. */
290 const paramsT &params /**< [in] the parameters needed to construct the
291 spectrum */
292 )
293 {
294 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
295 }
296};
297
298/// Earth Albedo Spectra
299/** The spectra can be one of:
300 * - "EPOXI" returns the apparent albedo spectrum from Cowan and Strait (2013) \cite cowan_2013.
301 * - "RawEarthshine" returns the unnormalized albedo spectrum measured using Earthshine by Turnbull et al. (2006)
302 * \cite turnbull_20016
303 * - "Earthshine" returns the Earthshine spectrum normalized to match the EPOXI result of 0.27 in the 550 nm band.
304 *
305 * \ingroup astrophot_spectra
306 */
307template <typename _units, typename verboseT = verbose::d>
309{
310 typedef _units units;
311 typedef typename units::realT realT;
312
313 static const bool freq = false;
314
315 typedef std::string paramsT; ///< The name of the spectrum can be "EPOXI", "Earthshine", or "RawEarthshine".
316
317 /// Convert from A to SI m
318 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
319
320 /// The Earthshine is a dimensionless albedo.
321 static constexpr realT fluxUnits = static_cast<realT>( 1 );
322
323 /// The location is specified by the EARTHSHINE_DATADIR environment variable.
324 static constexpr const char *dataDirEnvVar = "EARTHSHINE_DATADIR";
325
326 /// The name of the datafile is a constant.
327 static std::string fileName( const std::string &name )
328 {
329 if( name == "EPOXI" )
330 return "cowan_2013_EPOXI_albedo.dat";
331 if( name == "Earthshine" )
332 return "earthshine_epoxi_normalized.dat";
333 if( name == "RawEarthshine" )
334 return "Earthshine/F7_opt_NIR_ES_data.txt";
335
337
338 return "";
339 }
340
341 /// Read the Earthshine albedo spectrum, which is a simple two column ASCII format.
342 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
343 input. */
344 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
345 input. */
346 const std::string &path, /**< [in] the full path to the file. */
347 const paramsT &params /**< [in] the parameters needed to construct the
348 spectrum */
349 )
350 {
351 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
352 }
353};
354
355/// Venus Spectra
356/**
357 *
358 * \ingroup astrophot_spectra
359 */
360template <typename _units, typename verboseT = verbose::d>
362{
363 typedef _units units;
364 typedef typename units::realT realT;
365
366 static const bool freq = false;
367
368 typedef std::string paramsT; ///< The name of the spectrum can be "venus"
369
370 /// Convert from A to SI m
371 static constexpr realT wavelengthUnits = static_cast<realT>( 1e6 );
372
373 /// The Earthshine is a dimensionless albedo.
374 static constexpr realT fluxUnits = static_cast<realT>( 1 );
375
376 /// The location is specified by the EARTHSHINE_DATADIR environment variable.
377 static constexpr const char *dataDirEnvVar = "VENUS_DATADIR";
378
379 /// The name of the datafile is a constant.
380 static std::string fileName( const std::string &name )
381 {
382 if( name == "Venus" )
383 return "venus_combined_albedo.dat";
384
386
387 return "";
388 }
389
390 /// Read the Earthshine albedo spectrum, which is a simple two column ASCII format.
391 static error_t readSpectrum( std::vector<realT> &rawLambda, /**< [out] the raw wavelength. Should be empty on
392 input. */
393 std::vector<realT> &rawSpectrum, /**< [out] the raw spectrum. Should be empty on
394 input. */
395 const std::string &path, /**< [in] the full path to the file. */
396 const paramsT &params /**< [in] the parameters needed to construct the
397 spectrum */
398 )
399 {
400 return mx::ioutils::readColumns( path, rawLambda, rawSpectrum );
401 }
402};
403
404} // namespace astro
405
406} // namespace mx
407
408#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.
Definition error_t.hpp:27
@ invalidarg
An argument was invalid.
Definition error_t.hpp:29
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
Definition error.hpp:331
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 utility to read in columns from a text file.
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.
std::string paramsT
The name of the spectrum can be "EPOXI", "Earthshine", or "RawEarthshine".
static std::string fileName(const std::string &name)
The name of the datafile is a constant.
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 const char * dataDirEnvVar
The location is specified by the EARTHSHINE_DATADIR environment variable.
static constexpr realT wavelengthUnits
Convert from A to SI m.
static constexpr realT fluxUnits
The Earthshine is a dimensionless albedo.
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 constexpr realT wavelengthUnits
Convert from A to SI m.
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 const char * dataDirEnvVar
The location is specified by the EARTHSHINE_DATADIR environment variable.
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.
std::string paramsT
The name of the spectrum can be "venus".
Unit specifications and conversions.