irplib_ksigma_clip.c

00001 /* $Id: irplib_ksigma_clip.c,v 1.1 2011/11/02 13:18:28 amodigli Exp $
00002  *
00003  * This file is part of the irplib package
00004  * Copyright (C) 2002, 2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2011/11/02 13:18:28 $
00024  * $Revision: 1.1 $
00025  * $Name: visir-3_5_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <complex.h>
00033 
00034 /*---------------------------------------------------------------------------
00035                                   Includes
00036  ---------------------------------------------------------------------------*/
00037 
00038 #include <math.h>
00039 #include <string.h>
00040 #include <assert.h>
00041 #include <float.h>
00042 
00043 #include <cpl.h>
00044 
00045 #include "irplib_ksigma_clip.h"
00046 
00047 #include "irplib_hist.h"
00048 #include "irplib_utils.h"
00049 
00050 /*--------------------------------------------------------------------------*/
00051 
00052 /*
00053  * @defgroup ksigmaclip        kappa sigma clip functions
00054  */
00055 
00056 /*--------------------------------------------------------------------------*/
00057 
00058 /*---------------------------------------------------------------------------
00059                                   Defines
00060  ---------------------------------------------------------------------------*/
00061 
00062 /*---------------------------------------------------------------------------
00063                                   Private function prototypes
00064  ---------------------------------------------------------------------------*/
00065 
00066 static cpl_error_code
00067 irplib_ksigma_clip_double(const double  * pi,
00068               int               llx,
00069               int               lly,
00070               int               urx,
00071               int               ury,
00072               int               nx,
00073               double            var_sum,
00074               int               npixs,
00075               double            kappa,
00076               int               nclip,
00077               double            tolerance,
00078               double          * mean,
00079               double          * stdev);
00080 
00081 static cpl_error_code
00082 irplib_ksigma_clip_float(const float     * pi,
00083              int               llx,
00084              int               lly,
00085              int               urx,
00086              int               ury,
00087              int               nx,
00088              double            var_sum,
00089              int               npixs,
00090              double            kappa,
00091              int               nclip,
00092              double            tolerance,
00093              double          * mean,
00094              double          * stdev);
00095 
00096 static cpl_error_code
00097 irplib_ksigma_clip_int(const int       * pi,
00098                int               llx,
00099                int               lly,
00100                int               urx,
00101                int               ury,
00102                int               nx,
00103                double            var_sum,
00104                int               npixs,
00105                double            kappa,
00106                int               nclip,
00107                double            tolerance,
00108                double          * mean,
00109                double          * stdev);
00110 
00111 
00112 /*---------------------------------------------------------------------------*/
00164 /*---------------------------------------------------------------------------*/
00165 cpl_error_code
00166 irplib_ksigma_clip(const cpl_image * img,
00167            int               llx,
00168            int               lly,
00169            int               urx,
00170            int               ury,
00171            double            kappa,
00172            int               nclip,
00173            double            tolerance,
00174            double          * kmean,
00175            double          * kstdev)
00176 {
00177     cpl_errorstate inistate = cpl_errorstate_get();
00178 
00179     int nx, ny;
00180 
00181     cpl_stats * stats;
00182     double      mean, stdev, var_sum;
00183     int         npixs;
00184 
00185     cpl_ensure_code(img != NULL, CPL_ERROR_NULL_INPUT);
00186 
00187     nx = cpl_image_get_size_x(img);
00188     ny = cpl_image_get_size_y(img);
00189 
00190     cpl_ensure_code(llx > 0 && urx > llx && urx <= nx &&
00191             lly > 0 && ury > lly && ury <= ny,
00192             CPL_ERROR_ILLEGAL_INPUT);
00193 
00194     cpl_ensure_code(tolerance >= 0.0, CPL_ERROR_ILLEGAL_INPUT);
00195     cpl_ensure_code(kappa     >  1.0, CPL_ERROR_ILLEGAL_INPUT);
00196     cpl_ensure_code(nclip     >    0, CPL_ERROR_ILLEGAL_INPUT);
00197 
00198     stats = cpl_stats_new_from_image_window(img,
00199                         CPL_STATS_MEAN | CPL_STATS_STDEV,
00200                         llx, lly, urx, ury);
00201 
00202     npixs   = cpl_stats_get_npix(stats); /* Non-bad pixels in window */
00203     mean    = cpl_stats_get_mean(stats);
00204     stdev   = cpl_stats_get_stdev(stats);
00205     var_sum = stdev * stdev * (npixs - 1);
00206 
00207     cpl_stats_delete(stats);
00208 
00209     /* img, llx etc. may cause errors: Check and propagate */
00210     cpl_ensure_code(cpl_errorstate_is_equal(inistate), cpl_error_get_code());
00211 
00212     switch (cpl_image_get_type(img)) {
00213     case CPL_TYPE_DOUBLE:
00214     skip_if(irplib_ksigma_clip_double(cpl_image_get_data_double_const(img),
00215                       llx, lly, urx, ury, nx, var_sum,
00216                       npixs, kappa, nclip, tolerance,
00217                       &mean, &stdev));
00218     break;
00219     case CPL_TYPE_FLOAT:
00220     skip_if(irplib_ksigma_clip_float(cpl_image_get_data_float_const(img),
00221                      llx, lly, urx, ury, nx, var_sum,
00222                      npixs, kappa, nclip, tolerance,
00223                      &mean, &stdev));
00224     break;
00225     case CPL_TYPE_INT:
00226     skip_if(irplib_ksigma_clip_int(cpl_image_get_data_int_const(img),
00227                        llx, lly, urx, ury, nx, var_sum,
00228                        npixs, kappa, nclip, tolerance,
00229                        &mean, &stdev));
00230     break;
00231     default:
00232     /* It is an error in CPL to reach this point */
00233     assert( 0 );
00234     }
00235 
00236     *kmean = mean;
00237     if (kstdev != NULL) *kstdev = stdev; /* Optional */
00238 
00239     end_skip;
00240 
00241     return cpl_error_get_code();
00242 }
00243 
00244 #define CONCAT(a,b) a ## _ ## b
00245 #define CONCAT2X(a,b) CONCAT(a,b)
00246 
00247 #define CPL_TYPE double
00248 #include "irplib_ksigma_clip_body.h"
00249 #undef CPL_TYPE
00250 
00251 #define CPL_TYPE float
00252 #include "irplib_ksigma_clip_body.h"
00253 #undef CPL_TYPE
00254 
00255 #define CPL_TYPE int
00256 #include "irplib_ksigma_clip_body.h"
00257 #undef CPL_TYPE
00258 

Generated on Mon Feb 6 15:23:48 2012 for VISIR Pipeline Reference Manual by  doxygen 1.5.8