sinfo_remove_crh_single.c

00001 /*                                                                            *
00002  *   This file is part of the ESO X-shooter Pipeline                          *
00003  *   Copyright (C) 2006 European Southern Observatory                         *
00004  *                                                                            *
00005  *   This library is free software; you can redistribute it and/or modify     *
00006  *   it under the terms of the GNU General Public License as published by     *
00007  *   the Free Software Foundation; either version 2 of the License, or        *
00008  *   (at your option) any later version.                                      *
00009  *                                                                            *
00010  *   This program is distributed in the hope that it will be useful,          *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00013  *   GNU General Public License for more details.                             *
00014  *                                                                            *
00015  *   You should have received a copy of the GNU General Public License        *
00016  *   along with this program; if not, write to the Free Software              *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA     *
00018  *                                                                            */
00019 
00020 /*
00021  * $Author: amodigli $
00022 
00023  * $Date: 2012/03/03 10:18:26 $
00024  * $Revision: 1.5 $
00025  * $Log: sinfo_remove_crh_single.c,v $
00026  * Revision 1.5  2012/03/03 10:18:26  amodigli
00027  * fixed some doxygen warnings
00028  *
00029  * Revision 1.4  2010/12/29 12:29:05  amodigli
00030  * fixed doxygen warning, and bug in kernel filter indexing
00031  *
00032  * Revision 1.3  2009/09/03 15:04:48  kmirny
00033  * replacing deprecated functions
00034  *
00035  * Revision 1.2  2009/02/18 10:33:07  amodigli
00036  * fixed compiler warning
00037  *
00038  * Revision 1.1  2009/01/02 08:27:58  amodigli
00039  * added to repository
00040  *
00041  *
00042  */
00043 
00044 #ifdef HAVE_CONFIG_H
00045 #  include <config.h>
00046 #endif
00047 
00049 /*----------------------------------------------------------------------------*/
00056 /*----------------------------------------------------------------------------*/
00057 
00058 /*-----------------------------------------------------------------------------
00059   Includes
00060 -----------------------------------------------------------------------------*/
00061 
00062 #include <math.h>
00063 
00064 
00065 #include <sinfo_error.h>
00066 #include <sinfo_msg.h>
00067 #include <sinfo_utils_wrappers.h>
00068 #include <cpl.h>
00069 #include "sinfo_utilities.h"
00070 /*-----------------------------------------------------------------------------
00071   Functions prototypes
00072  -----------------------------------------------------------------------------*/
00073 cpl_image * sinfo_remove_crh_single( cpl_image * sci_image,
00074                      double crh_frac_max,
00075                      double sigma_lim,
00076                      double f_lim,
00077                      int max_iter,
00078                      double gain,
00079                      double ron);
00080 /*-----------------------------------------------------------------------------
00081   Implementation
00082  -----------------------------------------------------------------------------*/
00083 
00084 /* This should be defined in a more clever way, a parameter for example */
00085 #define MAX_ITERATIONS 6
00086 
00087 
00109 cpl_image * sinfo_remove_crh_single( cpl_image * sci_image,
00110                      double crh_frac_max,
00111                      double sigma_lim,
00112                      double f_lim,
00113                      int max_iter,
00114                      double gain,
00115                      double ron)
00116 {
00117   int i,j,k,l,m;
00118   double  frac = 0. ;
00119   /* Only pointers */
00120 
00121   /* Need to be free */
00122   //xsh_localization_list * loc_list = NULL ;
00123   cpl_image* laplacian_image = NULL;
00124   cpl_image* laplacian_redu_image = NULL;
00125   cpl_image* two_sub_sample = NULL;
00126   cpl_image* sci_median5_image = NULL;
00127   cpl_image* noise_image = NULL;
00128   cpl_image* s_image = NULL;
00129   cpl_image* s_median_image = NULL;
00130   cpl_image* s2_image = NULL;
00131   cpl_image* sci_median3_image = NULL;
00132   cpl_image* sci_median3_7_image = NULL;
00133   cpl_image* f_image = NULL;
00134   cpl_image* r_image = NULL;
00135   int two_sub_sample_nx = 0;
00136   int two_sub_sample_ny = 0;
00137   /* Only pointers */
00138   float* sci_data = NULL;
00139   float* two_sub_sample_data = NULL;
00140   float* laplacian_data = NULL;
00141   float* laplacian_redu_data = NULL;
00142   float* sci_median5_data = NULL;
00143   float* sci_median3_data = NULL;
00144   float* sci_median3_7_data = NULL;
00145   float* noise_data = NULL;
00146   float* s_data = NULL;
00147   float* s_median_data = NULL;
00148   float* s2_data = NULL;
00149   float* f_data = NULL;
00150   float* r_data = NULL;
00151   /* Need to be free */
00152   float* cosmic_data = NULL;
00153 
00154   cpl_matrix* laplacian_kernel = NULL;
00155   cpl_matrix* median3_kernel = NULL;
00156   cpl_matrix* median5_kernel = NULL;
00157   cpl_matrix* median7_kernel = NULL;
00158   int new_crh =1, nb_crh = 0;
00159   int nbiter = 1 ;
00160   cpl_vector* median = NULL;
00161   //const char * tag = NULL ;
00162   int nx=0;
00163   int ny=0;
00164   cpl_image* res_image=NULL;
00165 
00166   /* Check parameters */
00167   cknull( sci_image,"null input image" ) ; ;
00168 
00169   sinfo_msg( "Entering sinfo_remove_crh_single");
00170   sinfo_msg( "  Params: frac_max %.1f, sigma_lim %.2f f_lim %.2f, iter %d",
00171        crh_frac_max, sigma_lim, f_lim, max_iter);
00172 
00173   /* Preparing different kernels */
00174   nx=cpl_image_get_size_x(sci_image);
00175   ny=cpl_image_get_size_y(sci_image);
00176 
00177   /* Laplacian */
00178   check_nomsg( laplacian_kernel = cpl_matrix_new(3,3));
00179   cpl_matrix_set( laplacian_kernel,0,0,0.0);
00180   cpl_matrix_set( laplacian_kernel,0,1,-1.0);
00181   cpl_matrix_set( laplacian_kernel,0,2,0.0);
00182   cpl_matrix_set( laplacian_kernel,1,0,-1.0);
00183   cpl_matrix_set( laplacian_kernel,1,1,4.0);
00184   cpl_matrix_set( laplacian_kernel,1,2,-1.0);
00185   cpl_matrix_set( laplacian_kernel,2,0,0.0);
00186   cpl_matrix_set( laplacian_kernel,2,1,-1.0);
00187   cpl_matrix_set( laplacian_kernel,2,2,0.0);
00188   cpl_matrix_divide_scalar( laplacian_kernel, 4.0);
00189 /*
00190   cpl_matrix_set( laplacian_kernel,0,0,-1.0);
00191   cpl_matrix_set( laplacian_kernel,0,1,-1.0);
00192   cpl_matrix_set( laplacian_kernel,0,2,-1.0);
00193   cpl_matrix_set( laplacian_kernel,1,0,-1.0);
00194   cpl_matrix_set( laplacian_kernel,1,1,8.0);
00195   cpl_matrix_set( laplacian_kernel,1,2,-1.0);
00196   cpl_matrix_set( laplacian_kernel,2,0,-1.0);
00197   cpl_matrix_set( laplacian_kernel,2,1,-1.0);
00198   cpl_matrix_set( laplacian_kernel,2,2,-1.0);
00199   cpl_matrix_divide_scalar( laplacian_kernel, 8.0);
00200 */
00201   /* Median 3x3*/
00202   check_nomsg( median3_kernel = cpl_matrix_new(3,3));
00203   for(j=0; j< 3; j++){
00204     for(i=0; i< 3; i++){
00205       cpl_matrix_set( median3_kernel, i,j,1.0);
00206     }
00207   }
00208 
00209   /* Median 5x5 */
00210   check_nomsg( median5_kernel = cpl_matrix_new(5,5));
00211   for(j=0; j< 5; j++){
00212     for(i=0; i< 5; i++){
00213       cpl_matrix_set( median5_kernel, i,j,1.0);
00214     }
00215   }
00216 
00217   /* Median 7x7 */
00218   check_nomsg( median7_kernel = cpl_matrix_new(7,7));
00219   for(j=0; j< 7; j++){
00220     for(i=0; i< 7; i++){
00221       cpl_matrix_set( median7_kernel, i,j,1.0);
00222     }
00223   }
00224 
00225   check_nomsg (res_image = cpl_image_duplicate( sci_image));
00226 
00227   /* Allocate images and pointers */
00228   check_nomsg (sci_data = cpl_image_get_data_float( res_image));
00229 
00230   two_sub_sample_nx = nx*2;
00231   two_sub_sample_ny = ny*2;
00232   check_nomsg( two_sub_sample = cpl_image_new( two_sub_sample_nx,
00233     two_sub_sample_ny, CPL_TYPE_FLOAT));
00234   check_nomsg(two_sub_sample_data = cpl_image_get_data_float( two_sub_sample));
00235   check_nomsg( laplacian_redu_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00236   check_nomsg(laplacian_redu_data = cpl_image_get_data_float(
00237     laplacian_redu_image));
00238   check_nomsg( noise_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00239   check_nomsg( noise_data = cpl_image_get_data_float( noise_image));
00240   check_nomsg( s_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00241   check_nomsg( s_data = cpl_image_get_data_float( s_image));
00242   check_nomsg( s2_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00243   check_nomsg( s2_data = cpl_image_get_data_float( s2_image));
00244   check_nomsg( f_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00245   check_nomsg( f_data = cpl_image_get_data_float( f_image));
00246   check_nomsg( r_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00247   check_nomsg( r_data = cpl_image_get_data_float( r_image));
00248   cosmic_data=cpl_calloc(nx*ny, sizeof(float));
00249 
00250   /* LGG - Added limit on frac_max AND limit on nb iterations */
00251   while( new_crh > 0 && frac < crh_frac_max && nbiter <= max_iter ){
00252     sinfo_msg("Iteration %d",nbiter );
00253     /* Create a 2n x 2n images like this
00254         | 1 | 2 |  =>  | 1 | 1 | 2 | 2 |
00255         | 3 | 4 |      | 1 | 1 | 2 | 2 |
00256                        | 3 | 3 | 4 | 4 |
00257                        | 3 | 3 | 4 | 4 | */
00258     sinfo_msg_debug("Create a 2n images");
00259     for( j=0; j< ny; j++){
00260       for( i=0; i< nx; i++){
00261         float val = sci_data[i+j*nx];
00262 
00263     if ( val < 0. ) val = 0. ;
00264         two_sub_sample_data[i*2+j*2*two_sub_sample_nx] = val;
00265         two_sub_sample_data[i*2+1+j*2*two_sub_sample_nx] = val;
00266         two_sub_sample_data[i*2+(j*2+1)*two_sub_sample_nx] = val;
00267         two_sub_sample_data[i*2+1+(j*2+1)*two_sub_sample_nx] = val;
00268       }
00269     }
00270     sinfo_msg_debug("Doing laplacian convolution");
00271     /* Doing the laplacian convolution
00272         0  -1   0
00273        -1   4  -1
00274         0  -1   0 */
00275     laplacian_image = sinfo_image_filter_linear( two_sub_sample,
00276       laplacian_kernel);
00277 
00278     /* multiply by two to normalize correctly the laplacian [RD5]
00279        and filter negative values */
00280     sinfo_msg_debug("Normalize laplacian");
00281     check_nomsg (laplacian_data = cpl_image_get_data_float( laplacian_image));
00282     for ( i=0; i< two_sub_sample_nx*two_sub_sample_ny; i++){
00283       if (laplacian_data[i] > 0.0){
00284         laplacian_data[i] = 2.0 * laplacian_data[i];
00285       }
00286       else{
00287         laplacian_data[i] = 0.0;
00288       }
00289     }
00290     sinfo_msg_debug("Save Lpositive");
00291     cpl_image_save(laplacian_image, "Lpositive.fits", CPL_BPP_IEEE_FLOAT, NULL,
00292       CPL_IO_DEFAULT);
00293 
00294     /* resample to the original size
00295        | 1 | 1 | 2 | 2 |    | 1 | 2 |
00296        | 1 | 1 | 2 | 2 |    | 3 | 4 |
00297        | 3 | 3 | 4 | 4 | =>
00298        | 3 | 3 | 4 | 4 |               */
00299 
00300     sinfo_msg_debug("Resample to the original size");
00301 
00302     for( j=0; j< ny; j++){
00303       for( i=0; i< nx; i++){
00304         laplacian_redu_data[i+j*nx] =
00305           (laplacian_data[i*2+j*2*two_sub_sample_nx]+
00306            laplacian_data[i*2+1+j*2*two_sub_sample_nx]+
00307            laplacian_data[i*2+(j*2+1)*two_sub_sample_nx]+
00308            laplacian_data[i*2+1+(j*2+1)*two_sub_sample_nx])/4.0;
00309       }
00310     }
00311 
00312     cpl_image_save(laplacian_redu_image, "Lplus.fits", CPL_BPP_IEEE_FLOAT,
00313       NULL, CPL_IO_DEFAULT);
00314 
00315     sinfo_msg_debug("Apply median filter");
00316     /* Apply 5x5 median filter on data */
00317     check_nomsg( sci_median5_image = sinfo_image_filter_median( sci_image,
00318       median5_kernel));
00319     check_nomsg (sci_median5_data = cpl_image_get_data_float( sci_median5_image));
00320 
00321     sinfo_msg_debug("Compute noise");
00322     /* computes the noise image */
00323     for( i=0; i< nx*ny; i++){
00324       noise_data[i] = sqrt(sci_median5_data[i]*gain+
00325         ron*ron)/ gain;
00326     }
00327 
00328     sinfo_msg_debug("Compute S");
00329     /* compute S image */
00330     for( i=0; i< nx*ny; i++){
00331       s_data[i] = laplacian_redu_data[i] / (2.0*noise_data[i]);
00332     }
00333 
00334     sinfo_msg_debug("Compute S median");
00335     /* compute S median image */
00336     check_nomsg( s_median_image = sinfo_image_filter_median( s_image,
00337       median5_kernel));
00338     check_nomsg( s_median_data = cpl_image_get_data_float( s_median_image));
00339 
00340     sinfo_msg_debug("Compute s2");
00341     /* compute s2 */
00342     for( i=0; i< nx*ny; i++){
00343       s2_data[i] = s_data[i] -s_median_data[i];
00344     }
00345 
00346        cpl_image_save( s2_image, "S2.fits", CPL_BPP_IEEE_FLOAT, NULL,
00347      CPL_IO_DEFAULT);
00348 
00349     sinfo_msg_debug("Apply 3x3 filter");
00350     /* Apply 3x3 median filter on data */
00351     check_nomsg( sci_median3_image = sinfo_image_filter_median( sci_image,
00352       median3_kernel));
00353 
00354     sinfo_msg_debug("Apply 7x7 filter");
00355     /* Apply 7x7 median filter */
00356     check_nomsg( sci_median3_7_image = sinfo_image_filter_median( sci_median3_image,
00357       median7_kernel));
00358     sinfo_msg_debug("Apply 7x7 filter ok");
00359     check_nomsg ( sci_median3_data = cpl_image_get_data_float( sci_median3_image));
00360     check_nomsg ( sci_median3_7_data = cpl_image_get_data_float(
00361       sci_median3_7_image));
00362 
00363     sinfo_msg_debug("Compute F");
00364     /* compute F */
00365     for( i=0; i< nx*ny; i++){
00366       f_data[i] = sci_median3_data[i] -sci_median3_7_data[i];
00367       if (f_data[i] < 0.01){
00368         f_data[i] = 0.01;
00369       }
00370     }
00371     cpl_image_save( f_image, "F.fits", CPL_BPP_IEEE_FLOAT, NULL,
00372       CPL_IO_DEFAULT);
00373 
00374     sinfo_msg_debug("Compute R");
00375     /* compute R */
00376     for( i=0; i< nx*ny; i++){
00377       r_data[i] = laplacian_redu_data[i]/f_data[i];
00378     }
00379 
00380     cpl_image_save( r_image, "R.fits", CPL_BPP_IEEE_FLOAT, NULL,
00381       CPL_IO_DEFAULT);
00382 
00383     /* Search for cosmics */
00384 
00385     sinfo_msg_debug("Search for cosmic");
00386     new_crh = 0;
00387     median = cpl_vector_new(24);
00388 
00389     for( j=1; j< ny-1; j++){
00390       double *data = NULL;
00391       cpl_vector* med_vect = NULL;
00392 
00393       for( i=1; i< nx-1; i++){
00394         if ( (s2_data[i+j*nx] >= sigma_lim) &&
00395           (r_data[i+j*nx] >= f_lim)){
00396           int li,lj,ui,uj;
00397           cosmic_data[i+j*nx] = 1.0;
00398           new_crh++;
00399           li = i-2;
00400           lj = j-2;
00401           ui = i+2;
00402           uj = j+2;
00403           m = 0;
00404           if (li < 0) li = 0;
00405           if (ui >= nx) ui = nx-1;
00406           if (lj < 0) lj = 0;
00407           if (uj >= ny) uj = ny-1;
00408           for( k=lj; k <= uj; k++){
00409             for( l=li; l <= ui; l++){
00410               //sinfo_msg("REGDEBUG k %d l %d m %d", k, l, m);
00411               if ( k < j){
00412                 cpl_vector_set(median, m, sci_data[l+k*nx]);
00413                 m++;
00414               }
00415               else if ( (k == j) && ( l < i)){
00416                  cpl_vector_set(median, m, sci_data[l+k*nx]);
00417                 m++;
00418               }
00419               else if ( l!=i && k!=j && (s2_data[l+k*nx] < sigma_lim)
00420                 && (r_data[l+k*nx] < f_lim)){
00421                 cpl_vector_set(median, m, sci_data[l+k*nx]);
00422                 m++;
00423               }
00424             }
00425           }
00426           check_nomsg( data = cpl_vector_get_data( median));
00427           sinfo_msg_debug("REGDEBUG i %d j %d m %d", i, j ,m);
00428           check_nomsg( med_vect = cpl_vector_wrap( m, data));
00429           check_nomsg( sci_data[i+j*nx] = cpl_vector_get_median( med_vect));
00430           cpl_vector_unwrap( med_vect);
00431         }
00432       }
00433     }
00434     sinfoni_free_vector( &median ) ;
00435     nb_crh += new_crh;
00436     frac = (double)nb_crh/(double)(nx*ny) ;
00437     sinfo_msg("   new cosmics %d, total %d, frac %.4f [%d pixels]",new_crh,nb_crh,
00438         frac, nx*ny);
00439     nbiter++;
00440     sinfo_free_image( &laplacian_image);
00441     sinfo_free_image( &sci_median3_7_image ) ;
00442     sinfo_free_image( &sci_median3_image ) ;
00443     sinfo_free_image( &s_median_image ) ;
00444     sinfo_free_image( &sci_median5_image ) ;
00445   }
00446   {
00447     FILE *debug = NULL;
00448 
00449     debug = fopen("cosmic.log","w");
00450 
00451     for( j=0; j< ny; j++){
00452       for( i=0; i< nx; i++){
00453         if ( cosmic_data[i+j*nx] == 1.0){
00454           fprintf(debug,"%.1f %.1f\n",i+1.0,j+1.0);
00455         }
00456       }
00457     }
00458     fclose(debug);
00459   }
00460 
00461   //check_nomsg( res_frame = cpl_frame_duplicate( sci_frame ) ) ;
00462   //sinfo_msg( "Saving Result Frame '%s'", res_name ) ;
00463   //check_nomsg( add_qc_crh( sci_pre, nb_crh, 1, instrument ) ) ;
00464   //check_nomsg( res_frame = xsh_pre_save( sci_pre, res_name, 1 ) ) ;
00465   //tag = cpl_frame_get_tag( in_sci_frame ) ;
00466   //check_nomsg( cpl_frame_set_tag( res_frame, tag ) ) ;
00467 
00468   cleanup:
00469   //xsh_pre_free( &sci_pre);
00470   //xsh_localization_list_free( &loc_list ) ;
00471 
00472     /* free kernel */
00473     sinfoni_free_matrix( &laplacian_kernel);
00474     sinfoni_free_matrix( &median3_kernel);
00475     sinfoni_free_matrix( &median5_kernel);
00476     sinfoni_free_matrix( &median7_kernel);
00477     /* free images */
00478     sinfo_free_image( &laplacian_image);
00479     sinfo_free_image( &laplacian_redu_image);
00480     sinfo_free_image( &two_sub_sample);
00481     sinfo_free_image( &sci_median5_image);
00482     sinfo_free_image( &noise_image);
00483     sinfo_free_image( &s_image);
00484     sinfo_free_image( &s_median_image);
00485     sinfo_free_image( &s2_image);
00486     sinfo_free_image( &sci_median3_image);
00487     sinfo_free_image( &sci_median3_7_image);
00488     sinfo_free_image( &f_image);
00489     sinfo_free_image( &r_image);
00490     /* free vector */
00491     sinfoni_free_vector( &median);
00492     /* free tab */
00493     if(cosmic_data!=NULL) cpl_free( cosmic_data);
00494     return res_image;
00495 }

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1