39 #include "irplib_calib.h"
45 const int urx,
int ury,
55 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(6, 3, 0)
56 #define IRPLIB_FRAMESET_GET cpl_frameset_get_position
59 #define IRPLIB_FRAMESET_GET cpl_frameset_get_frame
82 return cpl_propertylist_get_double(plist,
"ESO DET DIT");
95 return cpl_propertylist_get_double(plist,
"EXPTIME");
119 const int urx,
int ury,
134 cpl_stats* stats=NULL;
137 tmp=cpl_image_extract(img,llx,lly,urx,ury);
138 cpl_image_accept_all(tmp);
139 for(i=0;i<nclip;i++) {
142 cpl_stats_delete(stats);
143 stats = cpl_stats_new_from_image(tmp, CPL_STATS_MEAN | CPL_STATS_STDEV);
144 mean = cpl_stats_get_mean(stats);
145 stdev = cpl_stats_get_stdev(stats);
147 threshold=kappa*stdev;
148 lo_cut=mean-threshold;
149 hi_cut=mean+threshold;
151 cpl_image_accept_all(tmp);
152 mask=cpl_mask_threshold_image_create(tmp,lo_cut,hi_cut);
155 cpl_image_reject_from_mask(tmp,mask);
156 cpl_mask_delete(mask);
162 cpl_image_delete(tmp);
163 cpl_stats_delete(stats);
201 cpl_image* img_on1=NULL;
202 cpl_image* img_on2=NULL;
203 cpl_image* img_on_dif=NULL;
205 cpl_image* img_of1=NULL;
206 cpl_image* img_of2=NULL;
207 cpl_image* img_of_dif=NULL;
209 cpl_table* res_tbl=NULL;
210 cpl_vector* dit_on=NULL;
211 cpl_vector* dit_of=NULL;
212 cpl_vector* exptime_on=NULL;
213 cpl_vector* exptime_of=NULL;
214 cpl_propertylist* plist=NULL;
234 const char* name=NULL;
241 double exptime_ref=0;
242 double exptime_tmp=0;
245 non = cpl_frameset_get_size(son);
246 nof = cpl_frameset_get_size(sof);
247 nfr = (non <= nof) ? non : nof;
249 dit_on=cpl_vector_new(nfr);
250 dit_of=cpl_vector_new(nfr);
251 exptime_on=cpl_vector_new(nfr);
252 exptime_of=cpl_vector_new(nfr);
256 frm=IRPLIB_FRAMESET_GET(son,i);
257 name=cpl_frame_get_filename(frm);
258 plist=cpl_propertylist_load(name,0);
261 cpl_propertylist_delete(plist);
262 cpl_vector_set(dit_on,i,dit_ref);
263 cpl_vector_set(exptime_on,i,exptime_ref);
265 frm=IRPLIB_FRAMESET_GET(sof,i);
266 name=cpl_frame_get_filename(frm);
267 plist=cpl_propertylist_load(name,0);
270 cpl_propertylist_delete(plist);
271 cpl_vector_set(dit_of,i,dit_ref);
272 cpl_vector_set(exptime_of,i,exptime_ref);
284 res_tbl=cpl_table_new(nfr);
285 cpl_table_new_column(res_tbl,
"adu", CPL_TYPE_DOUBLE);
286 cpl_table_new_column(res_tbl,
"gain", CPL_TYPE_DOUBLE);
289 frm=IRPLIB_FRAMESET_GET(son,i);
290 name=cpl_frame_get_filename(frm);
291 img_on1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
293 frm=IRPLIB_FRAMESET_GET(sof,i);
294 name=cpl_frame_get_filename(frm);
295 img_of1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
298 dit_ref=cpl_vector_get(dit_on,i);
299 exptime_ref=cpl_vector_get(exptime_on,i);
302 for(m=0;m<nfr; m++) {
304 frm=IRPLIB_FRAMESET_GET(son,m);
305 name=cpl_frame_get_filename(frm);
306 dit_tmp=cpl_vector_get(dit_on,m);
307 exptime_tmp=cpl_vector_get(exptime_on,m);
308 if(dit_tmp == dit_ref && exptime_tmp == exptime_ref) {
309 img_on2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
310 frm=IRPLIB_FRAMESET_GET(sof,m);
311 name=cpl_frame_get_filename(frm);
312 img_of2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
314 img_on_dif=cpl_image_subtract_create(img_on1,img_on2);
315 img_of_dif=cpl_image_subtract_create(img_of1,img_of2);
318 nclip,&avg_on1,&std);
320 nclip,&avg_on2,&std);
322 nclip,&avg_of1,&std);
324 nclip,&avg_of2,&std);
326 nclip,&avg_on_dif,&sig_on_dif);
328 nclip,&avg_of_dif,&sig_of_dif);
330 cpl_image_delete(img_on2);
331 cpl_image_delete(img_of2);
332 cpl_image_delete(img_on_dif);
333 cpl_image_delete(img_of_dif);
335 gain=((avg_on1+avg_on2)-(avg_of1+avg_of2))/
336 ((sig_on_dif*sig_on_dif)-(sig_of_dif*sig_of_dif));
338 cpl_table_set_double(res_tbl,
"gain",m,gain);
339 cpl_table_set_double(res_tbl,
"adu",m,
340 ((avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2));
345 cpl_image_delete(img_on1);
346 cpl_image_delete(img_of1);
350 cpl_vector_delete(dit_on);
351 cpl_vector_delete(dit_of);
352 cpl_vector_delete(exptime_on);
353 cpl_vector_delete(exptime_of);
393 const char* name=NULL;
395 cpl_vector* vec_adl=NULL;
396 cpl_vector* vec_dit=NULL;
397 cpl_vector* vec_avg=NULL;
398 cpl_vector* vec_med=NULL;
399 cpl_vector* vec_avg_dit=NULL;
400 cpl_vector* vec_med_dit=NULL;
401 cpl_propertylist* plist=NULL;
404 cpl_table* lin_tbl=NULL;
407 non = cpl_frameset_get_size(son);
408 nof = cpl_frameset_get_size(sof);
409 nfr = (non <= nof) ? non : nof;
411 lin_tbl=cpl_table_new(nfr);
412 cpl_table_new_column(lin_tbl,
"med", CPL_TYPE_DOUBLE);
413 cpl_table_new_column(lin_tbl,
"avg", CPL_TYPE_DOUBLE);
414 cpl_table_new_column(lin_tbl,
"med_dit", CPL_TYPE_DOUBLE);
415 cpl_table_new_column(lin_tbl,
"avg_dit", CPL_TYPE_DOUBLE);
416 cpl_table_new_column(lin_tbl,
"dit", CPL_TYPE_DOUBLE);
417 vec_med=cpl_vector_new(nfr);
418 vec_avg=cpl_vector_new(nfr);
419 vec_med_dit=cpl_vector_new(nfr);
420 vec_avg_dit=cpl_vector_new(nfr);
421 vec_dit=cpl_vector_new(nfr);
422 vec_adl=cpl_vector_new(nfr);
424 frm=IRPLIB_FRAMESET_GET(son,i);
425 name=cpl_frame_get_filename(frm);
426 img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
427 med_on=cpl_image_get_median(img);
428 avg_on=cpl_image_get_mean(img);
429 cpl_image_delete(img);
431 frm=IRPLIB_FRAMESET_GET(sof,i);
432 name=cpl_frame_get_filename(frm);
433 img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
434 med_of=cpl_image_get_median(img);
435 avg_of=cpl_image_get_mean(img);
436 cpl_image_delete(img);
439 plist=cpl_propertylist_load(name,0);
441 cpl_propertylist_delete(plist);
445 cpl_vector_set(vec_dit,i,dit);
446 cpl_vector_set(vec_avg,i,avg);
447 cpl_vector_set(vec_med,i,med);
448 cpl_vector_set(vec_avg_dit,i,avg_dit);
449 cpl_vector_set(vec_med_dit,i,med_dit);
452 cpl_table_set_double(lin_tbl,
"dit",i,dit);
453 cpl_table_set_double(lin_tbl,
"med",i,med);
454 cpl_table_set_double(lin_tbl,
"avg",i,avg);
455 cpl_table_set_double(lin_tbl,
"med_dit",i,med_dit);
456 cpl_table_set_double(lin_tbl,
"avg_dit",i,avg_dit);
459 cpl_table_new_column(lin_tbl,
"adl", CPL_TYPE_DOUBLE);
460 med_dit=cpl_vector_get_mean(vec_med_dit);
461 avg_dit=cpl_vector_get_mean(vec_avg_dit);
464 dit = cpl_table_get_double(lin_tbl,
"dit",i,status);
465 cpl_vector_set(vec_adl,i,dit*med_dit);
466 cpl_table_set_double(lin_tbl,
"adl",i,dit*med_dit);
470 cpl_vector_delete(vec_dit);
471 cpl_vector_delete(vec_adl);
472 cpl_vector_delete(vec_avg);
473 cpl_vector_delete(vec_med);
474 cpl_vector_delete(vec_avg_dit);
475 cpl_vector_delete(vec_med_dit);
494 cpl_imagelist * ilist,
495 const char * detlin_a,
496 const char * detlin_b,
497 const char * detlin_c)
507 double coeff_1, coeff_2, val ;
511 if (!ilist || !detlin_a || !detlin_b || !detlin_c)
return -1 ;
514 ima = cpl_image_load(detlin_a, CPL_TYPE_FLOAT, 0, 0) ;
515 imb = cpl_image_load(detlin_b, CPL_TYPE_FLOAT, 0, 0) ;
516 imc = cpl_image_load(detlin_c, CPL_TYPE_FLOAT, 0, 0) ;
517 if (!ima || !imb || !imc) {
518 cpl_msg_error(cpl_func,
"Cannot load the detlin images") ;
519 if (ima) cpl_image_delete(ima) ;
520 if (imb) cpl_image_delete(imb) ;
521 if (imc) cpl_image_delete(imc) ;
524 pima = cpl_image_get_data_float(ima) ;
525 pimb = cpl_image_get_data_float(imb) ;
526 pimc = cpl_image_get_data_float(imc) ;
529 nx = cpl_image_get_size_x(cpl_imagelist_get(ilist, 0)) ;
530 ny = cpl_image_get_size_y(cpl_imagelist_get(ilist, 0)) ;
531 ni = cpl_imagelist_get_size(ilist) ;
532 if ((cpl_image_get_size_x(ima) != nx) ||
533 (cpl_image_get_size_x(imb) != nx) ||
534 (cpl_image_get_size_x(imc) != nx) ||
535 (cpl_image_get_size_y(ima) != ny) ||
536 (cpl_image_get_size_y(imb) != ny) ||
537 (cpl_image_get_size_y(imc) != ny)) {
538 cpl_msg_error(cpl_func,
"Incompatible sizes") ;
539 cpl_image_delete(ima) ;
540 cpl_image_delete(imb) ;
541 cpl_image_delete(imc) ;
546 for (i=0 ; i<nx*ny ; i++) {
548 if (fabs(pima[i]) < 1e-30) {
549 coeff_1 = coeff_2 = (double)0.0 ;
551 coeff_1 = (double)pimb[i] / (
double)pima[i] ;
552 coeff_2 = (double)pimc[i] / (
double)pima[i] ;
555 for (j=0 ; j<ni ; j++) {
556 pdata = cpl_image_get_data_float(cpl_imagelist_get(ilist, j)) ;
557 val = (double)pdata[i] ;
558 pdata[i]=(float)(val+coeff_1*val*val+coeff_2*val*val*val) ;
562 cpl_image_delete(ima) ;
563 cpl_image_delete(imb) ;
564 cpl_image_delete(imc) ;
579 cpl_imagelist * ilist,
584 cpl_image * dark_image ;
585 cpl_image * flat_image ;
586 cpl_mask * bpm_im_bin ;
587 cpl_image * bpm_im_int ;
591 if (ilist == NULL)
return -1 ;
595 cpl_msg_info(cpl_func,
"Subtract the dark to the images") ;
597 if ((dark_image = cpl_image_load(dark, CPL_TYPE_FLOAT, 0, 0)) == NULL) {
598 cpl_msg_error(cpl_func,
"Cannot load the dark %s", dark) ;
602 if (cpl_imagelist_subtract_image(ilist, dark_image)!=CPL_ERROR_NONE) {
603 cpl_msg_error(cpl_func,
"Cannot apply the dark to the images") ;
604 cpl_image_delete(dark_image) ;
607 cpl_image_delete(dark_image) ;
612 cpl_msg_info(cpl_func,
"Divide the images by the flatfield") ;
614 if ((flat_image = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0)) == NULL) {
615 cpl_msg_error(cpl_func,
"Cannot load the flat field %s", flat) ;
619 if (cpl_imagelist_divide_image(ilist, flat_image)!=CPL_ERROR_NONE) {
620 cpl_msg_error(cpl_func,
"Cannot apply the flatfield to the images") ;
621 cpl_image_delete(flat_image) ;
624 cpl_image_delete(flat_image) ;
629 cpl_msg_info(cpl_func,
"Correct the bad pixels in the images") ;
631 if ((bpm_im_int = cpl_image_load(bpm, CPL_TYPE_INT, 0, 0)) == NULL) {
632 cpl_msg_error(cpl_func,
"Cannot load the bad pixel map %s", bpm) ;
636 bpm_im_bin = cpl_mask_threshold_image_create(bpm_im_int, -0.5, 0.5) ;
637 cpl_mask_not(bpm_im_bin) ;
638 cpl_image_delete(bpm_im_int) ;
640 for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
641 cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
642 if (cpl_detector_interpolate_rejected(
643 cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
644 cpl_msg_error(cpl_func,
"Cannot clean the bad pixels in obj %d",
646 cpl_mask_delete(bpm_im_bin) ;
650 cpl_mask_delete(bpm_im_bin) ;
static int irplib_get_clean_mean_window(cpl_image *img, const int llx, const int lly, const int urx, int ury, const int kappa, const int nclip, double *clean_mean, double *clean_stdev)
Get clean mean and stdev of an image over a window.
cpl_table * irplib_compute_gain(cpl_frameset *son, cpl_frameset *sof, int *zone, const int kappa, const int nclip)
Computes the detector's gain.
static double irplib_pfits_get_dit(const cpl_propertylist *plist)
find out the character string associated to the DIT keyword in a propertylist
int irplib_detlin_correct(cpl_imagelist *ilist, const char *detlin_a, const char *detlin_b, const char *detlin_c)
Apply the detector linearity correction.
cpl_table * irplib_compute_linearity(cpl_frameset *son, cpl_frameset *sof)
Computes the detector's linearity.
int irplib_flat_dark_bpm_calib(cpl_imagelist *ilist, const char *flat, const char *dark, const char *bpm)
Apply the calibration to the frames.
static double irplib_pfits_get_exp_time(const cpl_propertylist *plist)
find out the character string associated to the EXPTIME keyword