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,
130 cpl_stats* stats=NULL;
133 tmp=cpl_image_extract(img,llx,lly,urx,ury);
134 cpl_image_accept_all(tmp);
135 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_table* res_tbl=NULL;
202 cpl_vector* dit_on=NULL;
203 cpl_vector* dit_of=NULL;
204 cpl_vector* exptime_on=NULL;
205 cpl_vector* exptime_of=NULL;
215 const char* name=NULL;
219 double exptime_ref=0;
222 non = cpl_frameset_get_size(son);
223 nof = cpl_frameset_get_size(sof);
224 nfr = (non <= nof) ? non : nof;
226 dit_on=cpl_vector_new(nfr);
227 dit_of=cpl_vector_new(nfr);
228 exptime_on=cpl_vector_new(nfr);
229 exptime_of=cpl_vector_new(nfr);
232 cpl_propertylist* plist=NULL;
234 frm=IRPLIB_FRAMESET_GET(son,i);
235 name=cpl_frame_get_filename(frm);
236 plist=cpl_propertylist_load(name,0);
239 cpl_propertylist_delete(plist);
240 cpl_vector_set(dit_on,i,dit_ref);
241 cpl_vector_set(exptime_on,i,exptime_ref);
243 frm=IRPLIB_FRAMESET_GET(sof,i);
244 name=cpl_frame_get_filename(frm);
245 plist=cpl_propertylist_load(name,0);
248 cpl_propertylist_delete(plist);
249 cpl_vector_set(dit_of,i,dit_ref);
250 cpl_vector_set(exptime_of,i,exptime_ref);
262 res_tbl=cpl_table_new(nfr);
263 cpl_table_new_column(res_tbl,
"adu", CPL_TYPE_DOUBLE);
264 cpl_table_new_column(res_tbl,
"gain", CPL_TYPE_DOUBLE);
267 cpl_image* img_on1=NULL;
268 cpl_image* img_of1=NULL;
271 frm=IRPLIB_FRAMESET_GET(son,i);
272 name=cpl_frame_get_filename(frm);
273 img_on1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
275 frm=IRPLIB_FRAMESET_GET(sof,i);
276 name=cpl_frame_get_filename(frm);
277 img_of1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
280 dit_ref=cpl_vector_get(dit_on,i);
281 exptime_ref=cpl_vector_get(exptime_on,i);
284 for(m=0;m<nfr; m++) {
287 double exptime_tmp=0;
289 frm=IRPLIB_FRAMESET_GET(son,m);
290 name=cpl_frame_get_filename(frm);
291 dit_tmp=cpl_vector_get(dit_on,m);
292 exptime_tmp=cpl_vector_get(exptime_on,m);
293 if(dit_tmp == dit_ref && exptime_tmp == exptime_ref) {
294 cpl_image* img_on2=NULL;
295 cpl_image* img_on_dif=NULL;
297 cpl_image* img_of2=NULL;
298 cpl_image* img_of_dif=NULL;
311 img_on2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
312 frm=IRPLIB_FRAMESET_GET(sof,m);
313 name=cpl_frame_get_filename(frm);
314 img_of2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
316 img_on_dif=cpl_image_subtract_create(img_on1,img_on2);
317 img_of_dif=cpl_image_subtract_create(img_of1,img_of2);
320 nclip,&avg_on1,&std);
322 nclip,&avg_on2,&std);
324 nclip,&avg_of1,&std);
326 nclip,&avg_of2,&std);
328 nclip,&avg_on_dif,&sig_on_dif);
330 nclip,&avg_of_dif,&sig_of_dif);
332 cpl_image_delete(img_on2);
333 cpl_image_delete(img_of2);
334 cpl_image_delete(img_on_dif);
335 cpl_image_delete(img_of_dif);
337 gain=((avg_on1+avg_on2)-(avg_of1+avg_of2))/
338 ((sig_on_dif*sig_on_dif)-(sig_of_dif*sig_of_dif));
340 cpl_table_set_double(res_tbl,
"gain",m,gain);
341 cpl_table_set_double(res_tbl,
"adu",m,
342 ((avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2));
347 cpl_image_delete(img_on1);
348 cpl_image_delete(img_of1);
352 cpl_vector_delete(dit_on);
353 cpl_vector_delete(dit_of);
354 cpl_vector_delete(exptime_on);
355 cpl_vector_delete(exptime_of);
384 cpl_vector* vec_adl=NULL;
385 cpl_vector* vec_dit=NULL;
386 cpl_vector* vec_avg=NULL;
387 cpl_vector* vec_med=NULL;
388 cpl_vector* vec_avg_dit=NULL;
389 cpl_vector* vec_med_dit=NULL;
392 cpl_table* lin_tbl=NULL;
395 non = cpl_frameset_get_size(son);
396 nof = cpl_frameset_get_size(sof);
397 nfr = (non <= nof) ? non : nof;
399 lin_tbl=cpl_table_new(nfr);
400 cpl_table_new_column(lin_tbl,
"med", CPL_TYPE_DOUBLE);
401 cpl_table_new_column(lin_tbl,
"avg", CPL_TYPE_DOUBLE);
402 cpl_table_new_column(lin_tbl,
"med_dit", CPL_TYPE_DOUBLE);
403 cpl_table_new_column(lin_tbl,
"avg_dit", CPL_TYPE_DOUBLE);
404 cpl_table_new_column(lin_tbl,
"dit", CPL_TYPE_DOUBLE);
405 vec_med=cpl_vector_new(nfr);
406 vec_avg=cpl_vector_new(nfr);
407 vec_med_dit=cpl_vector_new(nfr);
408 vec_avg_dit=cpl_vector_new(nfr);
409 vec_dit=cpl_vector_new(nfr);
410 vec_adl=cpl_vector_new(nfr);
423 const char* name=NULL;
425 cpl_propertylist* plist=NULL;
427 frm=IRPLIB_FRAMESET_GET(son,i);
428 name=cpl_frame_get_filename(frm);
429 img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
430 med_on=cpl_image_get_median(img);
431 avg_on=cpl_image_get_mean(img);
432 cpl_image_delete(img);
434 frm=IRPLIB_FRAMESET_GET(sof,i);
435 name=cpl_frame_get_filename(frm);
436 img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
437 med_of=cpl_image_get_median(img);
438 avg_of=cpl_image_get_mean(img);
439 cpl_image_delete(img);
442 plist=cpl_propertylist_load(name,0);
444 cpl_propertylist_delete(plist);
448 cpl_vector_set(vec_dit,i,dit);
449 cpl_vector_set(vec_avg,i,avg);
450 cpl_vector_set(vec_med,i,med);
451 cpl_vector_set(vec_avg_dit,i,avg_dit);
452 cpl_vector_set(vec_med_dit,i,med_dit);
455 cpl_table_set_double(lin_tbl,
"dit",i,dit);
456 cpl_table_set_double(lin_tbl,
"med",i,med);
457 cpl_table_set_double(lin_tbl,
"avg",i,avg);
458 cpl_table_set_double(lin_tbl,
"med_dit",i,med_dit);
459 cpl_table_set_double(lin_tbl,
"avg_dit",i,avg_dit);
462 cpl_table_new_column(lin_tbl,
"adl", CPL_TYPE_DOUBLE);
463 med_dit=cpl_vector_get_mean(vec_med_dit);
468 dit = cpl_table_get_double(lin_tbl,
"dit",i,status);
469 cpl_vector_set(vec_adl,i,dit*med_dit);
470 cpl_table_set_double(lin_tbl,
"adl",i,dit*med_dit);
474 cpl_vector_delete(vec_dit);
475 cpl_vector_delete(vec_adl);
476 cpl_vector_delete(vec_avg);
477 cpl_vector_delete(vec_med);
478 cpl_vector_delete(vec_avg_dit);
479 cpl_vector_delete(vec_med_dit);
498 cpl_imagelist * ilist,
499 const char * detlin_a,
500 const char * detlin_b,
501 const char * detlin_c)
511 double coeff_1, coeff_2, val ;
515 if (!ilist || !detlin_a || !detlin_b || !detlin_c)
return -1 ;
518 ima = cpl_image_load(detlin_a, CPL_TYPE_FLOAT, 0, 0) ;
519 imb = cpl_image_load(detlin_b, CPL_TYPE_FLOAT, 0, 0) ;
520 imc = cpl_image_load(detlin_c, CPL_TYPE_FLOAT, 0, 0) ;
521 if (!ima || !imb || !imc) {
522 cpl_msg_error(cpl_func,
"Cannot load the detlin images") ;
523 if (ima) cpl_image_delete(ima) ;
524 if (imb) cpl_image_delete(imb) ;
525 if (imc) cpl_image_delete(imc) ;
528 pima = cpl_image_get_data_float(ima) ;
529 pimb = cpl_image_get_data_float(imb) ;
530 pimc = cpl_image_get_data_float(imc) ;
533 nx = cpl_image_get_size_x(cpl_imagelist_get(ilist, 0)) ;
534 ny = cpl_image_get_size_y(cpl_imagelist_get(ilist, 0)) ;
535 ni = cpl_imagelist_get_size(ilist) ;
536 if ((cpl_image_get_size_x(ima) != nx) ||
537 (cpl_image_get_size_x(imb) != nx) ||
538 (cpl_image_get_size_x(imc) != nx) ||
539 (cpl_image_get_size_y(ima) != ny) ||
540 (cpl_image_get_size_y(imb) != ny) ||
541 (cpl_image_get_size_y(imc) != ny)) {
542 cpl_msg_error(cpl_func,
"Incompatible sizes") ;
543 cpl_image_delete(ima) ;
544 cpl_image_delete(imb) ;
545 cpl_image_delete(imc) ;
550 for (i=0 ; i<nx*ny ; i++) {
552 if (fabs(pima[i]) < 1e-30) {
553 coeff_1 = coeff_2 = (double)0.0 ;
555 coeff_1 = (double)pimb[i] / (
double)pima[i] ;
556 coeff_2 = (double)pimc[i] / (
double)pima[i] ;
559 for (j=0 ; j<ni ; j++) {
560 pdata = cpl_image_get_data_float(cpl_imagelist_get(ilist, j)) ;
561 val = (double)pdata[i] ;
562 pdata[i]=(float)(val+coeff_1*val*val+coeff_2*val*val*val) ;
566 cpl_image_delete(ima) ;
567 cpl_image_delete(imb) ;
568 cpl_image_delete(imc) ;
583 cpl_imagelist * ilist,
589 if (ilist == NULL)
return -1 ;
593 cpl_image * dark_image ;
594 cpl_msg_info(cpl_func,
"Subtract the dark to the images") ;
596 if ((dark_image = cpl_image_load(dark, CPL_TYPE_FLOAT, 0, 0)) == NULL) {
597 cpl_msg_error(cpl_func,
"Cannot load the dark %s", dark) ;
601 if (cpl_imagelist_subtract_image(ilist, dark_image)!=CPL_ERROR_NONE) {
602 cpl_msg_error(cpl_func,
"Cannot apply the dark to the images") ;
603 cpl_image_delete(dark_image) ;
606 cpl_image_delete(dark_image) ;
611 cpl_image * flat_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_mask * bpm_im_bin ;
630 cpl_image * bpm_im_int ;
632 cpl_msg_info(cpl_func,
"Correct the bad pixels in the images") ;
634 if ((bpm_im_int = cpl_image_load(bpm, CPL_TYPE_INT, 0, 0)) == NULL) {
635 cpl_msg_error(cpl_func,
"Cannot load the bad pixel map %s", bpm) ;
639 bpm_im_bin = cpl_mask_threshold_image_create(bpm_im_int, -0.5, 0.5) ;
640 cpl_mask_not(bpm_im_bin) ;
641 cpl_image_delete(bpm_im_int) ;
643 for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
644 cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
645 if (cpl_detector_interpolate_rejected(
646 cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
647 cpl_msg_error(cpl_func,
"Cannot clean the bad pixels in obj %d",
649 cpl_mask_delete(bpm_im_bin) ;
653 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