24 #include <xtensor/xadapt.hpp>
25 #include <xtensor/xarray.hpp>
32 using DMatrix = xt::xarray<double, xt::layout_type::row_major>;
46 static constexpr
double h = 6.62607015e-27;
53 std::string name =
"";
55 std::string dtype =
"photon";
57 QLength wavelength_unit =
nm;
79 void calculate_sed_independent_properties();
84 const QLength& wavelength_unit,
85 const std::string dtype,
86 const std::string name);
120 const QLength& wavelength_unit,
125 const QLength& new_wavelength_unit);
141 const std::string dtype,
142 const std::string name){
143 double convfac = wavelength_unit.to(
nanometre);
145 this->wavelength_unit =
nm;
147 this->transmission = transmission;
149 if ((dtype.compare(
"photon") == 0) ||
150 (dtype.compare(
"energy") == 0)){
153 throw std::runtime_error(
"only photon and energy allowed");
155 this->calculate_sed_independent_properties();
178 void Filter::calculate_sed_independent_properties(){
180 const auto& wavelength_nm = this->wavelength_nm;
181 const auto& transmission = this->transmission;
183 size_t n_points = transmission.size();
184 double transmission_max = xt::amax(transmission)[0];
185 double transmission_max_100th = transmission_max / 100.;
187 auto norm = xt::trapz(transmission, wavelength_nm)[0];
188 auto _lT = xt::trapz(wavelength_nm * transmission, wavelength_nm)[0];
189 auto _cl = norm > 0 ? _lT / norm : 0.;
194 if (this->dtype.compare(
"photon") == 0){
195 lpivot2 = _lT / trapz(transmission / wavelength_nm, wavelength_nm)[0];
197 lpivot2 = norm / trapz(transmission / xt::square(wavelength_nm), wavelength_nm)[0];
199 this->lpivot = std::sqrt(lpivot2);
202 double lmax = wavelength_nm[0];
204 double lmin = wavelength_nm[n_points - 1];
205 for (
size_t i=0; i < transmission.size(); ++i){
206 if (transmission[i] > transmission_max_100th){
207 lmax = std::max(lmax, wavelength_nm[i]);
208 lmin = std::min(lmin, wavelength_nm[i]);
219 this->width = (norm / xt::amax(transmission)[0]);
228 double first = wavelength_nm[0];
229 double last = wavelength_nm[-1];
230 double thresh = transmission_max * 0.5;
231 for (
size_t i=0; i < wavelength_nm.size() - 1; ++i){
232 if((transmission[i+1] > thresh) and (transmission[i] <= thresh)){
233 first = wavelength_nm[i];
237 for (
size_t i=wavelength_nm.size(); i > 1; --i){
238 if((transmission[i-1] > thresh) and (transmission[i] <= thresh)){
239 last = wavelength_nm[i];
243 this->fwhm = last - first;
247 const DMatrix& vega_wavelength = vega.get_wavelength(
nm);
250 this->leff = xt::trapz(vega_wavelength * vega_T * vega_flux, vega_wavelength)[0] /
251 xt::trapz(vega_T * vega_flux, vega_wavelength)[0];
254 this->lphot = xt::trapz(xt::square(vega_wavelength) * vega_T * vega_flux, vega_wavelength)[0] /
255 xt::trapz(vega_wavelength * vega_T * vega_flux, vega_wavelength)[0];
267 double C1 = (this->wavelength_unit).to(
angstrom);
269 C1 = this->lpivot * this->lpivot * C1;
270 return 2.5 * std::log10(C1) + 48.60;
412 if ((xt::amin(filt_wave)[0] > xt::amax(
wavelength)[0])
413 || (xt::amax(filt_wave)[0] < xt::amin(
wavelength)[0])) {
418 auto new_trans = xt::interp(
wavelength, filt_wave, filt_trans, 0., 0.);
421 if (! xt::any(new_trans > 0)){
431 double b = xt::trapz(new_trans,
wavelength)[0];
445 auto new_trans = xt::interp(new_wavelength_nm, filt_wave, filt_trans, 0., 0.);
446 return Filter(new_wavelength_nm, new_trans,
nm, this->dtype, this->name);
459 auto new_trans = xt::interp(new_wavelength, filt_wave, filt_trans, 0., 0.);
460 return Filter(new_wavelength, new_trans, new_wavelength_unit,
461 this->dtype, this->name);
468 size_t n_points = this->transmission.size();
469 std::cout <<
"Filter Object information:\n"
470 <<
" name: " << this->name <<
"\n"
471 <<
" detector type: " << this->dtype <<
"\n"
472 <<
" wavelength units: " <<
"nm (internally set)" <<
"\n"
473 <<
" central wavelength: " << this->cl <<
" nm" <<
"\n"
474 <<
" pivot wavelength: " << this->lpivot <<
" nm" <<
"\n"
475 <<
" effective wavelength: " << this->leff <<
" nm" <<
"\n"
476 <<
" photon wavelength: " << this->lphot <<
" nm" <<
"\n"
477 <<
" minimum wavelength: " << this->lmin <<
" nm" <<
"\n"
478 <<
" maximum wavelength: " << this->lmax <<
" nm" <<
"\n"
479 <<
" norm: " << this->norm <<
"\n"
480 <<
" effective width: " << this->width <<
" nm" <<
"\n"
481 <<
" fullwidth half-max: " << this->fwhm <<
" nm" <<
"\n"
482 <<
" definition contains " << n_points <<
" points" <<
"\n"
588 return this->wavelength_nm;
598 return this->wavelength_nm *
nm.to(in);
607 return this->transmission;
617 return (this->dtype.compare(
"photon") == 0);