229 #include <uves_mbias_impl.h>
231 #include <uves_utils.h>
232 #include <uves_corrbadpix.h>
233 #include <uves_parameters.h>
235 #include <uves_dfs.h>
236 #include <uves_pfits.h>
237 #include <uves_qclog.h>
238 #include <uves_recipe.h>
239 #include <uves_utils_wrappers.h>
240 #include <uves_error.h>
241 #include <irplib_mkmaster.h>
242 #include <uves_msg.h>
253 static void uves_mbias_qclog(
const cpl_imagelist* raw_imgs,
256 const cpl_image* mbia,
263 uves_mbias_qc_ron_raw(
const cpl_image* rbia,
272 uves_mbias_define_parameters(cpl_parameterlist *parameters);
277 #define cpl_plugin_get_info uves_mbias_get_info
279 UVES_MBIAS_ID, UVES_MBIAS_DOM,
282 uves_mbias_define_parameters,
283 "Jonas M. Larsen",
"cpl@eso.org",
284 "Creates the master bias frame",
285 "This recipe creates a master bias frame by computing the median of all input\n"
286 "bias frames. All input frames must have same tag and size and must be either\n"
287 "BIAS_BLUE or BIAS_RED.\n"
288 "On blue input the recipe computes one master bias frame; on red input a \n"
289 "master bias frame for each chip is produced. The average, standard deviation\n"
290 "and median of the master bias image(s) are written to the FITS header(s)");
306 int uves_mbias_define_parameters_body(cpl_parameterlist *parameters,
307 const char *recipe_id)
313 if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
322 if (uves_corr_traps_define_parameters(parameters,recipe_id)
332 if (uves_master_stack_define_parameters(parameters,recipe_id)
338 return (cpl_error_get_code() != CPL_ERROR_NONE);
351 uves_mbias_define_parameters(cpl_parameterlist *parameters)
353 return uves_mbias_define_parameters_body(parameters, make_str(UVES_MBIAS_ID));
379 uves_mbias_process_chip(
const cpl_imagelist *raw_images,
385 const char* STACK_METHOD,
386 const double STACK_KLOW,
387 const double STACK_KHIGH,
388 const int STACK_NITER)
390 cpl_image *master_bias = NULL;
391 double exposure_time = 0;
392 int badpixels_cleaned;
395 bool red_ccd_is_new=
false;
396 cpl_vector* bias_levels=NULL;
399 uves_msg(
"Calculating master bias...");
403 if(strcmp(STACK_METHOD,
"MEDIAN")==0) {
413 nraw=cpl_imagelist_get_size(raw_images);
414 for (i = 0; i < nraw; i++)
417 "Error reading exposure time");
419 exposure_time /= nraw;
422 "Error setting master bias exposure time");
424 check( badpixels_cleaned =
426 chip, binx, biny,
false,red_ccd_is_new),
427 "Error replacing bad pixels");
429 uves_msg(
"%d bad pixels replaced", badpixels_cleaned);
433 if (cpl_error_get_code() != CPL_ERROR_NONE)
435 uves_free_image(&master_bias);
450 UVES_CONCAT2X(UVES_MBIAS_ID,exe)(cpl_frameset *frames,
451 const cpl_parameterlist *parameters,
452 const char *starttime)
454 uves_mbias_exe_body(frames, parameters, starttime, make_str(UVES_MBIAS_ID));
471 uves_mbias_exe_body(cpl_frameset *frames,
472 const cpl_parameterlist *parameters,
473 const char *starttime,
474 const char *recipe_id)
477 cpl_imagelist *raw_images[2] = {NULL, NULL};
480 cpl_table* qclog[2] = {NULL, NULL};
484 cpl_image *master_bias = NULL;
485 cpl_stats *mbias_stats = NULL;
488 char *product_filename = NULL;
492 const char* PROCESS_CHIP=NULL;
497 const char* STACK_METHOD=NULL;
499 double STACK_KHIGH=0;
504 check( uves_load_raw_imagelist(frames,
506 UVES_BIAS(
true), UVES_BIAS(
false),
508 raw_images, raw_headers, product_header,
509 &blue),
"Error loading raw frames");
513 "Could not get raw frame x-binning");
515 "Could not get raw frame y-binning");
516 check( uves_get_parameter(parameters, NULL,
"uves",
"process_chip", CPL_TYPE_STRING, &PROCESS_CHIP),
517 "Could not read parameter");
520 check( uves_get_parameter(parameters, NULL, recipe_id,
"clean_traps", CPL_TYPE_BOOL, &CLEAN_TRAPS),
521 "Could not read parameter");
523 check( uves_get_parameter(parameters, NULL, recipe_id,
"stack_method", CPL_TYPE_STRING, &STACK_METHOD),
524 "Could not read parameter");
527 check( uves_get_parameter(parameters, NULL, recipe_id,
"klow", CPL_TYPE_DOUBLE, &STACK_KLOW),
528 "Could not read parameter");
529 check( uves_get_parameter(parameters, NULL, recipe_id,
"khigh", CPL_TYPE_DOUBLE, &STACK_KHIGH),
530 "Could not read parameter");
531 check( uves_get_parameter(parameters, NULL, recipe_id,
"niter", CPL_TYPE_INT, &STACK_NITER),
532 "Could not read parameter");
536 chip != UVES_CHIP_INVALID;
539 if(strcmp(PROCESS_CHIP,
"REDU") == 0) {
550 uves_free_image(&master_bias);
551 check( master_bias = uves_mbias_process_chip(raw_images[raw_index],
552 raw_headers[raw_index],
553 product_header[raw_index],
560 "Error processing chip");
563 cpl_free(product_filename);
564 check( product_filename = uves_masterbias_filename(chip),
565 "Error getting filename");
568 uves_msg(
"Calculating QC parameters");
571 check(uves_mbias_qclog(raw_images[raw_index],
572 raw_headers[raw_index],
576 qclog[0]),
"error computing qclog");
581 check( uves_frameset_insert(frames,
583 CPL_FRAME_GROUP_PRODUCT,
584 CPL_FRAME_TYPE_IMAGE,
585 CPL_FRAME_LEVEL_INTERMEDIATE,
587 UVES_MASTER_BIAS(chip),
588 raw_headers[raw_index][0],
589 product_header[raw_index],
593 PACKAGE
"/" PACKAGE_VERSION,qclog,
596 "Could not add master bias %s to frameset", product_filename);
598 uves_msg(
"Master bias '%s' added to frameset", product_filename);
600 if(strcmp(PROCESS_CHIP,
"REDL") == 0) {
609 if (raw_images[0] != NULL)
612 for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++)
614 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
615 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
617 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
618 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
620 uves_free_imagelist(&raw_images[0]);
621 uves_free_imagelist(&raw_images[1]);
625 uves_free_image(&master_bias);
626 uves_free_propertylist(&product_header[0]);
627 uves_free_propertylist(&product_header[1]);
628 cpl_free(product_filename);
629 uves_free_stats(&mbias_stats);
637 count_good(
const cpl_image *image)
640 cpl_image_get_size_x(image) * cpl_image_get_size_y(image) -
641 cpl_image_count_rejected(image);
652 reject_lo_hi(cpl_image *image,
double min,
double max)
654 cpl_mask *mask_lo = NULL;
655 cpl_mask *mask_hi = NULL;
657 mask_lo = cpl_mask_threshold_image_create(image, -DBL_MAX, min);
658 mask_hi = cpl_mask_threshold_image_create(image, max, DBL_MAX);
662 cpl_mask_or(mask_lo, mask_hi);
664 cpl_image_reject_from_mask(image, mask_lo);
667 uves_free_mask(&mask_lo);
668 uves_free_mask(&mask_hi);
682 static void uves_mbias_qclog(
const cpl_imagelist* raw_imgs,
685 const cpl_image* mbia,
705 double upp_threshold= 0.0;
706 double low_threshold= 0.0;
708 double qc_ron_master= 0.0;
710 double master_median=0.0;
716 double struct_col=0.0;
717 double struct_row=0.0;
719 double time_s=+9999999.0;
720 double time_e=-9999999.0;
722 double qc_duty_cycle=0.;
723 double exposure_time=0;
727 const cpl_image* rbia=NULL;
728 cpl_image* tima=NULL;
729 cpl_image* avg_col=NULL;
730 cpl_image* avg_row=NULL;
734 "Test-on-Master-Bias",
738 uves_msg(
"Computing duty cycle...");
742 nraw = cpl_imagelist_get_size(raw_imgs);
746 "Number of frames combined",
750 for (i = 0; i < nraw; i++)
753 "Error reading exposure time");
754 if(exposure_time >= time_e) time_e = exposure_time;
755 if(exposure_time <= time_s) time_s = exposure_time;
758 qc_duty_cycle = (time_e-time_s)/ (nraw-1);
768 "Time to store a frame",
781 nx_pix = cpl_image_get_size_x(mbia);
782 ny_pix = cpl_image_get_size_y(mbia);
785 x_cent_s = (nx_pix - sample_x)/2;
786 x_cent_e = (nx_pix + sample_x)/2;
787 y_cent_s = (ny_pix - sample_y)/2;
788 y_cent_e = (ny_pix + sample_y)/2;
792 cpl_image_get_median_window(mbia,
796 y_cent_e)*(1 + extra));
798 cpl_image_get_median_window(mbia,
802 y_cent_e)*(1 - extra));
810 rbia = cpl_imagelist_get_const(raw_imgs,0);
812 x_cent_s,x_cent_e,y_cent_s,y_cent_e,qclog));
820 check_nomsg( reject_lo_hi(tima, low_threshold, upp_threshold) );
821 if (count_good(tima) >= 2)
823 check_nomsg(master_median = cpl_image_get_median(tima));
824 check_nomsg(qc_ron_master = cpl_image_get_stdev(tima));
830 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameters to -1",
833 uves_free_image(&tima);
838 "Median of pixel values",
841 sprintf(key_name,
"QC OUT%d RON MASTER", pn);
845 "Read noise frame in ADU",
862 if (chip != UVES_CHIP_BLUE) {
872 check_nomsg(avg_col = cpl_image_collapse_create(tima,1));
873 check_nomsg(cpl_image_divide_scalar(avg_col,cpl_image_get_size_x(tima)));
876 min = cpl_image_get_mean(avg_col) - 2;
877 max = cpl_image_get_mean(avg_col) + 2;
883 if (count_good(avg_col) >= 2)
885 check_nomsg(struct_col = cpl_image_get_stdev(avg_col));
890 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameter to -1",
891 count_good(avg_col));
894 sprintf(key_name,
"%s%d%s",
"QC OUT",pn,
" STRUCTY");
898 "structure in Y (bias slope)",
903 check_nomsg(avg_row = cpl_image_collapse_create(tima,0));
904 check_nomsg(cpl_image_divide_scalar(avg_row,cpl_image_get_size_y(tima)));
907 min = cpl_image_get_mean(avg_row) - 2;
908 max = cpl_image_get_mean(avg_row) + 2;
914 if (count_good(avg_row) >= 2)
916 check_nomsg(struct_row = cpl_image_get_stdev(avg_row));
921 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameter to -1",
922 count_good(avg_row));
926 sprintf(key_name,
"%s%d%s",
"QC OUT",pn,
" STRUCTX");
930 "structure in X (bias slope)",
937 uves_free_image(&avg_col);
938 uves_free_image(&avg_row);
939 uves_free_image(&tima);
958 uves_mbias_qc_ron_raw(
const cpl_image* rbia,
967 double qc_ron_raw=0.0;
968 double upp_threshold=0.0;
969 double low_threshold=0.0;
973 cpl_image* tima=NULL;
980 cpl_image_get_median_window(rbia,
984 y_cent_e)*(1 + extra));
987 cpl_image_get_median_window(rbia,
991 y_cent_e)*(1 - extra));
1000 check_nomsg( reject_lo_hi(tima, low_threshold, upp_threshold) );
1001 if (count_good(tima) >= 2)
1003 check_nomsg(qc_ron_raw = cpl_image_get_stdev(tima));
1008 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameter to -1",
1019 sprintf(key_name,
"%s%d%s",
"QC OUT",pn,
" RON RAW");
1023 "Read noise frame in ADU",
1027 uves_free_image(&tima);