X-shooter Pipeline Reference Manual 3.8.15
xsh_ksigma_clip.c
Go to the documentation of this file.
1/* $Id: xsh_detmon.c,v 1.11 2013-07-19 12:00:24 jtaylor 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: jtaylor $
23 * $Date: 2013-07-19 12:00:24 $
24 * $Revision: 1.11 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#include <complex.h>
33
34/*---------------------------------------------------------------------------
35 Includes
36 ---------------------------------------------------------------------------*/
37
38#include <math.h>
39#include <string.h>
40#include <assert.h>
41#include <float.h>
42
43#include <cpl.h>
44
45#include "xsh_ksigma_clip.h"
46
47
48static cpl_error_code
49xsh_ksigma_clip_double(const double * pi,
50 cpl_binary * pm,
51 int llx,
52 int lly,
53 int urx,
54 int ury,
55 int nx,
56 double var_sum,
57 int npixs,
58 double kappa,
59 int nclip,
60 double tolerance,
61 double * mean,
62 double * stdev);
63
64static cpl_error_code
65xsh_ksigma_clip_float(const float * pi,
66 cpl_binary * pm,
67 int llx,
68 int lly,
69 int urx,
70 int ury,
71 int nx,
72 double var_sum,
73 int npixs,
74 double kappa,
75 int nclip,
76 double tolerance,
77 double * mean,
78 double * stdev);
79
80static cpl_error_code
81xsh_ksigma_clip_int(const int * pi,
82 cpl_binary * pm,
83 int llx,
84 int lly,
85 int urx,
86 int ury,
87 int nx,
88 double var_sum,
89 int npixs,
90 double kappa,
91 int nclip,
92 double tolerance,
93 double * mean,
94 double * stdev);
95
96
97/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149cpl_error_code
150xsh_ksigma_clip(const cpl_image * img,
151 int llx,
152 int lly,
153 int urx,
154 int ury,
155 double kappa,
156 int nclip,
157 double tolerance,
158 double * kmean,
159 double * kstdev)
160{
161
162 int nx, ny;
163
164 double mean, stdev;
165 //int npixs;
166 cpl_image* sub_ima=NULL;
167 double kappa2=0;
168 double thresh=0;
169 double thresh_p=0;
170 cpl_binary * pm=0;
171 const float* pi=0;
172
173 int pix=0;
174 int i=0;
175 int j=0;
176 int k=0;
177
178 cpl_ensure_code(img != NULL, CPL_ERROR_NULL_INPUT);
179
180 nx = cpl_image_get_size_x(img);
181 ny = cpl_image_get_size_y(img);
182
183 cpl_ensure_code(llx > 0 && urx > llx && urx <= nx &&
184 lly > 0 && ury > lly && ury <= ny,
185 CPL_ERROR_ILLEGAL_INPUT);
186
187 cpl_ensure_code(tolerance >= 0.0, CPL_ERROR_ILLEGAL_INPUT);
188 cpl_ensure_code(kappa > 1.0, CPL_ERROR_ILLEGAL_INPUT);
189 cpl_ensure_code(nclip > 0, CPL_ERROR_ILLEGAL_INPUT);
190
191 sub_ima=cpl_image_extract(img,llx, lly, urx, ury);
192 //npixs= nx*ny-cpl_image_count_rejected(sub_ima);
193 cpl_image_delete(sub_ima);
194 mean = cpl_image_get_mean_window(img,llx, lly, urx, ury);
195 stdev = cpl_image_get_stdev_window(img,llx, lly, urx, ury);
196 pi=cpl_image_get_data_float_const(img);
197 pm=cpl_mask_get_data(cpl_image_get_bpm((cpl_image*)img));
198
199
200 kappa2=kappa*kappa;
201 thresh_p=-1;
202 for(k=0;k<nclip;k++) {
203 mean = cpl_image_get_mean_window(img,llx, lly, urx, ury);
204 stdev = cpl_image_get_stdev_window(img,llx, lly, urx, ury);
205 thresh=stdev*stdev*kappa2;
206 for(j=lly;j<ury;j++) {
207 for(i=llx;i<urx;i++) {
208 pix=i+j*nx;
209 if(
210 (pm[pix] != CPL_BINARY_1) &&
211 ((pi[pix]-mean)*(pi[pix]-mean) > thresh ) ) {
212 pm[pix]=CPL_BINARY_1;
213 } /* end check if pix is outlier */
214 } /* loop over i */
215 } /* loop over j */
216 if((fabs(thresh_p-thresh)) < tolerance) {
217 break;
218 } else {
219 thresh_p=thresh;
220 }
221 } /* loop over niter */
222
223 *kmean = mean;
224 if (kstdev != NULL) *kstdev = stdev; /* Optional */
225
226
227 return cpl_error_get_code();
228}
229
230#define CONCAT(a,b) a ## _ ## b
231#define CONCAT2X(a,b) CONCAT(a,b)
232
233#define CPL_TYPE double
234#include "xsh_detmon_body.h"
235#undef CPL_TYPE
236
237#define CPL_TYPE float
238#include "xsh_detmon_body.h"
239#undef CPL_TYPE
240
241#define CPL_TYPE int
242#include "xsh_detmon_body.h"
243#undef CPL_TYPE
244
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
int ny
static cpl_error_code xsh_ksigma_clip_float(const float *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)
static cpl_error_code xsh_ksigma_clip_double(const double *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)
static cpl_error_code xsh_ksigma_clip_int(const int *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)
cpl_error_code xsh_ksigma_clip(const cpl_image *img, int llx, int lly, int urx, int ury, double kappa, int nclip, double tolerance, double *kmean, double *kstdev)
Apply kappa-sigma clipping on input image.