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