File: extinction.cpp

package info (click to toggle)
cpl-plugin-vimos 3.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 23,188 kB
  • sloc: ansic: 169,153; cpp: 14,555; sh: 4,250; python: 1,423; makefile: 899; perl: 10
file content (111 lines) | stat: -rw-r--r-- 3,219 bytes parent folder | download | duplicates (8)
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
/*
 * This file is part of the MOSCA library
 * Copyright (C) 2002-2010 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * extinction.cpp
 *
 *  Created on: 2014 4 2
 *      Author: cgarcia
 */

#include <cmath>
#include <extinction.h>


mosca::extinction::extinction(const cpl_table * ext) :
 m_gsl_acc(NULL), m_gsl_interp(NULL)
{
    m_extinction = cpl_table_duplicate(ext);
    cpl_table_cast_column(m_extinction, "WAVE", "WAVE_D", CPL_TYPE_DOUBLE);
    cpl_table_cast_column(m_extinction, "EXTINCTION", 
                          "EXTINCTION_D", CPL_TYPE_DOUBLE);
}

mosca::extinction::extinction(const mosca::extinction& other) :
 m_gsl_acc(NULL), m_gsl_interp(NULL)
{
    m_extinction = cpl_table_duplicate(other.m_extinction);
}

mosca::extinction::extinction() :
m_extinction(NULL), m_gsl_acc(NULL), m_gsl_interp(NULL)
{
}

//TODO: Create a copy operator (copy destructor is not trivial)

mosca::extinction::~extinction()
{
    if(m_extinction != NULL)
        cpl_table_delete(m_extinction);
    if(m_gsl_interp != NULL)
    {
        gsl_interp_free (m_gsl_interp);
        gsl_interp_accel_free (m_gsl_acc);
    }
}

mosca::spectrum mosca::extinction::correct_spectrum
(const spectrum& spec, double airmass)
{
    std::vector<double> flux = spec.flux(); 
    std::vector<double> wave = spec.wave();
    
    for(size_t i = 0; i < flux.size(); ++i)
    {
        double wavelength = wave[i];
        double interp_ext = eval_at_wave(wavelength);
        double ext_factor = std::pow(10., interp_ext*0.4*airmass);
        double corrected_flux = flux[i]*ext_factor;
        flux[i] = corrected_flux;
    }
    
    mosca::spectrum spec_corrected(flux, wave);
    
    return spec_corrected;
}

double mosca::extinction::eval_at_wave(double wavelength)
{
    if(m_extinction == NULL)
        return 0.;

    cpl_size npts = cpl_table_get_nrow(m_extinction);
    double * wave = 
        cpl_table_get_data_double(m_extinction, "WAVE_D");
    double * ext = 
        cpl_table_get_data_double(m_extinction, "EXTINCTION_D");
    
    if(wavelength > wave[npts-1])
        return ext[npts-1];
    if(wavelength < wave[0])
        return ext[0];
                
    if(m_gsl_interp == NULL)
    {
        m_gsl_acc    = gsl_interp_accel_alloc ();
        m_gsl_interp = gsl_interp_alloc (gsl_interp_linear, npts);
        gsl_interp_init (m_gsl_interp,wave, ext, npts);
    }
    
    double ext_at_wave =
        gsl_interp_eval (m_gsl_interp, wave, ext, wavelength, m_gsl_acc);
    return ext_at_wave;
}