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
00065
00066
00067
00068
00069
00070
00071
00072
00079 void xsh_badpixelmap_or( xsh_pre *self, const xsh_pre *right )
00080 {
00081 int * qual0 = NULL,
00082 * qual1 = NULL ;
00083 int i ;
00084
00085 XSH_ASSURE_NOT_NULL( self ) ;
00086 XSH_ASSURE_NOT_NULL( right ) ;
00087 check(qual0 = cpl_image_get_data_int(right->qual));
00088 check(qual1 = cpl_image_get_data_int(self->qual));
00089 XSH_ASSURE_NOT_ILLEGAL(right->nx == self->nx);
00090 XSH_ASSURE_NOT_ILLEGAL(right->ny == self->ny );
00091
00092 for(i=0;i<self->nx*self->ny;i++) {
00093 qual1[i] |= qual0[i] ;
00094 }
00095
00096 cleanup:
00097 return ;
00098 }
00099
00109 void xsh_set_image_cpl_bpmap (cpl_image * image,
00110 cpl_image * bpmap, int nx, int ny)
00111 {
00112 int *bppix = NULL;
00113 int ix = 0, iy = 0;
00114
00115 assure( bpmap != NULL, CPL_ERROR_NULL_INPUT,
00116 "BpMap is NULL pointer" ) ;
00117
00118 check(bppix = cpl_image_get_data_int (bpmap));
00119
00120 for (iy = 0; iy < ny; iy++)
00121 for (ix = 0; ix < nx; ix++) {
00122 int j = ix + (iy * nx);
00123
00124 if (bppix[j] > XSH_GOOD_PIXEL_LEVEL) {
00125 cpl_image_reject (image, ix + 1, iy + 1);
00126 }
00127 }
00128
00129 cleanup:
00130 return;
00131 }
00132
00133
00134
00135 void xsh_bpmap_bitwise_to_flag(cpl_image * bpmap,int flag )
00136 {
00137 float* data = NULL;
00138 int nx=0;
00139 int ny=0;
00140 int i=0;
00141
00142 check(nx = cpl_image_get_size_x(bpmap));
00143 check(ny = cpl_image_get_size_y(bpmap));
00144
00145 check(data = cpl_image_get_data_float(bpmap));
00146
00147 for(i=0;i<nx*ny;i++){
00148 if(data[i]){
00149 data[i] = flag;
00150 }
00151 }
00152
00153 cleanup:
00154 return;
00155
00156
00157 }
00158
00159
00160 void xsh_bpmap_mask_bad_pixel(cpl_image * bpmap, cpl_mask* mask,
00161 int flag )
00162 {
00163 int* data = NULL;
00164 cpl_mask* old = NULL;
00165 cpl_binary* maskdata = NULL;
00166 int i = 0, nx = 0, ny = 0;
00167
00168
00169 assure(bpmap != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
00170 assure(mask != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
00171
00172
00173 check(nx = cpl_image_get_size_x(bpmap));
00174 check(ny = cpl_image_get_size_y(bpmap));
00175 assure(nx == cpl_mask_get_size_x(mask),CPL_ERROR_ILLEGAL_INPUT,
00176 "mask %d and image %d must have same size in x",
00177 cpl_mask_get_size_x(mask),nx);
00178 assure(ny == cpl_mask_get_size_y(mask),CPL_ERROR_ILLEGAL_INPUT,
00179 "mask %d and image %d must have same size in y",
00180 cpl_mask_get_size_y(mask),ny);
00181
00182
00183 check( old = cpl_image_get_bpm(bpmap));
00184
00185
00186 check(maskdata = cpl_mask_get_data(mask));
00187
00188
00189 check(data = cpl_image_get_data(bpmap));
00190
00191 for(i=0;i<nx*ny;i++){
00192 if(maskdata[i]){
00193 data[i] |= flag;
00194 }
00195 }
00196
00197
00198 check(cpl_mask_or(old,mask));
00199
00200 cleanup:
00201 return;
00202 }
00203
00204 void
00205 xsh_bpmap_set_bad_pixel (cpl_image * bpmap, int ix, int iy, int flag)
00206 {
00207 cpl_image_set (bpmap, ix, iy, (double) flag);
00208 cpl_image_reject (bpmap, ix, iy);
00209 }
00210
00219 cpl_image* xsh_bpmap_collapse_bpmap_create (cpl_imagelist * liste)
00220 {
00221
00222
00223
00224 cpl_image *bpmap = NULL;
00225 int* bppix = NULL;
00226 int nx, ny, npix;
00227 int nimg, i;
00228 cpl_image *result = NULL;
00229 int ix, iy;
00230
00231 xsh_msg( "---> xsh_bpmap_collapse_bpmap_create" ) ;
00232 bpmap = cpl_image_duplicate (cpl_imagelist_get (liste, 0));
00233 assure (bpmap != NULL, cpl_error_get_code (), "Cant duplicate first bpmap");
00234
00235 bppix = cpl_image_get_data_int (bpmap);
00236 assure (bppix != NULL, cpl_error_get_code (), "Cant get data int");
00237
00238 nimg = cpl_imagelist_get_size (liste);
00239
00240 nx = cpl_image_get_size_x (bpmap);
00241 ny = cpl_image_get_size_y (bpmap);
00242 npix = nx * ny;
00243 xsh_msg_dbg_low ("Nb of bpmap: %d, nx: %d, ny: %d [%d]", nimg, nx, ny, npix);
00244
00245 for (i = 1; i < nimg; i++) {
00246 int j = 0;
00247 cpl_image *current = NULL;
00248 int *curpix = NULL;
00249
00250 current = cpl_imagelist_get (liste, i);
00251 assure (current != NULL, cpl_error_get_code (), "Cant get bpmap #%d", i);
00252
00253 curpix = cpl_image_get_data_int (current);
00254 assure (curpix != NULL, cpl_error_get_code (),
00255 "Cant get data int for bpmap #%d", i);
00256
00257 for (j = 0; j < npix; j++)
00258 bppix[j] |= curpix[j];
00259 }
00260
00261 result = cpl_image_wrap_int (nx, ny, bppix);
00262 assure (result != NULL, cpl_error_get_code (), "Cant wrap final bpmap");
00263
00264
00265 for (iy = 0; iy < ny; iy++)
00266 for (ix = 0; ix < nx; ix++) {
00267 int j = ix + (iy * nx);
00268
00269 if (bppix[j] > XSH_GOOD_PIXEL_LEVEL)
00270 cpl_image_reject (result, ix + 1, iy + 1);
00271 }
00272
00273 return result;
00274
00275 cleanup:
00276 return NULL;
00277 }
00278
00288 int
00289 xsh_bpmap_count (cpl_image * bpmap, int nx, int ny)
00290 {
00291 int *pixbuf;
00292 int i, count = 0, npix = nx * ny;
00293
00294 pixbuf = cpl_image_get_data_int (bpmap);
00295 assure (pixbuf != NULL, cpl_error_get_code (), "Cant get pixel buffer");
00296
00297 for (i = 0; i < npix; i++) {
00298 if (*(pixbuf + i) != 0)
00299 count++;
00300 }
00301
00302 cleanup:
00303 return count;
00304 }
00305
00315 void xsh_bpmap_collapse_median(cpl_image* median, cpl_imagelist * list,
00316 cpl_mask * bpmap)
00317 {
00318 int nx, ny, nimg, i;
00319 cpl_image **pimg = NULL;
00320 cpl_vector *pixvector = NULL;
00321 double* data = NULL;
00322 cpl_binary* mask_data = NULL;
00323 int ix, iy, k;
00324
00325 XSH_ASSURE_NOT_NULL(median);
00326 XSH_ASSURE_NOT_NULL(list);
00327 XSH_ASSURE_NOT_NULL(bpmap);
00328
00329 check(nx = cpl_image_get_size_x(median));
00330 check(ny = cpl_image_get_size_y(median));
00331 check(nimg = cpl_imagelist_get_size(list));
00332
00333
00334 XSH_CALLOC(pimg, cpl_image*, nimg);
00335
00336
00337 for (i = 0; i < nimg; i++){
00338 *(pimg + i) = cpl_imagelist_get (list, i);
00339 assure(cpl_image_get_size_x(*(pimg + i)) == nx,CPL_ERROR_ILLEGAL_INPUT,
00340 " data list x size is not equal to median frame");
00341 assure(cpl_image_get_size_y(*(pimg + i)) == ny,CPL_ERROR_ILLEGAL_INPUT,
00342 " data list y size is not equal to median frame");
00343 }
00344
00345
00346 XSH_MALLOC(data, double, nimg);
00347 check(mask_data = cpl_mask_get_data(bpmap));
00348
00349 for (iy = 1; iy <= ny; iy++) {
00350
00351 for (ix = 1; ix <= nx; ix++) {
00352 int count = 0;
00353
00354 if (mask_data[(iy-1)*nx+ix-1] == CPL_BINARY_0){
00355 for (k = 0; k < nimg; k++) {
00356 int rej;
00357 double val;
00358
00359 check (val = cpl_image_get (pimg[k], ix, iy, &rej) ) ;
00360 if (rej == 0) {
00361 data[count] = val;
00362 count++;
00363 }
00364 }
00365 }
00366
00367 if (count) {
00368 double med = 0.0;
00369
00370 check(pixvector = cpl_vector_wrap(count,data));
00371 check(med = cpl_vector_get_median( pixvector));
00372 check(cpl_image_set(median, ix, iy, med));
00373 check(xsh_unwrap_vector(&pixvector));
00374
00375 }
00376 }
00377 }
00378 cleanup:
00379 if (cpl_error_get_code() != CPL_ERROR_NONE){
00380 xsh_unwrap_vector (&pixvector);
00381 }
00382 XSH_FREE(data);
00383 XSH_FREE(pimg) ;
00384 return;
00385 }
00386
00396 void xsh_bpmap_collapse_mean(cpl_image * mean, cpl_imagelist * list,
00397 cpl_mask * bpmap)
00398 {
00399 int nx, ny, nimg, i;
00400 cpl_image **pimg = NULL;
00401 cpl_vector *pixvector = NULL;
00402 double* data = NULL;
00403 cpl_binary* mask_data = NULL;
00404 int ix, iy, k;
00405
00406 XSH_ASSURE_NOT_NULL(mean);
00407 XSH_ASSURE_NOT_NULL(list);
00408 XSH_ASSURE_NOT_NULL(bpmap);
00409
00410 check(nx = cpl_image_get_size_x(mean));
00411 check(ny = cpl_image_get_size_y(mean));
00412 check(nimg = cpl_imagelist_get_size(list));
00413
00414
00415 XSH_CALLOC(pimg, cpl_image*, nimg);
00416
00417
00418 for (i = 0; i < nimg; i++){
00419 *(pimg + i) = cpl_imagelist_get (list, i);
00420 assure(cpl_image_get_size_x(*(pimg + i)) == nx,CPL_ERROR_ILLEGAL_INPUT,
00421 " data list x size is not equal to median frame");
00422 assure(cpl_image_get_size_y(*(pimg + i)) == ny,CPL_ERROR_ILLEGAL_INPUT,
00423 " data list y size is not equal to median frame");
00424 }
00425
00426
00427 XSH_MALLOC(data, double, nimg);
00428 check(mask_data = cpl_mask_get_data(bpmap));
00429
00430 for (iy = 1; iy <= ny; iy++) {
00431
00432 for (ix = 1; ix <= nx; ix++) {
00433 int count = 0;
00434
00435 if (mask_data[(iy-1)*nx+ix-1] == CPL_BINARY_0){
00436 for (k = 0; k < nimg; k++) {
00437 int rej;
00438 double val;
00439
00440 check (val = cpl_image_get (pimg[k], ix, iy, &rej) ) ;
00441 if (rej == 0) {
00442 data[count] = val;
00443 count++;
00444 }
00445 }
00446 }
00447
00448 if (count) {
00449 double avg = 0.0;
00450
00451 check(pixvector = cpl_vector_wrap(count,data));
00452 check(avg = cpl_vector_get_mean( pixvector));
00453 check(cpl_image_set(mean, ix, iy, avg ));
00454 check(xsh_unwrap_vector(&pixvector));
00455
00456 }
00457 }
00458 }
00459 cleanup:
00460 if (cpl_error_get_code() != CPL_ERROR_NONE){
00461 xsh_unwrap_vector (&pixvector);
00462 }
00463 XSH_FREE(data);
00464 XSH_FREE(pimg) ;
00465 return;
00466 }
00467
00474 cpl_error_code
00475 xsh_badpixelmap_coadd(cpl_frame *self, const cpl_frame *right )
00476 {
00477
00478
00479 int nx = 0, ny = 0;
00480 const char* self_name=NULL;
00481 const char* right_name=NULL;
00482 cpl_image* map_self=NULL;
00483 cpl_image* map_right=NULL;
00484 cpl_propertylist* plist=NULL;
00485 float* pself=NULL;
00486 float* pright=NULL;
00487 int i=0;
00488 int j=0;
00489
00490
00491 assure(self != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
00492 assure(right != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
00493
00494 check(self_name=cpl_frame_get_filename(self));
00495 check(right_name=cpl_frame_get_filename(right));
00496 xsh_msg("self_name=%s",self_name);
00497 check(plist=cpl_propertylist_load(self_name,0));
00498
00499 check(map_self=cpl_image_load(self_name,CPL_TYPE_FLOAT,0,0));
00500 check(map_right=cpl_image_load(right_name,CPL_TYPE_FLOAT,0,0));
00501 xsh_msg("coadd self %s with right %s",
00502 cpl_frame_get_tag(self),cpl_frame_get_tag(right));
00503
00504
00505 check(nx = cpl_image_get_size_x(map_self));
00506 check(ny = cpl_image_get_size_y(map_self));
00507 assure(nx == cpl_image_get_size_x(map_right),CPL_ERROR_ILLEGAL_INPUT,
00508 "addendum mask %d and original mask %d must have same size in x",
00509 cpl_image_get_size_x(map_right),nx);
00510 assure(ny == cpl_image_get_size_y(map_right),CPL_ERROR_ILLEGAL_INPUT,
00511 "addendum mask %d and original mask %d must have same size in y",
00512 cpl_image_get_size_y(map_right),ny);
00513
00514
00515 pself=cpl_image_get_data_float(map_self);
00516 pright=cpl_image_get_data_float(map_right);
00517 for(j=0;j<ny;j++) {
00518 for(i=0;i<nx;i++) {
00519
00520 if(pself[j*nx+i]==0 && pright[j*nx+i]!=0) {
00521
00522 pself[j*nx+i]=pright[j*nx+i];
00523 }
00524 }
00525 }
00526
00527 check(cpl_image_save(map_self,"self.fits",CPL_BPP_IEEE_FLOAT,
00528 plist,CPL_IO_DEFAULT));
00529
00530 check(cpl_image_save(map_right,"right.fits",CPL_BPP_IEEE_FLOAT,
00531 plist,CPL_IO_DEFAULT));
00532 xsh_msg("self name=%s",self_name);
00533
00534 check(cpl_image_save(map_self,"BP_COADD.fits",CPL_BPP_IEEE_FLOAT,
00535 plist,CPL_IO_DEFAULT));
00536 cpl_frame_set_filename(self,"BP_COADD.fits");
00537 cleanup:
00538
00539 xsh_free_propertylist(&plist);
00540 xsh_free_image(&map_self);
00541 xsh_free_image(&map_right);
00542
00543 return cpl_error_get_code();
00544
00545 }
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565 static cpl_error_code
00566 xsh_image_get_hot_cold_maps(cpl_image* masterbias,
00567 const double kappa_low,
00568 const double kappa_high,
00569 const int low_niter,
00570 const int high_niter,
00571 cpl_mask** bpmhot,
00572 int *hotpix_nb,
00573 cpl_mask** bpmcold,
00574 int *coldpix_nb,
00575 cpl_propertylist** hplist,
00576 cpl_propertylist** cplist)
00577 {
00578
00579
00580 double lower=0;
00581 double upper=0;
00582 double dark_med=0;
00583 double stdev=0;
00584 double mean=0;
00585
00586 check(xsh_ksigma_clip(masterbias, 1, 1,
00587 cpl_image_get_size_x(masterbias),
00588 cpl_image_get_size_y(masterbias),
00589 kappa_low,
00590 low_niter, 1e-5,
00591 &mean, &stdev));
00592
00593 check(cpl_propertylist_append_double(*cplist,XSH_QC_MASTER_MEAN,mean));
00594 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
00595
00596 check(cpl_propertylist_append_double(*cplist, XSH_QC_MASTER_RMS,stdev));
00597 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
00598
00599 cpl_image_accept_all(masterbias);
00600
00601
00602 check(dark_med = cpl_image_get_median(masterbias));
00603
00604
00605 lower = dark_med - stdev * kappa_low;
00606
00607 xsh_msg_dbg_low("dark_med=%f lower=%f",dark_med,lower);
00608
00609
00610 check_msg(*bpmcold = cpl_mask_threshold_image_create(masterbias,
00611 -FLT_MAX,lower),
00612 "Cannot compute the cold pixel map");
00613
00614
00615 if(*bpmcold!=NULL) {
00616 check(*coldpix_nb = cpl_mask_count(*bpmcold));
00617 } else {
00618 *coldpix_nb=0;
00619 }
00620 check(cpl_propertylist_append_int(*cplist, XSH_QC_COLD_PIX_NUM,*coldpix_nb));
00621 check(cpl_propertylist_set_comment(*cplist,XSH_QC_COLD_PIX_NUM,XSH_QC_COLD_PIX_NUM_C));
00622
00623
00624
00625 check(xsh_ksigma_clip(masterbias, 1, 1,
00626 cpl_image_get_size_x(masterbias),
00627 cpl_image_get_size_y(masterbias),
00628 kappa_high,
00629 high_niter, 1e-5,
00630 &mean, &stdev));
00631
00632
00633
00634 check(cpl_propertylist_append_double(*hplist, XSH_QC_MASTER_MEAN,mean));
00635 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
00636 check(cpl_propertylist_append_double(*hplist,XSH_QC_MASTER_RMS,stdev));
00637 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
00638
00639 cpl_image_accept_all(masterbias);
00640
00641
00642
00643 check(dark_med = cpl_image_get_median(masterbias));
00644
00645
00646 upper = dark_med + stdev * kappa_high;
00647 xsh_msg_dbg_low("dark_med=%f upper=%f",dark_med,upper);
00648
00649 check_msg(*bpmhot = cpl_mask_threshold_image_create(masterbias,
00650 upper,DBL_MAX),
00651 "Cannot compute the hot pixel map");
00652
00653 if(*bpmhot!=NULL) {
00654 check(*hotpix_nb = cpl_mask_count(*bpmhot));
00655 } else {
00656 *hotpix_nb=0;
00657 }
00658
00659 check(cpl_propertylist_append_int(*hplist,XSH_QC_HOT_PIX_NUM,*hotpix_nb));
00660 check(cpl_propertylist_set_comment(*hplist,XSH_QC_HOT_PIX_NUM,XSH_QC_HOT_PIX_NUM_C));
00661
00662
00663 cleanup:
00664 return cpl_error_get_code();
00665
00666 }
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 cpl_error_code
00680 xsh_image_get_hot_cold_pixs(cpl_frame* frame_image,
00681 xsh_instrument* instrument,
00682 const double ks_low,
00683 const int cold_niter,
00684 const double ks_high,
00685 const int hot_niter,
00686 cpl_frame** cpix_frm,
00687 cpl_frame** hpix_frm)
00688
00689 {
00690
00691
00692 cpl_image* image=NULL;
00693
00694 cpl_mask* bpmcold=NULL;
00695 cpl_mask* bpmhot=NULL;
00696 int hotpix_nb=0;
00697 int coldpix_nb=0;
00698 cpl_image* cpix_ima=NULL;
00699 cpl_image* hpix_ima=NULL;
00700 cpl_propertylist* cplist=NULL;
00701 cpl_propertylist* hplist=NULL;
00702 cpl_propertylist* plist=NULL;
00703
00704
00705 const char* cpix_pro_catg=NULL;
00706 const char* hpix_pro_catg=NULL;
00707 char* cpix_name=NULL;
00708 char* hpix_name=NULL;
00709 const char* name=NULL;
00710
00711 check(name=cpl_frame_get_filename(frame_image));
00712
00713 check(image=cpl_image_load(cpl_frame_get_filename(frame_image),
00714 CPL_TYPE_FLOAT,0,0));
00715
00716
00717 check(hplist=cpl_propertylist_new());
00718 check(cplist=cpl_propertylist_new());
00719 xsh_msg("determine hot and cold pixels");
00720 check(xsh_image_get_hot_cold_maps(image,
00721 ks_low,ks_high,cold_niter,hot_niter,
00722 &bpmhot,&hotpix_nb,
00723 &bpmcold,&coldpix_nb,&hplist,&cplist));
00724
00725
00726 xsh_msg("Hot pix nb=%d Cold pix nb=%d",hotpix_nb,coldpix_nb);
00727
00728
00729 check(cpix_ima=cpl_image_new_from_mask(bpmcold));
00730
00731 check(cpl_image_multiply_scalar(cpix_ima,QFLAG_CAMERA_DEFECT));
00732
00733
00734
00735
00736 check(cpix_name= cpl_sprintf("%s_%s_%s",XSH_BP_MAP_CP,
00737 xsh_instrument_arm_tostring(instrument),name));
00738
00739
00740 check(plist=cpl_propertylist_load(name,0));
00741 check(cpl_propertylist_append(plist,cplist));
00742 cpl_image_save(cpix_ima,cpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
00743 xsh_free_propertylist(&plist);
00744 xsh_free_propertylist(&cplist);
00745 xsh_add_temporary_file(cpix_name);
00746
00747
00748 check(cpix_pro_catg=XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_CP,instrument));
00749
00750 check(*cpix_frm=xsh_frame_product(cpix_name,cpix_pro_catg,
00751 CPL_FRAME_TYPE_IMAGE,
00752 CPL_FRAME_GROUP_PRODUCT,
00753 CPL_FRAME_LEVEL_FINAL));
00754
00755
00756 check(hpix_ima=cpl_image_new_from_mask(bpmhot));
00757
00758 check(cpl_image_multiply_scalar(hpix_ima,QFLAG_QUESTIONABLE_PIXEL));
00759
00760
00761
00762
00763 check(hpix_name=cpl_sprintf("%s_%s_%s",XSH_BP_MAP_HP,
00764 xsh_instrument_arm_tostring(instrument),name)) ;
00765
00766 check(plist=cpl_propertylist_load(name,0));
00767 check(cpl_propertylist_append(plist,hplist));
00768 cpl_image_save(hpix_ima,hpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
00769 xsh_free_propertylist(&plist);
00770 xsh_free_propertylist(&hplist);
00771 xsh_add_temporary_file(hpix_name);
00772
00773 check(hpix_pro_catg=XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_HP,instrument));
00774 check(*hpix_frm=xsh_frame_product(hpix_name,hpix_pro_catg,
00775 CPL_FRAME_TYPE_IMAGE,
00776 CPL_FRAME_GROUP_PRODUCT,
00777 CPL_FRAME_LEVEL_FINAL));
00778
00779 cleanup:
00780 xsh_free_mask(&bpmcold);
00781 xsh_free_mask(&bpmhot);
00782
00783 xsh_free_image(&image);
00784 xsh_free_image(&cpix_ima);
00785 xsh_free_image(&hpix_ima);
00786 xsh_free_propertylist(&cplist);
00787 xsh_free_propertylist(&hplist);
00788 xsh_free_propertylist(&plist);
00789 XSH_FREE(hpix_name);
00790 XSH_FREE(cpix_name);
00791
00792 return cpl_error_get_code();
00793
00794 }
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 cpl_frame*
00805 xsh_image_local_cold_pixs(cpl_image* ima,
00806 const double kappa,
00807 const int r,
00808 xsh_instrument* instr)
00809
00810 {
00811
00812 int nx=0;
00813 int ny=0;
00814 int i=0;
00815 int j=0;
00816 double med=0;
00817 double rms=0;
00818 double* pima=NULL;
00819 double* pmsk=NULL;
00820 cpl_image* msk=NULL;
00821 char name[80];
00822 char tag[80];
00823 cpl_frame* msk_frm=NULL;
00824
00825 nx=cpl_image_get_size_x(ima);
00826 ny=cpl_image_get_size_y(ima);
00827 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
00828
00829 pima=cpl_image_get_data_double(ima);
00830 pmsk=cpl_image_get_data_double(msk);
00831
00832 for(j=r;j<ny-r;j++) {
00833 for(i=r;i<nx-r;i++) {
00834 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
00835 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
00836 if(pima[i+j*nx]< med- kappa*rms) {
00837 pmsk[i+j*nx]=QFLAG_LOW_QE_PIXEL;
00838 }
00839 }
00840 }
00841
00842 sprintf(tag,"%s_%s",XSH_BP_MAP_DP,xsh_instrument_arm_tostring(instr));
00843 sprintf(name,"%s.fits",tag);
00844 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,
00845 CPL_IO_DEFAULT));
00846
00847 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
00848 CPL_FRAME_GROUP_PRODUCT,
00849 CPL_FRAME_LEVEL_FINAL));
00850
00851 cleanup:
00852 return msk_frm;
00853
00854 }
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 cpl_frame*
00866 xsh_image_local_hot_pixs(cpl_image* ima,
00867 const double kappa,
00868 const int r,
00869 xsh_instrument* instr)
00870
00871 {
00872
00873 int nx=0;
00874 int ny=0;
00875 int i=0;
00876 int j=0;
00877 double med=0;
00878 double rms=0;
00879 double* pima=NULL;
00880 double* pmsk=NULL;
00881 cpl_image* msk=NULL;
00882 char name[80];
00883 char tag[80];
00884 cpl_frame* msk_frm=NULL;
00885
00886 nx=cpl_image_get_size_x(ima);
00887 ny=cpl_image_get_size_y(ima);
00888 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
00889
00890 pima=cpl_image_get_data_double(ima);
00891 pmsk=cpl_image_get_data_double(msk);
00892
00893 for(j=r;j<ny-r;j++) {
00894 for(i=r;i<nx-r;i++) {
00895 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
00896 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
00897 if(pima[i+j*nx]> med+ kappa*rms) {
00898 pmsk[i+j*nx]=QFLAG_WELL_SATURATION;
00899 }
00900 }
00901 }
00902
00903 sprintf(tag,"%s_%s",XSH_BP_MAP_SP,xsh_instrument_arm_tostring(instr));
00904 sprintf(name,"%s.fits",tag);
00905 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,CPL_IO_DEFAULT));
00906
00907 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
00908 CPL_FRAME_GROUP_PRODUCT,
00909 CPL_FRAME_LEVEL_FINAL));
00910
00911 cleanup:
00912 return msk_frm;
00913
00914 }
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926 cpl_error_code
00927 xsh_image_clean_mask_pixs(cpl_image** ima,cpl_image* msk,const int r)
00928
00929 {
00930 int nx=0;
00931 int ny=0;
00932 int i=0;
00933 int j=0;
00934 double med=0;
00935 double* pima=NULL;
00936 double* pmsk=NULL;
00937
00938 nx=cpl_image_get_size_x(*ima);
00939 ny=cpl_image_get_size_y(*ima);
00940
00941 pima=cpl_image_get_data_double(*ima);
00942 pmsk=cpl_image_get_data_double(msk);
00943
00944 for(j=r;j<ny-r;j++) {
00945 for(i=r;i<nx-r;i++) {
00946 check(med=cpl_image_get_median_window(*ima,i-r+1,j-r+1,i+r,j+r));
00947 if(pmsk[i+j*nx]==1) {
00948 pima[i+j*nx]=med;
00949 }
00950 }
00951 }
00952
00953 cleanup:
00954 return cpl_error_get_code();
00955
00956 }
00957
00958 static cpl_error_code
00959 xsh_image_coadd(cpl_image **self, const cpl_image *add )
00960 {
00961 int nx1=0;
00962 int ny1=0;
00963 int nx2=0;
00964 int ny2=0;
00965 float* pself=NULL;
00966 const float* padd=NULL;
00967 int i=0;
00968
00969 XSH_ASSURE_NOT_NULL( self ) ;
00970 XSH_ASSURE_NOT_NULL( add ) ;
00971
00972 check(nx1=cpl_image_get_size_x(*self));
00973 check(ny1=cpl_image_get_size_y(*self));
00974
00975
00976 check(nx2=cpl_image_get_size_x(add));
00977 check(ny2=cpl_image_get_size_y(add));
00978
00979 pself=cpl_image_get_data_float(*self);
00980 padd=cpl_image_get_data_float_const(add);
00981
00982 if (nx1 != nx2 || ny1 != ny2) {
00983 xsh_msg("Input image of different size");
00984 }
00985 for(i=0;i<nx1*ny2;i++){
00986 if( (pself[i]==0) && (padd[i] !=0) ) {
00987 pself[i]+=padd[i];
00988 }
00989 }
00990 cleanup:
00991 return cpl_error_get_code();
00992
00993 }
00994
00995 cpl_image*
00996 xsh_image_flag_bptype_with_crox(cpl_image* ima)
00997
00998 {
00999
01000
01001 cpl_image* shift=NULL;
01002 cpl_image* res=NULL;
01003
01004
01005 res=cpl_image_duplicate(ima);
01006
01007
01008 shift=cpl_image_duplicate(ima);
01009 cpl_image_shift(shift,1,0);
01010 check(xsh_image_coadd(&res,shift));
01011 xsh_free_image(&shift);
01012
01013
01014 shift=cpl_image_duplicate(ima);
01015 cpl_image_shift(shift,-1,0);
01016 check(xsh_image_coadd(&res,shift));
01017 xsh_free_image(&shift);
01018
01019
01020 shift=cpl_image_duplicate(ima);
01021 cpl_image_shift(shift,0,-1);
01022 check(xsh_image_coadd(&res,shift));
01023 xsh_free_image(&shift);
01024
01025
01026 shift=cpl_image_duplicate(ima);
01027 cpl_image_shift(shift,0,1);
01028 check(xsh_image_coadd(&res,shift));
01029 xsh_free_image(&shift);
01030
01031
01032 cleanup:
01033 return res;
01034
01035 }
01036
01037
01038
01039