27 #include <cxmessages.h>
29 #include <cpl_recipe.h>
30 #include <cpl_plugininfo.h>
31 #include <cpl_parameterlist.h>
32 #include <cpl_frameset.h>
34 #include <cpl_errorstate.h>
36 #include <irplib_sdp_spectrum.h>
41 #include "gifiberutils.h"
42 #include "gislitgeometry.h"
43 #include "gipsfdata.h"
46 #include "giextract.h"
48 #include "gitransmission.h"
49 #include "girebinning.h"
50 #include "gisgcalibration.h"
51 #include "giastrometry.h"
53 #include "gimessages.h"
58 static cxint giscience(cpl_parameterlist*, cpl_frameset*);
60 static cxint _giraffe_make_sdp_spectra(
const cxchar* flux_filename,
61 const cxchar* err_filename,
63 cpl_frameset* allframes,
64 const cpl_parameterlist* parlist,
65 const cxchar* recipe_id);
74 giscience_create(cpl_plugin* plugin)
77 cpl_recipe* recipe = (cpl_recipe*)plugin;
79 cpl_parameter* p = NULL;
91 recipe->parameters = cpl_parameterlist_new();
92 cx_assert(recipe->parameters != NULL);
121 p = cpl_parameter_new_value(
"giraffe.siwc.apply",
123 "Enable simultaneous wavelength calibration "
128 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"siwc-apply");
129 cpl_parameterlist_append(recipe->parameters, p);
139 p = cpl_parameter_new_value(
"giraffe.sdp.format.generate",
141 "TRUE if additional files should be generated"
142 " in Science Data Product (SDP) format.",
146 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"generate-SDP-format");
147 cpl_parameterlist_append(recipe->parameters, p);
149 p = cpl_parameter_new_value(
"giraffe.sdp.nassoc.keys",
151 "Sets the number of dummy (empty) ASSONi,"
152 " ASSOCi and ASSOMi keywords to create.",
156 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
157 "dummy-association-keys");
158 cpl_parameterlist_append(recipe->parameters, p);
170 giscience_exec(cpl_plugin* plugin)
173 cpl_errorstate prev_state;
175 cpl_recipe* recipe = (cpl_recipe*)plugin;
178 cx_assert(recipe->parameters != NULL);
179 cx_assert(recipe->frames != NULL);
181 prev_state = cpl_errorstate_get();
182 result = giscience(recipe->parameters, recipe->frames);
184 cpl_errorstate_dump(prev_state, CPL_FALSE, cpl_errorstate_dump_one);
191 giscience_destroy(cpl_plugin* plugin)
194 cpl_recipe* recipe = (cpl_recipe*)plugin;
203 cpl_parameterlist_delete(recipe->parameters);
205 giraffe_error_clear();
217 giscience(cpl_parameterlist* config, cpl_frameset* set)
220 const cxchar*
const _id =
"giscience";
223 const cxchar* filename = NULL;
226 cxbool calsim = FALSE;
228 cxbool gensdp = FALSE;
229 cxint nassoc_keys = 0;
230 cxchar* flux_filename = NULL;
231 cxchar* err_filename = NULL;
238 cxdouble exptime = 0.;
240 cx_slist* slist = NULL;
242 cpl_propertylist* properties = NULL;
244 cpl_matrix* biasareas = NULL;
246 cpl_frame* science_frame = NULL;
247 cpl_frame* mbias_frame = NULL;
248 cpl_frame* mdark_frame = NULL;
249 cpl_frame* bpixel_frame = NULL;
250 cpl_frame* slight_frame = NULL;
251 cpl_frame* locy_frame = NULL;
252 cpl_frame* locw_frame = NULL;
253 cpl_frame* psfdata_frame = NULL;
254 cpl_frame* grating_frame = NULL;
255 cpl_frame* linemask_frame = NULL;
256 cpl_frame* slit_frame = NULL;
257 cpl_frame* wcal_frame = NULL;
258 cpl_frame* rscience_frame = NULL;
259 cpl_frame* sext_frame = NULL;
260 cpl_frame* rbin_frame = NULL;
262 cpl_parameter* p = NULL;
264 GiImage* mbias = NULL;
265 GiImage* mdark = NULL;
266 GiImage* bpixel = NULL;
267 GiImage* slight = NULL;
268 GiImage* sscience = NULL;
269 GiImage* rscience = NULL;
271 GiTable* fibers = NULL;
272 GiTable* slitgeometry = NULL;
273 GiTable* grating = NULL;
274 GiTable* wcalcoeff = NULL;
276 GiLocalization* localization = NULL;
277 GiExtraction* extraction = NULL;
278 GiRebinning* rebinning = NULL;
280 GiBiasConfig* bias_config = NULL;
281 GiExtractConfig* extract_config = NULL;
282 GiFlatConfig* flat_config = NULL;
283 GiRebinConfig* rebin_config = NULL;
285 GiInstrumentMode mode;
287 GiRecipeInfo info = {(cxchar*)_id, 1, NULL, config};
289 GiGroupInfo groups[] = {
290 {GIFRAME_SCIENCE, CPL_FRAME_GROUP_RAW},
291 {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
292 {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
293 {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
294 {GIFRAME_FIBER_FLAT_EXTSPECTRA, CPL_FRAME_GROUP_CALIB},
295 {GIFRAME_FIBER_FLAT_EXTERRORS, CPL_FRAME_GROUP_CALIB},
296 {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
297 {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
298 {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
299 {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
300 {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
301 {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
302 {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
303 {GIFRAME_LINE_MASK, CPL_FRAME_GROUP_CALIB},
304 {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
305 {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
306 {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
307 {NULL, CPL_FRAME_GROUP_NONE}
313 cpl_msg_error(_id,
"Invalid parameter list! Aborting ...");
318 cpl_msg_error(_id,
"Invalid frame set! Aborting ...");
322 status = giraffe_frameset_set_groups(set, groups);
325 cpl_msg_error(_id,
"Setting frame group information failed!");
334 nscience = cpl_frameset_count_tags(set, GIFRAME_SCIENCE);
337 cpl_msg_error(_id,
"Too few (%ld) raw frames (%s) present in "
338 "frame set! Aborting ...", nscience, GIFRAME_SCIENCE);
342 locy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
344 if (locy_frame == NULL) {
346 locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
348 if (locy_frame == NULL) {
349 cpl_msg_info(_id,
"No master localization (centroid position) "
350 "present in frame set. Aborting ...");
356 locw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
358 if (locw_frame == NULL) {
360 locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
362 if (locw_frame == NULL) {
363 cpl_msg_info(_id,
"No master localization (spectrum width) "
364 "present in frame set. Aborting ...");
370 grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
372 if (!grating_frame) {
373 cpl_msg_error(_id,
"No grating data present in frame set. "
381 cpl_msg_error(_id,
"No slit geometry present in frame set. "
386 wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
389 cpl_msg_error(_id,
"No dispersion solution present in frame set. "
394 linemask_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
396 if (!linemask_frame) {
397 cpl_msg_warning(_id,
"No reference line mask present in frame set.");
400 bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
403 cpl_msg_info(_id,
"No bad pixel map present in frame set.");
406 mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
409 cpl_msg_info(_id,
"No master bias present in frame set.");
412 mdark_frame = cpl_frameset_find(set, GIFRAME_DARK_MASTER);
415 cpl_msg_info(_id,
"No master dark present in frame set.");
418 slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
421 cpl_msg_info(_id,
"No scattered light model present in frame set.");
424 psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
426 if (!psfdata_frame) {
427 cpl_msg_info(_id,
"No PSF profile parameters present in frame set.");
435 slist = cx_slist_new();
437 science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
439 for (i = 0; i < nscience; i++) {
441 filename = cpl_frame_get_filename(science_frame);
449 cpl_msg_error(_id,
"Cannot load raw science frame from '%s'. "
450 "Aborting ...", filename);
457 cx_slist_push_back(slist, raw);
459 science_frame = cpl_frameset_find(set, NULL);
463 nscience = (cxint)cx_slist_size(slist);
464 sscience = cx_slist_pop_front(slist);
467 cx_assert(properties != NULL);
476 cpl_msg_info(_id,
"Averaging science frames ...");
478 exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
480 for (i = 1; i < nscience; i++) {
482 cpl_propertylist* _properties;
484 GiImage* science = cx_slist_pop_front(slist);
491 cx_assert(_properties != NULL);
493 exptime += cpl_propertylist_get_double(_properties, GIALIAS_EXPTIME);
502 cx_assert(cx_slist_empty(slist));
503 cx_slist_delete(slist);
513 cpl_msg_info(_id,
"Updating stacked science image properties ...");
515 cpl_propertylist_set_double(properties, GIALIAS_EXPTIME,
518 cpl_propertylist_append_double(properties, GIALIAS_EXPTTOT, exptime);
519 cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
520 "Total exposure time of all frames "
523 cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
527 cpl_propertylist_append_int(properties, GIALIAS_DATANCOM, nscience);
528 cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM,
529 "Number of combined frames");
543 if (bias_config->method == GIBIAS_METHOD_MASTER ||
544 bias_config->method == GIBIAS_METHOD_ZMASTER) {
547 cpl_msg_error(_id,
"Missing master bias frame! Selected bias "
548 "removal method requires a master bias frame!");
556 filename = cpl_frame_get_filename(mbias_frame);
563 cpl_msg_error(_id,
"Cannot load master bias from '%s'. "
564 "Aborting ...", filename);
581 filename = cpl_frame_get_filename(bpixel_frame);
588 cpl_msg_error(_id,
"Cannot load bad pixel map from '%s'. "
589 "Aborting ...", filename);
630 cpl_msg_error(_id,
"Bias removal failed. Aborting ...");
635 if (bpixel != NULL) {
651 GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
654 cpl_msg_info(_id,
"Correcting for dark current ...");
656 filename = cpl_frame_get_filename(mdark_frame);
662 cpl_msg_error(_id,
"Cannot load master dark from '%s'. "
663 "Aborting ...", filename);
668 if (bpixel != NULL) {
680 cpl_msg_error(_id,
"Dark subtraction failed! Aborting ...");
688 if (bpixel != NULL) {
707 cpl_msg_info(_id,
"Writing pre-processed science image ...");
712 GIFRAME_SCIENCE_REDUCED,
713 CPL_FRAME_LEVEL_INTERMEDIATE,
716 if (rscience_frame == NULL) {
717 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
724 cpl_frameset_insert(set, rscience_frame);
731 science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
733 cpl_msg_info(_id,
"Building fiber setup for frame '%s'.",
734 cpl_frame_get_filename(science_frame));
739 cpl_msg_error(_id,
"Cannot create fiber setup for frame '%s'! "
740 "Aborting ...", cpl_frame_get_filename(science_frame));
753 cpl_msg_info(_id,
"Fiber reference setup taken from localization "
754 "frame '%s'.", cpl_frame_get_filename(locy_frame));
761 localization = giraffe_localization_new();
763 filename = cpl_frame_get_filename(locy_frame);
770 cpl_msg_error(_id,
"Cannot load localization (centroid "
771 "position) frame from '%s'. Aborting ...",
774 giraffe_localization_destroy(localization);
788 filename = cpl_frame_get_filename(locw_frame);
795 cpl_msg_error(_id,
"Cannot load localization (spectrum width) "
796 "frame from '%s'. Aborting ...", filename);
798 giraffe_localization_destroy(localization);
818 filename = cpl_frame_get_filename(slight_frame);
825 cpl_msg_error(_id,
"Cannot load scattered light model from '%s'. "
826 "Aborting ...", filename);
830 giraffe_localization_destroy(localization);
849 if ((extract_config->emethod == GIEXTRACT_OPTIMAL) ||
850 (extract_config->emethod == GIEXTRACT_HORNE)) {
852 if (psfdata_frame == NULL) {
854 const cxchar* emethod =
"Optimal";
856 if (extract_config->emethod == GIEXTRACT_HORNE) {
860 cpl_msg_error(_id,
"%s spectrum extraction requires PSF "
861 "profile data. Aborting ...", emethod);
864 extract_config = NULL;
866 if (slight != NULL) {
871 giraffe_localization_destroy(localization);
890 filename = cpl_frame_get_filename(psfdata_frame);
893 localization->psf = giraffe_psfdata_new();
894 status = giraffe_psfdata_load(localization->psf, filename);
897 cpl_msg_error(_id,
"Cannot load PSF profile data frame from "
898 "'%s'. Aborting ...", filename);
901 extract_config = NULL;
903 if (slight != NULL) {
908 giraffe_localization_destroy(localization);
931 extraction = giraffe_extraction_new();
934 localization, bpixel, slight,
938 cpl_msg_error(_id,
"Spectrum extraction failed! Aborting ...");
940 giraffe_extraction_destroy(extraction);
945 giraffe_localization_destroy(localization);
978 if (flat_config->load == TRUE) {
980 cpl_frame* flat_frame = NULL;
982 GiImage* flat = NULL;
985 flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTSPECTRA);
987 if (flat_frame == NULL) {
988 cpl_msg_error(_id,
"Missing flat field spectra frame!");
992 giraffe_extraction_destroy(extraction);
993 giraffe_localization_destroy(localization);
1003 filename = cpl_frame_get_filename(flat_frame);
1009 cpl_msg_error(_id,
"Cannot load flat field spectra from '%s'. "
1010 "Aborting ...", filename);
1016 giraffe_extraction_destroy(extraction);
1017 giraffe_localization_destroy(localization);
1027 if (flat_config->apply == TRUE) {
1029 GiImage* errors = NULL;
1032 flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTERRORS);
1034 if (flat_frame == NULL) {
1035 cpl_msg_warning(_id,
"Missing flat field spectra errors "
1040 filename = cpl_frame_get_filename(flat_frame);
1046 cpl_msg_error(_id,
"Cannot load flat field spectra "
1047 "errors from '%s'. Aborting ...",
1055 giraffe_extraction_destroy(extraction);
1056 giraffe_localization_destroy(localization);
1068 cpl_msg_info(_id,
"Applying flat field correction ...");
1074 cpl_msg_error(_id,
"Flat field correction failed! "
1082 giraffe_extraction_destroy(extraction);
1083 giraffe_localization_destroy(localization);
1098 if (flat_config->transmission == TRUE) {
1100 const cxchar* _filename = cpl_frame_get_filename(flat_frame);
1102 GiTable* _fibers = NULL;
1105 cpl_msg_info(_id,
"Loading fiber setup for frame '%s'.",
1111 cpl_msg_error(_id,
"Cannot create fiber setup for "
1112 "frame '%s'! Aborting ...", _filename);
1118 giraffe_extraction_destroy(extraction);
1119 giraffe_localization_destroy(localization);
1129 cpl_msg_info(_id,
"Applying relative fiber transmission "
1132 status = giraffe_transmission_setup(fibers, _fibers);
1136 status = giraffe_transmission_apply(extraction, fibers);
1141 cpl_msg_error(_id,
"Relative transmission correction failed! "
1148 giraffe_extraction_destroy(extraction);
1149 giraffe_localization_destroy(localization);
1174 cpl_msg_info(_id,
"Writing extracted spectra ...");
1181 GIFRAME_SCIENCE_EXTSPECTRA,
1182 CPL_FRAME_LEVEL_FINAL,
1185 if (sext_frame == NULL) {
1186 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1188 giraffe_extraction_destroy(extraction);
1189 giraffe_localization_destroy(localization);
1202 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1203 "Aborting ...", cpl_frame_get_filename(sext_frame));
1205 cpl_frame_delete(sext_frame);
1207 giraffe_extraction_destroy(extraction);
1208 giraffe_localization_destroy(localization);
1218 cpl_frameset_insert(set, sext_frame);
1225 GIFRAME_SCIENCE_EXTERRORS,
1226 CPL_FRAME_LEVEL_FINAL,
1229 if (sext_frame == NULL) {
1230 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1232 giraffe_extraction_destroy(extraction);
1233 giraffe_localization_destroy(localization);
1246 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1247 "Aborting ...", cpl_frame_get_filename(sext_frame));
1249 cpl_frame_delete(sext_frame);
1251 giraffe_extraction_destroy(extraction);
1252 giraffe_localization_destroy(localization);
1262 cpl_frameset_insert(set, sext_frame);
1266 if (extraction->npixels != NULL) {
1271 GIFRAME_SCIENCE_EXTPIXELS,
1272 CPL_FRAME_LEVEL_FINAL,
1275 if (sext_frame == NULL) {
1276 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1278 giraffe_extraction_destroy(extraction);
1279 giraffe_localization_destroy(localization);
1292 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1293 "Aborting ...", cpl_frame_get_filename(sext_frame));
1295 cpl_frame_delete(sext_frame);
1297 giraffe_extraction_destroy(extraction);
1298 giraffe_localization_destroy(localization);
1308 cpl_frameset_insert(set, sext_frame);
1317 GIFRAME_SCIENCE_EXTTRACE,
1318 CPL_FRAME_LEVEL_FINAL,
1321 if (sext_frame == NULL) {
1322 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1324 giraffe_extraction_destroy(extraction);
1325 giraffe_localization_destroy(localization);
1338 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1339 "Aborting ...", cpl_frame_get_filename(sext_frame));
1341 cpl_frame_delete(sext_frame);
1343 giraffe_extraction_destroy(extraction);
1344 giraffe_localization_destroy(localization);
1354 cpl_frameset_insert(set, sext_frame);
1358 if (extraction->model != NULL) {
1363 GIFRAME_SCIENCE_EXTMODEL,
1364 CPL_FRAME_LEVEL_FINAL,
1367 if (sext_frame == NULL) {
1368 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1370 giraffe_extraction_destroy(extraction);
1371 giraffe_localization_destroy(localization);
1384 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1385 "Aborting ...", cpl_frame_get_filename(sext_frame));
1387 cpl_frame_delete(sext_frame);
1389 giraffe_extraction_destroy(extraction);
1390 giraffe_localization_destroy(localization);
1400 cpl_frameset_insert(set, sext_frame);
1410 filename = (cxchar *)cpl_frame_get_filename(wcal_frame);
1416 cpl_msg_error(_id,
"Cannot load dispersion solution from "
1417 "'%s'. Aborting ...", filename);
1419 giraffe_extraction_destroy(extraction);
1420 giraffe_localization_destroy(localization);
1435 filename = (cxchar *)cpl_frame_get_filename(grating_frame);
1443 cpl_msg_error(_id,
"Cannot load grating data from '%s'. "
1444 "Aborting ...", filename);
1446 giraffe_extraction_destroy(extraction);
1447 giraffe_localization_destroy(localization);
1463 filename = (cxchar *)cpl_frame_get_filename(slit_frame);
1467 if (slitgeometry == NULL) {
1468 cpl_msg_error(_id,
"Cannot load slit geometry data from '%s'. "
1469 "Aborting ...", filename);
1473 giraffe_extraction_destroy(extraction);
1474 giraffe_localization_destroy(localization);
1492 cpl_msg_error(_id,
"Slit geometry data from '%s' is not "
1493 "applicable for current fiber setup! "
1494 "Aborting ...", filename);
1499 giraffe_extraction_destroy(extraction);
1500 giraffe_localization_destroy(localization);
1518 cpl_msg_info(_id,
"Spectrum rebinning");
1525 localization, grating, slitgeometry,
1526 wcalcoeff, rebin_config);
1529 cpl_msg_error(_id,
"Rebinning of science spectra failed! Aborting...");
1533 giraffe_extraction_destroy(extraction);
1534 giraffe_localization_destroy(localization);
1555 p = cpl_parameterlist_find(config,
"giraffe.siwc.apply");
1556 cx_assert(p != NULL);
1558 siwc = cpl_parameter_get_bool(p);
1562 cx_assert(properties != NULL);
1565 if (cpl_propertylist_has(properties, GIALIAS_STSCTAL) == TRUE) {
1566 calsim = cpl_propertylist_get_bool(properties, GIALIAS_STSCTAL);
1570 if ((siwc == TRUE) && (calsim == TRUE) && (linemask_frame != NULL)) {
1579 if (siwc_config == NULL) {
1586 giraffe_extraction_destroy(extraction);
1587 giraffe_localization_destroy(localization);
1601 filename = cpl_frame_get_filename(linemask_frame);
1606 cpl_msg_error(_id,
"Cannot load line reference mask from '%s'. "
1607 "Aborting ...", filename);
1617 giraffe_extraction_destroy(extraction);
1618 giraffe_localization_destroy(localization);
1634 linemask, siwc_config);
1637 cpl_msg_error(_id,
"Applying simultaneous wavelength "
1638 "calibration correction failed! Aborting...");
1648 giraffe_extraction_destroy(extraction);
1649 giraffe_localization_destroy(localization);
1673 localization, grating, slitgeometry,
1674 wcalcoeff, rebin_config);
1677 cpl_msg_error(_id,
"Rebinning of science spectra failed! "
1682 giraffe_extraction_destroy(extraction);
1683 giraffe_localization_destroy(localization);
1699 giraffe_extraction_destroy(extraction);
1702 giraffe_localization_destroy(localization);
1703 localization = NULL;
1706 rebin_config = NULL;
1723 cpl_msg_warning(_id,
"Missing observation time properties! "
1724 "Barycentric correction computation "
1731 cpl_msg_warning(_id,
"Missing telescope location properties! "
1732 "Barycentric correction computation "
1739 cpl_msg_warning(_id,
"Object positions are not available "
1740 "Barycentric correction computation "
1747 cpl_msg_error(_id,
"Barycentric correction computation "
1748 "failed! Aborting...");
1774 GIFRAME_SCIENCE_RBNSPECTRA,
1775 CPL_FRAME_LEVEL_FINAL,
1778 if (rbin_frame == NULL) {
1779 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1795 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
1796 "file '%s'! Aborting ...",
1797 cpl_frame_get_filename(rbin_frame));
1806 cpl_frame_delete(rbin_frame);
1811 cpl_frameset_insert(set, rbin_frame);
1812 flux_filename = cpl_strdup(cpl_frame_get_filename(rbin_frame));
1819 GIFRAME_SCIENCE_RBNERRORS,
1820 CPL_FRAME_LEVEL_FINAL,
1823 if (rbin_frame == NULL) {
1824 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1833 cpl_free(flux_filename);
1841 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
1842 "file '%s'! Aborting ...",
1843 cpl_frame_get_filename(rbin_frame));
1853 cpl_frame_delete(rbin_frame);
1854 cpl_free(flux_filename);
1859 cpl_frameset_insert(set, rbin_frame);
1860 err_filename = cpl_strdup(cpl_frame_get_filename(rbin_frame));
1868 p = cpl_parameterlist_find(config,
"giraffe.sdp.format.generate");
1869 cx_assert(p != NULL);
1870 gensdp = cpl_parameter_get_bool(p);
1871 p = cpl_parameterlist_find(config,
"giraffe.sdp.nassoc.keys");
1872 cx_assert(p != NULL);
1873 nassoc_keys = cpl_parameter_get_int(p);
1876 if (mode == GIMODE_MEDUSA) {
1877 status = _giraffe_make_sdp_spectra(flux_filename, err_filename,
1878 nassoc_keys, set, config, _id);
1880 cpl_msg_error(_id,
"Failed to generate spectra in Science Data"
1881 " Product format.");
1891 cpl_free(flux_filename);
1892 cpl_free(err_filename);
1897 cpl_msg_warning(_id,
"Requested to generate SDP 1D spectra, but"
1898 " this is currently only supported for the MEDUSA"
1899 " mode. Skipping SDP generation.");
1902 cpl_free(flux_filename);
1903 cpl_free(err_filename);
1910 if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
1912 cpl_frame* rimg_frame = NULL;
1914 GiFieldOfView* fov = NULL;
1916 GiFieldOfViewConfig* fov_config = NULL;
1918 GiFieldOfViewCubeFormat cube_format = GIFOV_FORMAT_ESO3D;
1923 cube_format = fov_config->format;
1926 cpl_msg_info(_id,
"Reconstructing image and data cube from rebinned "
1932 slitgeometry, fov_config);
1937 cpl_msg_warning(_id,
"No reconstructed image was built. "
1938 "Fiber list has no fiber position "
1942 cpl_msg_error(_id,
"Image reconstruction failed! Aborting...");
1972 GIFRAME_SCIENCE_RCSPECTRA,
1973 CPL_FRAME_LEVEL_FINAL,
1976 if (rimg_frame == NULL) {
1977 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1991 cpl_frameset_insert(set, rimg_frame);
1999 GIFRAME_SCIENCE_RCERRORS,
2000 CPL_FRAME_LEVEL_FINAL,
2003 if (rimg_frame == NULL) {
2004 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2018 cpl_frameset_insert(set, rimg_frame);
2023 if (cube_format == GIFOV_FORMAT_SINGLE) {
2027 if (fov->cubes.spectra != NULL) {
2029 cxint component = 0;
2035 properties = cpl_propertylist_duplicate(properties);
2040 CPL_FRAME_LEVEL_FINAL,
2046 cpl_propertylist_delete(properties);
2049 if (rimg_frame == NULL) {
2050 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2062 slitgeometry = NULL;
2076 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
2077 "file '%s'! Aborting ...",
2078 cpl_frame_get_filename(rimg_frame));
2080 cpl_frame_delete(rimg_frame);
2092 slitgeometry = NULL;
2103 cpl_frameset_insert(set, rimg_frame);
2109 if (fov->cubes.errors != NULL) {
2111 cxint component = 1;
2117 properties = cpl_propertylist_duplicate(properties);
2122 CPL_FRAME_LEVEL_FINAL,
2128 cpl_propertylist_delete(properties);
2131 if (rimg_frame == NULL) {
2132 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2144 slitgeometry = NULL;
2158 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
2159 "file '%s'! Aborting ...",
2160 cpl_frame_get_filename(rimg_frame));
2162 cpl_frame_delete(rimg_frame);
2174 slitgeometry = NULL;
2185 cpl_frameset_insert(set, rimg_frame);
2196 properties = cpl_propertylist_duplicate(properties);
2201 CPL_FRAME_LEVEL_FINAL,
2207 cpl_propertylist_delete(properties);
2210 if (rimg_frame == NULL) {
2211 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2223 slitgeometry = NULL;
2237 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
2238 "file '%s'! Aborting ...",
2239 cpl_frame_get_filename(rimg_frame));
2241 cpl_frame_delete(rimg_frame);
2253 slitgeometry = NULL;
2264 cpl_frameset_insert(set, rimg_frame);
2297 cpl_plugin_get_info(cpl_pluginlist* list)
2300 cpl_recipe* recipe = cx_calloc(1,
sizeof *recipe);
2301 cpl_plugin* plugin = &recipe->interface;
2304 cpl_plugin_init(plugin,
2306 GIRAFFE_BINARY_VERSION,
2307 CPL_PLUGIN_TYPE_RECIPE,
2309 "Process a science observation.",
2310 "For detailed information please refer to the "
2311 "GIRAFFE pipeline user manual.\nIt is available at "
2312 "http://www.eso.org/pipelines.",
2320 cpl_pluginlist_append(list, plugin);
2329 typedef struct _giraffe_lut_entry {
2330 const char* expmode;
2336 } giraffe_lut_entry;
2345 static cpl_boolean _giraffe_lut_is_sorted(
const giraffe_lut_entry* lut,
2349 if (nentries < 2)
return CPL_TRUE;
2350 for (i = 0; i < nentries - 1; ++i) {
2351 if (strcmp(lut[i].expmode, lut[i+1].expmode) >= 0) {
2368 static const giraffe_lut_entry* _giraffe_find_lut_entry(
const char* expmode)
2370 static giraffe_lut_entry lut[] = {
2375 {
"H379.", 25000, 0.2250, 20.0, 60},
2376 {
"H379.0", 25000, 0.2250, 20.0, 60},
2377 {
"H395.8", 22000, 0.1913, 21.1, 53},
2378 {
"H412.4", 30000, 0.1326, 22.3, 48},
2379 {
"H429.7", 23000, 0.1471, 23.5, 67},
2380 {
"H447.1", 20000, 0.0906, 24.9, 63},
2381 {
"H447.1A", 20000, 0.0906, 24.9, 63},
2382 {
"H447.1B", 31000, 0.1297, 23.5, 58},
2383 {
"H465.6", 23000, 0.1573, 22.4, 57},
2384 {
"H484.5", 19000, 0.1175, 23.5, 57},
2385 {
"H484.5A", 19000, 0.1175, 23.5, 57},
2386 {
"H484.5B", 33000, 0.0907, 24.6, 61},
2387 {
"H504.8", 22000, 0.1726, 25.2, 56},
2388 {
"H525.8", 17000, 0.1397, 25.8, 49},
2389 {
"H525.8A", 17000, 0.1397, 25.8, 49},
2390 {
"H525.8B", 29000, 0.2014, 26.4, 44},
2391 {
"H548.8", 20000, 0.2389, 26.9, 51},
2392 {
"H572.8", 27000, 0.1844, 27.5, 49},
2393 {
"H599.3", 18000, 0.1683, 28.3, 57},
2394 {
"H627.3", 24000, 0.1268, 29.0, 50},
2395 {
"H651.5", 17000, 0.1185, 30.0, 42},
2396 {
"H651.5A", 17000, 0.1185, 30.0, 42},
2397 {
"H651.5B", 35000, 0.1253, 31.5, 37},
2398 {
"H665.", 17000, 0.2076, 33.0, 45},
2399 {
"H665.0", 17000, 0.2076, 33.0, 45},
2400 {
"H679.7", 19000, 0.1621, 34.5, 51},
2401 {
"H710.5", 25000, 0.1495, 36.0, 48},
2402 {
"H737.", 16000, 0.1456, 37.5, 47},
2403 {
"H737.0", 16000, 0.1456, 37.5, 47},
2404 {
"H737.0A", 16000, 0.1456, 37.5, 47},
2405 {
"H737.0B", 35000, 0.1147, 39.5, 40},
2406 {
"H769.1", 19000, 0.2811, 41.8, 35},
2407 {
"H805.3", 14000, 0.2662, 42.4, 39},
2408 {
"H805.3A", 14000, 0.2662, 42.4, 39},
2409 {
"H805.3B", 25000, 0.2342, 42.9, 28},
2410 {
"H836.6", 16000, 0.2032, 43.3, 21},
2411 {
"H836.6A", 16000, 0.2032, 43.3, 21},
2412 {
"H836.6B", 34000, 0.1073, 43.7, 14},
2413 {
"H875.7", 18000, 0.2026, 44.3, 29},
2414 {
"H920.5", 12000, 0.1568, 44.9, 32},
2415 {
"H920.5A", 12000, 0.1568, 44.9, 32},
2416 {
"H920.5B", 24000, 0.2531, 45.9, 28},
2417 {
"L385.7", 7500, 0.3358, 58.0, 43},
2418 {
"L427.2", 6300, 0.2152, 61.1, 62},
2419 {
"L479.7", 7500, 0.1554, 71.0, 61},
2420 {
"L543.1", 5800, 0.2065, 82.1, 58},
2421 {
"L614.2", 6600, 0.1803, 79.0, 51},
2422 {
"L682.2", 8100, 0.1843, 74.0, 50},
2423 {
"L773.4", 5400, 0.1617, 94.0, 44},
2424 {
"L881.7", 6600, 0.1614, 119.0, 29}
2426 static const size_t nentries =
sizeof(lut) /
sizeof(giraffe_lut_entry);
2428 int high = (int)nentries - 1;
2430 assert(_giraffe_lut_is_sorted(lut, nentries));
2431 assert(expmode != NULL);
2435 int mid = (low + high) >> 1;
2436 int result = strcmp(expmode, lut[mid].expmode);
2439 }
else if (result < 0) {
2444 }
while (high >= low);
2454 static double _giraffe_lookup_specres(
const char* expmode)
2456 const giraffe_lut_entry* entry = _giraffe_find_lut_entry(expmode);
2457 if (entry == NULL)
return NAN;
2458 return entry->specres;
2467 static double _giraffe_lookup_lamrms(
const char* expmode)
2469 const giraffe_lut_entry* entry = _giraffe_find_lut_entry(expmode);
2470 if (entry == NULL)
return NAN;
2471 if (isnan(entry->lamrms) || isnan(entry->spec_length))
return NAN;
2472 return entry->lamrms * entry->spec_length / 4100.;
2481 static double _giraffe_lookup_lamnlin(
const char* expmode)
2483 const giraffe_lut_entry* entry = _giraffe_find_lut_entry(expmode);
2484 if (entry == NULL)
return -1;
2485 return entry->lamnlin;
2499 static cpl_type _giraffe_calc_wave_type(
double crval2,
double crpix2,
2500 double cdelt2, cpl_size naxis2)
2502 static const double errfrac = 0.02;
2503 static const double single_precision_digits = 7;
2504 double lo = (1.0 - crpix2) * cdelt2;
2505 double hi = ((double)naxis2 - crpix2) * cdelt2;
2506 double maxwave = crval2 + (hi > lo ? hi : lo);
2507 double binfrac = (maxwave != 0.0) ? fabs(cdelt2 / maxwave) : 0.0;
2508 if (binfrac * errfrac < pow(10, -single_precision_digits)) {
2509 return CPL_TYPE_DOUBLE;
2511 return CPL_TYPE_FLOAT;
2525 static cpl_boolean _giraffe_ancillary_data_available(
const char* filename,
2526 const GiTable* fibertable)
2530 const char** spectypes = NULL;
2532 assert(filename != NULL);
2534 cpl_error_ensure(tbl != NULL, cpl_error_get_code(),
return CPL_FALSE,
2535 "The fiber table is not available for '%s'.", filename);
2537 spectypes = cpl_table_get_data_string_const(tbl, GIALIAS_COLUMN_TYPE);
2538 cpl_error_ensure(spectypes != NULL, cpl_error_get_code(),
return CPL_FALSE,
2539 "Could not fetch the '%s' column from the fiber setup"
2540 " table in '%s'.", GIALIAS_COLUMN_TYPE, filename);
2546 for (i = 0; i < cpl_table_get_nrow(tbl); ++i) {
2547 if ((spectypes[i][0] !=
'\0') && (strcmp(spectypes[i],
"M") != 0)) {
2574 static cpl_error_code _giraffe_make_ancillary_file(cpl_frameset* allframes,
2575 const char* outputfile,
2576 const char* infilename,
2577 const GiImage* fluximage,
2578 const GiTable* fibertable)
2580 cpl_error_code error = CPL_ERROR_NONE;
2582 cpl_frame* frame = NULL;
2584 GiImage* image = NULL;
2586 cpl_image* img = NULL;
2588 cpl_image* subimg = NULL;
2589 cpl_propertylist* comments = NULL;
2591 int* indices = NULL;
2592 const char** spectypes = NULL;
2594 assert(allframes != NULL);
2595 assert(outputfile != NULL);
2596 assert(infilename != NULL);
2598 cpl_error_ensure(srcimg != NULL && table != NULL && tbl != NULL,
2599 cpl_error_get_code(),
goto cleanup,
2600 "The image or table are not available for '%s'.",
2604 frame = cpl_frame_new();
2605 error |= cpl_frame_set_filename(frame, outputfile);
2606 error |= cpl_frame_set_tag(frame, GIALIAS_ASSO_PROCATG_VALUE);
2607 error |= cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
2608 error |= cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
2609 error |= cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
2610 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2611 "Failed to setup a new output frame for '%s'.",
2620 error |= cpl_table_unselect_all(tbl);
2621 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2622 "Failed to unselect all entries in fiber setup table from"
2623 " '%s'.", infilename);
2624 cpl_table_or_selected_string(tbl, GIALIAS_COLUMN_TYPE, CPL_NOT_EQUAL_TO,
2626 cpl_table_and_selected_int(tbl, GIALIAS_COLUMN_RP, CPL_NOT_EQUAL_TO, -1);
2627 cpl_table_not_selected(tbl);
2628 error |= cpl_table_erase_selected(tbl);
2629 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2630 "Failed to erase selected entries in fiber setup table"
2631 " from '%s'.", infilename);
2635 ny = cpl_image_get_size_y(srcimg);
2637 cpl_table_get_nrow(tbl), ny);
2641 cpl_error_ensure(image != NULL && img != NULL && retcode == 0,
2642 cpl_error_get_code(),
goto cleanup,
2643 "Failed to create image for output file '%s'.",
2648 GIALIAS_ASSO_PROCATG_VALUE);
2649 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2650 "Could not update keyword '%s' for output file '%s'.",
2651 GIALIAS_PROCATG, outputfile);
2656 const char* comments_to_remove[] = {
2657 " FITS (Flexible Image Transport System) format is defined in"
2659 " and Astrophysics', volume 376, page 359; bibcode: 2001A&A..."
2664 comments = cpl_propertylist_new();
2665 error |= cpl_propertylist_copy_property_regexp(comments, props,
2667 cpl_propertylist_erase_regexp(props,
"^COMMENT$", 0);
2668 for (ic = 0; ic < cpl_propertylist_get_size(comments); ++ic) {
2669 const char** cmnt_str;
2670 cpl_property* p = cpl_propertylist_get(comments, ic);
2671 for (cmnt_str = comments_to_remove; *cmnt_str != NULL; ++cmnt_str) {
2672 if (strcmp(cpl_property_get_string(p), *cmnt_str) == 0) {
2673 goto dont_add_comment;
2677 error |= cpl_propertylist_append_property(props, p);
2683 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2684 "Failed to cleanup comments in primary HDU for '%s'.",
2686 cpl_propertylist_delete(comments);
2692 indices = cpl_table_get_data_int(tbl, GIALIAS_COLUMN_INDEX);
2693 cpl_error_ensure(indices != NULL, cpl_error_get_code(),
goto cleanup,
2694 "Could not fetch the '%s' column from the fiber setup"
2695 " table in '%s'.", GIALIAS_COLUMN_INDEX, infilename);
2696 for (i = 0; i < cpl_table_get_nrow(tbl); ++i) {
2697 cpl_size oldindex = indices[i];
2698 cpl_size newindex = i+1;
2699 indices[i] = newindex;
2700 subimg = cpl_image_extract(srcimg, oldindex, 1, oldindex, ny);
2701 cpl_error_ensure(subimg != NULL, cpl_error_get_code(),
goto cleanup,
2702 "Could not extract sub image from '%s' at column %"
2703 CPL_SIZE_FORMAT
".", infilename, oldindex);
2704 error |= cpl_image_copy(img, subimg, newindex, 1);
2705 cpl_image_delete(subimg);
2707 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2708 "Could write sub image from '%s' at column %"
2709 CPL_SIZE_FORMAT
" to new image in '%s' at column %"
2710 CPL_SIZE_FORMAT
".", infilename, oldindex, outputfile,
2716 cpl_error_ensure(retcode == 0, cpl_error_get_code(),
goto cleanup,
2717 "Failed to write image to file '%s'.", outputfile);
2719 cpl_error_ensure(retcode == 0, cpl_error_get_code(),
goto cleanup,
2720 "Failed to attach the fiber setup table to file '%s'.",
2726 error |= cpl_frameset_insert(allframes, frame);
2727 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2728 "Could not add a new frame to the frame list for '%s'.",
2733 return CPL_ERROR_NONE;
2737 cpl_image_delete(subimg);
2740 cpl_frame_delete(frame);
2741 cpl_propertylist_delete(comments);
2742 return cpl_error_get_code();
2752 static char* _giraffe_calc_format_string(cpl_size maxfiles)
2754 double ndigits = 1.0;
2758 ndigits = ceil(log10((
double)maxfiles + 1.0));
2760 return cpl_sprintf(
"science_spectrum_%%0%.0f"CPL_SIZE_FORMAT
".fits",
2781 static cxint _giraffe_make_sdp_spectra(
const cxchar* flux_filename,
2782 const cxchar* err_filename,
2784 cpl_frameset* allframes,
2785 const cpl_parameterlist* parlist,
2786 const cxchar* recipe_id)
2788 cxint result_code = 1;
2790 cpl_error_code error = CPL_ERROR_NONE;
2791 cpl_errorstate prestate;
2792 const char* ancillary_filename =
"science_ancillary.fits";
2795 GiTable* fibertable = NULL;
2796 const cxchar* fibertable_name = NULL;
2797 irplib_sdp_spectrum* spectrum = NULL;
2798 cpl_propertylist* extrakeys = cpl_propertylist_new();
2799 cpl_propertylist* tablekeys = cpl_propertylist_new();
2800 cpl_propertylist* props;
2801 char* pipe_id = cpl_sprintf(
"%s/%s", PACKAGE, VERSION);
2802 const char* dict_id = PRODUCT_DID;
2803 const cpl_frame* inherit = NULL;
2804 cpl_frameset* usedframes = NULL;
2805 cpl_frameset* rawframes = NULL;
2806 cpl_frameset_iterator* iterator = NULL;
2807 cpl_size nx, ny, i, filecount;
2808 double exptime = NAN;
2809 double mjdobs = NAN;
2810 double mjdend = NAN;
2811 double wavelmin = NAN;
2812 double wavelmax = NAN;
2813 double specbin = NAN;
2814 double crpix2 = NAN;
2815 double crval2 = NAN;
2816 double cdelt2 = NAN;
2817 const char* cunit2 = NULL;
2819 const char* expmode = NULL;
2821 const int* indices = NULL;
2822 const int* fps = NULL;
2823 const char** objects = NULL;
2824 const char** spectypes = NULL;
2825 const double* ras = NULL;
2826 const double* decs = NULL;
2827 const double* gcorr = NULL;
2828 const double* hcorr = NULL;
2829 const double* bcorr = NULL;
2830 char* formatstr = NULL;
2831 char* filename = NULL;
2832 cpl_type wavecoltype;
2833 cpl_array* refwavearray = NULL;
2834 cpl_array* array = NULL;
2835 float* data_float = NULL;
2836 double* data_double = NULL;
2837 cpl_vector* fluximgcol = NULL;
2838 cpl_vector* errimgcol = NULL;
2839 cpl_boolean got_ancillary_data = CPL_FALSE;
2840 cpl_size assoc_key_offset = 1;
2842 double lamrms = NAN;
2843 double specerr = NAN;
2844 double specsye = NAN;
2846 cpl_error_ensure(flux_filename != NULL && err_filename != NULL
2847 && allframes != NULL && parlist != NULL
2848 && recipe_id != NULL, CPL_ERROR_NULL_INPUT,
goto cleanup,
2849 "NULL input parameters.");
2851 error |= cpl_propertylist_append_string(extrakeys, GIALIAS_PROCATG,
2852 GIALIAS_PROCATG_RBNSPEC_IDP);
2853 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_PROCATG,
2854 GIALIAS_PROCATG_COMMENT);
2855 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2856 "Could not set keyword '%s'.", GIALIAS_PROCATG);
2860 cpl_error_ensure(errorcode == 0, cpl_error_get_code(),
goto cleanup,
2861 "Could not load image data in primary HDU from '%s'.",
2864 cpl_error_ensure(errorcode == 0, cpl_error_get_code(),
goto cleanup,
2865 "Could not load image data in primary HDU from '%s'.",
2868 giraffe_error_push();
2870 if (fibertable == NULL) {
2872 GIALIAS_FIBER_SETUP);
2873 fibertable_name = err_filename;
2875 fibertable_name = flux_filename;
2877 cpl_error_ensure(fibertable != NULL, CPL_ERROR_DATA_NOT_FOUND,
goto cleanup,
2878 "Could not load the %s table from either '%s' or '%s'.",
2879 GIALIAS_FIBER_SETUP, flux_filename, err_filename);
2880 giraffe_error_pop();
2887 CPL_ERROR_INCOMPATIBLE_INPUT,
goto cleanup,
2888 "The images in files '%s' and '%s' are not the same size.",
2889 flux_filename, err_filename);
2893 usedframes = cpl_frameset_new();
2894 rawframes = cpl_frameset_new();
2895 iterator = cpl_frameset_iterator_new(allframes);
2897 const cpl_frame* frame = cpl_frameset_iterator_get_const(iterator);
2898 if (frame != NULL) {
2899 switch (cpl_frame_get_group(frame)) {
2900 case CPL_FRAME_GROUP_RAW:
2902 if (inherit == NULL) inherit = frame;
2903 error |= cpl_frameset_insert(rawframes,
2904 cpl_frame_duplicate(frame));
2905 error |= cpl_frameset_insert(usedframes,
2906 cpl_frame_duplicate(frame));
2908 case CPL_FRAME_GROUP_CALIB:
2909 error |= cpl_frameset_insert(usedframes,
2910 cpl_frame_duplicate(frame));
2916 prestate = cpl_errorstate_get();
2917 error |= cpl_frameset_iterator_advance(iterator, 1);
2918 if (error == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
2919 cpl_errorstate_set(prestate);
2921 }
else if (error != CPL_ERROR_NONE) {
2926 cpl_error_ensure(inherit != NULL, CPL_ERROR_DATA_NOT_FOUND,
goto cleanup,
2927 "No raw input frames found.");
2931 prestate = cpl_errorstate_get();
2932 exptime = cpl_propertylist_get_double(props, GIALIAS_EXPTIME);
2933 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
2934 goto cleanup,
"Could not find keyword '%s' in '%s'.",
2935 GIALIAS_EXPTIME, flux_filename);
2936 mjdobs = cpl_propertylist_get_double(props, GIALIAS_MJDOBS);
2937 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
2938 goto cleanup,
"Could not find keyword '%s' in '%s'.",
2939 GIALIAS_MJDOBS, flux_filename);
2941 mjdend = mjdobs + exptime / 86400.;
2944 crpix2 = cpl_propertylist_get_double(props, GIALIAS_CRPIX2);
2945 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
2946 goto cleanup,
"Could not find keyword '%s' in '%s'.",
2947 GIALIAS_CRPIX2, flux_filename);
2948 crval2 = cpl_propertylist_get_double(props, GIALIAS_CRVAL2);
2949 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
2950 goto cleanup,
"Could not find keyword '%s' in '%s'.",
2951 GIALIAS_CRVAL2, flux_filename);
2952 cdelt2 = cpl_propertylist_get_double(props, GIALIAS_CDELT2);
2953 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
2954 goto cleanup,
"Could not find keyword '%s' in '%s'.",
2955 GIALIAS_CDELT2, flux_filename);
2956 cunit2 = cpl_propertylist_get_string(props, GIALIAS_CUNIT2);
2957 cpl_error_ensure(cunit2 != NULL, cpl_error_get_code(),
2958 goto cleanup,
"Could not find keyword '%s' in '%s'.",
2959 GIALIAS_CUNIT2, flux_filename);
2961 if (strcmp(cunit2,
"nm") == 0) {
2962 wavelmin = (1.0 - crpix2) * cdelt2 + crval2;
2963 wavelmax = ((double)ny - crpix2) * cdelt2 + crval2;
2964 if (wavelmax < wavelmin) {
2965 double tmp = wavelmin;
2966 wavelmin = wavelmax;
2969 specbin = fabs(cdelt2);
2971 cpl_msg_warning(cpl_func,
"Do not know how to handle keyword %s = '%s'."
2972 " Will not set WAVELMIN, WAVELMAX or SPEC_BIN.",
2973 GIALIAS_CUNIT2, cunit2);
2976 if (cpl_propertylist_has(props, GIALIAS_SETUPNAME)) {
2977 expmode = cpl_propertylist_get_string(props, GIALIAS_SETUPNAME);
2978 cpl_error_ensure(expmode != NULL, cpl_error_get_code(),
goto cleanup,
2979 "Could not fetch the keyword '%s' from '%s'.",
2980 GIALIAS_SETUPNAME, flux_filename);
2981 }
else if (cpl_propertylist_has(props, GIALIAS_GRATNAME)) {
2982 const char* name = cpl_propertylist_get_string(props, GIALIAS_GRATNAME);
2983 cpl_error_ensure(name != NULL, cpl_error_get_code(),
goto cleanup,
2984 "Could not fetch the keyword '%s' from '%s'.",
2985 GIALIAS_GRATNAME, flux_filename);
2986 double wlen = cpl_propertylist_get_double(props, GIALIAS_GRATWLEN);
2987 cpl_error_ensure(cpl_errorstate_is_equal(prestate),
2988 cpl_error_get_code(),
goto cleanup,
2989 "Could not find keyword '%s' in '%s'.",
2990 GIALIAS_GRATWLEN, flux_filename);
2991 strbuf[0] = name[0];
2992 char* numstr = cpl_sprintf(
"%.1f", wlen);
2993 strncpy(strbuf+1, numstr,
sizeof(strbuf)-1);
2995 strbuf[
sizeof(strbuf)-1] =
'\0';
2998 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2999 "Neither '%s' nor '%s' and '%s' keywords were found in the"
3000 " file '%s'.", GIALIAS_SETUPNAME, GIALIAS_GRATNAME,
3001 GIALIAS_GRATWLEN, flux_filename);
3005 specres = _giraffe_lookup_specres(expmode);
3006 if (isnan(specres)) {
3007 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
3008 "The exposure mode '%s' is invalid or an unknown value."
3009 " Could not lookup the spectral resolution for 'SPEC_RES'.",
3016 if (cpl_propertylist_has(props,
"FILTER")) {
3017 prestate = cpl_errorstate_get();
3018 cpl_propertylist_copy_property(extrakeys, props,
"FILTER");
3019 cpl_property* prop = cpl_propertylist_get_property(extrakeys,
"FILTER");
3020 cpl_property_set_name(prop,
"OFILTER");
3021 cpl_error_ensure(cpl_errorstate_is_equal(prestate),
3022 cpl_error_get_code(),
goto cleanup,
3023 "Could not rename the 'FILTER' keyword.");
3027 prestate = cpl_errorstate_get();
3028 got_ancillary_data = _giraffe_ancillary_data_available(flux_filename,
3030 if (! cpl_errorstate_is_equal(prestate))
goto cleanup;
3031 if (got_ancillary_data) {
3032 error = _giraffe_make_ancillary_file(allframes, ancillary_filename,
3033 flux_filename, fluximage,
3035 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3036 "Failed to write the ancillary file '%s'.",
3037 ancillary_filename);
3041 spectrum = irplib_sdp_spectrum_new();
3042 error = CPL_ERROR_NONE;
3043 error |= irplib_sdp_spectrum_set_origin(spectrum, GIALIAS_ORIGIN_VALUE);
3044 error |= irplib_sdp_spectrum_set_prodlvl(spectrum, GIALIAS_PRODLVL_VALUE);
3045 error |= irplib_sdp_spectrum_copy_dispelem(spectrum,
3046 props, GIALIAS_GRATNAME);
3047 error |= irplib_sdp_spectrum_set_specsys(spectrum, GIALIAS_SPECSYS_VALUE);
3048 error |= irplib_sdp_spectrum_set_extobj(spectrum, GIALIAS_EXT_OBJ_VALUE);
3051 error |= irplib_sdp_spectrum_set_object(spectrum,
"");
3052 error |= irplib_sdp_spectrum_set_ra(spectrum, 0.0);
3053 error |= irplib_sdp_spectrum_set_dec(spectrum, 0.0);
3054 error |= irplib_sdp_spectrum_copy_exptime(spectrum, props, GIALIAS_EXPTIME);
3055 error |= irplib_sdp_spectrum_copy_texptime(spectrum,
3056 props, GIALIAS_EXPTIME);
3057 error |= irplib_sdp_spectrum_copy_mjdobs(spectrum, props, GIALIAS_MJDOBS);
3058 error |= irplib_sdp_spectrum_set_mjdend(spectrum, mjdend);
3059 if (cpl_propertylist_has(props, GIALIAS_TIMESYS)) {
3060 error |= irplib_sdp_spectrum_copy_timesys(spectrum,
3061 props, GIALIAS_TIMESYS);
3063 error |= irplib_sdp_spectrum_copy_progid(spectrum, props, GIALIAS_PROGID);
3064 error |= irplib_sdp_spectrum_copy_obid(spectrum, 1, props, GIALIAS_OBSID);
3065 error |= irplib_sdp_spectrum_set_mepoch(spectrum, GIALIAS_M_EPOCH_VALUE);
3066 error |= irplib_sdp_spectrum_append_prov(spectrum, 1, rawframes);
3067 error |= irplib_sdp_spectrum_copy_procsoft(spectrum,
3068 props, GIALIAS_PROPIPEID);
3069 error |= irplib_sdp_spectrum_copy_obstech(spectrum, props, GIALIAS_PROTECH);
3070 error |= irplib_sdp_spectrum_set_prodcatg(spectrum, GIALIAS_PRODCATG_VALUE);
3071 error |= irplib_sdp_spectrum_set_fluxcal(spectrum, GIALIAS_FLUXCAL_VALUE);
3072 error |= irplib_sdp_spectrum_set_contnorm(spectrum, GIALIAS_CONTNORM_VALUE);
3076 error |= irplib_sdp_spectrum_set_wavelmin(spectrum, 0.0);
3077 error |= irplib_sdp_spectrum_set_wavelmax(spectrum, 0.0);
3078 error |= irplib_sdp_spectrum_set_specbin(spectrum, 0.0);
3079 error |= irplib_sdp_spectrum_set_totflux(spectrum, GIALIAS_TOTFLUX_VALUE);
3080 error |= irplib_sdp_spectrum_set_fluxerr(spectrum, GIALIAS_FLUXERR_VALUE);
3081 error |= irplib_sdp_spectrum_set_ncombine(spectrum,
3082 cpl_frameset_get_size(rawframes));
3083 error |= irplib_sdp_spectrum_set_referenc(spectrum, GIALIAS_REFERENC);
3086 error |= irplib_sdp_spectrum_set_snr(spectrum, 0.0);
3089 if (cpl_propertylist_has(props, GIALIAS_LAMNLIN)) {
3090 error |= irplib_sdp_spectrum_copy_lamnlin(spectrum, props,
3092 lamnlin = irplib_sdp_spectrum_get_lamnlin(spectrum);
3094 lamnlin = _giraffe_lookup_lamnlin(expmode);
3095 if (lamnlin != -1) {
3096 error |= irplib_sdp_spectrum_set_lamnlin(spectrum, lamnlin);
3103 if (cpl_propertylist_has(props, GIALIAS_LAMRMS)) {
3104 error |= irplib_sdp_spectrum_copy_lamrms(spectrum, props,
3106 lamrms = irplib_sdp_spectrum_get_lamrms(spectrum);
3108 lamrms = _giraffe_lookup_lamrms(expmode);
3109 if (! isnan(lamrms)) {
3110 error |= irplib_sdp_spectrum_set_lamrms(spectrum, lamrms);
3124 if (cpl_propertylist_has(props, GIALIAS_SPEC_ERR)) {
3125 error |= irplib_sdp_spectrum_copy_specerr(spectrum, props,
3127 specerr = irplib_sdp_spectrum_get_specerr(spectrum);
3128 }
else if (lamnlin > 0) {
3129 if (cpl_propertylist_has(props, GIALIAS_CRDER1)) {
3130 prestate = cpl_errorstate_get();
3131 double crder1 = cpl_propertylist_get_double(props, GIALIAS_CRDER1);
3132 if (cpl_errorstate_is_equal(prestate) && crder1 > 0) {
3133 specerr = crder1 / sqrt(lamnlin);
3135 error = cpl_error_get_code();
3137 }
else if (! isnan(lamrms)) {
3138 specerr = lamrms / sqrt(lamnlin);
3140 if (! isnan(specerr)) {
3141 error |= irplib_sdp_spectrum_set_specerr(spectrum, specerr);
3148 if (cpl_propertylist_has(props, GIALIAS_SPEC_SYE)) {
3149 error |= irplib_sdp_spectrum_copy_specsye(spectrum, props,
3151 specsye = irplib_sdp_spectrum_get_specsye(spectrum);
3156 error |= irplib_sdp_spectrum_set_specsye(spectrum, 0.002);
3159 error |= irplib_sdp_spectrum_set_specres(spectrum, specres);
3160 error |= irplib_sdp_spectrum_copy_gain(spectrum, props, GIALIAS_CONAD);
3161 error |= irplib_sdp_spectrum_copy_detron(spectrum, props, GIALIAS_RON);
3162 if (got_ancillary_data) {
3163 error |= irplib_sdp_spectrum_set_asson(spectrum, 1, ancillary_filename);
3164 error |= irplib_sdp_spectrum_set_assoc(spectrum, 1,
3165 GIALIAS_ASSOC_VALUE);
3166 error |= irplib_sdp_spectrum_set_assom(spectrum, 1,
"");
3167 assoc_key_offset = 2;
3169 for (i = assoc_key_offset; i < nassoc_keys + assoc_key_offset; ++i) {
3171 error |= irplib_sdp_spectrum_set_asson(spectrum, i,
"");
3172 error |= irplib_sdp_spectrum_set_assoc(spectrum, i,
"");
3173 error |= irplib_sdp_spectrum_set_assom(spectrum, i,
"");
3176 error |= irplib_sdp_spectrum_set_voclass(spectrum, GIALIAS_VOCLASS_VALUE);
3177 error |= irplib_sdp_spectrum_set_vopub(spectrum, GIALIAS_VOPUB_VALUE);
3178 error |= irplib_sdp_spectrum_set_title(spectrum,
"");
3179 error |= cpl_propertylist_append_double(tablekeys, GIALIAS_APERTURE,
3180 GIALIAS_APERTURE_VALUE);
3181 error |= cpl_propertylist_set_comment(tablekeys, GIALIAS_APERTURE,
3182 GIALIAS_APERTURE_COMMENT);
3191 error |= irplib_sdp_spectrum_set_telapse(spectrum, exptime);
3192 error |= irplib_sdp_spectrum_set_tmid(spectrum, (mjdobs + mjdend) * 0.5);
3196 error |= irplib_sdp_spectrum_set_specval(spectrum, 0.0);
3197 error |= irplib_sdp_spectrum_set_specbw(spectrum, 0.0);
3198 error |= irplib_sdp_spectrum_set_nelem(spectrum, ny);
3199 error |= irplib_sdp_spectrum_set_tdmin(spectrum, 0.0);
3200 error |= irplib_sdp_spectrum_set_tdmax(spectrum, 0.0);
3203 error |= cpl_propertylist_append_int(extrakeys, GIALIAS_FPS, -1);
3204 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_FPS,
3205 GIALIAS_FPS_COMMENT);
3208 error |= cpl_propertylist_append_double(extrakeys, GIALIAS_GEOCORR, NAN);
3209 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_GEOCORR,
3210 GIALIAS_GEOCORR_COMMENT);
3211 error |= cpl_propertylist_append_double(extrakeys, GIALIAS_HELICORR, NAN);
3212 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_HELICORR,
3213 GIALIAS_HELICORR_COMMENT);
3214 error |= cpl_propertylist_append_double(extrakeys, GIALIAS_BARYCORR, NAN);
3215 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_BARYCORR,
3216 GIALIAS_BARYCORR_COMMENT);
3218 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3219 "Could not setup the common SDP spectrum keywords.");
3223 wavecoltype = _giraffe_calc_wave_type(crval2, crpix2, cdelt2, ny);
3226 refwavearray = cpl_array_new(ny, CPL_TYPE_DOUBLE);
3227 data_double = cpl_array_get_data_double(refwavearray);
3228 assert(data_double != NULL);
3229 for (i = 1; i <= ny; ++i) {
3230 data_double[i-1] = (i-crpix2) * cdelt2 + crval2;
3235 error |= irplib_sdp_spectrum_add_column(
3236 spectrum, GIALIAS_COLUMN_WAVE, wavecoltype,
3237 GIALIAS_COLUMN_WAVE_UNIT, NULL, GIALIAS_COLUMN_WAVE_TUTYP,
3238 GIALIAS_COLUMN_WAVE_TUCD, NULL);
3241 error |= irplib_sdp_spectrum_replace_column_comment(
3242 spectrum, GIALIAS_COLUMN_WAVE,
"TUCD",
"Air wavelength");
3244 error |= irplib_sdp_spectrum_set_column_tcomm(
3245 spectrum, GIALIAS_COLUMN_WAVE, GIALIAS_COLUMN_WAVE_TCOMM);
3246 error |= irplib_sdp_spectrum_add_column(
3247 spectrum, GIALIAS_COLUMN_FLUX_REDUCED, CPL_TYPE_DOUBLE,
3248 GIALIAS_COLUMN_FLUX_REDUCED_UNIT, NULL,
3249 GIALIAS_COLUMN_FLUX_REDUCED_TUTYP,
3250 GIALIAS_COLUMN_FLUX_REDUCED_TUCD, NULL);
3251 error |= irplib_sdp_spectrum_set_column_tcomm(
3252 spectrum, GIALIAS_COLUMN_FLUX_REDUCED,
"");
3253 error |= irplib_sdp_spectrum_add_column(
3254 spectrum, GIALIAS_COLUMN_ERR_REDUCED, CPL_TYPE_DOUBLE,
3255 GIALIAS_COLUMN_ERR_REDUCED_UNIT, NULL,
3256 GIALIAS_COLUMN_ERR_REDUCED_TUTYP,
3257 GIALIAS_COLUMN_ERR_REDUCED_TUCD, NULL);
3258 error |= irplib_sdp_spectrum_set_column_tcomm(
3259 spectrum, GIALIAS_COLUMN_ERR_REDUCED,
"");
3260 error |= irplib_sdp_spectrum_add_column(
3261 spectrum, GIALIAS_COLUMN_SNR, CPL_TYPE_DOUBLE,
3262 GIALIAS_COLUMN_SNR_UNIT, NULL, GIALIAS_COLUMN_SNR_TUTYP,
3263 GIALIAS_COLUMN_SNR_TUCD, NULL);
3264 error |= irplib_sdp_spectrum_set_column_tcomm(
3265 spectrum, GIALIAS_COLUMN_SNR, GIALIAS_COLUMN_SNR_TCOMM);
3266 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3267 "Could not setup the SDP spectrum columns.");
3270 GIALIAS_COLUMN_INDEX);
3274 GIALIAS_COLUMN_OBJECT);
3276 GIALIAS_COLUMN_TYPE);
3280 GIALIAS_COLUMN_DEC);
3282 GIALIAS_COLUMN_GCORR);
3284 GIALIAS_COLUMN_HCORR);
3286 GIALIAS_COLUMN_BCORR);
3287 cpl_error_ensure(indices != NULL && fps != NULL && objects != NULL
3288 && spectypes != NULL && ras != NULL && decs != NULL
3289 && gcorr != NULL && hcorr != NULL && bcorr != NULL,
3290 cpl_error_get_code(),
goto cleanup,
3291 "Could not fetch data from the fiber setup table in '%s'.",
3294 formatstr = _giraffe_calc_format_string(
3300 const double* wave_data;
3306 const char* remregexp =
"^(CDELT[0-9]+|CD[0-9]+_[0-9]+|CRPIX[0-9]+"
3307 "|CRDER[0-9]+|CSYER[0-9]+|BUNIT|BSCALE|BZERO"
3309 cpl_size specindex = indices[i];
3310 double vela, velb, beta;
3311 double correction_factor = 1.0;
3314 if (strcmp(spectypes[i],
"M") != 0)
continue;
3316 filename = cpl_sprintf(formatstr, ++filecount);
3321 vela = gcorr[i] * 1e3;
3322 velb = hcorr[i] * 1e3;
3323 beta = (vela + velb) / CPL_PHYS_C;
3324 cpl_error_ensure(-1 <= beta && beta <= 1,
3325 CPL_ERROR_ILLEGAL_OUTPUT,
goto cleanup,
3326 "The velocities GCORR = %g and HCORR = %g for spectrum"
3327 "%"CPL_SIZE_FORMAT
" in file '%s' give invalid"
3328 " Heliocentric correction factor values.",
3329 gcorr[i], hcorr[i], specindex, flux_filename);
3330 correction_factor = sqrt((1.0 + beta) / (1.0 - beta));
3334 wave_data = cpl_array_get_data_double_const(refwavearray);
3335 if (wavecoltype == CPL_TYPE_FLOAT) {
3336 data_float = cpl_malloc(ny *
sizeof(
float));
3337 for (j = 0; j < ny; ++j) {
3338 data_float[j] = wave_data[j] * correction_factor;
3340 array = cpl_array_wrap_float(data_float, ny);
3342 data_double = cpl_malloc(ny *
sizeof(
double));
3343 for (j = 0; j < ny; ++j) {
3344 data_double[j] = wave_data[j] * correction_factor;
3346 array = cpl_array_wrap_double(data_double, ny);
3348 error |= irplib_sdp_spectrum_set_column_data(
3349 spectrum, GIALIAS_COLUMN_WAVE, array);
3350 cpl_array_unwrap(array);
3353 fluximgcol = cpl_vector_new_from_image_column(
3355 flux_data = cpl_vector_get_data(fluximgcol);
3356 cpl_error_ensure(flux_data != NULL, cpl_error_get_code(),
goto cleanup,
3357 "Unable to extract data in column %"CPL_SIZE_FORMAT
3358 " from image in file '%s'.", specindex, flux_filename);
3359 array = cpl_array_wrap_double(flux_data, ny);
3360 error |= irplib_sdp_spectrum_set_column_data(
3361 spectrum, GIALIAS_COLUMN_FLUX_REDUCED, array);
3362 cpl_array_unwrap(array);
3365 errimgcol = cpl_vector_new_from_image_column(
3367 err_data = cpl_vector_get_data(errimgcol);
3368 cpl_error_ensure(err_data != NULL, cpl_error_get_code(),
goto cleanup,
3369 "Unable to extract data in column %"CPL_SIZE_FORMAT
3370 " from image in file '%s'.", specindex, err_filename);
3371 array = cpl_array_wrap_double(err_data, ny);
3372 error |= irplib_sdp_spectrum_set_column_data(
3373 spectrum, GIALIAS_COLUMN_ERR_REDUCED, array);
3374 cpl_array_unwrap(array);
3377 data_double = cpl_malloc(ny *
sizeof(
double));
3378 for (j = 0; j < ny; ++j) {
3379 data_double[j] = (err_data[j] != 0.0) ? flux_data[j] / err_data[j]
3382 array = cpl_array_wrap_double(data_double, ny);
3383 snr = cpl_array_get_median(array);
3384 error |= irplib_sdp_spectrum_set_column_data(spectrum,
3385 GIALIAS_COLUMN_SNR, array);
3386 cpl_array_unwrap(array);
3388 cpl_free(data_double);
3391 cpl_vector_delete(fluximgcol);
3393 cpl_vector_delete(errimgcol);
3396 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3397 "Could not setup the SDP spectrum columns for '%s'.",
3400 error |= irplib_sdp_spectrum_set_object(spectrum, objects[i]);
3401 error |= irplib_sdp_spectrum_set_title(spectrum, objects[i]);
3402 error |= irplib_sdp_spectrum_set_ra(spectrum, ras[i]);
3403 error |= irplib_sdp_spectrum_set_dec(spectrum, decs[i]);
3404 error |= irplib_sdp_spectrum_set_snr(spectrum, snr);
3405 error |= irplib_sdp_spectrum_set_column_tcomm(
3406 spectrum, GIALIAS_COLUMN_FLUX_REDUCED, flux_filename);
3407 error |= irplib_sdp_spectrum_set_column_tcomm(
3408 spectrum, GIALIAS_COLUMN_ERR_REDUCED, err_filename);
3410 error |= cpl_propertylist_update_int(extrakeys, GIALIAS_FPS, fps[i]);
3411 error |= cpl_propertylist_update_double(extrakeys, GIALIAS_GEOCORR,
3413 error |= cpl_propertylist_update_double(extrakeys, GIALIAS_HELICORR,
3415 error |= cpl_propertylist_update_double(extrakeys, GIALIAS_BARYCORR,
3419 error |= irplib_sdp_spectrum_set_wavelmin(spectrum,
3420 wavelmin * correction_factor);
3421 error |= irplib_sdp_spectrum_set_wavelmax(spectrum,
3422 wavelmax * correction_factor);
3423 error |= irplib_sdp_spectrum_set_specval(spectrum,
3424 (wavelmax + wavelmin) * 0.5 * correction_factor);
3425 error |= irplib_sdp_spectrum_set_specbw(spectrum,
3426 (wavelmax - wavelmin) * correction_factor);
3427 error |= irplib_sdp_spectrum_set_tdmin(spectrum,
3428 wavelmin * correction_factor);
3429 error |= irplib_sdp_spectrum_set_tdmax(spectrum,
3430 wavelmax * correction_factor);
3431 error |= irplib_sdp_spectrum_set_specbin(spectrum,
3432 specbin * correction_factor);
3433 if (! isnan(lamrms)) {
3434 error |= irplib_sdp_spectrum_set_lamrms(spectrum,
3435 lamrms * correction_factor);
3437 if (! isnan(specerr)) {
3438 error |= irplib_sdp_spectrum_set_specerr(spectrum,
3439 specerr * correction_factor);
3441 if (! isnan(specsye)) {
3442 error |= irplib_sdp_spectrum_set_specsye(spectrum,
3443 specsye * correction_factor);
3446 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3447 "Could not setup the SDP spectrum keywords for '%s'.",
3450 error |= irplib_dfs_save_spectrum(allframes, NULL, parlist, usedframes,
3451 inherit, spectrum, recipe_id,
3452 extrakeys, tablekeys, remregexp,
3453 pipe_id, dict_id, filename);
3454 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3455 "Failed to save SDP spectrum %"CPL_SIZE_FORMAT
3456 " to file '%s'.", specindex, filename);
3458 error |= irplib_fits_update_checksums(filename);
3459 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3460 "Failed to save update checksums for file '%s'.",
3466 if (filecount == 0) {
3467 cpl_msg_warning(cpl_func,
"No science spectra found in '%s'."
3468 " No SDP spectra created.", flux_filename);
3476 cpl_vector_delete(fluximgcol);
3477 cpl_vector_delete(errimgcol);
3478 cpl_array_unwrap(array);
3479 cpl_array_delete(refwavearray);
3480 cpl_free(data_float);
3481 cpl_free(data_double);
3483 cpl_free(formatstr);
3484 cpl_frameset_delete(usedframes);
3485 cpl_frameset_delete(rawframes);
3486 cpl_frameset_iterator_delete(iterator);
3488 cpl_propertylist_delete(tablekeys);
3489 cpl_propertylist_delete(extrakeys);
3493 irplib_sdp_spectrum_delete(spectrum);
cxint giraffe_add_rvcorrection(GiTable *fibers, const GiImage *spectra)
Add the barycentric and heliocentric corrections to the given fiber setup.
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.
cxint giraffe_subtract_dark(GiImage *image, const GiImage *dark, const GiImage *bpixel, GiDarkResults *data, const GiDarkConfig *config)
Subtract the dark current from a bias corrected image.
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.
GiTable * giraffe_fiberlist_load(const cxchar *filename, cxint dataset, const cxchar *tag)
Load a fiber table from a file.
GiFlatConfig * giraffe_flat_config_create(cpl_parameterlist *list)
Creates a setup structure for the flat field correction.
void giraffe_flat_config_destroy(GiFlatConfig *config)
Destroys a flat field setup structure.
void giraffe_flat_config_add(cpl_parameterlist *list)
Adds parameters for the flat field correction.
cxint giraffe_flat_apply(GiExtraction *extraction, const GiTable *fibers, const GiImage *flat, const GiImage *errors, GiFlatConfig *config)
Apply the flat field correction to the given extracted spectra.
void giraffe_fov_config_destroy(GiFieldOfViewConfig *config)
Destroys a field of view setup structure.
GiFieldOfView * giraffe_fov_new(void)
Create an empty container for the results of the field of view reconstruction.
GiFieldOfViewConfig * giraffe_fov_config_create(cpl_parameterlist *list)
Creates a setup structure for the field of view reconstruction.
cxint giraffe_fov_save_cubes_eso3d(const GiFieldOfView *self, cpl_propertylist *properties, const cxchar *filename, cxptr data)
Write the cube components of a field-of-view object to a file.
void giraffe_fov_delete(GiFieldOfView *self)
Deallocate a field of view object and its contents.
cxint giraffe_fov_build(GiFieldOfView *result, GiRebinning *rebinning, GiTable *fibers, GiTable *wsolution, GiTable *grating, GiTable *slitgeometry, GiFieldOfViewConfig *config)
Create and image and a data cube from extracted and rebinned spectra.
cxint giraffe_fov_save_cubes(const GiFieldOfView *self, cpl_propertylist *properties, const cxchar *filename, cxptr data)
Write the cube components of a field-of-view object to a file.
void giraffe_fov_config_add(cpl_parameterlist *list)
Adds parameters for the image and data cube construction.
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_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_create(cpl_type type, cxint nx, cxint ny)
Creates an image container of a given type.
GiImage * giraffe_image_new(cpl_type type)
Creates an empty image container.
cxint giraffe_image_set_properties(GiImage *self, cpl_propertylist *properties)
Attaches a property list to an image.
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.
cxint giraffe_compute_offsets(GiTable *fibers, const GiRebinning *rebinning, const GiTable *grating, const GiTable *mask, const GiSGCalConfig *config)
Compute wavelength offsets for a set of rebinned input spectrum.
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.
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.
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.
GiTable * giraffe_table_duplicate(const GiTable *src)
Duplicate a Giraffe table.
cxint giraffe_add_frameset_info(cpl_propertylist *plist, const cpl_frameset *set, cxint sequence)
Add frameset specific information to a property list.
const cxchar * giraffe_get_license(void)
Get the pipeline copyright and license.
GiInstrumentMode giraffe_get_mode(cpl_propertylist *properties)
Determines the instrument mode from a property list.
Slit geometry calibration configuration data structure.