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
00550 cpl_error_code
00551 xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right, const int mode )
00552 {
00553 int nx = 0, ny = 0;
00554 int* pself=NULL;
00555 const int* pright=NULL;
00556 int i=0;
00557 int j=0;
00558 int j_nx=0;
00559
00560
00561 check(nx = cpl_image_get_size_x(*self));
00562 check(ny = cpl_image_get_size_y(*self));
00563 assure(nx == cpl_image_get_size_x(right),CPL_ERROR_ILLEGAL_INPUT,
00564 "addendum mask %" CPL_SIZE_FORMAT " and original mask %d must have same size in x",
00565 cpl_image_get_size_x(right),nx);
00566 assure(ny == cpl_image_get_size_y(right),CPL_ERROR_ILLEGAL_INPUT,
00567 "addendum mask %" CPL_SIZE_FORMAT " and original mask %d must have same size in y",
00568 cpl_image_get_size_y(right),ny);
00569
00570
00571 pself=cpl_image_get_data_int(*self);
00572 pright=cpl_image_get_data_int_const(right);
00573 if(mode) {
00574 xsh_msg("Bitwise OR combine");
00575 for(j=0;j<ny;j++) {
00576 j_nx=j*nx;
00577 for(i=0;i<nx;i++) {
00578
00579 pself[j_nx+i]|=pright[j_nx+i];
00580
00581 }
00582 }
00583 } else {
00584 xsh_msg("Bitwise AND combine");
00585 for(j=0;j<ny;j++) {
00586 j_nx=j*nx;
00587 for(i=0;i<nx;i++) {
00588
00589 pself[j_nx+i]&=pright[j_nx+i];
00590 }
00591 }
00592 }
00593 cleanup:
00594
00595 return cpl_error_get_code();
00596
00597 }
00598
00599
00600
00601
00608 cpl_error_code
00609 xsh_badpixelmap_coadd(cpl_frame *self, const cpl_frame *right,const int mode )
00610 {
00611
00612
00613 const char* self_name=NULL;
00614 const char* right_name=NULL;
00615 cpl_image* map_self=NULL;
00616 cpl_image* map_right=NULL;
00617 cpl_propertylist* plist=NULL;
00618
00619
00620
00621 assure(self != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
00622 assure(right != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
00623
00624 check(self_name=cpl_frame_get_filename(self));
00625 check(right_name=cpl_frame_get_filename(right));
00626
00627 check(plist=cpl_propertylist_load(self_name,0));
00628
00629 check(map_self=cpl_image_load(self_name,CPL_TYPE_INT,0,0));
00630 check(map_right=cpl_image_load(right_name,CPL_TYPE_INT,0,0));
00631 xsh_msg("Bit-wise OR of %s with %s frame",
00632 cpl_frame_get_tag(self),cpl_frame_get_tag(right));
00633
00634 check(xsh_badpixelmap_image_coadd(&map_self,map_right,mode));
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 check(cpl_image_save(map_self,"BP_COMBINE.fits",CPL_BPP_IEEE_FLOAT,
00645 plist,CPL_IO_DEFAULT));
00646 cpl_frame_set_filename(self,"BP_COMBINE.fits");
00647 xsh_add_temporary_file("BP_COMBINE.fits");
00648
00649 cleanup:
00650
00651 xsh_free_propertylist(&plist);
00652 xsh_free_image(&map_self);
00653 xsh_free_image(&map_right);
00654
00655 return cpl_error_get_code();
00656
00657 }
00658
00659
00666 cpl_error_code
00667 xsh_frame_qual_update(cpl_frame *frame, const cpl_frame *bpmap,xsh_instrument* inst)
00668 {
00669 xsh_pre* pre=NULL;
00670 const char* frame_name=NULL;
00671 const char* bpmap_name=NULL;
00672 const char* frame_tag=NULL;
00673
00674 cpl_image* map_self=NULL;
00675 cpl_image* map_right=NULL;
00676 cpl_frame* product=NULL;
00677 const int mode_or=1;
00678 assure(frame != NULL, CPL_ERROR_NULL_INPUT,"INPUT frame is NULL pointer" ) ;
00679 assure(bpmap != NULL, CPL_ERROR_NULL_INPUT,"BP MAP frame is NULL pointer" ) ;
00680
00681 check(frame_name=cpl_frame_get_filename(frame));
00682 check(frame_tag=cpl_frame_get_tag(frame));
00683 check(bpmap_name=cpl_frame_get_filename(bpmap));
00684
00685 check(pre=xsh_pre_load(frame,inst));
00686 check(map_right=cpl_image_load(bpmap_name,CPL_TYPE_INT,0,0));
00687 xsh_badpixelmap_image_coadd(&(pre->qual), map_right,mode_or);
00688 check(product=xsh_pre_save(pre,frame_name,frame_tag,0));
00689
00690 cleanup:
00691 xsh_pre_free(&pre);
00692 xsh_free_image(&map_self);
00693 xsh_free_image(&map_right);
00694 xsh_free_frame(&product);
00695 return cpl_error_get_code();
00696
00697 }
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 static cpl_error_code
00718 xsh_image_get_hot_cold_maps(cpl_image* masterbias,
00719 const double kappa_low,
00720 const double kappa_high,
00721 const int low_niter,
00722 const int high_niter,
00723 cpl_mask** bpmhot,
00724 int *hotpix_nb,
00725 cpl_mask** bpmcold,
00726 int *coldpix_nb,
00727 cpl_propertylist** hplist,
00728 cpl_propertylist** cplist)
00729 {
00730
00731
00732 double lower=0;
00733 double upper=0;
00734 double dark_med=0;
00735 double stdev=0;
00736 double mean=0;
00737
00738 check(xsh_ksigma_clip(masterbias, 1, 1,
00739 cpl_image_get_size_x(masterbias),
00740 cpl_image_get_size_y(masterbias),
00741 kappa_low,
00742 low_niter, 1e-5,
00743 &mean, &stdev));
00744
00745 check(cpl_propertylist_append_double(*cplist,XSH_QC_MASTER_MEAN,mean));
00746 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
00747
00748 check(cpl_propertylist_append_double(*cplist, XSH_QC_MASTER_RMS,stdev));
00749 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
00750
00751 cpl_image_accept_all(masterbias);
00752
00753
00754 check(dark_med = cpl_image_get_median(masterbias));
00755
00756
00757 lower = dark_med - stdev * kappa_low;
00758
00759 xsh_msg_dbg_low("dark_med=%f lower=%f",dark_med,lower);
00760
00761
00762 check_msg(*bpmcold = cpl_mask_threshold_image_create(masterbias,
00763 -FLT_MAX,lower),
00764 "Cannot compute the cold pixel map");
00765
00766
00767 if(*bpmcold!=NULL) {
00768 check(*coldpix_nb = cpl_mask_count(*bpmcold));
00769 } else {
00770 *coldpix_nb=0;
00771 }
00772 check(cpl_propertylist_append_int(*cplist, XSH_QC_COLD_PIX_NUM,*coldpix_nb));
00773 check(cpl_propertylist_set_comment(*cplist,XSH_QC_COLD_PIX_NUM,XSH_QC_COLD_PIX_NUM_C));
00774
00775
00776
00777 check(xsh_ksigma_clip(masterbias, 1, 1,
00778 cpl_image_get_size_x(masterbias),
00779 cpl_image_get_size_y(masterbias),
00780 kappa_high,
00781 high_niter, 1e-5,
00782 &mean, &stdev));
00783
00784
00785
00786 check(cpl_propertylist_append_double(*hplist, XSH_QC_MASTER_MEAN,mean));
00787 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
00788 check(cpl_propertylist_append_double(*hplist,XSH_QC_MASTER_RMS,stdev));
00789 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
00790
00791 cpl_image_accept_all(masterbias);
00792
00793
00794
00795 check(dark_med = cpl_image_get_median(masterbias));
00796
00797
00798 upper = dark_med + stdev * kappa_high;
00799 xsh_msg_dbg_low("dark_med=%f upper=%f",dark_med,upper);
00800
00801 check_msg(*bpmhot = cpl_mask_threshold_image_create(masterbias,
00802 upper,DBL_MAX),
00803 "Cannot compute the hot pixel map");
00804
00805 if(*bpmhot!=NULL) {
00806 check(*hotpix_nb = cpl_mask_count(*bpmhot));
00807 } else {
00808 *hotpix_nb=0;
00809 }
00810
00811 check(cpl_propertylist_append_int(*hplist,XSH_QC_HOT_PIX_NUM,*hotpix_nb));
00812 check(cpl_propertylist_set_comment(*hplist,XSH_QC_HOT_PIX_NUM,XSH_QC_HOT_PIX_NUM_C));
00813
00814
00815 cleanup:
00816 return cpl_error_get_code();
00817
00818 }
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831 cpl_error_code
00832 xsh_image_get_hot_cold_pixs(cpl_frame* frame_image,
00833 xsh_instrument* instrument,
00834 const double ks_low,
00835 const int cold_niter,
00836 const double ks_high,
00837 const int hot_niter,
00838 cpl_frame** cpix_frm,
00839 cpl_frame** hpix_frm)
00840
00841 {
00842
00843
00844 cpl_image* image=NULL;
00845
00846 cpl_mask* bpmcold=NULL;
00847 cpl_mask* bpmhot=NULL;
00848 int hotpix_nb=0;
00849 int coldpix_nb=0;
00850 cpl_image* cpix_ima=NULL;
00851 cpl_image* hpix_ima=NULL;
00852 cpl_propertylist* cplist=NULL;
00853 cpl_propertylist* hplist=NULL;
00854 cpl_propertylist* plist=NULL;
00855
00856
00857 const char* cpix_pro_catg=NULL;
00858 const char* hpix_pro_catg=NULL;
00859 char* cpix_name=NULL;
00860 char* hpix_name=NULL;
00861 const char* name=NULL;
00862
00863 check(name=cpl_frame_get_filename(frame_image));
00864
00865 check(image=cpl_image_load(cpl_frame_get_filename(frame_image),
00866 CPL_TYPE_FLOAT,0,0));
00867
00868
00869 check(hplist=cpl_propertylist_new());
00870 check(cplist=cpl_propertylist_new());
00871 xsh_msg("determine hot and cold pixels");
00872 check(xsh_image_get_hot_cold_maps(image,
00873 ks_low,ks_high,cold_niter,hot_niter,
00874 &bpmhot,&hotpix_nb,
00875 &bpmcold,&coldpix_nb,&hplist,&cplist));
00876
00877
00878 xsh_msg("Hot pix nb=%d Cold pix nb=%d",hotpix_nb,coldpix_nb);
00879
00880
00881 check(cpix_ima=cpl_image_new_from_mask(bpmcold));
00882
00883 check(cpl_image_multiply_scalar(cpix_ima,QFLAG_CAMERA_DEFECT));
00884
00885
00886
00887
00888 check(cpix_name= cpl_sprintf("%s_%s.fits",XSH_BP_MAP_CP,
00889 xsh_instrument_arm_tostring(instrument)));
00890
00891
00892 check(plist=cpl_propertylist_load(name,0));
00893 check(cpl_propertylist_append(plist,cplist));
00894 cpl_image_save(cpix_ima,cpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
00895 xsh_free_propertylist(&plist);
00896 xsh_free_propertylist(&cplist);
00897
00898
00899
00900 check(cpix_pro_catg=XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_CP,instrument));
00901
00902 check(*cpix_frm=xsh_frame_product(cpix_name,cpix_pro_catg,
00903 CPL_FRAME_TYPE_IMAGE,
00904 CPL_FRAME_GROUP_PRODUCT,
00905 CPL_FRAME_LEVEL_FINAL));
00906
00907
00908 check(hpix_ima=cpl_image_new_from_mask(bpmhot));
00909
00910 check(cpl_image_multiply_scalar(hpix_ima,QFLAG_QUESTIONABLE_PIXEL));
00911
00912
00913
00914
00915 check(hpix_name=cpl_sprintf("%s_%s.fits",XSH_BP_MAP_HP,
00916 xsh_instrument_arm_tostring(instrument))) ;
00917
00918 check(plist=cpl_propertylist_load(name,0));
00919 check(cpl_propertylist_append(plist,hplist));
00920 cpl_image_save(hpix_ima,hpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
00921 xsh_free_propertylist(&plist);
00922 xsh_free_propertylist(&hplist);
00923
00924
00925 check(hpix_pro_catg=XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_HP,instrument));
00926 check(*hpix_frm=xsh_frame_product(hpix_name,hpix_pro_catg,
00927 CPL_FRAME_TYPE_IMAGE,
00928 CPL_FRAME_GROUP_PRODUCT,
00929 CPL_FRAME_LEVEL_FINAL));
00930
00931 cleanup:
00932 xsh_free_mask(&bpmcold);
00933 xsh_free_mask(&bpmhot);
00934
00935 xsh_free_image(&image);
00936 xsh_free_image(&cpix_ima);
00937 xsh_free_image(&hpix_ima);
00938 xsh_free_propertylist(&cplist);
00939 xsh_free_propertylist(&hplist);
00940 xsh_free_propertylist(&plist);
00941 XSH_FREE(hpix_name);
00942 XSH_FREE(cpix_name);
00943
00944 return cpl_error_get_code();
00945
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956 cpl_frame*
00957 xsh_image_local_cold_pixs(cpl_image* ima,
00958 const double kappa,
00959 const int r,
00960 xsh_instrument* instr)
00961
00962 {
00963
00964 int nx=0;
00965 int ny=0;
00966 int i=0;
00967 int j=0;
00968 double med=0;
00969 double rms=0;
00970 double* pima=NULL;
00971 double* pmsk=NULL;
00972 cpl_image* msk=NULL;
00973 char name[256];
00974 char tag[256];
00975 cpl_frame* msk_frm=NULL;
00976
00977 nx=cpl_image_get_size_x(ima);
00978 ny=cpl_image_get_size_y(ima);
00979 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
00980
00981 pima=cpl_image_get_data_double(ima);
00982 pmsk=cpl_image_get_data_double(msk);
00983
00984 for(j=r;j<ny-r;j++) {
00985 for(i=r;i<nx-r;i++) {
00986 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
00987 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
00988 if(pima[i+j*nx]< med- kappa*rms) {
00989 pmsk[i+j*nx]=QFLAG_LOW_QE_PIXEL;
00990 }
00991 }
00992 }
00993
00994 sprintf(tag,"%s_%s",XSH_BP_MAP_DP,xsh_instrument_arm_tostring(instr));
00995 sprintf(name,"%s.fits",tag);
00996 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,
00997 CPL_IO_DEFAULT));
00998
00999 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
01000 CPL_FRAME_GROUP_PRODUCT,
01001 CPL_FRAME_LEVEL_FINAL));
01002
01003 cleanup:
01004 return msk_frm;
01005
01006 }
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 cpl_frame*
01018 xsh_image_local_hot_pixs(cpl_image* ima,
01019 const double kappa,
01020 const int r,
01021 xsh_instrument* instr)
01022
01023 {
01024
01025 int nx=0;
01026 int ny=0;
01027 int i=0;
01028 int j=0;
01029 double med=0;
01030 double rms=0;
01031 double* pima=NULL;
01032 double* pmsk=NULL;
01033 cpl_image* msk=NULL;
01034 char name[256];
01035 char tag[256];
01036 cpl_frame* msk_frm=NULL;
01037
01038 nx=cpl_image_get_size_x(ima);
01039 ny=cpl_image_get_size_y(ima);
01040 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
01041
01042 pima=cpl_image_get_data_double(ima);
01043 pmsk=cpl_image_get_data_double(msk);
01044
01045 for(j=r;j<ny-r;j++) {
01046 for(i=r;i<nx-r;i++) {
01047 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
01048 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
01049 if(pima[i+j*nx]> med+ kappa*rms) {
01050 pmsk[i+j*nx]=QFLAG_WELL_SATURATION;
01051 }
01052 }
01053 }
01054
01055 sprintf(tag,"%s_%s",XSH_BP_MAP_SP,xsh_instrument_arm_tostring(instr));
01056 sprintf(name,"%s.fits",tag);
01057 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,CPL_IO_DEFAULT));
01058
01059 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
01060 CPL_FRAME_GROUP_PRODUCT,
01061 CPL_FRAME_LEVEL_FINAL));
01062
01063 cleanup:
01064 return msk_frm;
01065
01066 }
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078 cpl_error_code
01079 xsh_image_clean_mask_pixs(cpl_image** ima,cpl_image* msk,const int r)
01080
01081 {
01082 int nx=0;
01083 int ny=0;
01084 int i=0;
01085 int j=0;
01086 double med=0;
01087 double* pima=NULL;
01088 double* pmsk=NULL;
01089
01090 nx=cpl_image_get_size_x(*ima);
01091 ny=cpl_image_get_size_y(*ima);
01092
01093 pima=cpl_image_get_data_double(*ima);
01094 pmsk=cpl_image_get_data_double(msk);
01095
01096 for(j=r;j<ny-r;j++) {
01097 for(i=r;i<nx-r;i++) {
01098 check(med=cpl_image_get_median_window(*ima,i-r+1,j-r+1,i+r,j+r));
01099 if(pmsk[i+j*nx]==1) {
01100 pima[i+j*nx]=med;
01101 }
01102 }
01103 }
01104
01105 cleanup:
01106 return cpl_error_get_code();
01107
01108 }
01109
01110 static cpl_error_code
01111 xsh_image_coadd(cpl_image **self, const cpl_image *add )
01112 {
01113 int nx1=0;
01114 int ny1=0;
01115 int nx2=0;
01116 int ny2=0;
01117 float* pself=NULL;
01118 const float* padd=NULL;
01119 int i=0;
01120
01121 XSH_ASSURE_NOT_NULL( self ) ;
01122 XSH_ASSURE_NOT_NULL( add ) ;
01123
01124 check(nx1=cpl_image_get_size_x(*self));
01125 check(ny1=cpl_image_get_size_y(*self));
01126
01127
01128 check(nx2=cpl_image_get_size_x(add));
01129 check(ny2=cpl_image_get_size_y(add));
01130
01131 pself=cpl_image_get_data_float(*self);
01132 padd=cpl_image_get_data_float_const(add);
01133
01134 if (nx1 != nx2 || ny1 != ny2) {
01135 xsh_msg("Input image of different size");
01136 }
01137 for(i=0;i<nx1*ny2;i++){
01138 if( (pself[i]==0) && (padd[i] !=0) ) {
01139 pself[i]+=padd[i];
01140 }
01141 }
01142 cleanup:
01143 return cpl_error_get_code();
01144
01145 }
01146
01147 cpl_image*
01148 xsh_image_flag_bptype_with_crox(cpl_image* ima)
01149
01150 {
01151
01152
01153 cpl_image* shift=NULL;
01154 cpl_image* res=NULL;
01155
01156
01157 res=cpl_image_duplicate(ima);
01158
01159
01160 shift=cpl_image_duplicate(ima);
01161 cpl_image_shift(shift,1,0);
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,-1,0);
01168 check(xsh_image_coadd(&res,shift));
01169 xsh_free_image(&shift);
01170
01171
01172 shift=cpl_image_duplicate(ima);
01173 cpl_image_shift(shift,0,-1);
01174 check(xsh_image_coadd(&res,shift));
01175 xsh_free_image(&shift);
01176
01177
01178 shift=cpl_image_duplicate(ima);
01179 cpl_image_shift(shift,0,1);
01180 check(xsh_image_coadd(&res,shift));
01181 xsh_free_image(&shift);
01182
01183
01184 cleanup:
01185 return res;
01186
01187 }
01188
01189 cpl_error_code
01190 xsh_badpixelmap_flag_saturated_pixels(xsh_pre* pre,xsh_instrument* instr,const double cor_val, int flag, int* nsat)
01191 {
01192 int size=0;
01193 int i=0;
01194 float* pima=NULL;
01195 int* pqual=NULL;
01196 int bp_code_sat=0;
01197 int bp_code_neg=0;
01198 double thresh_max=0;
01199 double time=0;
01200 int ndit=0;
01201 double dit=0;
01202 float cpima=0;
01203
01204 XSH_ASSURE_NOT_NULL_MSG(pre, "Null input pre frame");
01205 XSH_ASSURE_NOT_NULL_MSG(instr, "Null input pre frame");
01206 size=pre->nx*pre->ny;
01207
01208 bp_code_sat=QFLAG_ADC_SATURATION;
01209 bp_code_neg=QFLAG_NEGATIVE_DATA;
01210 if(xsh_instrument_get_arm(instr) != XSH_ARM_NIR) {
01211 thresh_max=65000;
01212 } else {
01213 thresh_max=45000;
01214 dit=xsh_pfits_get_dit (pre->data_header);
01215 ndit=xsh_pfits_get_ndit (pre->data_header);
01216 time=dit*ndit;
01217 if(time>=1.2) {
01218 thresh_max=42000;
01219 bp_code_sat=QFLAG_SATURATED_DATA;
01220 }
01221 }
01222
01223 check(pima=cpl_image_get_data_float(pre->data));
01224 check(pqual=cpl_image_get_data_int(pre->qual));
01225
01226
01227 thresh_max=thresh_max -cor_val;
01228
01229 if(flag) {
01230 double thresh_min=1-cor_val;
01231 for(i=0;i<size;i++) {
01232 cpima=pima[i];
01233
01234 if( pima[i] > thresh_max) {
01235 pqual[i] |= bp_code_sat;
01236 (*nsat)++;
01237 }
01238 if ( cpima < thresh_min ) {
01239 pqual[i] |= bp_code_neg;
01240 }
01241 }
01242 } else {
01243 for(i=0;i<size;i++) {
01244 if( pima[i] > thresh_max) {
01245 (*nsat)++;
01246 }
01247 }
01248 }
01249
01250 cleanup:
01251 return cpl_error_get_code();
01252 }
01253
01254 cpl_error_code
01255 xsh_badpixelmap_count_range_pixels(xsh_pre* pre,
01256 const double thresh_min,
01257 const double thresh_max,
01258 const double cor_val,
01259 int* nrange,
01260 double* frange)
01261 {
01262 float* pdata=NULL;
01263 int i=0;
01264 int size=pre->nx*pre->ny;
01265
01266
01267 pdata=cpl_image_get_data_float(pre->data);
01268 for(i=0;i<size;i++) {
01269 if(pdata[i]>=(thresh_min-cor_val) &&
01270 pdata[i]<=(thresh_max-cor_val)) {
01271 (*nrange)++;
01272 }
01273 }
01274 *frange=(double)(*nrange)/(size);
01275 return cpl_error_get_code();
01276
01277 }
01278
01279 cpl_error_code
01280 xsh_badpixelmap_count_sat_pixels(xsh_pre* pre,const double sat_thresh,
01281 const double cor_val, int* nsat,
01282 double* frac_sat)
01283 {
01284 float* pdata=NULL;
01285 int i=0;
01286 int size=pre->nx*pre->ny;
01287
01288
01289 pdata=cpl_image_get_data_float(pre->data);
01290 for(i=0;i<size;i++) {
01291 if(pdata[i]>=(sat_thresh-cor_val) || pdata[i]==-cor_val) (*nsat)++;
01292 }
01293 *frac_sat=(double)(*nsat)/(size);
01294 return cpl_error_get_code();
01295
01296 }
01297
01298
01299 cpl_error_code
01300 xsh_badpixelmap_fill_bp_pattern_holes(cpl_frame* frm)
01301 {
01302 const char* name=NULL;
01303 cpl_image* dima=NULL;
01304 cpl_image* eima=NULL;
01305 cpl_image* qima=NULL;
01306 cpl_propertylist* phead=NULL;
01307 cpl_propertylist* ehead=NULL;
01308 cpl_propertylist* qhead=NULL;
01309 int* pima=NULL;
01310 int npix=0;
01311 int i=0;
01312 int j=0;
01313
01314 int nx=0;
01315 int ny=0;
01316 int j_nx=0;
01317 int j_nx_i=0;
01318
01319 name=cpl_frame_get_filename(frm);
01320 dima=cpl_image_load(name,XSH_PRE_DATA_TYPE,0,0);
01321 eima=cpl_image_load(name,XSH_PRE_ERRS_TYPE,0,1);
01322 qima=cpl_image_load(name,XSH_PRE_QUAL_TYPE,0,2);
01323 phead=cpl_propertylist_load(name,0);
01324 ehead=cpl_propertylist_load(name,1);
01325 qhead=cpl_propertylist_load(name,2);
01326 pima=cpl_image_get_data_int(qima);
01327 nx=cpl_image_get_size_x(qima);
01328 ny=cpl_image_get_size_y(qima);
01329 if(cpl_propertylist_has(phead,XSH_QC_NHPIX)) {
01330 npix=xsh_pfits_get_qc_nhpix(phead);
01331 }
01332
01333
01334 for(j=1;j<ny-1;j++){
01335 j_nx = j*nx;
01336 for(i=1;i<nx-1;i++){
01337 j_nx_i = j_nx + i;
01338 if ((pima[j_nx_i] & QFLAG_ELECTRONIC_PICKUP) > 0) {continue;};
01339 if ((pima[j_nx_i - 1] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
01340 if ((pima[j_nx_i + 1] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
01341 if ((pima[j_nx_i - nx] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
01342 if ((pima[j_nx_i + nx] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
01343 pima[j_nx_i] |= QFLAG_ELECTRONIC_PICKUP;
01344 npix++;
01345
01346 }
01347 }
01348
01349 xsh_pfits_set_qc_nhpix(phead,npix);
01350 xsh_pfits_set_qc_noisepix(phead,npix);
01351
01352 cpl_image_save(dima,name,XSH_PRE_DATA_BPP,phead,CPL_IO_DEFAULT);
01353 cpl_image_save(eima,name,XSH_PRE_ERRS_BPP,ehead,CPL_IO_EXTEND);
01354 cpl_image_save(qima,name,XSH_PRE_QUAL_BPP,qhead,CPL_IO_EXTEND);
01355
01356
01357 xsh_free_image(&dima);
01358 xsh_free_image(&eima);
01359 xsh_free_image(&qima);
01360 xsh_free_propertylist(&phead);
01361 xsh_free_propertylist(&ehead);
01362 xsh_free_propertylist(&qhead);
01363
01364 return cpl_error_get_code();
01365 }
01366
01389 cpl_frame* xsh_badpixelmap_extract(cpl_frame* frame, int xmin, int ymin,
01390 int xmax, int ymax)
01391 {
01392 cpl_image* ima=NULL;
01393 cpl_image* ext=NULL;
01394 cpl_propertylist* plist=NULL;
01395 const char* fname=NULL;
01396 cpl_frame* result=NULL;
01397 char name_o[256];
01398
01399 XSH_ASSURE_NOT_NULL( frame);
01400
01401 result=cpl_frame_duplicate(frame);
01402 fname=cpl_frame_get_filename(frame);
01403 plist=cpl_propertylist_load(fname,0);
01404 ima=cpl_image_load(fname, XSH_PRE_DATA_BPP, 0,0);
01405 ext=cpl_image_extract(ima,xmin,ymin,xmax,ymax);
01406 sprintf(name_o,"SUB_%s",fname);
01407 cpl_image_save(ext, name_o, XSH_PRE_DATA_BPP,plist, CPL_IO_DEFAULT);
01408 check( cpl_frame_set_filename( result, name_o));
01409 xsh_add_temporary_file(name_o);
01410
01411 cleanup:
01412 if( cpl_error_get_code() != CPL_ERROR_NONE) {
01413 xsh_free_frame( &result);
01414 }
01415 xsh_free_image(&ima);
01416 xsh_free_image(&ext);
01417 xsh_free_propertylist(&plist);
01418
01419 return result;
01420
01421 }
01422