DETMON Pipeline Reference Manual  1.3.0
irplib_ksigma_clip.c
1 /* $Id: irplib_ksigma_clip.c,v 1.1 2011-11-02 13:18: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-11-02 13:18:28 $
24  * $Revision: 1.1 $
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 "irplib_ksigma_clip.h"
46 
47 #include "irplib_hist.h"
48 #include "irplib_utils.h"
49 
50 /*--------------------------------------------------------------------------*/
51 
52 /*
53  * @defgroup ksigmaclip kappa sigma clip functions
54  */
55 
56 /*--------------------------------------------------------------------------*/
57 
58 /*---------------------------------------------------------------------------
59  Defines
60  ---------------------------------------------------------------------------*/
61 
62 /*---------------------------------------------------------------------------
63  Private function prototypes
64  ---------------------------------------------------------------------------*/
65 
66 static cpl_error_code
67 irplib_ksigma_clip_double(const double * pi,
68  int llx,
69  int lly,
70  int urx,
71  int ury,
72  int nx,
73  double var_sum,
74  int npixs,
75  double kappa,
76  int nclip,
77  double tolerance,
78  double * mean,
79  double * stdev);
80 
81 static cpl_error_code
82 irplib_ksigma_clip_float(const float * pi,
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 static cpl_error_code
97 irplib_ksigma_clip_int(const int * pi,
98  int llx,
99  int lly,
100  int urx,
101  int ury,
102  int nx,
103  double var_sum,
104  int npixs,
105  double kappa,
106  int nclip,
107  double tolerance,
108  double * mean,
109  double * stdev);
110 
111 
112 /*---------------------------------------------------------------------------*/
164 /*---------------------------------------------------------------------------*/
165 cpl_error_code
166 irplib_ksigma_clip(const cpl_image * img,
167  int llx,
168  int lly,
169  int urx,
170  int ury,
171  double kappa,
172  int nclip,
173  double tolerance,
174  double * kmean,
175  double * kstdev)
176 {
177  cpl_errorstate inistate = cpl_errorstate_get();
178 
179  int nx, ny;
180 
181  cpl_stats * stats;
182  double mean, stdev, var_sum;
183  int npixs;
184 
185  cpl_ensure_code(img != NULL, CPL_ERROR_NULL_INPUT);
186 
187  nx = cpl_image_get_size_x(img);
188  ny = cpl_image_get_size_y(img);
189 
190  cpl_ensure_code(llx > 0 && urx > llx && urx <= nx &&
191  lly > 0 && ury > lly && ury <= ny,
192  CPL_ERROR_ILLEGAL_INPUT);
193 
194  cpl_ensure_code(tolerance >= 0.0, CPL_ERROR_ILLEGAL_INPUT);
195  cpl_ensure_code(kappa > 1.0, CPL_ERROR_ILLEGAL_INPUT);
196  cpl_ensure_code(nclip > 0, CPL_ERROR_ILLEGAL_INPUT);
197 
198  stats = cpl_stats_new_from_image_window(img,
199  CPL_STATS_MEAN | CPL_STATS_STDEV,
200  llx, lly, urx, ury);
201 
202  npixs = cpl_stats_get_npix(stats); /* Non-bad pixels in window */
203  mean = cpl_stats_get_mean(stats);
204  stdev = cpl_stats_get_stdev(stats);
205  var_sum = stdev * stdev * (npixs - 1);
206 
207  cpl_stats_delete(stats);
208 
209  /* img, llx etc. may cause errors: Check and propagate */
210  cpl_ensure_code(cpl_errorstate_is_equal(inistate), cpl_error_get_code());
211 
212  switch (cpl_image_get_type(img)) {
213  case CPL_TYPE_DOUBLE:
214  skip_if(irplib_ksigma_clip_double(cpl_image_get_data_double_const(img),
215  llx, lly, urx, ury, nx, var_sum,
216  npixs, kappa, nclip, tolerance,
217  &mean, &stdev));
218  break;
219  case CPL_TYPE_FLOAT:
220  skip_if(irplib_ksigma_clip_float(cpl_image_get_data_float_const(img),
221  llx, lly, urx, ury, nx, var_sum,
222  npixs, kappa, nclip, tolerance,
223  &mean, &stdev));
224  break;
225  case CPL_TYPE_INT:
226  skip_if(irplib_ksigma_clip_int(cpl_image_get_data_int_const(img),
227  llx, lly, urx, ury, nx, var_sum,
228  npixs, kappa, nclip, tolerance,
229  &mean, &stdev));
230  break;
231  default:
232  /* It is an error in CPL to reach this point */
233  assert( 0 );
234  }
235 
236  *kmean = mean;
237  if (kstdev != NULL) *kstdev = stdev; /* Optional */
238 
239  end_skip;
240 
241  return cpl_error_get_code();
242 }
243 
244 #define CONCAT(a,b) a ## _ ## b
245 #define CONCAT2X(a,b) CONCAT(a,b)
246 
247 #define CPL_TYPE double
248 #include "irplib_ksigma_clip_body.h"
249 #undef CPL_TYPE
250 
251 #define CPL_TYPE float
252 #include "irplib_ksigma_clip_body.h"
253 #undef CPL_TYPE
254 
255 #define CPL_TYPE int
256 #include "irplib_ksigma_clip_body.h"
257 #undef CPL_TYPE
258