00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifdef HAVE_CONFIG_H
00045 # include <config.h>
00046 #endif
00047
00049
00056
00057
00058
00059
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
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
00082
00083
00084
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
00120
00121
00122
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
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
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
00162 int nx=0;
00163 int ny=0;
00164 cpl_image* res_image=NULL;
00165
00166
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
00174 nx=cpl_image_get_size_x(sci_image);
00175 ny=cpl_image_get_size_y(sci_image);
00176
00177
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
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
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
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
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
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
00251 while( new_crh > 0 && frac < crh_frac_max && nbiter <= max_iter ){
00252 sinfo_msg("Iteration %d",nbiter );
00253
00254
00255
00256
00257
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
00272
00273
00274
00275 laplacian_image = sinfo_image_filter_linear( two_sub_sample,
00276 laplacian_kernel);
00277
00278
00279
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
00295
00296
00297
00298
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
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
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
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
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
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
00351 check_nomsg( sci_median3_image = sinfo_image_filter_median( sci_image,
00352 median3_kernel));
00353
00354 sinfo_msg_debug("Apply 7x7 filter");
00355
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
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
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
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
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
00462
00463
00464
00465
00466
00467
00468 cleanup:
00469
00470
00471
00472
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
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
00491 sinfoni_free_vector( &median);
00492
00493 if(cosmic_data!=NULL) cpl_free( cosmic_data);
00494 return res_image;
00495 }