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
00044
00045
00048
00049
00050
00051
00052 #include <xsh_dfs.h>
00053 #include <xsh_utils.h>
00054 #include <xsh_data_pre.h>
00055 #include <xsh_qc_definition.h>
00056 #include <xsh_error.h>
00057 #include <xsh_msg.h>
00058
00059 #include <cpl.h>
00060 #include <string.h>
00061
00062 #include <xsh_badpixelmap.h>
00063 #include <xsh_detmon.h>
00064 #include <xsh_pfits.h>
00065
00066
00067
00068
00069
00070
00071
00072
00073
00080 void xsh_badpixelmap_or( xsh_pre *self, const xsh_pre *right )
00081 {
00082 int * qual0 = NULL,
00083 * qual1 = NULL ;
00084 int i ;
00085
00086 XSH_ASSURE_NOT_NULL( self ) ;
00087 XSH_ASSURE_NOT_NULL( right ) ;
00088 check(qual0 = cpl_image_get_data_int(right->qual));
00089 check(qual1 = cpl_image_get_data_int(self->qual));
00090 XSH_ASSURE_NOT_ILLEGAL(right->nx == self->nx);
00091 XSH_ASSURE_NOT_ILLEGAL(right->ny == self->ny );
00092
00093 for(i=0;i<self->nx*self->ny;i++) {
00094 qual1[i] |= qual0[i] ;
00095 }
00096
00097 cleanup:
00098 return ;
00099 }
00100
00108 cpl_frame*
00109 xsh_badpixelmap_crea_master_from_bpmap(cpl_frame* bpmap, xsh_instrument* inst){
00110
00111 const char* fname=NULL;
00112 cpl_propertylist* plist=NULL;
00113 cpl_image* image=NULL;
00114 char bname[40];
00115 char btag[40];
00116 cpl_frame* master=NULL;
00117 master=cpl_frame_duplicate(bpmap);
00118 fname=cpl_frame_get_filename(bpmap);
00119 plist=cpl_propertylist_load(fname,0);
00120 image=cpl_image_load(fname,CPL_TYPE_INT,0,0);
00121 sprintf(btag,"%s_%s",XSH_MASTER_BP_MAP_FLAT,xsh_instrument_arm_tostring(inst));
00122 sprintf(bname,"%s.fits",btag);
00123 xsh_pfits_set_pcatg(plist,btag);
00124 cpl_image_save(image,bname,XSH_PRE_QUAL_BPP,plist,CPL_IO_DEFAULT);
00125 cpl_frame_set_filename(master,bname);
00126 cpl_frame_set_tag(master,btag);
00127 xsh_add_temporary_file(bname);
00128
00129 xsh_free_propertylist(&plist);
00130 xsh_free_image(&image);
00131 return master;
00132 }
00133
00134
00135 static cpl_error_code xsh_flag_ima_bad_pix(cpl_image** image, int* bppix,const int decode_bp) {
00136 int iy = 0;
00137 int ix = 0;
00138 int nx = 0;
00139 int ny = 0;
00140 int iy_nx = 0;
00141
00142 nx = cpl_image_get_size_x(*image);
00143 ny = cpl_image_get_size_y(*image);
00144
00145
00146 for (iy = 0; iy < ny; iy++) {
00147 iy_nx=iy*nx;
00148 for (ix = 0; ix < nx; ix++) {
00149 int j = ix + iy_nx;
00150 if ((bppix[j] & decode_bp) > 0 ) {
00151 cpl_image_reject(*image, ix + 1, iy + 1);
00152 }
00153 }
00154 }
00155 return cpl_error_get_code();
00156 }
00157
00158
00159
00168 void xsh_set_image_cpl_bpmap(cpl_image * image, cpl_image * bpmap,const int decode_bp) {
00169 int *bppix = NULL;
00170
00171 assure( bpmap != NULL, CPL_ERROR_NULL_INPUT,
00172 "BpMap is NULL pointer" );
00173
00174 check(bppix = cpl_image_get_data_int (bpmap));
00175
00176 xsh_flag_ima_bad_pix(&image,bppix,decode_bp);
00177
00178 cleanup: return;
00179 }
00180
00181 void xsh_image_flag_bp(cpl_image * image,cpl_image * bpmap, xsh_instrument* inst)
00182 {
00183 cpl_mask* mask=NULL;
00184 int nx=0;
00185 int ny=0;
00186 int j_nx=0;
00187 int pix=0;
00188 int i=0;
00189 int j=0;
00190
00191
00192 cpl_binary* pmsk=0;
00193 int* pmap=0;
00194
00195
00196 nx=cpl_image_get_size_x(image);
00197 ny=cpl_image_get_size_y(image);
00198 mask=cpl_mask_new(nx,ny);
00199 pmsk=cpl_mask_get_data(mask);
00200 pmap=cpl_image_get_data_int(bpmap);
00201 for(j=0;j<ny;j++) {
00202 j_nx=j*nx;
00203 for(i=0;i<nx;i++) {
00204 pix=j_nx+i;
00205 if( (inst->decode_bp & pmap[pix]) > 0) {
00206 pmsk[pix]=CPL_BINARY_1;
00207 }
00208 }
00209 }
00210
00211 cpl_image_reject_from_mask(image,mask);
00212
00213 xsh_free_mask(&mask);
00214 return;
00215 }
00216
00217 void xsh_bpmap_bitwise_to_flag(cpl_image * bpmap,int flag )
00218 {
00219 float* data = NULL;
00220 int nx=0;
00221 int ny=0;
00222 int i=0;
00223
00224 check(nx = cpl_image_get_size_x(bpmap));
00225 check(ny = cpl_image_get_size_y(bpmap));
00226
00227 check(data = cpl_image_get_data_float(bpmap));
00228
00229 for(i=0;i<nx*ny;i++){
00230 if(data[i]){
00231 data[i] = flag;
00232 }
00233 }
00234
00235 cleanup:
00236 return;
00237
00238
00239 }
00240
00241
00242 void xsh_bpmap_mask_bad_pixel(cpl_image * bpmap, cpl_mask* mask,
00243 int flag )
00244 {
00245 int* data = NULL;
00246 cpl_mask* old = NULL;
00247 cpl_binary* maskdata = NULL;
00248 int i = 0, nx = 0, ny = 0;
00249
00250
00251 assure(bpmap != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
00252 assure(mask != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
00253
00254
00255 check(nx = cpl_image_get_size_x(bpmap));
00256 check(ny = cpl_image_get_size_y(bpmap));
00257 assure(nx == cpl_mask_get_size_x(mask),CPL_ERROR_ILLEGAL_INPUT,
00258 "mask %" CPL_SIZE_FORMAT " and image %d must have same size in x",
00259 cpl_mask_get_size_x(mask),nx);
00260 assure(ny == cpl_mask_get_size_y(mask),CPL_ERROR_ILLEGAL_INPUT,
00261 "mask %" CPL_SIZE_FORMAT " and image %d must have same size in y",
00262 cpl_mask_get_size_y(mask),ny);
00263
00264
00265 check( old = cpl_image_get_bpm(bpmap));
00266
00267
00268 check(maskdata = cpl_mask_get_data(mask));
00269
00270
00271 check(data = cpl_image_get_data(bpmap));
00272
00273 for(i=0;i<nx*ny;i++){
00274 if(maskdata[i]){
00275 data[i] |= flag;
00276 }
00277 }
00278
00279
00280 check(cpl_mask_or(old,mask));
00281
00282 cleanup:
00283 return;
00284 }
00285
00286 void
00287 xsh_bpmap_set_bad_pixel (cpl_image * bpmap, int ix, int iy, int flag)
00288 {
00289 cpl_image_set (bpmap, ix, iy, (double) flag);
00290 cpl_image_reject (bpmap, ix, iy);
00291 }
00292
00301 cpl_image* xsh_bpmap_collapse_bpmap_create (cpl_imagelist * liste,const int decode_bp)
00302 {
00303
00304
00305
00306 cpl_image *bpmap = NULL;
00307 int* bppix = NULL;
00308 int nx, ny, npix;
00309 int nimg, i;
00310 cpl_image *result = NULL;
00311
00312 xsh_msg( "---> xsh_bpmap_collapse_bpmap_create" ) ;
00313 bpmap = cpl_image_duplicate (cpl_imagelist_get (liste, 0));
00314 assure (bpmap != NULL, cpl_error_get_code (), "Cant duplicate first bpmap");
00315
00316 bppix = cpl_image_get_data_int (bpmap);
00317 assure (bppix != NULL, cpl_error_get_code (), "Cant get data int");
00318
00319 nimg = cpl_imagelist_get_size (liste);
00320
00321 nx = cpl_image_get_size_x (bpmap);
00322 ny = cpl_image_get_size_y (bpmap);
00323 npix = nx * ny;
00324 xsh_msg_dbg_low ("Nb of bpmap: %d, nx: %d, ny: %d [%d]", nimg, nx, ny, npix);
00325
00326 for (i = 1; i < nimg; i++) {
00327 int j = 0;
00328 cpl_image *current = NULL;
00329 int *curpix = NULL;
00330
00331 current = cpl_imagelist_get (liste, i);
00332 assure (current != NULL, cpl_error_get_code (), "Cant get bpmap #%d", i);
00333
00334 curpix = cpl_image_get_data_int (current);
00335 assure (curpix != NULL, cpl_error_get_code (),
00336 "Cant get data int for bpmap #%d", i);
00337
00338 for (j = 0; j < npix; j++)
00339 bppix[j] |= curpix[j];
00340 }
00341
00342 result = cpl_image_wrap_int (nx, ny, bppix);
00343 assure (result != NULL, cpl_error_get_code (), "Cant wrap final bpmap");
00344
00345 xsh_flag_ima_bad_pix(&result,bppix,decode_bp);
00346
00347 return result;
00348
00349 cleanup:
00350 return NULL;
00351 }
00352
00362 int
00363 xsh_bpmap_count (cpl_image * bpmap, int nx, int ny)
00364 {
00365 int *pixbuf;
00366 int i, count = 0, npix = nx * ny;
00367
00368 pixbuf = cpl_image_get_data_int (bpmap);
00369 assure (pixbuf != NULL, cpl_error_get_code (), "Cant get pixel buffer");
00370
00371 for (i = 0; i < npix; i++) {
00372 if (*(pixbuf + i) != 0)
00373 count++;
00374 }
00375
00376 cleanup:
00377 return count;
00378 }
00379
00389 void xsh_bpmap_collapse_median(cpl_image* median, cpl_imagelist * list,
00390 cpl_mask * bpmap)
00391 {
00392 int nx, ny, nimg, i;
00393 cpl_image **pimg = NULL;
00394 cpl_vector *pixvector = NULL;
00395 double* data = NULL;
00396 cpl_binary* mask_data = NULL;
00397 int ix, iy, k;
00398
00399 XSH_ASSURE_NOT_NULL(median);
00400 XSH_ASSURE_NOT_NULL(list);
00401 XSH_ASSURE_NOT_NULL(bpmap);
00402
00403 check(nx = cpl_image_get_size_x(median));
00404 check(ny = cpl_image_get_size_y(median));
00405 check(nimg = cpl_imagelist_get_size(list));
00406
00407
00408 XSH_CALLOC(pimg, cpl_image*, nimg);
00409
00410
00411 for (i = 0; i < nimg; i++){
00412 *(pimg + i) = cpl_imagelist_get (list, i);
00413 assure(cpl_image_get_size_x(*(pimg + i)) == nx,CPL_ERROR_ILLEGAL_INPUT,
00414 " data list x size is not equal to median frame");
00415 assure(cpl_image_get_size_y(*(pimg + i)) == ny,CPL_ERROR_ILLEGAL_INPUT,
00416 " data list y size is not equal to median frame");
00417 }
00418
00419
00420 XSH_MALLOC(data, double, nimg);
00421 check(mask_data = cpl_mask_get_data(bpmap));
00422
00423 for (iy = 1; iy <= ny; iy++) {
00424
00425 for (ix = 1; ix <= nx; ix++) {
00426 int count = 0;
00427
00428 if (mask_data[(iy-1)*nx+ix-1] == CPL_BINARY_0){
00429 for (k = 0; k < nimg; k++) {
00430 int rej;
00431 double val;
00432
00433 check (val = cpl_image_get (pimg[k], ix, iy, &rej) ) ;
00434 if (rej == 0) {
00435 data[count] = val;
00436 count++;
00437 }
00438 }
00439 }
00440
00441 if (count) {
00442 double med = 0.0;
00443
00444 check(pixvector = cpl_vector_wrap(count,data));
00445 check(med = cpl_vector_get_median( pixvector));
00446 check(cpl_image_set(median, ix, iy, med));
00447 check(xsh_unwrap_vector(&pixvector));
00448
00449 }
00450 }
00451 }
00452 cleanup:
00453 if (cpl_error_get_code() != CPL_ERROR_NONE){
00454 xsh_unwrap_vector (&pixvector);
00455 }
00456 XSH_FREE(data);
00457 XSH_FREE(pimg) ;
00458 return;
00459 }
00460
00470 void xsh_bpmap_collapse_mean(cpl_image * mean, cpl_imagelist * list,
00471 cpl_mask * bpmap)
00472 {
00473 int nx, ny, nimg, i;
00474 cpl_image **pimg = NULL;
00475 cpl_vector *pixvector = NULL;
00476 double* data = NULL;
00477 cpl_binary* mask_data = NULL;
00478 int ix, iy, k;
00479
00480 XSH_ASSURE_NOT_NULL(mean);
00481 XSH_ASSURE_NOT_NULL(list);
00482 XSH_ASSURE_NOT_NULL(bpmap);
00483
00484 check(nx = cpl_image_get_size_x(mean));
00485 check(ny = cpl_image_get_size_y(mean));
00486 check(nimg = cpl_imagelist_get_size(list));
00487
00488
00489 XSH_CALLOC(pimg, cpl_image*, nimg);
00490
00491
00492 for (i = 0; i < nimg; i++){
00493 *(pimg + i) = cpl_imagelist_get (list, i);
00494 assure(cpl_image_get_size_x(*(pimg + i)) == nx,CPL_ERROR_ILLEGAL_INPUT,
00495 " data list x size is not equal to median frame");
00496 assure(cpl_image_get_size_y(*(pimg + i)) == ny,CPL_ERROR_ILLEGAL_INPUT,
00497 " data list y size is not equal to median frame");
00498 }
00499
00500
00501 XSH_MALLOC(data, double, nimg);
00502 check(mask_data = cpl_mask_get_data(bpmap));
00503
00504 for (iy = 1; iy <= ny; iy++) {
00505
00506 for (ix = 1; ix <= nx; ix++) {
00507 int count = 0;
00508
00509 if (mask_data[(iy-1)*nx+ix-1] == CPL_BINARY_0){
00510 for (k = 0; k < nimg; k++) {
00511 int rej;
00512 double val;
00513
00514 check (val = cpl_image_get (pimg[k], ix, iy, &rej) ) ;
00515 if (rej == 0) {
00516 data[count] = val;
00517 count++;
00518 }
00519 }
00520 }
00521
00522 if (count) {
00523 double avg = 0.0;
00524
00525 check(pixvector = cpl_vector_wrap(count,data));
00526 check(avg = cpl_vector_get_mean( pixvector));
00527 check(cpl_image_set(mean, ix, iy, avg ));
00528 check(xsh_unwrap_vector(&pixvector));
00529
00530 }
00531 }
00532 }
00533 cleanup:
00534 if (cpl_error_get_code() != CPL_ERROR_NONE){
00535 xsh_unwrap_vector (&pixvector);
00536 }
00537 XSH_FREE(data);
00538 XSH_FREE(pimg) ;
00539 return;
00540 }
00541
00542
00549 static cpl_error_code
00550 xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right )
00551 {
00552 int nx = 0, ny = 0;
00553 int* pself=NULL;
00554 const int* pright=NULL;
00555 int i=0;
00556 int j=0;
00557 int j_nx=0;
00558
00559
00560 check(nx = cpl_image_get_size_x(*self));
00561 check(ny = cpl_image_get_size_y(*self));
00562 assure(nx == cpl_image_get_size_x(right),CPL_ERROR_ILLEGAL_INPUT,
00563 "addendum mask %" CPL_SIZE_FORMAT " and original mask %d must have same size in x",
00564 cpl_image_get_size_x(right),nx);
00565 assure(ny == cpl_image_get_size_y(right),CPL_ERROR_ILLEGAL_INPUT,
00566 "addendum mask %" CPL_SIZE_FORMAT " and original mask %d must have same size in y",
00567 cpl_image_get_size_y(right),ny);
00568
00569
00570 pself=cpl_image_get_data_int(*self);
00571 pright=cpl_image_get_data_int_const(right);
00572 for(j=0;j<ny;j++) {
00573 j_nx=j*nx;
00574 for(i=0;i<nx;i++) {
00575
00576
00577
00578 pself[j_nx+i]|=pright[j_nx+i];
00579
00580
00581 }
00582 }
00583
00584 cleanup:
00585
00586 return cpl_error_get_code();
00587
00588 }
00589
00590
00597 cpl_error_code
00598 xsh_badpixelmap_coadd(cpl_frame *self, const cpl_frame *right )
00599 {
00600
00601
00602 const char* self_name=NULL;
00603 const char* right_name=NULL;
00604 cpl_image* map_self=NULL;
00605 cpl_image* map_right=NULL;
00606 cpl_propertylist* plist=NULL;
00607
00608
00609
00610 assure(self != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
00611 assure(right != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
00612
00613 check(self_name=cpl_frame_get_filename(self));
00614 check(right_name=cpl_frame_get_filename(right));
00615 xsh_msg("self_name=%s",self_name);
00616 check(plist=cpl_propertylist_load(self_name,0));
00617
00618 check(map_self=cpl_image_load(self_name,CPL_TYPE_INT,0,0));
00619 check(map_right=cpl_image_load(right_name,CPL_TYPE_INT,0,0));
00620 xsh_msg("Bit-wise OR of %s with %s frame",
00621 cpl_frame_get_tag(self),cpl_frame_get_tag(right));
00622
00623 check(xsh_badpixelmap_image_coadd(&map_self,map_right));
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633 check(cpl_image_save(map_self,"BP_COMBINE.fits",CPL_BPP_IEEE_FLOAT,
00634 plist,CPL_IO_DEFAULT));
00635 cpl_frame_set_filename(self,"BP_COMBINE.fits");
00636 xsh_add_temporary_file("BP_COMBINE.fits");
00637
00638 cleanup:
00639
00640 xsh_free_propertylist(&plist);
00641 xsh_free_image(&map_self);
00642 xsh_free_image(&map_right);
00643
00644 return cpl_error_get_code();
00645
00646 }
00647
00648
00655 cpl_error_code
00656 xsh_frame_qual_update(cpl_frame *frame, const cpl_frame *bpmap,xsh_instrument* inst)
00657 {
00658 xsh_pre* pre=NULL;
00659 const char* frame_name=NULL;
00660 const char* bpmap_name=NULL;
00661 const char* frame_tag=NULL;
00662
00663 cpl_image* map_self=NULL;
00664 cpl_image* map_right=NULL;
00665 cpl_frame* product=NULL;
00666 assure(frame != NULL, CPL_ERROR_NULL_INPUT,"INPUT frame is NULL pointer" ) ;
00667 assure(bpmap != NULL, CPL_ERROR_NULL_INPUT,"BP MAP frame is NULL pointer" ) ;
00668
00669 check(frame_name=cpl_frame_get_filename(frame));
00670 check(frame_tag=cpl_frame_get_tag(frame));
00671 check(bpmap_name=cpl_frame_get_filename(bpmap));
00672
00673 check(pre=xsh_pre_load(frame,inst));
00674 check(map_right=cpl_image_load(bpmap_name,CPL_TYPE_INT,0,0));
00675 xsh_badpixelmap_image_coadd(&(pre->qual), map_right);
00676 check(product=xsh_pre_save(pre,frame_name,frame_tag,0));
00677
00678 cleanup:
00679 xsh_pre_free(&pre);
00680 xsh_free_image(&map_self);
00681 xsh_free_image(&map_right);
00682 xsh_free_frame(&product);
00683 return cpl_error_get_code();
00684
00685 }
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705 static cpl_error_code
00706 xsh_image_get_hot_cold_maps(cpl_image* masterbias,
00707 const double kappa_low,
00708 const double kappa_high,
00709 const int low_niter,
00710 const int high_niter,
00711 cpl_mask** bpmhot,
00712 int *hotpix_nb,
00713 cpl_mask** bpmcold,
00714 int *coldpix_nb,
00715 cpl_propertylist** hplist,
00716 cpl_propertylist** cplist)
00717 {
00718
00719
00720 double lower=0;
00721 double upper=0;
00722 double dark_med=0;
00723 double stdev=0;
00724 double mean=0;
00725
00726 check(xsh_ksigma_clip(masterbias, 1, 1,
00727 cpl_image_get_size_x(masterbias),
00728 cpl_image_get_size_y(masterbias),
00729 kappa_low,
00730 low_niter, 1e-5,
00731 &mean, &stdev));
00732
00733 check(cpl_propertylist_append_double(*cplist,XSH_QC_MASTER_MEAN,mean));
00734 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
00735
00736 check(cpl_propertylist_append_double(*cplist, XSH_QC_MASTER_RMS,stdev));
00737 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
00738
00739 cpl_image_accept_all(masterbias);
00740
00741
00742 check(dark_med = cpl_image_get_median(masterbias));
00743
00744
00745 lower = dark_med - stdev * kappa_low;
00746
00747 xsh_msg_dbg_low("dark_med=%f lower=%f",dark_med,lower);
00748
00749
00750 check_msg(*bpmcold = cpl_mask_threshold_image_create(masterbias,
00751 -FLT_MAX,lower),
00752 "Cannot compute the cold pixel map");
00753
00754
00755 if(*bpmcold!=NULL) {
00756 check(*coldpix_nb = cpl_mask_count(*bpmcold));
00757 } else {
00758 *coldpix_nb=0;
00759 }
00760 check(cpl_propertylist_append_int(*cplist, XSH_QC_COLD_PIX_NUM,*coldpix_nb));
00761 check(cpl_propertylist_set_comment(*cplist,XSH_QC_COLD_PIX_NUM,XSH_QC_COLD_PIX_NUM_C));
00762
00763
00764
00765 check(xsh_ksigma_clip(masterbias, 1, 1,
00766 cpl_image_get_size_x(masterbias),
00767 cpl_image_get_size_y(masterbias),
00768 kappa_high,
00769 high_niter, 1e-5,
00770 &mean, &stdev));
00771
00772
00773
00774 check(cpl_propertylist_append_double(*hplist, XSH_QC_MASTER_MEAN,mean));
00775 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
00776 check(cpl_propertylist_append_double(*hplist,XSH_QC_MASTER_RMS,stdev));
00777 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
00778
00779 cpl_image_accept_all(masterbias);
00780
00781
00782
00783 check(dark_med = cpl_image_get_median(masterbias));
00784
00785
00786 upper = dark_med + stdev * kappa_high;
00787 xsh_msg_dbg_low("dark_med=%f upper=%f",dark_med,upper);
00788
00789 check_msg(*bpmhot = cpl_mask_threshold_image_create(masterbias,
00790 upper,DBL_MAX),
00791 "Cannot compute the hot pixel map");
00792
00793 if(*bpmhot!=NULL) {
00794 check(*hotpix_nb = cpl_mask_count(*bpmhot));
00795 } else {
00796 *hotpix_nb=0;
00797 }
00798
00799 check(cpl_propertylist_append_int(*hplist,XSH_QC_HOT_PIX_NUM,*hotpix_nb));
00800 check(cpl_propertylist_set_comment(*hplist,XSH_QC_HOT_PIX_NUM,XSH_QC_HOT_PIX_NUM_C));
00801
00802
00803 cleanup:
00804 return cpl_error_get_code();
00805
00806 }
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 cpl_error_code
00820 xsh_image_get_hot_cold_pixs(cpl_frame* frame_image,
00821 xsh_instrument* instrument,
00822 const double ks_low,
00823 const int cold_niter,
00824 const double ks_high,
00825 const int hot_niter,
00826 cpl_frame** cpix_frm,
00827 cpl_frame** hpix_frm)
00828
00829 {
00830
00831
00832 cpl_image* image=NULL;
00833
00834 cpl_mask* bpmcold=NULL;
00835 cpl_mask* bpmhot=NULL;
00836 int hotpix_nb=0;
00837 int coldpix_nb=0;
00838 cpl_image* cpix_ima=NULL;
00839 cpl_image* hpix_ima=NULL;
00840 cpl_propertylist* cplist=NULL;
00841 cpl_propertylist* hplist=NULL;
00842 cpl_propertylist* plist=NULL;
00843
00844
00845 const char* cpix_pro_catg=NULL;
00846 const char* hpix_pro_catg=NULL;
00847 char* cpix_name=NULL;
00848 char* hpix_name=NULL;
00849 const char* name=NULL;
00850
00851 check(name=cpl_frame_get_filename(frame_image));
00852
00853 check(image=cpl_image_load(cpl_frame_get_filename(frame_image),
00854 CPL_TYPE_FLOAT,0,0));
00855
00856
00857 check(hplist=cpl_propertylist_new());
00858 check(cplist=cpl_propertylist_new());
00859 xsh_msg("determine hot and cold pixels");
00860 check(xsh_image_get_hot_cold_maps(image,
00861 ks_low,ks_high,cold_niter,hot_niter,
00862 &bpmhot,&hotpix_nb,
00863 &bpmcold,&coldpix_nb,&hplist,&cplist));
00864
00865
00866 xsh_msg("Hot pix nb=%d Cold pix nb=%d",hotpix_nb,coldpix_nb);
00867
00868
00869 check(cpix_ima=cpl_image_new_from_mask(bpmcold));
00870
00871 check(cpl_image_multiply_scalar(cpix_ima,QFLAG_CAMERA_DEFECT));
00872
00873
00874
00875
00876 check(cpix_name= cpl_sprintf("%s_%s.fits",XSH_BP_MAP_CP,
00877 xsh_instrument_arm_tostring(instrument)));
00878
00879
00880 check(plist=cpl_propertylist_load(name,0));
00881 check(cpl_propertylist_append(plist,cplist));
00882 cpl_image_save(cpix_ima,cpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
00883 xsh_free_propertylist(&plist);
00884 xsh_free_propertylist(&cplist);
00885
00886
00887
00888 check(cpix_pro_catg=XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_CP,instrument));
00889
00890 check(*cpix_frm=xsh_frame_product(cpix_name,cpix_pro_catg,
00891 CPL_FRAME_TYPE_IMAGE,
00892 CPL_FRAME_GROUP_PRODUCT,
00893 CPL_FRAME_LEVEL_FINAL));
00894
00895
00896 check(hpix_ima=cpl_image_new_from_mask(bpmhot));
00897
00898 check(cpl_image_multiply_scalar(hpix_ima,QFLAG_QUESTIONABLE_PIXEL));
00899
00900
00901
00902
00903 check(hpix_name=cpl_sprintf("%s_%s.fits",XSH_BP_MAP_HP,
00904 xsh_instrument_arm_tostring(instrument))) ;
00905
00906 check(plist=cpl_propertylist_load(name,0));
00907 check(cpl_propertylist_append(plist,hplist));
00908 cpl_image_save(hpix_ima,hpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
00909 xsh_free_propertylist(&plist);
00910 xsh_free_propertylist(&hplist);
00911
00912
00913 check(hpix_pro_catg=XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_HP,instrument));
00914 check(*hpix_frm=xsh_frame_product(hpix_name,hpix_pro_catg,
00915 CPL_FRAME_TYPE_IMAGE,
00916 CPL_FRAME_GROUP_PRODUCT,
00917 CPL_FRAME_LEVEL_FINAL));
00918
00919 cleanup:
00920 xsh_free_mask(&bpmcold);
00921 xsh_free_mask(&bpmhot);
00922
00923 xsh_free_image(&image);
00924 xsh_free_image(&cpix_ima);
00925 xsh_free_image(&hpix_ima);
00926 xsh_free_propertylist(&cplist);
00927 xsh_free_propertylist(&hplist);
00928 xsh_free_propertylist(&plist);
00929 XSH_FREE(hpix_name);
00930 XSH_FREE(cpix_name);
00931
00932 return cpl_error_get_code();
00933
00934 }
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944 cpl_frame*
00945 xsh_image_local_cold_pixs(cpl_image* ima,
00946 const double kappa,
00947 const int r,
00948 xsh_instrument* instr)
00949
00950 {
00951
00952 int nx=0;
00953 int ny=0;
00954 int i=0;
00955 int j=0;
00956 double med=0;
00957 double rms=0;
00958 double* pima=NULL;
00959 double* pmsk=NULL;
00960 cpl_image* msk=NULL;
00961 char name[80];
00962 char tag[80];
00963 cpl_frame* msk_frm=NULL;
00964
00965 nx=cpl_image_get_size_x(ima);
00966 ny=cpl_image_get_size_y(ima);
00967 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
00968
00969 pima=cpl_image_get_data_double(ima);
00970 pmsk=cpl_image_get_data_double(msk);
00971
00972 for(j=r;j<ny-r;j++) {
00973 for(i=r;i<nx-r;i++) {
00974 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
00975 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
00976 if(pima[i+j*nx]< med- kappa*rms) {
00977 pmsk[i+j*nx]=QFLAG_LOW_QE_PIXEL;
00978 }
00979 }
00980 }
00981
00982 sprintf(tag,"%s_%s",XSH_BP_MAP_DP,xsh_instrument_arm_tostring(instr));
00983 sprintf(name,"%s.fits",tag);
00984 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,
00985 CPL_IO_DEFAULT));
00986
00987 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
00988 CPL_FRAME_GROUP_PRODUCT,
00989 CPL_FRAME_LEVEL_FINAL));
00990
00991 cleanup:
00992 return msk_frm;
00993
00994 }
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 cpl_frame*
01006 xsh_image_local_hot_pixs(cpl_image* ima,
01007 const double kappa,
01008 const int r,
01009 xsh_instrument* instr)
01010
01011 {
01012
01013 int nx=0;
01014 int ny=0;
01015 int i=0;
01016 int j=0;
01017 double med=0;
01018 double rms=0;
01019 double* pima=NULL;
01020 double* pmsk=NULL;
01021 cpl_image* msk=NULL;
01022 char name[80];
01023 char tag[80];
01024 cpl_frame* msk_frm=NULL;
01025
01026 nx=cpl_image_get_size_x(ima);
01027 ny=cpl_image_get_size_y(ima);
01028 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
01029
01030 pima=cpl_image_get_data_double(ima);
01031 pmsk=cpl_image_get_data_double(msk);
01032
01033 for(j=r;j<ny-r;j++) {
01034 for(i=r;i<nx-r;i++) {
01035 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
01036 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
01037 if(pima[i+j*nx]> med+ kappa*rms) {
01038 pmsk[i+j*nx]=QFLAG_WELL_SATURATION;
01039 }
01040 }
01041 }
01042
01043 sprintf(tag,"%s_%s",XSH_BP_MAP_SP,xsh_instrument_arm_tostring(instr));
01044 sprintf(name,"%s.fits",tag);
01045 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,CPL_IO_DEFAULT));
01046
01047 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
01048 CPL_FRAME_GROUP_PRODUCT,
01049 CPL_FRAME_LEVEL_FINAL));
01050
01051 cleanup:
01052 return msk_frm;
01053
01054 }
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066 cpl_error_code
01067 xsh_image_clean_mask_pixs(cpl_image** ima,cpl_image* msk,const int r)
01068
01069 {
01070 int nx=0;
01071 int ny=0;
01072 int i=0;
01073 int j=0;
01074 double med=0;
01075 double* pima=NULL;
01076 double* pmsk=NULL;
01077
01078 nx=cpl_image_get_size_x(*ima);
01079 ny=cpl_image_get_size_y(*ima);
01080
01081 pima=cpl_image_get_data_double(*ima);
01082 pmsk=cpl_image_get_data_double(msk);
01083
01084 for(j=r;j<ny-r;j++) {
01085 for(i=r;i<nx-r;i++) {
01086 check(med=cpl_image_get_median_window(*ima,i-r+1,j-r+1,i+r,j+r));
01087 if(pmsk[i+j*nx]==1) {
01088 pima[i+j*nx]=med;
01089 }
01090 }
01091 }
01092
01093 cleanup:
01094 return cpl_error_get_code();
01095
01096 }
01097
01098 static cpl_error_code
01099 xsh_image_coadd(cpl_image **self, const cpl_image *add )
01100 {
01101 int nx1=0;
01102 int ny1=0;
01103 int nx2=0;
01104 int ny2=0;
01105 float* pself=NULL;
01106 const float* padd=NULL;
01107 int i=0;
01108
01109 XSH_ASSURE_NOT_NULL( self ) ;
01110 XSH_ASSURE_NOT_NULL( add ) ;
01111
01112 check(nx1=cpl_image_get_size_x(*self));
01113 check(ny1=cpl_image_get_size_y(*self));
01114
01115
01116 check(nx2=cpl_image_get_size_x(add));
01117 check(ny2=cpl_image_get_size_y(add));
01118
01119 pself=cpl_image_get_data_float(*self);
01120 padd=cpl_image_get_data_float_const(add);
01121
01122 if (nx1 != nx2 || ny1 != ny2) {
01123 xsh_msg("Input image of different size");
01124 }
01125 for(i=0;i<nx1*ny2;i++){
01126 if( (pself[i]==0) && (padd[i] !=0) ) {
01127 pself[i]+=padd[i];
01128 }
01129 }
01130 cleanup:
01131 return cpl_error_get_code();
01132
01133 }
01134
01135 cpl_image*
01136 xsh_image_flag_bptype_with_crox(cpl_image* ima)
01137
01138 {
01139
01140
01141 cpl_image* shift=NULL;
01142 cpl_image* res=NULL;
01143
01144
01145 res=cpl_image_duplicate(ima);
01146
01147
01148 shift=cpl_image_duplicate(ima);
01149 cpl_image_shift(shift,1,0);
01150 check(xsh_image_coadd(&res,shift));
01151 xsh_free_image(&shift);
01152
01153
01154 shift=cpl_image_duplicate(ima);
01155 cpl_image_shift(shift,-1,0);
01156 check(xsh_image_coadd(&res,shift));
01157 xsh_free_image(&shift);
01158
01159
01160 shift=cpl_image_duplicate(ima);
01161 cpl_image_shift(shift,0,-1);
01162 check(xsh_image_coadd(&res,shift));
01163 xsh_free_image(&shift);
01164
01165
01166 shift=cpl_image_duplicate(ima);
01167 cpl_image_shift(shift,0,1);
01168 check(xsh_image_coadd(&res,shift));
01169 xsh_free_image(&shift);
01170
01171
01172 cleanup:
01173 return res;
01174
01175 }
01176
01177 cpl_error_code
01178 xsh_badpixelmap_flag_saturated_pixels(xsh_pre* pre,xsh_instrument* instr)
01179 {
01180 int size=pre->nx*pre->ny;
01181 int i=0;
01182 float* pima=NULL;
01183 int* pqual=NULL;
01184 int bp_code_sat=0;
01185 int bp_code_neg=0;
01186 double thresh_max=0;
01187 double time=0;
01188 int ndit=0;
01189 double dit=0;
01190 XSH_ASSURE_NOT_NULL_MSG(pre, "Null input pre frame");
01191 XSH_ASSURE_NOT_NULL_MSG(instr, "Null input pre frame");
01192
01193 if(xsh_instrument_get_arm(instr) != XSH_ARM_NIR) {
01194 thresh_max=65000;
01195 bp_code_sat=QFLAG_ADC_SATURATION;
01196 bp_code_neg=QFLAG_OTHER_BAD_PIXEL;
01197 } else {
01198 dit=xsh_pfits_get_dit (pre->data_header);
01199 ndit=xsh_pfits_get_dit (pre->data_header);
01200 time=dit*ndit;
01201 thresh_max=45000;
01202 if(time<1.2) {
01203 bp_code_sat=QFLAG_ADC_SATURATION;
01204 } else {
01205 bp_code_sat=QFLAG_SATURATED_DATA;
01206 }
01207 bp_code_neg=QFLAG_NEGATIVE_DATA;
01208 }
01209
01210
01211 pima=cpl_image_get_data_float(pre->data);
01212 pqual=cpl_image_get_data_int(pre->qual);
01213
01214 for(i=0;i<size;i++) {
01215 if( pima[i]>thresh_max && (pqual[i] & pre->decode_bp) >0 ) {
01216 pqual[i]=bp_code_sat;
01217 } else if (pima[i]<1 && (pqual[i] & pre->decode_bp) >0 ) {
01218 pqual[i]=bp_code_neg;
01219 }
01220 }
01221
01222 cleanup:
01223 return cpl_error_get_code();
01224 }
01225
01226