42 #include <uves_mbias_impl.h>
44 #include <uves_utils.h>
45 #include <uves_corrbadpix.h>
46 #include <uves_parameters.h>
49 #include <uves_pfits.h>
50 #include <uves_qclog.h>
51 #include <uves_recipe.h>
52 #include <uves_utils_wrappers.h>
53 #include <uves_error.h>
54 #include <irplib_mkmaster.h>
66 static void uves_mbias_qclog(
const cpl_imagelist* raw_imgs,
69 const cpl_image* mbia,
76 uves_mbias_qc_ron_raw(
const cpl_image* rbia,
85 uves_mbias_define_parameters(cpl_parameterlist *parameters);
90 #define cpl_plugin_get_info uves_mbias_get_info
92 UVES_MBIAS_ID, UVES_MBIAS_DOM,
95 uves_mbias_define_parameters,
96 "Jonas M. Larsen",
"cpl@eso.org",
97 "Creates the master bias frame",
98 "This recipe creates a master bias frame by computing the median of all input\n"
99 "bias frames. All input frames must have same tag and size and must be either\n"
100 "BIAS_BLUE or BIAS_RED.\n"
101 "On blue input the recipe computes one master bias frame; on red input a \n"
102 "master bias frame for each chip is produced. The average, standard deviation\n"
103 "and median of the master bias image(s) are written to the FITS header(s)");
119 int uves_mbias_define_parameters_body(cpl_parameterlist *parameters,
120 const char *recipe_id)
126 if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
135 if (uves_corr_traps_define_parameters(parameters,recipe_id)
145 if (uves_master_stack_define_parameters(parameters,recipe_id)
151 return (cpl_error_get_code() != CPL_ERROR_NONE);
164 uves_mbias_define_parameters(cpl_parameterlist *parameters)
166 return uves_mbias_define_parameters_body(parameters, make_str(UVES_MBIAS_ID));
192 uves_mbias_process_chip(
const cpl_imagelist *raw_images,
198 const char* STACK_METHOD,
199 const double STACK_KLOW,
200 const double STACK_KHIGH,
201 const int STACK_NITER)
203 cpl_image *master_bias = NULL;
204 double exposure_time = 0;
205 int badpixels_cleaned;
208 bool red_ccd_is_new=
false;
209 cpl_vector* bias_levels=NULL;
212 uves_msg(
"Calculating master bias...");
216 if(strcmp(STACK_METHOD,
"MEDIAN")==0) {
226 nraw=cpl_imagelist_get_size(raw_images);
227 for (i = 0; i < nraw; i++)
230 "Error reading exposure time");
232 exposure_time /= nraw;
237 "Error setting master bias exposure time");
239 check( badpixels_cleaned =
241 chip, binx, biny,
false,red_ccd_is_new),
242 "Error replacing bad pixels");
244 uves_msg(
"%d bad pixels replaced", badpixels_cleaned);
248 if (cpl_error_get_code() != CPL_ERROR_NONE)
250 uves_free_image(&master_bias);
265 UVES_CONCAT2X(UVES_MBIAS_ID,exe)(cpl_frameset *frames,
266 const cpl_parameterlist *parameters,
267 const char *starttime)
269 uves_mbias_exe_body(frames, parameters, starttime, make_str(UVES_MBIAS_ID));
286 uves_mbias_exe_body(cpl_frameset *frames,
287 const cpl_parameterlist *parameters,
288 const char *starttime,
289 const char *recipe_id)
292 cpl_imagelist *raw_images[2] = {NULL, NULL};
295 cpl_table* qclog[2] = {NULL, NULL};
299 cpl_image *master_bias = NULL;
300 cpl_stats *mbias_stats = NULL;
303 char *product_filename = NULL;
307 const char* PROCESS_CHIP=NULL;
312 const char* STACK_METHOD=NULL;
314 double STACK_KHIGH=0;
319 check( uves_load_raw_imagelist(frames,
321 UVES_BIAS(
true), UVES_BIAS(
false),
323 raw_images, raw_headers, product_header,
324 &blue),
"Error loading raw frames");
328 "Could not get raw frame x-binning");
330 "Could not get raw frame y-binning");
331 check( uves_get_parameter(parameters, NULL,
"uves",
"process_chip", CPL_TYPE_STRING, &PROCESS_CHIP),
332 "Could not read parameter");
335 check( uves_get_parameter(parameters, NULL, recipe_id,
"clean_traps", CPL_TYPE_BOOL, &CLEAN_TRAPS),
336 "Could not read parameter");
338 check( uves_get_parameter(parameters, NULL, recipe_id,
"stack_method", CPL_TYPE_STRING, &STACK_METHOD),
339 "Could not read parameter");
342 check( uves_get_parameter(parameters, NULL, recipe_id,
"klow", CPL_TYPE_DOUBLE, &STACK_KLOW),
343 "Could not read parameter");
344 check( uves_get_parameter(parameters, NULL, recipe_id,
"khigh", CPL_TYPE_DOUBLE, &STACK_KHIGH),
345 "Could not read parameter");
346 check( uves_get_parameter(parameters, NULL, recipe_id,
"niter", CPL_TYPE_INT, &STACK_NITER),
347 "Could not read parameter");
351 chip != UVES_CHIP_INVALID;
354 if(strcmp(PROCESS_CHIP,
"REDU") == 0) {
365 uves_free_image(&master_bias);
366 check( master_bias = uves_mbias_process_chip(raw_images[raw_index],
367 raw_headers[raw_index],
368 product_header[raw_index],
375 "Error processing chip");
378 cpl_free(product_filename);
379 check( product_filename = uves_masterbias_filename(chip),
380 "Error getting filename");
383 uves_msg(
"Calculating QC parameters");
386 check(uves_mbias_qclog(raw_images[raw_index],
387 raw_headers[raw_index],
391 qclog[0]),
"error computing qclog");
396 check( uves_frameset_insert(frames,
398 CPL_FRAME_GROUP_PRODUCT,
399 CPL_FRAME_TYPE_IMAGE,
400 CPL_FRAME_LEVEL_INTERMEDIATE,
402 UVES_MASTER_BIAS(chip),
403 raw_headers[raw_index][0],
404 product_header[raw_index],
408 PACKAGE
"/" PACKAGE_VERSION,qclog,
411 "Could not add master bias %s to frameset", product_filename);
413 uves_msg(
"Master bias '%s' added to frameset", product_filename);
415 if(strcmp(PROCESS_CHIP,
"REDL") == 0) {
424 if (raw_images[0] != NULL)
427 for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++)
429 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
430 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
432 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
433 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
435 uves_free_imagelist(&raw_images[0]);
436 uves_free_imagelist(&raw_images[1]);
440 uves_free_image(&master_bias);
441 uves_free_propertylist(&product_header[0]);
442 uves_free_propertylist(&product_header[1]);
443 cpl_free(product_filename);
444 uves_free_stats(&mbias_stats);
452 count_good(
const cpl_image *image)
455 cpl_image_get_size_x(image) * cpl_image_get_size_y(image) -
456 cpl_image_count_rejected(image);
467 reject_lo_hi(cpl_image *image,
double min,
double max)
469 cpl_mask *mask_lo = NULL;
470 cpl_mask *mask_hi = NULL;
472 mask_lo = cpl_mask_threshold_image_create(image, -DBL_MAX, min);
473 mask_hi = cpl_mask_threshold_image_create(image, max, DBL_MAX);
477 cpl_mask_or(mask_lo, mask_hi);
479 cpl_image_reject_from_mask(image, mask_lo);
482 uves_free_mask(&mask_lo);
483 uves_free_mask(&mask_hi);
497 static void uves_mbias_qclog(
const cpl_imagelist* raw_imgs,
500 const cpl_image* mbia,
520 double upp_threshold= 0.0;
521 double low_threshold= 0.0;
523 double qc_ron_master= 0.0;
525 double master_median=0.0;
531 double struct_col=0.0;
532 double struct_row=0.0;
534 double time_s=+9999999.0;
535 double time_e=-9999999.0;
537 double qc_duty_cycle=0.;
538 double exposure_time=0;
542 const cpl_image* rbia=NULL;
543 cpl_image* tima=NULL;
544 cpl_image* avg_col=NULL;
545 cpl_image* avg_row=NULL;
549 "Test-on-Master-Bias",
553 uves_msg(
"Computing duty cycle...");
557 nraw = cpl_imagelist_get_size(raw_imgs);
561 "Number of frames combined",
565 for (i = 0; i < nraw; i++)
568 "Error reading exposure time");
569 if(exposure_time >= time_e) time_e = exposure_time;
570 if(exposure_time <= time_s) time_s = exposure_time;
573 qc_duty_cycle = (time_e-time_s)/ (nraw-1);
583 "Time to store a frame",
596 nx_pix = cpl_image_get_size_x(mbia);
597 ny_pix = cpl_image_get_size_y(mbia);
600 x_cent_s = (nx_pix - sample_x)/2;
601 x_cent_e = (nx_pix + sample_x)/2;
602 y_cent_s = (ny_pix - sample_y)/2;
603 y_cent_e = (ny_pix + sample_y)/2;
607 cpl_image_get_median_window(mbia,
611 y_cent_e)*(1 + extra));
613 cpl_image_get_median_window(mbia,
617 y_cent_e)*(1 - extra));
625 rbia = cpl_imagelist_get_const(raw_imgs,0);
627 x_cent_s,x_cent_e,y_cent_s,y_cent_e,qclog));
635 check_nomsg( reject_lo_hi(tima, low_threshold, upp_threshold) );
636 if (count_good(tima) >= 2)
638 check_nomsg(master_median = cpl_image_get_median(tima));
639 check_nomsg(qc_ron_master = cpl_image_get_stdev(tima));
645 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameters to -1",
648 uves_free_image(&tima);
653 "Median of pixel values",
656 sprintf(key_name,
"QC OUT%d RON MASTER", pn);
660 "Read noise frame in ADU",
677 if (chip != UVES_CHIP_BLUE) {
687 check_nomsg(avg_col = cpl_image_collapse_create(tima,1));
688 check_nomsg(cpl_image_divide_scalar(avg_col,cpl_image_get_size_x(tima)));
691 min = cpl_image_get_mean(avg_col) - 2;
692 max = cpl_image_get_mean(avg_col) + 2;
698 if (count_good(avg_col) >= 2)
700 check_nomsg(struct_col = cpl_image_get_stdev(avg_col));
705 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameter to -1",
706 count_good(avg_col));
709 sprintf(key_name,
"%s%d%s",
"QC OUT",pn,
" STRUCTY");
713 "structure in Y (bias slope)",
718 check_nomsg(avg_row = cpl_image_collapse_create(tima,0));
719 check_nomsg(cpl_image_divide_scalar(avg_row,cpl_image_get_size_y(tima)));
722 min = cpl_image_get_mean(avg_row) - 2;
723 max = cpl_image_get_mean(avg_row) + 2;
729 if (count_good(avg_row) >= 2)
731 check_nomsg(struct_row = cpl_image_get_stdev(avg_row));
736 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameter to -1",
737 count_good(avg_row));
741 sprintf(key_name,
"%s%d%s",
"QC OUT",pn,
" STRUCTX");
745 "structure in X (bias slope)",
752 uves_free_image(&avg_col);
753 uves_free_image(&avg_row);
754 uves_free_image(&tima);
773 uves_mbias_qc_ron_raw(
const cpl_image* rbia,
782 double qc_ron_raw=0.0;
783 double upp_threshold=0.0;
784 double low_threshold=0.0;
788 cpl_image* tima=NULL;
795 cpl_image_get_median_window(rbia,
799 y_cent_e)*(1 + extra));
802 cpl_image_get_median_window(rbia,
806 y_cent_e)*(1 - extra));
815 check_nomsg( reject_lo_hi(tima, low_threshold, upp_threshold) );
816 if (count_good(tima) >= 2)
818 check_nomsg(qc_ron_raw = cpl_image_get_stdev(tima));
823 uves_msg_warning(
"Only %d good pixels in image. Setting QC parameter to -1",
834 sprintf(key_name,
"%s%d%s",
"QC OUT",pn,
" RON RAW");
838 "Read noise frame in ADU",
842 uves_free_image(&tima);
#define uves_msg_warning(...)
Print an warning message.
int uves_qclog_add_string(cpl_table *table, const char *key_name, const char *value, const char *key_help, const char *format)
Add string key to QC-LOG table.
int uves_qclog_delete(cpl_table **table)
delete QC-LOG table
int uves_qclog_add_double(cpl_table *table, const char *key_name, const double value, const char *key_help, const char *format)
Add double key to QC-LOG table.
int uves_qclog_add_int(cpl_table *table, const char *key_name, const int value, const char *key_help, const char *format)
Add integer key to QC-LOG table.
int uves_pfits_get_binx(const uves_propertylist *plist)
Find out the x binning factor.
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
double uves_pfits_get_mjdobs(const uves_propertylist *plist)
Find out the modified julian observation date.
int uves_pfits_get_biny(const uves_propertylist *plist)
Find out the y binning factor.
bool uves_ccd_is_new(const uves_propertylist *plist)
Find out if CCD header is new.
double uves_pfits_get_exptime(const uves_propertylist *plist)
Find out the exposure time in seconds.
int uves_chip_get_index(enum uves_chip chip)
Convert to integer.
enum uves_chip uves_chip_get_first(bool blue)
Get first chip for blue or red arm.
cpl_error_code uves_pfits_set_extname(uves_propertylist *plist, const char *extname)
Write the EXTNAME keyword.
cpl_error_code uves_pfits_set_exptime(uves_propertylist *plist, double exptime)
Write the exposure time.
enum uves_chip uves_chip_get_next(enum uves_chip chip)
Get next chip.
#define uves_msg_debug(...)
Print a debug message.
const char * uves_chip_tostring_upper(enum uves_chip chip)
Convert to string.
int uves_correct_badpix_all(cpl_image *master_bias, uves_propertylist *mbias_header, enum uves_chip chip, int binx, int biny, int mark_bad, bool red_ccd_new)
Correct all bad pixels on a chip.
const char * uves_string_toupper(char *s)
Convert all lowercase characters in a string into uppercase characters.
cpl_table * uves_qclog_init(const uves_propertylist *raw_header, enum uves_chip chip)
Init QC-LOG table.
cpl_image * irplib_mkmaster_median(cpl_imagelist *images, const double kappa, const int nclip, const double tolerance)
Computes master frame by clean stack median of the input imagelist.
cpl_image * irplib_mkmaster_mean(cpl_imagelist *images, const double kappa, const int nclip, const double tolerance, const double klow, const double khigh, const int niter)
Computes master frame by clean stack mean of the input imagelist.