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>
42#include "gifiberutils.h"
43#include "gislitgeometry.h"
49#include "gitransmission.h"
50#include "girebinning.h"
51#include "gisgcalibration.h"
52#include "giastrometry.h"
54#include "gimessages.h"
58const char *sciqcpar[] = {
59 GIALIAS_QCSCHEME, GIALIAS_WLSTART, GIALIAS_WLEND,
60 GIALIAS_WLSTEP, GIALIAS_QCAIR, GIALIAS_QCFWHM,
61 GIALIAS_QCNFIB, GIALIAS_QCNFIBSCI, GIALIAS_QCNFIBSKY,
62 GIALIAS_QCMEANRED, GIALIAS_QCNSATSCI, GIALIAS_QCSNR,
63 GIALIAS_QCMAG, GIALIAS_QCDLTTEMP, GIALIAS_QCDLTTIME,
64 GIALIAS_QCBRIGHTFLG, GIALIAS_QCFLAG
67static cxint giscience(cpl_parameterlist*, cpl_frameset*);
69static cxint _giraffe_make_sdp_spectra(
const cxchar* flux_filename,
70 const cxchar* err_filename,
72 cpl_frameset* allframes,
73 const cpl_parameterlist* parlist,
74 const cxchar* recipe_id);
76const cxdouble saturation = 60000.;
85giscience_create(cpl_plugin* plugin)
88 cpl_recipe* recipe = (cpl_recipe*)plugin;
90 cpl_parameter* p = NULL;
102 recipe->parameters = cpl_parameterlist_new();
103 cx_assert(recipe->parameters != NULL);
132 p = cpl_parameter_new_value(
"giraffe.siwc.apply",
134 "Enable simultaneous wavelength calibration "
139 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"siwc-apply");
140 cpl_parameterlist_append(recipe->parameters, p);
150 p = cpl_parameter_new_value(
"giraffe.sdp.format.generate",
152 "TRUE if additional files should be generated"
153 " in Science Data Product (SDP) format.",
157 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"generate-SDP-format");
158 cpl_parameterlist_append(recipe->parameters, p);
160 p = cpl_parameter_new_value(
"giraffe.sdp.nassoc.keys",
162 "Sets the number of dummy (empty) ASSONi,"
163 " ASSOCi and ASSOMi keywords to create.",
167 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
168 "dummy-association-keys");
169 cpl_parameterlist_append(recipe->parameters, p);
181giscience_exec(cpl_plugin* plugin)
184 cpl_errorstate prev_state;
186 cpl_recipe* recipe = (cpl_recipe*)plugin;
189 cx_assert(recipe->parameters != NULL);
190 cx_assert(recipe->frames != NULL);
192 prev_state = cpl_errorstate_get();
193 result = giscience(recipe->parameters, recipe->frames);
195 cpl_errorstate_dump(prev_state, CPL_FALSE, cpl_errorstate_dump_one);
202giscience_destroy(cpl_plugin* plugin)
205 cpl_recipe* recipe = (cpl_recipe*)plugin;
214 cpl_parameterlist_delete(recipe->parameters);
216 giraffe_error_clear();
228giscience(cpl_parameterlist* config, cpl_frameset* set)
231 const cxchar*
const _id =
"giscience";
234 const cxchar* filename = NULL;
237 cxbool calsim = FALSE;
239 cxbool gensdp = FALSE;
240 cxint nassoc_keys = 0;
241 cxchar* flux_filename = NULL;
242 cxchar* err_filename = NULL;
249 cxdouble exptime = 0.;
251 cx_slist* slist = NULL;
253 cpl_propertylist* properties = NULL;
255 cpl_matrix* biasareas = NULL;
257 cpl_frame* science_frame = NULL;
258 cpl_frame* mbias_frame = NULL;
259 cpl_frame* mdark_frame = NULL;
260 cpl_frame* bpixel_frame = NULL;
261 cpl_frame* slight_frame = NULL;
262 cpl_frame* locy_frame = NULL;
263 cpl_frame* locw_frame = NULL;
264 cpl_frame* psfdata_frame = NULL;
265 cpl_frame* grating_frame = NULL;
266 cpl_frame* linemask_frame = NULL;
267 cpl_frame* slit_frame = NULL;
268 cpl_frame* wcal_frame = NULL;
269 cpl_frame* rscience_frame = NULL;
270 cpl_frame* sext_frame = NULL;
271 cpl_frame* rbin_frame = NULL;
273 cpl_parameter* p = NULL;
275 GiImage* mbias = NULL;
276 GiImage* mdark = NULL;
277 GiImage* bpixel = NULL;
278 GiImage* slight = NULL;
279 GiImage* sscience = NULL;
280 GiImage* rscience = NULL;
282 GiTable* fibers = NULL;
283 GiTable* slitgeometry = NULL;
284 GiTable* grating = NULL;
285 GiTable* wcalcoeff = NULL;
287 GiLocalization* localization = NULL;
288 GiExtraction* extraction = NULL;
289 GiRebinning* rebinning = NULL;
291 GiBiasConfig* bias_config = NULL;
292 GiExtractConfig* extract_config = NULL;
293 GiFlatConfig* flat_config = NULL;
294 GiRebinConfig* rebin_config = NULL;
296 GiInstrumentMode mode;
298 GiRecipeInfo info = {(cxchar*)_id, 1, NULL, config};
300 GiGroupInfo groups[] = {
301 {GIFRAME_SCIENCE, CPL_FRAME_GROUP_RAW},
302 {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
303 {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
304 {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
305 {GIFRAME_FIBER_FLAT_EXTSPECTRA, CPL_FRAME_GROUP_CALIB},
306 {GIFRAME_FIBER_FLAT_EXTERRORS, CPL_FRAME_GROUP_CALIB},
307 {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
308 {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
309 {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
310 {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
311 {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
312 {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
313 {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
314 {GIFRAME_LINE_MASK, CPL_FRAME_GROUP_CALIB},
315 {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
316 {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
317 {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
318 {NULL, CPL_FRAME_GROUP_NONE}
324 cpl_msg_error(_id,
"Invalid parameter list! Aborting ...");
329 cpl_msg_error(_id,
"Invalid frame set! Aborting ...");
333 status = giraffe_frameset_set_groups(set, groups);
336 cpl_msg_error(_id,
"Setting frame group information failed!");
345 nscience = cpl_frameset_count_tags(set, GIFRAME_SCIENCE);
348 cpl_msg_error(_id,
"Too few (%ld) raw frames (%s) present in "
349 "frame set! Aborting ...", nscience, GIFRAME_SCIENCE);
353 locy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
355 if (locy_frame == NULL) {
357 locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
359 if (locy_frame == NULL) {
360 cpl_msg_info(_id,
"No master localization (centroid position) "
361 "present in frame set. Aborting ...");
367 locw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
369 if (locw_frame == NULL) {
371 locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
373 if (locw_frame == NULL) {
374 cpl_msg_info(_id,
"No master localization (spectrum width) "
375 "present in frame set. Aborting ...");
381 grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
383 if (!grating_frame) {
384 cpl_msg_error(_id,
"No grating data present in frame set. "
392 cpl_msg_error(_id,
"No slit geometry present in frame set. "
397 wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
400 cpl_msg_error(_id,
"No dispersion solution present in frame set. "
405 linemask_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
407 if (!linemask_frame) {
408 cpl_msg_warning(_id,
"No reference line mask present in frame set.");
411 bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
414 cpl_msg_info(_id,
"No bad pixel map present in frame set.");
417 mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
420 cpl_msg_info(_id,
"No master bias present in frame set.");
423 mdark_frame = cpl_frameset_find(set, GIFRAME_DARK_MASTER);
426 cpl_msg_info(_id,
"No master dark present in frame set.");
429 slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
432 cpl_msg_info(_id,
"No scattered light model present in frame set.");
435 psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
437 if (!psfdata_frame) {
438 cpl_msg_info(_id,
"No PSF profile parameters present in frame set.");
446 slist = cx_slist_new();
448 science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
450 for (i = 0; i < nscience; i++) {
452 filename = cpl_frame_get_filename(science_frame);
460 cpl_msg_error(_id,
"Cannot load raw science frame from '%s'. "
461 "Aborting ...", filename);
468 cx_slist_push_back(slist, raw);
470 science_frame = cpl_frameset_find(set, NULL);
474 nscience = (cxint)cx_slist_size(slist);
475 sscience = cx_slist_pop_front(slist);
478 cx_assert(properties != NULL);
480 cpl_errorstate tempes = cpl_errorstate_get();
484 double *scipix = cpl_image_get_data_double(sciim);
485 if (scipix != NULL) {
486 const size_t nxy = (size_t)(cpl_image_get_size_x(sciim) *
487 cpl_image_get_size_y(sciim));
490 for (scii = 0; scii < nxy; scii++) {
491 if (scipix[scii] > saturation)
496 cpl_errorstate_set(tempes);
499 cpl_propertylist_update_int(properties, GIALIAS_QCNSATSCI, nsaturated);
508 cpl_msg_info(_id,
"Averaging science frames ...");
510 exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
512 for (i = 1; i < nscience; i++) {
514 cpl_propertylist* _properties;
516 GiImage* science = cx_slist_pop_front(slist);
523 cx_assert(_properties != NULL);
525 exptime += cpl_propertylist_get_double(_properties, GIALIAS_EXPTIME);
534 cx_assert(cx_slist_empty(slist));
535 cx_slist_delete(slist);
545 cpl_msg_info(_id,
"Updating stacked science image properties ...");
547 cpl_propertylist_set_double(properties, GIALIAS_EXPTIME,
550 cpl_propertylist_append_double(properties, GIALIAS_EXPTTOT, exptime);
551 cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
552 "Total exposure time of all frames "
555 cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
559 cpl_propertylist_append_int(properties, GIALIAS_DATANCOM, nscience);
560 cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM,
561 "Number of combined frames");
575 if (bias_config->method == GIBIAS_METHOD_MASTER ||
576 bias_config->method == GIBIAS_METHOD_ZMASTER) {
579 cpl_msg_error(_id,
"Missing master bias frame! Selected bias "
580 "removal method requires a master bias frame!");
588 filename = cpl_frame_get_filename(mbias_frame);
595 cpl_msg_error(_id,
"Cannot load master bias from '%s'. "
596 "Aborting ...", filename);
613 filename = cpl_frame_get_filename(bpixel_frame);
620 cpl_msg_error(_id,
"Cannot load bad pixel map from '%s'. "
621 "Aborting ...", filename);
662 cpl_msg_error(_id,
"Bias removal failed. Aborting ...");
667 if (bpixel != NULL) {
683 GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
686 cpl_msg_info(_id,
"Correcting for dark current ...");
688 filename = cpl_frame_get_filename(mdark_frame);
694 cpl_msg_error(_id,
"Cannot load master dark from '%s'. "
695 "Aborting ...", filename);
700 if (bpixel != NULL) {
712 cpl_msg_error(_id,
"Dark subtraction failed! Aborting ...");
720 if (bpixel != NULL) {
739 cpl_msg_info(_id,
"Writing pre-processed science image ...");
744 GIFRAME_SCIENCE_REDUCED,
745 CPL_FRAME_LEVEL_INTERMEDIATE,
748 if (rscience_frame == NULL) {
749 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
756 cpl_frameset_insert(set, rscience_frame);
763 science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
765 cpl_msg_info(_id,
"Building fiber setup for frame '%s'.",
766 cpl_frame_get_filename(science_frame));
771 cpl_msg_error(_id,
"Cannot create fiber setup for frame '%s'! "
772 "Aborting ...", cpl_frame_get_filename(science_frame));
785 cpl_msg_info(_id,
"Fiber reference setup taken from localization "
786 "frame '%s'.", cpl_frame_get_filename(locy_frame));
793 localization = giraffe_localization_new();
795 filename = cpl_frame_get_filename(locy_frame);
802 cpl_msg_error(_id,
"Cannot load localization (centroid "
803 "position) frame from '%s'. Aborting ...",
806 giraffe_localization_destroy(localization);
820 filename = cpl_frame_get_filename(locw_frame);
827 cpl_msg_error(_id,
"Cannot load localization (spectrum width) "
828 "frame from '%s'. Aborting ...", filename);
830 giraffe_localization_destroy(localization);
850 filename = cpl_frame_get_filename(slight_frame);
857 cpl_msg_error(_id,
"Cannot load scattered light model from '%s'. "
858 "Aborting ...", filename);
862 giraffe_localization_destroy(localization);
881 if ((extract_config->emethod == GIEXTRACT_OPTIMAL) ||
882 (extract_config->emethod == GIEXTRACT_HORNE)) {
884 if (psfdata_frame == NULL) {
886 const cxchar* emethod =
"Optimal";
888 if (extract_config->emethod == GIEXTRACT_HORNE) {
892 cpl_msg_error(_id,
"%s spectrum extraction requires PSF "
893 "profile data. Aborting ...", emethod);
896 extract_config = NULL;
898 if (slight != NULL) {
903 giraffe_localization_destroy(localization);
922 filename = cpl_frame_get_filename(psfdata_frame);
925 localization->psf = giraffe_psfdata_new();
926 status = giraffe_psfdata_load(localization->psf, filename);
929 cpl_msg_error(_id,
"Cannot load PSF profile data frame from "
930 "'%s'. Aborting ...", filename);
933 extract_config = NULL;
935 if (slight != NULL) {
940 giraffe_localization_destroy(localization);
963 extraction = giraffe_extraction_new();
966 localization, bpixel, slight,
970 cpl_msg_error(_id,
"Spectrum extraction failed! Aborting ...");
972 giraffe_extraction_destroy(extraction);
977 giraffe_localization_destroy(localization);
1010 if (flat_config->load == TRUE) {
1012 cpl_frame* flat_frame = NULL;
1014 GiImage* flat = NULL;
1017 flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTSPECTRA);
1019 if (flat_frame == NULL) {
1020 cpl_msg_error(_id,
"Missing flat field spectra frame!");
1024 giraffe_extraction_destroy(extraction);
1025 giraffe_localization_destroy(localization);
1035 filename = cpl_frame_get_filename(flat_frame);
1041 cpl_msg_error(_id,
"Cannot load flat field spectra from '%s'. "
1042 "Aborting ...", filename);
1048 giraffe_extraction_destroy(extraction);
1049 giraffe_localization_destroy(localization);
1059 if (flat_config->apply == TRUE) {
1061 GiImage* errors = NULL;
1064 flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTERRORS);
1066 if (flat_frame == NULL) {
1067 cpl_msg_warning(_id,
"Missing flat field spectra errors "
1072 filename = cpl_frame_get_filename(flat_frame);
1078 cpl_msg_error(_id,
"Cannot load flat field spectra "
1079 "errors from '%s'. Aborting ...",
1087 giraffe_extraction_destroy(extraction);
1088 giraffe_localization_destroy(localization);
1100 cpl_msg_info(_id,
"Applying flat field correction ...");
1106 cpl_msg_error(_id,
"Flat field correction failed! "
1114 giraffe_extraction_destroy(extraction);
1115 giraffe_localization_destroy(localization);
1130 if (flat_config->transmission == TRUE) {
1132 const cxchar* _filename = cpl_frame_get_filename(flat_frame);
1134 GiTable* _fibers = NULL;
1137 cpl_msg_info(_id,
"Loading fiber setup for frame '%s'.",
1143 cpl_msg_error(_id,
"Cannot create fiber setup for "
1144 "frame '%s'! Aborting ...", _filename);
1150 giraffe_extraction_destroy(extraction);
1151 giraffe_localization_destroy(localization);
1161 cpl_msg_info(_id,
"Applying relative fiber transmission "
1164 status = giraffe_transmission_setup(fibers, _fibers);
1168 status = giraffe_transmission_apply(extraction, fibers);
1173 cpl_msg_error(_id,
"Relative transmission correction failed! "
1180 giraffe_extraction_destroy(extraction);
1181 giraffe_localization_destroy(localization);
1206 cpl_msg_info(_id,
"Writing extracted spectra ...");
1214 giraffe_qc_update_sci_props(extprop, fibers);
1218 GIFRAME_SCIENCE_EXTSPECTRA,
1219 CPL_FRAME_LEVEL_FINAL,
1222 if (sext_frame == NULL) {
1223 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1225 giraffe_extraction_destroy(extraction);
1226 giraffe_localization_destroy(localization);
1239 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1240 "Aborting ...", cpl_frame_get_filename(sext_frame));
1242 cpl_frame_delete(sext_frame);
1244 giraffe_extraction_destroy(extraction);
1245 giraffe_localization_destroy(localization);
1255 cpl_frameset_insert(set, sext_frame);
1263 giraffe_qc_update_sci_props(exterrprop, fibers);
1269 GIFRAME_SCIENCE_EXTERRORS,
1270 CPL_FRAME_LEVEL_FINAL,
1273 if (sext_frame == NULL) {
1274 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1276 giraffe_extraction_destroy(extraction);
1277 giraffe_localization_destroy(localization);
1290 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1291 "Aborting ...", cpl_frame_get_filename(sext_frame));
1293 cpl_frame_delete(sext_frame);
1295 giraffe_extraction_destroy(extraction);
1296 giraffe_localization_destroy(localization);
1306 cpl_frameset_insert(set, sext_frame);
1310 if (extraction->npixels != NULL) {
1314 giraffe_qc_update_sci_props(extpixprop, fibers);
1319 GIFRAME_SCIENCE_EXTPIXELS,
1320 CPL_FRAME_LEVEL_FINAL,
1323 if (sext_frame == NULL) {
1324 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1326 giraffe_extraction_destroy(extraction);
1327 giraffe_localization_destroy(localization);
1340 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1341 "Aborting ...", cpl_frame_get_filename(sext_frame));
1343 cpl_frame_delete(sext_frame);
1345 giraffe_extraction_destroy(extraction);
1346 giraffe_localization_destroy(localization);
1356 cpl_frameset_insert(set, sext_frame);
1365 GIFRAME_SCIENCE_EXTTRACE,
1366 CPL_FRAME_LEVEL_FINAL,
1369 if (sext_frame == NULL) {
1370 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1372 giraffe_extraction_destroy(extraction);
1373 giraffe_localization_destroy(localization);
1386 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1387 "Aborting ...", cpl_frame_get_filename(sext_frame));
1389 cpl_frame_delete(sext_frame);
1391 giraffe_extraction_destroy(extraction);
1392 giraffe_localization_destroy(localization);
1402 cpl_frameset_insert(set, sext_frame);
1406 if (extraction->model != NULL) {
1411 GIFRAME_SCIENCE_EXTMODEL,
1412 CPL_FRAME_LEVEL_FINAL,
1415 if (sext_frame == NULL) {
1416 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1418 giraffe_extraction_destroy(extraction);
1419 giraffe_localization_destroy(localization);
1432 cpl_msg_error(_id,
"Cannot attach fiber setup to local file '%s'! "
1433 "Aborting ...", cpl_frame_get_filename(sext_frame));
1435 cpl_frame_delete(sext_frame);
1437 giraffe_extraction_destroy(extraction);
1438 giraffe_localization_destroy(localization);
1448 cpl_frameset_insert(set, sext_frame);
1458 filename = (cxchar *)cpl_frame_get_filename(wcal_frame);
1460 tempes = cpl_errorstate_get();
1462 const char qcdltcopy[] =
"^(ESO INS TEMP53 VAL|MJD-OBS)$";
1463 cpl_propertylist * qcdltlist = cpl_propertylist_load_regexp(filename, 0,
1465 cpl_errorstate_set(tempes);
1471 cpl_msg_error(_id,
"Cannot load dispersion solution from "
1472 "'%s'. Aborting ...", filename);
1474 giraffe_extraction_destroy(extraction);
1475 giraffe_localization_destroy(localization);
1490 filename = (cxchar *)cpl_frame_get_filename(grating_frame);
1498 cpl_msg_error(_id,
"Cannot load grating data from '%s'. "
1499 "Aborting ...", filename);
1501 giraffe_extraction_destroy(extraction);
1502 giraffe_localization_destroy(localization);
1518 filename = (cxchar *)cpl_frame_get_filename(slit_frame);
1522 if (slitgeometry == NULL) {
1523 cpl_msg_error(_id,
"Cannot load slit geometry data from '%s'. "
1524 "Aborting ...", filename);
1528 giraffe_extraction_destroy(extraction);
1529 giraffe_localization_destroy(localization);
1547 cpl_msg_error(_id,
"Slit geometry data from '%s' is not "
1548 "applicable for current fiber setup! "
1549 "Aborting ...", filename);
1554 giraffe_extraction_destroy(extraction);
1555 giraffe_localization_destroy(localization);
1573 cpl_msg_info(_id,
"Spectrum rebinning");
1580 localization, grating, slitgeometry,
1581 wcalcoeff, rebin_config);
1584 cpl_msg_error(_id,
"Rebinning of science spectra failed! Aborting...");
1588 giraffe_extraction_destroy(extraction);
1589 giraffe_localization_destroy(localization);
1610 p = cpl_parameterlist_find(config,
"giraffe.siwc.apply");
1611 cx_assert(p != NULL);
1613 siwc = cpl_parameter_get_bool(p);
1617 cx_assert(properties != NULL);
1620 if (cpl_propertylist_has(properties, GIALIAS_STSCTAL) == TRUE) {
1621 calsim = cpl_propertylist_get_bool(properties, GIALIAS_STSCTAL);
1625 if ((siwc == TRUE) && (calsim == TRUE) && (linemask_frame != NULL)) {
1634 if (siwc_config == NULL) {
1641 giraffe_extraction_destroy(extraction);
1642 giraffe_localization_destroy(localization);
1656 filename = cpl_frame_get_filename(linemask_frame);
1661 cpl_msg_error(_id,
"Cannot load line reference mask from '%s'. "
1662 "Aborting ...", filename);
1672 giraffe_extraction_destroy(extraction);
1673 giraffe_localization_destroy(localization);
1689 linemask, siwc_config);
1692 cpl_msg_error(_id,
"Applying simultaneous wavelength "
1693 "calibration correction failed! Aborting...");
1703 giraffe_extraction_destroy(extraction);
1704 giraffe_localization_destroy(localization);
1728 localization, grating, slitgeometry,
1729 wcalcoeff, rebin_config);
1732 cpl_msg_error(_id,
"Rebinning of science spectra failed! "
1737 giraffe_extraction_destroy(extraction);
1738 giraffe_localization_destroy(localization);
1754 giraffe_extraction_destroy(extraction);
1757 giraffe_localization_destroy(localization);
1758 localization = NULL;
1761 rebin_config = NULL;
1778 cpl_msg_warning(_id,
"Missing observation time properties! "
1779 "Barycentric correction computation "
1786 cpl_msg_warning(_id,
"Missing telescope location properties! "
1787 "Barycentric correction computation "
1794 cpl_msg_warning(_id,
"Object positions are not available "
1795 "Barycentric correction computation "
1802 cpl_msg_error(_id,
"Barycentric correction computation "
1803 "failed! Aborting...");
1830 tempes = cpl_errorstate_get();
1842 double dlttemp = cpl_propertylist_get_double(rbprop,
"ESO INS TEMP53 VAL");
1843 dlttemp -= cpl_propertylist_get_double(qcdltlist,
"ESO INS TEMP53 VAL");
1845 double dltdate = cpl_propertylist_get_double(rbprop,
"MJD-OBS");
1846 dltdate -= cpl_propertylist_get_double(qcdltlist,
"MJD-OBS");
1848 cpl_propertylist_append_double(rbprop, GIALIAS_QCDLTTEMP, dlttemp);
1849 cpl_propertylist_append_double(rbprop, GIALIAS_QCDLTTIME, dltdate);
1858 int bitmask = pow(2, nbits) - 1;
1860 if (-1.5 < dlttemp && dlttemp < 1.5) {
1864 if (-1.0 < dltdate && dltdate < 1.0) {
1868 if (nsaturated < 1000){
1872 if (cpl_propertylist_get_int(rbprop, GIALIAS_QCNFIBSCI) >= 10) {
1876 if (cpl_propertylist_get_int(rbprop, GIALIAS_QCNFIBSKY) >= 1) {
1885 char bitmaskstr[nbits + 1];
1887 for (
int biti = 0; biti < nbits; biti++) {
1888 bitmaskstr[biti] = (bitmask & (1 << biti)) ?
'1' :
'0';
1890 bitmaskstr[nbits] =
'\0';
1892 cpl_propertylist_append_string(rbprop, GIALIAS_QCFLAG, bitmaskstr);
1894 cpl_errorstate_set(tempes);
1899 GIFRAME_SCIENCE_RBNSPECTRA,
1900 CPL_FRAME_LEVEL_FINAL,
1903 if (rbin_frame == NULL) {
1904 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1920 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
1921 "file '%s'! Aborting ...",
1922 cpl_frame_get_filename(rbin_frame));
1931 cpl_frame_delete(rbin_frame);
1936 cpl_frameset_insert(set, rbin_frame);
1937 flux_filename = cpl_strdup(cpl_frame_get_filename(rbin_frame));
1941 tempes = cpl_errorstate_get();
1953 cpl_propertylist_append_double(rberrprop, GIALIAS_QCDLTTEMP, dlttemp);
1954 cpl_propertylist_append_double(rberrprop, GIALIAS_QCDLTTIME, dltdate);
1956 cpl_propertylist_append_string(rberrprop, GIALIAS_QCFLAG, bitmaskstr);
1960 cpl_errorstate_set(tempes);
1963 GIFRAME_SCIENCE_RBNERRORS,
1964 CPL_FRAME_LEVEL_FINAL,
1967 if (rbin_frame == NULL) {
1968 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
1977 cpl_free(flux_filename);
1985 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
1986 "file '%s'! Aborting ...",
1987 cpl_frame_get_filename(rbin_frame));
1997 cpl_frame_delete(rbin_frame);
1998 cpl_free(flux_filename);
2003 cpl_frameset_insert(set, rbin_frame);
2004 err_filename = cpl_strdup(cpl_frame_get_filename(rbin_frame));
2012 p = cpl_parameterlist_find(config,
"giraffe.sdp.format.generate");
2013 cx_assert(p != NULL);
2014 gensdp = cpl_parameter_get_bool(p);
2015 p = cpl_parameterlist_find(config,
"giraffe.sdp.nassoc.keys");
2016 cx_assert(p != NULL);
2017 nassoc_keys = cpl_parameter_get_int(p);
2020 if (mode == GIMODE_MEDUSA) {
2021 status = _giraffe_make_sdp_spectra(flux_filename, err_filename,
2022 nassoc_keys, set, config, _id);
2024 cpl_msg_error(_id,
"Failed to generate spectra in Science Data"
2025 " Product format.");
2035 cpl_free(flux_filename);
2036 cpl_free(err_filename);
2041 cpl_msg_warning(_id,
"Requested to generate SDP 1D spectra, but"
2042 " this is currently only supported for the MEDUSA"
2043 " mode. Skipping SDP generation.");
2046 cpl_free(flux_filename);
2047 cpl_free(err_filename);
2054 if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
2056 cpl_frame* rimg_frame = NULL;
2058 GiFieldOfView* fov = NULL;
2060 GiFieldOfViewConfig* fov_config = NULL;
2062 GiFieldOfViewCubeFormat cube_format = GIFOV_FORMAT_ESO3D;
2067 cube_format = fov_config->format;
2070 cpl_msg_info(_id,
"Reconstructing image and data cube from rebinned "
2076 slitgeometry, fov_config);
2081 cpl_msg_warning(_id,
"No reconstructed image was built. "
2082 "Fiber list has no fiber position "
2086 cpl_msg_error(_id,
"Image reconstruction failed! Aborting...");
2116 GIFRAME_SCIENCE_RCSPECTRA,
2117 CPL_FRAME_LEVEL_FINAL,
2120 if (rimg_frame == NULL) {
2121 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2135 cpl_frameset_insert(set, rimg_frame);
2143 GIFRAME_SCIENCE_RCERRORS,
2144 CPL_FRAME_LEVEL_FINAL,
2147 if (rimg_frame == NULL) {
2148 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2162 cpl_frameset_insert(set, rimg_frame);
2167 if (cube_format == GIFOV_FORMAT_SINGLE) {
2171 if (fov->cubes.spectra != NULL) {
2173 cxint component = 0;
2179 properties = cpl_propertylist_duplicate(properties);
2184 CPL_FRAME_LEVEL_FINAL,
2190 cpl_propertylist_delete(properties);
2193 if (rimg_frame == NULL) {
2194 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2206 slitgeometry = NULL;
2220 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
2221 "file '%s'! Aborting ...",
2222 cpl_frame_get_filename(rimg_frame));
2224 cpl_frame_delete(rimg_frame);
2236 slitgeometry = NULL;
2247 cpl_frameset_insert(set, rimg_frame);
2253 if (fov->cubes.errors != NULL) {
2255 cxint component = 1;
2261 properties = cpl_propertylist_duplicate(properties);
2266 CPL_FRAME_LEVEL_FINAL,
2272 cpl_propertylist_delete(properties);
2275 if (rimg_frame == NULL) {
2276 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2288 slitgeometry = NULL;
2302 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
2303 "file '%s'! Aborting ...",
2304 cpl_frame_get_filename(rimg_frame));
2306 cpl_frame_delete(rimg_frame);
2318 slitgeometry = NULL;
2329 cpl_frameset_insert(set, rimg_frame);
2340 properties = cpl_propertylist_duplicate(properties);
2345 CPL_FRAME_LEVEL_FINAL,
2351 cpl_propertylist_delete(properties);
2354 if (rimg_frame == NULL) {
2355 cpl_msg_error(_id,
"Cannot create local file! Aborting ...");
2367 slitgeometry = NULL;
2381 cpl_msg_error(_id,
"Cannot attach fiber setup to local "
2382 "file '%s'! Aborting ...",
2383 cpl_frame_get_filename(rimg_frame));
2385 cpl_frame_delete(rimg_frame);
2397 slitgeometry = NULL;
2408 cpl_frameset_insert(set, rimg_frame);
2441cpl_plugin_get_info(cpl_pluginlist* list)
2444 cpl_recipe* recipe = cx_calloc(1,
sizeof *recipe);
2445 cpl_plugin* plugin = &recipe->interface;
2448 cpl_plugin_init(plugin,
2450 GIRAFFE_BINARY_VERSION,
2451 CPL_PLUGIN_TYPE_RECIPE,
2453 "Process a science observation.",
2454 "For detailed information please refer to the "
2455 "GIRAFFE pipeline user manual.\nIt is available at "
2456 "http://www.eso.org/pipelines.",
2464 cpl_pluginlist_append(list, plugin);
2473typedef struct _giraffe_lut_entry {
2474 const char* expmode;
2489static cpl_boolean _giraffe_lut_is_sorted(
const giraffe_lut_entry* lut,
2493 if (nentries < 2)
return CPL_TRUE;
2494 for (i = 0; i < nentries - 1; ++i) {
2495 if (strcmp(lut[i].expmode, lut[i+1].expmode) >= 0) {
2512static const giraffe_lut_entry* _giraffe_find_lut_entry(
const char* expmode)
2514 static giraffe_lut_entry lut[] = {
2519 {
"H379.", 25000, 0.2250, 20.0, 60},
2520 {
"H379.0", 25000, 0.2250, 20.0, 60},
2521 {
"H395.8", 22000, 0.1913, 21.1, 53},
2522 {
"H412.4", 30000, 0.1326, 22.3, 48},
2523 {
"H429.7", 23000, 0.1471, 23.5, 67},
2524 {
"H447.1", 20000, 0.0906, 24.9, 63},
2525 {
"H447.1A", 20000, 0.0906, 24.9, 63},
2526 {
"H447.1B", 31000, 0.1297, 23.5, 58},
2527 {
"H465.6", 23000, 0.1573, 22.4, 57},
2528 {
"H484.5", 19000, 0.1175, 23.5, 57},
2529 {
"H484.5A", 19000, 0.1175, 23.5, 57},
2530 {
"H484.5B", 33000, 0.0907, 24.6, 61},
2531 {
"H504.8", 22000, 0.1726, 25.2, 56},
2532 {
"H525.8", 17000, 0.1397, 25.8, 49},
2533 {
"H525.8A", 17000, 0.1397, 25.8, 49},
2534 {
"H525.8B", 29000, 0.2014, 26.4, 44},
2535 {
"H548.8", 20000, 0.2389, 26.9, 51},
2536 {
"H572.8", 27000, 0.1844, 27.5, 49},
2537 {
"H599.3", 18000, 0.1683, 28.3, 57},
2538 {
"H627.3", 24000, 0.1268, 29.0, 50},
2539 {
"H651.5", 17000, 0.1185, 30.0, 42},
2540 {
"H651.5A", 17000, 0.1185, 30.0, 42},
2541 {
"H651.5B", 35000, 0.1253, 31.5, 37},
2542 {
"H665.", 17000, 0.2076, 33.0, 45},
2543 {
"H665.0", 17000, 0.2076, 33.0, 45},
2544 {
"H679.7", 19000, 0.1621, 34.5, 51},
2545 {
"H710.5", 25000, 0.1495, 36.0, 48},
2546 {
"H737.", 16000, 0.1456, 37.5, 47},
2547 {
"H737.0", 16000, 0.1456, 37.5, 47},
2548 {
"H737.0A", 16000, 0.1456, 37.5, 47},
2549 {
"H737.0B", 35000, 0.1147, 39.5, 40},
2550 {
"H769.1", 19000, 0.2811, 41.8, 35},
2551 {
"H805.3", 14000, 0.2662, 42.4, 39},
2552 {
"H805.3A", 14000, 0.2662, 42.4, 39},
2553 {
"H805.3B", 25000, 0.2342, 42.9, 28},
2554 {
"H836.6", 16000, 0.2032, 43.3, 21},
2555 {
"H836.6A", 16000, 0.2032, 43.3, 21},
2556 {
"H836.6B", 34000, 0.1073, 43.7, 14},
2557 {
"H875.7", 18000, 0.2026, 44.3, 29},
2558 {
"H920.5", 12000, 0.1568, 44.9, 32},
2559 {
"H920.5A", 12000, 0.1568, 44.9, 32},
2560 {
"H920.5B", 24000, 0.2531, 45.9, 28},
2561 {
"L385.7", 7500, 0.3358, 58.0, 43},
2562 {
"L427.2", 6300, 0.2152, 61.1, 62},
2563 {
"L479.7", 7500, 0.1554, 71.0, 61},
2564 {
"L543.1", 5800, 0.2065, 82.1, 58},
2565 {
"L614.2", 6600, 0.1803, 79.0, 51},
2566 {
"L682.2", 8100, 0.1843, 74.0, 50},
2567 {
"L773.4", 5400, 0.1617, 94.0, 44},
2568 {
"L881.7", 6600, 0.1614, 119.0, 29}
2570 static const size_t nentries =
sizeof(lut) /
sizeof(giraffe_lut_entry);
2572 int high = (int)nentries - 1;
2574 assert(_giraffe_lut_is_sorted(lut, nentries));
2575 assert(expmode != NULL);
2579 int mid = (low + high) >> 1;
2580 int result = strcmp(expmode, lut[mid].expmode);
2583 }
else if (result < 0) {
2588 }
while (high >= low);
2598static double _giraffe_lookup_specres(
const char* expmode)
2600 const giraffe_lut_entry* entry = _giraffe_find_lut_entry(expmode);
2601 if (entry == NULL)
return NAN;
2602 return entry->specres;
2611static double _giraffe_lookup_lamrms(
const char* expmode)
2613 const giraffe_lut_entry* entry = _giraffe_find_lut_entry(expmode);
2614 if (entry == NULL)
return NAN;
2615 if (isnan(entry->lamrms) || isnan(entry->spec_length))
return NAN;
2616 return entry->lamrms * entry->spec_length / 4100.;
2625static double _giraffe_lookup_lamnlin(
const char* expmode)
2627 const giraffe_lut_entry* entry = _giraffe_find_lut_entry(expmode);
2628 if (entry == NULL)
return -1;
2629 return entry->lamnlin;
2643static cpl_type _giraffe_calc_wave_type(
double crval2,
double crpix2,
2644 double cdelt2, cpl_size naxis2)
2646 static const double errfrac = 0.02;
2647 static const double single_precision_digits = 7;
2648 double lo = (1.0 - crpix2) * cdelt2;
2649 double hi = ((double)naxis2 - crpix2) * cdelt2;
2650 double maxwave = crval2 + (hi > lo ? hi : lo);
2651 double binfrac = (maxwave != 0.0) ? fabs(cdelt2 / maxwave) : 0.0;
2652 if (binfrac * errfrac < pow(10, -single_precision_digits)) {
2653 return CPL_TYPE_DOUBLE;
2655 return CPL_TYPE_FLOAT;
2669static cpl_boolean _giraffe_ancillary_data_available(
const char* filename,
2670 const GiTable* fibertable)
2674 const char** spectypes = NULL;
2676 assert(filename != NULL);
2678 cpl_error_ensure(tbl != NULL, cpl_error_get_code(),
return CPL_FALSE,
2679 "The fiber table is not available for '%s'.", filename);
2681 spectypes = cpl_table_get_data_string_const(tbl, GIALIAS_COLUMN_TYPE);
2682 cpl_error_ensure(spectypes != NULL, cpl_error_get_code(),
return CPL_FALSE,
2683 "Could not fetch the '%s' column from the fiber setup"
2684 " table in '%s'.", GIALIAS_COLUMN_TYPE, filename);
2690 for (i = 0; i < cpl_table_get_nrow(tbl); ++i) {
2691 if ((spectypes[i][0] !=
'\0') && (strcmp(spectypes[i],
"M") != 0)) {
2718static cpl_error_code _giraffe_make_ancillary_file(cpl_frameset* allframes,
2719 const char* outputfile,
2720 const char* infilename,
2721 const GiImage* fluximage,
2722 const GiTable* fibertable)
2724 cpl_error_code error = CPL_ERROR_NONE;
2726 cpl_frame* frame = NULL;
2728 GiImage* image = NULL;
2730 cpl_image* img = NULL;
2732 cpl_image* subimg = NULL;
2733 cpl_propertylist* comments = NULL;
2735 int* indices = NULL;
2736 const char** spectypes = NULL;
2738 assert(allframes != NULL);
2739 assert(outputfile != NULL);
2740 assert(infilename != NULL);
2742 cpl_error_ensure(srcimg != NULL && table != NULL && tbl != NULL,
2743 cpl_error_get_code(),
goto cleanup,
2744 "The image or table are not available for '%s'.",
2748 frame = cpl_frame_new();
2749 error |= cpl_frame_set_filename(frame, outputfile);
2750 error |= cpl_frame_set_tag(frame, GIALIAS_ASSO_PROCATG_VALUE);
2751 error |= cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
2752 error |= cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
2753 error |= cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
2754 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2755 "Failed to setup a new output frame for '%s'.",
2764 error |= cpl_table_unselect_all(tbl);
2765 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2766 "Failed to unselect all entries in fiber setup table from"
2767 " '%s'.", infilename);
2768 cpl_table_or_selected_string(tbl, GIALIAS_COLUMN_TYPE, CPL_NOT_EQUAL_TO,
2770 cpl_table_and_selected_int(tbl, GIALIAS_COLUMN_RP, CPL_NOT_EQUAL_TO, -1);
2771 cpl_table_not_selected(tbl);
2772 error |= cpl_table_erase_selected(tbl);
2773 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2774 "Failed to erase selected entries in fiber setup table"
2775 " from '%s'.", infilename);
2779 ny = cpl_image_get_size_y(srcimg);
2781 cpl_table_get_nrow(tbl), ny);
2785 cpl_error_ensure(image != NULL && img != NULL && retcode == 0,
2786 cpl_error_get_code(),
goto cleanup,
2787 "Failed to create image for output file '%s'.",
2792 GIALIAS_ASSO_PROCATG_VALUE);
2793 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2794 "Could not update keyword '%s' for output file '%s'.",
2795 GIALIAS_PROCATG, outputfile);
2800 const char* comments_to_remove[] = {
2801 " FITS (Flexible Image Transport System) format is defined in"
2803 " and Astrophysics', volume 376, page 359; bibcode: 2001A&A..."
2808 comments = cpl_propertylist_new();
2809 error |= cpl_propertylist_copy_property_regexp(comments, props,
2811 cpl_propertylist_erase_regexp(props,
"^COMMENT$", 0);
2812 for (ic = 0; ic < cpl_propertylist_get_size(comments); ++ic) {
2813 const char** cmnt_str;
2814 cpl_property* p = cpl_propertylist_get(comments, ic);
2815 for (cmnt_str = comments_to_remove; *cmnt_str != NULL; ++cmnt_str) {
2816 if (strcmp(cpl_property_get_string(p), *cmnt_str) == 0) {
2817 goto dont_add_comment;
2821 error |= cpl_propertylist_append_property(props, p);
2827 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2828 "Failed to cleanup comments in primary HDU for '%s'.",
2830 cpl_propertylist_delete(comments);
2836 indices = cpl_table_get_data_int(tbl, GIALIAS_COLUMN_INDEX);
2837 cpl_error_ensure(indices != NULL, cpl_error_get_code(),
goto cleanup,
2838 "Could not fetch the '%s' column from the fiber setup"
2839 " table in '%s'.", GIALIAS_COLUMN_INDEX, infilename);
2840 for (i = 0; i < cpl_table_get_nrow(tbl); ++i) {
2841 cpl_size oldindex = indices[i];
2842 cpl_size newindex = i+1;
2843 indices[i] = newindex;
2844 subimg = cpl_image_extract(srcimg, oldindex, 1, oldindex, ny);
2845 cpl_error_ensure(subimg != NULL, cpl_error_get_code(),
goto cleanup,
2846 "Could not extract sub image from '%s' at column %"
2847 CPL_SIZE_FORMAT
".", infilename, oldindex);
2848 error |= cpl_image_copy(img, subimg, newindex, 1);
2849 cpl_image_delete(subimg);
2851 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2852 "Could write sub image from '%s' at column %"
2853 CPL_SIZE_FORMAT
" to new image in '%s' at column %"
2854 CPL_SIZE_FORMAT
".", infilename, oldindex, outputfile,
2860 cpl_error_ensure(retcode == 0, cpl_error_get_code(),
goto cleanup,
2861 "Failed to write image to file '%s'.", outputfile);
2863 cpl_error_ensure(retcode == 0, cpl_error_get_code(),
goto cleanup,
2864 "Failed to attach the fiber setup table to file '%s'.",
2870 error |= cpl_frameset_insert(allframes, frame);
2871 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
2872 "Could not add a new frame to the frame list for '%s'.",
2877 return CPL_ERROR_NONE;
2881 cpl_image_delete(subimg);
2884 cpl_frame_delete(frame);
2885 cpl_propertylist_delete(comments);
2886 return cpl_error_get_code();
2896static char* _giraffe_calc_format_string(cpl_size maxfiles)
2898 double ndigits = 1.0;
2902 ndigits = ceil(log10((
double)maxfiles + 1.0));
2904 return cpl_sprintf(
"science_spectrum_%%0%.0f"CPL_SIZE_FORMAT
".fits",
2925static cxint _giraffe_make_sdp_spectra(
const cxchar* flux_filename,
2926 const cxchar* err_filename,
2928 cpl_frameset* allframes,
2929 const cpl_parameterlist* parlist,
2930 const cxchar* recipe_id)
2932 cxint result_code = 1;
2934 cpl_error_code error = CPL_ERROR_NONE;
2935 cpl_errorstate prestate;
2936 const char* ancillary_filename =
"science_ancillary.fits";
2939 GiTable* fibertable = NULL;
2940 const cxchar* fibertable_name = NULL;
2941 irplib_sdp_spectrum* spectrum = NULL;
2942 cpl_propertylist* extrakeys = cpl_propertylist_new();
2943 cpl_propertylist* tablekeys = cpl_propertylist_new();
2944 cpl_propertylist* props;
2945 char* pipe_id = cpl_sprintf(
"%s/%s", PACKAGE, VERSION);
2946 const char* dict_id = PRODUCT_DID;
2947 const cpl_frame* inherit = NULL;
2948 cpl_frameset* usedframes = NULL;
2949 cpl_frameset* rawframes = NULL;
2950 cpl_frameset_iterator* iterator = NULL;
2951 cpl_size nx, ny, i, filecount;
2952 double exptime = NAN;
2953 double mjdobs = NAN;
2954 double mjdend = NAN;
2955 double wavelmin = NAN;
2956 double wavelmax = NAN;
2957 double specbin = NAN;
2958 double crpix2 = NAN;
2959 double crval2 = NAN;
2960 double cdelt2 = NAN;
2961 const char* cunit2 = NULL;
2963 const char* expmode = NULL;
2965 const int* indices = NULL;
2966 const int* fps = NULL;
2967 const char** objects = NULL;
2968 const char** spectypes = NULL;
2969 const double* ras = NULL;
2970 const double* decs = NULL;
2971 const double* gcorr = NULL;
2972 const double* hcorr = NULL;
2973 const double* bcorr = NULL;
2974 char* formatstr = NULL;
2975 char* filename = NULL;
2976 cpl_type wavecoltype;
2977 cpl_array* refwavearray = NULL;
2978 cpl_array* array = NULL;
2979 float* data_float = NULL;
2980 double* data_double = NULL;
2981 cpl_vector* fluximgcol = NULL;
2982 cpl_vector* errimgcol = NULL;
2983 cpl_boolean got_ancillary_data = CPL_FALSE;
2984 cpl_size assoc_key_offset = 1;
2986 double lamrms = NAN;
2987 double specerr = NAN;
2988 double specsye = NAN;
2990 cpl_error_ensure(flux_filename != NULL && err_filename != NULL
2991 && allframes != NULL && parlist != NULL
2992 && recipe_id != NULL, CPL_ERROR_NULL_INPUT,
goto cleanup,
2993 "NULL input parameters.");
2995 error |= cpl_propertylist_append_string(extrakeys, GIALIAS_PROCATG,
2996 GIALIAS_PROCATG_RBNSPEC_IDP);
2997 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_PROCATG,
2998 GIALIAS_PROCATG_COMMENT);
2999 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3000 "Could not set keyword '%s'.", GIALIAS_PROCATG);
3004 cpl_error_ensure(errorcode == 0, cpl_error_get_code(),
goto cleanup,
3005 "Could not load image data in primary HDU from '%s'.",
3008 cpl_error_ensure(errorcode == 0, cpl_error_get_code(),
goto cleanup,
3009 "Could not load image data in primary HDU from '%s'.",
3012 giraffe_error_push();
3014 if (fibertable == NULL) {
3016 GIALIAS_FIBER_SETUP);
3017 fibertable_name = err_filename;
3019 fibertable_name = flux_filename;
3021 cpl_error_ensure(fibertable != NULL, CPL_ERROR_DATA_NOT_FOUND,
goto cleanup,
3022 "Could not load the %s table from either '%s' or '%s'.",
3023 GIALIAS_FIBER_SETUP, flux_filename, err_filename);
3024 giraffe_error_pop();
3031 CPL_ERROR_INCOMPATIBLE_INPUT,
goto cleanup,
3032 "The images in files '%s' and '%s' are not the same size.",
3033 flux_filename, err_filename);
3037 usedframes = cpl_frameset_new();
3038 rawframes = cpl_frameset_new();
3039 iterator = cpl_frameset_iterator_new(allframes);
3041 const cpl_frame* frame = cpl_frameset_iterator_get_const(iterator);
3042 if (frame != NULL) {
3043 switch (cpl_frame_get_group(frame)) {
3044 case CPL_FRAME_GROUP_RAW:
3046 if (inherit == NULL) inherit = frame;
3047 error |= cpl_frameset_insert(rawframes,
3048 cpl_frame_duplicate(frame));
3049 error |= cpl_frameset_insert(usedframes,
3050 cpl_frame_duplicate(frame));
3052 case CPL_FRAME_GROUP_CALIB:
3053 error |= cpl_frameset_insert(usedframes,
3054 cpl_frame_duplicate(frame));
3060 prestate = cpl_errorstate_get();
3061 error |= cpl_frameset_iterator_advance(iterator, 1);
3062 if (error == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
3063 cpl_errorstate_set(prestate);
3065 }
else if (error != CPL_ERROR_NONE) {
3070 cpl_error_ensure(inherit != NULL, CPL_ERROR_DATA_NOT_FOUND,
goto cleanup,
3071 "No raw input frames found.");
3075 prestate = cpl_errorstate_get();
3076 exptime = cpl_propertylist_get_double(props, GIALIAS_EXPTIME);
3077 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
3078 goto cleanup,
"Could not find keyword '%s' in '%s'.",
3079 GIALIAS_EXPTIME, flux_filename);
3080 mjdobs = cpl_propertylist_get_double(props, GIALIAS_MJDOBS);
3081 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
3082 goto cleanup,
"Could not find keyword '%s' in '%s'.",
3083 GIALIAS_MJDOBS, flux_filename);
3085 mjdend = mjdobs + exptime / 86400.;
3088 crpix2 = cpl_propertylist_get_double(props, GIALIAS_CRPIX2);
3089 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
3090 goto cleanup,
"Could not find keyword '%s' in '%s'.",
3091 GIALIAS_CRPIX2, flux_filename);
3092 crval2 = cpl_propertylist_get_double(props, GIALIAS_CRVAL2);
3093 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
3094 goto cleanup,
"Could not find keyword '%s' in '%s'.",
3095 GIALIAS_CRVAL2, flux_filename);
3096 cdelt2 = cpl_propertylist_get_double(props, GIALIAS_CDELT2);
3097 cpl_error_ensure(cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
3098 goto cleanup,
"Could not find keyword '%s' in '%s'.",
3099 GIALIAS_CDELT2, flux_filename);
3100 cunit2 = cpl_propertylist_get_string(props, GIALIAS_CUNIT2);
3101 cpl_error_ensure(cunit2 != NULL, cpl_error_get_code(),
3102 goto cleanup,
"Could not find keyword '%s' in '%s'.",
3103 GIALIAS_CUNIT2, flux_filename);
3105 if (strcmp(cunit2,
"nm") == 0) {
3106 wavelmin = (1.0 - crpix2) * cdelt2 + crval2;
3107 wavelmax = ((double)ny - crpix2) * cdelt2 + crval2;
3108 if (wavelmax < wavelmin) {
3109 double tmp = wavelmin;
3110 wavelmin = wavelmax;
3113 specbin = fabs(cdelt2);
3115 cpl_msg_warning(cpl_func,
"Do not know how to handle keyword %s = '%s'."
3116 " Will not set WAVELMIN, WAVELMAX or SPEC_BIN.",
3117 GIALIAS_CUNIT2, cunit2);
3120 if (cpl_propertylist_has(props, GIALIAS_SETUPNAME)) {
3121 expmode = cpl_propertylist_get_string(props, GIALIAS_SETUPNAME);
3122 cpl_error_ensure(expmode != NULL, cpl_error_get_code(),
goto cleanup,
3123 "Could not fetch the keyword '%s' from '%s'.",
3124 GIALIAS_SETUPNAME, flux_filename);
3125 }
else if (cpl_propertylist_has(props, GIALIAS_GRATNAME)) {
3126 const char* name = cpl_propertylist_get_string(props, GIALIAS_GRATNAME);
3127 cpl_error_ensure(name != NULL, cpl_error_get_code(),
goto cleanup,
3128 "Could not fetch the keyword '%s' from '%s'.",
3129 GIALIAS_GRATNAME, flux_filename);
3130 double wlen = cpl_propertylist_get_double(props, GIALIAS_GRATWLEN);
3131 cpl_error_ensure(cpl_errorstate_is_equal(prestate),
3132 cpl_error_get_code(),
goto cleanup,
3133 "Could not find keyword '%s' in '%s'.",
3134 GIALIAS_GRATWLEN, flux_filename);
3135 strbuf[0] = name[0];
3136 char* numstr = cpl_sprintf(
"%.1f", wlen);
3137 strncpy(strbuf+1, numstr,
sizeof(strbuf)-1);
3139 strbuf[
sizeof(strbuf)-1] =
'\0';
3142 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
3143 "Neither '%s' nor '%s' and '%s' keywords were found in the"
3144 " file '%s'.", GIALIAS_SETUPNAME, GIALIAS_GRATNAME,
3145 GIALIAS_GRATWLEN, flux_filename);
3149 specres = _giraffe_lookup_specres(expmode);
3150 if (isnan(specres)) {
3151 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
3152 "The exposure mode '%s' is invalid or an unknown value."
3153 " Could not lookup the spectral resolution for 'SPEC_RES'.",
3160 if (cpl_propertylist_has(props,
"FILTER")) {
3161 prestate = cpl_errorstate_get();
3162 cpl_propertylist_copy_property(extrakeys, props,
"FILTER");
3163 cpl_property* prop = cpl_propertylist_get_property(extrakeys,
"FILTER");
3164 cpl_property_set_name(prop,
"OFILTER");
3165 cpl_error_ensure(cpl_errorstate_is_equal(prestate),
3166 cpl_error_get_code(),
goto cleanup,
3167 "Could not rename the 'FILTER' keyword.");
3171 prestate = cpl_errorstate_get();
3172 got_ancillary_data = _giraffe_ancillary_data_available(flux_filename,
3174 if (! cpl_errorstate_is_equal(prestate))
goto cleanup;
3175 if (got_ancillary_data) {
3176 error = _giraffe_make_ancillary_file(allframes, ancillary_filename,
3177 flux_filename, fluximage,
3179 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3180 "Failed to write the ancillary file '%s'.",
3181 ancillary_filename);
3185 spectrum = irplib_sdp_spectrum_new();
3186 error = CPL_ERROR_NONE;
3187 error |= irplib_sdp_spectrum_set_origin(spectrum, GIALIAS_ORIGIN_VALUE);
3188 error |= irplib_sdp_spectrum_set_prodlvl(spectrum, GIALIAS_PRODLVL_VALUE);
3189 error |= irplib_sdp_spectrum_copy_dispelem(spectrum,
3190 props, GIALIAS_GRATNAME);
3191 error |= irplib_sdp_spectrum_set_specsys(spectrum, GIALIAS_SPECSYS_VALUE);
3192 error |= irplib_sdp_spectrum_set_extobj(spectrum, GIALIAS_EXT_OBJ_VALUE);
3195 error |= irplib_sdp_spectrum_set_object(spectrum,
"");
3196 error |= irplib_sdp_spectrum_set_ra(spectrum, 0.0);
3197 error |= irplib_sdp_spectrum_set_dec(spectrum, 0.0);
3198 error |= irplib_sdp_spectrum_copy_exptime(spectrum, props, GIALIAS_EXPTIME);
3199 error |= irplib_sdp_spectrum_copy_texptime(spectrum,
3200 props, GIALIAS_EXPTIME);
3201 error |= irplib_sdp_spectrum_copy_mjdobs(spectrum, props, GIALIAS_MJDOBS);
3202 error |= irplib_sdp_spectrum_set_mjdend(spectrum, mjdend);
3203 if (cpl_propertylist_has(props, GIALIAS_TIMESYS)) {
3204 error |= irplib_sdp_spectrum_copy_timesys(spectrum,
3205 props, GIALIAS_TIMESYS);
3207 error |= irplib_sdp_spectrum_copy_progid(spectrum, props, GIALIAS_PROGID);
3208 error |= irplib_sdp_spectrum_copy_obid(spectrum, 1, props, GIALIAS_OBSID);
3209 error |= irplib_sdp_spectrum_set_mepoch(spectrum, GIALIAS_M_EPOCH_VALUE);
3210 error |= irplib_sdp_spectrum_append_prov(spectrum, 1, rawframes);
3211 error |= irplib_sdp_spectrum_copy_procsoft(spectrum,
3212 props, GIALIAS_PROPIPEID);
3213 error |= irplib_sdp_spectrum_copy_obstech(spectrum, props, GIALIAS_PROTECH);
3214 error |= irplib_sdp_spectrum_set_prodcatg(spectrum, GIALIAS_PRODCATG_VALUE);
3215 error |= irplib_sdp_spectrum_set_fluxcal(spectrum, GIALIAS_FLUXCAL_VALUE);
3216 error |= irplib_sdp_spectrum_set_contnorm(spectrum, GIALIAS_CONTNORM_VALUE);
3220 error |= irplib_sdp_spectrum_set_wavelmin(spectrum, 0.0);
3221 error |= irplib_sdp_spectrum_set_wavelmax(spectrum, 0.0);
3222 error |= irplib_sdp_spectrum_set_specbin(spectrum, 0.0);
3223 error |= irplib_sdp_spectrum_set_totflux(spectrum, GIALIAS_TOTFLUX_VALUE);
3224 error |= irplib_sdp_spectrum_set_fluxerr(spectrum, GIALIAS_FLUXERR_VALUE);
3225 error |= irplib_sdp_spectrum_set_ncombine(spectrum,
3226 cpl_frameset_get_size(rawframes));
3227 error |= irplib_sdp_spectrum_set_referenc(spectrum, GIALIAS_REFERENC);
3230 error |= irplib_sdp_spectrum_set_snr(spectrum, 0.0);
3233 if (cpl_propertylist_has(props, GIALIAS_LAMNLIN)) {
3234 error |= irplib_sdp_spectrum_copy_lamnlin(spectrum, props,
3236 lamnlin = irplib_sdp_spectrum_get_lamnlin(spectrum);
3238 lamnlin = _giraffe_lookup_lamnlin(expmode);
3239 if (lamnlin != -1) {
3240 error |= irplib_sdp_spectrum_set_lamnlin(spectrum, lamnlin);
3247 if (cpl_propertylist_has(props, GIALIAS_LAMRMS)) {
3248 error |= irplib_sdp_spectrum_copy_lamrms(spectrum, props,
3250 lamrms = irplib_sdp_spectrum_get_lamrms(spectrum);
3252 lamrms = _giraffe_lookup_lamrms(expmode);
3253 if (! isnan(lamrms)) {
3254 error |= irplib_sdp_spectrum_set_lamrms(spectrum, lamrms);
3268 if (cpl_propertylist_has(props, GIALIAS_SPEC_ERR)) {
3269 error |= irplib_sdp_spectrum_copy_specerr(spectrum, props,
3271 specerr = irplib_sdp_spectrum_get_specerr(spectrum);
3272 }
else if (lamnlin > 0) {
3273 if (cpl_propertylist_has(props, GIALIAS_CRDER1)) {
3274 prestate = cpl_errorstate_get();
3275 double crder1 = cpl_propertylist_get_double(props, GIALIAS_CRDER1);
3276 if (cpl_errorstate_is_equal(prestate) && crder1 > 0) {
3277 specerr = crder1 / sqrt(lamnlin);
3279 error = cpl_error_get_code();
3281 }
else if (! isnan(lamrms)) {
3282 specerr = lamrms / sqrt(lamnlin);
3284 if (! isnan(specerr)) {
3285 error |= irplib_sdp_spectrum_set_specerr(spectrum, specerr);
3292 if (cpl_propertylist_has(props, GIALIAS_SPEC_SYE)) {
3293 error |= irplib_sdp_spectrum_copy_specsye(spectrum, props,
3295 specsye = irplib_sdp_spectrum_get_specsye(spectrum);
3300 error |= irplib_sdp_spectrum_set_specsye(spectrum, 0.002);
3303 error |= irplib_sdp_spectrum_set_specres(spectrum, specres);
3304 error |= irplib_sdp_spectrum_copy_gain(spectrum, props, GIALIAS_CONAD);
3305 error |= irplib_sdp_spectrum_copy_detron(spectrum, props, GIALIAS_RON);
3306 if (got_ancillary_data) {
3307 error |= irplib_sdp_spectrum_set_asson(spectrum, 1, ancillary_filename);
3308 error |= irplib_sdp_spectrum_set_assoc(spectrum, 1,
3309 GIALIAS_ASSOC_VALUE);
3310 error |= irplib_sdp_spectrum_set_assom(spectrum, 1,
"");
3311 assoc_key_offset = 2;
3313 for (i = assoc_key_offset; i < nassoc_keys + assoc_key_offset; ++i) {
3315 error |= irplib_sdp_spectrum_set_asson(spectrum, i,
"");
3316 error |= irplib_sdp_spectrum_set_assoc(spectrum, i,
"");
3317 error |= irplib_sdp_spectrum_set_assom(spectrum, i,
"");
3320 error |= irplib_sdp_spectrum_set_voclass(spectrum, GIALIAS_VOCLASS_VALUE);
3321 error |= irplib_sdp_spectrum_set_vopub(spectrum, GIALIAS_VOPUB_VALUE);
3322 error |= irplib_sdp_spectrum_set_title(spectrum,
"");
3323 error |= cpl_propertylist_append_double(tablekeys, GIALIAS_APERTURE,
3324 GIALIAS_APERTURE_VALUE);
3325 error |= cpl_propertylist_set_comment(tablekeys, GIALIAS_APERTURE,
3326 GIALIAS_APERTURE_COMMENT);
3335 error |= irplib_sdp_spectrum_set_telapse(spectrum, exptime);
3336 error |= irplib_sdp_spectrum_set_tmid(spectrum, (mjdobs + mjdend) * 0.5);
3340 error |= irplib_sdp_spectrum_set_specval(spectrum, 0.0);
3341 error |= irplib_sdp_spectrum_set_specbw(spectrum, 0.0);
3342 error |= irplib_sdp_spectrum_set_nelem(spectrum, ny);
3343 error |= irplib_sdp_spectrum_set_tdmin(spectrum, 0.0);
3344 error |= irplib_sdp_spectrum_set_tdmax(spectrum, 0.0);
3347 error |= cpl_propertylist_append_int(extrakeys, GIALIAS_FPS, -1);
3348 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_FPS,
3349 GIALIAS_FPS_COMMENT);
3352 error |= cpl_propertylist_append_double(extrakeys, GIALIAS_GEOCORR, NAN);
3353 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_GEOCORR,
3354 GIALIAS_GEOCORR_COMMENT);
3355 error |= cpl_propertylist_append_double(extrakeys, GIALIAS_HELICORR, NAN);
3356 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_HELICORR,
3357 GIALIAS_HELICORR_COMMENT);
3358 error |= cpl_propertylist_append_double(extrakeys, GIALIAS_BARYCORR, NAN);
3359 error |= cpl_propertylist_set_comment(extrakeys, GIALIAS_BARYCORR,
3360 GIALIAS_BARYCORR_COMMENT);
3362 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3363 "Could not setup the common SDP spectrum keywords.");
3365 for (
int qci = 0; qci <
sizeof(sciqcpar) /
sizeof(sciqcpar[0]); qci++) {
3366 if (cpl_propertylist_has(props, sciqcpar[qci])) {
3367 cpl_propertylist_copy_property(extrakeys, props, sciqcpar[qci]);
3373 wavecoltype = _giraffe_calc_wave_type(crval2, crpix2, cdelt2, ny);
3376 refwavearray = cpl_array_new(ny, CPL_TYPE_DOUBLE);
3377 data_double = cpl_array_get_data_double(refwavearray);
3378 assert(data_double != NULL);
3379 for (i = 1; i <= ny; ++i) {
3380 data_double[i-1] = (i-crpix2) * cdelt2 + crval2;
3385 error |= irplib_sdp_spectrum_add_column(
3386 spectrum, GIALIAS_COLUMN_WAVE, wavecoltype,
3387 GIALIAS_COLUMN_WAVE_UNIT, NULL, GIALIAS_COLUMN_WAVE_TUTYP,
3388 GIALIAS_COLUMN_WAVE_TUCD, NULL);
3391 error |= irplib_sdp_spectrum_replace_column_comment(
3392 spectrum, GIALIAS_COLUMN_WAVE,
"TUCD",
"Air wavelength");
3394 error |= irplib_sdp_spectrum_set_column_tcomm(
3395 spectrum, GIALIAS_COLUMN_WAVE, GIALIAS_COLUMN_WAVE_TCOMM);
3396 error |= irplib_sdp_spectrum_add_column(
3397 spectrum, GIALIAS_COLUMN_FLUX_REDUCED, CPL_TYPE_DOUBLE,
3398 GIALIAS_COLUMN_FLUX_REDUCED_UNIT, NULL,
3399 GIALIAS_COLUMN_FLUX_REDUCED_TUTYP,
3400 GIALIAS_COLUMN_FLUX_REDUCED_TUCD, NULL);
3401 error |= irplib_sdp_spectrum_set_column_tcomm(
3402 spectrum, GIALIAS_COLUMN_FLUX_REDUCED,
"");
3403 error |= irplib_sdp_spectrum_add_column(
3404 spectrum, GIALIAS_COLUMN_ERR_REDUCED, CPL_TYPE_DOUBLE,
3405 GIALIAS_COLUMN_ERR_REDUCED_UNIT, NULL,
3406 GIALIAS_COLUMN_ERR_REDUCED_TUTYP,
3407 GIALIAS_COLUMN_ERR_REDUCED_TUCD, NULL);
3408 error |= irplib_sdp_spectrum_set_column_tcomm(
3409 spectrum, GIALIAS_COLUMN_ERR_REDUCED,
"");
3410 error |= irplib_sdp_spectrum_add_column(
3411 spectrum, GIALIAS_COLUMN_SNR, CPL_TYPE_DOUBLE,
3412 GIALIAS_COLUMN_SNR_UNIT, NULL, GIALIAS_COLUMN_SNR_TUTYP,
3413 GIALIAS_COLUMN_SNR_TUCD, NULL);
3414 error |= irplib_sdp_spectrum_set_column_tcomm(
3415 spectrum, GIALIAS_COLUMN_SNR, GIALIAS_COLUMN_SNR_TCOMM);
3416 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3417 "Could not setup the SDP spectrum columns.");
3420 GIALIAS_COLUMN_INDEX);
3424 GIALIAS_COLUMN_OBJECT);
3426 GIALIAS_COLUMN_TYPE);
3430 GIALIAS_COLUMN_DEC);
3432 GIALIAS_COLUMN_GCORR);
3434 GIALIAS_COLUMN_HCORR);
3436 GIALIAS_COLUMN_BCORR);
3437 cpl_error_ensure(indices != NULL && fps != NULL && objects != NULL
3438 && spectypes != NULL && ras != NULL && decs != NULL
3439 && gcorr != NULL && hcorr != NULL && bcorr != NULL,
3440 cpl_error_get_code(),
goto cleanup,
3441 "Could not fetch data from the fiber setup table in '%s'.",
3444 formatstr = _giraffe_calc_format_string(
3447 cpl_errorstate tempes = cpl_errorstate_get();
3452 cpl_image_get_maxpos(maxim, &maxx, &maxy);
3454 cpl_image_delete(maxim);
3456 cpl_errorstate_set(tempes);
3461 const double* wave_data;
3467 const char* remregexp =
"^(CDELT[0-9]+|CD[0-9]+_[0-9]+|CRPIX[0-9]+"
3468 "|CRDER[0-9]+|CSYER[0-9]+|BUNIT|BSCALE|BZERO"
3470 cpl_size specindex = indices[i];
3471 double vela, velb, beta;
3472 double correction_factor = 1.0;
3475 if (strcmp(spectypes[i],
"M") != 0)
continue;
3477 filename = cpl_sprintf(formatstr, ++filecount);
3482 vela = gcorr[i] * 1e3;
3483 velb = hcorr[i] * 1e3;
3484 beta = (vela + velb) / CPL_PHYS_C;
3485 cpl_error_ensure(-1 <= beta && beta <= 1,
3486 CPL_ERROR_ILLEGAL_OUTPUT,
goto cleanup,
3487 "The velocities GCORR = %g and HCORR = %g for spectrum"
3488 "%"CPL_SIZE_FORMAT
" in file '%s' give invalid"
3489 " Heliocentric correction factor values.",
3490 gcorr[i], hcorr[i], specindex, flux_filename);
3491 correction_factor = sqrt((1.0 + beta) / (1.0 - beta));
3495 wave_data = cpl_array_get_data_double_const(refwavearray);
3496 if (wavecoltype == CPL_TYPE_FLOAT) {
3497 data_float = cpl_malloc(ny *
sizeof(
float));
3498 for (j = 0; j < ny; ++j) {
3499 data_float[j] = wave_data[j] * correction_factor;
3501 array = cpl_array_wrap_float(data_float, ny);
3503 data_double = cpl_malloc(ny *
sizeof(
double));
3504 for (j = 0; j < ny; ++j) {
3505 data_double[j] = wave_data[j] * correction_factor;
3507 array = cpl_array_wrap_double(data_double, ny);
3509 error |= irplib_sdp_spectrum_set_column_data(
3510 spectrum, GIALIAS_COLUMN_WAVE, array);
3511 cpl_array_unwrap(array);
3514 fluximgcol = cpl_vector_new_from_image_column(
3516 flux_data = cpl_vector_get_data(fluximgcol);
3517 cpl_error_ensure(flux_data != NULL, cpl_error_get_code(),
goto cleanup,
3518 "Unable to extract data in column %"CPL_SIZE_FORMAT
3519 " from image in file '%s'.", specindex, flux_filename);
3520 array = cpl_array_wrap_double(flux_data, ny);
3523 cpl_propertylist_update_char(extrakeys, GIALIAS_QCBRIGHTFLG,
'Y');
3526 cpl_propertylist_update_char(extrakeys, GIALIAS_QCBRIGHTFLG,
'N');
3529 double fluxmean = cpl_array_get_mean(array);
3530 cpl_propertylist_update_double(extrakeys, GIALIAS_QCMEANRED, fluxmean);
3531 error |= irplib_sdp_spectrum_set_column_data(
3532 spectrum, GIALIAS_COLUMN_FLUX_REDUCED, array);
3533 cpl_array_unwrap(array);
3536 errimgcol = cpl_vector_new_from_image_column(
3538 err_data = cpl_vector_get_data(errimgcol);
3539 cpl_error_ensure(err_data != NULL, cpl_error_get_code(),
goto cleanup,
3540 "Unable to extract data in column %"CPL_SIZE_FORMAT
3541 " from image in file '%s'.", specindex, err_filename);
3542 array = cpl_array_wrap_double(err_data, ny);
3543 error |= irplib_sdp_spectrum_set_column_data(
3544 spectrum, GIALIAS_COLUMN_ERR_REDUCED, array);
3545 cpl_array_unwrap(array);
3548 data_double = cpl_malloc(ny *
sizeof(
double));
3549 for (j = 0; j < ny; ++j) {
3550 data_double[j] = (err_data[j] != 0.0) ? flux_data[j] / err_data[j]
3553 array = cpl_array_wrap_double(data_double, ny);
3554 snr = cpl_array_get_median(array);
3555 double snrmean = cpl_array_get_mean(array);
3556 cpl_propertylist_update_double(extrakeys, GIALIAS_QCSNR, snrmean);
3557 error |= irplib_sdp_spectrum_set_column_data(spectrum,
3558 GIALIAS_COLUMN_SNR, array);
3559 cpl_array_unwrap(array);
3561 cpl_free(data_double);
3564 cpl_vector_delete(fluximgcol);
3566 cpl_vector_delete(errimgcol);
3571 estmag = cpl_table_get_double(
giraffe_table_get(fibertable),
"MAGNITUDE", i, NULL);
3573 cpl_propertylist_update_double(extrakeys, GIALIAS_QCMAG, estmag);
3575 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3576 "Could not setup the SDP spectrum columns for '%s'.",
3579 error |= irplib_sdp_spectrum_set_object(spectrum, objects[i]);
3580 error |= irplib_sdp_spectrum_set_title(spectrum, objects[i]);
3581 error |= irplib_sdp_spectrum_set_ra(spectrum, ras[i]);
3582 error |= irplib_sdp_spectrum_set_dec(spectrum, decs[i]);
3583 error |= irplib_sdp_spectrum_set_snr(spectrum, snr);
3584 error |= irplib_sdp_spectrum_set_column_tcomm(
3585 spectrum, GIALIAS_COLUMN_FLUX_REDUCED, flux_filename);
3586 error |= irplib_sdp_spectrum_set_column_tcomm(
3587 spectrum, GIALIAS_COLUMN_ERR_REDUCED, err_filename);
3589 error |= cpl_propertylist_update_int(extrakeys, GIALIAS_FPS, fps[i]);
3590 error |= cpl_propertylist_update_double(extrakeys, GIALIAS_GEOCORR,
3592 error |= cpl_propertylist_update_double(extrakeys, GIALIAS_HELICORR,
3594 error |= cpl_propertylist_update_double(extrakeys, GIALIAS_BARYCORR,
3598 error |= irplib_sdp_spectrum_set_wavelmin(spectrum,
3599 wavelmin * correction_factor);
3600 error |= irplib_sdp_spectrum_set_wavelmax(spectrum,
3601 wavelmax * correction_factor);
3602 error |= irplib_sdp_spectrum_set_specval(spectrum,
3603 (wavelmax + wavelmin) * 0.5 * correction_factor);
3604 error |= irplib_sdp_spectrum_set_specbw(spectrum,
3605 (wavelmax - wavelmin) * correction_factor);
3606 error |= irplib_sdp_spectrum_set_tdmin(spectrum,
3607 wavelmin * correction_factor);
3608 error |= irplib_sdp_spectrum_set_tdmax(spectrum,
3609 wavelmax * correction_factor);
3610 error |= irplib_sdp_spectrum_set_specbin(spectrum,
3611 specbin * correction_factor);
3612 if (! isnan(lamrms)) {
3613 error |= irplib_sdp_spectrum_set_lamrms(spectrum,
3614 lamrms * correction_factor);
3616 if (! isnan(specerr)) {
3617 error |= irplib_sdp_spectrum_set_specerr(spectrum,
3618 specerr * correction_factor);
3620 if (! isnan(specsye)) {
3621 error |= irplib_sdp_spectrum_set_specsye(spectrum,
3622 specsye * correction_factor);
3625 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3626 "Could not setup the SDP spectrum keywords for '%s'.",
3629 error |= irplib_dfs_save_spectrum(allframes, NULL, parlist, usedframes,
3630 inherit, spectrum, recipe_id,
3631 extrakeys, tablekeys, remregexp,
3632 pipe_id, dict_id, filename);
3633 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3634 "Failed to save SDP spectrum %"CPL_SIZE_FORMAT
3635 " to file '%s'.", specindex, filename);
3637 error |= irplib_fits_update_checksums(filename);
3638 cpl_error_ensure(! error, cpl_error_get_code(),
goto cleanup,
3639 "Failed to save update checksums for file '%s'.",
3645 if (filecount == 0) {
3646 cpl_msg_warning(cpl_func,
"No science spectra found in '%s'."
3647 " No SDP spectra created.", flux_filename);
3655 cpl_vector_delete(fluximgcol);
3656 cpl_vector_delete(errimgcol);
3657 cpl_array_unwrap(array);
3658 cpl_array_delete(refwavearray);
3659 cpl_free(data_float);
3660 cpl_free(data_double);
3662 cpl_free(formatstr);
3663 cpl_frameset_delete(usedframes);
3664 cpl_frameset_delete(rawframes);
3665 cpl_frameset_iterator_delete(iterator);
3667 cpl_propertylist_delete(tablekeys);
3668 cpl_propertylist_delete(extrakeys);
3672 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.
GiTable * giraffe_fiberlist_load(const cxchar *filename, cxint dataset, const cxchar *tag)
Load a fiber table from a file.
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.
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.
GiFieldOfViewConfig * giraffe_fov_config_create(cpl_parameterlist *list)
Creates a setup structure for the field of view reconstruction.
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.
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(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.
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_image * giraffe_image_get(const GiImage *self)
Gets the image data.
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
void giraffe_image_delete(GiImage *self)
Destroys an image.
GiImage * giraffe_image_create(cpl_type type, cxint nx, cxint ny)
Creates an image container of a given type.
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.
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.
GiRebinConfig * giraffe_rebin_config_create(cpl_parameterlist *list)
Creates a setup structure for the rebinning.
GiRebinning * giraffe_rebinning_new(void)
Create an empty rebinning results container.
void giraffe_rebinning_destroy(GiRebinning *rebinning)
Destroys a rebinning results container and its contents.
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.
GiTable * giraffe_table_new(void)
Creates a new, empty Giraffe table.
GiTable * giraffe_table_duplicate(const GiTable *src)
Duplicate a Giraffe table.
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.
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_add_frameset_info(cpl_propertylist *plist, const cpl_frameset *set, cxint sequence)
Add frameset specific information to a property list.
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.
GiInstrumentMode giraffe_get_mode(cpl_propertylist *properties)
Determines the instrument mode from a property list.
Slit geometry calibration configuration data structure.