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,
410 cpl_image **rebinned_noise,
412 cpl_image **merged_sky,
413 cpl_image **merged_science,
415 cpl_image **reduced_science,
416 cpl_image **reduced_science_error,
417 cpl_table **cosmic_mask,
418 cpl_image **wave_map,
419 cpl_image **fluxcal_science,
420 cpl_image **fluxcal_error,
422 cpl_table **info_tbl,
423 double *extraction_slit,
424 cpl_table **order_trace)
427 cpl_image *merged_noise = NULL;
429 cpl_image *reduced_rebinned = NULL;
430 cpl_image *reduced_rebinned_noise = NULL;
432 cpl_table *response_table = NULL;
435 uves_msg(
"Reducing science object");
463 flatfielded_variance,
464 flatfielded_variance_header,
475 &reduced_rebinned_noise,
477 reduced_science_error,
481 "Could not reduce frame");
484 check( uves_plot_image_rows(*reduced_science,
485 1 + cpl_image_get_size_y(*reduced_science)/2,
486 1 + cpl_image_get_size_y(*reduced_science)/2, 1,
487 "Wavelength (arbitrary units)",
"Relative flux",
488 "Reduced science spectrum"),
497 if (response_curve != NULL || master_response != NULL)
504 int n_traces = cpl_image_get_size_y(*reduced_science);
506 uves_msg(
"Flux calibrating spectrum");
517 if (response_curve != NULL) {
518 response_table = cpl_table_new(cpl_image_get_size_x(response_curve));
519 cpl_table_new_column(response_table,
"LAMBDA", CPL_TYPE_DOUBLE);
520 cpl_table_new_column(response_table,
"FLUX_CONV", CPL_TYPE_DOUBLE);
523 "Error reading response curve start wavelength from header");
526 "Error reading bin width from header");
528 for (bin = 1; bin <= cpl_image_get_size_x(response_curve); bin++) {
533 lambda = lambda_start + (bin-1) * dlambda;
535 check( response = cpl_image_get(response_curve, bin, 1, &pis_rejected),
536 "Error reading response curve bin = %d", bin);
538 check((cpl_table_set_double(response_table,
"LAMBDA", bin - 1, lambda),
539 cpl_table_set_double(response_table,
"FLUX_CONV", bin - 1, response)),
540 "Error updating response table at row %d", bin - 1);
544 response_table = cpl_table_duplicate( master_response );
552 *reduced_science_error,
560 "Error normalizing reduced spectrum");
566 uves_msg(
"Multiplying by response function");
568 int nbins = cpl_image_get_size_x(*fluxcal_science);
569 int ntraces = cpl_image_get_size_y(*fluxcal_science);
570 double *fluxcal_science_data = cpl_image_get_data_double(*fluxcal_science);
571 double *fluxcal_science_noise = cpl_image_get_data_double(*fluxcal_error);
574 "Error reading start wavelength from reduced science header");
577 "Error reading bin width from header");
579 for (bin = 1; bin <= nbins; bin++)
586 lambda = lambda_start + (bin-1) * dlambda;
590 "LAMBDA",
"FLUX_CONV", &istart),
591 "Error interpolating response curve at lambda = %f wlu", lambda);
593 for (trace = 1; trace <= ntraces; trace++)
599 fluxcal_science_data [(bin-1) + (trace-1)*nbins] *= response;
600 fluxcal_science_noise[(bin-1) + (trace-1)*nbins] *= response;
609 check( uves_plot_image_rows(*fluxcal_science,
610 1 + cpl_image_get_size_y(*fluxcal_science)/2,
611 1 + cpl_image_get_size_y(*fluxcal_science)/2, 1,
612 "Wavelength (arbitrary units)",
613 "Flux (10^-16 erg/cm^2/Angstrom/s)",
614 "Flux calibrated science spectrum"),
619 "10^-16 erg/cm^2/Angstrom/s",
624 "Error initializing flux calibrated spectrum header");
629 uves_msg(
"Skipping absolute flux calibration");
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;
709 cpl_image *rebinned_science_error = 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_image(&rebinned_science_error);
1050 uves_free_propertylist(&rebinned_header);
1051 uves_free_image(&merged_sky);
1052 uves_free_image(&merged_science);
1053 uves_free_propertylist(&merged_header);
1054 uves_free_image(&reduced_science);
1055 uves_free_image(&reduced_science_error);
1056 uves_free_table(&cosmic_mask);
1057 uves_free_image(&fluxcal_science);
1058 uves_free_image(&fluxcal_error);
1059 uves_free_propertylist(&fluxcal_header);
1060 uves_free_table(&info_tbl);
1061 uves_free_table(&order_trace);
1065 check( badpixels_cleaned =
1067 raw_header[raw_index],
1069 false,red_ccd_is_new),
1070 "Error replacing bad pixels");
1077 raw_image[raw_index],
1078 raw_header[raw_index],
1079 rotated_header[raw_index],
1090 dispersion_relation,
1093 response_curve_header,
1103 &flatfielded_variance,
1104 &flatfielded_variance_header,
1108 &rebinned_science_error,
1114 &reduced_science_error,
1123 "Science reduction failed");
1131 raw_header[raw_index],
1132 raw_image[raw_index],
1135 "Could not compute QC parameters");
1147 cpl_free(product_filename);
1148 check( product_filename =
1150 uves_scired_red_2d_science_filename(chip) :
1151 ((m_method == MERGE_NOAPPEND) ?
1152 uves_scired_red_noappend_science_filename(chip): uves_scired_red_science_filename(chip)),
1153 "Error getting filename");
1155 cpl_free(product_tag);
1156 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1157 product_tag = uves_sprintf(
1160 (extract_is_2d) ?
"_2D" :
"",
1165 check( uves_frameset_insert(frames,
1167 CPL_FRAME_GROUP_PRODUCT,
1168 CPL_FRAME_TYPE_IMAGE,
1169 CPL_FRAME_LEVEL_FINAL,
1172 raw_header[raw_index],
1177 PACKAGE
"/" PACKAGE_VERSION,
1182 "Could not add reduced science spectrum '%s' (%s) to frameset",
1183 product_filename, product_tag);
1185 uves_msg(
"Reduced science spectrum '%s' (%s) added to frameset",
1186 product_filename, product_tag);
1191 cpl_free(product_filename);
1192 check( product_filename = uves_scired_ext2d_filename(chip),
1193 "Error getting filename");
1195 cpl_free(product_tag);
1197 uves_sprintf(
"EXT_2D_%s_%s", sci_type,
1202 check( uves_frameset_insert(frames,
1204 CPL_FRAME_GROUP_PRODUCT,
1205 CPL_FRAME_TYPE_IMAGE,
1206 CPL_FRAME_LEVEL_FINAL,
1209 raw_header[raw_index],
1214 PACKAGE
"/" PACKAGE_VERSION,
1218 "Could not add 2d extracted "
1219 "spectrum '%s' (%s) to frameset",
1220 product_filename, product_tag);
1222 uves_msg(
"2d extracted spectrum '%s' (%s) added to frameset",
1223 product_filename, product_tag);
1229 cpl_free(product_filename);
1230 check( product_filename = (extract_is_2d) ?
1231 uves_scired_merged_2d_science_filename(chip) :
1232 uves_scired_merged_science_filename(chip),
1233 "Error getting filename");
1234 cpl_free(product_tag);
1235 product_tag = uves_sprintf(
1237 (extract_is_2d) ?
"MER_2D" :
"MERGED",
1242 check( uves_frameset_insert(frames,
1244 CPL_FRAME_GROUP_PRODUCT,
1245 CPL_FRAME_TYPE_IMAGE,
1246 CPL_FRAME_LEVEL_FINAL,
1249 raw_header[raw_index],
1254 PACKAGE
"/" PACKAGE_VERSION,
1258 "Could not add merged science spectrum '%s' (%s) to frameset",
1259 product_filename, product_tag);
1261 uves_msg(
"Merged science spectrum '%s' (%s) added to frameset",
1262 product_filename, product_tag);
1269 cpl_free(product_filename);
1270 check( product_filename = (extract_is_2d) ?
1271 uves_scired_resampled_2d_filename(chip) :
1272 uves_scired_resampled_filename(chip),
1273 "Error getting filename");
1275 cpl_free(product_tag);
1277 uves_sprintf(
"WCALIB_%s_%s", sci_type,
1282 check( uves_frameset_insert(frames,
1284 CPL_FRAME_GROUP_PRODUCT,
1285 CPL_FRAME_TYPE_IMAGE,
1286 CPL_FRAME_LEVEL_FINAL,
1289 raw_header[raw_index],
1294 PACKAGE
"/" PACKAGE_VERSION,
1298 "Could not add wavelength calibrated science "
1299 "spectrum '%s' (%s) to frameset", product_filename,
1302 uves_msg(
"Wavelength calibrated science spectrum '%s' "
1303 "(%s) added to frameset", product_filename,
1307 cpl_free(product_filename);
1309 check( product_filename =
1310 uves_order_extract_qc_standard_filename(chip),
1311 "Error getting filename");
1314 check( uves_frameset_insert(frames,
1316 CPL_FRAME_GROUP_PRODUCT,
1317 CPL_FRAME_TYPE_TABLE,
1318 CPL_FRAME_LEVEL_INTERMEDIATE,
1320 UVES_ORDER_EXTRACT_QC(chip),
1321 raw_header[raw_index],
1322 rotated_header[raw_index],
1326 PACKAGE
"/" PACKAGE_VERSION,
1330 "Could not add extraction quality table %s (%s)"
1332 UVES_ORDER_EXTRACT_QC(chip));
1334 uves_msg(
"Extraction quality table '%s' "
1335 "(%s) added to frameset", product_filename,
1336 UVES_ORDER_EXTRACT_QC(chip));
1343 const char *ff =
"";
1347 context = uves_sprintf(
"%s.%s", recipe_id, UVES_REDUCE_ID);
1348 check( uves_get_parameter(parameters, NULL,
1351 CPL_TYPE_STRING, &ff),
1352 "Could not read parameter");
1355 if (strcmp(ff,
"no") != 0)
1359 cpl_table *qc_tabs[] = {NULL, NULL, NULL};
1365 if ( strcmp(recipe_id, make_str(UVES_TFLAT_ID)) == 0 )
1372 raw_header[raw_index],
1374 "Could not compute QC parameters");
1376 qc_tabs[0] = qclog_tflat;
1377 qc_tabs[1] = qclog[0];
1381 qc_tabs[0] = qclog[0];
1385 cpl_free(product_filename);
1386 check( product_filename =
1388 uves_scired_rebinned_2d_filename(chip) :
1389 uves_scired_rebinned_filename(chip),
1390 "Error getting filename");
1392 cpl_free(product_tag);
1393 product_tag = uves_sprintf(
1395 (extract_is_2d) ?
"WCAL_2D" :
"WCALIB_FF",
1400 check( uves_frameset_insert(frames,
1402 CPL_FRAME_GROUP_PRODUCT,
1403 CPL_FRAME_TYPE_IMAGE,
1404 CPL_FRAME_LEVEL_FINAL,
1407 raw_header[raw_index],
1417 "Could not add wavelength calibrated flat-fielded "
1418 "science spectrum '%s' (%s) to frameset",
1419 product_filename, product_tag);
1421 uves_msg(
"Wavelength calibrated flat-fielded science "
1422 "spectrum '%s' (%s) added to frameset",
1423 product_filename, product_tag);
1425 cpl_free(product_filename);
1426 check( product_filename =
1428 uves_scired_rebinned_2d_error_filename(chip) :
1429 uves_scired_rebinned_error_filename(chip),
1430 "Error getting filename");
1432 cpl_free(product_tag);
1433 product_tag = uves_sprintf(
1435 (extract_is_2d) ?
"ERRORBAR_WCAL_2D" :
"ERRORBAR_WCALIB_FF",
1442 check( uves_frameset_insert(frames,
1443 rebinned_science_error,
1444 CPL_FRAME_GROUP_PRODUCT,
1445 CPL_FRAME_TYPE_IMAGE,
1446 CPL_FRAME_LEVEL_FINAL,
1449 raw_header[raw_index],
1459 "Could not add wavelength calibrated flat-fielded "
1460 "science spectrum '%s' (%s) to frameset",
1461 product_filename, product_tag);
1463 uves_msg(
"Wavelength calibrated flat-fielded science "
1464 "spectrum error '%s' (%s) added to frameset",
1465 product_filename, product_tag);
1473 cpl_free(product_filename);
1474 check( product_filename =
1475 uves_scired_resampledmf_filename(chip),
1476 "Error getting filename");
1478 cpl_free(product_tag);
1481 "WCALIB_FLAT_OBJ_%s",
1513 cpl_free(raw_frames);
1517 cpl_frame_set_group(mflat_frame,
1518 CPL_FRAME_GROUP_RAW);
1522 check( uves_frameset_insert(
1525 CPL_FRAME_GROUP_PRODUCT,
1526 CPL_FRAME_TYPE_IMAGE,
1527 CPL_FRAME_LEVEL_FINAL,
1539 CPL_STATS_MIN | CPL_STATS_MAX),
1540 "Could not add wavelength calibrated "
1541 "flat-field '%s' (%s) to frameset",
1542 product_filename, product_tag);
1544 uves_msg(
"Wavelength calibrated flat-field "
1545 "spectrum '%s' (%s) added to frameset",
1546 product_filename, product_tag);
1552 raw_frames[i] != NULL;
1555 cpl_frame_set_group(
1557 CPL_FRAME_GROUP_RAW);
1560 cpl_frame_set_group(mflat_frame,
1561 CPL_FRAME_GROUP_CALIB);
1567 cpl_free(product_filename);
1568 check( product_filename =
1569 uves_scired_ff2d_filename(chip),
1570 "Error getting filename");
1572 cpl_free(product_tag);
1575 "FF_2D_%s_%s", sci_type,
1579 check( uves_frameset_insert(
1582 CPL_FRAME_GROUP_PRODUCT,
1583 CPL_FRAME_TYPE_IMAGE,
1584 CPL_FRAME_LEVEL_FINAL,
1587 raw_header[raw_index],
1597 "Could not add 2d extracted, flat-fielded "
1598 "spectrum '%s' (%s) to frameset",
1599 product_filename, product_tag);
1601 uves_msg(
"2d extracted, flat-fielded spectrum "
1602 "'%s' (%s) added to frameset",
1603 product_filename, product_tag);
1610 "Error writing error spectrum header");
1614 cpl_free(product_filename);
1616 check( product_filename =
1618 uves_scired_red_2d_error_filename(chip) :
1619 ((m_method == MERGE_NOAPPEND) ?
1620 uves_scired_red_noappend_error_filename(chip): uves_scired_red_error_filename(chip)),
1621 "Error getting filename");
1624 cpl_free(product_tag);
1625 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1626 product_tag = uves_sprintf(
"%s%s_%s_%s",
1627 (extract_is_2d) ?
"ERR_2D" :
"ERRORBAR",catg_is_noappend,
1640 check( uves_frameset_insert(
1642 reduced_science_error,
1643 CPL_FRAME_GROUP_PRODUCT,
1644 CPL_FRAME_TYPE_IMAGE,
1645 CPL_FRAME_LEVEL_FINAL,
1648 raw_header[raw_index],
1653 PACKAGE
"/" PACKAGE_VERSION,
1656 CPL_STATS_MIN | CPL_STATS_MAX),
1657 "Could not add error bars '%s' (%s) to frameset",
1658 product_filename, product_tag);
1660 uves_msg(
"Science spectrum error '%s' (%s) "
1661 "added to frameset",
1662 product_filename, product_tag);
1671 cpl_free(product_filename);
1672 check( product_filename =
1673 uves_scired_ff_variance_filename(chip),
1674 "Error getting filename");
1676 cpl_free(product_tag);
1678 uves_sprintf(
"VARIANCE_%s_%s", sci_type,
1684 check( uves_frameset_insert(frames,
1685 flatfielded_variance,
1686 CPL_FRAME_GROUP_PRODUCT,
1687 CPL_FRAME_TYPE_IMAGE,
1688 CPL_FRAME_LEVEL_FINAL,
1691 raw_header[raw_index],
1692 flatfielded_variance_header,
1696 PACKAGE
"/" PACKAGE_VERSION,
1699 CPL_STATS_MIN | CPL_STATS_MAX),
1700 "Could not add flat-fielded spectrum variance "
1701 "'%s' (%s) to frameset",
1702 product_filename, product_tag);
1704 uves_msg(
"Flat-fielded spectrum variance '%s' (%s) "
1705 "added to frameset",
1706 product_filename, product_tag);
1714 cpl_free(product_filename);
1715 check( product_filename =
1716 uves_scired_background_filename(chip),
1717 "Error getting filename");
1719 cpl_free(product_tag);
1721 uves_sprintf(
"BKG_SCI_%s",
1725 check( uves_frameset_insert(frames,
1727 CPL_FRAME_GROUP_PRODUCT,
1728 CPL_FRAME_TYPE_IMAGE,
1729 CPL_FRAME_LEVEL_FINAL,
1732 raw_header[raw_index],
1733 rotated_header[raw_index],
1737 PACKAGE
"/" PACKAGE_VERSION,
1740 CPL_STATS_MIN | CPL_STATS_MAX),
1741 "Could not add background image '%s' (%s) "
1742 "to frameset", product_filename, product_tag);
1744 uves_msg(
"Background image '%s' (%s) added to frameset",
1745 product_filename, product_tag);
1749 if (order_trace != NULL)
1752 uves_free_propertylist(&order_trace_header);
1766 order_trace_header,
"ESO QC OPTEX NORD",
1768 cpl_table_get_column_max(ordertable,
"Order")-
1769 cpl_table_get_column_min(ordertable,
"Order")+1));
1772 order_trace_header,
"ESO QC OPTEX XSIZE",
1773 cpl_image_get_size_x(raw_image[raw_index]));
1776 order_trace_header,
"ESO QC OPTEX YSIZE",
1777 uves_round_double(extraction_slit));
1780 cpl_free(product_filename);
1781 check( product_filename =
1782 uves_scired_ordertrace_filename(chip),
1783 "Error getting filename");
1785 cpl_free(product_tag);
1787 uves_sprintf(
"ORDER_TRACE_%s",
1791 check( uves_frameset_insert(frames,
1793 CPL_FRAME_GROUP_PRODUCT,
1794 CPL_FRAME_TYPE_TABLE,
1795 CPL_FRAME_LEVEL_FINAL,
1798 raw_header[raw_index],
1808 "Could not add sky spectrum '%s' (%s) "
1810 product_filename, product_tag);
1812 uves_msg(
"Order trace table '%s' (%s) "
1813 "added to frameset",
1814 product_filename, product_tag);
1819 if (cosmic_mask != NULL)
1822 uves_free_propertylist(&cosmic_mask_header);
1825 cpl_free(product_filename);
1826 check( product_filename =
1827 uves_scired_crmask_filename(chip),
1828 "Error getting filename");
1830 cpl_free(product_tag);
1832 uves_sprintf(
"CRMASK_%s",
1836 check( uves_frameset_insert(frames,
1838 CPL_FRAME_GROUP_PRODUCT,
1839 CPL_FRAME_TYPE_TABLE,
1840 CPL_FRAME_LEVEL_FINAL,
1843 raw_header[raw_index],
1853 "Could not add cosmic ray table "
1854 "'%s' (%s) to frameset",
1855 product_filename, product_tag);
1857 uves_msg(
"Cosmic ray table '%s' (%s) "
1858 "added to frameset",
1859 product_filename, product_tag);
1869 if (wave_map != NULL)
1872 uves_free_propertylist(&wave_map_header);
1875 cpl_free(product_filename);
1876 check( product_filename =
1877 uves_scired_wmap_filename(chip),
1878 "Error getting filename");
1880 cpl_free(product_tag);
1882 uves_sprintf(
"WAVE_MAP_%s",
1886 check( uves_frameset_insert(frames,
1888 CPL_FRAME_GROUP_PRODUCT,
1889 CPL_FRAME_TYPE_IMAGE,
1890 CPL_FRAME_LEVEL_FINAL,
1893 raw_header[raw_index],
1903 "Could not add wave map "
1904 "'%s' (%s) to frameset",
1905 product_filename, product_tag);
1908 "added to frameset",
1909 product_filename, product_tag);
1913 uves_free_image(&wave_map);
1916 if (merged_sky != NULL)
1922 cpl_free(product_filename);
1923 check( product_filename =
1924 uves_scired_merged_sky_filename(chip),
1925 "Error getting filename");
1927 cpl_free(product_tag);
1929 uves_sprintf(
"MERGED_SKY_%s",
1935 check( uves_frameset_insert(
1938 CPL_FRAME_GROUP_PRODUCT,
1939 CPL_FRAME_TYPE_IMAGE,
1940 CPL_FRAME_LEVEL_FINAL,
1943 raw_header[raw_index],
1952 CPL_STATS_MIN | CPL_STATS_MAX),
1953 "Could not add sky spectrum "
1954 "'%s' (%s) to frameset",
1955 product_filename, product_tag);
1957 uves_msg(
"Sky spectrum '%s' (%s) added to frameset",
1958 product_filename, product_tag);
1967 if (fluxcal_science != NULL)
1971 cpl_free(product_filename);
1973 check( product_filename =
1975 uves_scired_fluxcal_science_2d_filename(chip) :
1976 ((m_method == MERGE_NOAPPEND) ?
1977 uves_scired_fluxcal_science_noappend_filename(chip): uves_scired_fluxcal_science_filename(chip)),
1978 "Error getting filename");
1981 cpl_free(product_tag);
1985 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
1986 product_tag = uves_sprintf(
"FLUXCAL%s%s_%s_%s",
1987 (extract_is_2d) ?
"_2D" :
"",catg_is_noappend,
1995 check( uves_frameset_insert(frames,
1997 CPL_FRAME_GROUP_PRODUCT,
1998 CPL_FRAME_TYPE_IMAGE,
1999 CPL_FRAME_LEVEL_FINAL,
2002 raw_header[raw_index],
2011 CPL_STATS_MIN | CPL_STATS_MAX),
2012 "Could not add flux-calibrated science "
2013 "spectrum '%s' (%s) to frameset",
2014 product_filename, product_tag);
2016 uves_msg(
"Flux-calibrated science spectrum "
2017 "'%s' (%s) added to frameset",
2018 product_filename, product_tag);
2022 "10^-16 erg/cm^2/Angstrom/s"),
2023 "Error writing error spectrum header");
2025 cpl_free(product_filename);
2027 check( product_filename =
2029 uves_scired_fluxcal_error_2d_filename(chip) :
2030 ((m_method == MERGE_NOAPPEND) ?
2031 uves_scired_fluxcal_error_noappend_filename(chip): uves_scired_fluxcal_error_filename(chip)),
2032 "Error getting filename");
2035 cpl_free(product_tag);
2038 catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
2039 product_tag = uves_sprintf(
"FLUXCAL_ERRORBAR%s%s_%s_%s",
2040 (extract_is_2d) ?
"_2D" :
"",catg_is_noappend,
2045 check( uves_frameset_insert(frames,
2047 CPL_FRAME_GROUP_PRODUCT,
2048 CPL_FRAME_TYPE_IMAGE,
2049 CPL_FRAME_LEVEL_FINAL,
2052 raw_header[raw_index],
2061 CPL_STATS_MIN | CPL_STATS_MAX),
2062 "Could not add flux-calibrated science "
2063 "spectrum error '%s' (%s) to frameset",
2064 product_filename, product_tag);
2066 uves_msg(
"Flux-calibrated science spectrum error "
2067 "'%s' (%s) added to frameset",
2068 product_filename, product_tag);
2075 uves_msg(
"Skipping trace number %d", trace_number);
2082 if(strcmp(PROCESS_CHIP,
"REDL") == 0) {
2091 uves_free_table(&info_tbl);
2092 uves_free_image(&raw_image[0]);
2093 uves_free_image(&raw_image[1]);
2094 uves_free_propertylist(&raw_header[0]);
2095 uves_free_propertylist(&raw_header[1]);
2096 uves_free_propertylist(&rotated_header[0]);
2097 uves_free_propertylist(&rotated_header[1]);
2098 uves_free_propertylist(&wave_map_header);
2099 uves_free_propertylist(&wave_acc_header);
2102 uves_free_image(&master_bias);
2103 uves_free_propertylist(&master_bias_header);
2105 uves_free_image(&master_dark);
2106 uves_free_propertylist(&master_dark_header);
2108 uves_free_image(&master_flat);
2109 uves_free_propertylist(&master_flat_header);
2111 uves_free_table(&ordertable);
2112 uves_free_propertylist(&ordertable_header);
2114 uves_free_table(&traces);
2116 uves_free_table_const( &(linetable[0]) );
2117 uves_free_table_const( &(linetable[1]) );
2118 uves_free_table_const( &(linetable[2]) );
2119 uves_free_propertylist_const( &(linetable_header[0]) );
2120 uves_free_propertylist_const( &(linetable_header[1]) );
2121 uves_free_propertylist_const( &(linetable_header[2]) );
2126 uves_free_image(&response_curve);
2127 uves_free_propertylist(&response_curve_header);
2128 uves_free_table(&master_response);
2130 uves_free_table(&atm_extinction);
2135 uves_free_image(&background);
2136 uves_free_image(&flatfielded_variance);
2137 uves_free_propertylist(&flatfielded_variance_header);
2138 uves_free_image(&rebinned_science);
2139 uves_free_image(&rebinned_science_error);
2140 uves_free_propertylist(&rebinned_header);
2141 uves_free_image(&resampled_science);
2142 uves_free_image(&resampled_mf);
2143 uves_free_image(&merged_sky);
2145 uves_free_image(&merged_science);
2146 uves_free_propertylist(&merged_header);
2147 uves_free_image(&reduced_science);
2148 uves_free_image(&reduced_science_error);
2149 uves_free_image(&fluxcal_science);
2150 uves_free_image(&fluxcal_error);
2151 uves_free_propertylist(&fluxcal_header);
2152 uves_free_table(&cosmic_mask);
2153 uves_free_propertylist(&cosmic_mask_header);
2155 uves_free_table(&order_trace);
2156 uves_free_propertylist(&order_trace_header);
2158 uves_free_image(&x2d);
2159 uves_free_image(&fx2d);
2160 uves_free_propertylist(&x2d_header);
2162 cpl_free(raw_frames);
2163 cpl_free(product_filename);
2165 cpl_free(product_tag);
2183 const cpl_image *raw_image,
2192 "Science-Reduction-Test-Results",
2221 cpl_image *window = NULL;
2238 "Instrument mode used.",
2244 "Optical path used.",
2250 "Slit common name.",
2254 "Error reading exposure time");
2256 nx = cpl_image_get_size_x(ima);
2257 ny = cpl_image_get_size_y(ima);
2259 for (i = 1; i <= ny; i++)
2263 int xlo = uves_max_int(1 , (nx+1)/2 - size);
2264 int xhi = uves_min_int(nx, (nx+1)/2 + size);
2266 double min, max, avg, rms, med;
2268 uves_free_image(&window);
2269 window = cpl_image_extract(ima, xlo, i, xhi, i);
2272 if (cpl_image_count_rejected(window) >= cpl_image_get_size_x(window) - 2)
2274 min = max = avg = rms = med = 0;
2278 min = cpl_image_get_min (window) / exptime;
2279 max = cpl_image_get_max (window) / exptime;
2280 avg = cpl_image_get_mean (window) / exptime;
2281 rms = cpl_image_get_stdev (window) / exptime;
2282 med = cpl_image_get_median(window) / exptime;
2285 sprintf(key_name,
"QC ORD%d DATAMIN", i);
2289 "extracted order datamin",
2292 sprintf(key_name,
"QC ORD%d DATAMAX", i);
2296 "extracted order datamax",
2299 sprintf(key_name,
"QC ORD%d DATAAVG", i);
2303 "extracted order datamean",
2306 sprintf(key_name,
"QC ORD%d DATARMS", i);
2310 "extracted order datarms",
2313 sprintf(key_name,
"QC ORD%d DATAMED", i);
2317 "extracted order datamed",
2322 uves_free_image(&window);