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 #ifdef HAVE_CONFIG_H
00028 # include <config.h>
00029 #endif
00030
00031
00038
00041
00042
00043
00044
00045 #include <math.h>
00046
00047 #include <xsh_drl.h>
00048 #include <xsh_data_pre.h>
00049 #include <xsh_dfs.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_error.h>
00052 #include <xsh_msg.h>
00053 #include <cpl.h>
00054 #include <xsh_badpixelmap.h>
00055 #include <xsh_parameters.h>
00056 #include <xsh_utils_wrappers.h>
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #define MAX_ITERATIONS 6
00068 #define REGDEBUG_FULL 0
00069
00070
00071
00072
00080 static void add_qc_crh (xsh_pre* pre, int nbcrh, int nframes)
00081 {
00082 double qc_crrate = 0.0;
00083 int nbcr_avg ;
00085 XSH_ASSURE_NOT_NULL(pre);
00086 XSH_ASSURE_NOT_ILLEGAL(pre->pszx >0. && pre->pszy > 0);
00087 XSH_ASSURE_NOT_ILLEGAL(pre->exptime > 0);
00088
00089
00090
00091
00092
00093
00094 xsh_msg_dbg_medium( "add_qc_crh - Exptime = %f", pre->exptime ) ;
00095 qc_crrate =
00096 (double) nbcrh / (((double) nframes) * (pre->exptime *
00097 (pre->pszx / 10000.0) * (pre->pszy /10000.0) * pre->nx * pre->ny)) ;
00098 nbcr_avg = nbcrh/nframes ;
00099
00100
00101
00102
00103 check( xsh_pfits_set_qc_crrate( pre->data_header,qc_crrate) ) ;
00104 check( xsh_pfits_set_qc_ncrh( pre->data_header,nbcrh) ) ;
00105 check( xsh_pfits_set_qc_ncrh_mean( pre->data_header,nbcr_avg) ) ;
00106
00107 check( xsh_pfits_set_qc_crrate( pre->qual_header,qc_crrate) ) ;
00108 check( xsh_pfits_set_qc_ncrh( pre->qual_header,nbcrh) ) ;
00109 check( xsh_pfits_set_qc_ncrh_mean( pre->qual_header,nbcr_avg) ) ;
00110 cleanup:
00111 return ;
00112 }
00113
00114
00115
00116
00117
00118 cpl_frame * xsh_abs_remove_crh_single( cpl_frame *sci_frame,
00119 cpl_frame* wavemap_frame,
00120 xsh_instrument * instrument,
00121 xsh_remove_crh_single_param * single_par,
00122 const char * res_tag)
00123 {
00124 cpl_frame *result_frame = NULL;
00125 cpl_frame *abs_frame = NULL;
00126 cpl_frame *sign_frame = NULL;
00127 cpl_frame *rmcrh_frame = NULL;
00128 const char* name = NULL;
00129 cpl_propertylist *header = NULL;
00130 int qc_ncrh =0;
00131
00132 XSH_ASSURE_NOT_NULL( sci_frame);
00133
00134 check( name = cpl_frame_get_filename( sci_frame));
00135 check( header = cpl_propertylist_load( name, 0));
00136 if(cpl_propertylist_has(header,XSH_QC_CRH_NUMBER)) {
00137 check( qc_ncrh = xsh_pfits_get_qc_ncrh( header));
00138 } else {
00139 qc_ncrh =0;
00140 }
00141
00142 if (qc_ncrh > 0){
00143 xsh_msg( "Not use remove crh single");
00144 check( result_frame = cpl_frame_duplicate( sci_frame));
00145 }
00146 else{
00147 check( abs_frame = xsh_frame_abs( sci_frame, instrument, &sign_frame));
00148 check( rmcrh_frame = xsh_remove_crh_single( abs_frame, wavemap_frame, instrument,
00149 single_par, res_tag));
00150 check( result_frame = xsh_frame_mult( rmcrh_frame, instrument, sign_frame));
00151 }
00152
00153 cleanup:
00154 if (cpl_error_get_code() != CPL_ERROR_NONE){
00155 xsh_free_frame( &result_frame);
00156 }
00157 xsh_free_propertylist( &header);
00158 xsh_free_frame( &abs_frame);
00159 xsh_free_frame( &sign_frame);
00160 xsh_free_frame( &rmcrh_frame);
00161 return result_frame;
00162 }
00163
00164
00181
00182 cpl_frame * xsh_remove_crh_single( cpl_frame * in_sci_frame,
00183 cpl_frame* wavemap,
00184 xsh_instrument * instrument,
00185 xsh_remove_crh_single_param * single_par,
00186 const char * res_tag )
00187 {
00188 int i,j,k,l,m;
00189 double sigma_lim, f_lim, frac_max, frac = 0.;
00190
00191 cpl_frame* res_frame = NULL ;
00192 int *qual = NULL;
00193 char* res_name=NULL;
00194
00195 cpl_image* laplacian_image = NULL;
00196 cpl_image* laplacian_redu_image = NULL;
00197 cpl_image* two_sub_sample = NULL;
00198 cpl_image* sci_median5_image = NULL;
00199 cpl_image* noise_image = NULL;
00200 cpl_image* s_image = NULL;
00201 cpl_image* s_median_image = NULL;
00202 cpl_image* s2_image = NULL;
00203 cpl_image* sci_median3_image = NULL;
00204 cpl_image* sci_median3_7_image = NULL;
00205 cpl_image* f_image = NULL;
00206 cpl_image* r_image = NULL;
00207 cpl_image* c_image = NULL;
00208 int two_sub_sample_nx = 0;
00209 int two_sub_sample_ny = 0;
00210 xsh_pre* in_sci_pre = NULL;
00211 xsh_pre* sci_pre = NULL;
00212 cpl_image* sci_image = NULL;
00213
00214 float* sci_data = NULL;
00215 float* two_sub_sample_data = NULL;
00216 float* laplacian_data = NULL;
00217 float* laplacian_redu_data = NULL;
00218 float* sci_median5_data = NULL;
00219 float* sci_median3_data = NULL;
00220 float* sci_median3_7_data = NULL;
00221 float* noise_data = NULL;
00222 float* s_data = NULL;
00223 float* s_median_data = NULL;
00224 float* s2_data = NULL;
00225 float* f_data = NULL;
00226 float* r_data = NULL;
00227 int* c_data = NULL;
00228
00229
00230 cpl_matrix* laplacian_kernel = NULL;
00231 cpl_matrix* median3_kernel = NULL;
00232 cpl_matrix* median5_kernel = NULL;
00233 cpl_matrix* median7_kernel = NULL;
00234 int new_crh =1, nb_crh = 0;
00235 int nbiter = 1, max_iter = MAX_ITERATIONS ;
00236 cpl_vector* median = NULL;
00237 cpl_image* w_image=NULL;
00238 double* w_data=NULL;
00239 const char* w_name=NULL;
00240 int fctx=1;
00241 int fcty=1;
00242 int wmap_xsize_diff=0;
00243 double ron, conad;
00244 int nx=0;
00245 int ny=0;
00246 double ron2=0;
00247
00248
00249 XSH_ASSURE_NOT_NULL_MSG( in_sci_frame,"Null input science frame" ) ;
00250 XSH_ASSURE_NOT_NULL_MSG( single_par,"Null input parameters" ) ;
00251 XSH_ASSURE_NOT_NULL_MSG( instrument,"Null instrument setting" ) ;
00252
00253 sigma_lim = single_par->sigma_lim;
00254 f_lim = single_par->f_lim;
00255 frac_max = single_par->crh_frac_max ;
00256 max_iter = single_par->nb_iter ;
00257 xsh_msg_dbg_high( " Params: frac_max %.1f, sigma_lim %.2f f_lim %.2f, iter %d",
00258 single_par->crh_frac_max, sigma_lim, f_lim, max_iter);
00259
00260
00261 check( in_sci_pre = xsh_pre_load( in_sci_frame, instrument));
00262 ron = in_sci_pre->ron;
00263 ron2=ron*ron;
00264 conad = in_sci_pre->conad;
00265 check( sci_pre = xsh_pre_duplicate( in_sci_pre ) ) ;
00266 xsh_pre_free(&in_sci_pre);
00267 check( sci_image = xsh_pre_get_data( sci_pre));
00268 check ( qual = cpl_image_get_data_int( sci_pre->qual));
00269 nx=sci_pre->nx;
00270 ny=sci_pre->ny;
00271
00272 if(wavemap!=NULL) {
00273 check( w_name = cpl_frame_get_filename( wavemap));
00274 check( w_image = cpl_image_load( w_name, CPL_TYPE_DOUBLE,0, 0));
00275 } else {
00276
00277
00278 check( w_image = cpl_image_new(nx,ny,CPL_TYPE_DOUBLE));
00279 cpl_image_add_scalar(w_image,1.);
00280 }
00281
00282 fctx=cpl_image_get_size_x(w_image)/nx;
00283 fcty=cpl_image_get_size_y(w_image)/ny;
00284 if( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR){
00285 fctx = (fctx<1) ? 1 : fctx;
00286 fcty = (fcty<1) ? 1 : fcty;
00287 wmap_xsize_diff=nx-cpl_image_get_size_x(w_image);
00288 }
00289
00290 xsh_msg_dbg_low("fctx=%d fcty=%d",fctx,fcty);
00291 xsh_msg_dbg_low("sci nx=%d ny=%d",nx,ny);
00292
00293 check(w_data=cpl_image_get_data_double(w_image));
00294
00295
00296
00297 xsh_msg_dbg_low("--remove-crh-single : Preparing kernels");
00298
00299 check( laplacian_kernel = cpl_matrix_new(3,3));
00300 cpl_matrix_set( laplacian_kernel,0,0,0.0);
00301 cpl_matrix_set( laplacian_kernel,0,1,-1.0);
00302 cpl_matrix_set( laplacian_kernel,0,2,0.0);
00303 cpl_matrix_set( laplacian_kernel,1,0,-1.0);
00304 cpl_matrix_set( laplacian_kernel,1,1,4.0);
00305 cpl_matrix_set( laplacian_kernel,1,2,-1.0);
00306 cpl_matrix_set( laplacian_kernel,2,0,0.0);
00307 cpl_matrix_set( laplacian_kernel,2,1,-1.0);
00308 cpl_matrix_set( laplacian_kernel,2,2,0.0);
00309 cpl_matrix_divide_scalar( laplacian_kernel, 4.0);
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 check( median3_kernel = cpl_matrix_new(3,3));
00324 for(j=0; j< 3; j++){
00325 for(i=0; i< 3; i++){
00326 cpl_matrix_set( median3_kernel, i,j,1.0);
00327 }
00328 }
00329
00330
00331 check( median5_kernel = cpl_matrix_new(5,5));
00332 for(j=0; j< 5; j++){
00333 for(i=0; i< 5; i++){
00334 cpl_matrix_set( median5_kernel, i,j,1.0);
00335 }
00336 }
00337
00338
00339 check( median7_kernel = cpl_matrix_new(7,7));
00340 for(j=0; j< 7; j++){
00341 for(i=0; i< 7; i++){
00342 cpl_matrix_set( median7_kernel, i,j,1.0);
00343 }
00344 }
00345
00346
00347
00348 check (sci_data = cpl_image_get_data_float( sci_image));
00349
00350 two_sub_sample_nx = nx*2;
00351 two_sub_sample_ny = ny*2;
00352 check( two_sub_sample = cpl_image_new( two_sub_sample_nx,
00353 two_sub_sample_ny, CPL_TYPE_FLOAT));
00354 check (two_sub_sample_data = cpl_image_get_data_float( two_sub_sample));
00355 check( laplacian_redu_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00356 check (laplacian_redu_data = cpl_image_get_data_float(
00357 laplacian_redu_image));
00358 check( noise_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00359 check ( noise_data = cpl_image_get_data_float( noise_image));
00360 check( s_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00361 check ( s_data = cpl_image_get_data_float( s_image));
00362 check( s2_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00363 check ( s2_data = cpl_image_get_data_float( s2_image));
00364 check( f_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00365 check( f_data = cpl_image_get_data_float( f_image));
00366 check( r_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00367
00368 check( r_data = cpl_image_get_data_float( r_image));
00369 check( c_image = cpl_image_new(nx,ny, CPL_TYPE_INT));
00370 check( c_data = cpl_image_get_data_int( c_image));
00371
00372
00373
00374 while( new_crh > 0 && frac < frac_max && nbiter <= max_iter ){
00375 xsh_msg_dbg_low("Iteration %d",nbiter);
00376
00377
00378
00379
00380
00381 xsh_msg_dbg_medium("Create a 2n images");
00382
00383 int j_2_two_sub_sample_nx=0;
00384 int pix=0;
00385 for( j=0; j< ny; j++){
00386 j_2_two_sub_sample_nx=j*2*two_sub_sample_nx;
00387 for( i=0; i< nx; i++){
00388 float val = sci_data[i+j*nx];
00389 pix=i*2+j_2_two_sub_sample_nx;
00390 if ( val < 0. ) val = 0. ;
00391 two_sub_sample_data[pix] = val;
00392 two_sub_sample_data[pix+1] = val;
00393 two_sub_sample_data[pix+two_sub_sample_nx] = val;
00394 two_sub_sample_data[pix+two_sub_sample_nx+1] = val;
00395 }
00396 }
00397 xsh_msg_dbg_medium("Doing laplacian convolution");
00398
00399
00400
00401
00402 check(laplacian_image = xsh_image_filter_linear( two_sub_sample,
00403 laplacian_kernel));
00404
00405
00406
00407 xsh_msg_dbg_medium("Normalize laplacian");
00408 check (laplacian_data = cpl_image_get_data_float( laplacian_image));
00409 for ( i=0; i< two_sub_sample_nx*two_sub_sample_ny; i++){
00410 if (laplacian_data[i] > 0.0){
00411 laplacian_data[i] *= 2.0;
00412 }
00413 else{
00414 laplacian_data[i] = 0.0;
00415 }
00416 }
00417 #if REGDEBUG_FULL
00418 xsh_msg_dbg_medium("Save Lpositive");
00419 cpl_image_save(laplacian_image, "Lpositive.fits", CPL_BPP_IEEE_FLOAT, NULL,
00420 CPL_IO_DEFAULT);
00421 #endif
00422
00423
00424
00425
00426
00427
00428 xsh_msg_dbg_medium("Resample to the original size");
00429
00430 for( j=0; j< ny; j++){
00431 j_2_two_sub_sample_nx=j*2*two_sub_sample_nx;
00432 for( i=0; i< nx; i++){
00433 pix=i*2+j_2_two_sub_sample_nx;
00434 laplacian_redu_data[i+j*nx] =
00435 (laplacian_data[pix]+
00436 laplacian_data[pix+1]+
00437 laplacian_data[pix+two_sub_sample_nx]+
00438 laplacian_data[pix+two_sub_sample_nx+1])*0.25;
00439 }
00440 }
00441 #if REGDEBUG_FULL
00442 cpl_image_save(laplacian_redu_image, "Lplus.fits", CPL_BPP_IEEE_FLOAT,
00443 NULL, CPL_IO_DEFAULT);
00444 #endif
00445 xsh_msg_dbg_medium("Apply median filter");
00446
00447 check( sci_median5_image = xsh_image_filter_median( sci_image,
00448 median5_kernel));
00449 check (sci_median5_data = cpl_image_get_data_float( sci_median5_image));
00450
00451 xsh_msg_dbg_medium("Compute noise");
00452
00453 for( i=0; i< nx*ny; i++){
00454 noise_data[i] = sqrt(sci_median5_data[i]*conad+ron2)/ conad;
00455 }
00456
00457 xsh_msg_dbg_medium("Compute S");
00458
00459 for( i=0; i< nx*ny; i++){
00460 s_data[i] = laplacian_redu_data[i] / (2.0*noise_data[i]);
00461 }
00462
00463 xsh_msg_dbg_medium("Compute S median");
00464
00465 check( s_median_image = xsh_image_filter_median( s_image,
00466 median5_kernel));
00467
00468 check( s_median_data = cpl_image_get_data_float( s_median_image));
00469
00470 xsh_msg_dbg_medium("Compute s2");
00471
00472 for( i=0; i< nx*ny; i++){
00473 s2_data[i] = s_data[i] -s_median_data[i];
00474 }
00475 #if REGDEBUG_FULL
00476 cpl_image_save( s2_image, "S2.fits", CPL_BPP_IEEE_FLOAT, NULL,
00477 CPL_IO_DEFAULT);
00478 #endif
00479 xsh_msg_dbg_medium("Apply 3x3 filter");
00480
00481 check( sci_median3_image = xsh_image_filter_median( sci_image,
00482 median3_kernel));
00483
00484 xsh_msg_dbg_medium("Apply 7x7 filter");
00485
00486 check( sci_median3_7_image = xsh_image_filter_median( sci_median3_image,
00487 median7_kernel));
00488 xsh_msg_dbg_medium("Apply 7x7 filter ok");
00489
00490 check ( sci_median3_data = cpl_image_get_data_float( sci_median3_image));
00491 check ( sci_median3_7_data = cpl_image_get_data_float(
00492 sci_median3_7_image));
00493
00494 xsh_msg_dbg_medium("Compute F");
00495
00496 for( i=0; i< nx*ny; i++){
00497 f_data[i] = sci_median3_data[i] -sci_median3_7_data[i];
00498 if (f_data[i] < 0.01){
00499 f_data[i] = 0.01;
00500 }
00501 }
00502 #if REGDEBUG_FULL
00503 cpl_image_save( f_image, "F.fits", CPL_BPP_IEEE_FLOAT, NULL,
00504 CPL_IO_DEFAULT);
00505 #endif
00506 xsh_msg_dbg_medium("Compute R");
00507
00508 for( i=0; i< nx*ny; i++){
00509 r_data[i] = laplacian_redu_data[i]/f_data[i];
00510 }
00511 #if REGDEBUG_FULL
00512 cpl_image_save( r_image, "R.fits", CPL_BPP_IEEE_FLOAT, NULL,
00513 CPL_IO_DEFAULT);
00514 #endif
00515
00516 xsh_msg_dbg_medium("Search for cosmic");
00517 new_crh = 0;
00518 median = cpl_vector_new(24);
00519
00520 for( j=1; j< ny-1; j++){
00521 double *data = NULL;
00522 cpl_vector* med_vect = NULL;
00523
00524
00525 for( i=1; i< nx-1-wmap_xsize_diff; i++){
00526 if(w_data[i*fctx+j*fcty*(nx-wmap_xsize_diff)] != 0) {
00527 if ( (s2_data[i+j*nx] >= sigma_lim) &&
00528 (r_data[i+j*nx] >= f_lim)){
00529 int li,lj,ui,uj;
00530 c_data[i+j*nx] = 1;
00531 new_crh++;
00532 li = i-2;
00533 lj = j-2;
00534 ui = i+2;
00535 uj = j+2;
00536 m = 0;
00537 if (li < 0) li = 0;
00538 if (ui >= nx) ui = nx-1;
00539 if (lj < 0) lj = 0;
00540 if (uj >= ny) uj = ny-1;
00541 for( k=lj; k <= uj; k++){
00542 for( l=li; l <= ui; l++){
00543 if ( k < j){
00544 cpl_vector_set(median, m, sci_data[l+k*nx]);
00545 m++;
00546 }
00547 else if ( (k == j) && ( l < i)){
00548 cpl_vector_set(median, m, sci_data[l+k*nx]);
00549 m++;
00550 }
00551 else if ( l!=i && k!=j && (s2_data[l+k*nx] < sigma_lim)
00552 && (r_data[l+k*nx] < f_lim)){
00553 cpl_vector_set(median, m, sci_data[l+k*nx]);
00554 m++;
00555 }
00556 }
00557 }
00558 check( data = cpl_vector_get_data( median));
00559
00560 check( med_vect = cpl_vector_wrap( m, data));
00561 check( sci_data[i+j*nx] = cpl_vector_get_median( med_vect));
00562 cpl_vector_unwrap( med_vect);
00563 }
00564 }
00565 }
00566 }
00567 xsh_free_vector( &median ) ;
00568 nb_crh += new_crh;
00569 frac = (double)nb_crh/(double)(nx*ny) ;
00570 xsh_msg_dbg_low(" new cosmics %d, total %d, frac %.4f [%d pixels]",new_crh,nb_crh,
00571 frac, nx*ny);
00572 nbiter++;
00573 xsh_free_image( &laplacian_image);
00574 xsh_free_image( &sci_median3_7_image ) ;
00575 xsh_free_image( &sci_median3_image ) ;
00576 xsh_free_image( &s_median_image ) ;
00577 xsh_free_image( &sci_median5_image ) ;
00578 }
00579 #if REGDEBUG_FULL
00580 cpl_image_save( c_image, "CRH_SINGLE.fits", CPL_BPP_32_SIGNED, NULL,
00581 CPL_IO_DEFAULT);
00582 #endif
00583
00584 if ( xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM){
00585 FILE *debug = NULL;
00586
00587 debug = fopen("cosmic.log","w");
00588
00589 for( j=0; j< ny; j++){
00590 for( i=0; i< nx; i++){
00591 if ( c_data[i+j*nx] == 1){
00592 fprintf(debug,"%.1f %.1f\n",i+1.0,j+1.0);
00593 }
00594 }
00595 }
00596 fclose(debug);
00597 }
00598
00599
00600 for( j=0; j< ny; j++){
00601 for( i=0; i< nx; i++){
00602 if ( c_data[i+j*nx] == 1){
00603 qual[i+j*nx] |= QFLAG_COSMIC_RAY_REMOVED;
00604 }
00605 }
00606 }
00607
00608
00609 res_name=xsh_stringcat_any(res_tag,".fits", NULL) ;
00610 xsh_msg_dbg_high( "Saving Result Frame '%s'", res_name ) ;
00611 check( add_qc_crh( sci_pre, nb_crh, 1) ) ;
00612 check( res_frame = xsh_pre_save( sci_pre, res_name, res_tag,0 ) ) ;
00613 check( cpl_frame_set_tag( res_frame, res_tag ) ) ;
00614 XSH_FREE( res_name);
00615
00616 cleanup:
00617 xsh_pre_free( &sci_pre);
00618
00619
00620 xsh_free_matrix( &laplacian_kernel);
00621 xsh_free_matrix( &median3_kernel);
00622 xsh_free_matrix( &median5_kernel);
00623 xsh_free_matrix( &median7_kernel);
00624
00625 xsh_free_image( &laplacian_image);
00626 xsh_free_image( &laplacian_redu_image);
00627 xsh_free_image( &two_sub_sample);
00628 xsh_free_image( &sci_median5_image);
00629 xsh_free_image( &noise_image);
00630 xsh_free_image( &s_image);
00631 xsh_free_image( &s_median_image);
00632 xsh_free_image( &s2_image);
00633 xsh_free_image( &sci_median3_image);
00634 xsh_free_image( &sci_median3_7_image);
00635 xsh_free_image( &f_image);
00636 xsh_free_image( &r_image);
00637 xsh_free_image( &c_image);
00638 xsh_free_image( &w_image);
00639
00640 xsh_free_vector( &median);
00641
00642 return res_frame;
00643 }