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 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00039
00042
00043
00044
00045 #include <xsh_drl.h>
00046 #include <xsh_dfs.h>
00047
00048 #include <xsh_pfits_qc.h>
00049 #include <xsh_utils_wrappers.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_pfits_qc.h>
00052 #include <xsh_error.h>
00053 #include <xsh_msg.h>
00054 #include <xsh_badpixelmap.h>
00055 #include <xsh_paf_save.h>
00056 #include <xsh_utils_image.h>
00057 #include <xsh_detmon.h>
00058 #include <xsh_irplib_utils.h>
00059 #include <math.h>
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #define RECIPE_ID "xsh_mbias"
00070 #define HIST_FACT 2.354820045
00071
00072
00073
00074
00075 static cpl_error_code
00076 xsh_compute_ron_mbias(cpl_frameset* raws,
00077 xsh_pre* master,
00078 cpl_parameterlist* parameters);
00079
00080
00081 static cpl_error_code
00082 xsh_compute_fpn_mbias(cpl_frameset* raws,
00083 xsh_pre* master,
00084 xsh_instrument* instrument,
00085 cpl_parameterlist* parameters);
00086
00087 static cpl_error_code
00088 xsh_mbias_get_fpn(const cpl_image* ima,
00089 cpl_parameterlist* parameters,
00090 double* fpn);
00091
00092
00093 static cpl_error_code
00094 xsh_my_detmon_ronbias_random(const cpl_imagelist * rawbiases,
00095 const cpl_image * masterbias,
00096 cpl_propertylist * qclist,
00097 const char* method,
00098 const int random_sizex,
00099 const int random_nsamples);
00100
00101 static cpl_error_code
00102 xsh_my_detmon_ronbias_histo(const cpl_imagelist * rawbiases,
00103 const cpl_image * masterbias,
00104 cpl_propertylist * qclist,
00105 const char* method);
00106
00107 static cpl_error_code
00108 xsh_my_detmon_ronbias_region(const cpl_imagelist * rawbiases,
00109 const cpl_image * masterbias,
00110 cpl_propertylist * qclist,
00111 const char* pmethod,
00112 const int ref_llx,
00113 const int ref_lly,
00114 const int ref_urx,
00115 const int ref_ury,
00116 const int stacking_ks_low,
00117 const int stacking_ks_iter);
00118
00119
00120
00121 static cpl_error_code
00122 xsh_my_detmon_ronbias_preoverscan(const cpl_imagelist * rawbiases,
00123
00124 cpl_propertylist * qclist,
00125 cpl_image ** synthetic,
00126 const int prescan_llx,
00127 const int prescan_lly,
00128 const int prescan_urx,
00129 const int prescan_ury,
00130 const int overscan_llx,
00131 const int overscan_lly,
00132 const int overscan_urx,
00133 const int overscan_ury,
00134 const int stacking_ks_low,
00135 const int stacking_ks_iter);
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 static double
00146 xsh_mdark_get_contam(xsh_pre * pre,cpl_parameterlist* drs_params,
00147 xsh_instrument* instr)
00148 {
00149
00150 double contam=0;
00151
00152 int llx_on=0;
00153 int lly_on=0;
00154 int urx_on=0;
00155 int ury_on=0;
00156
00157 int llx_off=0;
00158 int lly_off=0;
00159 int urx_off=0;
00160 int ury_off=0;
00161 if(xsh_parameters_find(drs_params,"xsh_mdark","contam_on_llx")!=NULL){
00162 check(llx_on=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_on_llx"));
00163 check(lly_on=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_on_lly"));
00164 check(urx_on=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_on_urx"));
00165 check(ury_on=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_on_ury"));
00166
00167
00168 check(llx_off=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_off_llx"));
00169 check(lly_off=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_off_lly"));
00170 check(urx_off=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_off_urx"));
00171 check(ury_off=xsh_parameters_get_int(drs_params,"xsh_mdark","contam_off_ury"));
00172
00173 } else {
00174 if(xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
00175 llx_on =801; lly_on =1058;urx_on =810; ury_on =1067;
00176 llx_off=855; lly_off=1066;urx_off=864; ury_off=1075;
00177 } else if(xsh_instrument_get_arm(instr) == XSH_ARM_VIS) {
00178 llx_on =1672; lly_on =2672;urx_on =1681;ury_on =2681;
00179 llx_off=1926; lly_off=2941;urx_off=1935;ury_off=2950;
00180 } else if(xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
00181 llx_on =991; lly_on =1083;urx_on =1015;ury_on =1108;
00182 llx_off=1029;lly_off=1075;urx_off=1053;ury_off=1110;
00183 }
00184 }
00185
00186 llx_on=llx_on/pre->binx;
00187 lly_on=lly_on/pre->biny;
00188 urx_on=urx_on/pre->binx;
00189 ury_on=ury_on/pre->biny;
00190
00191 llx_off=llx_off/pre->binx;
00192 lly_off=lly_off/pre->biny;
00193 urx_off=urx_off/pre->binx;
00194 ury_off=ury_off/pre->biny;
00195
00196 check(contam=
00197 cpl_image_get_median_window(pre->data,llx_on,lly_on,urx_on,ury_on)-
00198 cpl_image_get_median_window(pre->data,llx_off,lly_off,urx_off,ury_off));
00199
00200
00201 cleanup:
00202
00203
00204 return contam;
00205
00206
00207 }
00208
00209
00221 static void
00222 set_masterdark_qc (xsh_pre * preFrame,
00223 xsh_instrument * instrument,
00224 cpl_parameterlist* drs_params,
00225 cpl_parameterlist* parameters,
00226 double exptime,
00227 cpl_frame* crh_frm, cpl_frame* bpm_frm)
00228 {
00229 int nx =0, ny = 0;
00230 double avg_value = 0.0;
00231 double median_value = 0.0;
00232 double stdev = 0.0;
00233 cpl_parameter* p=NULL;
00234
00235 int ref_llx=-1;
00236 int ref_lly=-1;
00237 int ref_urx=-1;
00238 int ref_ury=-1;
00239
00240
00241 int ron_llx=-1;
00242 int ron_lly=-1;
00243 int ron_urx=-1;
00244 int ron_ury=-1;
00245 int ron_nsamp=100;
00246 int ron_hsize=4;
00247
00248 int zone_fpn[4];
00249 int fpn_llx=-1;
00250 int fpn_lly=-1;
00251 int fpn_urx=-1;
00252 int fpn_ury=-1;
00253 int fpn_nsamp=100;
00254 int fpn_hsize=4;
00255
00256
00257 double qc_fpn_val=0;
00258 double qc_fpn_err=0;
00259 double contam=0;
00260
00261 int sx=0;
00262 int sy=0;
00263 const char* crh_name=NULL;
00264 const char* bpm_name=NULL;
00265 cpl_image* crh_ima=NULL;
00266 cpl_image* bpm_ima=NULL;
00267 int* pcrh=NULL;
00268 int* pbpm=NULL;
00269 int* pqua=NULL;
00270 int i=0;
00271 xsh_pre* dup_frm=NULL;
00272 bool bpm_found=false;
00273 bool crh_found=false;
00274
00275
00276 nx = xsh_pre_get_nx (preFrame);
00277 assure (nx != 0, cpl_error_get_code (), "Cant get X size");
00278 ny = xsh_pre_get_ny (preFrame);
00279 assure (ny != 0, cpl_error_get_code (), "Cant get Y size");
00280
00281 sx=cpl_image_get_size_x(preFrame->data);
00282 sy=cpl_image_get_size_y(preFrame->data);
00283 if(drs_params !=NULL){
00284 check(ref_llx=xsh_parameters_get_int(drs_params,"xsh_mdark","ref1_llx"));
00285 check(ref_lly=xsh_parameters_get_int(drs_params,"xsh_mdark","ref1_lly"));
00286 check(ref_urx=xsh_parameters_get_int(drs_params,"xsh_mdark","ref1_urx"));
00287 check(ref_ury=xsh_parameters_get_int(drs_params,"xsh_mdark","ref1_ury"));
00288 } else {
00289 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ref1_llx"));
00290 check(ref_llx = cpl_parameter_get_int(p));
00291
00292 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ref1_lly"));
00293 check(ref_lly = cpl_parameter_get_int(p));
00294
00295 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ref1_urx"));
00296 check(ref_urx = cpl_parameter_get_int(p));
00297
00298 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ref1_ury"));
00299 check(ref_ury = cpl_parameter_get_int(p));
00300 }
00301
00302
00303 if(ref_llx == -1) ref_llx=1;
00304 if(ref_lly == -1) ref_lly=1;
00305 if(ref_urx == -1) ref_urx=sx;
00306 if(ref_ury == -1) ref_ury=sy;
00307
00308 ref_llx = (ref_llx>0) ? ref_llx : 1;
00309 ref_lly = (ref_lly>0) ? ref_lly : 1;
00310 ref_urx = (ref_urx<sx) ? ref_urx : sx;
00311 ref_ury = (ref_ury<sy) ? ref_ury : sy;
00312
00313
00314 if(drs_params!=NULL){
00315
00316 check(ron_llx=xsh_parameters_get_int(drs_params,"xsh_mdark","ron_llx"));
00317 check(ron_lly=xsh_parameters_get_int(drs_params,"xsh_mdark","ron_lly"));
00318 check(ron_urx=xsh_parameters_get_int(drs_params,"xsh_mdark","ron_urx"));
00319 check(ron_ury=xsh_parameters_get_int(drs_params,"xsh_mdark","ron_ury"));
00320 check(ron_hsize=xsh_parameters_get_int(drs_params,"xsh_mdark","ron_hsize"));
00321 check(ron_nsamp=xsh_parameters_get_int(drs_params,"xsh_mdark","ron_nsamples"));
00322
00323 } else {
00324
00325 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ron_llx"));
00326 check(ron_llx = cpl_parameter_get_int(p));
00327
00328 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ron_lly"));
00329 check(ron_lly = cpl_parameter_get_int(p));
00330
00331 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ron_urx"));
00332 check(ron_urx = cpl_parameter_get_int(p));
00333
00334 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ron_urx"));
00335 check(ron_ury = cpl_parameter_get_int(p));
00336
00337 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ron_hsize"));
00338 check(ron_hsize = cpl_parameter_get_int(p));
00339
00340 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.ron_nsamples"));
00341 check(ron_nsamp = cpl_parameter_get_int(p));
00342
00343 }
00344
00345 if(ron_llx == -1) ron_llx=1;
00346 if(ron_lly == -1) ron_lly=1;
00347 if(ron_urx == -1) ron_urx=sx;
00348 if(ron_ury == -1) ron_ury=sy;
00349
00350 ron_llx = (ron_llx>0) ? ron_llx : 1;
00351 ron_lly = (ron_lly>0) ? ron_lly : 1;
00352 ron_urx = (ron_urx<sx) ? ron_urx : sx;
00353 ron_ury = (ron_ury<sy) ? ron_ury : sy;
00354
00355
00356
00357
00358 if(drs_params !=NULL){
00359 check(fpn_llx=xsh_parameters_get_int(drs_params,"xsh_mdark","fpn_llx"));
00360 check(fpn_lly=xsh_parameters_get_int(drs_params,"xsh_mdark","fpn_lly"));
00361 check(fpn_urx=xsh_parameters_get_int(drs_params,"xsh_mdark","fpn_urx"));
00362 check(fpn_ury=xsh_parameters_get_int(drs_params,"xsh_mdark","fpn_ury"));
00363 } else {
00364 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.fpn_llx"));
00365 check(fpn_llx = cpl_parameter_get_int(p));
00366
00367 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.fpn_lly"));
00368 check(fpn_lly = cpl_parameter_get_int(p));
00369
00370 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.fpn_urx"));
00371 check(fpn_urx = cpl_parameter_get_int(p));
00372
00373 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.fpn_urx"));
00374 check(fpn_ury = cpl_parameter_get_int(p));
00375
00376 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.fpn_hsize"));
00377 check(fpn_hsize = cpl_parameter_get_int(p));
00378
00379 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mdark.fpn_nsamples"));
00380 check(fpn_nsamp = cpl_parameter_get_int(p));
00381 }
00382
00383
00384
00385 if(fpn_llx == -1) fpn_llx=1;
00386 if(fpn_lly == -1) fpn_lly=1;
00387 if(fpn_urx == -1) fpn_urx=sx;
00388 if(fpn_ury == -1) fpn_ury=sy;
00389
00390 fpn_llx = (fpn_llx>0) ? fpn_llx : 1;
00391 fpn_lly = (fpn_lly>0) ? fpn_lly : 1;
00392 fpn_urx = (fpn_urx<sx) ? fpn_urx : sx;
00393 fpn_ury = (fpn_ury<sy) ? fpn_ury : sy;
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 dup_frm=xsh_pre_duplicate(preFrame);
00407
00408 if(crh_frm!=NULL) {
00409 check(crh_name=cpl_frame_get_filename(crh_frm));
00410 check(crh_ima=cpl_image_load(crh_name,XSH_PRE_QUAL_TYPE,0,0));
00411 check(pcrh=cpl_image_get_data_int(crh_ima));
00412 crh_found=true;
00413 }
00414
00415 if(bpm_frm != NULL) {
00416 check(bpm_name=cpl_frame_get_filename(bpm_frm));
00417 check(bpm_ima=cpl_image_load(bpm_name,XSH_PRE_QUAL_TYPE,0,0));
00418 check(pbpm=cpl_image_get_data_int(bpm_ima));
00419 bpm_found=true;
00420 }
00421
00422 if(bpm_found || crh_found) {
00423 check(pqua=cpl_image_get_data_int( preFrame->qual));
00424 sx=preFrame->nx;
00425 sy=preFrame->ny;
00426
00427 if(bpm_found && crh_found) {
00428 for(i=0;i<sx*sy;i++) {
00429 if(pcrh[i] != 0 || pbpm[i] != 0) pqua[i]=XSH_GOOD_PIXEL_LEVEL+1;
00430 }
00431 } else if (bpm_found) {
00432 for(i=0;i<sx*sy;i++) {
00433 if(pbpm[i] != 0) pqua[i]=XSH_GOOD_PIXEL_LEVEL+1;
00434 }
00435 } else if (crh_found) {
00436 for(i=0;i<sx*sy;i++) {
00437 if(pcrh[i] != 0) pqua[i]=XSH_GOOD_PIXEL_LEVEL+1;
00438 }
00439 }
00440 }
00441 xsh_free_image(&bpm_ima);
00442 xsh_free_image(&crh_ima);
00443 xsh_pre_free(&dup_frm);
00444
00445 check( xsh_pre_median_mean_stdev_window( preFrame,
00446 &avg_value, &median_value,&stdev,
00447 ref_llx,ref_lly,ref_urx,ref_ury) ) ;
00448
00449 xsh_pfits_set_qc_mdarkavg( preFrame->data_header,avg_value) ;
00450 xsh_pfits_set_qc_mdarkmed( preFrame->data_header,median_value) ;
00451 xsh_pfits_set_qc_mdarkrms( preFrame->data_header,stdev) ;
00452
00453
00454
00455
00456
00457 xsh_pfits_set_qc_mdarkavg( preFrame->qual_header,avg_value) ;
00458 xsh_pfits_set_qc_mdarkmed( preFrame->qual_header,median_value) ;
00459 xsh_pfits_set_qc_mdarkrms( preFrame->qual_header,stdev) ;
00460
00461
00462
00463
00464 zone_fpn[0]=fpn_llx;
00465 zone_fpn[1]=fpn_urx;
00466 zone_fpn[2]=fpn_lly;
00467 zone_fpn[3]=fpn_ury;
00468 check_msg(cpl_flux_get_noise_window(preFrame->data, zone_fpn, fpn_hsize,
00469 fpn_nsamp, &qc_fpn_val, &qc_fpn_err),
00470 "Error computing noise in a window");
00471
00472 xsh_pfits_set_qc_norm_fpn(preFrame->qual_header,qc_fpn_val) ;
00473 xsh_pfits_set_qc_norm_fpn_err(preFrame->qual_header,qc_fpn_err) ;
00474
00475 qc_fpn_val *= exptime;
00476 qc_fpn_err *= exptime;
00477
00478 check(xsh_pfits_set_qc_fpn( preFrame->qual_header,qc_fpn_val) );
00479
00480 check(contam=xsh_mdark_get_contam(preFrame,parameters,instrument));
00481 check(xsh_pfits_set_qc_contamination( preFrame->data_header,contam)) ;
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 cleanup:
00497
00498 xsh_free_image(&bpm_ima);
00499 xsh_free_image(&crh_ima);
00500 xsh_pre_free(&dup_frm);
00501
00502
00503 return;
00504 }
00505
00519 cpl_frame *
00520 xsh_create_master_dark (cpl_frame * medFrame, xsh_instrument* instr,
00521 cpl_parameterlist* parameters,
00522 cpl_parameterlist* drs_params,
00523 cpl_frame* crh_frm,cpl_frame* bpm_frm)
00524 {
00525
00526
00527
00528
00529
00530 xsh_pre *medPre = NULL;
00531 double exptime = 0.0;
00532 cpl_frame* resFrame = NULL;
00533 const char* tag = XSH_GET_TAG_FROM_ARM (XSH_MASTER_DARK, instr);
00534 char* finalName = NULL;
00535
00536 XSH_ASSURE_NOT_NULL(medFrame);
00537 XSH_ASSURE_NOT_NULL(instr);
00538
00539 check(medPre = xsh_pre_load (medFrame,instr));
00540 XSH_ASSURE_NOT_NULL(medPre);
00541
00542 exptime = medPre->exptime;
00543
00544
00545 if ( instr->arm != XSH_ARM_NIR ) {
00546 check_msg (cpl_image_divide_scalar (medPre->data, exptime),
00547 "Cant divide median image by exptime %lf", exptime);
00548 check_msg (cpl_image_divide_scalar (medPre->errs, exptime),
00549 "Cant divide median image by exptime %lf", exptime);
00550
00551
00552 xsh_msg_dbg_high ("Set EXPTIME = 1.");
00553 check(xsh_pfits_set_exptime (medPre->data_header, (double) 1.));
00554 check(xsh_pfits_set_exptime (medPre->errs_header, (double) 1.));
00555 }
00556
00557
00558 if ( xsh_instrument_get_arm(instr) == XSH_ARM_NIR ) {
00559 check(finalName =cpl_sprintf("masterdark_%s.fits",
00560 xsh_instrument_arm_tostring(instr)));
00561 } else {
00562
00563
00564
00565
00566
00567
00568
00569 check(finalName =cpl_sprintf("masterdark_%s.fits",
00570 xsh_instrument_arm_tostring(instr)));
00571 }
00572
00573
00574
00575
00576
00577 check(set_masterdark_qc (medPre, instr,drs_params,parameters,exptime,crh_frm,bpm_frm));
00578
00579
00580
00581
00582
00583
00584 check ( resFrame = xsh_pre_save (medPre, finalName, tag,1 ));
00585 assure (resFrame != NULL, cpl_error_get_code (), "Cant save PRE");
00586 xsh_msg_dbg_high ("PRE frame saved as file %s", finalName);
00587
00588 check(cpl_frame_set_level (resFrame, CPL_FRAME_LEVEL_FINAL));
00589 check(cpl_frame_set_tag (resFrame, tag));
00590
00591 cleanup:
00592 if (cpl_error_get_code() != CPL_ERROR_NONE){
00593 xsh_free_frame(&resFrame);
00594 }
00595 cpl_free(finalName);
00596 xsh_pre_free(&medPre);
00597 return resFrame;
00598 }
00599
00600
00601
00608
00609 static void
00610 reject_lo_hi(cpl_image *image, double min, double max)
00611 {
00612 cpl_mask *mask_lo = NULL;
00613 cpl_mask *mask_hi = NULL;
00614
00615 mask_lo = cpl_mask_threshold_image_create(image, -DBL_MAX, min);
00616 mask_hi = cpl_mask_threshold_image_create(image, max, DBL_MAX);
00617 assure_mem( mask_lo );
00618 assure_mem( mask_hi );
00619
00620 cpl_mask_or(mask_lo, mask_hi);
00621
00622 cpl_image_reject_from_mask(image, mask_lo);
00623
00624 cleanup:
00625 xsh_free_mask(&mask_lo);
00626 xsh_free_mask(&mask_hi);
00627 return;
00628 }
00629
00630 static int
00631 count_good(const cpl_image *image)
00632 {
00633 return
00634 cpl_image_get_size_x(image) * cpl_image_get_size_y(image) -
00635 cpl_image_count_rejected(image);
00636 }
00637
00638 static double
00639 get_masterbias_qc_structure_row_region(cpl_image * tima)
00640 {
00641
00642 cpl_image* avg_row=NULL;
00643 double min=0;
00644 double max=0;
00645 double struct_row=0;
00646
00647
00648
00649 check(avg_row = cpl_image_collapse_create(tima,0));
00650 check(cpl_image_divide_scalar(avg_row,cpl_image_get_size_y(tima)));
00651
00652
00653 min = cpl_image_get_mean(avg_row) - 2;
00654 max = cpl_image_get_mean(avg_row) + 2;
00655
00656
00657
00658
00659 check( reject_lo_hi(avg_row, min, max) );
00660 if (count_good(avg_row) >= 2)
00661 {
00662 check(struct_row = cpl_image_get_stdev(avg_row));
00663 }
00664 else
00665 {
00666 struct_row = -1;
00667 xsh_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
00668 count_good(avg_row));
00669 }
00670
00671 cleanup:
00672 xsh_free_image(&avg_row);
00673
00674
00675 return struct_row;
00676
00677 }
00678
00679
00680
00681
00682 static double
00683 get_masterbias_qc_structure_col_region(cpl_image * tima)
00684 {
00685
00686
00687 cpl_image* avg_col=NULL;
00688 double min=0;
00689 double max=0;
00690 double struct_col=0;
00691
00692 check(avg_col = cpl_image_collapse_create(tima,1));
00693 check(cpl_image_divide_scalar(avg_col,cpl_image_get_size_x(tima)));
00694
00695
00696 min = cpl_image_get_mean(avg_col) - 2;
00697 max = cpl_image_get_mean(avg_col) + 2;
00698
00699
00700
00701
00702 check( reject_lo_hi(avg_col, min, max) );
00703 if (count_good(avg_col) >= 2)
00704 {
00705 check(struct_col = cpl_image_get_stdev(avg_col));
00706 }
00707 else
00708 {
00709 struct_col = -1;
00710 xsh_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
00711 count_good(avg_col));
00712 }
00713
00714 cleanup:
00715 xsh_free_image(&avg_col);
00716
00717
00718 return struct_col;
00719
00720 }
00721
00722
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00796 static void
00797 set_masterbias_qc_uves_structure_region(xsh_pre * master,
00798 const int llx,
00799 const int lly,
00800 const int urx,
00801 const int ury,
00802 const int ref_x,
00803 const int ref_y,
00804 const int reg_id,
00805 const int dlevel)
00806 {
00807
00808 cpl_image* xima=NULL;
00809 cpl_image* yima=NULL;
00810 double struct_row=0;
00811 double struct_col=0;
00812
00813 double mean = 0.0;
00814 double median = 0.0;
00815 double stdev = 0.0;
00816
00817 double structx = 0.0;
00818 double structy = 0.0;
00819 int nb_images = 0;
00820
00821 check( xsh_pre_median_mean_stdev_window( master, &mean, &median,&stdev,
00822 llx,lly,urx,ury) ) ;
00823
00824 check(xsh_pfits_set_qc_mbiasavg (master->data_header, mean));
00825 check(xsh_pfits_set_qc_mbiasmed (master->data_header, median));
00826 check(xsh_pfits_set_qc_mbiasrms (master->data_header, stdev));
00827
00828
00829
00830 if(reg_id==1){
00831 check(xima=cpl_image_extract(master->data,llx,lly,urx,ref_y));
00832 } else {
00833 check(xima=cpl_image_extract(master->data,llx,ref_y,urx,ury));
00834 }
00835 if (0) {
00836
00837
00838
00839 check(cpl_image_threshold(xima,
00840 -DBL_MAX,median+3*stdev,
00841 -DBL_MAX,median+3*stdev));
00842 }
00843
00844 if(reg_id==1){
00845 check(yima=cpl_image_extract(master->data,llx,lly,ref_x,ury));
00846 } else {
00847 check(yima=cpl_image_extract(master->data,ref_x,lly,urx,ury));
00848 }
00849 if (0) {
00850
00851
00852
00853 check(cpl_image_threshold(yima,
00854 -DBL_MAX,median+3*stdev,
00855 -DBL_MAX,median+3*stdev));
00856 }
00857
00858 check(struct_row=get_masterbias_qc_structure_row_region(xima));
00859 structx=struct_row;
00860
00861 check(struct_col=get_masterbias_qc_structure_col_region(yima));
00862 structy=struct_col;
00863
00864
00865 if(reg_id==1) {
00866 check(xsh_pfits_set_qc_reg1_structx(master->data_header,structx));
00867 check(xsh_pfits_set_qc_reg1_structy(master->data_header,structy));
00868 } else {
00869 check(xsh_pfits_set_qc_reg2_structx(master->data_header,structx));
00870 check(xsh_pfits_set_qc_reg2_structy(master->data_header,structy));
00871 }
00872
00873 check( nb_images = xsh_pfits_get_datancom( master->data_header));
00874
00875
00876
00877
00878
00879 cleanup:
00880 xsh_free_image(&xima);
00881 xsh_free_image(&yima);
00882 return;
00883
00884 }
00885
00901 static void
00902 set_masterbias_qc_structure_region(xsh_pre * master,
00903 const int llx,
00904 const int lly,
00905 const int urx,
00906 const int ury,
00907 const int ref_x,
00908 const int ref_y,
00909 const int reg_id,
00910 const int dlevel)
00911 {
00912
00913
00914
00915
00916
00917
00918
00919 check(set_masterbias_qc_uves_structure_region(master,llx,lly,urx,ury,
00920 ref_x,ref_y,reg_id,dlevel));
00921
00922 cleanup:
00923 return;
00924
00925 }
00926
00927
00935 static void
00936 set_masterbias_qc_structure(xsh_pre * master, cpl_parameterlist* parameters)
00937 {
00938
00939 cpl_parameter* p=NULL;
00940 int ref_x=-1;
00941 int ref_y=-1;
00942
00943 int ref_llx=0;
00944 int ref_lly=0;
00945 int ref_urx=0;
00946 int ref_ury=0;
00947
00948 int sx=0;
00949 int sy=0;
00950 int dlevel=0;
00951
00952 sx=cpl_image_get_size_x(master->data);
00953 sy=cpl_image_get_size_y(master->data);
00954
00955 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.struct_refx"));
00956 check(ref_x = cpl_parameter_get_int(p));
00957
00958 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.struct_refy"));
00959 check(ref_y = cpl_parameter_get_int(p));
00960
00961 check(dlevel=xsh_parameters_debug_level_get("xsh_mbias",parameters));
00962
00963 ref_llx=1;ref_urx=sx;
00964 ref_lly=1;ref_ury=sy;
00965
00966 if(ref_x == -1) {
00967 ref_x=sx/2;
00968 }
00969
00970 if(ref_y == -1) {
00971 ref_y=sy/2;
00972 }
00973
00974 ref_x = (ref_x>0) ? ref_x : 1;
00975 ref_y = (ref_y>0) ? ref_y : 1;
00976 ref_x = (ref_x<sx) ? ref_x : sx;
00977 ref_y = (ref_y<sy) ? ref_y : sy;
00978
00979 set_masterbias_qc_structure_region(master,ref_llx,ref_lly,ref_urx,ref_ury,
00980 ref_x,ref_y,1,dlevel);
00981
00982
00983 set_masterbias_qc_structure_region(master,ref_llx,ref_lly,ref_urx,ref_ury,
00984 ref_x,ref_y,2,dlevel);
00985
00986
00987 cleanup:
00988 return;
00989
00990 }
00991
00992
00993
00994
00995
00996
00997
01017 cpl_frame *
01018 xsh_create_master_bias (cpl_frameset* raws,
01019 cpl_frame * frame,
01020 xsh_instrument* instr,
01021 cpl_parameterlist* parameters)
01022 {
01023 xsh_pre *master = NULL;
01024 cpl_frame *masterFrame = NULL;
01025 const char* tag = XSH_GET_TAG_FROM_ARM( XSH_MASTER_BIAS, instr);
01026 char mastername[256];
01027 int binx=0;
01028 int biny=0;
01029
01030
01031
01032
01033 XSH_ASSURE_NOT_NULL(frame);
01034 check(master = xsh_pre_load (frame,instr));
01035
01036 check(xsh_pfits_set_pcatg (master->data_header, tag));
01037
01038 check(binx=xsh_pfits_get_binx(master->data_header));
01039 check(biny=xsh_pfits_get_biny(master->data_header));
01040
01041 if ( parameters != NULL ) {
01042 check( set_masterbias_qc_structure(master,parameters));
01043 if(cpl_frameset_get_size(raws) > 1 ) {
01044 check(xsh_compute_ron_mbias(raws,master,parameters));
01045 check(xsh_compute_fpn_mbias(raws,master,instr,parameters));
01046 }
01047 }
01048
01049
01050
01051
01052
01053 sprintf(mastername,"MASTER_BIAS_%s.fits",
01054 xsh_instrument_arm_tostring (instr));
01055
01056 xsh_msg ("Create master bias");
01057 check(masterFrame = xsh_pre_save (master, mastername,tag,1 ));
01058 cpl_frame_set_tag (masterFrame, tag);
01059 cpl_frame_set_group(masterFrame,CPL_FRAME_GROUP_CALIB);
01060
01061 cleanup:
01062 xsh_pre_free(&master);
01063 return masterFrame;
01064 }
01065
01066 cpl_frame *
01067 xsh_create_master_flat (cpl_frame *frame, xsh_instrument *instr)
01068 {
01069 xsh_pre *master = NULL;
01070 cpl_frame *masterFrame = NULL;
01071 const char *tag = NULL;
01072 char *name = NULL;
01073 double mean = 0.0;
01074 double median = 0.0;
01075 double stdev = 0.0;
01076
01077
01078 XSH_ASSURE_NOT_NULL( frame);
01079 XSH_ASSURE_NOT_NULL( instr);
01080
01081 check( master = xsh_pre_load( frame, instr));
01082
01083
01084 tag = XSH_GET_TAG_FROM_LAMP( XSH_MASTER_FLAT, instr);
01085 check( xsh_pfits_set_pcatg( master->data_header, tag));
01086
01087 check( xsh_pre_normalize( master));
01088
01089 mean = cpl_image_get_mean( master->data);
01090 median = cpl_image_get_median( master->data);
01091 stdev = cpl_image_get_stdev( master->data);
01092
01093
01094
01095
01096
01097 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT",".fits",instr);
01098 xsh_msg ("Create master flat %s tag %s", name, tag);
01099
01100 check( masterFrame = xsh_pre_save( master, name, tag,1));
01101 check( cpl_frame_set_tag( masterFrame, tag));
01102 check( cpl_frame_set_group( masterFrame, CPL_FRAME_GROUP_CALIB));
01103
01104 cleanup:
01105 XSH_FREE( name);
01106 xsh_pre_free( &master);
01107 return masterFrame;
01108 }
01109
01110
01111 cpl_frame *
01112 xsh_create_master_bpmap (cpl_frame * mdarkFrame, xsh_instrument* instr)
01113 {
01114
01115
01116
01117
01118
01119 xsh_pre * mdark_pre = NULL ;
01120 cpl_frame* resFrame = NULL;
01121 const char* tag = XSH_GET_TAG_FROM_ARM( XSH_MASTER_BP_MAP, instr);
01122 char* finalName = NULL;
01123 int i=0, j=0;
01124 int* qual_data = NULL;
01125 int nx,ny;
01126 int nb_flag_pixels = 0;
01127 int error_pixels[32];
01128
01129
01130 XSH_ASSURE_NOT_NULL( mdarkFrame);
01131 XSH_ASSURE_NOT_NULL( instr);
01132
01133
01134 check( mdark_pre = xsh_pre_load( mdarkFrame, instr));
01135
01136 if ( xsh_instrument_get_arm(instr) == XSH_ARM_NIR ) {
01137 check(finalName=cpl_sprintf("MASTER_BP_MAP_%s.fits",
01138 xsh_instrument_arm_tostring(instr)));
01139
01140 } else {
01141
01142
01143
01144
01145
01146
01147 check(finalName=cpl_sprintf("MASTER_BP_MAP_%s.fits",
01148 xsh_instrument_arm_tostring(instr)));
01149
01150 }
01151
01152 check(resFrame=xsh_frame_product(finalName,tag,
01153 CPL_FRAME_TYPE_IMAGE,
01154 CPL_FRAME_GROUP_PRODUCT,
01155 CPL_FRAME_LEVEL_FINAL));
01156
01157
01158 check( qual_data = cpl_image_get_data_int( mdark_pre->qual));
01159 check( nx = cpl_image_get_size_x( mdark_pre->qual));
01160 check( ny = cpl_image_get_size_y( mdark_pre->qual));
01161
01162
01163 for(j=0; j<32; j++){
01164 error_pixels[j] = 0;
01165 }
01166
01167 for(i=0; i<nx*ny; i++){
01168 if( qual_data[i] > 0){
01169 nb_flag_pixels++;
01170 for( j=0;j<32;j++){
01171 int cmp = pow(2,j);
01172 if ( qual_data[i] & cmp){
01173 error_pixels[j]++;
01174 }
01175 }
01176 }
01177 }
01178
01179
01180
01181 check( xsh_pfits_set_qc_bp_map_ntotal(mdark_pre->qual_header,nb_flag_pixels));
01182
01183 for(i=1; i< 33; i++){
01184 check( xsh_pfits_set_qc_multi( mdark_pre->qual_header,
01185 (void *)&(error_pixels[i-1]), XSH_QC_BP_MAP_NFLAGi, instr, i));
01186 }
01187
01188
01189 check_msg (cpl_image_save (mdark_pre->qual, finalName, XSH_PRE_DATA_BPP,
01190 mdark_pre->qual_header, CPL_IO_DEFAULT),
01191 "Could not save bpmap to %s extension", finalName);
01192
01193
01194 cleanup:
01195 if (cpl_error_get_code() != CPL_ERROR_NONE){
01196 xsh_free_frame(&resFrame);
01197 }
01198 XSH_FREE(finalName);
01199 xsh_pre_free( &mdark_pre);
01200 return resFrame;
01201 }
01202
01203 static cpl_error_code
01204 xsh_compute_ron_mbias(cpl_frameset* raws,
01205 xsh_pre* master,
01206 cpl_parameterlist* parameters)
01207 {
01208
01209 const char* ron_method="ALL";
01210 int random_sizex=-1;
01211 int random_nsamples=-1;
01212 double ron=0;
01213 double ron_err=0;
01214
01215
01216 int ref_llx=-1;
01217 int ref_lly=-1;
01218 int ref_urx=-1;
01219 int ref_ury=-1;
01220
01221
01222 int ref2_llx=-1;
01223 int ref2_lly=-1;
01224 int ref2_urx=-1;
01225 int ref2_ury=-1;
01226
01227
01228 cpl_parameter* p=NULL;
01229
01230 const cpl_frame * fr = NULL;
01231
01232 cpl_propertylist * plist = NULL;
01233
01234 int naxis1 = 0;
01235 int naxis2 = 0;
01236
01237 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ron_method"));
01238 check(ron_method = cpl_parameter_get_string(p));
01239
01240 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.random_sizex"));
01241 check(random_sizex = cpl_parameter_get_int(p));
01242
01243 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.random_nsamples"));
01244 check(random_nsamples = cpl_parameter_get_int(p));
01245
01246 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_llx"));
01247 check(ref_llx = cpl_parameter_get_int(p));
01248
01249 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_lly"));
01250 check(ref_lly = cpl_parameter_get_int(p));
01251
01252 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_urx"));
01253 check(ref_urx = cpl_parameter_get_int(p));
01254
01255 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_urx"));
01256 check(ref_ury = cpl_parameter_get_int(p));
01257
01258 check(fr=cpl_frameset_get_first_const(raws));
01259
01260 check(plist=cpl_propertylist_load(cpl_frame_get_filename(fr),0));
01261 check(naxis1 = cpl_propertylist_get_int(plist, "NAXIS1"));
01262 check(naxis2 = cpl_propertylist_get_int(plist, "NAXIS2"));
01263
01264
01265 cpl_propertylist_delete(plist);
01266
01267
01268 if(ref_llx == -1) ref_llx = naxis1 / 8;
01269 if(ref_lly == -1) ref_lly = naxis2 / 8;
01270 if(ref_urx == -1) ref_urx = naxis1 * 7 / 8;
01271 if(ref_ury == -1) ref_ury = naxis2 * 7 / 8;
01272
01273
01274 check(xsh_compute_ron(raws,ref_llx,ref_lly,ref_urx,ref_ury,random_nsamples,random_sizex,1,&ron,&ron_err));
01275 xsh_pfits_set_qc_ron1(master->data_header,ron);
01276 xsh_pfits_set_qc_ron1_err(master->data_header,ron_err);
01277
01278
01279 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_llx"));
01280 check(ref2_llx = cpl_parameter_get_int(p));
01281
01282 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_lly"));
01283 check(ref2_lly = cpl_parameter_get_int(p));
01284
01285 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_urx"));
01286 check(ref2_urx = cpl_parameter_get_int(p));
01287
01288 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_urx"));
01289 check(ref2_ury = cpl_parameter_get_int(p));
01290
01291
01292
01293 if(ref2_llx == -1) ref2_llx = naxis1 / 8;
01294 if(ref2_lly == -1) ref2_lly = naxis2 / 8;
01295 if(ref2_urx == -1) ref2_urx = naxis1 * 7 / 8;
01296 if(ref2_ury == -1) ref2_ury = naxis2 * 7 / 8;
01297
01298 if(
01299 (ref2_llx != ref_llx) ||
01300 (ref2_lly != ref_lly) ||
01301 (ref2_urx != ref_urx) ||
01302 (ref2_ury != ref_ury)
01303 ) {
01304
01305 check(xsh_compute_ron(raws,ref2_llx,ref2_lly,ref2_urx,ref2_ury,random_nsamples,random_sizex,2,&ron,&ron_err));
01306
01307 xsh_pfits_set_qc_ron2(master->data_header,ron);
01308 xsh_pfits_set_qc_ron2_err(master->data_header,ron_err);
01309
01310 }
01311
01312
01313 cleanup:
01314
01315 return cpl_error_get_code();
01316
01317 }
01318
01319
01320 static cpl_error_code
01321 xsh_compute_fpn_mbias(cpl_frameset* raws,
01322 xsh_pre* master,
01323 xsh_instrument* instrument,
01324 cpl_parameterlist* parameters)
01325 {
01326
01327 int nraws=0;
01328 double ron=0;
01329 double fpn=0;
01330 cpl_frame* frm1=NULL;
01331 cpl_frame* frm2=NULL;
01332 cpl_image* ima1=NULL;
01333 cpl_image* ima2=NULL;
01334 cpl_image* ima=NULL;
01335
01336
01337 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
01338 nraws=cpl_frameset_get_size(raws);
01339 if(nraws > 1) {
01340 frm1=cpl_frameset_get_first(raws);
01341 frm2=cpl_frameset_get_next(raws);
01342 ima1=cpl_image_load(cpl_frame_get_filename(frm1),CPL_TYPE_FLOAT,0,0);
01343 ima2=cpl_image_load(cpl_frame_get_filename(frm2),CPL_TYPE_FLOAT,0,0);
01344
01345 ima = cpl_image_duplicate(ima1);
01346 check(ron = xsh_image_get_stdev_clean(ima, NULL) / sqrt(2.0));
01347
01348 check(fpn = xsh_fixed_pattern_noise_bias(ima1,ima2,ron));
01349
01350 xsh_pfits_set_qc_ron_master(master->data_header,ron);
01351 xsh_pfits_set_qc_fpn_master(master->data_header,fpn);
01352
01353 xsh_free_image(&ima);
01354 xsh_free_image(&ima1);
01355 xsh_free_image(&ima2);
01356 }
01357 } else {
01358 check(xsh_mbias_get_fpn(master->data,parameters,&fpn));
01359 xsh_msg("Fixed Pattern Noise=%f",fpn);
01360
01361 }
01362
01363 cleanup:
01364 xsh_free_image(&ima);
01365 xsh_free_image(&ima1);
01366 xsh_free_image(&ima2);
01367 return cpl_error_get_code();
01368
01369 }
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383 static cpl_error_code
01384 xsh_my_detmon_ronbias_random(const cpl_imagelist * rawbiases,
01385 const cpl_image * masterbias,
01386 cpl_propertylist * qclist,
01387 const char* method,
01388 const int random_sizex,
01389 const int random_nsamples)
01390 {
01391 int nraws = cpl_imagelist_get_size(rawbiases);
01392 int i;
01393 double bias = DBL_MAX;
01394 double bias_error;
01395
01396 double ron_error;
01397 double *ron =
01398 (double *) cpl_malloc(sizeof(double) * nraws);
01399
01400 cpl_vector *v;
01401 double stdev;
01402 cpl_error_code error;
01403
01404
01405
01406 if(!strcmp(method, "DIF")) {
01407 nraws--;
01408
01409
01410 for(i = 0; i < nraws; i++) {
01411 const cpl_image *c1_raw =
01412 cpl_imagelist_get_const(rawbiases, i);
01413 const cpl_image *c2_raw =
01414 cpl_imagelist_get_const(rawbiases, i + 1);
01415
01416 const cpl_image *c_raw = cpl_image_subtract_create(c1_raw,
01417 c2_raw);
01418 cpl_flux_get_noise_window(c_raw, NULL,
01419 random_sizex / 2,
01420 random_nsamples,
01421 ron + i, &ron_error);
01422 cpl_image_delete((cpl_image*)c_raw);
01423 }
01424
01425 } else {
01426 for(i = 0; i < nraws; i++) {
01427 const cpl_image *c_raw = cpl_imagelist_get_const(rawbiases, i);
01428 cpl_flux_get_noise_window(c_raw, NULL,
01429 random_sizex / 2,
01430 random_nsamples,
01431 ron + i, &ron_error);
01432 }
01433 }
01434
01435
01436
01437
01438
01439 xsh_flux_get_bias_window(masterbias, NULL,
01440 random_sizex / 2,
01441 random_nsamples,
01442 &bias, &bias_error);
01443
01444 v = cpl_vector_wrap(nraws, ron);
01445 stdev = cpl_vector_get_median_const(v);
01446 cpl_vector_unwrap(v);
01447
01448 cpl_free(ron);
01449
01450 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS RANDOM VAL", bias);
01451 cpl_ensure_code(!error, error);
01452 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS RANDOM RON", stdev);
01453 cpl_ensure_code(!error, error);
01454 return cpl_error_get_code();
01455 }
01456
01457
01458
01459
01460 static cpl_error_code
01461 xsh_my_detmon_ronbias_histo(const cpl_imagelist * rawbiases,
01462 const cpl_image * masterbias,
01463 cpl_propertylist * qclist,
01464 const char* method)
01465 {
01466 int nraws = cpl_imagelist_get_size(rawbiases);
01467 int i;
01468
01469 double mbias = DBL_MAX;
01470 double mfwhm = DBL_MAX;
01471 double mmax = DBL_MAX;
01472
01473 cpl_vector * fwhms;
01474 cpl_vector * maxs;
01475
01476 double mean_fwhm = DBL_MAX;
01477
01478 if(!strcmp(method, "DIF")) nraws--;
01479
01480 fwhms = cpl_vector_new(nraws);
01481 maxs = cpl_vector_new(nraws);
01482
01483 for(i = 0; i < nraws; i++) {
01484
01485 const cpl_image * c_raw;
01486 double bias = DBL_MAX;
01487 double fwhm = DBL_MAX;
01488 double max = DBL_MAX;
01489
01490 if(strcmp(method, "DIF")) {
01491 c_raw = cpl_imagelist_get_const(rawbiases, i);
01492 } else {
01493 const cpl_image *c1_raw = cpl_imagelist_get_const(rawbiases, i);
01494 const cpl_image * c2_raw = cpl_imagelist_get_const(rawbiases, i+1);
01495 c_raw = cpl_image_subtract_create(c1_raw, c2_raw);
01496 }
01497
01498 skip_if(xsh_detmon_ronbias_histo_reduce(c_raw, &bias, &fwhm, &max));
01499
01500 skip_if(bias == DBL_MAX || fwhm == DBL_MAX || max == DBL_MAX);
01501
01502 if(!strcmp(method, "DIF"))
01503 cpl_image_delete((cpl_image *)c_raw);
01504
01505 skip_if(cpl_vector_set(maxs, i, max));
01506 skip_if(cpl_vector_set(fwhms, i, fwhm));
01507
01508
01509
01510
01511
01512
01513 }
01514
01515 skip_if(cpl_vector_divide_scalar(fwhms, HIST_FACT));
01516
01517 xsh_detmon_ronbias_histo_reduce(masterbias, &mbias, &mfwhm, &mmax);
01518
01519 cpl_msg_warning(cpl_func,"mfwhm=%f",mfwhm);
01520 skip_if(mbias == DBL_MAX || mfwhm == DBL_MAX || mmax == DBL_MAX);
01521
01522 skip_if(cpl_propertylist_append_double(qclist, "ESO QC BIAS HISTO VAL",
01523 mbias));
01524 mean_fwhm = cpl_vector_get_mean(fwhms);
01525
01526 skip_if(mean_fwhm == DBL_MAX);
01527 skip_if(cpl_error_get_code());
01528
01529 skip_if(cpl_propertylist_append_double(qclist, "ESO QC BIAS HISTO RON",
01530 mean_fwhm));
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541 end_skip;
01542
01543 cpl_vector_delete(fwhms);
01544 cpl_vector_delete(maxs);
01545
01546 return cpl_error_get_code();
01547 }
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561 static cpl_error_code
01562 xsh_my_detmon_ronbias_region(const cpl_imagelist * rawbiases,
01563 const cpl_image * masterbias,
01564 cpl_propertylist * qclist,
01565 const char* pmethod,
01566 const int ref_llx,
01567 const int ref_lly,
01568 const int ref_urx,
01569 const int ref_ury,
01570 const int stacking_ks_low,
01571 const int stacking_ks_iter)
01572 {
01573
01574 int nraws = cpl_imagelist_get_size(rawbiases);
01575 int i;
01576
01577 int nx =
01578 cpl_image_get_size_x(cpl_imagelist_get_const(rawbiases, 0));
01579 int ny =
01580 cpl_image_get_size_y(cpl_imagelist_get_const(rawbiases, 0));
01581
01582 cpl_vector *rmssreg;
01583 cpl_error_code error;
01584
01585 const cpl_image * c_raw;
01586 double median, mbias, mstdev;
01587
01588 if(!strcmp(pmethod, "DIF")) nraws--;
01589
01590 rmssreg = cpl_vector_new(nraws);
01591
01592 if(nx < ref_urx ||
01593 ny < ref_ury) {
01594 cpl_msg_warning(cpl_func, "REGION method not applied. Given "
01595 "limits of prescan and overscan area "
01596 "exceed image size. Please check and rerun.");
01597 return CPL_ERROR_NONE;
01598 }
01599
01600 for(i = 0; i < nraws; i++) {
01601 double mean = 0;
01602 double stdev = 0;
01603 if(strcmp(pmethod, "DIF")) {
01604 c_raw = cpl_imagelist_get_const(rawbiases, i);
01605 } else {
01606 const cpl_image *c1_raw = cpl_imagelist_get_const(rawbiases, i);
01607 const cpl_image * c2_raw = cpl_imagelist_get_const(rawbiases, i+1);
01608 c_raw = cpl_image_subtract_create(c1_raw, c2_raw);
01609 }
01610 error = xsh_ksigma_clip(c_raw,
01611 ref_llx,
01612 ref_lly,
01613 ref_urx,
01614 ref_ury,
01615 (double) stacking_ks_low,
01616 stacking_ks_iter, 1e-5,
01617 &mean, &stdev);
01618
01619 cpl_ensure_code(!error, error);
01620
01621
01622
01623
01624
01625
01626 error = cpl_vector_set(rmssreg, i, stdev);
01627 cpl_ensure_code(!error, error);
01628 if(!strcmp(pmethod, "DIF")) cpl_image_delete((cpl_image *)c_raw);
01629 }
01630
01631 median = cpl_image_get_median_window(masterbias,
01632 ref_llx,
01633 ref_lly,
01634 ref_urx,
01635 ref_ury);
01636 error = xsh_ksigma_clip(masterbias,
01637 ref_llx,
01638 ref_lly,
01639 ref_urx,
01640 ref_ury,
01641 (double) stacking_ks_low,
01642 stacking_ks_iter, 1e-5,
01643 &mbias, &mstdev);
01644
01645
01646 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS REGION MED",
01647 median);
01648 cpl_ensure_code(!error, error);
01649
01650 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS REGION VAL",
01651 mbias);
01652 cpl_ensure_code(!error, error);
01653 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS REGION RON",
01654 cpl_vector_get_mean(rmssreg));
01655 cpl_ensure_code(!error, error);
01656
01657
01658
01659
01660
01661
01662 cpl_vector_delete(rmssreg);
01663
01664 return cpl_error_get_code();
01665 }
01666
01667 static cpl_error_code
01668 xsh_mbias_get_fpn(const cpl_image* ima,
01669 cpl_parameterlist* parameters,
01670 double* fpn)
01671 {
01672
01673 int fpn_llx=0;
01674 int fpn_lly=0;
01675 int fpn_urx=0;
01676 int fpn_ury=0;
01677 int fpn_hsize=0;
01678 int fpn_nsamp=0;
01679 cpl_parameter* p=NULL;
01680
01681 int zone[4];
01682 int sx=0;
01683 int sy=0;
01684
01685 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_llx"));
01686 check(fpn_llx = cpl_parameter_get_int(p));
01687
01688 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_lly"));
01689 check(fpn_lly = cpl_parameter_get_int(p));
01690
01691 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_urx"));
01692 check(fpn_urx = cpl_parameter_get_int(p));
01693
01694 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_ury"));
01695 check(fpn_ury = cpl_parameter_get_int(p));
01696
01697 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_hsize"));
01698 check(fpn_hsize = cpl_parameter_get_int(p));
01699
01700 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_nsamples"));
01701 check(fpn_nsamp = cpl_parameter_get_int(p));
01702
01703 sx=cpl_image_get_size_x(ima);
01704 sy=cpl_image_get_size_y(ima);
01705
01706 fpn_llx=(fpn_llx>0) ? fpn_llx: 0;
01707 fpn_lly=(fpn_lly>0) ? fpn_lly: 0;
01708
01709 fpn_urx=(fpn_urx<sx) ? fpn_urx: sx;
01710 fpn_ury=(fpn_ury<sy) ? fpn_ury: sy;
01711
01712
01713 zone[0]=fpn_llx;
01714 zone[1]=fpn_urx;
01715 zone[2]=fpn_lly;
01716 zone[3]=fpn_ury;
01717
01718
01719 check_msg(cpl_flux_get_noise_window(ima,zone,fpn_hsize,fpn_nsamp,fpn,NULL),
01720 "Error computing noise in a window");
01721
01722 cleanup:
01723 return cpl_error_get_code();
01724
01725 }
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740 static cpl_error_code
01741 xsh_my_detmon_ronbias_preoverscan(const cpl_imagelist * rawbiases,
01742
01743 cpl_propertylist * qclist,
01744 cpl_image ** synthetic,
01745 const int prescan_llx,
01746 const int prescan_lly,
01747 const int prescan_urx,
01748 const int prescan_ury,
01749 const int overscan_llx,
01750 const int overscan_lly,
01751 const int overscan_urx,
01752 const int overscan_ury,
01753 const int stacking_ks_low,
01754 const int stacking_ks_iter)
01755 {
01756 int i;
01757 int nx, ny;
01758 int nraws;
01759
01760 cpl_vector *meanspre;
01761 cpl_vector *medspre;
01762 cpl_vector *rmsspre;
01763 cpl_vector *meansover;
01764 cpl_vector *medsover;
01765 cpl_vector *rmssover;
01766
01767 cpl_error_code error;
01768
01769 nraws = cpl_imagelist_get_size(rawbiases);
01770 cpl_ensure_code(nraws != -1, CPL_ERROR_ILLEGAL_INPUT);
01771
01772 meanspre = cpl_vector_new(nraws);
01773 medspre = cpl_vector_new(nraws);
01774 rmsspre = cpl_vector_new(nraws);
01775 meansover = cpl_vector_new(nraws);
01776 medsover = cpl_vector_new(nraws);
01777 rmssover = cpl_vector_new(nraws);
01778
01779 nx = cpl_image_get_size_x(cpl_imagelist_get_const(rawbiases, 0));
01780 ny = cpl_image_get_size_y(cpl_imagelist_get_const(rawbiases, 0));
01781 cpl_ensure_code(nx != -1 && ny != -1, CPL_ERROR_ILLEGAL_INPUT);
01782
01783 if(nx < prescan_urx ||
01784 nx < overscan_urx ||
01785 ny < prescan_ury ||
01786 ny < overscan_ury) {
01787 cpl_msg_warning(cpl_func, "PREOVERSCAN method not applied. Given "
01788 "limits of prescan and overscan area "
01789 "exceed image size. Please check and rerun.");
01790 return CPL_ERROR_NONE;
01791 }
01792
01793 for(i = 0; i < nraws; i++) {
01794 double mean = 0;
01795 double stdev = 0;
01796
01797 cpl_image *prescan = NULL;
01798 cpl_image *overscan = NULL;
01799
01800 const cpl_image *c_raw = cpl_imagelist_get_const(rawbiases, i);
01801
01802 cpl_ensure_code(c_raw != NULL, CPL_ERROR_ILLEGAL_INPUT);
01803
01804 prescan =
01805 cpl_image_extract(c_raw,
01806 prescan_llx,
01807 prescan_lly,
01808 prescan_urx,
01809 prescan_ury);
01810 cpl_ensure_code(prescan != NULL, CPL_ERROR_ILLEGAL_INPUT);
01811 overscan =
01812 cpl_image_extract(c_raw,
01813 overscan_llx,
01814 overscan_lly,
01815 overscan_urx,
01816 overscan_ury);
01817 cpl_ensure_code(overscan != NULL, CPL_ERROR_ILLEGAL_INPUT);
01818
01819 if(i == 0) {
01820 *synthetic = xsh_detmon_build_synthetic(prescan, overscan);
01821 cpl_msg_info(cpl_func, "Creating SYNTHETIC frame");
01822 if(*synthetic == NULL) {
01823 cpl_msg_error(cpl_func, "Error creating SYNTHETIC frame");
01824 return CPL_ERROR_UNSPECIFIED;
01825 }
01826 }
01827
01828 error = xsh_ksigma_clip(c_raw,prescan_llx,prescan_lly,
01829 prescan_urx,prescan_ury,
01830 (double) stacking_ks_low,stacking_ks_iter,
01831 1e-5,&mean, &stdev);
01832 cpl_ensure_code(!error, error);
01833
01834 cpl_ensure_code(mean != 0 && stdev != 0, CPL_ERROR_UNSPECIFIED);
01835
01836 error = cpl_vector_set(medspre, i, cpl_image_get_median(prescan));
01837 cpl_ensure_code(!error, error);
01838
01839
01840 error = cpl_vector_set(meanspre, i, mean);
01841 cpl_ensure_code(!error, error);
01842 error = cpl_vector_set(rmsspre, i, stdev);
01843 cpl_ensure_code(!error, error);
01844 error = xsh_ksigma_clip(c_raw,overscan_llx,overscan_lly,
01845 overscan_urx,overscan_ury,
01846 (double) stacking_ks_low,stacking_ks_iter,
01847 1e-5,&mean, &stdev);
01848
01849 cpl_ensure_code(!error, error);
01850
01851 cpl_ensure_code(mean != 0 && stdev != 0, CPL_ERROR_UNSPECIFIED);
01852
01853 error = cpl_vector_set(medsover, i, cpl_image_get_median(overscan));
01854 cpl_ensure_code(!error, error);
01855
01856
01857
01858 error = cpl_vector_set(meansover, i, mean);
01859 cpl_ensure_code(!error, error);
01860 error = cpl_vector_set(rmssover, i, stdev);
01861 cpl_ensure_code(!error, error);
01862
01863 cpl_image_delete(prescan);
01864 cpl_image_delete(overscan);
01865 }
01866
01867 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS PRESCAN MEAN",
01868 cpl_vector_get_mean(meanspre));
01869 cpl_ensure_code(!error, error);
01870 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS PRESCAN MED",
01871 cpl_vector_get_mean(medspre));
01872 cpl_ensure_code(!error, error);
01873 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS PRESCAN RON",
01874 cpl_vector_get_mean(rmsspre));
01875 cpl_ensure_code(!error, error);
01876
01877 error =
01878 cpl_propertylist_append_double(qclist, "ESO QC BIAS OVERSCAN MEAN",
01879 cpl_vector_get_mean(meansover));
01880 cpl_ensure_code(!error, error);
01881 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS OVERSCAN MED",
01882 cpl_vector_get_mean(medsover));
01883 cpl_ensure_code(!error, error);
01884 error = cpl_propertylist_append_double(qclist, "ESO QC BIAS OVERSCAN RON",
01885 cpl_vector_get_mean(rmssover));
01886 cpl_ensure_code(!error, error);
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924 cpl_vector_delete(meanspre);
01925 cpl_vector_delete(medspre);
01926 cpl_vector_delete(rmsspre);
01927 cpl_vector_delete(meansover);
01928 cpl_vector_delete(medsover);
01929 cpl_vector_delete(rmssover);
01930
01931 return CPL_ERROR_NONE;
01932 }
01933
01934