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
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <math.h>
00037 #include <cpl.h>
00038
00039 #include "irplib_calib.h"
00040
00041 static int
00042 irplib_get_clean_mean_window(cpl_image* img,
00043 const int llx,
00044 const int lly,
00045 const int urx, int ury,
00046 const int kappa,
00047 const int nclip,
00048 double* clean_mean,
00049 double* clean_stdev);
00050
00051
00052
00053
00054
00055 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(6, 3, 0)
00056 #define IRPLIB_FRAMESET_GET cpl_frameset_get_position
00057 #else
00058
00059 #define IRPLIB_FRAMESET_GET cpl_frameset_get_frame
00060 #endif
00061
00062
00063 static double irplib_pfits_get_dit(const cpl_propertylist * plist);
00064 static double irplib_pfits_get_exp_time(const cpl_propertylist* plist);
00065
00069
00070
00072
00079
00080 static double irplib_pfits_get_dit(const cpl_propertylist * plist)
00081 {
00082 return cpl_propertylist_get_double(plist,"ESO DET DIT");
00083 }
00084
00085
00091
00092 static double irplib_pfits_get_exp_time(const cpl_propertylist* plist)
00093 {
00094
00095 return cpl_propertylist_get_double(plist,"EXPTIME");
00096
00097 }
00098
00099
00115 static int
00116 irplib_get_clean_mean_window(cpl_image* img,
00117 const int llx,
00118 const int lly,
00119 const int urx, int ury,
00120 const int kappa,
00121 const int nclip,
00122 double* clean_mean,
00123 double* clean_stdev)
00124 {
00125
00126
00127 double mean=0;
00128 double stdev=0;
00129 cpl_image* tmp=NULL;
00130 cpl_stats* stats=NULL;
00131 int i=0;
00132
00133 tmp=cpl_image_extract(img,llx,lly,urx,ury);
00134 cpl_image_accept_all(tmp);
00135 for(i=0;i<nclip;i++) {
00136
00137 double threshold=0;
00138 double lo_cut=0;
00139 double hi_cut=0;
00140 cpl_mask* mask=NULL;
00141
00142 cpl_stats_delete(stats);
00143 stats = cpl_stats_new_from_image(tmp, CPL_STATS_MEAN | CPL_STATS_STDEV);
00144 mean = cpl_stats_get_mean(stats);
00145 stdev = cpl_stats_get_stdev(stats);
00146
00147 threshold=kappa*stdev;
00148 lo_cut=mean-threshold;
00149 hi_cut=mean+threshold;
00150
00151 cpl_image_accept_all(tmp);
00152 mask=cpl_mask_threshold_image_create(tmp,lo_cut,hi_cut);
00153
00154 cpl_mask_not(mask);
00155 cpl_image_reject_from_mask(tmp,mask);
00156 cpl_mask_delete(mask);
00157
00158
00159 }
00160 *clean_mean=mean;
00161 *clean_stdev=stdev;
00162 cpl_image_delete(tmp);
00163 cpl_stats_delete(stats);
00164
00165 return 0;
00166
00167
00168 }
00169
00170
00171
00172
00187
00188
00189
00190 cpl_table*
00191 irplib_compute_gain(
00192 cpl_frameset* son,
00193 cpl_frameset* sof,
00194 int* zone,
00195 const int kappa,
00196 const int nclip)
00197 {
00198
00199 cpl_frame* frm=NULL;
00200
00201 cpl_table* res_tbl=NULL;
00202 cpl_vector* dit_on=NULL;
00203 cpl_vector* dit_of=NULL;
00204 cpl_vector* exptime_on=NULL;
00205 cpl_vector* exptime_of=NULL;
00206
00207 int non=0;
00208 int nof=0;
00209 int nfr=0;
00210 int llx;
00211 int lly;
00212 int urx;
00213 int ury;
00214
00215 const char* name=NULL;
00216 int i=0;
00217
00218 double dit_ref=0;
00219 double exptime_ref=0;
00220
00221
00222 non = cpl_frameset_get_size(son);
00223 nof = cpl_frameset_get_size(sof);
00224 nfr = (non <= nof) ? non : nof;
00225
00226 dit_on=cpl_vector_new(nfr);
00227 dit_of=cpl_vector_new(nfr);
00228 exptime_on=cpl_vector_new(nfr);
00229 exptime_of=cpl_vector_new(nfr);
00230
00231 for(i=0;i<nfr;i++) {
00232 cpl_propertylist* plist=NULL;
00233
00234 frm=IRPLIB_FRAMESET_GET(son,i);
00235 name=cpl_frame_get_filename(frm);
00236 plist=cpl_propertylist_load(name,0);
00237 dit_ref=irplib_pfits_get_dit(plist);
00238 exptime_ref=(double)irplib_pfits_get_exp_time(plist);
00239 cpl_propertylist_delete(plist);
00240 cpl_vector_set(dit_on,i,dit_ref);
00241 cpl_vector_set(exptime_on,i,exptime_ref);
00242
00243 frm=IRPLIB_FRAMESET_GET(sof,i);
00244 name=cpl_frame_get_filename(frm);
00245 plist=cpl_propertylist_load(name,0);
00246 dit_ref=irplib_pfits_get_dit(plist);
00247 exptime_ref=(double)irplib_pfits_get_exp_time(plist);
00248 cpl_propertylist_delete(plist);
00249 cpl_vector_set(dit_of,i,dit_ref);
00250 cpl_vector_set(exptime_of,i,exptime_ref);
00251
00252 }
00253
00254
00255 llx=zone[0];
00256 lly=zone[1];
00257 urx=zone[2];
00258 ury=zone[3];
00259
00260
00261
00262 res_tbl=cpl_table_new(nfr);
00263 cpl_table_new_column(res_tbl,"adu", CPL_TYPE_DOUBLE);
00264 cpl_table_new_column(res_tbl,"gain", CPL_TYPE_DOUBLE);
00265
00266 for(i=0;i<nfr;i++) {
00267 cpl_image* img_on1=NULL;
00268 cpl_image* img_of1=NULL;
00269 int m=0;
00270
00271 frm=IRPLIB_FRAMESET_GET(son,i);
00272 name=cpl_frame_get_filename(frm);
00273 img_on1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00274
00275 frm=IRPLIB_FRAMESET_GET(sof,i);
00276 name=cpl_frame_get_filename(frm);
00277 img_of1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00278
00279
00280 dit_ref=cpl_vector_get(dit_on,i);
00281 exptime_ref=cpl_vector_get(exptime_on,i);
00282
00283
00284 for(m=0;m<nfr; m++) {
00285 if(m != i) {
00286 double dit_tmp=0;
00287 double exptime_tmp=0;
00288
00289 frm=IRPLIB_FRAMESET_GET(son,m);
00290 name=cpl_frame_get_filename(frm);
00291 dit_tmp=cpl_vector_get(dit_on,m);
00292 exptime_tmp=cpl_vector_get(exptime_on,m);
00293 if(dit_tmp == dit_ref && exptime_tmp == exptime_ref) {
00294 cpl_image* img_on2=NULL;
00295 cpl_image* img_on_dif=NULL;
00296
00297 cpl_image* img_of2=NULL;
00298 cpl_image* img_of_dif=NULL;
00299
00300 double avg_on1=0;
00301 double avg_on2=0;
00302 double avg_of1=0;
00303 double avg_of2=0;
00304 double avg_on_dif=0;
00305 double avg_of_dif=0;
00306 double std=0;
00307 double sig_on_dif=0;
00308 double sig_of_dif=0;
00309 double gain=0;
00310
00311 img_on2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00312 frm=IRPLIB_FRAMESET_GET(sof,m);
00313 name=cpl_frame_get_filename(frm);
00314 img_of2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00315
00316 img_on_dif=cpl_image_subtract_create(img_on1,img_on2);
00317 img_of_dif=cpl_image_subtract_create(img_of1,img_of2);
00318
00319 irplib_get_clean_mean_window(img_on1,llx,lly,urx,ury,kappa,
00320 nclip,&avg_on1,&std);
00321 irplib_get_clean_mean_window(img_on2,llx,lly,urx,ury,kappa,
00322 nclip,&avg_on2,&std);
00323 irplib_get_clean_mean_window(img_of1,llx,lly,urx,ury,kappa,
00324 nclip,&avg_of1,&std);
00325 irplib_get_clean_mean_window(img_of2,llx,lly,urx,ury,kappa,
00326 nclip,&avg_of2,&std);
00327 irplib_get_clean_mean_window(img_on_dif,llx,lly,urx,ury,kappa,
00328 nclip,&avg_on_dif,&sig_on_dif);
00329 irplib_get_clean_mean_window(img_of_dif,llx,lly,urx,ury,kappa,
00330 nclip,&avg_of_dif,&sig_of_dif);
00331
00332 cpl_image_delete(img_on2);
00333 cpl_image_delete(img_of2);
00334 cpl_image_delete(img_on_dif);
00335 cpl_image_delete(img_of_dif);
00336
00337 gain=((avg_on1+avg_on2)-(avg_of1+avg_of2))/
00338 ((sig_on_dif*sig_on_dif)-(sig_of_dif*sig_of_dif));
00339
00340 cpl_table_set_double(res_tbl,"gain",m,gain);
00341 cpl_table_set_double(res_tbl,"adu",m,
00342 ((avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2));
00343
00344 }
00345 }
00346 }
00347 cpl_image_delete(img_on1);
00348 cpl_image_delete(img_of1);
00349 }
00350
00351
00352 cpl_vector_delete(dit_on);
00353 cpl_vector_delete(dit_of);
00354 cpl_vector_delete(exptime_on);
00355 cpl_vector_delete(exptime_of);
00356
00357 return res_tbl;
00358
00359 }
00360
00361
00371
00372
00373
00374 cpl_table* irplib_compute_linearity(cpl_frameset* son, cpl_frameset* sof)
00375 {
00376
00377 int non=0;
00378 int nof=0;
00379 int nfr=0;
00380 int i=0;
00381 double med_dit=0;
00382
00383
00384 cpl_vector* vec_adl=NULL;
00385 cpl_vector* vec_dit=NULL;
00386 cpl_vector* vec_avg=NULL;
00387 cpl_vector* vec_med=NULL;
00388 cpl_vector* vec_avg_dit=NULL;
00389 cpl_vector* vec_med_dit=NULL;
00390
00391 double dit=0;
00392 cpl_table* lin_tbl=NULL;
00393
00394
00395 non = cpl_frameset_get_size(son);
00396 nof = cpl_frameset_get_size(sof);
00397 nfr = (non <= nof) ? non : nof;
00398
00399 lin_tbl=cpl_table_new(nfr);
00400 cpl_table_new_column(lin_tbl,"med", CPL_TYPE_DOUBLE);
00401 cpl_table_new_column(lin_tbl,"avg", CPL_TYPE_DOUBLE);
00402 cpl_table_new_column(lin_tbl,"med_dit", CPL_TYPE_DOUBLE);
00403 cpl_table_new_column(lin_tbl,"avg_dit", CPL_TYPE_DOUBLE);
00404 cpl_table_new_column(lin_tbl,"dit", CPL_TYPE_DOUBLE);
00405 vec_med=cpl_vector_new(nfr);
00406 vec_avg=cpl_vector_new(nfr);
00407 vec_med_dit=cpl_vector_new(nfr);
00408 vec_avg_dit=cpl_vector_new(nfr);
00409 vec_dit=cpl_vector_new(nfr);
00410 vec_adl=cpl_vector_new(nfr);
00411 for(i=0;i<nfr;i++) {
00412 cpl_frame* frm=NULL;
00413
00414 double med_on=0;
00415 double avg_on=0;
00416 double med_of=0;
00417 double avg_of=0;
00418
00419 double med=0;
00420 double avg=0;
00421 double avg_dit=0;
00422
00423 const char* name=NULL;
00424 cpl_image* img=NULL;
00425 cpl_propertylist* plist=NULL;
00426
00427 frm=IRPLIB_FRAMESET_GET(son,i);
00428 name=cpl_frame_get_filename(frm);
00429 img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00430 med_on=cpl_image_get_median(img);
00431 avg_on=cpl_image_get_mean(img);
00432 cpl_image_delete(img);
00433
00434 frm=IRPLIB_FRAMESET_GET(sof,i);
00435 name=cpl_frame_get_filename(frm);
00436 img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00437 med_of=cpl_image_get_median(img);
00438 avg_of=cpl_image_get_mean(img);
00439 cpl_image_delete(img);
00440 med=med_on-med_of;
00441 avg=avg_on-avg_of;
00442 plist=cpl_propertylist_load(name,0);
00443 dit=(double)irplib_pfits_get_dit(plist);
00444 cpl_propertylist_delete(plist);
00445 avg_dit=avg/dit;
00446 med_dit=med/dit;
00447
00448 cpl_vector_set(vec_dit,i,dit);
00449 cpl_vector_set(vec_avg,i,avg);
00450 cpl_vector_set(vec_med,i,med);
00451 cpl_vector_set(vec_avg_dit,i,avg_dit);
00452 cpl_vector_set(vec_med_dit,i,med_dit);
00453
00454
00455 cpl_table_set_double(lin_tbl,"dit",i,dit);
00456 cpl_table_set_double(lin_tbl,"med",i,med);
00457 cpl_table_set_double(lin_tbl,"avg",i,avg);
00458 cpl_table_set_double(lin_tbl,"med_dit",i,med_dit);
00459 cpl_table_set_double(lin_tbl,"avg_dit",i,avg_dit);
00460
00461 }
00462 cpl_table_new_column(lin_tbl,"adl", CPL_TYPE_DOUBLE);
00463 med_dit=cpl_vector_get_mean(vec_med_dit);
00464
00465
00466 for(i=0;i<nfr;i++) {
00467 int* status=0;
00468 dit = cpl_table_get_double(lin_tbl,"dit",i,status);
00469 cpl_vector_set(vec_adl,i,dit*med_dit);
00470 cpl_table_set_double(lin_tbl,"adl",i,dit*med_dit);
00471 }
00472
00473
00474 cpl_vector_delete(vec_dit);
00475 cpl_vector_delete(vec_adl);
00476 cpl_vector_delete(vec_avg);
00477 cpl_vector_delete(vec_med);
00478 cpl_vector_delete(vec_avg_dit);
00479 cpl_vector_delete(vec_med_dit);
00480
00481
00482 return lin_tbl;
00483
00484 }
00485
00486
00487
00496
00497 int irplib_detlin_correct(
00498 cpl_imagelist * ilist,
00499 const char * detlin_a,
00500 const char * detlin_b,
00501 const char * detlin_c)
00502 {
00503 cpl_image * ima ;
00504 cpl_image * imb ;
00505 cpl_image * imc ;
00506 float * pima ;
00507 float * pimb ;
00508 float * pimc ;
00509 float * pdata ;
00510 int nx, ny, ni ;
00511 double coeff_1, coeff_2, val ;
00512 int i, j ;
00513
00514
00515 if (!ilist || !detlin_a || !detlin_b || !detlin_c) return -1 ;
00516
00517
00518 ima = cpl_image_load(detlin_a, CPL_TYPE_FLOAT, 0, 0) ;
00519 imb = cpl_image_load(detlin_b, CPL_TYPE_FLOAT, 0, 0) ;
00520 imc = cpl_image_load(detlin_c, CPL_TYPE_FLOAT, 0, 0) ;
00521 if (!ima || !imb || !imc) {
00522 cpl_msg_error(cpl_func, "Cannot load the detlin images") ;
00523 if (ima) cpl_image_delete(ima) ;
00524 if (imb) cpl_image_delete(imb) ;
00525 if (imc) cpl_image_delete(imc) ;
00526 return -1 ;
00527 }
00528 pima = cpl_image_get_data_float(ima) ;
00529 pimb = cpl_image_get_data_float(imb) ;
00530 pimc = cpl_image_get_data_float(imc) ;
00531
00532
00533 nx = cpl_image_get_size_x(cpl_imagelist_get(ilist, 0)) ;
00534 ny = cpl_image_get_size_y(cpl_imagelist_get(ilist, 0)) ;
00535 ni = cpl_imagelist_get_size(ilist) ;
00536 if ((cpl_image_get_size_x(ima) != nx) ||
00537 (cpl_image_get_size_x(imb) != nx) ||
00538 (cpl_image_get_size_x(imc) != nx) ||
00539 (cpl_image_get_size_y(ima) != ny) ||
00540 (cpl_image_get_size_y(imb) != ny) ||
00541 (cpl_image_get_size_y(imc) != ny)) {
00542 cpl_msg_error(cpl_func, "Incompatible sizes") ;
00543 cpl_image_delete(ima) ;
00544 cpl_image_delete(imb) ;
00545 cpl_image_delete(imc) ;
00546 return -1 ;
00547 }
00548
00549
00550 for (i=0 ; i<nx*ny ; i++) {
00551
00552 if (fabs(pima[i]) < 1e-30) {
00553 coeff_1 = coeff_2 = (double)0.0 ;
00554 } else {
00555 coeff_1 = (double)pimb[i] / (double)pima[i] ;
00556 coeff_2 = (double)pimc[i] / (double)pima[i] ;
00557 }
00558
00559 for (j=0 ; j<ni ; j++) {
00560 pdata = cpl_image_get_data_float(cpl_imagelist_get(ilist, j)) ;
00561 val = (double)pdata[i] ;
00562 pdata[i]=(float)(val+coeff_1*val*val+coeff_2*val*val*val) ;
00563 }
00564 }
00565
00566 cpl_image_delete(ima) ;
00567 cpl_image_delete(imb) ;
00568 cpl_image_delete(imc) ;
00569 return 0 ;
00570 }
00571
00572
00581
00582 int irplib_flat_dark_bpm_calib(
00583 cpl_imagelist * ilist,
00584 const char * flat,
00585 const char * dark,
00586 const char * bpm)
00587 {
00588
00589 if (ilist == NULL) return -1 ;
00590
00591
00592 if (dark != NULL) {
00593 cpl_image * dark_image ;
00594 cpl_msg_info(cpl_func, "Subtract the dark to the images") ;
00595
00596 if ((dark_image = cpl_image_load(dark, CPL_TYPE_FLOAT, 0, 0)) == NULL) {
00597 cpl_msg_error(cpl_func, "Cannot load the dark %s", dark) ;
00598 return -1 ;
00599 }
00600
00601 if (cpl_imagelist_subtract_image(ilist, dark_image)!=CPL_ERROR_NONE) {
00602 cpl_msg_error(cpl_func, "Cannot apply the dark to the images") ;
00603 cpl_image_delete(dark_image) ;
00604 return -1 ;
00605 }
00606 cpl_image_delete(dark_image) ;
00607 }
00608
00609
00610 if (flat != NULL) {
00611 cpl_image * flat_image ;
00612 cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00613
00614 if ((flat_image = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0)) == NULL) {
00615 cpl_msg_error(cpl_func, "Cannot load the flat field %s", flat) ;
00616 return -1 ;
00617 }
00618
00619 if (cpl_imagelist_divide_image(ilist, flat_image)!=CPL_ERROR_NONE) {
00620 cpl_msg_error(cpl_func, "Cannot apply the flatfield to the images") ;
00621 cpl_image_delete(flat_image) ;
00622 return -1 ;
00623 }
00624 cpl_image_delete(flat_image) ;
00625 }
00626
00627
00628 if (bpm != NULL) {
00629 cpl_mask * bpm_im_bin ;
00630 cpl_image * bpm_im_int ;
00631 int i ;
00632 cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00633
00634 if ((bpm_im_int = cpl_image_load(bpm, CPL_TYPE_INT, 0, 0)) == NULL) {
00635 cpl_msg_error(cpl_func, "Cannot load the bad pixel map %s", bpm) ;
00636 return -1 ;
00637 }
00638
00639 bpm_im_bin = cpl_mask_threshold_image_create(bpm_im_int, -0.5, 0.5) ;
00640 cpl_mask_not(bpm_im_bin) ;
00641 cpl_image_delete(bpm_im_int) ;
00642
00643 for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
00644 cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
00645 if (cpl_detector_interpolate_rejected(
00646 cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
00647 cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
00648 i+1);
00649 cpl_mask_delete(bpm_im_bin) ;
00650 return -1 ;
00651 }
00652 }
00653 cpl_mask_delete(bpm_im_bin) ;
00654 }
00655
00656
00657 return 0 ;
00658 }
00659