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 #ifdef HAVE_CONFIG_H
00042 # include <config.h>
00043 #endif
00044
00045
00052
00055
00056
00057
00058
00059 #include <math.h>
00060
00061
00062 #include <sinfo_error.h>
00063 #include <sinfo_msg.h>
00064 #include <sinfo_utils_wrappers.h>
00065 #include <cpl.h>
00066 #include "sinfo_utilities.h"
00067
00068
00069
00070 cpl_image * sinfo_remove_crh_single( cpl_image * sci_image,
00071 double crh_frac_max,
00072 double sigma_lim,
00073 double f_lim,
00074 int max_iter,
00075 double gain,
00076 double ron);
00077
00078
00079
00080
00081
00082 #define MAX_ITERATIONS 6
00083
00084
00106 cpl_image * sinfo_remove_crh_single( cpl_image * sci_image,
00107 double crh_frac_max,
00108 double sigma_lim,
00109 double f_lim,
00110 int max_iter,
00111 double gain,
00112 double ron)
00113 {
00114 int i,j,k,l,m;
00115 double frac = 0. ;
00116
00117
00118
00119
00120 cpl_image* laplacian_image = NULL;
00121 cpl_image* laplacian_redu_image = NULL;
00122 cpl_image* two_sub_sample = NULL;
00123 cpl_image* sci_median5_image = NULL;
00124 cpl_image* noise_image = NULL;
00125 cpl_image* s_image = NULL;
00126 cpl_image* s_median_image = NULL;
00127 cpl_image* s2_image = NULL;
00128 cpl_image* sci_median3_image = NULL;
00129 cpl_image* sci_median3_7_image = NULL;
00130 cpl_image* f_image = NULL;
00131 cpl_image* r_image = NULL;
00132 int two_sub_sample_nx = 0;
00133 int two_sub_sample_ny = 0;
00134
00135 float* sci_data = NULL;
00136 float* two_sub_sample_data = NULL;
00137 float* laplacian_data = NULL;
00138 float* laplacian_redu_data = NULL;
00139 float* sci_median5_data = NULL;
00140 float* sci_median3_data = NULL;
00141 float* sci_median3_7_data = NULL;
00142 float* noise_data = NULL;
00143 float* s_data = NULL;
00144 float* s_median_data = NULL;
00145 float* s2_data = NULL;
00146 float* f_data = NULL;
00147 float* r_data = NULL;
00148
00149 float* cosmic_data = NULL;
00150
00151 cpl_matrix* laplacian_kernel = NULL;
00152 cpl_matrix* median3_kernel = NULL;
00153 cpl_matrix* median5_kernel = NULL;
00154 cpl_matrix* median7_kernel = NULL;
00155 int new_crh =1, nb_crh = 0;
00156 int nbiter = 1 ;
00157 cpl_vector* median = NULL;
00158
00159 int nx=0;
00160 int ny=0;
00161 cpl_image* res_image=NULL;
00162
00163
00164 cknull( sci_image,"null input image" ) ; ;
00165
00166 sinfo_msg( "Entering sinfo_remove_crh_single");
00167 sinfo_msg( " Params: frac_max %.1f, sigma_lim %.2f f_lim %.2f, iter %d",
00168 crh_frac_max, sigma_lim, f_lim, max_iter);
00169
00170
00171 nx=cpl_image_get_size_x(sci_image);
00172 ny=cpl_image_get_size_y(sci_image);
00173
00174
00175 check_nomsg( laplacian_kernel = cpl_matrix_new(3,3));
00176 cpl_matrix_set( laplacian_kernel,0,0,0.0);
00177 cpl_matrix_set( laplacian_kernel,0,1,-1.0);
00178 cpl_matrix_set( laplacian_kernel,0,2,0.0);
00179 cpl_matrix_set( laplacian_kernel,1,0,-1.0);
00180 cpl_matrix_set( laplacian_kernel,1,1,4.0);
00181 cpl_matrix_set( laplacian_kernel,1,2,-1.0);
00182 cpl_matrix_set( laplacian_kernel,2,0,0.0);
00183 cpl_matrix_set( laplacian_kernel,2,1,-1.0);
00184 cpl_matrix_set( laplacian_kernel,2,2,0.0);
00185 cpl_matrix_divide_scalar( laplacian_kernel, 4.0);
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 check_nomsg( median3_kernel = cpl_matrix_new(3,3));
00200 for(j=0; j< 3; j++){
00201 for(i=0; i< 3; i++){
00202 cpl_matrix_set( median3_kernel, i,j,1.0);
00203 }
00204 }
00205
00206
00207 check_nomsg( median5_kernel = cpl_matrix_new(5,5));
00208 for(j=0; j< 5; j++){
00209 for(i=0; i< 5; i++){
00210 cpl_matrix_set( median5_kernel, i,j,1.0);
00211 }
00212 }
00213
00214
00215 check_nomsg( median7_kernel = cpl_matrix_new(7,7));
00216 for(j=0; j< 7; j++){
00217 for(i=0; i< 7; i++){
00218 cpl_matrix_set( median7_kernel, i,j,1.0);
00219 }
00220 }
00221
00222 check_nomsg (res_image = cpl_image_duplicate( sci_image));
00223
00224
00225 check_nomsg (sci_data = cpl_image_get_data_float( res_image));
00226
00227 two_sub_sample_nx = nx*2;
00228 two_sub_sample_ny = ny*2;
00229 check_nomsg( two_sub_sample = cpl_image_new( two_sub_sample_nx,
00230 two_sub_sample_ny, CPL_TYPE_FLOAT));
00231 check_nomsg(two_sub_sample_data = cpl_image_get_data_float( two_sub_sample));
00232 check_nomsg( laplacian_redu_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00233 check_nomsg(laplacian_redu_data = cpl_image_get_data_float(
00234 laplacian_redu_image));
00235 check_nomsg( noise_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00236 check_nomsg( noise_data = cpl_image_get_data_float( noise_image));
00237 check_nomsg( s_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00238 check_nomsg( s_data = cpl_image_get_data_float( s_image));
00239 check_nomsg( s2_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00240 check_nomsg( s2_data = cpl_image_get_data_float( s2_image));
00241 check_nomsg( f_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00242 check_nomsg( f_data = cpl_image_get_data_float( f_image));
00243 check_nomsg( r_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00244 check_nomsg( r_data = cpl_image_get_data_float( r_image));
00245 cosmic_data=cpl_calloc(nx*ny, sizeof(float));
00246
00247
00248 while( new_crh > 0 && frac < crh_frac_max && nbiter <= max_iter ){
00249 sinfo_msg("Iteration %d",nbiter );
00250
00251
00252
00253
00254
00255 sinfo_msg_debug("Create a 2n images");
00256 for( j=0; j< ny; j++){
00257 for( i=0; i< nx; i++){
00258 float val = sci_data[i+j*nx];
00259
00260 if ( val < 0. ) val = 0. ;
00261 two_sub_sample_data[i*2+j*2*two_sub_sample_nx] = val;
00262 two_sub_sample_data[i*2+1+j*2*two_sub_sample_nx] = val;
00263 two_sub_sample_data[i*2+(j*2+1)*two_sub_sample_nx] = val;
00264 two_sub_sample_data[i*2+1+(j*2+1)*two_sub_sample_nx] = val;
00265 }
00266 }
00267 sinfo_msg_debug("Doing laplacian convolution");
00268
00269
00270
00271
00272 laplacian_image = sinfo_image_filter_linear( two_sub_sample,
00273 laplacian_kernel);
00274
00275
00276
00277 sinfo_msg_debug("Normalize laplacian");
00278 check_nomsg (laplacian_data = cpl_image_get_data_float( laplacian_image));
00279 for ( i=0; i< two_sub_sample_nx*two_sub_sample_ny; i++){
00280 if (laplacian_data[i] > 0.0){
00281 laplacian_data[i] = 2.0 * laplacian_data[i];
00282 }
00283 else{
00284 laplacian_data[i] = 0.0;
00285 }
00286 }
00287 sinfo_msg_debug("Save Lpositive");
00288 cpl_image_save(laplacian_image, "Lpositive.fits", CPL_BPP_IEEE_FLOAT, NULL,
00289 CPL_IO_DEFAULT);
00290
00291
00292
00293
00294
00295
00296
00297 sinfo_msg_debug("Resample to the original size");
00298
00299 for( j=0; j< ny; j++){
00300 for( i=0; i< nx; i++){
00301 laplacian_redu_data[i+j*nx] =
00302 (laplacian_data[i*2+j*2*two_sub_sample_nx]+
00303 laplacian_data[i*2+1+j*2*two_sub_sample_nx]+
00304 laplacian_data[i*2+(j*2+1)*two_sub_sample_nx]+
00305 laplacian_data[i*2+1+(j*2+1)*two_sub_sample_nx])/4.0;
00306 }
00307 }
00308
00309 cpl_image_save(laplacian_redu_image, "Lplus.fits", CPL_BPP_IEEE_FLOAT,
00310 NULL, CPL_IO_DEFAULT);
00311
00312 sinfo_msg_debug("Apply median filter");
00313
00314 check_nomsg( sci_median5_image = sinfo_image_filter_median( sci_image,
00315 median5_kernel));
00316 check_nomsg (sci_median5_data = cpl_image_get_data_float( sci_median5_image));
00317
00318 sinfo_msg_debug("Compute noise");
00319
00320 for( i=0; i< nx*ny; i++){
00321 noise_data[i] = sqrt(sci_median5_data[i]*gain+
00322 ron*ron)/ gain;
00323 }
00324
00325 sinfo_msg_debug("Compute S");
00326
00327 for( i=0; i< nx*ny; i++){
00328 s_data[i] = laplacian_redu_data[i] / (2.0*noise_data[i]);
00329 }
00330
00331 sinfo_msg_debug("Compute S median");
00332
00333 check_nomsg( s_median_image = sinfo_image_filter_median( s_image,
00334 median5_kernel));
00335 check_nomsg( s_median_data = cpl_image_get_data_float( s_median_image));
00336
00337 sinfo_msg_debug("Compute s2");
00338
00339 for( i=0; i< nx*ny; i++){
00340 s2_data[i] = s_data[i] -s_median_data[i];
00341 }
00342
00343 cpl_image_save( s2_image, "S2.fits", CPL_BPP_IEEE_FLOAT, NULL,
00344 CPL_IO_DEFAULT);
00345
00346 sinfo_msg_debug("Apply 3x3 filter");
00347
00348 check_nomsg( sci_median3_image = sinfo_image_filter_median( sci_image,
00349 median3_kernel));
00350
00351 sinfo_msg_debug("Apply 7x7 filter");
00352
00353 check_nomsg( sci_median3_7_image = sinfo_image_filter_median( sci_median3_image,
00354 median7_kernel));
00355 sinfo_msg_debug("Apply 7x7 filter ok");
00356 check_nomsg ( sci_median3_data = cpl_image_get_data_float( sci_median3_image));
00357 check_nomsg ( sci_median3_7_data = cpl_image_get_data_float(
00358 sci_median3_7_image));
00359
00360 sinfo_msg_debug("Compute F");
00361
00362 for( i=0; i< nx*ny; i++){
00363 f_data[i] = sci_median3_data[i] -sci_median3_7_data[i];
00364 if (f_data[i] < 0.01){
00365 f_data[i] = 0.01;
00366 }
00367 }
00368 cpl_image_save( f_image, "F.fits", CPL_BPP_IEEE_FLOAT, NULL,
00369 CPL_IO_DEFAULT);
00370
00371 sinfo_msg_debug("Compute R");
00372
00373 for( i=0; i< nx*ny; i++){
00374 r_data[i] = laplacian_redu_data[i]/f_data[i];
00375 }
00376
00377 cpl_image_save( r_image, "R.fits", CPL_BPP_IEEE_FLOAT, NULL,
00378 CPL_IO_DEFAULT);
00379
00380
00381
00382 sinfo_msg_debug("Search for cosmic");
00383 new_crh = 0;
00384 median = cpl_vector_new(24);
00385
00386 for( j=1; j< ny-1; j++){
00387 double *data = NULL;
00388 cpl_vector* med_vect = NULL;
00389
00390 for( i=1; i< nx-1; i++){
00391 if ( (s2_data[i+j*nx] >= sigma_lim) &&
00392 (r_data[i+j*nx] >= f_lim)){
00393 int li,lj,ui,uj;
00394 cosmic_data[i+j*nx] = 1.0;
00395 new_crh++;
00396 li = i-2;
00397 lj = j-2;
00398 ui = i+2;
00399 uj = j+2;
00400 m = 0;
00401 if (li < 0) li = 0;
00402 if (ui >= nx) ui = nx-1;
00403 if (lj < 0) lj = 0;
00404 if (uj >= ny) uj = ny-1;
00405 for( k=lj; k <= uj; k++){
00406 for( l=li; l <= ui; l++){
00407
00408 if ( k < j){
00409 cpl_vector_set(median, m, sci_data[l+k*nx]);
00410 m++;
00411 }
00412 else if ( (k == j) && ( l < i)){
00413 cpl_vector_set(median, m, sci_data[l+k*nx]);
00414 m++;
00415 }
00416 else if ( l!=i && k!=j && (s2_data[l+k*nx] < sigma_lim)
00417 && (r_data[l+k*nx] < f_lim)){
00418 cpl_vector_set(median, m, sci_data[l+k*nx]);
00419 m++;
00420 }
00421 }
00422 }
00423 check_nomsg( data = cpl_vector_get_data( median));
00424 sinfo_msg_debug("REGDEBUG i %d j %d m %d", i, j ,m);
00425 check_nomsg( med_vect = cpl_vector_wrap( m, data));
00426 check_nomsg( sci_data[i+j*nx] = cpl_vector_get_median( med_vect));
00427 cpl_vector_unwrap( med_vect);
00428 }
00429 }
00430 }
00431 sinfoni_free_vector( &median ) ;
00432 nb_crh += new_crh;
00433 frac = (double)nb_crh/(double)(nx*ny) ;
00434 sinfo_msg(" new cosmics %d, total %d, frac %.4f [%d pixels]",new_crh,nb_crh,
00435 frac, nx*ny);
00436 nbiter++;
00437 sinfo_free_image( &laplacian_image);
00438 sinfo_free_image( &sci_median3_7_image ) ;
00439 sinfo_free_image( &sci_median3_image ) ;
00440 sinfo_free_image( &s_median_image ) ;
00441 sinfo_free_image( &sci_median5_image ) ;
00442 }
00443 {
00444 FILE *debug = NULL;
00445
00446 debug = fopen("cosmic.log","w");
00447
00448 for( j=0; j< ny; j++){
00449 for( i=0; i< nx; i++){
00450 if ( cosmic_data[i+j*nx] == 1.0){
00451 fprintf(debug,"%.1f %.1f\n",i+1.0,j+1.0);
00452 }
00453 }
00454 }
00455 fclose(debug);
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465 cleanup:
00466
00467
00468
00469
00470 sinfoni_free_matrix( &laplacian_kernel);
00471 sinfoni_free_matrix( &median3_kernel);
00472 sinfoni_free_matrix( &median5_kernel);
00473 sinfoni_free_matrix( &median7_kernel);
00474
00475 sinfo_free_image( &laplacian_image);
00476 sinfo_free_image( &laplacian_redu_image);
00477 sinfo_free_image( &two_sub_sample);
00478 sinfo_free_image( &sci_median5_image);
00479 sinfo_free_image( &noise_image);
00480 sinfo_free_image( &s_image);
00481 sinfo_free_image( &s_median_image);
00482 sinfo_free_image( &s2_image);
00483 sinfo_free_image( &sci_median3_image);
00484 sinfo_free_image( &sci_median3_7_image);
00485 sinfo_free_image( &f_image);
00486 sinfo_free_image( &r_image);
00487
00488 sinfoni_free_vector( &median);
00489
00490 if(cosmic_data!=NULL) cpl_free( cosmic_data);
00491 return res_image;
00492 }