44 #include <uves_reduce_scired.h>
46 #include <uves_reduce.h>
47 #include <uves_reduce_utils.h>
48 #include <uves_corrbadpix.h>
50 #include <uves_chip.h>
51 #include <uves_plot.h>
53 #include <uves_pfits.h>
54 #include <uves_parameters.h>
56 #include <uves_utils.h>
57 #include <uves_utils_wrappers.h>
58 #include <uves_qclog.h>
59 #include <uves_error.h>
60 #include <uves_merge.h>
62 #include <uves_dump.h>
73 const cpl_image *raw_image,
86 const char *
const uves_scired_desc_short =
"Reduces a science frame";
87 const char *
const uves_scired_desc =
88 "This recipe reduces a science frame (SCIENCE_BLUE or SCIENCE_RED, or\n"
89 "UVES_SCI_POINT_BLUE or UVES_SCI_POINT_RED, or \n"
90 "UVES_SCI_EXTND_BLUE or UVES_SCI_EXTND_RED or \n"
91 "UVES_SCI_SLICER_BLUE or UVES_SCI_SLICER_RED) using "
92 "a combination (depending on recipe parameters and provided input frames) of "
94 " - bias subtraction,\n"
95 " - dark subtraction,\n"
96 " - background subtraction,\n"
97 " - extraction/cosmic ray removal,\n"
98 " - flat field correction,\n"
99 " - wavelength rebinning,\n"
100 " - sky subtraction,\n"
101 " - order merging,\n"
102 " - response correction (if response curve is provided).\n"
104 "Additional input for this recipe are: \n"
105 "order table(s) for each chip, ORDER_TABLE_xxxx (where xxxx=BLUE, REDL, REDU),\n"
106 "line table(s) for each chip, LINE_TABLE_xxxx, a master bias frame,\n"
107 "MASTER_BIAS_xxxx, a master flat, MASTER_FLAT_xxxx, \n"
108 "optionally an instrument response table, INSTR_RESPONSE_xxx\n"
109 "optionally a table describing the atmospheric extintion,\n"
111 "For each chip (xxxx = BLUE, REDL, REDU) the recipe produces a combination of "
113 " 'RED_SCIENCE_xxxx' Reduced science spectrum\n"
114 " 'MERGED_SCIENCE_xxxx' Merged spectrum, no sky subtraction\n"
115 " 'WCALIB_SCIENCE_xxxx' Extracted, wavelength calibrated frame in\n"
116 " (wavelength, order) space\n"
117 " 'WCALIB_FF_SCIENCE_xxxx' Extracted, flat-fielded, wave.cal. frame in\n"
118 " (wavelength, order) space\n"
119 " (Only if flatfielding done)\n"
120 " 'WCALIB_FLAT_OBJ_xxxx' Extracted, wavelength calibrated flat field\n"
121 " in (wavelength, order) space\n"
122 " (Only if flatfielding done)\n"
123 " 'ERRORBAR_SCIENCE_xxxx' Error bars of 'RED_SCIENCE_xxxx'\n"
124 " 'VARIANCE_SCIENCE_xxxx' Variance of extracted, flatfielded object in\n"
125 " (pixel, order) space\n"
126 " 'ORDER_TRACE_xxxx' Table describing the spatial profile\n"
127 " 'FLUXCAL_SCIENCE_xxxx' Flux-calibrated science spectrum\n"
128 " 'FLUXCAL_ERROR_xxxx' Error bars of 'FLUXCAL_SCIENCE_xxxx'\n"
129 " 'BKG_SCI_xxxx' The subtracted background image\n"
130 " 'CRMASK_xxxx' List of cosmic ray hits\n"
131 " 'MERGED_SKY_xxxx' The merged sky spectrum\n"
132 " 'EXT_2D_SCIENCE_xxxx' The 2d extracted spectrum\n"
133 " 'FF2D_SCIENCE_xxxx' The 2d extracted, flat-fielded spectrum\n"
134 " 'WCAL2D_SCIENCE_xxxx' The 2d extracted, flat-fielded, wave.cal. spectrum\n"
135 " 'MER2D_SCIENCE_xxxx' The 2d reduced, flux-calibrated (if possible) \n"
136 " science spectrum\n";
150 const char* key_comm=NULL;
156 sprintf(key_name_o,
"CUNIT1");
157 key_comm=
"Wavelength units";
160 sprintf(key_name_i,
"ESO QC LINE RESIDRMS WLU");
161 sprintf(key_name_o,
"LAMRMS");
162 key_comm=
"RMS of wavelength solution [CUNIT1]";
171 sprintf(key_name_i,
"ESO QC NLINSOL");
172 sprintf(key_name_o,
"LAMNLIN");
173 key_comm=
"No. of lines used in wavelength solution";
182 sprintf(key_name_i,
"ESO QC LINE WAVEERR");
183 sprintf(key_name_o,
"CRDER1");
184 key_comm=
"Wavelength uncertainty [CUNIT1]";
192 sprintf(key_name_i,
"ESO QC LINE SYSERR");
193 sprintf(key_name_o,
"CSYER1");
194 key_comm=
"Typical systematic wavelength error [CUNIT1]";
217 const char *recipe_id)
223 if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
232 if (uves_corr_traps_define_parameters(parameters,recipe_id)
241 if (uves_propagate_parameters_step(UVES_REDUCE_ID, parameters,
242 recipe_id, NULL) != 0)
247 return (cpl_error_get_code() != CPL_ERROR_NONE);
252 uves_get_pro_catg_special(
bool extract_is_2d, merge_method m_method) {
253 const char* result=NULL;
254 if(extract_is_2d && m_method == MERGE_NOAPPEND) {
256 }
else if (!extract_is_2d &&
257 m_method == MERGE_NOAPPEND) {
276 cpl_frame **result = NULL;
280 result = cpl_calloc( cpl_frameset_get_size(frames) + 1,
283 for (f = cpl_frameset_get_first(frames), i = 0;
285 f = cpl_frameset_get_next(frames))
287 if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_RAW)
290 cpl_frame_set_group(f, CPL_FRAME_GROUP_NONE);
373 static cpl_error_code
377 const cpl_image *master_bias,
379 const cpl_image *master_dark,
381 const cpl_image *master_flat,
383 const cpl_table *ordertable,
385 const cpl_table *linetable[3],
388 const cpl_image *response_curve,
389 const cpl_table *master_response,
391 const cpl_table *atm_extinction,
399 const cpl_parameterlist *parameters,
400 const char *recipe_id,
404 cpl_image **background,
405 cpl_image **flatfielded_variance,
407 cpl_image **resampled_science,
408 cpl_image **resampled_mf,
409 cpl_image **rebinned_science,
411 cpl_image **merged_sky,
412 cpl_image **merged_science,
414 cpl_image **reduced_science,
415 cpl_image **reduced_science_error,
416 cpl_table **cosmic_mask,
417 cpl_image **wave_map,
418 cpl_image **fluxcal_science,
419 cpl_image **fluxcal_error,
421 cpl_table **info_tbl,
422 double *extraction_slit,
423 cpl_table **order_trace)
425 cpl_image *rebinned_noise = NULL;
426 cpl_image *merged_noise = NULL;
428 cpl_image *reduced_rebinned = NULL;
429 cpl_image *reduced_rebinned_noise = NULL;
431 cpl_table *response_table = NULL;
434 uves_msg(
"Reducing science object");
462 flatfielded_variance,
463 flatfielded_variance_header,
474 &reduced_rebinned_noise,
476 reduced_science_error,
480 "Could not reduce frame");
483 check( uves_plot_image_rows(*reduced_science,
484 1 + cpl_image_get_size_y(*reduced_science)/2,
485 1 + cpl_image_get_size_y(*reduced_science)/2, 1,
486 "Wavelength (arbitrary units)",
"Relative flux",
487 "Reduced science spectrum"),
496 if (response_curve != NULL || master_response != NULL)
503 int n_traces = cpl_image_get_size_y(*reduced_science);
505 uves_msg(
"Flux calibrating spectrum");
516 if (response_curve != NULL) {
517 response_table = cpl_table_new(cpl_image_get_size_x(response_curve));
518 cpl_table_new_column(response_table,
"LAMBDA", CPL_TYPE_DOUBLE);
519 cpl_table_new_column(response_table,
"FLUX_CONV", CPL_TYPE_DOUBLE);
522 "Error reading response curve start wavelength from header");
525 "Error reading bin width from header");
527 for (bin = 1; bin <= cpl_image_get_size_x(response_curve); bin++) {
532 lambda = lambda_start + (bin-1) * dlambda;
534 check( response = cpl_image_get(response_curve, bin, 1, &pis_rejected),
535 "Error reading response curve bin = %d", bin);
537 check((cpl_table_set_double(response_table,
"LAMBDA", bin - 1, lambda),
538 cpl_table_set_double(response_table,
"FLUX_CONV", bin - 1, response)),
539 "Error updating response table at row %d", bin - 1);
543 response_table = cpl_table_duplicate( master_response );
551 *reduced_science_error,
559 "Error normalizing reduced spectrum");
565 uves_msg(
"Multiplying by response function");
567 int nbins = cpl_image_get_size_x(*fluxcal_science);
568 int ntraces = cpl_image_get_size_y(*fluxcal_science);
569 double *fluxcal_science_data = cpl_image_get_data_double(*fluxcal_science);
570 double *fluxcal_science_noise = cpl_image_get_data_double(*fluxcal_error);
573 "Error reading start wavelength from reduced science header");
576 "Error reading bin width from header");
578 for (bin = 1; bin <= nbins; bin++)
585 lambda = lambda_start + (bin-1) * dlambda;
589 "LAMBDA",
"FLUX_CONV", &istart),
590 "Error interpolating response curve at lambda = %f wlu", lambda);
592 for (trace = 1; trace <= ntraces; trace++)
598 fluxcal_science_data [(bin-1) + (trace-1)*nbins] *= response;
599 fluxcal_science_noise[(bin-1) + (trace-1)*nbins] *= response;
608 check( uves_plot_image_rows(*fluxcal_science,
609 1 + cpl_image_get_size_y(*fluxcal_science)/2,
610 1 + cpl_image_get_size_y(*fluxcal_science)/2, 1,
611 "Wavelength (arbitrary units)",
612 "Flux (10^-16 erg/cm^2/Angstrom/s)",
613 "Flux calibrated science spectrum"),
618 "10^-16 erg/cm^2/Angstrom/s",
623 "Error initializing flux calibrated spectrum header");
628 uves_msg(
"Skipping absolute flux calibration");
632 uves_free_image(&rebinned_noise);
633 uves_free_image(&merged_noise);
634 uves_free_image(&reduced_rebinned_noise);
635 uves_free_image(&reduced_rebinned);
636 uves_free_table(&response_table);
638 if (cpl_error_get_code() != CPL_ERROR_NONE)
642 return cpl_error_get_code();
657 const char *recipe_id,
const char *starttime)
662 bool extract_is_2d =
false;
665 cpl_image *raw_image[2] = {NULL, NULL};
670 cpl_image *master_bias = NULL;
673 cpl_image *master_flat = NULL;
676 cpl_image *master_dark = NULL;
679 cpl_table *ordertable = NULL;
682 cpl_table *traces = NULL;
685 const cpl_table *linetable[3] = {NULL, NULL, NULL};
687 const polynomial *dispersion_relation[3] = {NULL, NULL, NULL};
689 cpl_image *response_curve = NULL;
691 cpl_table *master_response = NULL;
693 cpl_table *atm_extinction = NULL;
697 cpl_image *background = NULL;
700 cpl_image *flatfielded_variance = NULL;
704 cpl_image *resampled_science = NULL;
706 cpl_image *resampled_mf = NULL;
708 cpl_image *rebinned_science = NULL;
713 cpl_image *merged_sky = NULL;
718 cpl_image *merged_science = NULL;
720 cpl_image *reduced_science = NULL;
721 cpl_image *reduced_science_error = NULL;
725 cpl_image *fluxcal_science = NULL;
726 cpl_image *fluxcal_error = NULL;
730 cpl_table *order_trace = NULL;
735 cpl_image *x2d = NULL;
738 cpl_image *fx2d = NULL;
741 cpl_table *cosmic_mask = NULL;
745 cpl_table* qclog[2] = {NULL, NULL};
746 cpl_table *qclog_tflat = NULL;
749 const char *raw_filename =
"";
750 const char *atm_ext_filename =
"";
751 const char *sci_type =
"";
752 cpl_frame **raw_frames = NULL;
753 char *product_tag = NULL;
754 char *product_filename = NULL;
755 char *context = NULL;
756 double extraction_slit;
763 cpl_table* info_tbl = NULL;
764 const char* PROCESS_CHIP=NULL;
765 bool red_ccd_is_new=0;
766 merge_method m_method;
767 const char* catg_is_noappend=NULL;
768 cpl_image* wave_map=NULL;
774 const char *ex_method =
"";
777 check( uves_get_parameter(parameters, NULL,
"uves",
"debug", CPL_TYPE_BOOL, &debug_mode),
778 "Could not read parameter");
782 check( uves_get_parameter(parameters, NULL,
"uves",
"debug", CPL_TYPE_BOOL, &debug_mode),
783 "Could not read parameter");
787 check( uves_get_parameter(parameters, NULL,
"uves",
"process_chip", CPL_TYPE_STRING, &PROCESS_CHIP),
788 "Could not read parameter");
792 check( uves_get_parameter(parameters,NULL,recipe_id,
"clean_traps", CPL_TYPE_BOOL, &CLEAN_TRAPS),
793 "Could not read parameter");
798 context = uves_sprintf(
"%s.%s.%s", recipe_id, UVES_REDUCE_ID, UVES_EXTRACT_ID);
800 check( uves_get_parameter(parameters, NULL,
802 CPL_TYPE_STRING, &ex_method),
803 "Could not read parameter");
805 extract_is_2d = (strcmp(ex_method,
"2d") == 0);
808 check( uves_load_science(frames, &raw_filename, raw_image, raw_header, rotated_header,
810 "Error loading raw frame");
812 if ((strcmp(sci_type,
"SCI_SLICER") == 0 ||
813 strcmp(sci_type,
"SCI_EXTND" ) == 0) &&
814 strcmp(ex_method,
"optimal") == 0)
817 "This may not work because the sky cannot be "
818 "reliably determined",
824 if (cpl_frameset_find(frames, UVES_EXTCOEFF_TABLE) != NULL)
826 check( uves_load_atmo_ext(frames, &atm_ext_filename, &atm_extinction),
827 "Error loading atm. extinction coefficients");
829 uves_msg_low(
"Using atmospheric extinction table in '%s'", atm_ext_filename);
833 uves_msg_low(
"No atmospheric extinction table. Flux calibration not done");
837 "Could not get merging method");
843 "Could not read x binning factor from input header");
845 "Could not read y binning factor from input header");
851 chip != UVES_CHIP_INVALID;
857 if(strcmp(PROCESS_CHIP,
"REDU") == 0) {
861 cpl_frame *mflat_frame = NULL;
862 const char *ordertable_filename =
"";
863 const char *linetable_filename =
"";
864 const char *master_bias_filename =
"";
865 const char *master_dark_filename =
"";
866 const char *master_flat_filename =
"";
867 const char *response_curve_filename =
"";
868 const char *chip_name =
"";
874 uves_msg(
"Processing %s chip in '%s'",
883 uves_free_image(&master_bias);
884 uves_free_propertylist(&master_bias_header);
885 if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
887 uves_free_image(&master_bias);
888 uves_free_propertylist(&master_bias_header);
890 check( uves_load_mbias(frames, chip_name, &master_bias_filename,
891 &master_bias, &master_bias_header,
893 "Error loading master bias");
895 uves_msg_low(
"Using master bias in '%s'", master_bias_filename);
899 uves_msg_low(
"No master bias in SOF. Bias subtraction not done");
903 uves_free_image(&master_dark);
904 uves_free_propertylist(&master_dark_header);
905 if (cpl_frameset_find(frames, UVES_MASTER_DARK(chip)) != NULL ||
906 cpl_frameset_find(frames, UVES_MASTER_PDARK(chip)) != NULL)
909 check( uves_load_mdark(frames, chip_name, &master_dark_filename,
910 &master_dark, &master_dark_header, chip),
911 "Error loading master dark");
913 uves_msg_low(
"Using master dark in '%s'", master_dark_filename);
917 uves_msg_low(
"No master dark in SOF. Dark subtraction not done");
921 uves_free_image(&master_flat);
922 uves_free_propertylist(&master_flat_header);
923 check( uves_load_mflat(frames, chip_name, &master_flat_filename, &master_flat,
924 &master_flat_header, chip, &mflat_frame),
925 "Error loading master flat");
927 uves_msg_low(
"Using master flat in '%s'", master_flat_filename);
931 uves_free_table (&ordertable);
932 uves_free_propertylist(&ordertable_header);
934 uves_free_table (&traces);
936 check( uves_load_ordertable(frames,
939 &ordertable_filename,
948 "Could not load order table");
949 uves_msg_low(
"Using order table in '%s'", ordertable_filename);
953 if (atm_extinction != NULL)
955 if (cpl_frameset_find(frames, UVES_INSTR_RESPONSE(chip)) != NULL ||
956 cpl_frameset_find(frames, UVES_MASTER_RESPONSE(chip)) != NULL)
958 uves_free_image(&response_curve);
959 uves_free_table(&master_response);
960 uves_free_propertylist(&response_curve_header);
961 check( uves_load_response_curve(frames, chip_name,
962 &response_curve_filename,
965 &response_curve_header,
967 "Error loading response curve");
970 master_response != NULL ?
"master " :
"",
971 response_curve_filename);
976 "Flux calibration not done");
982 "Do not look for response curve");
986 for(tracerow = 0; tracerow < cpl_table_get_nrow(traces); tracerow++)
991 int badpixels_cleaned;
992 trace_offset = cpl_table_get_double(traces,
"Offset" , tracerow, NULL);
993 trace_number = cpl_table_get_int (traces,
"TraceID" , tracerow, NULL);
994 trace_enabled = cpl_table_get_int (traces,
"Tracemask" , tracerow, NULL);
996 if (trace_enabled != 0)
1000 if (cpl_table_get_nrow(traces) > 1) {
1001 uves_msg(
"Processing trace %d", trace_number);
1008 for (window = 1; window <= 3; window ++) {
1009 uves_free_table_const ( &(linetable[window-1]) );
1010 uves_free_propertylist_const( &(linetable_header[window-1]) );
1012 check( uves_load_linetable_const(frames,
1016 cpl_table_get_column_min(
1017 ordertable,
"Order"),
1018 cpl_table_get_column_max(
1019 ordertable,
"Order"),
1020 &linetable_filename,
1021 &(linetable [window-1]),
1022 &(linetable_header [window-1]),
1023 &(dispersion_relation[window-1]),
1028 "Could not load line table, window #%d", window);
1030 uves_msg_low(
"Using line table(s) in '%s'", linetable_filename);
1034 uves_free_propertylist(&wave_acc_header);
1035 wave_acc_header=uves_paste_wave_accuracy(plist);
1036 uves_free_propertylist(&plist);
1040 uves_free_image(&x2d);
1041 uves_free_image(&fx2d);
1042 uves_free_propertylist(&x2d_header);
1043 uves_free_image(&background);
1044 uves_free_image(&flatfielded_variance);
1045 uves_free_propertylist(&flatfielded_variance_header);
1046 uves_free_image(&resampled_science);
1047 uves_free_image(&resampled_mf);
1048 uves_free_image(&rebinned_science);
1049 uves_free_propertylist(&rebinned_header);
1050 uves_free_image(&merged_sky);
1051 uves_free_image(&merged_science);
1052 uves_free_propertylist(&merged_header);
1053 uves_free_image(&reduced_science);
1054 uves_free_image(&reduced_science_error);
1055 uves_free_table(&cosmic_mask);
1056 uves_free_image(&fluxcal_science);
1057 uves_free_image(&fluxcal_error);
1058 uves_free_propertylist(&fluxcal_header);
1059 uves_free_table(&info_tbl);
1060 uves_free_table(&order_trace);
1064 check( badpixels_cleaned =
1066 raw_header[raw_index],
1068 false,red_ccd_is_new),
1069 "Error replacing bad pixels");
1076 raw_image[raw_index],
1077 raw_header[raw_index],
1078 rotated_header[raw_index],
1089 dispersion_relation,
1092 response_curve_header,
1102 &flatfielded_variance,
1103 &flatfielded_variance_header,
1112 &reduced_science_error,
1121 "Science reduction failed");
1129 raw_header[raw_index],
1130 raw_image[raw_index],
1133 "Could not compute QC parameters");
1145 cpl_free(product_filename);
1146 check( product_filename =
1148 uves_scired_red_2d_science_filename(chip) :
1149 ((m_method == MERGE_NOAPPEND) ?
1150 uves_scired_red_noappend_science_filename(chip): uves_scired_red_science_filename(chip)),
1151 "Error getting filename");
1153 cpl_free(product_tag);
1154 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1155 product_tag = uves_sprintf(
1158 (extract_is_2d) ?
"_2D" :
"",
1163 check( uves_frameset_insert(frames,
1165 CPL_FRAME_GROUP_PRODUCT,
1166 CPL_FRAME_TYPE_IMAGE,
1167 CPL_FRAME_LEVEL_FINAL,
1170 raw_header[raw_index],
1175 PACKAGE
"/" PACKAGE_VERSION,
1180 "Could not add reduced science spectrum '%s' (%s) to frameset",
1181 product_filename, product_tag);
1183 uves_msg(
"Reduced science spectrum '%s' (%s) added to frameset",
1184 product_filename, product_tag);
1189 cpl_free(product_filename);
1190 check( product_filename = uves_scired_ext2d_filename(chip),
1191 "Error getting filename");
1193 cpl_free(product_tag);
1195 uves_sprintf(
"EXT_2D_%s_%s", sci_type,
1200 check( uves_frameset_insert(frames,
1202 CPL_FRAME_GROUP_PRODUCT,
1203 CPL_FRAME_TYPE_IMAGE,
1204 CPL_FRAME_LEVEL_FINAL,
1207 raw_header[raw_index],
1212 PACKAGE
"/" PACKAGE_VERSION,
1216 "Could not add 2d extracted "
1217 "spectrum '%s' (%s) to frameset",
1218 product_filename, product_tag);
1220 uves_msg(
"2d extracted spectrum '%s' (%s) added to frameset",
1221 product_filename, product_tag);
1227 cpl_free(product_filename);
1228 check( product_filename = (extract_is_2d) ?
1229 uves_scired_merged_2d_science_filename(chip) :
1230 uves_scired_merged_science_filename(chip),
1231 "Error getting filename");
1232 cpl_free(product_tag);
1233 product_tag = uves_sprintf(
1235 (extract_is_2d) ?
"MER_2D" :
"MERGED",
1240 check( uves_frameset_insert(frames,
1242 CPL_FRAME_GROUP_PRODUCT,
1243 CPL_FRAME_TYPE_IMAGE,
1244 CPL_FRAME_LEVEL_FINAL,
1247 raw_header[raw_index],
1252 PACKAGE
"/" PACKAGE_VERSION,
1256 "Could not add merged science spectrum '%s' (%s) to frameset",
1257 product_filename, product_tag);
1259 uves_msg(
"Merged science spectrum '%s' (%s) added to frameset",
1260 product_filename, product_tag);
1267 cpl_free(product_filename);
1268 check( product_filename = (extract_is_2d) ?
1269 uves_scired_resampled_2d_filename(chip) :
1270 uves_scired_resampled_filename(chip),
1271 "Error getting filename");
1273 cpl_free(product_tag);
1275 uves_sprintf(
"WCALIB_%s_%s", sci_type,
1280 check( uves_frameset_insert(frames,
1282 CPL_FRAME_GROUP_PRODUCT,
1283 CPL_FRAME_TYPE_IMAGE,
1284 CPL_FRAME_LEVEL_FINAL,
1287 raw_header[raw_index],
1292 PACKAGE
"/" PACKAGE_VERSION,
1296 "Could not add wavelength calibrated science "
1297 "spectrum '%s' (%s) to frameset", product_filename,
1300 uves_msg(
"Wavelength calibrated science spectrum '%s' "
1301 "(%s) added to frameset", product_filename,
1305 cpl_free(product_filename);
1307 check( product_filename =
1308 uves_order_extract_qc_standard_filename(chip),
1309 "Error getting filename");
1312 check( uves_frameset_insert(frames,
1314 CPL_FRAME_GROUP_PRODUCT,
1315 CPL_FRAME_TYPE_TABLE,
1316 CPL_FRAME_LEVEL_INTERMEDIATE,
1318 UVES_ORDER_EXTRACT_QC(chip),
1319 raw_header[raw_index],
1320 rotated_header[raw_index],
1324 PACKAGE
"/" PACKAGE_VERSION,
1328 "Could not add extraction quality table %s (%s)"
1330 UVES_ORDER_EXTRACT_QC(chip));
1332 uves_msg(
"Extraction quality table '%s' "
1333 "(%s) added to frameset", product_filename,
1334 UVES_ORDER_EXTRACT_QC(chip));
1341 const char *ff =
"";
1345 context = uves_sprintf(
"%s.%s", recipe_id, UVES_REDUCE_ID);
1346 check( uves_get_parameter(parameters, NULL,
1349 CPL_TYPE_STRING, &ff),
1350 "Could not read parameter");
1353 if (strcmp(ff,
"no") != 0)
1357 cpl_table *qc_tabs[] = {NULL, NULL, NULL};
1363 if ( strcmp(recipe_id, make_str(UVES_TFLAT_ID)) == 0 )
1370 raw_header[raw_index],
1372 "Could not compute QC parameters");
1374 qc_tabs[0] = qclog_tflat;
1375 qc_tabs[1] = qclog[0];
1379 qc_tabs[0] = qclog[0];
1383 cpl_free(product_filename);
1384 check( product_filename =
1386 uves_scired_rebinned_2d_filename(chip) :
1387 uves_scired_rebinned_filename(chip),
1388 "Error getting filename");
1390 cpl_free(product_tag);
1391 product_tag = uves_sprintf(
1393 (extract_is_2d) ?
"WCAL_2D" :
"WCALIB_FF",
1398 check( uves_frameset_insert(frames,
1400 CPL_FRAME_GROUP_PRODUCT,
1401 CPL_FRAME_TYPE_IMAGE,
1402 CPL_FRAME_LEVEL_FINAL,
1405 raw_header[raw_index],
1415 "Could not add wavelength calibrated flat-fielded "
1416 "science spectrum '%s' (%s) to frameset",
1417 product_filename, product_tag);
1419 uves_msg(
"Wavelength calibrated flat-fielded science "
1420 "spectrum '%s' (%s) added to frameset",
1421 product_filename, product_tag);
1427 cpl_free(product_filename);
1428 check( product_filename =
1429 uves_scired_resampledmf_filename(chip),
1430 "Error getting filename");
1432 cpl_free(product_tag);
1435 "WCALIB_FLAT_OBJ_%s",
1467 cpl_free(raw_frames);
1471 cpl_frame_set_group(mflat_frame,
1472 CPL_FRAME_GROUP_RAW);
1476 check( uves_frameset_insert(
1479 CPL_FRAME_GROUP_PRODUCT,
1480 CPL_FRAME_TYPE_IMAGE,
1481 CPL_FRAME_LEVEL_FINAL,
1493 CPL_STATS_MIN | CPL_STATS_MAX),
1494 "Could not add wavelength calibrated "
1495 "flat-field '%s' (%s) to frameset",
1496 product_filename, product_tag);
1498 uves_msg(
"Wavelength calibrated flat-field "
1499 "spectrum '%s' (%s) added to frameset",
1500 product_filename, product_tag);
1506 raw_frames[i] != NULL;
1509 cpl_frame_set_group(
1511 CPL_FRAME_GROUP_RAW);
1514 cpl_frame_set_group(mflat_frame,
1515 CPL_FRAME_GROUP_CALIB);
1521 cpl_free(product_filename);
1522 check( product_filename =
1523 uves_scired_ff2d_filename(chip),
1524 "Error getting filename");
1526 cpl_free(product_tag);
1529 "FF_2D_%s_%s", sci_type,
1533 check( uves_frameset_insert(
1536 CPL_FRAME_GROUP_PRODUCT,
1537 CPL_FRAME_TYPE_IMAGE,
1538 CPL_FRAME_LEVEL_FINAL,
1541 raw_header[raw_index],
1551 "Could not add 2d extracted, flat-fielded "
1552 "spectrum '%s' (%s) to frameset",
1553 product_filename, product_tag);
1555 uves_msg(
"2d extracted, flat-fielded spectrum "
1556 "'%s' (%s) added to frameset",
1557 product_filename, product_tag);
1564 "Error writing error spectrum header");
1568 cpl_free(product_filename);
1570 check( product_filename =
1572 uves_scired_red_2d_error_filename(chip) :
1573 ((m_method == MERGE_NOAPPEND) ?
1574 uves_scired_red_noappend_error_filename(chip): uves_scired_red_error_filename(chip)),
1575 "Error getting filename");
1578 cpl_free(product_tag);
1579 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1580 product_tag = uves_sprintf(
"%s%s_%s_%s",
1581 (extract_is_2d) ?
"ERR_2D" :
"ERRORBAR",catg_is_noappend,
1594 check( uves_frameset_insert(
1596 reduced_science_error,
1597 CPL_FRAME_GROUP_PRODUCT,
1598 CPL_FRAME_TYPE_IMAGE,
1599 CPL_FRAME_LEVEL_FINAL,
1602 raw_header[raw_index],
1607 PACKAGE
"/" PACKAGE_VERSION,
1610 CPL_STATS_MIN | CPL_STATS_MAX),
1611 "Could not add error bars '%s' (%s) to frameset",
1612 product_filename, product_tag);
1614 uves_msg(
"Science spectrum error '%s' (%s) "
1615 "added to frameset",
1616 product_filename, product_tag);
1625 cpl_free(product_filename);
1626 check( product_filename =
1627 uves_scired_ff_variance_filename(chip),
1628 "Error getting filename");
1630 cpl_free(product_tag);
1632 uves_sprintf(
"VARIANCE_%s_%s", sci_type,
1638 check( uves_frameset_insert(frames,
1639 flatfielded_variance,
1640 CPL_FRAME_GROUP_PRODUCT,
1641 CPL_FRAME_TYPE_IMAGE,
1642 CPL_FRAME_LEVEL_FINAL,
1645 raw_header[raw_index],
1646 flatfielded_variance_header,
1650 PACKAGE
"/" PACKAGE_VERSION,
1653 CPL_STATS_MIN | CPL_STATS_MAX),
1654 "Could not add flat-fielded spectrum variance "
1655 "'%s' (%s) to frameset",
1656 product_filename, product_tag);
1658 uves_msg(
"Flat-fielded spectrum variance '%s' (%s) "
1659 "added to frameset",
1660 product_filename, product_tag);
1668 cpl_free(product_filename);
1669 check( product_filename =
1670 uves_scired_background_filename(chip),
1671 "Error getting filename");
1673 cpl_free(product_tag);
1675 uves_sprintf(
"BKG_SCI_%s",
1679 check( uves_frameset_insert(frames,
1681 CPL_FRAME_GROUP_PRODUCT,
1682 CPL_FRAME_TYPE_IMAGE,
1683 CPL_FRAME_LEVEL_FINAL,
1686 raw_header[raw_index],
1687 rotated_header[raw_index],
1691 PACKAGE
"/" PACKAGE_VERSION,
1694 CPL_STATS_MIN | CPL_STATS_MAX),
1695 "Could not add background image '%s' (%s) "
1696 "to frameset", product_filename, product_tag);
1698 uves_msg(
"Background image '%s' (%s) added to frameset",
1699 product_filename, product_tag);
1703 if (order_trace != NULL)
1706 uves_free_propertylist(&order_trace_header);
1720 order_trace_header,
"ESO QC OPTEX NORD",
1722 cpl_table_get_column_max(ordertable,
"Order")-
1723 cpl_table_get_column_min(ordertable,
"Order")+1));
1726 order_trace_header,
"ESO QC OPTEX XSIZE",
1727 cpl_image_get_size_x(raw_image[raw_index]));
1730 order_trace_header,
"ESO QC OPTEX YSIZE",
1731 uves_round_double(extraction_slit));
1734 cpl_free(product_filename);
1735 check( product_filename =
1736 uves_scired_ordertrace_filename(chip),
1737 "Error getting filename");
1739 cpl_free(product_tag);
1741 uves_sprintf(
"ORDER_TRACE_%s",
1745 check( uves_frameset_insert(frames,
1747 CPL_FRAME_GROUP_PRODUCT,
1748 CPL_FRAME_TYPE_TABLE,
1749 CPL_FRAME_LEVEL_FINAL,
1752 raw_header[raw_index],
1762 "Could not add sky spectrum '%s' (%s) "
1764 product_filename, product_tag);
1766 uves_msg(
"Order trace table '%s' (%s) "
1767 "added to frameset",
1768 product_filename, product_tag);
1773 if (cosmic_mask != NULL)
1776 uves_free_propertylist(&cosmic_mask_header);
1779 cpl_free(product_filename);
1780 check( product_filename =
1781 uves_scired_crmask_filename(chip),
1782 "Error getting filename");
1784 cpl_free(product_tag);
1786 uves_sprintf(
"CRMASK_%s",
1790 check( uves_frameset_insert(frames,
1792 CPL_FRAME_GROUP_PRODUCT,
1793 CPL_FRAME_TYPE_TABLE,
1794 CPL_FRAME_LEVEL_FINAL,
1797 raw_header[raw_index],
1807 "Could not add cosmic ray table "
1808 "'%s' (%s) to frameset",
1809 product_filename, product_tag);
1811 uves_msg(
"Cosmic ray table '%s' (%s) "
1812 "added to frameset",
1813 product_filename, product_tag);
1823 if (wave_map != NULL)
1826 uves_free_propertylist(&wave_map_header);
1829 cpl_free(product_filename);
1830 check( product_filename =
1831 uves_scired_wmap_filename(chip),
1832 "Error getting filename");
1834 cpl_free(product_tag);
1836 uves_sprintf(
"WAVE_MAP_%s",
1840 check( uves_frameset_insert(frames,
1842 CPL_FRAME_GROUP_PRODUCT,
1843 CPL_FRAME_TYPE_IMAGE,
1844 CPL_FRAME_LEVEL_FINAL,
1847 raw_header[raw_index],
1857 "Could not add wave map "
1858 "'%s' (%s) to frameset",
1859 product_filename, product_tag);
1862 "added to frameset",
1863 product_filename, product_tag);
1867 uves_free_image(&wave_map);
1870 if (merged_sky != NULL)
1876 cpl_free(product_filename);
1877 check( product_filename =
1878 uves_scired_merged_sky_filename(chip),
1879 "Error getting filename");
1881 cpl_free(product_tag);
1883 uves_sprintf(
"MERGED_SKY_%s",
1889 check( uves_frameset_insert(
1892 CPL_FRAME_GROUP_PRODUCT,
1893 CPL_FRAME_TYPE_IMAGE,
1894 CPL_FRAME_LEVEL_FINAL,
1897 raw_header[raw_index],
1906 CPL_STATS_MIN | CPL_STATS_MAX),
1907 "Could not add sky spectrum "
1908 "'%s' (%s) to frameset",
1909 product_filename, product_tag);
1911 uves_msg(
"Sky spectrum '%s' (%s) added to frameset",
1912 product_filename, product_tag);
1921 if (fluxcal_science != NULL)
1925 cpl_free(product_filename);
1927 check( product_filename =
1929 uves_scired_fluxcal_science_2d_filename(chip) :
1930 ((m_method == MERGE_NOAPPEND) ?
1931 uves_scired_fluxcal_science_noappend_filename(chip): uves_scired_fluxcal_science_filename(chip)),
1932 "Error getting filename");
1935 cpl_free(product_tag);
1939 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1940 product_tag = uves_sprintf(
"FLUXCAL%s%s_%s_%s",
1941 (extract_is_2d) ?
"_2D" :
"",catg_is_noappend,
1949 check( uves_frameset_insert(frames,
1951 CPL_FRAME_GROUP_PRODUCT,
1952 CPL_FRAME_TYPE_IMAGE,
1953 CPL_FRAME_LEVEL_FINAL,
1956 raw_header[raw_index],
1965 CPL_STATS_MIN | CPL_STATS_MAX),
1966 "Could not add flux-calibrated science "
1967 "spectrum '%s' (%s) to frameset",
1968 product_filename, product_tag);
1970 uves_msg(
"Flux-calibrated science spectrum "
1971 "'%s' (%s) added to frameset",
1972 product_filename, product_tag);
1976 "10^-16 erg/cm^2/Angstrom/s"),
1977 "Error writing error spectrum header");
1979 cpl_free(product_filename);
1981 check( product_filename =
1983 uves_scired_fluxcal_error_2d_filename(chip) :
1984 ((m_method == MERGE_NOAPPEND) ?
1985 uves_scired_fluxcal_error_noappend_filename(chip): uves_scired_fluxcal_error_filename(chip)),
1986 "Error getting filename");
1989 cpl_free(product_tag);
1992 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1993 product_tag = uves_sprintf(
"FLUXCAL_ERRORBAR%s%s_%s_%s",
1994 (extract_is_2d) ?
"_2D" :
"",catg_is_noappend,
1999 check( uves_frameset_insert(frames,
2001 CPL_FRAME_GROUP_PRODUCT,
2002 CPL_FRAME_TYPE_IMAGE,
2003 CPL_FRAME_LEVEL_FINAL,
2006 raw_header[raw_index],
2015 CPL_STATS_MIN | CPL_STATS_MAX),
2016 "Could not add flux-calibrated science "
2017 "spectrum error '%s' (%s) to frameset",
2018 product_filename, product_tag);
2020 uves_msg(
"Flux-calibrated science spectrum error "
2021 "'%s' (%s) added to frameset",
2022 product_filename, product_tag);
2029 uves_msg(
"Skipping trace number %d", trace_number);
2036 if(strcmp(PROCESS_CHIP,
"REDL") == 0) {
2045 uves_free_table(&info_tbl);
2046 uves_free_image(&raw_image[0]);
2047 uves_free_image(&raw_image[1]);
2048 uves_free_propertylist(&raw_header[0]);
2049 uves_free_propertylist(&raw_header[1]);
2050 uves_free_propertylist(&rotated_header[0]);
2051 uves_free_propertylist(&rotated_header[1]);
2052 uves_free_propertylist(&wave_map_header);
2053 uves_free_propertylist(&wave_acc_header);
2056 uves_free_image(&master_bias);
2057 uves_free_propertylist(&master_bias_header);
2059 uves_free_image(&master_dark);
2060 uves_free_propertylist(&master_dark_header);
2062 uves_free_image(&master_flat);
2063 uves_free_propertylist(&master_flat_header);
2065 uves_free_table(&ordertable);
2066 uves_free_propertylist(&ordertable_header);
2068 uves_free_table(&traces);
2070 uves_free_table_const( &(linetable[0]) );
2071 uves_free_table_const( &(linetable[1]) );
2072 uves_free_table_const( &(linetable[2]) );
2073 uves_free_propertylist_const( &(linetable_header[0]) );
2074 uves_free_propertylist_const( &(linetable_header[1]) );
2075 uves_free_propertylist_const( &(linetable_header[2]) );
2080 uves_free_image(&response_curve);
2081 uves_free_propertylist(&response_curve_header);
2082 uves_free_table(&master_response);
2084 uves_free_table(&atm_extinction);
2089 uves_free_image(&background);
2090 uves_free_image(&flatfielded_variance);
2091 uves_free_propertylist(&flatfielded_variance_header);
2092 uves_free_image(&rebinned_science);
2093 uves_free_propertylist(&rebinned_header);
2094 uves_free_image(&resampled_science);
2095 uves_free_image(&resampled_mf);
2096 uves_free_image(&merged_sky);
2098 uves_free_image(&merged_science);
2099 uves_free_propertylist(&merged_header);
2100 uves_free_image(&reduced_science);
2101 uves_free_image(&reduced_science_error);
2102 uves_free_image(&fluxcal_science);
2103 uves_free_image(&fluxcal_error);
2104 uves_free_propertylist(&fluxcal_header);
2105 uves_free_table(&cosmic_mask);
2106 uves_free_propertylist(&cosmic_mask_header);
2108 uves_free_table(&order_trace);
2109 uves_free_propertylist(&order_trace_header);
2111 uves_free_image(&x2d);
2112 uves_free_image(&fx2d);
2113 uves_free_propertylist(&x2d_header);
2115 cpl_free(raw_frames);
2116 cpl_free(product_filename);
2118 cpl_free(product_tag);
2136 const cpl_image *raw_image,
2145 "Science-Reduction-Test-Results",
2174 cpl_image *window = NULL;
2191 "Instrument mode used.",
2197 "Optical path used.",
2203 "Slit common name.",
2207 "Error reading exposure time");
2209 nx = cpl_image_get_size_x(ima);
2210 ny = cpl_image_get_size_y(ima);
2212 for (i = 1; i <= ny; i++)
2216 int xlo = uves_max_int(1 , (nx+1)/2 - size);
2217 int xhi = uves_min_int(nx, (nx+1)/2 + size);
2219 double min, max, avg, rms, med;
2221 uves_free_image(&window);
2222 window = cpl_image_extract(ima, xlo, i, xhi, i);
2225 if (cpl_image_count_rejected(window) >= cpl_image_get_size_x(window) - 2)
2227 min = max = avg = rms = med = 0;
2231 min = cpl_image_get_min (window) / exptime;
2232 max = cpl_image_get_max (window) / exptime;
2233 avg = cpl_image_get_mean (window) / exptime;
2234 rms = cpl_image_get_stdev (window) / exptime;
2235 med = cpl_image_get_median(window) / exptime;
2238 sprintf(key_name,
"QC ORD%d DATAMIN", i);
2242 "extracted order datamin",
2245 sprintf(key_name,
"QC ORD%d DATAMAX", i);
2249 "extracted order datamax",
2252 sprintf(key_name,
"QC ORD%d DATAAVG", i);
2256 "extracted order datamean",
2259 sprintf(key_name,
"QC ORD%d DATARMS", i);
2263 "extracted order datarms",
2266 sprintf(key_name,
"QC ORD%d DATAMED", i);
2270 "extracted order datamed",
2275 uves_free_image(&window);