X-shooter Pipeline Reference Manual 3.8.15
xsh_badpixelmap.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2013-07-24 12:15:12 $
23 * $Revision: 1.83 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*-------------------------------------------------------------------------*/
44/*----------------------------------------------------------------------------*/
45
48/*-----------------------------------------------------------------------------
49 Includes
50 -----------------------------------------------------------------------------*/
51
52#include <xsh_dfs.h>
53#include <xsh_utils.h>
54#include <xsh_data_pre.h>
55#include <xsh_qc_definition.h>
56#include <xsh_error.h>
57#include <xsh_msg.h>
58#include <xsh_ksigma_clip.h>
59#include <cpl.h>
60#include <string.h>
61
62#include <xsh_badpixelmap.h>
63#include <xsh_detmon.h>
64#include <xsh_pfits.h>
65
66/*-----------------------------------------------------------------------------
67 Function prototypes
68 -----------------------------------------------------------------------------*/
69
70/*-----------------------------------------------------------------------------
71 Functions
72 -----------------------------------------------------------------------------*/
73
80void xsh_badpixelmap_or( xsh_pre *self, const xsh_pre *right )
81{
82 int * qual0 = NULL,
83 * qual1 = NULL ;
84 int i ;
85
86 XSH_ASSURE_NOT_NULL( self ) ;
87 XSH_ASSURE_NOT_NULL( right ) ;
88 check(qual0 = cpl_image_get_data_int(right->qual));
89 check(qual1 = cpl_image_get_data_int(self->qual));
90 XSH_ASSURE_NOT_ILLEGAL(right->nx == self->nx);
91 XSH_ASSURE_NOT_ILLEGAL(right->ny == self->ny );
92
93 for(i=0;i<self->nx*self->ny;i++) {
94 qual1[i] |= qual0[i] ;
95 }
96
97 cleanup:
98 return ;
99}
100
108cpl_frame*
110
111 const char* fname=NULL;
112 cpl_propertylist* plist=NULL;
113 cpl_image* image=NULL;
114 char bname[40];
115 char btag[40];
116 cpl_frame* master=NULL;
117 master=cpl_frame_duplicate(bpmap);
118 fname=cpl_frame_get_filename(bpmap);
119 plist=cpl_propertylist_load(fname,0);
120 image=cpl_image_load(fname,CPL_TYPE_INT,0,0);
121 sprintf(btag,"%s_%s",XSH_MASTER_BP_MAP_FLAT,xsh_instrument_arm_tostring(inst));
122 sprintf(bname,"%s.fits",btag);
123 xsh_pfits_set_pcatg(plist,btag);
124 cpl_image_save(image,bname,XSH_PRE_QUAL_BPP,plist,CPL_IO_DEFAULT);
125 cpl_frame_set_filename(master,bname);
126 cpl_frame_set_tag(master,btag);
128
129 xsh_free_propertylist(&plist);
130 xsh_free_image(&image);
131 return master;
132}
133
134
135static cpl_error_code xsh_flag_ima_bad_pix(cpl_image** image, int* bppix,const int decode_bp) {
136 int iy = 0;
137 int ix = 0;
138 int nx = 0;
139 int ny = 0;
140 int iy_nx = 0;
141
142 nx = cpl_image_get_size_x(*image);
143 ny = cpl_image_get_size_y(*image);
144
145 /* Set the bad pixel map array for the image */
146 for (iy = 0; iy < ny; iy++) {
147 iy_nx=iy*nx;
148 for (ix = 0; ix < nx; ix++) {
149 int j = ix + iy_nx;
150 if ((bppix[j] & decode_bp) > 0 ) {
151 cpl_image_reject(*image, ix + 1, iy + 1);
152 }
153 }
154 }
155 return cpl_error_get_code();
156}
157
158
159/*----------------------------------------------------------------------------*/
177/*----------------------------------------------------------------------------*/
178cpl_mask * xsh_bpm_filter(
179 const cpl_mask * input_mask,
180 cpl_size kernel_nx,
181 cpl_size kernel_ny,
182 cpl_filter_mode filter)
183{
184 cpl_mask * kernel = NULL;
185 cpl_mask * filtered_mask = NULL;
186 cpl_mask * expanded_mask = NULL;
187 cpl_mask * expanded_filtered_mask = NULL;
188
189 /* Check Entries */
190 cpl_ensure(input_mask != NULL, CPL_ERROR_NULL_INPUT, NULL);
191 cpl_ensure(kernel_nx >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
192 cpl_ensure(kernel_ny >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
193 cpl_ensure(filter == CPL_FILTER_EROSION || filter == CPL_FILTER_DILATION ||
194 filter == CPL_FILTER_OPENING || filter == CPL_FILTER_CLOSING,
195 CPL_ERROR_ILLEGAL_INPUT, NULL);
196
197 /* Only odd-sized masks allowed */
198 cpl_ensure((kernel_nx&1) == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
199 cpl_ensure((kernel_ny&1) == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
200
201 kernel = cpl_mask_new(kernel_nx, kernel_ny);
202 cpl_mask_not(kernel); /* All values set to unity*/
203
204 /* Enlarge the original mask with the kernel size and assume that outside
205 * all pixels are good */
206 expanded_mask = cpl_mask_new(
207 cpl_mask_get_size_x(input_mask) + 2 * kernel_nx,
208 cpl_mask_get_size_y(input_mask) + 2 * kernel_ny);
209
210 cpl_mask_copy(expanded_mask, input_mask, kernel_nx + 1, kernel_ny +1 );
211
212 expanded_filtered_mask = cpl_mask_new(cpl_mask_get_size_x(expanded_mask),
213 cpl_mask_get_size_y(expanded_mask));
214
215 if(cpl_mask_filter(expanded_filtered_mask, expanded_mask, kernel, filter,
216 CPL_BORDER_ZERO) != CPL_ERROR_NONE) {
217
218 cpl_mask_delete(kernel);
219 cpl_mask_delete(expanded_filtered_mask);
220 cpl_mask_delete(expanded_mask);
221 return NULL;
222 }
223
224 /* Extract the original mask from the expanded mask */
225 filtered_mask = cpl_mask_extract(expanded_filtered_mask,
226 kernel_nx+1, kernel_ny + 1,
227 cpl_mask_get_size_x(input_mask) + kernel_nx,
228 cpl_mask_get_size_y(input_mask) + kernel_ny);
229
230
231 /* Free memory */
232 cpl_mask_delete(kernel);
233 cpl_mask_delete(expanded_filtered_mask);
234 cpl_mask_delete(expanded_mask);
235
236 return filtered_mask;
237}
238
247cpl_mask*
248xsh_code_is_in_qual(cpl_image * qual, const int code) {
249 cpl_mask *mask = NULL;
250
251 assure( qual != NULL, CPL_ERROR_NULL_INPUT,
252 "QUAL input is NULL pointer" );
253 cpl_size nx=cpl_image_get_size_x(qual);
254 cpl_size ny=cpl_image_get_size_y(qual);
255
256 mask=cpl_mask_new(nx,ny);
257 int* pqual=cpl_image_get_data_int(qual);
258 cpl_binary* pmask=cpl_mask_get_data(mask);
259 int size=nx*ny;
260 for(int i=0;i<size;i++){
261 if( (pqual[i] & code) ==0 ) {
262 /* if pixel is bad flag it in table */
263 pmask[i]=CPL_BINARY_1;
264 }
265 }
266 cleanup:
267 return mask;
268}
269
278cpl_mask*
279xsh_qual_to_cpl_mask(cpl_image * qual, const int decode_bp) {
280 cpl_mask *mask = NULL;
281
282 assure( qual != NULL, CPL_ERROR_NULL_INPUT,
283 "QUAL input is NULL pointer" );
284 cpl_size nx=cpl_image_get_size_x(qual);
285 cpl_size ny=cpl_image_get_size_y(qual);
286
287 mask=cpl_mask_new(nx,ny);
288 int* pqual=cpl_image_get_data_int(qual);
289 cpl_binary* pmask=cpl_mask_get_data(mask);
290 int size=nx*ny;
291 for(int i=0;i<size;i++){
292 if( (pqual[i] & decode_bp) >0 ) {
293 /* if pixel is bad flag it in table */
294 pmask[i]=CPL_BINARY_1;
295 }
296 }
297 cleanup:
298 return mask;
299}
300
301
310void xsh_set_image_cpl_bpmap(cpl_image * image, cpl_image * bpmap,const int decode_bp) {
311 int *bppix = NULL;
312
313 assure( bpmap != NULL, CPL_ERROR_NULL_INPUT,
314 "BpMap is NULL pointer" );
315
316 check(bppix = cpl_image_get_data_int (bpmap));
317 /* Set the bad pixel map array for the image */
318 xsh_flag_ima_bad_pix(&image,bppix,decode_bp);
319
320 cleanup: return;
321}
322
323void xsh_image_flag_bp(cpl_image * image,cpl_image * bpmap, xsh_instrument* inst)
324{
325 cpl_mask* mask=NULL;
326 int nx=0;
327 int ny=0;
328 int j_nx=0;
329 int pix=0;
330 int i=0;
331 int j=0;
332
333
334 cpl_binary* pmsk=0;
335 int* pmap=0;
336
337 /* TODO use decode_bp to flag only certain pixels */
338 nx=cpl_image_get_size_x(image);
339 ny=cpl_image_get_size_y(image);
340 mask=cpl_mask_new(nx,ny);
341 pmsk=cpl_mask_get_data(mask);
342 pmap=cpl_image_get_data_int(bpmap);
343 for(j=0;j<ny;j++) {
344 j_nx=j*nx;
345 for(i=0;i<nx;i++) {
346 pix=j_nx+i;
347 if( (inst->decode_bp & pmap[pix]) > 0) {
348 pmsk[pix]=CPL_BINARY_1;
349 }
350 }
351 }
352
353 cpl_image_reject_from_mask(image,mask);
354
355 xsh_free_mask(&mask);
356 return;
357}
358
359void xsh_bpmap_bitwise_to_flag(cpl_image * bpmap,int flag )
360{
361 float* data = NULL;
362 int nx=0;
363 int ny=0;
364 int i=0;
365
366 check(nx = cpl_image_get_size_x(bpmap));
367 check(ny = cpl_image_get_size_y(bpmap));
368
369 check(data = cpl_image_get_data_float(bpmap));
370
371 for(i=0;i<nx*ny;i++){
372 if(data[i]){
373 data[i] = flag;
374 }
375 }
376
377 cleanup:
378 return;
379
380
381}
382
383
384void xsh_bpmap_mask_bad_pixel(cpl_image * bpmap, cpl_mask* mask,
385 int flag )
386{
387 int* data = NULL;
388 cpl_mask* old = NULL;
389 cpl_binary* maskdata = NULL;
390 int i = 0, nx = 0, ny = 0;
391
392 /* check parameters */
393 assure(bpmap != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
394 assure(mask != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
395
396 /* get size */
397 check(nx = cpl_image_get_size_x(bpmap));
398 check(ny = cpl_image_get_size_y(bpmap));
399 assure(nx == cpl_mask_get_size_x(mask),CPL_ERROR_ILLEGAL_INPUT,
400 "mask %" CPL_SIZE_FORMAT " and image %d must have same size in x",
401 cpl_mask_get_size_x(mask),nx);
402 assure(ny == cpl_mask_get_size_y(mask),CPL_ERROR_ILLEGAL_INPUT,
403 "mask %" CPL_SIZE_FORMAT " and image %d must have same size in y",
404 cpl_mask_get_size_y(mask),ny);
405
406 /* get bpmap mask */
407 check( old = cpl_image_get_bpm(bpmap));
408
409 /* get mask data */
410 check(maskdata = cpl_mask_get_data(mask));
411
412 /* get bpmap data */
413 check(data = cpl_image_get_data(bpmap));
414 /* apply mask to image */
415 for(i=0;i<nx*ny;i++){
416 if(maskdata[i]){
417 data[i] |= flag;
418 }
419 }
420
421 /* merge the two mask with OR*/
422 check(cpl_mask_or(old,mask));
423
424 cleanup:
425 return;
426}
427
428void
429xsh_bpmap_set_bad_pixel (cpl_image * bpmap, int ix, int iy, int flag)
430{
431 cpl_image_set (bpmap, ix, iy, (double) flag);
432 cpl_image_reject (bpmap, ix, iy);
433}
434
443cpl_image* xsh_bpmap_collapse_bpmap_create (cpl_imagelist * liste,const int decode_bp)
444{
445 // from a list of bpmap images, create one image, the union of all the bpmap
446 // images
447
448 cpl_image *bpmap = NULL;
449 int* bppix = NULL;
450 int nx, ny, npix;
451 int nimg, i;
452 cpl_image *result = NULL;
453
454 xsh_msg( "---> xsh_bpmap_collapse_bpmap_create" ) ;
455 bpmap = cpl_image_duplicate (cpl_imagelist_get (liste, 0));
456 assure (bpmap != NULL, cpl_error_get_code (), "Cant duplicate first bpmap");
457
458 bppix = cpl_image_get_data_int (bpmap);
459 assure (bppix != NULL, cpl_error_get_code (), "Cant get data int");
460
461 nimg = cpl_imagelist_get_size (liste);
462
463 nx = cpl_image_get_size_x (bpmap);
464 ny = cpl_image_get_size_y (bpmap);
465 npix = nx * ny;
466 xsh_msg_dbg_low ("Nb of bpmap: %d, nx: %d, ny: %d [%d]", nimg, nx, ny, npix);
467
468 for (i = 1; i < nimg; i++) {
469 int j = 0;
470 cpl_image *current = NULL;
471 int *curpix = NULL;
472
473 current = cpl_imagelist_get (liste, i);
474 assure (current != NULL, cpl_error_get_code (), "Cant get bpmap #%d", i);
475
476 curpix = cpl_image_get_data_int (current);
477 assure (curpix != NULL, cpl_error_get_code (),
478 "Cant get data int for bpmap #%d", i);
479
480 for (j = 0; j < npix; j++)
481 bppix[j] |= curpix[j];
482 }
483
484 result = cpl_image_wrap_int (nx, ny, bppix);
485 assure (result != NULL, cpl_error_get_code (), "Cant wrap final bpmap");
486 /* Set the bad pixel map array for the image */
487 xsh_flag_ima_bad_pix(&result,bppix,decode_bp);
488
489 return result;
490
491cleanup:
492 return NULL;
493}
494
504int
505xsh_bpmap_count (cpl_image * bpmap, int nx, int ny)
506{
507 int *pixbuf;
508 int i, count = 0, npix = nx * ny;
509
510 pixbuf = cpl_image_get_data_int (bpmap);
511 assure (pixbuf != NULL, cpl_error_get_code (), "Cant get pixel buffer");
512
513 for (i = 0; i < npix; i++) {
514 if (*(pixbuf + i) != 0)
515 count++;
516 }
517
518cleanup:
519 return count;
520}
521
531void xsh_bpmap_collapse_median(cpl_image* median, cpl_imagelist * list,
532 cpl_mask * bpmap)
533{
534 int nx, ny, nimg, i;
535 cpl_image **pimg = NULL;
536 cpl_vector *pixvector = NULL;
537 double* data = NULL;
538 cpl_binary* mask_data = NULL;
539 int ix, iy, k;
540
541 XSH_ASSURE_NOT_NULL(median);
543 XSH_ASSURE_NOT_NULL(bpmap);
544
545 check(nx = cpl_image_get_size_x(median));
546 check(ny = cpl_image_get_size_y(median));
547 check(nimg = cpl_imagelist_get_size(list));
548
549 /* create the array of pointers to images in list */
550 XSH_CALLOC(pimg, cpl_image*, nimg);
551
552 /* Populate images pointer array */
553 for (i = 0; i < nimg; i++){
554 *(pimg + i) = cpl_imagelist_get (list, i);
555 assure(cpl_image_get_size_x(*(pimg + i)) == nx,CPL_ERROR_ILLEGAL_INPUT,
556 " data list x size is not equal to median frame");
557 assure(cpl_image_get_size_y(*(pimg + i)) == ny,CPL_ERROR_ILLEGAL_INPUT,
558 " data list y size is not equal to median frame");
559 }
560
561 /* create pixvector Vector */
562 XSH_MALLOC(data, double, nimg);
563 check(mask_data = cpl_mask_get_data(bpmap));
564 /* Loop over all pixels */
565 for (iy = 1; iy <= ny; iy++) {
566// xsh_msg("iy %d / %d",iy,ny);
567 for (ix = 1; ix <= nx; ix++) {
568 int count = 0;
569 /* pixel need to be compute */
570 if (mask_data[(iy-1)*nx+ix-1] == CPL_BINARY_0){
571 for (k = 0; k < nimg; k++) {
572 int rej;
573 double val;
574
575 check (val = cpl_image_get (pimg[k], ix, iy, &rej) ) ;
576 if (rej == 0) {
577 data[count] = val;
578 count++;
579 }
580 }
581 }
582 /* now get median of pixvector and set the value in the median */
583 if (count) {
584 double med = 0.0;
585
586 check(pixvector = cpl_vector_wrap(count,data));
587 check(med = cpl_vector_get_median( pixvector));
588 check(cpl_image_set(median, ix, iy, med));
589 check(xsh_unwrap_vector(&pixvector));
590
591 }
592 }
593 }
594 cleanup:
595 if (cpl_error_get_code() != CPL_ERROR_NONE){
596 xsh_unwrap_vector (&pixvector);
597 }
598 XSH_FREE(data);
599 XSH_FREE(pimg) ;
600 return;
601}
602
612void xsh_bpmap_collapse_mean(cpl_image * mean, cpl_imagelist * list,
613 cpl_mask * bpmap)
614{
615 int nx, ny, nimg, i;
616 cpl_image **pimg = NULL;
617 cpl_vector *pixvector = NULL;
618 double* data = NULL;
619 cpl_binary* mask_data = NULL;
620 int ix, iy, k;
621
624 XSH_ASSURE_NOT_NULL(bpmap);
625
626 check(nx = cpl_image_get_size_x(mean));
627 check(ny = cpl_image_get_size_y(mean));
628 check(nimg = cpl_imagelist_get_size(list));
629
630 /* create the array of pointers to images in list */
631 XSH_CALLOC(pimg, cpl_image*, nimg);
632
633 /* Populate images pointer array */
634 for (i = 0; i < nimg; i++){
635 *(pimg + i) = cpl_imagelist_get (list, i);
636 assure(cpl_image_get_size_x(*(pimg + i)) == nx,CPL_ERROR_ILLEGAL_INPUT,
637 " data list x size is not equal to median frame");
638 assure(cpl_image_get_size_y(*(pimg + i)) == ny,CPL_ERROR_ILLEGAL_INPUT,
639 " data list y size is not equal to median frame");
640 }
641
642 /* create pixvector Vector */
643 XSH_MALLOC(data, double, nimg);
644 check(mask_data = cpl_mask_get_data(bpmap));
645 /* Loop over all pixels */
646 for (iy = 1; iy <= ny; iy++) {
647// xsh_msg("iy %d / %d",iy,ny);
648 for (ix = 1; ix <= nx; ix++) {
649 int count = 0;
650 /* pixel need to be compute */
651 if (mask_data[(iy-1)*nx+ix-1] == CPL_BINARY_0){
652 for (k = 0; k < nimg; k++) {
653 int rej;
654 double val;
655
656 check (val = cpl_image_get (pimg[k], ix, iy, &rej) ) ;
657 if (rej == 0) {
658 data[count] = val;
659 count++;
660 }
661 }
662 }
663 /* now get avg of pixvector and set the value into the mean */
664 if (count) {
665 double avg = 0.0;
666
667 check(pixvector = cpl_vector_wrap(count,data));
668 check(avg = cpl_vector_get_mean( pixvector));
669 check(cpl_image_set(mean, ix, iy, avg ));
670 check(xsh_unwrap_vector(&pixvector));
671
672 }
673 }
674 }
675 cleanup:
676 if (cpl_error_get_code() != CPL_ERROR_NONE){
677 xsh_unwrap_vector (&pixvector);
678 }
679 XSH_FREE(data);
680 XSH_FREE(pimg) ;
681 return;
682}
683
691cpl_error_code
692xsh_badpixel_flag_rejected(cpl_image* qual, cpl_image* image)
693{
694 assure( CPL_TYPE_INT == cpl_image_get_type(qual), cpl_error_get_code(),
695 "wrong ima qual data type");
696 int nx = cpl_image_get_size_x(qual);
697 int ny = cpl_image_get_size_y(qual);
698 //xsh_msg("nx=%d ny=%d",nx,ny);
699 //nx = cpl_image_get_size_x(image);
700 //ny = cpl_image_get_size_y(image);
701 //xsh_msg("nx=%d ny=%d",nx,ny);
702
703 int* pqual=cpl_image_get_data_int(qual);
704 cpl_mask* bpm=cpl_image_get_bpm(image);
705 cpl_binary* pbpm=cpl_mask_get_data(bpm);
706 //xsh_msg("Bitwise OR combine");
707 for(int j=0;j<ny;j++) {
708 int j_nx=j*nx;
709 for(int i=0;i<nx;i++) {
710 if( pbpm[j_nx+i] == CPL_BINARY_1 ) {
711
712 pqual[j_nx+i] |= QFLAG_ALL_PIX_BAD;
713 }
714 }
715 }
716 cleanup:
717 return cpl_error_get_code();
718}
719
727cpl_error_code
728xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right, const int mode )
729{
730 int nx = 0, ny = 0;
731 int* pself=NULL;
732 const int* pright=NULL;
733 int i=0;
734 int j=0;
735 int j_nx=0;
736
737 /* get size */
738 check(nx = cpl_image_get_size_x(*self));
739 check(ny = cpl_image_get_size_y(*self));
740 assure(nx == cpl_image_get_size_x(right),CPL_ERROR_ILLEGAL_INPUT,
741 "addendum mask %" CPL_SIZE_FORMAT " and original mask %d must have same size in x",
742 cpl_image_get_size_x(right),nx);
743 assure(ny == cpl_image_get_size_y(right),CPL_ERROR_ILLEGAL_INPUT,
744 "addendum mask %" CPL_SIZE_FORMAT " and original mask %d must have same size in y",
745 cpl_image_get_size_y(right),ny);
746
747 /* get bpmap mask */
748 pself=cpl_image_get_data_int(*self);
749 pright=cpl_image_get_data_int_const(right);
750 if(mode) {
751 //xsh_msg("Bitwise OR combine");
752 for(j=0;j<ny;j++) {
753 j_nx=j*nx;
754 for(i=0;i<nx;i++) {
755
756 pself[j_nx+i]|=pright[j_nx+i];
757
758 }
759 }
760 } else {
761 //xsh_msg("Bitwise AND combine");
762 for(j=0;j<ny;j++) {
763 j_nx=j*nx;
764 for(i=0;i<nx;i++) {
765
766 pself[j_nx+i]&=pright[j_nx+i];
767 }
768 }
769 }
770 cleanup:
771
772 return cpl_error_get_code();
773
774}
775
776
777
778
785cpl_error_code
786xsh_badpixelmap_coadd(cpl_frame *self, const cpl_frame *right,const int mode )
787{
788
789
790 const char* self_name=NULL;
791 const char* right_name=NULL;
792 cpl_image* map_self=NULL;
793 cpl_image* map_right=NULL;
794 cpl_propertylist* plist=NULL;
795
796
797 /* check parameters */
798 assure(self != NULL, CPL_ERROR_NULL_INPUT,"BpMap is NULL pointer" ) ;
799 assure(right != NULL, CPL_ERROR_NULL_INPUT,"mask is NULL pointer" ) ;
800
801 check(self_name=cpl_frame_get_filename(self));
802 check(right_name=cpl_frame_get_filename(right));
803 //xsh_msg("self_name=%s",self_name);
804 check(plist=cpl_propertylist_load(self_name,0));
805
806 check(map_self=cpl_image_load(self_name,CPL_TYPE_INT,0,0));
807 check(map_right=cpl_image_load(right_name,CPL_TYPE_INT,0,0));
808 xsh_msg("Bit-wise OR of %s with %s frame",
809 cpl_frame_get_tag(self),cpl_frame_get_tag(right));
810
811 check(xsh_badpixelmap_image_coadd(&map_self,map_right,mode));
812
813 /*
814 check(cpl_image_save(map_self,"self.fits",CPL_BPP_IEEE_FLOAT,
815 plist,CPL_IO_DEFAULT));
816
817 check(cpl_image_save(map_right,"right.fits",CPL_BPP_IEEE_FLOAT,
818 plist,CPL_IO_DEFAULT));
819 xsh_msg("self name=%s",self_name);
820 */
821 check(cpl_image_save(map_self,"BP_COMBINE.fits",CPL_BPP_IEEE_FLOAT,
822 plist,CPL_IO_DEFAULT));
823 cpl_frame_set_filename(self,"BP_COMBINE.fits");
824 xsh_add_temporary_file("BP_COMBINE.fits");
825
826 cleanup:
827
828 xsh_free_propertylist(&plist);
829 xsh_free_image(&map_self);
830 xsh_free_image(&map_right);
831
832 return cpl_error_get_code();
833
834}
835
836
843cpl_error_code
844xsh_frame_qual_update(cpl_frame *frame, const cpl_frame *bpmap,xsh_instrument* inst)
845{
846 xsh_pre* pre=NULL;
847 const char* frame_name=NULL;
848 const char* bpmap_name=NULL;
849 const char* frame_tag=NULL;
850
851 cpl_image* map_self=NULL;
852 cpl_image* map_right=NULL;
853 cpl_frame* product=NULL;
854 const int mode_or=1;
855 assure(frame != NULL, CPL_ERROR_NULL_INPUT,"INPUT frame is NULL pointer" ) ;
856 assure(bpmap != NULL, CPL_ERROR_NULL_INPUT,"BP MAP frame is NULL pointer" ) ;
857
858 check(frame_name=cpl_frame_get_filename(frame));
859 check(frame_tag=cpl_frame_get_tag(frame));
860 check(bpmap_name=cpl_frame_get_filename(bpmap));
861
862 check(pre=xsh_pre_load(frame,inst));
863 check(map_right=cpl_image_load(bpmap_name,CPL_TYPE_INT,0,0));
864 xsh_badpixelmap_image_coadd(&(pre->qual), map_right,mode_or);
865 check(product=xsh_pre_save(pre,frame_name,frame_tag,0));
866
867 cleanup:
868 xsh_pre_free(&pre);
869 xsh_free_image(&map_self);
870 xsh_free_image(&map_right);
871 xsh_free_frame(&product);
872 return cpl_error_get_code();
873
874}
875
876/*-------------------------------------------------------------------------*/
877/*
878 * @brief Get cold bpixels mask
879 * @param master Input image
880 * @param ks_low ref level
881 * @param ks_high ref level
882 * @param ks_iter number of iterations
883 * @param bpmhot hot pixels map
884 * @param hotpix_nb number of hot pixels
885 * @param bpmcold cold pixels map
886 * @param coldpix_nb number of hot pixels
887 * @param hplist cold pix header
888 * @param cplist cold pix header
889
890 * @return CPL_ERROR_NONE on success.
891 */
892/*-------------------------------------------------------------------------*/
893
894static cpl_error_code
895xsh_image_get_hot_cold_maps(cpl_image* masterbias,
896 const double kappa_low,
897 const double kappa_high,
898 const int low_niter,
899 const int high_niter,
900 cpl_mask** bpmhot,
901 int *hotpix_nb,
902 cpl_mask** bpmcold,
903 int *coldpix_nb,
904 cpl_propertylist** hplist,
905 cpl_propertylist** cplist)
906{
907
908
909 double lower=0;
910 double upper=0;
911 double dark_med=0;
912 double stdev=0;
913 double mean=0;
914
915 check(xsh_ksigma_clip(masterbias, 1, 1,
916 cpl_image_get_size_x(masterbias),
917 cpl_image_get_size_y(masterbias),
918 kappa_low,
919 low_niter, 1e-5,
920 &mean, &stdev));
921
922 check(cpl_propertylist_append_double(*cplist,XSH_QC_MASTER_MEAN,mean));
923 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
924
925 check(cpl_propertylist_append_double(*cplist, XSH_QC_MASTER_RMS,stdev));
926 check(cpl_propertylist_set_comment(*cplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
927
928 cpl_image_accept_all(masterbias);
929
930 /* Compute median-rms of the central part of the dark */
931 check(dark_med = cpl_image_get_median(masterbias));
932
933
934 lower = dark_med - stdev * kappa_low;
935 //upper = dark_med + stdev * kappa_low;
936 xsh_msg_dbg_low("dark_med=%f lower=%f",dark_med,lower);
937
938
939 check_msg(*bpmcold = cpl_mask_threshold_image_create(masterbias,
940 -FLT_MAX,lower),
941 "Cannot compute the cold pixel map");
942
943
944 if(*bpmcold!=NULL) {
945 check(*coldpix_nb = cpl_mask_count(*bpmcold));
946 } else {
947 *coldpix_nb=0;
948 }
949 check(cpl_propertylist_append_int(*cplist, XSH_QC_COLD_PIX_NUM,*coldpix_nb));
950 check(cpl_propertylist_set_comment(*cplist,XSH_QC_COLD_PIX_NUM,XSH_QC_COLD_PIX_NUM_C));
951
952/* Hot pixels */
953
954 check(xsh_ksigma_clip(masterbias, 1, 1,
955 cpl_image_get_size_x(masterbias),
956 cpl_image_get_size_y(masterbias),
957 kappa_high,
958 high_niter, 1e-5,
959 &mean, &stdev));
960
961
962
963 check(cpl_propertylist_append_double(*hplist, XSH_QC_MASTER_MEAN,mean));
964 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_MEAN,XSH_QC_MASTER_MEAN_C));
965 check(cpl_propertylist_append_double(*hplist,XSH_QC_MASTER_RMS,stdev));
966 check(cpl_propertylist_set_comment(*hplist,XSH_QC_MASTER_RMS,XSH_QC_MASTER_RMS_C));
967
968 cpl_image_accept_all(masterbias);
969
970
971 /* Compute median-rms of the central part of the dark */
972 check(dark_med = cpl_image_get_median(masterbias));
973
974 //lower = dark_med - stdev * kappa_high;
975 upper = dark_med + stdev * kappa_high;
976 xsh_msg_dbg_low("dark_med=%f upper=%f",dark_med,upper);
977
978 check_msg(*bpmhot = cpl_mask_threshold_image_create(masterbias,
979 upper,DBL_MAX),
980 "Cannot compute the hot pixel map");
981
982 if(*bpmhot!=NULL) {
983 check(*hotpix_nb = cpl_mask_count(*bpmhot));
984 } else {
985 *hotpix_nb=0;
986 }
987
988 check(cpl_propertylist_append_int(*hplist,XSH_QC_HOT_PIX_NUM,*hotpix_nb));
989 check(cpl_propertylist_set_comment(*hplist,XSH_QC_HOT_PIX_NUM,XSH_QC_HOT_PIX_NUM_C));
990
991
992 cleanup:
993 return cpl_error_get_code();
994
995}
996
997
998
999/*-------------------------------------------------------------------------*/
1000/*
1001 * @brief Get cold bpixels mask
1002 * @param master Input image
1003 * @param parameters recipe parameters
1004 * @param frameset recipe frameset
1005 * @return CPL_ERROR_NONE on success.
1006 */
1007/*-------------------------------------------------------------------------*/
1008cpl_error_code
1009xsh_image_get_hot_cold_pixs(cpl_frame* frame_image,
1011 const double ks_low,
1012 const int cold_niter,
1013 const double ks_high,
1014 const int hot_niter,
1015 cpl_frame** cpix_frm,
1016 cpl_frame** hpix_frm)
1017
1018{
1019
1020
1021 cpl_image* image=NULL;
1022
1023 cpl_mask* bpmcold=NULL;
1024 cpl_mask* bpmhot=NULL;
1025 int hotpix_nb=0;
1026 int coldpix_nb=0;
1027 cpl_image* cpix_ima=NULL;
1028 cpl_image* hpix_ima=NULL;
1029 cpl_propertylist* cplist=NULL;
1030 cpl_propertylist* hplist=NULL;
1031 cpl_propertylist* plist=NULL;
1032
1033
1034 const char* cpix_pro_catg=NULL;
1035 const char* hpix_pro_catg=NULL;
1036 char* cpix_name=NULL;
1037 char* hpix_name=NULL;
1038 const char* name=NULL;
1039
1040 check(name=cpl_frame_get_filename(frame_image));
1041
1042 check(image=cpl_image_load(cpl_frame_get_filename(frame_image),
1043 CPL_TYPE_FLOAT,0,0));
1044
1045
1046 check(hplist=cpl_propertylist_new());
1047 check(cplist=cpl_propertylist_new());
1048 xsh_msg("determine hot and cold pixels");
1050 ks_low,ks_high,cold_niter,hot_niter,
1051 &bpmhot,&hotpix_nb,
1052 &bpmcold,&coldpix_nb,&hplist,&cplist));
1053
1054
1055 xsh_msg("Hot pix nb=%d Cold pix nb=%d",hotpix_nb,coldpix_nb);
1056
1057
1058 check(cpix_ima=cpl_image_new_from_mask(bpmcold));
1059 //check(cpl_image_cast(cpix_ima,CPL_TYPE_FLOAT));
1060 check(cpl_image_multiply_scalar(cpix_ima,QFLAG_CAMERA_DEFECT));
1061
1062
1063 //check(xsh_bpmap_bitwise_to_flag(cpix_ima,QFLAG_CAMERA_DEFECT));
1064
1065 check(cpix_name= cpl_sprintf("%s_%s.fits",XSH_BP_MAP_CP,
1067
1068
1069 check(plist=cpl_propertylist_load(name,0));
1070 check(cpl_propertylist_append(plist,cplist));
1071 cpl_image_save(cpix_ima,cpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
1072 xsh_free_propertylist(&plist);
1073 xsh_free_propertylist(&cplist);
1074 //xsh_add_temporary_file(cpix_name);
1075
1076
1078
1079 check(*cpix_frm=xsh_frame_product(cpix_name,cpix_pro_catg,
1080 CPL_FRAME_TYPE_IMAGE,
1081 CPL_FRAME_GROUP_PRODUCT,
1082 CPL_FRAME_LEVEL_FINAL));
1083
1084
1085 check(hpix_ima=cpl_image_new_from_mask(bpmhot));
1086 //check(cpl_image_cast(hpix_ima,CPL_TYPE_FLOAT));
1087 check(cpl_image_multiply_scalar(hpix_ima,QFLAG_QUESTIONABLE_PIXEL));
1088
1089 //check(xsh_bpmap_bitwise_to_flag(cpix_ima,QFLAG_CAMERA_DEFECT));
1090
1091
1092 check(hpix_name=cpl_sprintf("%s_%s.fits",XSH_BP_MAP_HP,
1094
1095 check(plist=cpl_propertylist_load(name,0));
1096 check(cpl_propertylist_append(plist,hplist));
1097 cpl_image_save(hpix_ima,hpix_name,CPL_BPP_32_SIGNED,plist,CPL_IO_DEFAULT);
1098 xsh_free_propertylist(&plist);
1099 xsh_free_propertylist(&hplist);
1100 //xsh_add_temporary_file(hpix_name);
1101
1103 check(*hpix_frm=xsh_frame_product(hpix_name,hpix_pro_catg,
1104 CPL_FRAME_TYPE_IMAGE,
1105 CPL_FRAME_GROUP_PRODUCT,
1106 CPL_FRAME_LEVEL_FINAL));
1107
1108 cleanup:
1109 xsh_free_mask(&bpmcold);
1110 xsh_free_mask(&bpmhot);
1111
1112 xsh_free_image(&image);
1113 xsh_free_image(&cpix_ima);
1114 xsh_free_image(&hpix_ima);
1115 xsh_free_propertylist(&cplist);
1116 xsh_free_propertylist(&hplist);
1117 xsh_free_propertylist(&plist);
1118 XSH_FREE(hpix_name);
1119 XSH_FREE(cpix_name);
1120
1121 return cpl_error_get_code();
1122
1123}
1124/*-------------------------------------------------------------------------*/
1125/*
1126 * @brief Get cold bpixels mask
1127 * @param master Input image
1128 * @param parameters recipe parameters
1129 * @param frameset recipe frameset
1130 * @return mask frame on success.
1131 */
1132/*-------------------------------------------------------------------------*/
1133cpl_frame*
1135 const double kappa,
1136 const int r,
1137 xsh_instrument* instr)
1138
1139{
1140
1141 int nx=0;
1142 int ny=0;
1143 int i=0;
1144 int j=0;
1145 double med=0;
1146 double rms=0;
1147 double* pima=NULL;
1148 double* pmsk=NULL;
1149 cpl_image* msk=NULL;
1150 char name[256];
1151 char tag[256];
1152 cpl_frame* msk_frm=NULL;
1153
1154 nx=cpl_image_get_size_x(ima);
1155 ny=cpl_image_get_size_y(ima);
1156 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
1157
1158 pima=cpl_image_get_data_double(ima);
1159 pmsk=cpl_image_get_data_double(msk);
1160
1161 for(j=r;j<ny-r;j++) {
1162 for(i=r;i<nx-r;i++) {
1163 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
1164 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
1165 if(pima[i+j*nx]< med- kappa*rms) {
1166 pmsk[i+j*nx]=QFLAG_LOW_QE_PIXEL;
1167 }
1168 }
1169 }
1170
1171 sprintf(tag,"%s_%s",XSH_BP_MAP_DP,xsh_instrument_arm_tostring(instr));
1172 sprintf(name,"%s.fits",tag);
1173 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,
1174 CPL_IO_DEFAULT));
1175
1176 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
1177 CPL_FRAME_GROUP_PRODUCT,
1178 CPL_FRAME_LEVEL_FINAL));
1179
1180 cleanup:
1181 return msk_frm;
1182
1183}
1184
1185/*-------------------------------------------------------------------------*/
1186/*
1187 * @brief Get hot bpixels mask
1188 * @param master Input image
1189 * @param parameters recipe parameters
1190 * @param frameset recipe frameset
1191 * @return mask frame on success.
1192 */
1193/*-------------------------------------------------------------------------*/
1194cpl_frame*
1196 const double kappa,
1197 const int r,
1198 xsh_instrument* instr)
1199
1200{
1201
1202 int nx=0;
1203 int ny=0;
1204 int i=0;
1205 int j=0;
1206 double med=0;
1207 double rms=0;
1208 double* pima=NULL;
1209 double* pmsk=NULL;
1210 cpl_image* msk=NULL;
1211 char name[256];
1212 char tag[256];
1213 cpl_frame* msk_frm=NULL;
1214
1215 nx=cpl_image_get_size_x(ima);
1216 ny=cpl_image_get_size_y(ima);
1217 msk=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE);
1218
1219 pima=cpl_image_get_data_double(ima);
1220 pmsk=cpl_image_get_data_double(msk);
1221
1222 for(j=r;j<ny-r;j++) {
1223 for(i=r;i<nx-r;i++) {
1224 check(rms=cpl_image_get_stdev_window(ima,i-r+1,j-r+1,i+r,j+r));
1225 check(med=cpl_image_get_median_window(ima,i-r+1,j-r+1,i+r,j+r));
1226 if(pima[i+j*nx]> med+ kappa*rms) {
1227 pmsk[i+j*nx]=QFLAG_WELL_SATURATION;
1228 }
1229 }
1230 }
1231
1232 sprintf(tag,"%s_%s",XSH_BP_MAP_SP,xsh_instrument_arm_tostring(instr));
1233 sprintf(name,"%s.fits",tag);
1234 check(cpl_image_save(msk,name,XSH_PRE_DATA_BPP,NULL,CPL_IO_DEFAULT));
1235
1236 check(msk_frm=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
1237 CPL_FRAME_GROUP_PRODUCT,
1238 CPL_FRAME_LEVEL_FINAL));
1239
1240 cleanup:
1241 return msk_frm;
1242
1243}
1244
1245
1246/*-------------------------------------------------------------------------*/
1247/*
1248 * @brief Get cold bpixels mask
1249 * @param master Input image
1250 * @param parameters recipe parameters
1251 * @param frameset recipe frameset
1252 * @return CPL_ERROR_NONE on success.
1253 */
1254/*-------------------------------------------------------------------------*/
1255cpl_error_code
1256xsh_image_clean_mask_pixs(cpl_image** ima,cpl_image* msk,const int r)
1257
1258{
1259 int nx=0;
1260 int ny=0;
1261 int i=0;
1262 int j=0;
1263 double med=0;
1264 double* pima=NULL;
1265 double* pmsk=NULL;
1266
1267 nx=cpl_image_get_size_x(*ima);
1268 ny=cpl_image_get_size_y(*ima);
1269
1270 pima=cpl_image_get_data_double(*ima);
1271 pmsk=cpl_image_get_data_double(msk);
1272
1273 for(j=r;j<ny-r;j++) {
1274 for(i=r;i<nx-r;i++) {
1275 check(med=cpl_image_get_median_window(*ima,i-r+1,j-r+1,i+r,j+r));
1276 if(pmsk[i+j*nx]==1) {
1277 pima[i+j*nx]=med;
1278 }
1279 }
1280 }
1281
1282 cleanup:
1283 return cpl_error_get_code();
1284
1285}
1286
1287static cpl_error_code
1288xsh_image_coadd(cpl_image **self, const cpl_image *add )
1289{
1290 int nx1=0;
1291 int ny1=0;
1292 int nx2=0;
1293 int ny2=0;
1294 float* pself=NULL;
1295 const float* padd=NULL;
1296 int i=0;
1297
1298 XSH_ASSURE_NOT_NULL( self ) ;
1299 XSH_ASSURE_NOT_NULL( add ) ;
1300
1301 check(nx1=cpl_image_get_size_x(*self));
1302 check(ny1=cpl_image_get_size_y(*self));
1303
1304
1305 check(nx2=cpl_image_get_size_x(add));
1306 check(ny2=cpl_image_get_size_y(add));
1307
1308 pself=cpl_image_get_data_float(*self);
1309 padd=cpl_image_get_data_float_const(add);
1310
1311 if (nx1 != nx2 || ny1 != ny2) {
1312 xsh_msg("Input image of different size");
1313 }
1314 for(i=0;i<nx1*ny2;i++){
1315 if( (pself[i]==0) && (padd[i] !=0) ) {
1316 pself[i]+=padd[i];
1317 }
1318 }
1319 cleanup:
1320 return cpl_error_get_code();
1321
1322}
1323
1324cpl_image*
1326
1327{
1328
1329
1330 cpl_image* shift=NULL;
1331 cpl_image* res=NULL;
1332
1333
1334 res=cpl_image_duplicate(ima);
1335
1336 /* shift x+1 */
1337 shift=cpl_image_duplicate(ima);
1338 cpl_image_shift(shift,1,0);
1339 check(xsh_image_coadd(&res,shift));
1340 xsh_free_image(&shift);
1341
1342 /* shift x-1 */
1343 shift=cpl_image_duplicate(ima);
1344 cpl_image_shift(shift,-1,0);
1345 check(xsh_image_coadd(&res,shift));
1346 xsh_free_image(&shift);
1347
1348 /* shift y-1 */
1349 shift=cpl_image_duplicate(ima);
1350 cpl_image_shift(shift,0,-1);
1351 check(xsh_image_coadd(&res,shift));
1352 xsh_free_image(&shift);
1353
1354 /* shift y+1 */
1355 shift=cpl_image_duplicate(ima);
1356 cpl_image_shift(shift,0,1);
1357 check(xsh_image_coadd(&res,shift));
1358 xsh_free_image(&shift);
1359
1360
1361 cleanup:
1362 return res;
1363
1364}
1365
1366cpl_error_code
1367xsh_count_crh(xsh_pre* pre, xsh_instrument* instr,const int datancom)
1368{
1369 int* pqual=NULL;
1370 int size;
1371 int ncrh=0;
1372 int i=0;
1373
1374 float ncrh_avg=0;
1375 XSH_ASSURE_NOT_NULL_MSG(pre, "Null input pre frame");
1376 XSH_ASSURE_NOT_NULL_MSG(instr, "Null input pre frame");
1377 size=pre->nx*pre->ny;
1378
1379
1380 check(pqual=cpl_image_get_data_int(pre->qual));
1381 for(i=0;i<size;i++) {
1382 if(
1383 (QFLAG_COSMIC_RAY_REMOVED & pqual[i]) > 0 ||
1384 (QFLAG_COSMIC_RAY_UNREMOVED & pqual[i]) > 0
1385 )
1386 {
1387 (ncrh)++;
1388 }
1389 }
1390 xsh_msg("ncrh=%d",ncrh);
1392 xsh_msg("datancom=%d",datancom);
1393 ncrh_avg=ncrh/datancom;
1394 xsh_msg("ncrh=%f",ncrh_avg);
1396
1397 cleanup:
1398 return cpl_error_get_code();
1399}
1400
1401
1402cpl_error_code
1403xsh_count_satpix(xsh_pre* pre, xsh_instrument* instr,const int datancom)
1404{
1405 int* pqual=NULL;
1406 int size;
1407 int bp_code_sat=0;
1408 int nsat=0;
1409 int i=0;
1410
1411 float nsat_avg=0;
1412 XSH_ASSURE_NOT_NULL_MSG(pre, "Null input pre frame");
1413 XSH_ASSURE_NOT_NULL_MSG(instr, "Null input pre frame");
1414 size=pre->nx*pre->ny;
1415 bp_code_sat=QFLAG_ADC_SATURATION;
1416
1417 if(xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
1418 bp_code_sat=QFLAG_SATURATED_DATA;
1419 }
1420
1421 check(pqual=cpl_image_get_data_int(pre->qual));
1422 for(i=0;i<size;i++) {
1423 if( (bp_code_sat & pqual[i]) > 0 ) {
1424 (nsat)++;
1425 }
1426 }
1427 xsh_msg("nsat=%d",nsat);
1429 xsh_msg("datancom=%d",datancom);
1430 nsat_avg=nsat/datancom;
1431 nsat_avg=((float)nsat/size);
1432 xsh_msg("nsat=%f",nsat_avg);
1434
1435 cleanup:
1436 return cpl_error_get_code();
1437}
1438
1439cpl_error_code
1441 const double cor_val, const int flag,
1442 const int is_flat, int* nsat)
1443{
1444 //int size=0;
1445 int i=0;
1446 float* pima=NULL;
1447 int* pqual=NULL;
1448 int bp_code_sat=0;
1449 int bp_code_neg=0;
1450 double thresh_max=0;
1451 //double time=0;
1452 float cpima=0;
1453
1454 XSH_ASSURE_NOT_NULL_MSG(pre, "Null input pre frame");
1455 XSH_ASSURE_NOT_NULL_MSG(instr, "Null input pre frame");
1456 //size=pre->nx*pre->ny;
1457 /* define thresholds and corresponding codes */
1458 bp_code_sat=QFLAG_ADC_SATURATION;
1459 bp_code_neg=QFLAG_NEGATIVE_DATA;
1460 if(xsh_instrument_get_arm(instr) != XSH_ARM_NIR) {
1461 thresh_max=65000;
1462 } else {
1463 thresh_max=42000;
1464 bp_code_sat=QFLAG_SATURATED_DATA;
1465 }
1466
1467 check(pima=cpl_image_get_data_float(pre->data));
1468 check(pqual=cpl_image_get_data_int(pre->qual));
1469 int nx=pre->nx;
1470 int ny=pre->ny;
1471 int ipix=0;
1472 int j=0;
1473 int jnx=0;
1474 //xsh_msg("nx=%d ny=%d",nx,ny);
1475
1476 /* if the frame was overscan corrected we correct the threshold */
1477 thresh_max=thresh_max -cor_val;
1478 /* position of bottom left edge of NIR frame corresponding to K band */
1479 int knx=758;
1480 knx=1000;
1481 knx=nx; //removed special treatment of K band: PIPE-7075
1482 if(flag) {
1483 double thresh_min=1-cor_val;
1484 for(j=0;j<ny;j++) {
1485 jnx=j*nx;
1486 for(i=0;i<nx;i++) {
1487 ipix=i+jnx;
1488 //for(ipix=0;ipix<size;ipix++) {
1489 cpima=pima[ipix];
1490 //xsh_msg("ima val=%g",cpima);
1491 if( pima[ipix] > thresh_max) {
1492 pqual[ipix] |= bp_code_sat;
1493 (*nsat)++;
1494 }
1495 if ( cpima < thresh_min ) {
1496 pqual[ipix] |= bp_code_neg;
1497 }
1498 }
1499 }
1500 } else {
1501 for(j=0;j<ny;j++) {
1502 jnx=j*nx;
1503 for(i=0;i<knx;i++) {
1504 ipix=i+jnx;
1505 if( pima[ipix] > thresh_max) {
1506 (*nsat)++;
1507 }
1508 }
1509 }
1510 }
1511
1512
1513 cleanup:
1514 return cpl_error_get_code();
1515}
1516
1517cpl_error_code
1519 const double thresh_min,
1520 const double thresh_max,
1521 const double cor_val,
1522 int* nrange,
1523 double* frange)
1524{
1525 float* pdata=NULL;
1526 int i=0;
1527 int size=pre->nx*pre->ny;
1528
1529 /* Flag saturated pixels for QC */
1530 pdata=cpl_image_get_data_float(pre->data);
1531 for(i=0;i<size;i++) {
1532 if(pdata[i]>=(thresh_min-cor_val) &&
1533 pdata[i]<=(thresh_max-cor_val)) {
1534 (*nrange)++;
1535 }
1536 }
1537 *frange=(double)(*nrange)/(size);
1538 return cpl_error_get_code();
1539
1540}
1541
1542cpl_error_code
1543xsh_badpixelmap_count_sat_pixels(xsh_pre* pre,const double sat_thresh,
1544 const double cor_val, int* nsat,
1545 double* frac_sat)
1546{
1547 float* pdata=NULL;
1548 int i=0;
1549 int size=pre->nx*pre->ny;
1550
1551 /* Flag saturated pixels for QC */
1552 pdata=cpl_image_get_data_float(pre->data);
1553 for(i=0;i<size;i++) {
1554 if(pdata[i]>=(sat_thresh-cor_val) || pdata[i]==-cor_val) (*nsat)++;
1555 }
1556 *frac_sat=(double)(*nsat)/(size);
1557 return cpl_error_get_code();
1558
1559}
1560
1561
1562cpl_error_code
1564{
1565 const char* name=NULL;
1566 cpl_image* dima=NULL;
1567 cpl_image* eima=NULL;
1568 cpl_image* qima=NULL;
1569 cpl_propertylist* phead=NULL;
1570 cpl_propertylist* ehead=NULL;
1571 cpl_propertylist* qhead=NULL;
1572 int* pima=NULL;
1573 int npix=0;
1574 int i=0;
1575 int j=0;
1576
1577 int nx=0;
1578 int ny=0;
1579 int j_nx=0;
1580 int j_nx_i=0;
1581
1582 name=cpl_frame_get_filename(frm);
1583 dima=cpl_image_load(name,XSH_PRE_DATA_TYPE,0,0);
1584 eima=cpl_image_load(name,XSH_PRE_ERRS_TYPE,0,1);
1585 qima=cpl_image_load(name,XSH_PRE_QUAL_TYPE,0,2);
1586 phead=cpl_propertylist_load(name,0);
1587 ehead=cpl_propertylist_load(name,1);
1588 qhead=cpl_propertylist_load(name,2);
1589 pima=cpl_image_get_data_int(qima);
1590 nx=cpl_image_get_size_x(qima);
1591 ny=cpl_image_get_size_y(qima);
1592 if(cpl_propertylist_has(phead,XSH_QC_NHPIX)) {
1593 npix=xsh_pfits_get_qc_nhpix(phead);
1594 }
1595
1596
1597 for(j=1;j<ny-1;j++){
1598 j_nx = j*nx;
1599 for(i=1;i<nx-1;i++){
1600 j_nx_i = j_nx + i;
1601 if ((pima[j_nx_i] & QFLAG_ELECTRONIC_PICKUP) > 0) {continue;};
1602 if ((pima[j_nx_i - 1] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
1603 if ((pima[j_nx_i + 1] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
1604 if ((pima[j_nx_i - nx] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
1605 if ((pima[j_nx_i + nx] & QFLAG_ELECTRONIC_PICKUP) == 0) {continue;};
1606 pima[j_nx_i] |= QFLAG_ELECTRONIC_PICKUP;
1607 npix++;
1608 //xsh_msg("i=%d j=%d",i,j);
1609 }
1610 }
1611
1612 xsh_pfits_set_qc_nhpix(phead,npix);
1613 xsh_pfits_set_qc_noisepix(phead,npix);
1614
1615 cpl_image_save(dima,name,XSH_PRE_DATA_BPP,phead,CPL_IO_DEFAULT);
1616 cpl_image_save(eima,name,XSH_PRE_ERRS_BPP,ehead,CPL_IO_EXTEND);
1617 cpl_image_save(qima,name,XSH_PRE_QUAL_BPP,qhead,CPL_IO_EXTEND);
1618
1619
1620 xsh_free_image(&dima);
1621 xsh_free_image(&eima);
1622 xsh_free_image(&qima);
1623 xsh_free_propertylist(&phead);
1624 xsh_free_propertylist(&ehead);
1625 xsh_free_propertylist(&qhead);
1626
1627 return cpl_error_get_code();
1628}
1629/*---------------------------------------------------------------------------*/
1652cpl_frame* xsh_badpixelmap_extract(cpl_frame* frame, int xmin, int ymin,
1653 int xmax, int ymax)
1654{
1655 cpl_image* ima=NULL;
1656 cpl_image* ext=NULL;
1657 cpl_propertylist* plist=NULL;
1658 const char* fname=NULL;
1659 cpl_frame* result=NULL;
1660 char name_o[256];
1661
1662 XSH_ASSURE_NOT_NULL( frame);
1663
1664 result=cpl_frame_duplicate(frame);
1665 fname=cpl_frame_get_filename(frame);
1666 plist=cpl_propertylist_load(fname,0);
1667 ima=cpl_image_load(fname, XSH_PRE_DATA_BPP, 0,0);
1668 ext=cpl_image_extract(ima,xmin,ymin,xmax,ymax);
1669 sprintf(name_o,"SUB_%s",fname);
1670 cpl_image_save(ext, name_o, XSH_PRE_DATA_BPP,plist, CPL_IO_DEFAULT);
1671 check( cpl_frame_set_filename( result, name_o));
1672 xsh_add_temporary_file(name_o);
1673
1674 cleanup:
1675 if( cpl_error_get_code() != CPL_ERROR_NONE) {
1676 xsh_free_frame( &result);
1677 }
1678 xsh_free_image(&ima);
1679 xsh_free_image(&ext);
1680 xsh_free_propertylist(&plist);
1681
1682 return result;
1683
1684}
1685
static char mode[32]
static xsh_instrument * instrument
cpl_error_code xsh_count_crh(xsh_pre *pre, xsh_instrument *instr, const int datancom)
cpl_mask * xsh_qual_to_cpl_mask(cpl_image *qual, const int decode_bp)
void xsh_badpixelmap_or(xsh_pre *self, const xsh_pre *right)
cpl_mask * xsh_code_is_in_qual(cpl_image *qual, const int code)
cpl_error_code xsh_frame_qual_update(cpl_frame *frame, const cpl_frame *bpmap, xsh_instrument *inst)
static cpl_error_code xsh_image_get_hot_cold_maps(cpl_image *masterbias, const double kappa_low, const double kappa_high, const int low_niter, const int high_niter, cpl_mask **bpmhot, int *hotpix_nb, cpl_mask **bpmcold, int *coldpix_nb, cpl_propertylist **hplist, cpl_propertylist **cplist)
cpl_error_code xsh_image_clean_mask_pixs(cpl_image **ima, cpl_image *msk, const int r)
cpl_error_code xsh_image_get_hot_cold_pixs(cpl_frame *frame_image, xsh_instrument *instrument, const double ks_low, const int cold_niter, const double ks_high, const int hot_niter, cpl_frame **cpix_frm, cpl_frame **hpix_frm)
cpl_image * xsh_image_flag_bptype_with_crox(cpl_image *ima)
cpl_image * xsh_bpmap_collapse_bpmap_create(cpl_imagelist *liste, const int decode_bp)
cpl_error_code xsh_badpixelmap_flag_saturated_pixels(xsh_pre *pre, xsh_instrument *instr, const double cor_val, const int flag, const int is_flat, int *nsat)
cpl_mask * xsh_bpm_filter(const cpl_mask *input_mask, cpl_size kernel_nx, cpl_size kernel_ny, cpl_filter_mode filter)
Allows the growing and shrinking of bad pixel masks. It can be used to e.g. set pixels to bad if the ...
static cpl_error_code xsh_flag_ima_bad_pix(cpl_image **image, int *bppix, const int decode_bp)
void xsh_image_flag_bp(cpl_image *image, cpl_image *bpmap, xsh_instrument *inst)
cpl_error_code xsh_badpixelmap_count_sat_pixels(xsh_pre *pre, const double sat_thresh, const double cor_val, int *nsat, double *frac_sat)
void xsh_bpmap_collapse_mean(cpl_image *mean, cpl_imagelist *list, cpl_mask *bpmap)
static cpl_error_code xsh_image_coadd(cpl_image **self, const cpl_image *add)
void xsh_bpmap_collapse_median(cpl_image *median, cpl_imagelist *list, cpl_mask *bpmap)
void xsh_bpmap_bitwise_to_flag(cpl_image *bpmap, int flag)
int xsh_bpmap_count(cpl_image *bpmap, int nx, int ny)
cpl_error_code xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right, const int mode)
void xsh_bpmap_set_bad_pixel(cpl_image *bpmap, int ix, int iy, int flag)
cpl_frame * xsh_image_local_cold_pixs(cpl_image *ima, const double kappa, const int r, xsh_instrument *instr)
cpl_frame * xsh_badpixelmap_crea_master_from_bpmap(cpl_frame *bpmap, xsh_instrument *inst)
cpl_error_code xsh_badpixelmap_count_range_pixels(xsh_pre *pre, const double thresh_min, const double thresh_max, const double cor_val, int *nrange, double *frange)
cpl_error_code xsh_badpixelmap_coadd(cpl_frame *self, const cpl_frame *right, const int mode)
cpl_error_code xsh_count_satpix(xsh_pre *pre, xsh_instrument *instr, const int datancom)
void xsh_bpmap_mask_bad_pixel(cpl_image *bpmap, cpl_mask *mask, int flag)
void xsh_set_image_cpl_bpmap(cpl_image *image, cpl_image *bpmap, const int decode_bp)
cpl_frame * xsh_image_local_hot_pixs(cpl_image *ima, const double kappa, const int r, xsh_instrument *instr)
cpl_frame * xsh_badpixelmap_extract(cpl_frame *frame, int xmin, int ymin, int xmax, int ymax)
This function create a sub bad pixel map frame from input bad pixel map frame. the sub frame is descr...
cpl_error_code xsh_badpixelmap_fill_bp_pattern_holes(cpl_frame *frm)
cpl_error_code xsh_badpixel_flag_rejected(cpl_image *qual, cpl_image *image)
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
Definition: xsh_error.h:103
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
int size
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
void xsh_pfits_set_total_frac_sat(cpl_propertylist *plist, double value)
Write the fraction of saturated pixels value.
Definition: xsh_pfits.c:1138
void xsh_pfits_set_total_nsat(cpl_propertylist *plist, int value)
Write the total number of saturated pixels value.
Definition: xsh_pfits.c:1117
void xsh_pfits_set_qc_ncrh_mean(cpl_propertylist *plist, const double value)
Write the QC.NCRH.AVG value.
Definition: xsh_pfits_qc.c:771
int xsh_pfits_get_qc_nhpix(const cpl_propertylist *plist)
find out the QC.NHPIX value
Definition: xsh_pfits_qc.c:174
void xsh_pfits_set_qc_ncrh(cpl_propertylist *plist, int value)
Write the QC.NCRH value.
Definition: xsh_pfits_qc.c:754
void xsh_pfits_set_qc_nhpix(cpl_propertylist *plist, int value)
Write the QC.NHPIX value.
Definition: xsh_pfits_qc.c:703
void xsh_pfits_set_qc_noisepix(cpl_propertylist *plist, int value)
Write the QC.NHPIX value.
Definition: xsh_pfits_qc.c:720
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
Definition: xsh_utils.c:2345
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_mask(cpl_mask **m)
Deallocate an image mask and set the pointer to NULL.
Definition: xsh_utils.c:2149
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
cpl_image * qual
Definition: xsh_data_pre.h:71
cpl_image * data
Definition: xsh_data_pre.h:65
cpl_propertylist * data_header
Definition: xsh_data_pre.h:66
#define QFLAG_CAMERA_DEFECT
#define QFLAG_ADC_SATURATION
#define QFLAG_QUESTIONABLE_PIXEL
#define QFLAG_ELECTRONIC_PICKUP
#define QFLAG_WELL_SATURATION
#define QFLAG_COSMIC_RAY_REMOVED
#define QFLAG_ALL_PIX_BAD
#define QFLAG_SATURATED_DATA
#define QFLAG_NEGATIVE_DATA
#define QFLAG_LOW_QE_PIXEL
#define QFLAG_COSMIC_RAY_UNREMOVED
@ XSH_ARM_NIR
#define XSH_PRE_DATA_TYPE
Definition: xsh_data_pre.h:42
#define XSH_PRE_QUAL_TYPE
Definition: xsh_data_pre.h:46
#define XSH_PRE_ERRS_BPP
Definition: xsh_data_pre.h:45
#define XSH_PRE_QUAL_BPP
Definition: xsh_data_pre.h:47
#define XSH_PRE_ERRS_TYPE
Definition: xsh_data_pre.h:44
#define XSH_PRE_DATA_BPP
Definition: xsh_data_pre.h:43
static cpl_error_code TYPE_ADD() xsh_ksigma_clip(const CPL_TYPE *pi, cpl_binary *pm, int llx, int lly, int urx, int ury, int nx, double var_sum, int npixs, double kappa, int nclip, double tolerance, double *mean, double *stdev)
int nx
double kappa
Definition: xsh_detmon_lg.c:81
int ny
int filter
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
#define XSH_BP_MAP_HP
Definition: xsh_dfs.h:620
#define XSH_BP_MAP_SP
Definition: xsh_dfs.h:631
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_MASTER_BP_MAP_FLAT
Definition: xsh_dfs.h:674
#define XSH_BP_MAP_CP
Definition: xsh_dfs.h:625
#define XSH_BP_MAP_DP
Definition: xsh_dfs.h:637
#define XSH_QC_NHPIX
Definition: xsh_pfits_qc.h:122
#define XSH_QC_MASTER_RMS_C
#define XSH_QC_MASTER_MEAN
#define XSH_QC_MASTER_RMS
#define XSH_QC_HOT_PIX_NUM_C
#define XSH_QC_COLD_PIX_NUM
#define XSH_QC_COLD_PIX_NUM_C
#define XSH_QC_MASTER_MEAN_C
#define XSH_QC_HOT_PIX_NUM
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_MALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:49
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56