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
00045
00046
00047 #ifdef HAVE_CONFIG_H
00048 # include <config.h>
00049 #endif
00050
00051
00058
00061
00062
00063
00064
00065 #include <math.h>
00066
00067
00068
00069
00070
00071 #include <uves_error.h>
00072 #include <uves_msg.h>
00073 #include <uves_utils_wrappers.h>
00074 #include <cpl.h>
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
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
00114
00115
00116
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
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
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
00156 int nx=0;
00157 int ny=0;
00158 cpl_image* res_image=NULL;
00159
00160
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
00168 nx=cpl_image_get_size_x(sci_image);
00169 ny=cpl_image_get_size_y(sci_image);
00170
00171
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
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
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
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
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
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
00245 while( new_crh > 0 && frac < crh_frac_max && nbiter <= max_iter ){
00246 uves_msg("Iteration %d",nbiter );
00247
00248
00249
00250
00251
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
00266
00267
00268
00269 check_nomsg(laplacian_image = uves_image_filter_linear( two_sub_sample,
00270 laplacian_kernel));
00271
00272
00273
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
00289
00290
00291
00292
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
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
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
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
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
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
00345 check_nomsg( sci_median3_image = uves_image_filter_median( sci_image,
00346 median3_kernel));
00347
00348 uves_msg_debug("Apply 7x7 filter");
00349
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
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
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
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
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
00456
00457
00458
00459
00460
00461
00462
00463 cleanup:
00464
00465
00466
00467
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
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
00486 uves_free_vector( &median);
00487
00488 if(cosmic_data!=NULL) cpl_free( cosmic_data);
00489 return res_image;
00490 }