|
35 #include <cxmessages.h>
38 #include <cpl_recipe.h>
39 #include <cpl_plugininfo.h>
40 #include <cpl_parameterlist.h>
41 #include <cpl_frameset.h>
50 #include "gifiberutils.h"
51 #include "gislitgeometry.h"
54 #include "gilocalize.h"
56 #include "giextract.h"
57 #include "gitransmission.h"
63 static cxint gimasterflat(cpl_parameterlist* config, cpl_frameset* set);
64 static cxint giqcmasterflat(cpl_frameset* set);
73 gimasterflat_create(cpl_plugin* plugin)
76 cpl_recipe* recipe = (cpl_recipe*)plugin;
90 recipe->parameters = cpl_parameterlist_new();
91 cx_assert(recipe->parameters != NULL);
124 p = cpl_parameter_new_value( "giraffe.masterflat.transmission",
126 "Controls the relative fiber transmission "
128 "giraffe.masterflat",
131 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "transmission");
132 cpl_parameterlist_append(recipe->parameters, p);
137 p = cpl_parameter_new_value( "giraffe.masterflat.slight",
139 "Controls the scattered light model "
141 "giraffe.masterflat",
143 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "slight");
144 cpl_parameterlist_append(recipe->parameters, p);
158 gimasterflat_exec(cpl_plugin* plugin)
161 cpl_recipe* recipe = (cpl_recipe*)plugin;
166 if (recipe->parameters == NULL || recipe->frames == NULL) {
170 status = gimasterflat(recipe->parameters, recipe->frames);
176 status = giqcmasterflat(recipe->frames);
188 gimasterflat_destroy(cpl_plugin* plugin)
191 cpl_recipe* recipe = (cpl_recipe*)plugin;
200 cpl_parameterlist_delete(recipe->parameters);
202 giraffe_error_clear();
214 gimasterflat(cpl_parameterlist* config, cpl_frameset* set)
217 const cxchar* const _id = "gimasterflat";
220 cxbool transmission = FALSE;
221 cxbool slmodel = FALSE;
228 cxdouble exptime = 0.;
231 cx_slist* flats = NULL;
233 cpl_parameter* p = NULL;
235 cpl_propertylist* properties = NULL;
237 cpl_matrix* biasareas = NULL;
239 cpl_frame* flat_frame = NULL;
240 cpl_frame* mbias_frame = NULL;
241 cpl_frame* mdark_frame = NULL;
242 cpl_frame* bpixel_frame = NULL;
243 cpl_frame* slight_frame = NULL;
244 cpl_frame* mlocy_frame = NULL;
245 cpl_frame* mlocw_frame = NULL;
246 cpl_frame* mlpsf_frame = NULL;
247 cpl_frame* mflat_frame = NULL;
248 cpl_frame* sloc_frame = NULL;
249 cpl_frame* ploc_frame = NULL;
250 cpl_frame* sext_frame = NULL;
251 cpl_frame* slit_frame = NULL;
252 cpl_frame* grating_frame = NULL;
253 cpl_frame* wcal_frame = NULL;
255 GiImage* bpixel = NULL;
256 GiImage* mbias = NULL;
257 GiImage* mdark = NULL;
258 GiImage* slight = NULL;
259 GiImage* sflat = NULL;
260 GiImage* mflat = NULL;
262 GiTable* fibers = NULL;
263 GiTable* grating = NULL;
264 GiTable* slitgeometry = NULL;
265 GiTable* wlsolution = NULL;
267 GiLocalization* sloc = NULL;
268 GiLocalization* ploc = NULL;
269 GiLocalization* mloc = NULL;
271 GiExtraction* extraction = NULL;
273 GiBiasConfig* bias_config = NULL;
275 GiFibersConfig* fibers_config = NULL;
277 GiLocalizeConfig* localize_config = NULL;
279 GiPsfConfig* psf_config = NULL;
281 GiExtractConfig* extract_config = NULL;
283 GiTransmissionConfig* transmission_config = NULL;
285 GiRecipeInfo info = {(cxchar*)_id, 1, NULL};
287 GiGroupInfo groups[] = {
288 {GIFRAME_FIBER_FLAT, CPL_FRAME_GROUP_RAW},
289 {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
290 {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
291 {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
292 {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
293 {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
294 {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
295 {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
296 {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
297 {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
298 {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
299 {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
300 {NULL, CPL_FRAME_GROUP_NONE}
306 cpl_msg_error(_id, "Invalid parameter list! Aborting ...");
311 cpl_msg_error(_id, "Invalid frame set! Aborting ...");
315 status = giraffe_frameset_set_groups( set, groups);
318 cpl_msg_error(_id, "Setting frame group information failed!");
323 p = cpl_parameterlist_find(config, "giraffe.masterflat.transmission");
326 transmission = cpl_parameter_get_bool(p);
329 p = cpl_parameterlist_find(config, "giraffe.masterflat.slight");
332 slmodel = cpl_parameter_get_bool(p);
340 nflats = cpl_frameset_count_tags( set, GIFRAME_FIBER_FLAT);
343 cpl_msg_error(_id, "Too few (%d) raw frames (%s) present in "
344 "frame set! Aborting ...", nflats, GIFRAME_FIBER_FLAT);
349 bpixel_frame = cpl_frameset_find( set, GIFRAME_BADPIXEL_MAP);
352 cpl_msg_info(_id, "No bad pixel map present in frame set.");
355 mbias_frame = cpl_frameset_find( set, GIFRAME_BIAS_MASTER);
358 cpl_msg_info(_id, "No master bias present in frame set.");
361 mdark_frame = cpl_frameset_find( set, GIFRAME_DARK_MASTER);
364 cpl_msg_info(_id, "No master dark present in frame set.");
367 mlocy_frame = cpl_frameset_find( set, GIFRAME_LOCALIZATION_CENTROID);
370 cpl_msg_info(_id, "No master localization (centroid position) "
371 "present in frame set.");
374 mlocw_frame = cpl_frameset_find( set, GIFRAME_LOCALIZATION_WIDTH);
377 cpl_msg_info(_id, "No master localization (spectrum width) "
378 "present in frame set.");
381 mlpsf_frame = cpl_frameset_find( set, GIFRAME_PSF_DATA);
384 cpl_msg_info(_id, "No master localization (PSF parameters) "
385 "present in frame set.");
388 slight_frame = cpl_frameset_find( set, GIFRAME_SCATTERED_LIGHT_MODEL);
391 cpl_msg_info(_id, "No scattered light model present in frame set.");
394 grating_frame = cpl_frameset_find( set, GIFRAME_GRATING);
396 if (!grating_frame) {
397 cpl_msg_info(_id, "No grating data present in frame set. "
405 cpl_msg_info(_id, "No slitgeometry present in frame set. "
410 wcal_frame = cpl_frameset_find( set, GIFRAME_WAVELENGTH_SOLUTION);
413 cpl_msg_info(_id, "No wavelength solution present in frame set.");
421 flats = cx_slist_new();
423 flat_frame = cpl_frameset_find( set, GIFRAME_FIBER_FLAT);
425 for (i = 0; i < nflats; i++) {
427 const cxchar* filename = cpl_frame_get_filename(flat_frame);
435 cpl_msg_error(_id, "Cannot load raw flat from '%s'. "
436 "Aborting ...", filename);
443 cx_slist_push_back(flats, raw);
445 flat_frame = cpl_frameset_find( set, NULL);
458 cpl_msg_info(_id, "Averaging flat field frames ...");
460 nflats = (cxint)cx_slist_size(flats);
461 sflat = cx_slist_pop_front(flats);
464 cx_assert(properties != NULL);
466 exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
468 for (i = 1; i < nflats; i++) {
470 cpl_propertylist* _properties;
472 GiImage* flat = cx_slist_pop_front(flats);
478 cx_assert(_properties != NULL);
480 exptime += cpl_propertylist_get_double(_properties, GIALIAS_EXPTIME);
488 cx_assert(cx_slist_empty(flats));
489 cx_slist_delete(flats);
497 cpl_msg_info(_id, "Updating stacked flat field image properties ...");
499 cpl_propertylist_update_double(properties, GIALIAS_EXPTIME,
502 cpl_propertylist_update_double(properties, GIALIAS_EXPTTOT, exptime);
503 cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
504 "Total exposure time of all frames "
507 cpl_propertylist_update_int(properties, GIALIAS_DATANCOM, nflats);
508 cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM, "Number of "
511 cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
520 if (bias_config->method == GIBIAS_METHOD_MASTER ||
521 bias_config->method == GIBIAS_METHOD_ZMASTER) {
524 cpl_msg_error(_id, "Missing master bias frame! Selected bias "
525 "removal method requires a master bias frame!");
533 const cxchar* filename = cpl_frame_get_filename(mbias_frame);
540 cpl_msg_error(_id, "Cannot load master bias from '%s'. "
541 "Aborting ...", filename);
558 const cxchar* filename = cpl_frame_get_filename(bpixel_frame);
565 cpl_msg_error(_id, "Cannot load bad pixel map from '%s'. "
566 "Aborting ...", filename);
600 cpl_msg_error(_id, "Bias removal failed. Aborting ...");
605 if (bpixel != NULL) {
621 const cxchar* filename = cpl_frame_get_filename(mdark_frame);
623 GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
630 cpl_msg_error(_id, "Cannot load master dark from '%s'. "
631 "Aborting ...", filename);
636 if (bpixel != NULL) {
648 cpl_msg_error(_id, "Dark subtraction failed! Aborting ...");
656 if (bpixel != NULL) {
675 cpl_msg_info(_id, "Writing master flat field image ...");
680 GIFRAME_FIBER_FLAT_MASTER,
681 CPL_FRAME_LEVEL_FINAL,
684 if (mflat_frame == NULL) {
685 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
696 cpl_frameset_insert( set, mflat_frame);
703 cpl_msg_info(_id, "Recipe Step: Fiber setup");
706 flat_frame = cpl_frameset_find( set, GIFRAME_FIBER_FLAT);
708 cpl_msg_info(_id, "Building fiber setup for frame '%s'.",
709 cpl_frame_get_filename(flat_frame));
711 if (mlocy_frame == NULL) {
713 GiTable *active_fibers = NULL;
715 if (slit_frame != NULL) {
717 const cxchar *tag = cpl_frame_get_tag(slit_frame);
718 const cxchar *filename = cpl_frame_get_filename(slit_frame);
720 if (strcmp(tag, GIFRAME_SLITSETUP) == 0) {
724 cpl_frame_get_filename(slit_frame),
725 1, "SLIT_GEOMETRY_SETUP");
728 cpl_msg_error(_id, "Cannot load expected fiber setup from "
729 "slit geometry '%s'! Aborting ...", filename);
749 cpl_msg_error(_id, "Cannot determine fiber setup from flat "
750 "field frame '%s'! Aborting ...",
751 cpl_frame_get_filename(flat_frame));
766 active_fibers = NULL;
768 cpl_msg_info(_id, "Fiber setup taken from flat field frame '%s'.",
769 cpl_frame_get_filename(flat_frame));
774 cpl_msg_info(_id, "Fiber reference setup taken from localization "
775 "frame '%s'.", cpl_frame_get_filename(mlocy_frame));
780 cpl_msg_error(_id, "Cannot create fiber setup for frame '%s'! "
781 "Aborting ...", cpl_frame_get_filename(flat_frame));
803 if (mlocy_frame != NULL) {
805 const cxchar* filename = cpl_frame_get_filename(mlocy_frame);
808 mloc = giraffe_localization_new();
813 cpl_msg_error(_id, "Cannot load master localization centroids "
814 "from '%s'. Aborting ...", filename);
816 giraffe_localization_delete(mloc);
835 if (localize_config->full == FALSE) {
841 cpl_msg_error(_id, "Localization computation using only SIWC spectra "
842 "is not yet supported! Aborting ...");
857 sloc = giraffe_localization_new();
860 bpixel, localize_config);
863 cpl_msg_error(_id, "Spectrum localization failed! Aborting ...");
866 giraffe_localization_destroy(sloc);
869 giraffe_localization_destroy(mloc);
887 giraffe_localization_destroy(mloc);
896 cpl_msg_info(_id, "Writing fiber localization ...");
904 GIFRAME_LOCALIZATION_CENTROID,
905 CPL_FRAME_LEVEL_FINAL,
908 if (sloc_frame == NULL) {
909 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
911 giraffe_localization_destroy(sloc);
926 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
927 "Aborting ...", cpl_frame_get_filename(sloc_frame));
929 cpl_frame_delete(sloc_frame);
931 giraffe_localization_destroy(sloc);
943 cpl_frameset_insert( set, sloc_frame);
951 GIFRAME_LOCALIZATION_WIDTH,
952 CPL_FRAME_LEVEL_FINAL,
955 if (sloc_frame == NULL) {
956 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
958 giraffe_localization_destroy(sloc);
973 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
974 "Aborting ...", cpl_frame_get_filename(sloc_frame));
976 cpl_frame_delete(sloc_frame);
978 giraffe_localization_destroy(sloc);
990 cpl_frameset_insert( set, sloc_frame);
999 GIFRAME_LOCALIZATION_FIT,
1000 CPL_FRAME_LEVEL_FINAL,
1003 if (sloc_frame == NULL) {
1004 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1006 giraffe_localization_destroy(sloc);
1019 cpl_frameset_insert( set, sloc_frame);
1036 if (psf_config == NULL) {
1037 cpl_msg_error(_id, "Invalid fiber profile fit configuration!");
1039 giraffe_localization_destroy(sloc);
1048 if (bpixel != NULL) {
1057 ploc = giraffe_localization_new();
1060 bpixel, psf_config);
1063 cpl_msg_error(_id, "Fiber profile computation failed! Aborting ...");
1065 giraffe_localization_destroy(ploc);
1071 giraffe_localization_destroy(sloc);
1080 if (bpixel != NULL) {
1092 giraffe_localization_destroy(sloc);
1101 cpl_msg_info(_id, "Writing fiber traces ...");
1109 GIFRAME_PSF_CENTROID,
1110 CPL_FRAME_LEVEL_FINAL,
1113 if (ploc_frame == NULL) {
1114 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1116 giraffe_localization_destroy(ploc);
1125 if (bpixel != NULL) {
1136 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1137 "Aborting ...", cpl_frame_get_filename(ploc_frame));
1139 cpl_frame_delete(ploc_frame);
1141 giraffe_localization_destroy(ploc);
1150 if (bpixel != NULL) {
1158 cpl_frameset_insert( set, ploc_frame);
1167 CPL_FRAME_LEVEL_FINAL,
1170 if (ploc_frame == NULL) {
1171 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1173 giraffe_localization_destroy(ploc);
1182 if (bpixel != NULL) {
1193 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1194 "Aborting ...", cpl_frame_get_filename(ploc_frame));
1196 cpl_frame_delete(ploc_frame);
1198 giraffe_localization_destroy(ploc);
1207 if (bpixel != NULL) {
1215 cpl_frameset_insert( set, ploc_frame);
1224 CPL_FRAME_LEVEL_FINAL,
1227 if (ploc_frame == NULL) {
1228 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1230 giraffe_localization_destroy(ploc);
1239 if (bpixel != NULL) {
1250 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1251 "Aborting ...", cpl_frame_get_filename(ploc_frame));
1253 cpl_frame_delete(ploc_frame);
1255 giraffe_localization_destroy(ploc);
1264 if (bpixel != NULL) {
1272 cpl_frameset_insert( set, ploc_frame);
1277 GiFrameCreator creator = (GiFrameCreator) giraffe_psfdata_save;
1282 CPL_FRAME_LEVEL_FINAL,
1283 properties, ploc->psf,
1287 if (ploc_frame == NULL) {
1288 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1290 giraffe_localization_destroy(ploc);
1299 if (bpixel != NULL) {
1310 cpl_msg_error(_id, "Cannot attach fiber setup to local "
1311 "file '%s'! Aborting ...",
1312 cpl_frame_get_filename(ploc_frame));
1314 cpl_frame_delete(ploc_frame);
1316 giraffe_localization_destroy(ploc);
1325 if (bpixel != NULL) {
1333 cpl_frameset_insert( set, ploc_frame);
1344 if (slmodel == TRUE) {
1346 cpl_frame* slmodel_frame = NULL;
1348 GiSLightConfig* slight_config = NULL;
1351 cpl_msg_info(_id, "Computing scattered light model ...");
1355 if (slight_config == NULL) {
1356 cpl_msg_error(_id, "Invalid scattered light model "
1366 giraffe_localization_destroy(ploc);
1376 bpixel, NULL, slight_config);
1379 cpl_msg_error(_id, "Scattered light model computation failed! "
1389 if (bpixel != NULL) {
1394 giraffe_localization_destroy(ploc);
1402 slight_config = NULL;
1409 cpl_msg_info(_id, "Writing scattered light model ...");
1415 GIFRAME_SCATTERED_LIGHT_MODEL,
1416 CPL_FRAME_LEVEL_FINAL,
1419 if (slmodel_frame == NULL) {
1420 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1427 if (bpixel != NULL) {
1432 giraffe_localization_destroy(ploc);
1438 cpl_frameset_insert( set, slmodel_frame);
1450 cpl_msg_info(_id, "Extracting spectra ...");
1452 if (slight_frame != NULL) {
1454 const cxchar* filename = cpl_frame_get_filename(slight_frame);
1461 cpl_msg_error(_id, "Cannot load scattered light model from '%s'. "
1462 "Aborting ...", filename);
1469 if (bpixel != NULL) {
1474 giraffe_localization_destroy(ploc);
1485 extraction = giraffe_extraction_new();
1488 ploc, bpixel, slight,
1492 cpl_msg_error(_id, "Spectrum extraction failed! Aborting ...");
1494 giraffe_extraction_destroy(extraction);
1498 giraffe_localization_destroy(ploc);
1504 if (bpixel != NULL) {
1517 if (bpixel != NULL) {
1534 cpl_propertylist_update_double(properties, GIALIAS_FLAT_SCALE, mean);
1535 cpl_propertylist_set_comment(properties, GIALIAS_FLAT_SCALE,
1536 "Flat field scale factor");
1542 cpl_propertylist_update_double(properties, GIALIAS_FLAT_SCALE, mean);
1543 cpl_propertylist_set_comment(properties, GIALIAS_FLAT_SCALE,
1544 "Flat field scale factor");
1551 if (transmission == TRUE) {
1553 const cxchar* filename = NULL;
1558 cpl_msg_info(_id, "Computing relative fiber transmission ...");
1560 filename = cpl_frame_get_filename(grating_frame);
1566 cpl_msg_error(_id, "Cannot load grating data from '%s'. "
1567 "Aborting ...", filename);
1573 giraffe_extraction_destroy(extraction);
1575 giraffe_localization_destroy(ploc);
1584 filename = cpl_frame_get_filename(slit_frame);
1588 if (slitgeometry == NULL) {
1589 cpl_msg_error(_id, "Cannot load slit geometry data from '%s'. "
1590 "Aborting ...", filename);
1596 giraffe_extraction_destroy(extraction);
1598 giraffe_localization_destroy(ploc);
1614 cpl_msg_error(_id, "Slit geometry data from '%s' is not "
1615 "applicable for current fiber setup! "
1616 "Aborting ...", filename);
1623 giraffe_extraction_destroy(extraction);
1625 giraffe_localization_destroy(ploc);
1636 if (wcal_frame != NULL) {
1638 filename = cpl_frame_get_filename(wcal_frame);
1640 cpl_msg_info(_id, "Loading wavelength solution from '%s'",
1647 cpl_msg_error(_id, "Cannot load wavelength solution from "
1648 "'%s'. Aborting ...", filename);
1656 giraffe_extraction_destroy(extraction);
1658 giraffe_localization_destroy(ploc);
1669 status = giraffe_transmission_compute(extraction, fibers,
1671 grating, slitgeometry);
1674 cpl_msg_error(_id, "Relative transmission computation failed! "
1677 if (wlsolution != NULL) {
1687 giraffe_extraction_destroy(extraction);
1689 giraffe_localization_destroy(ploc);
1697 if (wlsolution != NULL) {
1709 giraffe_localization_destroy(ploc);
1718 cpl_msg_info(_id, "Writing extracted spectra ...");
1725 GIFRAME_FIBER_FLAT_EXTSPECTRA,
1726 CPL_FRAME_LEVEL_FINAL,
1729 if (sext_frame == NULL) {
1730 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1732 giraffe_extraction_destroy(extraction);
1741 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1742 "Aborting ...", cpl_frame_get_filename(sext_frame));
1744 cpl_frame_delete(sext_frame);
1746 giraffe_extraction_destroy(extraction);
1752 cpl_frameset_insert( set, sext_frame);
1759 GIFRAME_FIBER_FLAT_EXTERRORS,
1760 CPL_FRAME_LEVEL_FINAL,
1763 if (sext_frame == NULL) {
1764 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1766 giraffe_extraction_destroy(extraction);
1775 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1776 "Aborting ...", cpl_frame_get_filename(sext_frame));
1778 cpl_frame_delete(sext_frame);
1780 giraffe_extraction_destroy(extraction);
1786 cpl_frameset_insert( set, sext_frame);
1790 if (extraction->npixels != NULL) {
1795 GIFRAME_FIBER_FLAT_EXTPIXELS,
1796 CPL_FRAME_LEVEL_FINAL,
1799 if (sext_frame == NULL) {
1800 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1802 giraffe_extraction_destroy(extraction);
1811 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1812 "Aborting ...", cpl_frame_get_filename(sext_frame));
1814 cpl_frame_delete(sext_frame);
1816 giraffe_extraction_destroy(extraction);
1822 cpl_frameset_insert( set, sext_frame);
1831 GIFRAME_FIBER_FLAT_EXTTRACE,
1832 CPL_FRAME_LEVEL_FINAL,
1835 if (sext_frame == NULL) {
1836 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1838 giraffe_extraction_destroy(extraction);
1847 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1848 "Aborting ...", cpl_frame_get_filename(sext_frame));
1850 cpl_frame_delete(sext_frame);
1852 giraffe_extraction_destroy(extraction);
1858 cpl_frameset_insert( set, sext_frame);
1862 if (extraction->model != NULL) {
1867 GIFRAME_FIBER_FLAT_EXTMODEL,
1868 CPL_FRAME_LEVEL_FINAL,
1871 if (sext_frame == NULL) {
1872 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1874 giraffe_extraction_destroy(extraction);
1883 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1884 "Aborting ...", cpl_frame_get_filename(sext_frame));
1886 cpl_frame_delete(sext_frame);
1888 giraffe_extraction_destroy(extraction);
1894 cpl_frameset_insert( set, sext_frame);
1903 giraffe_extraction_destroy(extraction);
1912 giqcmasterflat(cpl_frameset* set)
1915 const cxchar* const fctid = "giqcmasterflat";
1922 cxint nsaturated = 0;
1925 const cxdouble saturation = 60000.;
1926 const cxdouble* pixels = NULL;
1927 cxdouble efficiency[2] = {0., 0.};
1928 cxdouble scale = 1.;
1932 cxdouble* _pdata = NULL;
1933 cxdouble* _tdata = NULL;
1935 cpl_propertylist* properties = NULL;
1936 cpl_propertylist* qclog = NULL;
1938 cpl_frame* rframe = NULL;
1939 cpl_frame* pframe = NULL;
1941 cpl_image* _rimage = NULL;
1942 cpl_image* _pimage = NULL;
1943 cpl_image* _test = NULL;
1945 cpl_table* _ptable = NULL;
1947 GiImage* rimage = NULL;
1948 GiImage* pimage = NULL;
1950 GiTable* ptable = NULL;
1954 GiWindow w = {0, 0, 0, 0};
1958 cpl_msg_info(fctid, "Computing QC1 parameters ...");
1960 qc = giraffe_qclog_open(0);
1963 cpl_msg_error(fctid, "Cannot create QC1 log!");
1967 qclog = giraffe_paf_get_properties(qc);
1968 cx_assert(qclog != NULL);
1977 CPL_FRAME_GROUP_PRODUCT);
1979 if (pframe == NULL) {
1981 cpl_msg_warning(fctid, "Product '%s' not found.",
1982 GIFRAME_FIBER_FLAT_EXTSPECTRA);
1984 cpl_msg_warning(fctid, "Setting lamp efficiencies (%s, %s) to 0.",
1985 GIALIAS_QCLAMP, GIALIAS_QCLAMP_SIMCAL);
1997 cpl_msg_error(fctid, "Could not load extracted spectra '%s'!",
1998 cpl_frame_get_filename(pframe));
2003 giraffe_paf_delete(qc);
2010 cx_assert(_pimage != NULL);
2018 cpl_msg_error(fctid, "Could not load extracted spectra fiber setup!");
2026 giraffe_paf_delete(qc);
2033 cx_assert(_ptable != NULL);
2035 if (cpl_table_has_column(_ptable, "RP") == FALSE) {
2037 cpl_msg_warning(fctid, "Column 'RP' not found in fiber setup table!");
2038 cpl_msg_warning(fctid, "Setting lamp efficiencies (%s, %s) to 0.",
2039 GIALIAS_QCLAMP, GIALIAS_QCLAMP_SIMCAL);
2048 cx_assert(properties != NULL);
2050 if (cpl_propertylist_has(properties, GIALIAS_EXPTIME) == FALSE) {
2052 cpl_msg_warning(fctid, "Property '%s' not found in '%s'.",
2053 GIALIAS_EXPTIME, cpl_frame_get_filename(rframe));
2054 cpl_msg_warning(fctid, "Setting lamp efficiencies (%s, %s) to 0.",
2055 GIALIAS_QCLAMP, GIALIAS_QCLAMP_SIMCAL);
2063 cxbool scaled = cpl_propertylist_has(properties,
2064 GIALIAS_FLAT_SCALE);
2067 cxint nb = cpl_image_get_size_y(_pimage);
2068 cxint nf[2] = {0, 0};
2070 cxdouble exptime = cpl_propertylist_get_double(properties,
2072 cxdouble* _sum = NULL;
2074 cpl_image* sum = NULL;
2077 if (scaled == TRUE) {
2079 scale = cpl_propertylist_get_double(properties,
2080 GIALIAS_FLAT_SCALE);
2081 cpl_image_multiply_scalar(_pimage, scale);
2086 sum = cpl_image_collapse_create(_pimage, 0);
2087 _sum = cpl_image_get_data_double(sum);
2089 for (fiber = 0; fiber < cpl_table_get_nrow(_ptable); ++fiber) {
2091 cxint rp = cpl_table_get_int(_ptable, "RP", fiber, NULL);
2094 efficiency[1] += _sum[fiber];
2098 efficiency[0] += _sum[fiber];
2106 cpl_image_delete(sum);
2110 cpl_msg_warning(fctid, "No OzPoz fibers found in the "
2111 "current fiber setup.");
2112 cpl_msg_warning(fctid, "Setting lamp efficiency (%s) to 0.",
2117 efficiency[0] /= nf[0] * nb * exptime;
2121 cpl_msg_warning(fctid, "No simultaneous calibration fibers "
2122 "found in the current fiber setup.");
2123 cpl_msg_warning(fctid, "Setting lamp efficiency (%s) to 0.",
2124 GIALIAS_QCLAMP_SIMCAL);
2128 efficiency[1] /= nf[1] * nb * exptime;
2153 CPL_FRAME_GROUP_PRODUCT);
2155 if (pframe == NULL) {
2156 cpl_msg_error(fctid, "Missing product frame (%s)",
2157 GIFRAME_FIBER_FLAT_MASTER);
2159 giraffe_paf_delete(qc);
2165 cpl_msg_info(fctid, "Processing product frame '%s' (%s)",
2166 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
2172 cpl_msg_error(fctid, "Could not load master flat field '%s'!",
2173 cpl_frame_get_filename(pframe));
2178 giraffe_paf_delete(qc);
2189 rframe = cpl_frameset_find( set, GIFRAME_FIBER_FLAT);
2191 if (rframe == NULL) {
2192 cpl_msg_error(fctid, "Missing raw frame (%s)", GIFRAME_FIBER_FLAT);
2197 giraffe_paf_delete(qc);
2208 cpl_msg_error(fctid, "Could not load flat field '%s'!",
2209 cpl_frame_get_filename(rframe));
2217 giraffe_paf_delete(qc);
2225 cx_assert(_rimage != NULL);
2228 cx_assert(properties != NULL);
2240 cpl_propertylist_update_string(qclog, "PRO.CATG",
2241 cpl_frame_get_tag(pframe));
2242 cpl_propertylist_set_comment(qclog, "PRO.CATG",
2243 "Pipeline product category");
2246 cx_assert(properties != NULL);
2264 cx_assert(properties != NULL);
2266 if (cpl_propertylist_has(properties, GIALIAS_OVSCX) == TRUE) {
2268 cxint _ox = cpl_propertylist_get_int(properties, GIALIAS_OVSCX);
2269 cxint _nx = cpl_image_get_size_x(_rimage) - 2 * CX_MAX(0, _ox) - 1;
2271 w.x0 = CX_MAX(0, _ox) + 1;
2276 if (cpl_propertylist_has(properties, GIALIAS_OVSCY) == TRUE) {
2278 cxint _oy = cpl_propertylist_get_int(properties, GIALIAS_OVSCY);
2279 cxint _ny = cpl_image_get_size_y(_rimage) - 2 * CX_MAX(0, _oy) - 1;
2281 w.y0 = CX_MAX(0, _oy) + 1;
2286 mean = cpl_image_get_mean_window(_rimage, w.x0, w.y0, w.x1, w.y1);
2288 pixels = cpl_image_get_data(_rimage);
2289 npixel = cpl_image_get_size_x(_rimage) * cpl_image_get_size_y(_rimage);
2291 for (i = 0; i < npixel; i++) {
2292 if (pixels[i] > saturation) {
2299 cx_assert(properties != NULL);
2301 cpl_propertylist_update_double(properties, GIALIAS_QCMEAN, mean);
2302 cpl_propertylist_set_comment(properties, GIALIAS_QCMEAN, "Mean level of "
2309 cpl_propertylist_update_int(properties, GIALIAS_QCNSAT, nsaturated);
2310 cpl_propertylist_set_comment(properties, GIALIAS_QCNSAT, "Number of "
2311 "saturated pixels in the first raw frame");
2321 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP, efficiency[0]);
2322 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP,
2323 "Calibration lamp efficiency");
2328 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP_SIMCAL,
2330 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP_SIMCAL,
2331 "SIMCAL lamp efficiency");
2334 GIALIAS_QCLAMP_SIMCAL);
2346 giraffe_qclog_close(qc);
2354 qc = giraffe_qclog_open(1);
2357 cpl_msg_error(fctid, "Cannot create QC1 log!");
2365 qclog = giraffe_paf_get_properties(qc);
2366 cx_assert(qclog != NULL);
2369 CPL_FRAME_GROUP_PRODUCT);
2371 if (pframe == NULL) {
2372 cpl_msg_error(fctid, "Missing product frame (%s)",
2373 GIFRAME_LOCALIZATION_CENTROID);
2375 giraffe_paf_delete(qc);
2384 cpl_msg_info(fctid, "Processing product frame '%s' (%s)",
2385 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
2392 cpl_msg_error(fctid, "Could not load localization centroids '%s'!",
2393 cpl_frame_get_filename(pframe));
2401 giraffe_paf_delete(qc);
2412 cpl_msg_error(fctid, "Could not load localization centroids '%s'!",
2413 cpl_frame_get_filename(pframe));
2424 giraffe_paf_delete(qc);
2431 cx_assert(properties != NULL);
2443 cpl_propertylist_update_string(qclog, "PRO.CATG",
2444 cpl_frame_get_tag(pframe));
2445 cpl_propertylist_set_comment(qclog, "PRO.CATG",
2446 "Pipeline product category");
2449 cx_assert(properties != NULL);
2467 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP, efficiency[0]);
2468 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP,
2469 "Calibration lamp efficiency");
2474 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP_SIMCAL,
2476 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP_SIMCAL,
2477 "SIMCAL lamp efficiency");
2480 GIALIAS_QCLAMP_SIMCAL);
2487 _test = cpl_image_collapse_create(_pimage, 0);
2488 cpl_image_divide_scalar(_test, cpl_image_get_size_y(_pimage));
2490 _pdata = cpl_image_get_data(_pimage);
2491 _tdata = cpl_image_get_data(_test);
2495 nx = cpl_image_get_size_x(_pimage);
2496 ny = cpl_image_get_size_y(_pimage);
2498 for (i = 0; i < nx; i++) {
2505 for (j = 0; j < ny; j++) {
2506 _rms += pow(_pdata[j * nx + i] - _tdata[i], 2.);
2509 rms += sqrt(_rms / (ny - 1));
2515 cpl_image_delete(_test);
2518 cpl_propertylist_update_double(properties, GIALIAS_QCLCRMS, rms);
2519 cpl_propertylist_set_comment(properties, GIALIAS_QCLCRMS,
2520 "Mean fibre signal curvature");
2530 for (i = 0; i < nx; i++) {
2532 cxdouble min = cpl_image_get_min_window(_pimage,
2533 i + 1, 1, i + 1, ny);
2534 cxdouble max = cpl_image_get_max_window(_pimage,
2535 i + 1, 1, i + 1, ny);
2543 cpl_propertylist_update_double(properties, GIALIAS_QCLCDIFF, diff);
2544 cpl_propertylist_set_comment(properties, GIALIAS_QCLCDIFF,
2545 "Mean difference of fibre signal "
2555 cpl_msg_error(fctid, "Could not save localization centroids '%s'!",
2556 cpl_frame_get_filename(pframe));
2567 giraffe_paf_delete(qc);
2577 cpl_msg_error(fctid, "Could not save localization centroids '%s'!",
2578 cpl_frame_get_filename(pframe));
2589 giraffe_paf_delete(qc);
2601 giraffe_qclog_close(qc);
2609 qc = giraffe_qclog_open(2);
2612 cpl_msg_error(fctid, "Cannot create QC1 log!");
2620 qclog = giraffe_paf_get_properties(qc);
2621 cx_assert(qclog != NULL);
2624 CPL_FRAME_GROUP_PRODUCT);
2626 if (pframe == NULL) {
2627 cpl_msg_error(fctid, "Missing product frame (%s)",
2628 GIFRAME_LOCALIZATION_WIDTH);
2630 giraffe_paf_delete(qc);
2639 cpl_msg_info(fctid, "Processing product frame '%s' (%s)",
2640 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
2647 cpl_msg_error(fctid, "Could not load localization widths '%s'!",
2648 cpl_frame_get_filename(pframe));
2656 giraffe_paf_delete(qc);
2667 cpl_msg_error(fctid, "Could not load localization widths '%s'!",
2668 cpl_frame_get_filename(pframe));
2679 giraffe_paf_delete(qc);
2686 cx_assert(properties != NULL);
2698 cpl_propertylist_update_string(qclog, "PRO.CATG",
2699 cpl_frame_get_tag(pframe));
2700 cpl_propertylist_set_comment(qclog, "PRO.CATG",
2701 "Pipeline product category");
2704 cx_assert(properties != NULL);
2722 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP, efficiency[0]);
2723 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP,
2724 "Calibration lamp efficiency");
2729 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP_SIMCAL,
2731 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP_SIMCAL,
2732 "SIMCAL lamp efficiency");
2735 GIALIAS_QCLAMP_SIMCAL);
2742 _test = cpl_image_collapse_create(_pimage, 0);
2743 cpl_image_divide_scalar(_test, cpl_image_get_size_y(_pimage));
2745 _pdata = cpl_image_get_data(_pimage);
2746 _tdata = cpl_image_get_data(_test);
2751 nx = cpl_image_get_size_x(_pimage);
2752 ny = cpl_image_get_size_y(_pimage);
2754 for (i = 0; i < nx; i++) {
2761 for (j = 0; j < ny; j++) {
2762 _rms += pow(_pdata[j * nx + i] - _tdata[i], 2.);
2766 rms += sqrt(_rms / (ny - 1));
2773 cpl_image_delete(_test);
2777 cpl_propertylist_update_double(properties, GIALIAS_QCLWAVG, mean);
2778 cpl_propertylist_set_comment(properties, GIALIAS_QCLWAVG,
2779 "Mean fibre half width");
2784 cpl_propertylist_update_double(properties, GIALIAS_QCLWRMS, rms);
2785 cpl_propertylist_set_comment(properties, GIALIAS_QCLWRMS,
2786 "RMS of fibre half width");
2795 cpl_msg_error(fctid, "Could not save localization widths '%s'!",
2796 cpl_frame_get_filename(pframe));
2807 giraffe_paf_delete(qc);
2817 cpl_msg_error(fctid, "Could not save localization widths '%s'!",
2818 cpl_frame_get_filename(pframe));
2829 giraffe_paf_delete(qc);
2841 giraffe_qclog_close(qc);
2849 qc = giraffe_qclog_open(3);
2852 cpl_msg_error(fctid, "Cannot create QC1 log!");
2860 qclog = giraffe_paf_get_properties(qc);
2861 cx_assert(qclog != NULL);
2864 CPL_FRAME_GROUP_PRODUCT);
2866 if (pframe == NULL) {
2867 cpl_msg_error(fctid, "Missing product frame (%s)",
2868 GIFRAME_FIBER_FLAT_EXTSPECTRA);
2870 giraffe_paf_delete(qc);
2879 cpl_msg_info(fctid, "Processing product frame '%s' (%s)",
2880 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
2887 cpl_msg_error(fctid, "Could not load extracted flat field spectra "
2888 "'%s'!", cpl_frame_get_filename(pframe));
2896 giraffe_paf_delete(qc);
2907 cpl_msg_error(fctid, "Could not load extracted flat field spectra "
2908 "'%s'!", cpl_frame_get_filename(pframe));
2919 giraffe_paf_delete(qc);
2926 cx_assert(properties != NULL);
2938 cpl_propertylist_update_string(qclog, "PRO.CATG",
2939 cpl_frame_get_tag(pframe));
2940 cpl_propertylist_set_comment(qclog, "PRO.CATG",
2941 "Pipeline product category");
2944 cx_assert(properties != NULL);
2962 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP, efficiency[0]);
2963 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP,
2964 "Calibration lamp efficiency");
2969 cpl_propertylist_update_double(properties, GIALIAS_QCLAMP_SIMCAL,
2971 cpl_propertylist_set_comment(properties, GIALIAS_QCLAMP_SIMCAL,
2972 "SIMCAL lamp efficiency");
2975 GIALIAS_QCLAMP_SIMCAL);
2980 if (cpl_table_has_column(_ptable, "TRANSMISSION") == FALSE) {
2981 cpl_msg_warning(fctid, "Relative fiber transmission not available! "
2982 "QC parameter computation skipped.");
2986 const cxdouble low = 0.5;
2987 const cxdouble high = 2.0;
2994 cpl_table* ttable = NULL;
2999 cpl_table_unselect_all(_ptable);
3001 cpl_table_or_selected_int(_ptable, "RP", CPL_GREATER_THAN, 0);
3002 cpl_table_and_selected_double(_ptable, "TRANSMISSION",
3003 CPL_GREATER_THAN, low);
3004 cpl_table_and_selected_double(_ptable, "TRANSMISSION",
3005 CPL_LESS_THAN, high);
3007 ttable = cpl_table_extract_selected(_ptable);
3009 nf = cpl_table_get_nrow(ttable);
3010 if (ttable == NULL || nf <= 0) {
3011 cpl_msg_warning(fctid, "No fiber found within transmission "
3012 "range ]%.2f, %.2f[", low, high);
3017 t = cpl_table_get_column_median(ttable, "TRANSMISSION");
3021 for (i = 0; i < cpl_table_get_nrow(ttable); i++) {
3023 cxdouble _t = cpl_table_get_double(ttable,
3024 "TRANSMISSION", i, NULL);
3025 rms += pow(_t - t, 2.);
3029 rms = sqrt(rms / (cpl_table_get_nrow(ttable) - 1));
3039 if (!cpl_table_has_column(_ptable, "DTRANSMISSION")) {
3040 cpl_msg_warning(fctid, "Relative fiber transmission error not "
3041 "available! QC parameter computation skipped.");
3045 cx_assert(cpl_table_has_column(ttable, "DTRANSMISSION") != 0);
3046 dt = cpl_table_get_column_median(ttable, "DTRANSMISSION");
3050 cpl_table_delete(ttable);
3054 cpl_propertylist_update_double(properties, GIALIAS_QCTRMED, t);
3055 cpl_propertylist_set_comment(properties, GIALIAS_QCTRMED, "Median of "
3056 "relative fibre transmission");
3062 cpl_propertylist_update_double(properties, GIALIAS_QCTRRMS, rms);
3063 cpl_propertylist_set_comment(properties, GIALIAS_QCTRRMS, "RMS of "
3064 "relative fibre transmission");
3070 cpl_propertylist_update_double(properties, GIALIAS_QCTRERR, dt);
3071 cpl_propertylist_set_comment(properties, GIALIAS_QCTRERR, "Median of "
3072 "relative fibre transmission error");
3080 cpl_propertylist_update_int(properties, GIALIAS_QCTRNF, nf);
3081 cpl_propertylist_set_comment(properties, GIALIAS_QCTRNF, "Number "
3082 "of fibres used for median transmission computation.");
3085 properties, GIALIAS_QCTRNF);
3087 cpl_msg_debug(fctid, "%d fibers used for median transmission "
3088 "computation.", nf);
3095 cpl_msg_error(fctid, "Could not save extracted flat field spectra "
3096 "'%s'!", cpl_frame_get_filename(pframe));
3107 giraffe_paf_delete(qc);
3117 cpl_msg_error(fctid, "Could not save extracted flat field spectra "
3118 "'%s'!", cpl_frame_get_filename(pframe));
3129 giraffe_paf_delete(qc);
3141 giraffe_qclog_close(qc);
3162 cpl_plugin_get_info(cpl_pluginlist* list)
3165 cpl_recipe* recipe = cx_calloc(1, sizeof *recipe);
3166 cpl_plugin* plugin = &recipe->interface;
3169 cpl_plugin_init(plugin,
3171 GIRAFFE_BINARY_VERSION,
3172 CPL_PLUGIN_TYPE_RECIPE,
3174 "Create the fiber master flat field and the "
3175 "localization mask.",
3176 "For detailed information please refer to the "
3177 "GIRAFFE pipeline user manual.\nIt is available at "
3178 "http://www.eso.org/pipelines.",
3182 gimasterflat_create,
3184 gimasterflat_destroy);
3186 cpl_pluginlist_append(list, plugin);
|