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
|
/* $Id: wavelength_calibration.cpp,v 1.5 2013/08/07 15:47:01 cgarcia Exp $
*
* This file is part of the MOSCA library
* Copyright (C) 2013 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: cgarcia $
* $Date: 2013/08/07 15:47:01 $
* $Revision: 1.5 $
* $Name: $
*/
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <cpl_table.h>
#include <cpl_msg.h>
#include <cpl_polynomial.h>
#include "wavelength_calibration.h"
#include "statistics.h"
mosca::wavelength_calibration::wavelength_calibration()
{
}
mosca::wavelength_calibration::wavelength_calibration
(const std::string& fits_disp_coeff, double refwave) :
m_refwave(refwave)
{
/* We read from extension 1 of slit location table */
cpl_table* idscoeff = cpl_table_load(fits_disp_coeff.c_str(), 1, 1);
from_idscoeff(idscoeff, refwave);
cpl_table_delete(idscoeff);
}
mosca::wavelength_calibration::wavelength_calibration
(const cpl_table * idscoeff, double refwave) :
m_refwave(refwave)
{
from_idscoeff(idscoeff, refwave);
}
mosca::wavelength_calibration::wavelength_calibration
(const wavelength_calibration& rhs)
{
m_refwave = rhs.m_refwave;
std::vector<cpl_polynomial *>::const_iterator poly;
for(poly = rhs.m_wave_coeff.begin(); poly != rhs.m_wave_coeff.end(); ++poly)
{
if(*poly != NULL)
m_wave_coeff.push_back(cpl_polynomial_duplicate(*poly));
else
m_wave_coeff.push_back(NULL);
}
}
//TODO: Create a copy operator (copy constructor is not trivial)
void mosca::wavelength_calibration::from_idscoeff
(const cpl_table * idscoeff, double refwave)
{
cpl_size ncol = cpl_table_get_ncol(idscoeff) - 2;
for(cpl_size irow = 0 ; irow < cpl_table_get_nrow(idscoeff); irow++)
{
std::vector<double> pol_coeff;
int null = 0;
for(cpl_size idx_coeff = 0; idx_coeff < ncol; idx_coeff++)
{
std::ostringstream colname;
colname<<std::left<<"c"<<idx_coeff;
if(cpl_table_has_column(idscoeff, colname.str().c_str()))
{
pol_coeff.push_back(cpl_table_get_double
(idscoeff, colname.str().c_str(), irow, &null));
if(null)
break;
}
}
cpl_polynomial * poly = NULL;
if(!null)
{
poly = cpl_polynomial_new(1);
cpl_size idx;
std::vector<double>::reverse_iterator coeff;
for( idx = pol_coeff.size() - 1, coeff = pol_coeff.rbegin(); coeff != pol_coeff.rend();
++coeff, --idx)
cpl_polynomial_set_coeff(poly, &idx, *coeff);
}
m_wave_coeff.push_back(poly);
m_nlines.push_back(cpl_table_get_int
(idscoeff, "nlines", irow, &null));
}
m_refwave = refwave;
}
mosca::wavelength_calibration::~wavelength_calibration()
{
std::vector<cpl_polynomial *>::iterator poly;
for(poly = m_wave_coeff.begin(); poly != m_wave_coeff.end(); ++poly)
{
if(*poly != NULL)
cpl_polynomial_delete(*poly);
}
}
double mosca::wavelength_calibration::get_pixel(double spatial_corrected_pos,
double wavelength) const
{
double pixel = -1;
size_t row = (size_t)(spatial_corrected_pos);
if(row >= m_wave_coeff.size())
return pixel;
cpl_polynomial * poly = m_wave_coeff[row];
if(poly == NULL)
return pixel;
pixel = cpl_polynomial_eval_1d(poly, wavelength - m_refwave, NULL);
return pixel;
}
//TODO:Starting at 0 or 1?
double mosca::wavelength_calibration::get_wave(double spatial_corrected_pos,
double dispersion_pos) const
{
double wavelength = -1; //Denotes and error. TODO
/* TODO: I think this is wrong, the reference wavelength probably has to be added
* Already done, but needs to be checked */
size_t row = (size_t)(spatial_corrected_pos);
if(row >= m_wave_coeff.size())
return wavelength;
cpl_polynomial * poly = m_wave_coeff[row];
if(poly == NULL)
return wavelength;
cpl_polynomial * inv_poly = cpl_polynomial_duplicate(poly);
cpl_size zero_coeff = 0;
double coeff = cpl_polynomial_get_coeff(inv_poly, &zero_coeff);
cpl_polynomial_set_coeff(inv_poly, &zero_coeff, coeff - dispersion_pos);
wavelength = 0;
cpl_polynomial_solve_1d(inv_poly, wavelength, &wavelength, 1);
if(cpl_error_get_code() == CPL_ERROR_DIVISION_BY_ZERO ||
cpl_error_get_code() == CPL_ERROR_CONTINUE)
{
cpl_error_reset();
cpl_polynomial_delete(inv_poly);
return -1;
}
cpl_polynomial_delete(inv_poly);
return wavelength + m_refwave;
}
void mosca::wavelength_calibration::min_max_wave(double& min_wave,
double& max_wave,
int size_dispersion,
int min_spa_row,
int max_spa_row) const
{
std::vector<double> wave_pix_0;
std::vector<double> wave_pix_size;
/* TODO: Check this limits */
for(int irow = min_spa_row ; irow < max_spa_row; ++irow)
{
cpl_polynomial * poly = m_wave_coeff[irow];
if(poly != NULL)
{
double wave_0 = cpl_polynomial_eval_1d(poly, 0., NULL);
double wave_size = cpl_polynomial_eval_1d(poly, (double)size_dispersion, NULL);
wave_pix_0.push_back(wave_0);
wave_pix_size.push_back(wave_size);
}
}
if(wave_pix_0.size() != 0)
{
min_wave = *std::min_element(wave_pix_0.begin(), wave_pix_0.end());
max_wave = *std::max_element(wave_pix_size.begin(), wave_pix_size.end());
}
else
min_wave = max_wave = 0;
}
double mosca::wavelength_calibration::mean_dispersion(double start_wave,
double end_wave,
int min_spa_row,
int max_spa_row) const
{
std::vector<double> pix_startwave;
std::vector<double> pix_endwave;
for(int irow = min_spa_row ; irow < max_spa_row; ++irow)
{
if(m_nlines[irow] != 0) //For some reason there are wavelength calibrations that are not NULL in the coeffs but have just zero lines.
{
double pix_start = get_pixel((double)irow, start_wave);
double pix_end = get_pixel((double)irow, end_wave);
if(pix_start != -1 )
pix_startwave.push_back(pix_start);
if(pix_end != -1)
pix_endwave.push_back(pix_end);
}
}
if(!pix_startwave.empty() && !pix_endwave.empty())
{
double mean_pix_start= mosca::mean(pix_startwave.begin(), pix_startwave.end());
double mean_pix_end = mosca::mean(pix_endwave.begin(), pix_endwave.end());
double pixels_span = std::fabs(mean_pix_end - mean_pix_start);
double mean_disp = (end_wave - start_wave) / pixels_span;
return mean_disp;
}
else
return 0;
}
bool mosca::wavelength_calibration::has_valid_cal
(double spatial_corrected_pos) const
{
size_t row = (size_t)(spatial_corrected_pos);
if(row >= m_wave_coeff.size())
return false;
cpl_polynomial * poly = m_wave_coeff[row];
if(poly == NULL)
return false;
return true;
}
bool mosca::wavelength_calibration::is_monotonical
(size_t spa_row, double start_wave, double end_wave, double dispersion) const
{
if(spa_row >= m_wave_coeff.size())
return false;
cpl_polynomial * poly = m_wave_coeff[spa_row];
if(poly == NULL)
return false;
for(double wave = start_wave; wave <= end_wave; wave += dispersion)
{
double grad;
cpl_polynomial_eval_1d(poly, wave - m_refwave, &grad);
if(grad < 0)
return false;
}
return true;
}
double mosca::wavelength_calibration::get_refwave() const
{
return m_refwave;
}
|