File: image_normalisation.tcc

package info (click to toggle)
cpl-plugin-vimos 3.1.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,044 kB
  • sloc: ansic: 160,597; cpp: 13,619; sh: 4,323; python: 1,425; makefile: 793; perl: 10
file content (108 lines) | stat: -rw-r--r-- 3,723 bytes parent folder | download | duplicates (3)
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
/*
 * 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 <image_normalisation.h>




template<typename  T>
mosca::image mosca::image_normalise
(mosca::image& image,
 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)
{
    
    //Collapsing to get the profiles in each direction
    std::vector<T> slit_spa_profile = image.collapse<T>(mosca::DISPERSION_AXIS);
    std::vector<T> slit_disp_profile = image.collapse<T>(mosca::SPATIAL_AXIS);

    T * p_ima = image.get_data<T>();
    T total_flux = 
         std::accumulate(p_ima, p_ima + image.size_x() * image.size_y(), T(0));
    
    //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;
    else 
        slit_spa_norm_profile = std::vector<T>(slit_spa_profile.size(), 
                T(total_flux / slit_spa_profile.size()));
    
    if (disp_smooth_radius > 0 || disp_fit_nknots > 0)
        slit_disp_norm_profile = slit_disp_profile;
    else 
        slit_disp_norm_profile = std::vector<T>(slit_disp_profile.size(), 
                T(total_flux / slit_disp_profile.size()));

    if (spa_smooth_radius > 0)
        mosca::vector_smooth<T>(slit_spa_norm_profile, spa_smooth_radius);

    if (spa_fit_polyorder > 0)
    {
        size_t used_spa_fit_polyorder = spa_fit_polyorder;  
        mosca::vector_polynomial polfit;
        polfit.fit<T>(slit_spa_norm_profile, used_spa_fit_polyorder, 
                fit_threshold);
    }
    
    if (disp_smooth_radius > 0)
        mosca::vector_smooth<T>(slit_disp_norm_profile, disp_smooth_radius);

    if (disp_fit_nknots > 0)
    {
        size_t used_disp_fit_nknots = disp_fit_nknots;  
        mosca::vector_cubicspline splfit;
        splfit.fit<T>(slit_disp_norm_profile, 
                      used_disp_fit_nknots, fit_threshold);
    }
    
    cpl_size nx = image.size_x();
    cpl_size ny = image.size_y();
    mosca::image result(nx, ny, mosca::type_trait<T>::cpl_eq_type,
                        image.dispersion_axis());
    T * p_res = result.get_data<T>();
    for (cpl_size j = 0; j< ny; ++j)
    {
        for (cpl_size i = 0; i< nx; ++i, ++p_res)
        {
            if(image.dispersion_axis() == mosca::X_AXIS)
                *p_res = slit_spa_norm_profile[j] * slit_disp_norm_profile[i] / 
                total_flux;
            else
                *p_res = slit_spa_norm_profile[i] * slit_disp_norm_profile[j] / 
                total_flux;
        }
    }

    return result;
}