uves_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: 2010/12/08 14:11:18 $
00024  * $Revision: 1.8 $
00025  * $Log: uves_remove_crh_single.c,v $
00026  * Revision 1.8  2010/12/08 14:11:18  amodigli
00027  * fixed typo on starting index for initalizing kernels in LA-cosmic
00028  *
00029  * Revision 1.7  2010/09/24 09:32:07  amodigli
00030  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00031  *
00032  * Revision 1.5  2010/02/08 07:10:14  amodigli
00033  * replace cpl_image_filter_median/linear by uves_image_filter_median/linear
00034  *
00035  * Revision 1.4  2010/01/07 08:12:09  amodigli
00036  * saved old version
00037  *
00038  * Revision 1.2  2009/02/18 12:06:38  amodigli
00039  * fixe compiler warning
00040  *
00041  * Revision 1.1  2009/01/02 08:31:34  amodigli
00042  * copmmitted to CVS
00043  *
00044  *
00045  */
00046 
00047 #ifdef HAVE_CONFIG_H
00048 #  include <config.h>
00049 #endif
00050 
00051 /*----------------------------------------------------------------------------*/
00058 /*----------------------------------------------------------------------------*/
00061 /*-----------------------------------------------------------------------------
00062   Includes
00063 -----------------------------------------------------------------------------*/
00064 
00065 #include <math.h>
00066 
00067 //#include <xsh_drl.h>
00068 //#include <xsh_data_pre.h>
00069 //#include <xsh_dfs.h>
00070 //#include <xsh_pfits.h>
00071 #include <uves_error.h>
00072 #include <uves_msg.h>
00073 #include <uves_utils_wrappers.h>
00074 #include <cpl.h>
00075 //#include <xsh_badpixelmap.h>
00076 //#include <xsh_parameters.h>
00077 //#include <xsh_data_localization.h>
00078 
00079 /*-----------------------------------------------------------------------------
00080   Functions prototypes
00081  -----------------------------------------------------------------------------*/
00082 
00083 /*-----------------------------------------------------------------------------
00084   Implementation
00085  -----------------------------------------------------------------------------*/
00086 
00087 /* This should be defined in a more clever way, a parameter for example */
00088 #define MAX_ITERATIONS 6
00089 
00090 
00103 cpl_image * uves_remove_crh_single( cpl_image * sci_image,
00104                      double crh_frac_max,
00105                      double sigma_lim,
00106                      double f_lim,
00107                      int max_iter,
00108                      double gain,
00109                      double ron)
00110 {
00111   int i,j,k,l,m;
00112   double  frac = 0. ;
00113   /* Only pointers */
00114 
00115   /* Need to be free */
00116   //xsh_localization_list * loc_list = NULL ;
00117   cpl_image* laplacian_image = NULL;
00118   cpl_image* laplacian_redu_image = NULL;
00119   cpl_image* two_sub_sample = NULL;
00120   cpl_image* sci_median5_image = NULL;
00121   cpl_image* noise_image = NULL;
00122   cpl_image* s_image = NULL;
00123   cpl_image* s_median_image = NULL;
00124   cpl_image* s2_image = NULL;
00125   cpl_image* sci_median3_image = NULL;
00126   cpl_image* sci_median3_7_image = NULL;
00127   cpl_image* f_image = NULL;
00128   cpl_image* r_image = NULL;
00129   int two_sub_sample_nx = 0;
00130   int two_sub_sample_ny = 0;
00131   /* Only pointers */
00132   float* sci_data = NULL;
00133   float* two_sub_sample_data = NULL;
00134   float* laplacian_data = NULL;
00135   float* laplacian_redu_data = NULL;
00136   float* sci_median5_data = NULL;
00137   float* sci_median3_data = NULL;
00138   float* sci_median3_7_data = NULL;
00139   float* noise_data = NULL;
00140   float* s_data = NULL;
00141   float* s_median_data = NULL;
00142   float* s2_data = NULL;
00143   float* f_data = NULL;
00144   float* r_data = NULL;
00145   /* Need to be free */
00146   float* cosmic_data = NULL; 
00147  
00148   cpl_matrix* laplacian_kernel = NULL;
00149   cpl_matrix* median3_kernel = NULL;
00150   cpl_matrix* median5_kernel = NULL;
00151   cpl_matrix* median7_kernel = NULL;
00152   int new_crh =1, nb_crh = 0;
00153   int nbiter = 1 ;
00154   cpl_vector* median = NULL;
00155   /* const char * tag = NULL ; */
00156   int nx=0;
00157   int ny=0;
00158   cpl_image* res_image=NULL;
00159 
00160   /* Check parameters */
00161   cknull( sci_image,"null input image" ) ; ;
00162 
00163   uves_msg( "Entering uves_remove_crh_single");
00164   uves_msg( "  Params: frac_max %.1f, sigma_lim %.2f f_lim %.2f, iter %d",
00165        crh_frac_max, sigma_lim, f_lim, max_iter);
00166 
00167   /* Preparing different kernels */
00168   nx=cpl_image_get_size_x(sci_image);
00169   ny=cpl_image_get_size_y(sci_image);
00170 
00171   /* Laplacian */
00172   check_nomsg( laplacian_kernel = cpl_matrix_new(3,3));
00173   cpl_matrix_set( laplacian_kernel,0,0,0.0);
00174   cpl_matrix_set( laplacian_kernel,0,1,-1.0);
00175   cpl_matrix_set( laplacian_kernel,0,2,0.0);
00176   cpl_matrix_set( laplacian_kernel,1,0,-1.0);
00177   cpl_matrix_set( laplacian_kernel,1,1,4.0);
00178   cpl_matrix_set( laplacian_kernel,1,2,-1.0);
00179   cpl_matrix_set( laplacian_kernel,2,0,0.0);
00180   cpl_matrix_set( laplacian_kernel,2,1,-1.0);
00181   cpl_matrix_set( laplacian_kernel,2,2,0.0);
00182   cpl_matrix_divide_scalar( laplacian_kernel, 4.0);     
00183 /*
00184   cpl_matrix_set( laplacian_kernel,0,0,-1.0);
00185   cpl_matrix_set( laplacian_kernel,0,1,-1.0);
00186   cpl_matrix_set( laplacian_kernel,0,2,-1.0);
00187   cpl_matrix_set( laplacian_kernel,1,0,-1.0);
00188   cpl_matrix_set( laplacian_kernel,1,1,8.0);
00189   cpl_matrix_set( laplacian_kernel,1,2,-1.0);
00190   cpl_matrix_set( laplacian_kernel,2,0,-1.0);
00191   cpl_matrix_set( laplacian_kernel,2,1,-1.0);
00192   cpl_matrix_set( laplacian_kernel,2,2,-1.0);
00193   cpl_matrix_divide_scalar( laplacian_kernel, 8.0);
00194 */
00195   /* Median 3x3*/
00196   check_nomsg( median3_kernel = cpl_matrix_new(3,3));
00197   for(j=0; j< 3; j++){
00198     for(i=0; i< 3; i++){
00199       cpl_matrix_set( median3_kernel, i,j,1.0);
00200     }
00201   }
00202 
00203   /* Median 5x5 */
00204   check_nomsg( median5_kernel = cpl_matrix_new(5,5));
00205   for(j=0; j< 5; j++){
00206     for(i=0; i< 5; i++){
00207       cpl_matrix_set( median5_kernel, i,j,1.0);
00208     }
00209   }
00210 
00211   /* Median 7x7 */
00212   check_nomsg( median7_kernel = cpl_matrix_new(7,7));
00213   for(j=0; j< 7; j++){
00214     for(i=0; i< 7; i++){
00215       cpl_matrix_set( median7_kernel, i,j,1.0);
00216     }
00217   }
00218 
00219   check_nomsg (res_image = cpl_image_duplicate( sci_image));
00220 
00221   /* Allocate images and pointers */
00222   check_nomsg (sci_data = cpl_image_get_data_float( res_image));
00223 
00224   two_sub_sample_nx = nx*2;
00225   two_sub_sample_ny = ny*2;
00226   check_nomsg( two_sub_sample = cpl_image_new( two_sub_sample_nx,
00227     two_sub_sample_ny, CPL_TYPE_FLOAT));
00228   check_nomsg(two_sub_sample_data = cpl_image_get_data_float( two_sub_sample));
00229   check_nomsg( laplacian_redu_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00230   check_nomsg(laplacian_redu_data = cpl_image_get_data_float(
00231     laplacian_redu_image));
00232   check_nomsg( noise_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00233   check_nomsg( noise_data = cpl_image_get_data_float( noise_image));
00234   check_nomsg( s_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00235   check_nomsg( s_data = cpl_image_get_data_float( s_image));
00236   check_nomsg( s2_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00237   check_nomsg( s2_data = cpl_image_get_data_float( s2_image));
00238   check_nomsg( f_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00239   check_nomsg( f_data = cpl_image_get_data_float( f_image));
00240   check_nomsg( r_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00241   check_nomsg( r_data = cpl_image_get_data_float( r_image));
00242   cosmic_data=cpl_calloc(nx*ny, sizeof(float));
00243 
00244   /* LGG - Added limit on frac_max AND limit on nb iterations */
00245   while( new_crh > 0 && frac < crh_frac_max && nbiter <= max_iter ){
00246     uves_msg("Iteration %d",nbiter );
00247     /* Create a 2n x 2n images like this 
00248         | 1 | 2 |  =>  | 1 | 1 | 2 | 2 |
00249         | 3 | 4 |      | 1 | 1 | 2 | 2 |
00250                        | 3 | 3 | 4 | 4 |
00251                        | 3 | 3 | 4 | 4 | */
00252     uves_msg_debug("Create a 2n images");
00253     for( j=0; j< ny; j++){
00254       for( i=0; i< nx; i++){
00255         float val = sci_data[i+j*nx];
00256 
00257     if ( val < 0. ) val = 0. ;
00258         two_sub_sample_data[i*2+j*2*two_sub_sample_nx] = val;
00259         two_sub_sample_data[i*2+1+j*2*two_sub_sample_nx] = val;
00260         two_sub_sample_data[i*2+(j*2+1)*two_sub_sample_nx] = val;
00261         two_sub_sample_data[i*2+1+(j*2+1)*two_sub_sample_nx] = val;
00262       }
00263     }
00264     uves_msg_debug("Doing laplacian convolution");
00265     /* Doing the laplacian convolution 
00266         0  -1   0
00267        -1   4  -1 
00268         0  -1   0 */
00269     check_nomsg(laplacian_image = uves_image_filter_linear( two_sub_sample, 
00270                                                             laplacian_kernel));
00271 
00272     /* multiply by two to normalize correctly the laplacian [RD5] 
00273        and filter negative values */
00274     uves_msg_debug("Normalize laplacian");
00275     check_nomsg (laplacian_data = cpl_image_get_data_float( laplacian_image));
00276     for ( i=0; i< two_sub_sample_nx*two_sub_sample_ny; i++){
00277       if (laplacian_data[i] > 0.0){
00278         laplacian_data[i] = 2.0 * laplacian_data[i]; 
00279       }
00280       else{
00281         laplacian_data[i] = 0.0;
00282       }
00283     }
00284     uves_msg_debug("Save Lpositive");
00285     cpl_image_save(laplacian_image, "Lpositive.fits", CPL_BPP_IEEE_FLOAT, NULL,
00286       CPL_IO_DEFAULT);
00287 
00288     /* resample to the original size 
00289        | 1 | 1 | 2 | 2 |    | 1 | 2 | 
00290        | 1 | 1 | 2 | 2 |    | 3 | 4 |
00291        | 3 | 3 | 4 | 4 | => 
00292        | 3 | 3 | 4 | 4 |               */
00293 
00294     uves_msg_debug("Resample to the original size");
00295 
00296     for( j=0; j< ny; j++){
00297       for( i=0; i< nx; i++){
00298         laplacian_redu_data[i+j*nx] = 
00299           (laplacian_data[i*2+j*2*two_sub_sample_nx]+
00300            laplacian_data[i*2+1+j*2*two_sub_sample_nx]+
00301            laplacian_data[i*2+(j*2+1)*two_sub_sample_nx]+
00302            laplacian_data[i*2+1+(j*2+1)*two_sub_sample_nx])/4.0;
00303       }
00304     }
00305 
00306     cpl_image_save(laplacian_redu_image, "Lplus.fits", CPL_BPP_IEEE_FLOAT, 
00307       NULL, CPL_IO_DEFAULT);
00308 
00309     uves_msg_debug("Apply median filter");
00310     /* Apply 5x5 median filter on data */
00311     check_nomsg( sci_median5_image = uves_image_filter_median( sci_image, 
00312       median5_kernel)); 
00313     check_nomsg (sci_median5_data = cpl_image_get_data_float( sci_median5_image));
00314  
00315     uves_msg_debug("Compute noise");
00316     /* computes the noise image */
00317     for( i=0; i< nx*ny; i++){
00318       noise_data[i] = sqrt(sci_median5_data[i]*gain+
00319         ron*ron)/ gain;
00320     }
00321 
00322     uves_msg_debug("Compute S");
00323     /* compute S image */
00324     for( i=0; i< nx*ny; i++){
00325       s_data[i] = laplacian_redu_data[i] / (2.0*noise_data[i]);
00326     }
00327 
00328     uves_msg_debug("Compute S median");
00329     /* compute S median image */
00330     check_nomsg( s_median_image = uves_image_filter_median( s_image,
00331       median5_kernel));
00332     check_nomsg( s_median_data = cpl_image_get_data_float( s_median_image));
00333 
00334     uves_msg_debug("Compute s2");
00335     /* compute s2 */
00336     for( i=0; i< nx*ny; i++){
00337       s2_data[i] = s_data[i] -s_median_data[i];
00338     }
00339  
00340        cpl_image_save( s2_image, "S2.fits", CPL_BPP_IEEE_FLOAT, NULL,
00341      CPL_IO_DEFAULT);
00342 
00343     uves_msg_debug("Apply 3x3 filter");
00344     /* Apply 3x3 median filter on data */
00345     check_nomsg( sci_median3_image = uves_image_filter_median( sci_image,
00346       median3_kernel));
00347 
00348     uves_msg_debug("Apply 7x7 filter");
00349     /* Apply 7x7 median filter */ 
00350     check_nomsg( sci_median3_7_image = uves_image_filter_median( sci_median3_image,
00351       median7_kernel));
00352     uves_msg_debug("Apply 7x7 filter ok");
00353     check_nomsg ( sci_median3_data = cpl_image_get_data_float( sci_median3_image));
00354     check_nomsg ( sci_median3_7_data = cpl_image_get_data_float( 
00355       sci_median3_7_image));
00356 
00357     uves_msg_debug("Compute F");
00358     /* compute F */
00359     for( i=0; i< nx*ny; i++){
00360       f_data[i] = sci_median3_data[i] -sci_median3_7_data[i];
00361       if (f_data[i] < 0.01){
00362         f_data[i] = 0.01;
00363       }
00364     }
00365     cpl_image_save( f_image, "F.fits", CPL_BPP_IEEE_FLOAT, NULL,
00366       CPL_IO_DEFAULT);
00367 
00368     uves_msg_debug("Compute R");
00369     /* compute R */
00370     for( i=0; i< nx*ny; i++){
00371       r_data[i] = laplacian_redu_data[i]/f_data[i];
00372     }
00373 
00374     cpl_image_save( r_image, "R.fits", CPL_BPP_IEEE_FLOAT, NULL,
00375       CPL_IO_DEFAULT);
00376 
00377     /* Search for cosmics */
00378 
00379     uves_msg_debug("Search for cosmic");
00380     new_crh = 0;
00381     median = cpl_vector_new(24);
00382 
00383     for( j=1; j< ny-1; j++){
00384       double *data = NULL;
00385       cpl_vector* med_vect = NULL;
00386 
00387       for( i=1; i< nx-1; i++){
00388         if ( (s2_data[i+j*nx] >= sigma_lim) && 
00389           (r_data[i+j*nx] >= f_lim)){
00390           int li,lj,ui,uj;
00391           cosmic_data[i+j*nx] = 1.0;
00392           new_crh++;
00393           li = i-2;
00394           lj = j-2;
00395           ui = i+2;
00396           uj = j+2;
00397           m = 0;
00398           if (li < 0) li = 0;
00399           if (ui >= nx) ui = nx-1;
00400           if (lj < 0) lj = 0;
00401           if (uj >= ny) uj = ny-1;
00402           for( k=lj; k <= uj; k++){
00403             for( l=li; l <= ui; l++){
00404               //uves_msg("REGDEBUG k %d l %d m %d", k, l, m);
00405               if ( k < j){
00406                 cpl_vector_set(median, m, sci_data[l+k*nx]);
00407                 m++;
00408               }
00409               else if ( (k == j) && ( l < i)){
00410                  cpl_vector_set(median, m, sci_data[l+k*nx]);
00411                 m++;
00412               }
00413               else if ( l!=i && k!=j && (s2_data[l+k*nx] < sigma_lim)
00414                 && (r_data[l+k*nx] < f_lim)){
00415                 cpl_vector_set(median, m, sci_data[l+k*nx]);
00416                 m++;
00417               }
00418             }
00419           }
00420           check_nomsg( data = cpl_vector_get_data( median));
00421           uves_msg_debug("REGDEBUG i %d j %d m %d", i, j ,m);
00422           check_nomsg( med_vect = cpl_vector_wrap( m, data));
00423           check_nomsg( sci_data[i+j*nx] = cpl_vector_get_median( med_vect));
00424           cpl_vector_unwrap( med_vect);
00425         }
00426       }
00427     }
00428     uves_free_vector( &median ) ;
00429     nb_crh += new_crh;
00430     frac = (double)nb_crh/(double)(nx*ny) ;
00431     uves_msg("   new cosmics %d, total %d, frac %.4f [%d pixels]",new_crh,nb_crh,
00432         frac, nx*ny);
00433     nbiter++;
00434     uves_free_image( &laplacian_image);
00435     uves_free_image( &sci_median3_7_image ) ;
00436     uves_free_image( &sci_median3_image ) ;
00437     uves_free_image( &s_median_image ) ;
00438     uves_free_image( &sci_median5_image ) ;
00439   }
00440   {
00441     FILE *debug = NULL;
00442 
00443     debug = fopen("cosmic.log","w");
00444     
00445     for( j=0; j< ny; j++){
00446       for( i=0; i< nx; i++){
00447         if ( cosmic_data[i+j*nx] == 1.0){
00448           fprintf(debug,"%.1f %.1f\n",i+1.0,j+1.0);
00449         }
00450       }
00451     }
00452     fclose(debug);
00453   }
00454 
00455   //check_nomsg( res_frame = cpl_frame_duplicate( sci_frame ) ) ;
00456   //uves_msg( "Saving Result Frame '%s'", res_name ) ;
00457   //check_nomsg( add_qc_crh( sci_pre, nb_crh, 1, instrument ) ) ;
00458   //check_nomsg( res_frame = xsh_pre_save( sci_pre, res_name, 1 ) ) ;
00459   //tag = cpl_frame_get_tag( in_sci_frame ) ;
00460   //check_nomsg( cpl_frame_set_tag( res_frame, tag ) ) ;
00461 
00462 
00463   cleanup:
00464   //xsh_pre_free( &sci_pre);
00465   //xsh_localization_list_free( &loc_list ) ;
00466 
00467     /* free kernel */
00468     uves_free_matrix( &laplacian_kernel);
00469     uves_free_matrix( &median3_kernel);
00470     uves_free_matrix( &median5_kernel);
00471     uves_free_matrix( &median7_kernel);
00472     /* free images */
00473     uves_free_image( &laplacian_image);
00474     uves_free_image( &laplacian_redu_image);
00475     uves_free_image( &two_sub_sample);
00476     uves_free_image( &sci_median5_image);
00477     uves_free_image( &noise_image);
00478     uves_free_image( &s_image);
00479     uves_free_image( &s_median_image); 
00480     uves_free_image( &s2_image);
00481     uves_free_image( &sci_median3_image);
00482     uves_free_image( &sci_median3_7_image);
00483     uves_free_image( &f_image);
00484     uves_free_image( &r_image);
00485     /* free vector */
00486     uves_free_vector( &median);
00487     /* free tab */
00488     if(cosmic_data!=NULL) cpl_free( cosmic_data);
00489     return res_image;
00490 }

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1