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 REGDEBUG_FULL 0
00068
00069
00070
00071
00079 static void add_qc_crh (xsh_pre* pre, int nbcrh, int nframes)
00080 {
00081 double qc_crrate = 0.0;
00082 int nbcr_avg ;
00084 XSH_ASSURE_NOT_NULL(pre);
00085 XSH_ASSURE_NOT_ILLEGAL(pre->pszx >0. && pre->pszy > 0);
00086 XSH_ASSURE_NOT_ILLEGAL(pre->exptime > 0);
00087
00088
00089
00090
00091
00092
00093 xsh_msg_dbg_medium( "add_qc_crh - Exptime = %f", pre->exptime ) ;
00094 qc_crrate =
00095 (double) nbcrh / (((double) nframes) * (pre->exptime *
00096 (pre->pszx / 10000.0) * (pre->pszy /10000.0) * pre->nx * pre->ny)) ;
00097 nbcr_avg = nbcrh/nframes ;
00098
00099
00100
00101
00102 check( xsh_pfits_set_qc_crrate( pre->data_header,qc_crrate) ) ;
00103 check( xsh_pfits_set_qc_ncrh( pre->data_header,nbcrh) ) ;
00104 check( xsh_pfits_set_qc_ncrh_mean( pre->data_header,nbcr_avg) ) ;
00105
00106 check( xsh_pfits_set_qc_crrate( pre->qual_header,qc_crrate) ) ;
00107 check( xsh_pfits_set_qc_ncrh( pre->qual_header,nbcrh) ) ;
00108 check( xsh_pfits_set_qc_ncrh_mean( pre->qual_header,nbcr_avg) ) ;
00109 cleanup:
00110 return ;
00111 }
00112
00113
00114
00115
00116
00117 cpl_frame * xsh_abs_remove_crh_single( cpl_frame *sci_frame,
00118 xsh_instrument * instrument,
00119 xsh_remove_crh_single_param * single_par,
00120 const char * res_tag)
00121 {
00122 cpl_frame *result_frame = NULL;
00123 cpl_frame *abs_frame = NULL;
00124 cpl_frame *sign_frame = NULL;
00125 cpl_frame *rmcrh_frame = NULL;
00126 const char* name = NULL;
00127 cpl_propertylist *header = NULL;
00128 int qc_ncrh =0;
00129
00130 XSH_ASSURE_NOT_NULL( sci_frame);
00131
00132 check( name = cpl_frame_get_filename( sci_frame));
00133 check( header = cpl_propertylist_load( name, 0));
00134 if(cpl_propertylist_has(header,XSH_QC_CRH_NUMBER)) {
00135 check( qc_ncrh = xsh_pfits_get_qc_ncrh( header));
00136 } else {
00137 qc_ncrh =0;
00138 }
00139
00140 if (qc_ncrh > 0){
00141 xsh_msg( "Not use remove crh single");
00142 check( result_frame = cpl_frame_duplicate( sci_frame));
00143 }
00144 else{
00145 check( abs_frame = xsh_frame_abs( sci_frame, instrument, &sign_frame));
00146 check( rmcrh_frame = xsh_remove_crh_single( abs_frame, instrument,
00147 single_par, res_tag));
00148 check( result_frame = xsh_frame_mult( rmcrh_frame, instrument, sign_frame));
00149 }
00150
00151 cleanup:
00152 if (cpl_error_get_code() != CPL_ERROR_NONE){
00153 xsh_free_frame( &result_frame);
00154 }
00155 xsh_free_propertylist( &header);
00156 xsh_free_frame( &abs_frame);
00157 xsh_free_frame( &sign_frame);
00158 xsh_free_frame( &rmcrh_frame);
00159 return result_frame;
00160 }
00161
00162
00177
00178 cpl_frame * xsh_remove_crh_single(cpl_frame * in_sci_frame,
00179 xsh_instrument * instrument, xsh_remove_crh_single_param * single_par,
00180 const char * res_tag) {
00181 int i, j,k,l,m;
00182 double sigma_lim, f_lim;
00183
00184 cpl_frame* res_frame = NULL;
00185 char* res_name = NULL;
00186
00187 cpl_image* laplacian_image = NULL;
00188 cpl_image* laplacian_redu_image = NULL;
00189
00190 cpl_image* two_sub_sample = NULL;
00191 cpl_image* noise_image = NULL;
00192 cpl_image* s_median_image = NULL;
00193 cpl_image* s_image = NULL;
00194 cpl_image* sci_median3_image = NULL;
00195 cpl_image* sci_median3_7_image = NULL;
00196 cpl_image* f_image = NULL;
00197 cpl_image* r_image = NULL;
00198 int two_sub_sample_nx = 0;
00199 int two_sub_sample_ny = 0;
00200 xsh_pre* in_sci_pre = NULL;
00201 xsh_pre* sci_pre = NULL;
00203 cpl_image* sci_image = NULL;
00204 cpl_image* sci_copy = NULL;
00205 cpl_image* err_image = NULL;
00206 cpl_image* qua_image = NULL;
00208
00209 float* sci_data = NULL;
00210 float* err_data = NULL;
00211 int* qua_data = NULL;
00212
00213 float* two_sub_sample_data = NULL;
00214 float* laplacian_data = NULL;
00215 float* laplacian_redu_data = NULL;
00216
00217 float* sci_median3_data = NULL;
00218 float* sci_median3_7_data = NULL;
00219
00220 float* s_data = NULL;
00221 float* s_median_data = NULL;
00222
00223 float* f_data = NULL;
00224 float* r_data = NULL;
00225
00226
00227 cpl_matrix* laplacian_kernel = NULL;
00228 cpl_matrix* median3_kernel = NULL;
00229 cpl_matrix* median5_kernel = NULL;
00230 cpl_matrix* median7_kernel = NULL;
00231 int new_crh = 1, nb_crh = 0;
00232 int nbiter = 1;
00233 int max_iter=1;
00234 cpl_vector* median = NULL;
00235
00236 int nx = 0;
00237 int ny = 0;
00238 int j_nx=0;
00239
00240 XSH_ASSURE_NOT_NULL_MSG( in_sci_frame,"Null input science frame" );
00241 XSH_ASSURE_NOT_NULL_MSG( single_par,"Null input parameters" );
00242 XSH_ASSURE_NOT_NULL_MSG( instrument,"Null instrument setting" );
00243
00244 sigma_lim = single_par->sigma_lim;
00245 f_lim = single_par->f_lim;
00246 max_iter = single_par->nb_iter;
00247 xsh_msg_dbg_high(
00248 " Params: sigma_lim %.2f f_lim %.2f, iter %d", sigma_lim, f_lim, max_iter);
00249
00250 check( in_sci_pre = xsh_pre_load( in_sci_frame, instrument));
00251 check( sci_pre = xsh_pre_duplicate( in_sci_pre ) );
00252 xsh_pre_free(&in_sci_pre);
00253 check( sci_image = xsh_pre_get_data( sci_pre));
00254 check( sci_copy = cpl_image_duplicate( sci_image));
00255 check( err_image = xsh_pre_get_errs( sci_pre));
00256 check( qua_image = xsh_pre_get_qual( sci_pre));
00257 nx = sci_pre->nx;
00258 ny = sci_pre->ny;
00259
00260
00261
00262 xsh_msg_dbg_low("--remove-crh-single : Preparing kernels");
00263
00264 check( laplacian_kernel = cpl_matrix_new(3,3));
00265 cpl_matrix_set(laplacian_kernel, 0, 0, 0.0);
00266 cpl_matrix_set(laplacian_kernel, 0, 1, -1.0);
00267 cpl_matrix_set(laplacian_kernel, 0, 2, 0.0);
00268 cpl_matrix_set(laplacian_kernel, 1, 0, -1.0);
00269 cpl_matrix_set(laplacian_kernel, 1, 1, 4.0);
00270 cpl_matrix_set(laplacian_kernel, 1, 2, -1.0);
00271 cpl_matrix_set(laplacian_kernel, 2, 0, 0.0);
00272 cpl_matrix_set(laplacian_kernel, 2, 1, -1.0);
00273 cpl_matrix_set(laplacian_kernel, 2, 2, 0.0);
00274
00275
00276 check( median3_kernel = cpl_matrix_new(3,3));
00277 for (j = 0; j < 3; j++) {
00278 for (i = 0; i < 3; i++) {
00279 cpl_matrix_set(median3_kernel, i, j, 1.0);
00280 }
00281 }
00282
00283
00284 check( median5_kernel = cpl_matrix_new(5,5));
00285 for (j = 0; j < 5; j++) {
00286 for (i = 0; i < 5; i++) {
00287 cpl_matrix_set(median5_kernel, i, j, 1.0);
00288 }
00289 }
00290
00291
00292 check( median7_kernel = cpl_matrix_new(7,7));
00293 for (j = 0; j < 7; j++) {
00294 for (i = 0; i < 7; i++) {
00295 cpl_matrix_set(median7_kernel, i, j, 1.0);
00296 }
00297 }
00298
00299
00300 check(sci_data = cpl_image_get_data_float( sci_copy));
00301 check(err_data = cpl_image_get_data_float( err_image));
00302 check(qua_data = cpl_image_get_data_int( qua_image));
00303
00304 two_sub_sample_nx = nx * 2;
00305 two_sub_sample_ny = ny * 2;
00306 check(
00307 two_sub_sample = cpl_image_new( two_sub_sample_nx, two_sub_sample_ny, CPL_TYPE_FLOAT));
00308 check(two_sub_sample_data = cpl_image_get_data_float( two_sub_sample));
00309 check( laplacian_redu_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00310 check(laplacian_redu_data = cpl_image_get_data_float( laplacian_redu_image));
00311 check( noise_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00312
00313 check( s_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00314 check( s_data = cpl_image_get_data_float( s_image));
00315
00316
00317 check( f_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00318 check( f_data = cpl_image_get_data_float( f_image));
00319 check( r_image = cpl_image_new(nx,ny, CPL_TYPE_FLOAT));
00320
00321 check( r_data = cpl_image_get_data_float( r_image));
00322
00323
00324 while (new_crh > 0 && nbiter <= max_iter) {
00325 xsh_msg_dbg_low("Iteration %d", nbiter);
00326
00327
00328
00329
00330
00331
00332
00333
00334 xsh_msg_dbg_medium("Create super-sampled image");
00335 int j_2_two_sub_sample_nx = 0;
00336 int pix = 0;
00337 for (j = 0; j < ny; j++) {
00338 j_2_two_sub_sample_nx = j * 2 * two_sub_sample_nx;
00339 j_nx = j*nx;
00340 for (i = 0; i < nx; i++) {
00341 float val = sci_data[i + j_nx];
00342 pix = i * 2 + j_2_two_sub_sample_nx;
00343 two_sub_sample_data[pix] = val;
00344 two_sub_sample_data[pix + two_sub_sample_nx] = val;
00345 pix++;
00346 two_sub_sample_data[pix] = val;
00347 two_sub_sample_data[pix + two_sub_sample_nx] = val;
00348 }
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358 xsh_msg_dbg_medium("Doing Laplacian convolution");
00359 check(
00360 laplacian_image = xsh_image_filter_linear( two_sub_sample, laplacian_kernel));
00361
00362 check(laplacian_data = cpl_image_get_data_float( laplacian_image));
00363 for (i = 0; i < two_sub_sample_nx * two_sub_sample_ny; i++) {
00364 if (laplacian_data[i] < 0.0) {
00365 laplacian_data[i] = 0.0;
00366 } else {
00367 laplacian_data[i] *= 8.0;
00368 }
00369 }
00370
00371
00372 #if REGDEBUG_FULL
00373 xsh_msg_dbg_medium("Save Lpositive");
00374 cpl_image_save(laplacian_image, "Lpositive.fits", CPL_BPP_IEEE_FLOAT, NULL,
00375 CPL_IO_DEFAULT);
00376 #endif
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 xsh_msg_dbg_medium("Resample to the original size");
00387 for (j = 0; j < ny; j++) {
00388 j_2_two_sub_sample_nx = j * 2 * two_sub_sample_nx;
00389 j_nx = j*nx;
00390 for (i = 0; i < nx; i++) {
00391 pix = i * 2 + j_2_two_sub_sample_nx;
00392 laplacian_redu_data[i + j_nx] = (laplacian_data[pix]
00393 + laplacian_data[pix + 1] + laplacian_data[pix + two_sub_sample_nx]
00394 + laplacian_data[pix + two_sub_sample_nx + 1]) * 0.25;
00395 }
00396 }
00397
00398 #if REGDEBUG_FULL
00399 cpl_image_save(laplacian_redu_image, "Lplus.fits", CPL_BPP_IEEE_FLOAT,
00400 NULL, CPL_IO_DEFAULT);
00401 #endif
00402
00403
00404
00405 xsh_msg_dbg_medium("Compute S");
00406 for (i = 0; i < nx * ny; i++) {
00407 s_data[i] = 0.5 * laplacian_redu_data[i] / err_data[i];
00408 }
00409
00410
00411 xsh_msg_dbg_medium("Compute S median");
00412 check( s_median_image = xsh_image_filter_median( s_image, median5_kernel));
00413 check( s_median_data = cpl_image_get_data_float( s_median_image));
00414
00415
00416
00417 xsh_msg_dbg_medium("Compute s2");
00418 for (i = 0; i < nx * ny; i++) {
00419 s_data[i] -= s_median_data[i];
00420 }
00421 #if REGDEBUG_FULL
00422 cpl_image_save( s_image, "S2.fits", CPL_BPP_IEEE_FLOAT, NULL,
00423 CPL_IO_DEFAULT);
00424 #endif
00425
00426
00427
00428 xsh_msg_dbg_medium("Apply 3x3 filter");
00429 check(sci_median3_image = xsh_image_filter_median( sci_copy, median3_kernel));
00430 check( sci_median3_data = cpl_image_get_data_float( sci_median3_image));
00431
00432
00433 xsh_msg_dbg_medium("Apply 7x7 filter");
00434 check(
00435 sci_median3_7_image = xsh_image_filter_median( sci_median3_image, median7_kernel));
00436 check( sci_median3_7_data = cpl_image_get_data_float( sci_median3_7_image));
00437
00438
00439
00440
00441 xsh_msg_dbg_medium("Compute F");
00442 for (i = 0; i < nx * ny; i++) {
00443 f_data[i] = sci_median3_data[i] - sci_median3_7_data[i];
00444 if (f_data[i] < 0.01) {
00445 f_data[i] = 0.01;
00446 }
00447 }
00448 #if REGDEBUG_FULL
00449 cpl_image_save( f_image, "F.fits", CPL_BPP_IEEE_FLOAT, NULL,
00450 CPL_IO_DEFAULT);
00451 #endif
00452
00453
00454 xsh_msg_dbg_medium("Compute R");
00455 for (i = 0; i < nx * ny; i++) {
00456 r_data[i] = laplacian_redu_data[i] / f_data[i];
00457 }
00458 #if REGDEBUG_FULL
00459 cpl_image_save( r_image, "R.fits", CPL_BPP_IEEE_FLOAT, NULL,
00460 CPL_IO_DEFAULT);
00461 #endif
00462
00463
00464
00465 xsh_msg_dbg_medium("Search for cosmic ray hits");
00466 new_crh = 0;
00467 median = cpl_vector_new(24);
00468 for (j = 0; j < ny - 1; j++) {
00469
00470 double *data = NULL;
00471 cpl_vector* med_vect = NULL;
00472
00473 j_nx = j*nx;
00474 for (i = 0; i < nx - 1 ; i++) {
00475 int i_plus_j_nx = i + j_nx;
00476 if ( s_data[i_plus_j_nx] > sigma_lim ) {
00477 if ( r_data[i_plus_j_nx] > f_lim ) {
00478
00479
00480 qua_data[i + j_nx] |= QFLAG_COSMIC_RAY_UNREMOVED;
00481 new_crh++;
00482
00483
00484
00485 int li, lj, ui, uj;
00486 li = i - 2;
00487 lj = j - 2;
00488 ui = i + 2;
00489 uj = j + 2;
00490 m = 0;
00491
00492 if (li < 0) { li = 0; }
00493 if (ui >= nx) { ui = nx - 1; }
00494 if (lj < 0) { lj = 0; }
00495 if (uj >= ny) { uj = ny - 1;}
00496
00497 for (k = lj; k <= uj; k++) {
00498 int k_nx = k*nx;
00499 for (l = li; l <= ui; l++) {
00500 int l_plus_k_nx = l + k_nx;
00501 if (s_data[l_plus_k_nx] <= sigma_lim) {
00502 cpl_vector_set(median, m, sci_data[l_plus_k_nx]);
00503 m++;
00504 continue;
00505 }
00506 if (r_data[l_plus_k_nx] <= f_lim) {
00507 cpl_vector_set(median, m, sci_data[l_plus_k_nx]);
00508 m++;
00509 }
00510 }
00511 }
00512
00513
00514 if(m==0) continue;
00515
00516
00517 check( data = cpl_vector_get_data( median));
00518 check( med_vect = cpl_vector_wrap( m, data));
00519 check( sci_data[i_plus_j_nx] = cpl_vector_get_median(med_vect));
00520 cpl_vector_unwrap(med_vect);
00521
00522
00523 }
00524 }
00525 }
00526 }
00527
00528 xsh_free_vector(&median);
00529
00530 nb_crh += new_crh;
00531 xsh_msg_dbg_low(
00532 " new cosmics %d, total %d [%d pixels]", new_crh, nb_crh, nx*ny);
00533 nbiter++;
00534 xsh_free_image(&laplacian_image);
00535 xsh_free_image(&sci_median3_7_image);
00536 xsh_free_image(&sci_median3_image);
00537 xsh_free_image(&s_median_image);
00538 }
00539 #if REGDEBUG_FULL
00540 cpl_image_save( qua_image, "CRH_SINGLE.fits", CPL_BPP_32_SIGNED, NULL,
00541 CPL_IO_DEFAULT);
00542 #endif
00543
00544
00545
00546 res_name = xsh_stringcat_any(res_tag, ".fits", NULL);
00547 xsh_msg_dbg_high( "Saving Result Frame '%s'", res_name);
00548 check( add_qc_crh( sci_pre, nb_crh, 1) );
00549 check( res_frame = xsh_pre_save( sci_pre, res_name, res_tag,0 ) );
00550 check( cpl_frame_set_tag( res_frame, res_tag ) );
00551 XSH_FREE( res_name);
00552
00553 cleanup:
00554 xsh_pre_free(&sci_pre);
00555
00556
00557 xsh_free_matrix(&laplacian_kernel);
00558 xsh_free_matrix(&median3_kernel);
00559 xsh_free_matrix(&median5_kernel);
00560 xsh_free_matrix(&median7_kernel);
00561
00562 xsh_free_image(&laplacian_image);
00563 xsh_free_image(&laplacian_redu_image);
00564 xsh_free_image(&two_sub_sample);
00565 xsh_free_image(&noise_image);
00566 xsh_free_image(&s_median_image);
00567 xsh_free_image(&s_image);
00568 xsh_free_image(&sci_median3_image);
00569 xsh_free_image(&sci_median3_7_image);
00570 xsh_free_image(&sci_copy);
00571 xsh_free_image(&f_image);
00572 xsh_free_image(&r_image);
00573
00574 return res_frame;
00575 }
00576
00577