27 #include <cxmessages.h>
29 #include <cpl_recipe.h>
30 #include <cpl_plugininfo.h>
31 #include <cpl_parameterlist.h>
32 #include <cpl_frameset.h>
33 #include <cpl_propertylist.h>
40 #include "gislitgeometry.h"
41 #include "gipsfdata.h"
42 #include "gifiberutils.h"
44 #include "giextract.h"
47 #include "giwlcalibration.h"
48 #include "girebinning.h"
49 #include "gisgcalibration.h"
52 static cxint giwavecalibration(cpl_parameterlist* config, cpl_frameset* set);
53 static cxint giqcwavecalibration(cpl_frameset* set);
62 giwavecalibration_create(cpl_plugin* plugin)
65 cpl_recipe* recipe = (cpl_recipe*)plugin;
79 recipe->parameters = cpl_parameterlist_new();
80 cx_assert(recipe->parameters != NULL);
103 p = cpl_parameter_new_value(
"giraffe.wcal.rebin",
105 "Rebin extracted arc-lamp spectra.",
109 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"wcal-rebin");
110 cpl_parameterlist_append(recipe->parameters, p);
116 p = cpl_parameter_new_value(
"giraffe.wcal.slitgeometry",
118 "Controls the slit geometry calibration.",
122 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"wcal-slit");
123 cpl_parameterlist_append(recipe->parameters, p);
136 giwavecalibration_exec(cpl_plugin* plugin)
139 cpl_recipe* recipe = (cpl_recipe*)plugin;
144 if (recipe->parameters == NULL || recipe->frames == NULL) {
148 status = giwavecalibration(recipe->parameters, recipe->frames);
154 status = giqcwavecalibration(recipe->frames);
166 giwavecalibration_destroy(cpl_plugin* plugin)
169 cpl_recipe* recipe = (cpl_recipe*)plugin;
178 cpl_parameterlist_delete(recipe->parameters); recipe->parameters = NULL;
180 giraffe_error_clear();
191 giwavecalibration(cpl_parameterlist* config, cpl_frameset* set)
194 const cxchar*
const fctid =
"giwavecalibration";
197 const cxchar* filename = NULL;
199 cxbool rebin = FALSE;
200 cxbool slitgeometry = FALSE;
203 cxint narcspectrum = 0;
205 const cpl_propertylist* properties = NULL;
207 cpl_frame* arcspec_frame = NULL;
208 cpl_frame* bpixel_frame = NULL;
209 cpl_frame* mbias_frame = NULL;
210 cpl_frame* mlocy_frame = NULL;
211 cpl_frame* mlocw_frame = NULL;
212 cpl_frame* psfdata_frame = NULL;
213 cpl_frame* slight_frame = NULL;
214 cpl_frame* grating_frame = NULL;
215 cpl_frame* slitgeo_frame = NULL;
216 cpl_frame* lines_frame = NULL;
217 cpl_frame* wcal_frame = NULL;
218 cpl_frame* ldata_frame = NULL;
219 cpl_frame* scal_frame = NULL;
220 cpl_frame* sext_frame = NULL;
222 cpl_parameter* p = NULL;
224 cpl_matrix* biasareas = NULL;
226 GiImage* arcspectrum = NULL;
227 GiImage* bsarcspectrum = NULL;
228 GiImage* slight = NULL;
229 GiImage* mbias = NULL;
230 GiImage* bpixel = NULL;
232 GiLocalization* localization = NULL;
233 GiExtraction* extraction = NULL;
234 GiRebinning* rebinning = NULL;
235 GiWCalData* wlsolution = NULL;
237 GiTable* fibers = NULL;
238 GiTable* grating = NULL;
239 GiTable* slitgeo = NULL;
240 GiTable* wavelengths = NULL;
241 GiTable* wcal_initial = NULL;
245 GiBiasConfig* bias_config = NULL;
246 GiExtractConfig* extract_config = NULL;
249 GiFrameCreator creator = NULL;
251 GiRecipeInfo info = {(cxchar*)fctid, 1, NULL, config};
253 GiGroupInfo groups[] = {
254 {GIFRAME_ARC_SPECTRUM, CPL_FRAME_GROUP_RAW},
255 {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
256 {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
257 {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
258 {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
259 {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
260 {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
261 {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
262 {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
263 {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
264 {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
265 {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
266 {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
267 {GIFRAME_LINE_CATALOG, CPL_FRAME_GROUP_CALIB},
268 {GIFRAME_LINE_MASK, CPL_FRAME_GROUP_CALIB},
269 {NULL, CPL_FRAME_GROUP_NONE}
275 cpl_msg_error(fctid,
"Invalid parameter list! Aborting ...");
280 cpl_msg_error(fctid,
"Invalid frame set! Aborting ...");
284 p = cpl_parameterlist_find(config,
"giraffe.wcal.rebin");
287 rebin = cpl_parameter_get_bool(p);
290 p = cpl_parameterlist_find(config,
"giraffe.wcal.slitgeometry");
293 slitgeometry = cpl_parameter_get_bool(p);
296 status = giraffe_frameset_set_groups(set, groups);
299 cpl_msg_error(fctid,
"Setting frame group information failed!");
308 cpl_msg_info(fctid,
"Recipe Step : Initialization");
314 narcspectrum = cpl_frameset_count_tags(set, GIFRAME_ARC_SPECTRUM);
316 if (narcspectrum > 1) {
317 cpl_msg_error(fctid,
"Only one arc spectrum frame allowed! "
321 else if (narcspectrum < 1) {
322 cpl_msg_error(fctid,
"Arc spectrum frame is missing! "
327 arcspec_frame = cpl_frameset_find(set, GIFRAME_ARC_SPECTRUM);
328 bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
331 cpl_msg_info(fctid,
"No bad pixel map present in frame set.");
334 mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
337 cpl_msg_info(fctid,
"No master bias present in frame set.");
340 mlocy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
342 if (mlocy_frame == NULL) {
344 mlocy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
346 if (mlocy_frame == NULL) {
347 cpl_msg_info(fctid,
"No master localization (centroid position) "
348 "present in frame set. Aborting ...");
354 mlocw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
356 if (mlocw_frame == NULL) {
358 mlocw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
360 if (mlocw_frame == NULL) {
361 cpl_msg_info(fctid,
"No master localization (spectrum width) "
362 "present in frame set. Aborting ...");
368 psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
370 if (!psfdata_frame) {
371 cpl_msg_info(fctid,
"No PSF profile parameters present in frame set.");
374 slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
377 cpl_msg_info(fctid,
"No scattered light model present in frame set.");
380 grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
382 if (!grating_frame) {
383 cpl_msg_error(fctid,
"No grating table present in frame set, "
390 if (!slitgeo_frame) {
391 cpl_msg_error(fctid,
"No slit geometry table present in frame "
396 lines_frame = cpl_frameset_find(set, GIFRAME_LINE_CATALOG);
399 cpl_msg_error(fctid,
"No wavelength table present in frame set, "
404 wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
407 cpl_msg_info(fctid,
"No wavelength solution present in frame set.");
410 scal_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
414 if (slitgeometry == TRUE) {
415 cpl_msg_error(fctid,
"No line mask present in frame "
416 "set. Aborting ...");
420 cpl_msg_info(fctid,
"No slit geometry mask present in frame "
437 filename = cpl_frame_get_filename(bpixel_frame);
443 cpl_msg_error(fctid,
"Cannot load bad pixel map from '%s'. "
444 "Aborting ...", filename);
457 filename = cpl_frame_get_filename(arcspec_frame);
463 cpl_msg_error(fctid,
"Cannot load arc spectrum from '%s'. "
464 "Aborting...", filename);
476 cpl_msg_info(fctid,
"Recipe Step : Bias Removal");
484 if (bias_config->method == GIBIAS_METHOD_MASTER ||
485 bias_config->method == GIBIAS_METHOD_ZMASTER) {
488 cpl_msg_error(fctid,
"Missing master bias frame! Selected bias "
489 "removal method requires a master bias "
501 filename = cpl_frame_get_filename(mbias_frame);
507 cpl_msg_error(fctid,
"Cannot load master bias from '%s'. "
508 "Aborting ...", filename);
528 biasareas, bias_config);
542 cpl_msg_error(fctid,
"Bias removal failed. Aborting ...");
554 cpl_msg_info(fctid,
"Recipe Step : Fiber Setup");
556 cpl_msg_info(fctid,
"Building fiber setup for frame '%s'.",
557 cpl_frame_get_filename(arcspec_frame));
562 cpl_msg_error(fctid,
"Cannot create fiber setup for frame '%s'! "
563 "Aborting ...", cpl_frame_get_filename(arcspec_frame));
571 cpl_msg_info(fctid,
"Fiber reference setup taken from localization "
572 "frame '%s'.", cpl_frame_get_filename(mlocy_frame));
579 localization = giraffe_localization_new();
581 filename = cpl_frame_get_filename(mlocy_frame);
588 cpl_msg_error(fctid,
"Cannot load localization (centroid "
589 "position) frame from '%s'. Aborting ...",
592 giraffe_localization_destroy(localization);
602 filename = cpl_frame_get_filename(mlocw_frame);
609 cpl_msg_error(fctid,
"Cannot load localization (spectrum width) "
610 "frame from '%s'. Aborting ...", filename);
612 giraffe_localization_destroy(localization);
626 cpl_msg_info(fctid,
"Recipe Step : Spectrum Extraction");
630 filename = cpl_frame_get_filename(slight_frame);
637 cpl_msg_error(fctid,
"Cannot load scattered light model from "
638 "'%s'. Aborting ...", filename);
643 giraffe_localization_destroy(localization);
654 if ((extract_config->emethod == GIEXTRACT_OPTIMAL) ||
655 (extract_config->emethod == GIEXTRACT_HORNE)) {
657 if (psfdata_frame == NULL) {
659 const cxchar* emethod =
"Optimal";
661 if (extract_config->emethod == GIEXTRACT_HORNE) {
665 cpl_msg_error(fctid,
"%s spectrum extraction requires PSF "
666 "profile data. Aborting ...", emethod);
669 extract_config = NULL;
671 if (slight != NULL) {
676 giraffe_localization_destroy(localization);
688 bsarcspectrum = NULL;
695 filename = cpl_frame_get_filename(psfdata_frame);
698 localization->psf = giraffe_psfdata_new();
699 status = giraffe_psfdata_load(localization->psf, filename);
702 cpl_msg_error(fctid,
"Cannot load PSF profile data frame from "
703 "'%s'. Aborting ...", filename);
706 extract_config = NULL;
708 if (slight != NULL) {
713 giraffe_localization_destroy(localization);
725 bsarcspectrum = NULL;
736 extraction = giraffe_extraction_new();
739 localization, bpixel, slight,
743 cpl_msg_error(fctid,
"Spectrum extraction failed! Aborting ...");
748 giraffe_localization_destroy(localization);
751 giraffe_extraction_destroy(extraction);
763 bsarcspectrum = NULL;
766 extract_config = NULL;
773 cpl_msg_info(fctid,
"Writing extracted spectra ...");
780 GIFRAME_ARC_LAMP_EXTSPECTRA,
781 CPL_FRAME_LEVEL_INTERMEDIATE,
784 if (sext_frame == NULL) {
785 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
789 giraffe_localization_destroy(localization);
790 giraffe_extraction_destroy(extraction);
798 cpl_msg_error(fctid,
"Cannot attach fiber setup to local file '%s'! "
799 "Aborting ...", cpl_frame_get_filename(sext_frame));
801 cpl_frame_delete(sext_frame);
805 giraffe_localization_destroy(localization);
806 giraffe_extraction_destroy(extraction);
811 cpl_frameset_insert(set, sext_frame);
818 GIFRAME_ARC_LAMP_EXTERRORS,
819 CPL_FRAME_LEVEL_INTERMEDIATE,
822 if (sext_frame == NULL) {
823 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
827 giraffe_localization_destroy(localization);
828 giraffe_extraction_destroy(extraction);
836 cpl_msg_error(fctid,
"Cannot attach fiber setup to local file '%s'! "
837 "Aborting ...", cpl_frame_get_filename(sext_frame));
839 cpl_frame_delete(sext_frame);
843 giraffe_localization_destroy(localization);
844 giraffe_extraction_destroy(extraction);
849 cpl_frameset_insert(set, sext_frame);
853 if (extraction->npixels != NULL) {
858 GIFRAME_ARC_LAMP_EXTPIXELS,
859 CPL_FRAME_LEVEL_INTERMEDIATE,
862 if (sext_frame == NULL) {
863 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
867 giraffe_localization_destroy(localization);
868 giraffe_extraction_destroy(extraction);
876 cpl_msg_error(fctid,
"Cannot attach fiber setup to local file '%s'! "
877 "Aborting ...", cpl_frame_get_filename(sext_frame));
879 cpl_frame_delete(sext_frame);
883 giraffe_localization_destroy(localization);
884 giraffe_extraction_destroy(extraction);
889 cpl_frameset_insert(set, sext_frame);
898 GIFRAME_ARC_LAMP_EXTTRACE,
899 CPL_FRAME_LEVEL_INTERMEDIATE,
902 if (sext_frame == NULL) {
903 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
907 giraffe_localization_destroy(localization);
908 giraffe_extraction_destroy(extraction);
916 cpl_msg_error(fctid,
"Cannot attach fiber setup to local file '%s'! "
917 "Aborting ...", cpl_frame_get_filename(sext_frame));
919 cpl_frame_delete(sext_frame);
923 giraffe_localization_destroy(localization);
924 giraffe_extraction_destroy(extraction);
929 cpl_frameset_insert(set, sext_frame);
933 if (extraction->model != NULL) {
938 GIFRAME_ARC_LAMP_EXTMODEL,
939 CPL_FRAME_LEVEL_FINAL,
942 if (sext_frame == NULL) {
943 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
947 giraffe_localization_destroy(localization);
948 giraffe_extraction_destroy(extraction);
956 cpl_msg_error(fctid,
"Cannot attach fiber setup to local file '%s'! "
957 "Aborting ...", cpl_frame_get_filename(sext_frame));
959 cpl_frame_delete(sext_frame);
963 giraffe_localization_destroy(localization);
964 giraffe_extraction_destroy(extraction);
969 cpl_frameset_insert(set, sext_frame);
977 cpl_msg_info(fctid,
"Recipe Step : Wavelength Calibration");
979 filename = cpl_frame_get_filename(grating_frame);
986 cpl_msg_error(fctid,
"Cannot load grating table from '%s'. "
987 "Aborting ...", filename);
990 giraffe_localization_destroy(localization);
991 giraffe_extraction_destroy(extraction);
997 filename = cpl_frame_get_filename(slitgeo_frame);
1001 if (slitgeo == NULL) {
1002 cpl_msg_error(fctid,
"Cannot load slit geometry table from '%s'. "
1003 "Aborting ...", filename);
1006 giraffe_localization_destroy(localization);
1007 giraffe_extraction_destroy(extraction);
1021 cpl_msg_error(fctid,
"Slit geometry data from '%s' is not "
1022 "applicable for current fiber setup! "
1023 "Aborting ...", filename);
1027 giraffe_localization_destroy(localization);
1028 giraffe_extraction_destroy(extraction);
1037 filename = cpl_frame_get_filename(lines_frame);
1044 cpl_msg_error(fctid,
"Cannot load arc-line data from '%s'. "
1045 "Aborting ...", filename);
1048 giraffe_localization_destroy(localization);
1049 giraffe_extraction_destroy(extraction);
1057 if (wcal_frame != NULL) {
1061 filename = cpl_frame_get_filename(wcal_frame);
1065 cpl_msg_error(fctid,
"Cannot load initial wavelength solution "
1066 "from '%s'. Aborting ...", filename);
1071 giraffe_localization_destroy(localization);
1072 giraffe_extraction_destroy(extraction);
1084 if (wcal_config == NULL) {
1086 cpl_msg_error(fctid,
"Could not create wavelength calibration "
1087 "setup: error parsing configuration parameters! "
1090 giraffe_localization_destroy(localization);
1091 giraffe_extraction_destroy(extraction);
1101 wlsolution = giraffe_wcaldata_new();
1104 localization, fibers, slitgeo,
1105 grating, wavelengths, wcal_initial,
1109 cpl_msg_error(fctid,
"Error during wavelength calibration, "
1113 giraffe_localization_destroy(localization);
1114 giraffe_extraction_destroy(extraction);
1120 giraffe_wcaldata_delete(wlsolution);
1131 wcal_initial = NULL;
1146 GIFRAME_WAVELENGTH_SOLUTION,
1147 CPL_FRAME_LEVEL_FINAL,
1150 if (wcal_frame == NULL) {
1152 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
1155 giraffe_localization_destroy(localization);
1156 giraffe_extraction_destroy(extraction);
1160 giraffe_wcaldata_delete(wlsolution);
1166 cpl_frameset_insert(set, wcal_frame);
1171 creator = (GiFrameCreator)giraffe_linedata_writer;
1174 CPL_FRAME_LEVEL_FINAL,
1175 properties, wlsolution->linedata,
1179 if (ldata_frame == NULL) {
1181 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
1184 giraffe_localization_destroy(localization);
1185 giraffe_extraction_destroy(extraction);
1189 giraffe_wcaldata_delete(wlsolution);
1199 giraffe_linedata_delete(wlsolution->linedata);
1200 wlsolution->linedata = NULL;
1202 cpl_frameset_insert(set, ldata_frame);
1210 if (slitgeometry == TRUE) {
1212 cpl_frame* slit_frame = NULL;
1216 GiTable* mask = NULL;
1217 GiTable* slit = NULL;
1220 cpl_msg_info(fctid,
"Calibrating slit geometry ...");
1225 filename = cpl_frame_get_filename(scal_frame);
1231 cpl_msg_error(fctid,
"Cannot load slit geometry mask from '%s'. "
1232 "Aborting ...", filename);
1238 giraffe_wcaldata_delete(wlsolution);
1240 giraffe_localization_destroy(localization);
1241 giraffe_extraction_destroy(extraction);
1254 wlsolution->coeffs, slitgeo, grating,
1258 cpl_msg_error(fctid,
"Slit geometry calibration failed! "
1266 giraffe_wcaldata_delete(wlsolution);
1268 giraffe_localization_destroy(localization);
1269 giraffe_extraction_destroy(extraction);
1287 slit_frame = giraffe_slitgeometry_save(slit);
1291 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
1298 giraffe_wcaldata_delete(wlsolution);
1300 giraffe_localization_destroy(localization);
1301 giraffe_extraction_destroy(extraction);
1310 cpl_frameset_insert(set, slit_frame);
1330 if (rebin == TRUE) {
1332 cpl_frame* rbin_frame = NULL;
1334 GiRebinConfig* rebin_config;
1337 cpl_msg_info(fctid,
"Recipe Step : Spectrum Rebinning");
1344 localization, grating, slitgeo,
1345 wlsolution->coeffs, rebin_config);
1348 cpl_msg_error(fctid,
"Rebinning of arc-lamp spectra failed! "
1351 giraffe_wcaldata_delete(wlsolution);
1354 giraffe_localization_destroy(localization);
1355 giraffe_extraction_destroy(extraction);
1378 GIFRAME_ARC_LAMP_RBNSPECTRA,
1379 CPL_FRAME_LEVEL_FINAL,
1382 if (rbin_frame == NULL) {
1384 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
1386 giraffe_wcaldata_delete(wlsolution);
1389 giraffe_localization_destroy(localization);
1390 giraffe_extraction_destroy(extraction);
1403 cpl_msg_error(fctid,
"Cannot attach fiber setup to local "
1404 "file '%s'! Aborting ...",
1405 cpl_frame_get_filename(rbin_frame));
1407 giraffe_wcaldata_delete(wlsolution);
1410 giraffe_localization_destroy(localization);
1411 giraffe_extraction_destroy(extraction);
1417 cpl_frame_delete(rbin_frame);
1423 cpl_frameset_insert(set, rbin_frame);
1430 GIFRAME_ARC_LAMP_RBNERRORS,
1431 CPL_FRAME_LEVEL_FINAL,
1434 if (rbin_frame == NULL) {
1436 cpl_msg_error(fctid,
"Cannot create local file! Aborting ...");
1438 giraffe_wcaldata_delete(wlsolution);
1441 giraffe_localization_destroy(localization);
1442 giraffe_extraction_destroy(extraction);
1456 cpl_msg_error(fctid,
"Cannot attach fiber setup to local "
1457 "file '%s'! Aborting ...",
1458 cpl_frame_get_filename(rbin_frame));
1460 giraffe_wcaldata_delete(wlsolution);
1463 giraffe_localization_destroy(localization);
1464 giraffe_extraction_destroy(extraction);
1470 cpl_frame_delete(rbin_frame);
1476 cpl_frameset_insert(set, rbin_frame);
1488 giraffe_localization_destroy(localization);
1489 localization = NULL;
1491 giraffe_extraction_destroy(extraction);
1494 if (rebinning != NULL) {
1505 giraffe_wcaldata_delete(wlsolution);
1514 giqcwavecalibration(cpl_frameset* set)
1517 const cxchar*
const fctid =
"giqcwavecalibration";
1525 cxint nsaturated = 0;
1528 const cxdouble rmsscale = 3.;
1529 const cxdouble saturation = 60000.;
1530 const cxdouble* pixels = NULL;
1532 cxdouble efficiency[2] = {0., 0.};
1533 cxdouble wlcenter = 0.;
1536 cxdouble pixel2nm = 1.;
1537 cxdouble fwhm_domain[2] = {0., 100.};
1538 cxdouble* _tdata = NULL;
1540 cpl_propertylist* properties = NULL;
1541 cpl_propertylist* qclog = NULL;
1543 cpl_frame* rframe = NULL;
1544 cpl_frame* pframe = NULL;
1546 const cpl_image* _rimage = NULL;
1547 const cpl_image* _pimage = NULL;
1549 cpl_image* _test = NULL;
1550 cpl_image* _test0 = NULL;
1551 cpl_image* _test1 = NULL;
1553 cpl_table* _ptable = NULL;
1555 GiImage* rimage = NULL;
1556 GiImage* pimage = NULL;
1558 GiTable* ptable = NULL;
1560 GiLineData* plines = NULL;
1568 cpl_msg_info(fctid,
"Computing QC1 parameters ...");
1577 CPL_FRAME_GROUP_PRODUCT);
1579 if (pframe == NULL) {
1581 cpl_msg_warning(fctid,
"Product '%s' not found.",
1582 GIFRAME_ARC_LAMP_EXTSPECTRA);
1584 cpl_msg_warning(fctid,
"Setting lamp efficiencies (%s, %s) to 0.",
1585 GIALIAS_QCLAMP, GIALIAS_QCLAMP_SIMCAL);
1597 cpl_msg_error(fctid,
"Could not load extracted spectra '%s'!",
1598 cpl_frame_get_filename(pframe));
1603 giraffe_paf_delete(qc);
1610 cx_assert(_pimage != NULL);
1618 cpl_msg_error(fctid,
"Could not load extracted spectra fiber setup!");
1626 giraffe_paf_delete(qc);
1633 cx_assert(_ptable != NULL);
1635 if (cpl_table_has_column(_ptable,
"RP") == FALSE) {
1637 cpl_msg_warning(fctid,
"Column 'RP' not found in fiber setup table!");
1638 cpl_msg_warning(fctid,
"Setting lamp efficiencies (%s, %s) to 0.",
1639 GIALIAS_QCLAMP, GIALIAS_QCLAMP_SIMCAL);
1648 cx_assert(properties != NULL);
1650 if (cpl_propertylist_has(properties, GIALIAS_EXPTIME) == FALSE) {
1652 cpl_msg_warning(fctid,
"Property '%s' not found in '%s'.",
1653 GIALIAS_EXPTIME, cpl_frame_get_filename(rframe));
1654 cpl_msg_warning(fctid,
"Setting lamp efficiencies (%s, %s) to 0.",
1655 GIALIAS_QCLAMP, GIALIAS_QCLAMP_SIMCAL);
1664 cxint nb = cpl_image_get_size_y(_pimage);
1665 cxint nf[2] = {0, 0};
1667 cxdouble exptime = cpl_propertylist_get_double(properties,
1669 cxdouble* _sum = NULL;
1671 cpl_image* sum = NULL;
1674 sum = cpl_image_collapse_create(_pimage, 0);
1675 _sum = cpl_image_get_data_double(sum);
1677 for (fiber = 0; fiber < cpl_table_get_nrow(_ptable); ++fiber) {
1679 cxint rp = cpl_table_get_int(_ptable,
"RP", fiber, NULL);
1682 efficiency[1] += _sum[fiber];
1686 efficiency[0] += _sum[fiber];
1694 cpl_image_delete(sum);
1698 cpl_msg_warning(fctid,
"No OzPoz fibers found in the "
1699 "current fiber setup.");
1700 cpl_msg_warning(fctid,
"Setting lamp efficiency (%s) to 0.",
1705 efficiency[0] /= nf[0] * nb * exptime;
1709 cpl_msg_warning(fctid,
"No simultaneous calibration fibers "
1710 "found in the current fiber setup.");
1711 cpl_msg_warning(fctid,
"Setting lamp efficiency (%s) to 0.",
1712 GIALIAS_QCLAMP_SIMCAL);
1716 efficiency[1] /= nf[1] * nb * exptime;
1739 rframe = cpl_frameset_find(set, GIFRAME_ARC_SPECTRUM);
1741 if (rframe == NULL) {
1742 cpl_msg_error(fctid,
"Missing raw frame (%s)", GIFRAME_ARC_SPECTRUM);
1744 giraffe_paf_delete(qc);
1750 cpl_msg_info(fctid,
"Processing reference frame '%s' (%s)",
1751 cpl_frame_get_filename(rframe), cpl_frame_get_tag(rframe));
1758 cpl_msg_error(fctid,
"Could not load arc-lamp spectra '%s'!",
1759 cpl_frame_get_filename(rframe));
1764 giraffe_paf_delete(qc);
1772 cx_assert(_rimage != NULL);
1781 cx_assert(properties != NULL);
1783 if (cpl_propertylist_has(properties, GIALIAS_OVSCX)) {
1785 cxint _ox = cpl_propertylist_get_int(properties, GIALIAS_OVSCX);
1786 cxint _nx = cpl_image_get_size_x(_rimage) - 2 * CX_MAX(0, _ox) - 1;
1788 w.x0 = CX_MAX(0, _ox) + 1;
1793 if (cpl_propertylist_has(properties, GIALIAS_OVSCY)) {
1795 cxint _oy = cpl_propertylist_get_int(properties, GIALIAS_OVSCY);
1796 cxint _ny = cpl_image_get_size_y(_rimage) - 2 * CX_MAX(0, _oy) - 1;
1798 w.y0 = CX_MAX(0, _oy) + 1;
1803 mean = cpl_image_get_mean_window(_rimage, w.x0, w.y0, w.x1, w.y1);
1805 pixels = cpl_image_get_data_const(_rimage);
1806 npixel = cpl_image_get_size_x(_rimage) * cpl_image_get_size_y(_rimage);
1808 for (i = 0; i < npixel; i++) {
1809 if (pixels[i] > saturation) {
1819 qc = giraffe_qclog_open(0);
1822 cpl_msg_error(fctid,
"Cannot create QC1 log!");
1830 qclog = giraffe_paf_get_properties(qc);
1831 cx_assert(qclog != NULL);
1834 CPL_FRAME_GROUP_PRODUCT);
1836 if (pframe == NULL) {
1837 cpl_msg_error(fctid,
"Missing product frame (%s)",
1838 GIFRAME_WAVELENGTH_SOLUTION);
1840 giraffe_paf_delete(qc);
1849 cpl_msg_info(fctid,
"Processing product frame '%s' (%s)",
1850 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
1857 cpl_msg_error(fctid,
"Could not load dispersion solution '%s'!",
1858 cpl_frame_get_filename(pframe));
1866 giraffe_paf_delete(qc);
1873 cx_assert(properties != NULL);
1888 cpl_propertylist_update_string(qclog,
"PRO.CATG",
1889 cpl_frame_get_tag(pframe));
1890 cpl_propertylist_set_comment(qclog,
"PRO.CATG",
1891 "Pipeline product category");
1894 cx_assert(properties != NULL);
1899 GIALIAS_WSOL_NLINES);
1901 GIALIAS_WSOL_NACCEPT);
1903 GIALIAS_WSOL_NREJECT);
1911 giraffe_qclog_close(qc);
1919 qc = giraffe_qclog_open(1);
1922 cpl_msg_error(fctid,
"Cannot create QC1 log!");
1926 qclog = giraffe_paf_get_properties(qc);
1927 cx_assert(qclog != NULL);
1930 CPL_FRAME_GROUP_PRODUCT);
1932 if (pframe == NULL) {
1933 cpl_msg_error(fctid,
"Missing product frame (%s)",
1934 GIFRAME_ARC_LAMP_RBNSPECTRA);
1939 giraffe_paf_delete(qc);
1945 cpl_msg_info(fctid,
"Processing product frame '%s' (%s)",
1946 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
1952 cpl_msg_error(fctid,
"Could not load rebinned arc-lamp spectra '%s'!",
1953 cpl_frame_get_filename(pframe));
1961 giraffe_paf_delete(qc);
1972 cpl_msg_error(fctid,
"Could not load rebinned arc-lamp spectra '%s'!",
1973 cpl_frame_get_filename(pframe));
1984 giraffe_paf_delete(qc);
1991 cx_assert(properties != NULL);
2004 cpl_propertylist_update_string(qclog,
"PRO.CATG",
2005 cpl_frame_get_tag(pframe));
2006 cpl_propertylist_set_comment(qclog,
"PRO.CATG",
2007 "Pipeline product category");
2010 cx_assert(properties != NULL);
2022 cpl_propertylist_update_double(properties, GIALIAS_QCMEAN, mean);
2023 cpl_propertylist_set_comment(properties, GIALIAS_QCMEAN,
"Mean level of "
2030 cpl_propertylist_update_int(properties, GIALIAS_QCNSAT, nsaturated);
2031 cpl_propertylist_set_comment(properties, GIALIAS_QCNSAT,
"Number of "
2032 "saturated pixels in the first raw frame");
2042 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP, efficiency[0]);
2043 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP,
2044 "Calibration lamp efficiency");
2049 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP_SIMCAL,
2051 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP_SIMCAL,
2052 "SIMCAL lamp efficiency");
2055 GIALIAS_QCLAMP_SIMCAL);
2062 _test = cpl_image_duplicate(_pimage);
2063 _tdata = cpl_image_get_data(_test);
2065 nx = cpl_image_get_size_x(_test);
2066 ny = cpl_image_get_size_y(_test);
2068 for (i = 0; i < nx * ny; i++) {
2069 _tdata[i] = _tdata[i] > 0. ? log(_tdata[i]) : 0.;
2074 _test0 = cpl_image_extract(_test, 4, 1, 4, ny);
2076 _test1 = cpl_image_collapse_create(_test, 1);
2077 cpl_image_divide_scalar(_test1, nx);
2079 cpl_image_delete(_test);
2083 _test = cpl_image_subtract_create(_test0, _test1);
2085 cpl_image_delete(_test0);
2088 cpl_image_delete(_test1);
2092 _tdata = cpl_image_get_data(_test);
2096 for (i = 0; i < ny; i++) {
2097 _tdata[i] = exp(_tdata[i]);
2098 rms += pow(_tdata[i], 2.);
2101 rms = sqrt(rms / (ny - 1));
2103 cpl_image_delete(_test);
2106 cpl_propertylist_update_double(properties, GIALIAS_QCRBRMS, rms);
2107 cpl_propertylist_set_comment(properties, GIALIAS_QCRBRMS,
2108 "RMS of rebinned arc-lamp spectra");
2117 cpl_msg_error(fctid,
"Could not save rebinned arc-lamp spectra "
2118 "'%s'!", cpl_frame_get_filename(pframe));
2129 giraffe_paf_delete(qc);
2139 cpl_msg_error(fctid,
"Could not save rebinned arc-lamp spectra "
2140 "'%s'!", cpl_frame_get_filename(pframe));
2151 giraffe_paf_delete(qc);
2163 giraffe_qclog_close(qc);
2171 qc = giraffe_qclog_open(2);
2174 cpl_msg_error(fctid,
"Cannot create QC1 log!");
2178 qclog = giraffe_paf_get_properties(qc);
2179 cx_assert(qclog != NULL);
2182 CPL_FRAME_GROUP_PRODUCT);
2184 if (pframe == NULL) {
2185 cpl_msg_error(fctid,
"Missing product frame (%s)",
2191 giraffe_paf_delete(qc);
2197 cpl_msg_info(fctid,
"Processing product frame '%s' (%s)",
2198 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
2205 plines = giraffe_linedata_new();
2207 status = giraffe_linedata_load(plines, cpl_frame_get_filename(pframe));
2210 cpl_msg_error(fctid,
"Could not load line data '%s'!",
2211 cpl_frame_get_filename(pframe));
2213 giraffe_linedata_delete(plines);
2219 giraffe_paf_delete(qc);
2226 cx_assert(properties != NULL);
2239 cpl_propertylist_update_string(qclog,
"PRO.CATG",
2240 cpl_frame_get_tag(pframe));
2241 cpl_propertylist_set_comment(qclog,
"PRO.CATG",
2242 "Pipeline product category");
2245 _pimage = giraffe_linedata_get_data(plines,
"FWHM");
2247 if (_pimage == NULL) {
2248 cpl_msg_error(fctid,
"FWHM line data not found!");
2250 giraffe_linedata_delete(plines);
2256 giraffe_paf_delete(qc);
2262 nx = cpl_image_get_size_x(_pimage);
2263 ny = cpl_image_get_size_y(_pimage);
2265 pixels = cpl_image_get_data_const(_pimage);
2267 for (j = 0; j < 2; ++j) {
2269 register cxint ndata = nx * ny;
2275 for (i = 0; i < ndata; ++i) {
2277 if ((pixels[i] >= fwhm_domain[0]) &&
2278 (pixels[i] < fwhm_domain[1])) {
2286 cpl_msg_error(fctid,
"All line FWHM data are invalid!");
2288 giraffe_linedata_delete(plines);
2294 giraffe_paf_delete(qc);
2302 for (i = 0; i < ndata; ++i) {
2304 if ((pixels[i] >= fwhm_domain[0]) &&
2305 (pixels[i] < fwhm_domain[1])) {
2306 rms += pow(pixels[i] - mean, 2.);
2312 rms = sqrt(rms / (npixel - 1));
2315 fwhm_domain[0] = CX_MAX(mean - rmsscale * rms, 0.);
2316 fwhm_domain[1] = CX_MIN(mean + rmsscale * rms, 100.);
2321 properties = cpl_propertylist_load_regexp(cpl_frame_get_filename(pframe),
2322 0,
"^COMMENT$", TRUE);
2324 cx_assert(properties != NULL);
2326 if (cpl_propertylist_has(properties, GIALIAS_GRATWLEN) == FALSE) {
2327 cpl_msg_error(fctid,
"Grating central wavelength property '%s' not "
2328 "found!", GIALIAS_GRATWLEN);
2330 cpl_propertylist_delete(properties);
2333 giraffe_linedata_delete(plines);
2339 giraffe_paf_delete(qc);
2345 if (cpl_propertylist_has(properties, GIALIAS_WSOL_SCALE) == FALSE) {
2346 cpl_msg_error(fctid,
"Line data property '%s' not found!",
2347 GIALIAS_WSOL_SCALE);
2349 cpl_propertylist_delete(properties);
2352 giraffe_linedata_delete(plines);
2358 giraffe_paf_delete(qc);
2364 wlcenter = cpl_propertylist_get_double(properties, GIALIAS_GRATWLEN);
2365 pixel2nm = cpl_propertylist_get_double(properties, GIALIAS_WSOL_SCALE);
2370 cpl_propertylist_update_double(properties, GIALIAS_QCRESOLAVG, mean);
2371 cpl_propertylist_set_comment(properties, GIALIAS_QCRESOLAVG,
2372 "Average line FWHM [nm]");
2374 cpl_propertylist_update_double(properties, GIALIAS_QCRESOLRMS, rms);
2375 cpl_propertylist_set_comment(properties, GIALIAS_QCRESOLRMS,
2376 "RMS of line FWHM [nm]");
2378 cpl_propertylist_update_int(properties, GIALIAS_QCRESOLTOT, nx * ny);
2379 cpl_propertylist_set_comment(properties, GIALIAS_QCRESOLTOT,
2380 "Total number of lines available for FWHM RMS "
2383 cpl_propertylist_update_int(properties, GIALIAS_QCRESOLLIN, npixel);
2384 cpl_propertylist_set_comment(properties, GIALIAS_QCRESOLLIN,
2385 "Number of lines used for FWHM RMS "
2388 cpl_propertylist_update_double(properties, GIALIAS_QCRESOLPWR,
2390 cpl_propertylist_set_comment(properties, GIALIAS_QCRESOLPWR,
2394 GIALIAS_QCRESOLAVG);
2396 GIALIAS_QCRESOLRMS);
2398 GIALIAS_QCRESOLTOT);
2400 GIALIAS_QCRESOLLIN);
2402 GIALIAS_QCRESOLPWR);
2405 status = giraffe_linedata_save(plines, properties,
2406 cpl_frame_get_filename(pframe));
2409 cpl_msg_error(fctid,
"Could not save line data "
2410 "'%s'!", cpl_frame_get_filename(pframe));
2412 cpl_propertylist_delete(properties);
2415 giraffe_linedata_delete(plines);
2421 giraffe_paf_delete(qc);
2427 cpl_propertylist_delete(properties);
2430 giraffe_qclog_close(qc);
2451 cpl_plugin_get_info(cpl_pluginlist* list)
2454 cpl_recipe* recipe = cx_calloc(1,
sizeof *recipe);
2455 cpl_plugin* plugin = &recipe->interface;
2457 cpl_plugin_init(plugin,
2459 GIRAFFE_BINARY_VERSION,
2460 CPL_PLUGIN_TYPE_RECIPE,
2461 "giwavecalibration",
2462 "Compute dispersion solution from an arc-lamp spectrum.",
2463 "For detailed information please refer to the "
2464 "GIRAFFE pipeline user manual.\nIt is available at "
2465 "http://www.eso.org/pipelines.",
2469 giwavecalibration_create,
2470 giwavecalibration_exec,
2471 giwavecalibration_destroy);
2473 cpl_pluginlist_append(list, plugin);
GiBiasConfig * giraffe_bias_config_create(cpl_parameterlist *list)
Creates a setup structure for a bias removal task.
void giraffe_bias_config_add(cpl_parameterlist *list)
Adds parameters for the bias removal.
cxint giraffe_bias_remove(GiImage *result, const GiImage *raw, const GiImage *master_bias, const GiImage *bad_pixels, const cpl_matrix *biaslimits, const GiBiasConfig *config)
Removes the bias from an image.
void giraffe_bias_config_destroy(GiBiasConfig *config)
Destroys a bias removal setup structure.
GiTable * giraffe_fibers_setup(const cpl_frame *frame, const cpl_frame *reference)
Setup a fiber list.
cxint giraffe_fiberlist_compare(const GiTable *fibers, const GiTable *reference)
Compare two fiber lists.
cxint giraffe_fiberlist_attach(cpl_frame *frame, GiTable *fibers)
Attach a fiber table to a frame.
cpl_frame * giraffe_frame_create_table(GiTable *table, const cxchar *tag, cpl_frame_level level, cxbool save, cxbool update)
Create a table product frame.
cpl_frame * giraffe_frame_create_image(GiImage *image, const cxchar *tag, cpl_frame_level level, cxbool save, cxbool update)
Create an image product frame.
cpl_frame * giraffe_get_slitgeometry(const cpl_frameset *set)
Get the slit geometry frame from a frame set.
cpl_frame * giraffe_get_frame(const cpl_frameset *set, const cxchar *tag, cpl_frame_group group)
Get a frame from a frame set.
cpl_frame * giraffe_frame_create(const cxchar *tag, cpl_frame_level level, const cpl_propertylist *properties, cxcptr object, cxcptr data, GiFrameCreator creator)
Create a product frame using a provided frame creator.
void giraffe_image_delete(GiImage *self)
Destroys an image.
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
cxint giraffe_image_add_info(GiImage *image, const GiRecipeInfo *info, const cpl_frameset *set)
Add additional frame information to an image.
cxint giraffe_image_save(GiImage *self, const cxchar *filename)
Write a Giraffe image to a file.
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
GiImage * giraffe_image_new(cpl_type type)
Creates an empty image container.
cxint giraffe_image_load(GiImage *self, const cxchar *filename, cxint position)
Gets image data and properties from a file.
void giraffe_rebin_config_destroy(GiRebinConfig *config)
Destroys a spectrum extraction setup structure.
cxint giraffe_rebin_spectra(GiRebinning *rebinning, const GiExtraction *extraction, const GiTable *fibers, const GiLocalization *localization, const GiTable *grating, const GiTable *slitgeo, const GiTable *solution, const GiRebinConfig *config)
Rebin an Extracted Spectra Frame and associated Errors Frame.
void giraffe_rebinning_destroy(GiRebinning *rebinning)
Destroys a rebinning results container and its contents.
GiRebinning * giraffe_rebinning_new(void)
Create an empty rebinning results container.
GiRebinConfig * giraffe_rebin_config_create(cpl_parameterlist *list)
Creates a setup structure for the rebinning.
void giraffe_rebin_config_add(cpl_parameterlist *list)
Adds parameters for the rebinning.
GiSGCalConfig * giraffe_sgcalibration_config_create(cpl_parameterlist *list)
Creates a setup structure for the slit geometry calibration.
void giraffe_sgcalibration_config_destroy(GiSGCalConfig *config)
Destroys a sgcalibration field setup structure.
void giraffe_sgcalibration_config_add(cpl_parameterlist *list)
Adds parameters for the sgcalibration correction computation.
cxint giraffe_calibrate_slit(GiTable *result, const GiExtraction *extraction, const GiLocalization *localization, const GiTable *fibers, const GiTable *wlsolution, const GiTable *slitgeometry, const GiTable *grating, const GiTable *mask, const GiSGCalConfig *config)
Compute a slit geometry corresponding to the given rebinned spectrum.
GiTable * giraffe_slitgeometry_load(const GiTable *fibers, const cxchar *filename, cxint pos, const cxchar *tag)
Load the slit geometry information for a given fiber setup.
cxint giraffe_table_load(GiTable *self, const cxchar *filename, cxint position, const cxchar *id)
Reads a data set from a file into a Giraffe table.
cxint giraffe_table_attach(GiTable *self, const cxchar *filename, cxint position, const cxchar *id)
Attach a Giraffe table to a file.
cpl_propertylist * giraffe_table_get_properties(const GiTable *self)
Gets the table properties.
GiTable * giraffe_table_new(void)
Creates a new, empty Giraffe table.
void giraffe_table_delete(GiTable *self)
Destroys a Giraffe table.
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
cxint giraffe_table_add_info(GiTable *table, const GiRecipeInfo *info, const cpl_frameset *set)
Add additional frame information to a table.
cxint giraffe_propertylist_copy(cpl_propertylist *self, const cxchar *name, const cpl_propertylist *other, const cxchar *othername)
Copy a property from one list to another.
const cxchar * giraffe_get_license(void)
Get the pipeline copyright and license.
void giraffe_wlcalibration_config_add(cpl_parameterlist *list)
Adds parameters for the wavelength calibration.
cxint giraffe_calibrate_wavelength(GiWCalData *result, GiExtraction *extraction, GiLocalization *localization, GiTable *fibers, GiTable *slitgeometry, GiTable *grating, GiTable *lines, GiTable *initial, GiWCalConfig *config)
Compute the wavelength solution for the given extracted arc-lamp spectra.
GiWCalConfig * giraffe_wlcalibration_config_create(cpl_parameterlist *list)
Creates a setup structure for the wavelength calibration.
void giraffe_wlcalibration_config_destroy(GiWCalConfig *config)
Destroys a wavelength calibration setup structure.
Slit geometry calibration configuration data structure.
Wavelength calibration configuration data structure.