-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsource.cpp
401 lines (325 loc) · 13.2 KB
/
source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <cmath>
#include <algorithm>
#include "source.h"
#include <CCfits/CCfits>
#include <helper.h>
#include <source.h>
/// mark fmt as header only
#define FMT_HEADER_ONLY
#include <fmt/format.h>
// integration routine
template<typename Method, typename F, typename Float>
double integrate(F f, Float a, Float b, int steps, Method m) {
double s = 0;
double h = (b - a) / steps;
for (int i = 0; i < steps; ++i)
s += m(f, a + h * i, h);
return h * s;
}
// methods
class rectangular {
public:
enum position_type {
left, middle, right
};
rectangular(position_type pos) : position(pos) {}
template<typename F, typename Float>
double operator()(F f, Float x, Float h) const {
switch (position) {
case left:
return f(x);
case middle:
return f(x + h / 2);
case right:
return f(x + h);
}
}
private:
const position_type position;
};
Source::Source() : integration_steps(10), shift(1.), list_like(true) {
}
Source::~Source() {
}
std::vector<double> Source::get_interpolated_spectral_density(std::vector<double> wavelength) {
if (!this->list_like) {
std::vector<double> spectrum;
std::vector<double> diff;
diff.reserve(wavelength.size());
for (int i = 0; i < wavelength.size() - 2; i++)
diff.push_back(this->shift * (wavelength[i + 1] - wavelength[i]));
diff.push_back(diff.back());
diff.push_back(diff.back());
auto f = [this](double wl) { return this->get_spectral_density(wl); };
for (int i = 0; i < wavelength.size(); i++) {
double a = this->shift * wavelength[i] - diff[i] / 2.;
double b = this->shift * wavelength[i] + diff[i] / 2.;
// double result2 = this->integral_s(a, b, this->integration_steps) / diff[i];
double result = s_val * integrate(f, a, b, integration_steps, rectangular(rectangular::middle)) / diff[i];
spectrum.push_back(result);
}
return spectrum;
} else {
std::vector<double> spectrum;
spectrum.reserve(wavelength.size());
for (double wl : wavelength) {
spectrum.push_back(get_spectral_density(wl));
}
return spectrum;
}
}
std::vector<double> Source::get_photon_flux(std::vector<double> wavelength) {
if (!this->list_like) {
// convert microwatts / micrometer to photons / s per wavelength intervall
double ch_factor = 5.03E12;
std::vector<double> flux;
std::vector<double> diff;
for (int i = 0; i < wavelength.size() - 2; i++) {
diff.push_back(wavelength[i + 1] - wavelength[i]);
}
diff.push_back(diff.back());
diff.push_back(diff.back());
// expected to be microwatts/micrometer
std::vector<double> spectrum = this->get_interpolated_spectral_density(wavelength);
for (int i = 0; i < wavelength.size(); i++) {
flux.push_back(spectrum[i] * wavelength[i] * ch_factor * diff[i]);
}
return flux;
} else {
std::vector<double> spectrum;
for (int i = 0; i < wavelength.size(); i++) {
// expected to be photons / s for list like sources
spectrum.push_back(get_spectral_density(wavelength[i]));
}
return spectrum;
}
}
void Source::set_integration_steps(int n) {
this->integration_steps = n;
}
double Source::integral_s(double a, double b, int n) {
double step = (b - a) / n; // width of each small rectangle
double area = 0.0; // signed area
for (int i = 0; i < n; i++) {
area += this->get_spectral_density(a + (i + 0.5) * step) * step; // sum up each small rectangle
}
return area;
}
void Source::set_doppler_shift(double shift) {
this->shift = 1. + shift / 2.99792458E8;
}
double Constant::get_spectral_density(double wavelength) {
return this->value;
}
Constant::Constant(double value) : value(value) {
list_like = false;
name = fmt::format("Constant {0}", value);
}
Constant::Constant() {
this->value = 0.01; // uW per um (micro watts per micro meter)
}
Blackbody::Blackbody(double T, double magnitude, double telescope_area) : StellarSource(magnitude, telescope_area),
T(T) {
this->list_like = false;
name = fmt::format("Blackbody T: {0}, mag: {1}, telescope: {2}", T, magnitude, telescope_area);
min_w = 0; // Maybe set a cutoff based off intensity?
max_w = 10;
calc_flux_scale();
}
double Blackbody::planck(const double &T, const double &wavelength) {
const double hPlanck = 6.62606896e-34; //J * s;
const double speedOfLight = 2.99792458e8; //m / s;
const double kBoltzmann = 1.3806504e-23; //J / K
double a = 2.0 * hPlanck * speedOfLight * speedOfLight;
double b = hPlanck * speedOfLight / (wavelength * kBoltzmann * T);
double c = 1E0; // Conversion factor for intensity
double intensity = c * a / (pow(wavelength, 5) * (exp(b) - 1.0)); // (J / s ) / m^3 -> (uW) / ( m^2 * um )
return intensity;
}
double Blackbody::get_spectral_density(double wavelength) {
return this->planck(this->T, wavelength / 1E6);
}
PhoenixSpectrum::PhoenixSpectrum(std::string spectrum_file, std::string wavelength_file, double magnitude,
double telescope_area) : StellarSource(magnitude, telescope_area) {
list_like = false;
name = fmt::format("Phoenix file: {0}, mag: {1}, telescope: {2}", spectrum_file, magnitude, telescope_area);
this->read_spectrum(spectrum_file, wavelength_file);
calc_flux_scale();
}
void PhoenixSpectrum::read_spectrum(std::string spectrum_file, std::string wavelength_file) {
// read in wavelength file
std::unique_ptr<CCfits::FITS> ptr_FITS_wl(new CCfits::FITS(wavelength_file, CCfits::Read, true));
CCfits::PHDU &wl = ptr_FITS_wl->pHDU();
std::valarray<double> contents_wl;
wl.readAllKeys();
wl.read(contents_wl);
std::unique_ptr<CCfits::FITS> ptr_FITS_spectrum(new CCfits::FITS(spectrum_file, CCfits::Read, true));
CCfits::PHDU &spec = ptr_FITS_spectrum->pHDU();
std::valarray<double> contents_spec;
spec.readAllKeys();
spec.read(contents_spec);
// find wavelength limits
// convert contents_spec from erg/s/cm^2/cm to uW/m^2/um
for (long j = 0; j < contents_wl.size() - 1; ++j) {
this->data[contents_wl[j] / 10000.] = 0.1 * contents_spec[j];
}
}
double PhoenixSpectrum::get_spectral_density(double wavelength) {
return interpolate(this->data, wavelength);
}
CoehloSpectrum::CoehloSpectrum(std::string spectrum_file, double magnitude, double telescope_area)
: StellarSource(magnitude, telescope_area) {
this->read_spectrum(std::move(spectrum_file));
list_like = false;
name = fmt::format("Coehlo spectrum: {0}, mag: {1}, telescope: {2}", spectrum_file, magnitude, telescope_area);
calc_flux_scale();
}
void CoehloSpectrum::read_spectrum(std::string spectrum_file) {
//// read in wavelength file
std::unique_ptr<CCfits::FITS> ptr_FITS_spectrum(new CCfits::FITS(spectrum_file, CCfits::Read, true));
CCfits::PHDU &spec = ptr_FITS_spectrum->pHDU();
std::valarray<double> contents_spec;
spec.readAllKeys();
spec.read(contents_spec);
long ax1(spec.axis(0));
long max_idx = ax1;
long min_idx = 0;
std::unique_ptr<CCfits::FITS> pInfile(new CCfits::FITS(spectrum_file, CCfits::Read));
double minwl;
pInfile->pHDU().readKey("CRVAL1", minwl);
double wld;
pInfile->pHDU().readKey("CDELT1", wld);
// find wavelength limits
// convert contents_spec from erg/s/cm^2/A to uW/s/m^2/um
for (long j = min_idx; j < max_idx; ++j) {
this->data[(minwl + j * wld) / 10000.] = (1E7) * contents_spec[j];
}
}
double CoehloSpectrum::get_spectral_density(double wavelength) {
return interpolate(this->data, wavelength);
}
CustomSpectrum::CustomSpectrum(double magnitude, double telescope_area, const std::string spectrum_file,
std::string wave_file) : StellarSource(magnitude, telescope_area) {
list_like = false;
name = fmt::format("custom : {0}, mag: {1}, telescope: {2}", spectrum_file, magnitude, telescope_area);
read_spectrum(spectrum_file, wave_file);
calc_flux_scale();
}
void CustomSpectrum::read_spectrum(const std::string spectrum_file, std::string wave_file) {
//// read in wavelength file
std::unique_ptr<CCfits::FITS> ptr_FITS_wl(new CCfits::FITS(wave_file, CCfits::Read, true));
CCfits::PHDU &wl = ptr_FITS_wl->pHDU();
std::valarray<double> contents_wl;
wl.readAllKeys();
wl.read(contents_wl);
long ax1(wl.axis(0));
long max_idx = ax1;
long min_idx = 0;
double min_wavelength_angstrom = min_w * 10000.;
double max_wavelength_angstrom = max_w * 10000.;
// find wavelength limits
for (long j = 0; j < ax1; ++j) {
if (contents_wl[j] < min_wavelength_angstrom)
min_idx = j;
if (contents_wl[j] > max_wavelength_angstrom)
max_idx = j;
}
std::unique_ptr<CCfits::FITS> ptr_FITS_spectrum(new CCfits::FITS(spectrum_file, CCfits::Read, true));
CCfits::PHDU &spec = ptr_FITS_spectrum->pHDU();
std::valarray<double> contents_spec;
spec.readAllKeys();
spec.read(contents_spec);
std::unique_ptr<CCfits::FITS> pInfile(new CCfits::FITS(spectrum_file, CCfits::Read));
// find wavelength limits
// convert contents_spec from erg/s/cm^2/A to uW/s/m^2/um
for (long j = min_idx; j < max_idx; ++j) {
this->data[contents_wl[j] / 10000.] = (1E7) * contents_spec[j];
}
}
double CustomSpectrum::get_spectral_density(double wavelength) {
return interpolate(this->data, wavelength);
}
LineList::LineList(const std::string linelist_file) {
name = fmt::format("LineList file: {0}", linelist_file);
list_like = true;
this->read_spectrum(linelist_file);
}
void LineList::read_spectrum(std::string linelist_file) {
std::ifstream file(linelist_file.c_str());
for (CSVIterator loop(file); loop != CSVIterator(); ++loop) {
std::cout << (*loop)[0] << std::endl;
event.push_back(stod((*loop)[0]));
intensity.push_back(stod((*loop)[1]));
this->data.insert(std::pair<double, double>(stod((*loop)[0]), stod((*loop)[1])));
}
}
std::vector<double> LineList::get_interpolated_spectral_density(std::vector<double> wavelength) {
std::vector<double> spectrum;
for (auto const &wl: wavelength) {
spectrum.push_back(double(data[wl]));
}
return spectrum;
}
double LineList::get_spectral_density(double wavelength) {
return data[wavelength];
}
std::vector<double> LineList::get_wavelength() {
std::vector<double> wavelength;
for (auto m: this->data) {
wavelength.push_back(m.first * shift);
}
return wavelength;
}
CalibrationSource::CalibrationSource() {
stellar_source = false;
}
StellarSource::StellarSource(double magnitude, double telescope_area) : mag(magnitude), telescope_area(telescope_area) {
}
void StellarSource::calc_flux_scale() {
// V-band filter
std::vector<double> v_filter_wl{0.47, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6,
0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7};
std::vector<double> v_filter_tp{0, 0.03, 0.163, 0.458, 0.78, 0.967, 1, 0.973, 0.898, 0.792, 0.684, 0.574, 0.461,
0.359, 0.27, 0.197, 0.135, 0.081, 0.045, 0.025, 0.017, 0.013, 0.009, 0};
std::map<double, double> v_filter;
for (size_t i = 0; i < v_filter_wl.size(); ++i)
v_filter[v_filter_wl[i]] = v_filter_tp[i];
s_val = 1.0;
// get total flux in filter range
std::vector<double> wl;
double lower_wl_limit = std::max(min_w, v_filter_wl.front());
double upper_wl_limit = std::min(max_w, v_filter_wl.back());
int n = 1000000;
double step = (upper_wl_limit - lower_wl_limit) / n;
for (int i = 0; i < n; ++i)
wl.push_back(lower_wl_limit + step * i);
std::vector<double> spec = this->get_interpolated_spectral_density(wl);
// get flux * V-filter
double total_flux = 0.;
for (int i = 0; i < n; i++) {
total_flux += spec[i] * interpolate(v_filter, lower_wl_limit + step * i) * step;
}
s_val = pow(10, mag / (-2.5)) * v_zp / total_flux * telescope_area;
}
IdealEtalon::IdealEtalon(double d, double n, double theta, double R, double I) : d(d / 1000.), n(n), theta(theta),
R(R), I(I) {
this->cF = this->coefficient_of_finesse(R);
this->integration_steps = 10;
this->list_like = false;
}
double IdealEtalon::coefficient_of_finesse(double R) {
return 4. * R / ((1. - R) * (1. - R));
}
double IdealEtalon::T(double wl, double theta, double d, double n, double cF) {
//delta = (2. * math.pi / wl) * 2. * n * math.cos(theta) * d
//return 1. / (1. + cF * np.sin(0.5 * delta) ** 2)
double delta = (2. * M_PI * n * cos(theta) * d) / wl;
double sind = sin(delta);
return 1. / (1. + cF * sind * sind);
}
double IdealEtalon::get_local_efficiency(double wavelength) {
return this->T(wavelength / 1E6, theta, d, n, cF);
}
double IdealEtalon::get_spectral_density(double wavelength) {
return I * get_local_efficiency(wavelength);
}