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
|
/*
* This file is part of the FORS Data Reduction Pipeline
* 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
*/
/*
* image_normalisation.cpp
*
* Created on: 2014 3 28
* Author: cgarcia
*/
#include <cpl.h>
#include <vector>
#include <iostream>
#include <iterator>
#include <numeric>
#include <exception>
#include "image_normalisation.h"
#include "vector_utils.h"
const char* mosca::no_flux_exception::what() const throw()
{
const char* ret =
"The sum of all the flux contributions for the provided slit "
"is zero, making normalisation not possible";
return ret;
}
template<typename T>
mosca::image mosca::image_normalise
(mosca::image& slit_image,
mosca::image& slit_image_weight,
int spa_smooth_radius, int disp_smooth_radius,
int spa_fit_polyorder, int disp_fit_nknots, double fit_threshold,
std::vector<T>& slit_spa_norm_profile, std::vector<T>& slit_disp_norm_profile)
{
//TODO: Check dispersion axis are the same
if(slit_image.size_x() != slit_image_weight.size_x() ||
slit_image.size_y() != slit_image_weight.size_y())
throw std::invalid_argument("image and weight sizes do not match");
//TODO: Use size_disp rather than size_x in the relevant places
mosca::image slit_weighted = slit_image;
std::transform (slit_image.get_data<T>(),
slit_image.get_data<T>() + slit_image.size_x() * slit_image.size_y(),
slit_image_weight.get_data<T>(),
slit_weighted.get_data<T>(), std::multiplies<T>());
//Collapsing the data to get the profiles in each direction
std::vector<T> slit_spa_profile =
slit_weighted.collapse<T>(mosca::DISPERSION_AXIS);
std::vector<T> slit_disp_profile =
slit_weighted.collapse<T>(mosca::SPATIAL_AXIS);
//Collapsing the weights
std::vector<T> weight_spa_profile =
slit_image_weight.collapse<T>(mosca::DISPERSION_AXIS);
std::vector<T> weight_disp_profile =
slit_image_weight.collapse<T>(mosca::SPATIAL_AXIS);
//Getting the profiles properly weighted
std::vector<T> slit_spa_profile_w;
std::transform (slit_spa_profile.begin(), slit_spa_profile.end(),
weight_spa_profile.begin(), std::back_inserter(slit_spa_profile_w),
std::divides<T>());
std::vector<T> slit_disp_profile_w;
std::transform (slit_disp_profile.begin(), slit_disp_profile.end(),
weight_disp_profile.begin(), std::back_inserter(slit_disp_profile_w),
std::divides<T>());
T * p_ima = slit_weighted.get_data<T>();
T total_flux =
std::accumulate(p_ima, p_ima + slit_image.size_x() * slit_image.size_y(), T(0));
T * p_weight = slit_image_weight.get_data<T>();
T total_weight =
std::accumulate(p_weight, p_weight + slit_image.size_x() * slit_image.size_y(), T(0));
if(total_flux == T(0) || total_weight == T(0))
{
slit_spa_norm_profile = slit_spa_profile;
slit_disp_norm_profile = slit_disp_profile;
return slit_image;
}
//If we are doing any fitting/smoothing in that direction,
//initialise it to the current profile, if not initialise it to a constant
if (spa_smooth_radius > 0 || spa_fit_polyorder > 0)
slit_spa_norm_profile = slit_spa_profile_w;
else
slit_spa_norm_profile = std::vector<T>(slit_spa_profile_w.size(),
T(total_flux / total_weight));
if (disp_smooth_radius > 0 || disp_fit_nknots > 0)
slit_disp_norm_profile = slit_disp_profile_w;
else
slit_disp_norm_profile = std::vector<T>(slit_disp_profile_w.size(),
T(total_flux / total_weight));
if (spa_smooth_radius > 0)
{
std::vector<bool> mask;
std::transform(weight_spa_profile.begin(), weight_spa_profile.end(),
std::back_inserter(mask), std::bind1st(std::not_equal_to<T>(), T(0)));
mosca::vector_smooth<T>(slit_spa_norm_profile, mask, spa_smooth_radius);
}
if (spa_fit_polyorder > 0)
{
std::vector<bool> mask;
const double max_el = *std::max_element(slit_spa_norm_profile.begin(), slit_spa_norm_profile.end());
const double th_this = fit_threshold * max_el;
std::transform(slit_spa_norm_profile.begin(), slit_spa_norm_profile.end(),
std::back_inserter(mask), std::bind2nd(std::greater_equal<T>(), th_this));
size_t used_spa_fit_polyorder = spa_fit_polyorder;
mosca::vector_polynomial polfit;
polfit.fit<T>(slit_spa_norm_profile, mask, used_spa_fit_polyorder);
}
if (disp_smooth_radius > 0)
{
std::vector<bool> mask;
std::transform(weight_disp_profile.begin(), weight_disp_profile.end(),
std::back_inserter(mask), std::bind1st(std::not_equal_to<T>(), T(0)));
mosca::vector_smooth<T>(slit_disp_norm_profile, mask, disp_smooth_radius);
}
if (disp_fit_nknots > 0)
{
std::vector<bool> mask;
std::transform(weight_disp_profile.begin(), weight_disp_profile.end(),
std::back_inserter(mask), std::bind1st(std::not_equal_to<T>(), T(0)));
size_t used_disp_fit_nknots = disp_fit_nknots;
mosca::vector_cubicspline splfit;
splfit.fit<T>(slit_disp_norm_profile, mask,
used_disp_fit_nknots);
}
cpl_size nx = slit_image.size_x();
cpl_size ny = slit_image.size_y();
mosca::image result(nx, ny, mosca::type_trait<T>::cpl_eq_type,
slit_image.dispersion_axis());
T * p_res = result.get_data<T>();
p_weight = slit_image_weight.get_data<T>();
for (cpl_size j = 0; j< ny; ++j)
{
for (cpl_size i = 0; i< nx; ++i, ++p_res, ++p_weight)
{
if(*p_weight != 0)
{
if(slit_image.dispersion_axis() == mosca::X_AXIS)
*p_res = slit_spa_norm_profile[j] * slit_disp_norm_profile[i] /
total_flux * total_weight;
else
*p_res = slit_spa_norm_profile[i] * slit_disp_norm_profile[j] /
total_flux * total_weight;
}
else
*p_res = 1;
}
}
return result;
}
|