72 cpl_image * errs, cpl_mask * mask,
76 cpl_image **pimg = NULL;
77 cpl_image **perrs = NULL;
80 float* medvals = NULL;
81 float* mederrs = NULL;
82 cpl_binary *bpmdata = NULL;
86 check(nimg = cpl_imagelist_get_size (datalist));
89 pimg = cpl_malloc (nimg *
sizeof (cpl_image *));
90 assure (pimg != NULL, cpl_error_get_code (),
91 "Cant allocate memory for image pointers");
92 perrs = cpl_malloc (nimg *
sizeof (cpl_image *));
93 assure (pimg != NULL, cpl_error_get_code (),
94 "Cant allocate memory for image pointers");
99 for (i = 0; i < nimg; i++){
100 *(pimg + i) = cpl_imagelist_get (datalist, i);
101 *(perrs + i) = cpl_imagelist_get (errslist, i);
105 check(
nx = cpl_image_get_size_x (*pimg));
106 check(
ny = cpl_image_get_size_y (*pimg));
109 check(medvals = cpl_image_get_data_float(median));
110 check(mederrs = cpl_image_get_data_float(errs));
111 check(bpmdata = cpl_mask_get_data(mask));
113 for (iy = 1; iy <=
ny; iy++){
114 for (ix = 1; ix <=
nx; ix++) {
122 medval = medvals[ix-1+(iy-1)*
nx];
126 for (k = 0; k < nimg; k++) {
128 if (!bpmdata[ix-1+(iy-1)*
nx]) {
129 double delta = 0.0, kvalue = 0.0;
131 kvalue = cpl_image_get (*(pimg + k), ix, iy, &rej);
136 delta = fabs(kvalue - medval);
142 cpl_image_reject (*(pimg + k), ix, iy);
143 cpl_image_reject (*(perrs + k), ix, iy);
145 xsh_msg (
"CR Rejected for Pixel %d,%d [%d] (%lf vs %lf)",
146 ix, iy, k, medval, kvalue);
153 xsh_msg_dbg_high(
"Pixel %d,%d already known as bad pixel in image %d", ix, iy,
162 bpmdata[ix-1+(iy-1)*
nx] = CPL_BINARY_1;
186 double qc_crrate = 0.0;
199 (double) nbcrh / (((
double) nframes) * (pre->
exptime *
200 (pre->
pszx / 10000.0) * (pre->
pszy /10000.0) * pre->
nx * pre->
ny)) ;
201 nbcr_avg = (float)nbcrh/nframes ;
220 cpl_imagelist* dataList,
221 cpl_imagelist* errsList,cpl_mask* badpix_mask,
xsh_instrument* instr,cpl_mask** crh_mask)
223 long int nbpixcosmic=-1;
228 check(
size = cpl_frameset_get_size( rawFrames));
231 long nbpixtotal = cpl_frameset_get_size(rawFrames) * pre->
nx * pre->
ny;
232 double frac_accepted = 1;
235 && (nbpixcosmic!=0) ) {
242 *crh_mask=cpl_mask_duplicate(badpix_mask);
251 check(cpl_mask_and(*crh_mask,badpix_mask));
257 xsh_msg(
" Iteration Nb %d/%d, new cosmic found %ld",
261 frac_accepted -= ((double)nbpixcosmic / (
double)nbpixtotal);
274 nb_crh_total += nbpixcosmic ;
282 xsh_msg(
"Nb of cosmics removed by frame stacking: %d ", nb_crh_total);
290 return cpl_error_get_code();
297 cpl_mask* cosmic_mask = NULL;
302 cpl_mask* img_mask=NULL;
303 int size=cpl_imagelist_get_size(dataList);
305 cosmic_mask = cpl_mask_new(pre->
nx, pre->
ny);
306 for(i=0;i<
size; i++){
307 img = cpl_imagelist_get( dataList, i);
308 img_mask = cpl_image_get_bpm( img);
309 for(jmask=1; jmask<=pre->
ny; jmask++){
310 for(imask=1; imask <= pre->
nx; imask++){
311 if (cpl_mask_get(img_mask,imask, jmask) == CPL_BINARY_1){
312 cpl_mask_set(cosmic_mask, imask, jmask,CPL_BINARY_1);
322 cpl_mask_delete( cosmic_mask);
324 return cpl_error_get_code();
342 const char *result_tag,
346 cpl_imagelist ** list,
351 cpl_frame* medFrame = NULL;
352 cpl_imagelist *dataList = NULL;
353 cpl_image* qual_comb = NULL;
356 cpl_imagelist *errsList = NULL;
358 cpl_mask* badpix_mask = NULL;
359 cpl_mask* crh_mask = NULL;
361 cpl_frame *current = NULL;
366 char result_name[256];
367 const int mode_and = 0;
374 check(
size = cpl_frameset_get_size( rawFrames));
380 check( dataList = cpl_imagelist_new());
381 check( errsList = cpl_imagelist_new());
384 cpl_mask* mask_comb;;
386 for (i=0;i <
size;i++){
388 check(current = cpl_frameset_get_frame(rawFrames,i));
395 mask_tmp=cpl_image_get_bpm(pre->
data);
397 check_msg( cpl_imagelist_set(dataList,cpl_image_duplicate(pre->
data),
398 i),
"Can't add Data Image %d to imagelist", i) ;
399 check_msg( cpl_imagelist_set(errsList,cpl_image_duplicate(pre->
errs),
400 i),
"Can't add Data Image %d to imagelist", i) ;
408 mask_comb=cpl_mask_duplicate(mask_tmp);
409 qual_comb = cpl_image_duplicate(pre->
qual);
412 cpl_mask_or(mask_comb,mask_tmp);
424 cpl_mask_delete(mask_comb);
435 pre->
data=cpl_imagelist_collapse_sigclip_create(dataList,
436 stack_par->
klow,stack_par->
khigh,.1,CPL_COLLAPSE_MEAN,NULL);
440 int nrej=cpl_image_count_rejected(pre->
data);
446 pre->
qual = cpl_image_duplicate(qual_comb);
465 sprintf(result_name,
"%s.fits", result_tag);
468 if(strstr(result_tag,
"DARK") != NULL) {
484 check(cpl_frame_set_tag(medFrame, result_tag));
487 if ((crh_mask != NULL) && ((crh_ima) != NULL)) {
489 check(*crh_ima=cpl_image_new_from_mask(crh_mask));
490 cpl_mask_not(crh_mask);
494 cpl_image_threshold(*crh_ima, 1.1, DBL_MAX, 0, DBL_MAX);
505 if (cpl_error_get_code () != CPL_ERROR_NONE) {
528 const char *result_tag,
532 cpl_imagelist ** list,
536 cpl_frame * result = NULL ;
554 cpl_frame * result = NULL ;
static xsh_instrument * instrument
static xsh_clipping_param crh_clipping
void xsh_image_flag_bp(cpl_image *image, cpl_image *bpmap, xsh_instrument *inst)
void xsh_bpmap_collapse_median(cpl_image *median, cpl_imagelist *list, cpl_mask *bpmap)
cpl_error_code xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right, const int mode)
void xsh_bpmap_mask_bad_pixel(cpl_image *bpmap, cpl_mask *mask, int flag)
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.
cpl_mask * xsh_pre_get_bpmap(const xsh_pre *pre)
Get bpmap of pre structure.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
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_ILLEGAL(cond)
#define assure(CONDITION, ERROR_CODE,...)
#define check_msg(COMMAND,...)
#define XSH_ASSURE_NOT_NULL(pointer)
#define xsh_msg_dbg_medium(...)
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
#define xsh_msg_dbg_high(...)
int xsh_pfits_get_qc_ncrh(const cpl_propertylist *plist)
Find out the QC NCRH value.
void xsh_pfits_set_qc_ncrh_mean(cpl_propertylist *plist, const double value)
Write the QC.NCRH.AVG value.
void xsh_pfits_set_qc_ncrh(cpl_propertylist *plist, int value)
Write the QC.NCRH value.
void xsh_pfits_set_qc_crrate(cpl_propertylist *plist, double value)
Write the QC.CRRATE value.
void xsh_pfits_set_qc_ncrh_tot(cpl_propertylist *plist, const int value)
Write the QC.NCRH.TOT value.
cpl_frame * xsh_combine_offset(cpl_frameset *rawFrames, const char *result_tag, xsh_stack_param *stack_par, xsh_instrument *instrument, cpl_imagelist **list, cpl_image **crh_ima, const int save_tmp)
cpl_error_code xsh_flag_cosmic_debug(xsh_pre *pre, cpl_imagelist *dataList)
static void add_qc_crh(xsh_pre *pre, int nbcrh, int nframes)
static int xsh_remove_cr(cpl_imagelist *datalist, cpl_imagelist *errslist, cpl_image *median, cpl_image *errs, cpl_mask *mask, xsh_clipping_param *crh_clipping)
cpl_frame * xsh_remove_crh_multiple(cpl_frameset *rawFrames, const char *result_tag, xsh_stack_param *stack_par, xsh_clipping_param *crh_clipping, xsh_instrument *instrument, cpl_imagelist **list, cpl_image **crh_ima, const int save_tmp)
static cpl_error_code xsh_find_cosmics(cpl_frameset *rawFrames, xsh_pre *pre, xsh_clipping_param *crh_clipping, cpl_imagelist *dataList, cpl_imagelist *errsList, cpl_mask *badpix_mask, xsh_instrument *instr, cpl_mask **crh_mask)
static cpl_frame * xsh_remove_cosmics(cpl_frameset *rawFrames, const char *result_tag, xsh_stack_param *stack_par, xsh_clipping_param *crh_clipping, xsh_instrument *instr, cpl_imagelist **list, cpl_image **crh_ima, const int save_tmp)
Creates median, error and bad pixel map images from a set of frames.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
void xsh_free_mask(cpl_mask **m)
Deallocate an image mask and set the pointer to NULL.
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
cpl_propertylist * qual_header
cpl_propertylist * data_header
const char * stack_method
#define QFLAG_COSMIC_RAY_REMOVED
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_QC_CRH_NUMBER_TOT
#define XSH_QC_CRH_NUMBER_MEAN
#define XSH_QC_CRH_NUMBER
cpl_error_code xsh_collapse_errs(cpl_image *errs, cpl_imagelist *list, const int mode)
cpl_image * xsh_imagelist_collapse_median_create(cpl_imagelist *iml)
Compute median on imagelist.