X-shooter Pipeline Reference Manual 3.8.15
xsh_detmon_body.h
Go to the documentation of this file.
1/* $Id: xsh_detmon_body.h,v 1.7 2011-12-02 14:15:28 amodigli Exp $
2 *
3 * This file is part of the irplib package
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: amodigli $
23 * $Date: 2011-12-02 14:15:28 $
24 * $Revision: 1.7 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#define TYPE_ADD(a) CONCAT2X(a, CPL_TYPE)
29
30static cpl_error_code
32 cpl_binary* pm,
33 int llx,
34 int lly,
35 int urx,
36 int ury,
37 int nx,
38 double var_sum,
39 int npixs,
40 double kappa,
41 int nclip,
42 double tolerance,
43 double * mean,
44 double * stdev)
45{
46 int pos0 = (llx - 1) + (lly - 1) * nx; /* 1st pixel to process */
47 double nb = (double) npixs; /* Non-bad pixels in window */
48
49 double lo_cut = *mean - kappa * (*stdev);
50 double hi_cut = *mean + kappa * (*stdev);
51
52 double lo_cut_p = lo_cut;
53 double hi_cut_p = hi_cut;
54
55 double c_var_sum;
56 double c_mean = 0; /* Avoid false uninit warning */
57 double c_stdev = 0; /* Avoid false uninit warning */
58
59 int iclip;
60
61 for (iclip = 0; iclip < nclip; iclip++) {
62 int pos = pos0;
63 int i, j;
64
65 c_var_sum = var_sum;
66 c_mean = *mean;
67 c_stdev = *stdev;
68 nb = npixs;
69
70 for (j = lly - 1; j < ury; j++, pos += (nx - urx + llx - 1)) {
71 for (i = llx - 1; i < urx; i++, pos++) {
72 if ( (pi[pos] > hi_cut || pi[pos] < lo_cut) &&
73 (pm[pos] == CPL_BINARY_1) ) {
74 const double delta = (double)pi[pos] - c_mean;
75
76 c_var_sum -= nb * delta * delta / (nb - 1.0);
77 c_mean -= delta / (nb - 1.0);
78 nb = nb - 1.0;
79 }
80 }
81 }
82 if (nb == 1.0 || c_var_sum < 0.0) {
83 cpl_msg_error(cpl_func, "Iteration %d: Too many pixels were "
84 "removed. This may cause unexpected behaviour. "
85 "Please set a lower number of iterations "
86 "or increase the value of kappa\n", iclip);
87 cpl_msg_error(cpl_func,"iclip=%d nb=%g c_var_sum=%g",
88 iclip,nb,c_var_sum);
89 cpl_error_set(cpl_func, CPL_ERROR_DIVISION_BY_ZERO);
90 } else {
91 c_stdev = sqrt(c_var_sum / (nb - 1.0));
92 }
93
94 lo_cut = c_mean - kappa * c_stdev;
95 hi_cut = c_mean + kappa * c_stdev;
96
97 if(fabs(lo_cut - lo_cut_p) < tolerance &&
98 fabs(hi_cut - hi_cut_p) < tolerance) {
99 break;
100 } else {
101 lo_cut_p = lo_cut;
102 hi_cut_p = hi_cut;
103 }
104
105 }
106
107 *mean = c_mean;
108 *stdev = c_stdev;
109
110 return cpl_error_get_code();
111}
#define CPL_TYPE
Definition: xsh_fit.c:848
#define TYPE_ADD(a)
static cpl_error_code TYPE_ADD() xsh_ksigma_clip(const CPL_TYPE *pi, cpl_binary *pm, int llx, int lly, int urx, int ury, int nx, double var_sum, int npixs, double kappa, int nclip, double tolerance, double *mean, double *stdev)
int nx
double kappa
Definition: xsh_detmon_lg.c:81
int lly
Definition: xsh_detmon_lg.c:86
int llx
Definition: xsh_detmon_lg.c:85
int urx
Definition: xsh_detmon_lg.c:87
double tolerance
int ury
Definition: xsh_detmon_lg.c:88