GIRAFFE Pipeline Reference Manual

gistandard.c
1/*
2 * This file is part of the GIRAFFE Pipeline
3 * Copyright (C) 2002-2019 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <math.h>
25
26#include <cxmemory.h>
27#include <cxmessages.h>
28#include <cxslist.h>
29
30#include <cpl_recipe.h>
31#include <cpl_plugininfo.h>
32#include <cpl_parameterlist.h>
33#include <cpl_frameset.h>
34#include <cpl_msg.h>
35#include <cpl_errorstate.h>
36
37#include "gialias.h"
38#include "giframe.h"
39#include "gifibers.h"
40#include "gifiberutils.h"
41#include "gislitgeometry.h"
42#include "gipsfdata.h"
43#include "gibias.h"
44#include "gidark.h"
45#include "giextract.h"
46#include "giflat.h"
47#include "gitransmission.h"
48#include "girebinning.h"
49#include "gifov.h"
50#include "gifxcalibration.h"
51#include "gimessages.h"
52#include "gierror.h"
53#include "giqclog.h"
54#include "giutils.h"
55
56
57static cxint gistandard(cpl_parameterlist* config, cpl_frameset* set);
58static cxint giqcstandard(cpl_frameset* set);
59
60
61
62/*
63 * Create the recipe instance, i.e. setup the parameter list for this
64 * recipe and make it available to the application using the interface.
65 */
66
67static cxint
68gistandard_create(cpl_plugin* plugin)
69{
70
71 cpl_recipe* recipe = (cpl_recipe*)plugin;
72
73 cpl_parameter* p = NULL;
74
75
76 giraffe_error_init();
77
78
79 /*
80 * We have to provide the option we accept to the application. We
81 * need to setup our parameter list and hook it into the recipe
82 * interface.
83 */
84
85 recipe->parameters = cpl_parameterlist_new();
86 cx_assert(recipe->parameters != NULL);
87
88
89 /*
90 * Fill the parameter list.
91 */
92
93 /* Bias removal */
94
95 giraffe_bias_config_add(recipe->parameters);
96
97 /* Dark subtraction */
98
99 /* TBD */
100
101 /* Spectrum extraction */
102
103 giraffe_extract_config_add(recipe->parameters);
104
105 /* Flat fielding and relative fiber transmission correction */
106
107 giraffe_flat_config_add(recipe->parameters);
108
109 p = cpl_parameterlist_find(recipe->parameters, "giraffe.flat.apply");
110 cx_assert(p != NULL);
111
112 cpl_parameter_set_default_bool(p, FALSE);
113
114 /* Spectrum rebinning */
115
116 giraffe_rebin_config_add(recipe->parameters);
117
118 p = cpl_parameterlist_find(recipe->parameters,
119 "giraffe.rebinning.range");
120 cx_assert(p != NULL);
121
122 cpl_parameter_set_default_string(p, "common");
123
124 /* Image reconstruction (IFU and Argus only) */
125
126 giraffe_fov_config_add(recipe->parameters);
127
128 /* Flux calibration */
129
130 giraffe_fxcalibration_config_add(recipe->parameters);
131
132 return 0;
133
134}
135
136
137/*
138 * Execute the plugin instance given by the interface.
139 */
140
141static cxint
142gistandard_exec(cpl_plugin* plugin)
143{
144
145 cxint status = 0;
146
147 cpl_recipe* recipe = (cpl_recipe*)plugin;
148
149
150 cx_assert(recipe->parameters != NULL);
151 cx_assert(recipe->frames != NULL);
152
153 status = gistandard(recipe->parameters, recipe->frames);
154
155 if (status != 0) {
156 return 1;
157 }
158
159 status = giqcstandard(recipe->frames);
160
161 if (status != 0) {
162 return 1;
163 }
164
165 return 0;
166
167}
168
169
170static cxint
171gistandard_destroy(cpl_plugin* plugin)
172{
173
174 cpl_recipe* recipe = (cpl_recipe*)plugin;
175
176
177 /*
178 * We just destroy what was created during the plugin initialization
179 * phase, i.e. the parameter list. The frame set is managed by the
180 * application which called us, so we must not touch it,
181 */
182
183 cpl_parameterlist_delete(recipe->parameters);
184
185 giraffe_error_clear();
186
187 return 0;
188
189}
190
191
192/*
193 * The actual recipe starts here.
194 */
195
196static cxint
197gistandard(cpl_parameterlist* config, cpl_frameset* set)
198{
199
200 const cxchar* const _id = "gistandard";
201
202
203 const cxchar* filename = NULL;
204
205 cxint status = 0;
206
207 cxlong i;
208 cxlong nstandard = 0;
209
210 cxdouble exptime = 0.;
211
212 cx_slist* slist = NULL;
213
214 cpl_propertylist* properties = NULL;
215
216 cpl_matrix* biasareas = NULL;
217
218 cpl_frame* standard_frame = NULL;
219 cpl_frame* mbias_frame = NULL;
220 cpl_frame* mdark_frame = NULL;
221 cpl_frame* bpixel_frame = NULL;
222 cpl_frame* slight_frame = NULL;
223 cpl_frame* locy_frame = NULL;
224 cpl_frame* locw_frame = NULL;
225 cpl_frame* flat_frame = NULL;
226 cpl_frame* psfdata_frame = NULL;
227 cpl_frame* grating_frame = NULL;
228 cpl_frame* slit_frame = NULL;
229 cpl_frame* wcal_frame = NULL;
230 cpl_frame* rstandard_frame = NULL;
231 cpl_frame* sext_frame = NULL;
232 cpl_frame* rbin_frame = NULL;
233 cpl_frame* atmext_frame = NULL;
234 cpl_frame* flxstd_frame = NULL;
235 cpl_frame* rsp_frame = NULL;
236
237 GiImage* mbias = NULL;
238 GiImage* mdark = NULL;
239 GiImage* bpixel = NULL;
240 GiImage* slight = NULL;
241 GiImage* sstandard = NULL;
242 GiImage* rstandard = NULL;
243
244 GiTable* fibers = NULL;
245 GiTable* slitgeometry = NULL;
246 GiTable* grating = NULL;
247 GiTable* wcalcoeff = NULL;
248 GiTable* atmext = NULL;
249 GiTable* flxstd = NULL;
250 GiTable* refflx = NULL;
251
252 GiLocalization* localization = NULL;
253 GiExtraction* extraction = NULL;
254 GiRebinning* rebinning = NULL;
255 GiResponse* response = NULL;
256
257 GiBiasConfig* bias_config = NULL;
258 GiExtractConfig* extract_config = NULL;
259 GiFlatConfig* flat_config = NULL;
260 GiRebinConfig* rebin_config = NULL;
261 GiFxCalibrationConfig* fxcal_config = NULL;
262
263
264 GiInstrumentMode mode;
265
266 GiRecipeInfo info = {(cxchar*)_id, 1, NULL, config};
267
268 GiGroupInfo groups[] = {
269 {GIFRAME_STANDARD, CPL_FRAME_GROUP_RAW},
270 {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
271 {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
272 {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
273 {GIFRAME_FIBER_FLAT_EXTSPECTRA, CPL_FRAME_GROUP_CALIB},
274 {GIFRAME_FIBER_FLAT_EXTERRORS, CPL_FRAME_GROUP_CALIB},
275 {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
276 {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
277 {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
278 {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
279 {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
280 {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
281 {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
282 {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
283 {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
284 {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
285 {GIFRAME_EXTINCTION, CPL_FRAME_GROUP_CALIB},
286 {GIFRAME_FLUX_STANDARDS, CPL_FRAME_GROUP_CALIB},
287 {NULL, CPL_FRAME_GROUP_NONE}
288 };
289
290
291
292 if (!config) {
293 cpl_msg_error(_id, "Invalid parameter list! Aborting ...");
294 return 1;
295 }
296
297 if (!set) {
298 cpl_msg_error(_id, "Invalid frame set! Aborting ...");
299 return 1;
300 }
301
302 status = giraffe_frameset_set_groups(set, groups);
303
304 if (status != 0) {
305 cpl_msg_error(_id, "Setting frame group information failed!");
306 return 1;
307 }
308
309
310 /*
311 * Verify the frame set contents
312 */
313
314 nstandard = cpl_frameset_count_tags(set, GIFRAME_STANDARD);
315
316 if (nstandard < 1) {
317 cpl_msg_error(_id, "Too few (%ld) raw frames (%s) present in "
318 "frame set! Aborting ...", nstandard, GIFRAME_STANDARD);
319 return 1;
320 }
321
322 locy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
323
324 if (locy_frame == NULL) {
325
326 locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
327
328 if (locy_frame == NULL) {
329 cpl_msg_info(_id, "No master localization (centroid position) "
330 "present in frame set. Aborting ...");
331 return 1;
332 }
333
334 }
335
336 locw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
337
338 if (locw_frame == NULL) {
339
340 locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
341
342 if (locw_frame == NULL) {
343 cpl_msg_info(_id, "No master localization (spectrum width) "
344 "present in frame set. Aborting ...");
345 return 1;
346 }
347
348 }
349
350 flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTSPECTRA);
351
352 if (flat_frame == NULL) {
353
354 cpl_msg_error(_id, "No extracted flat field spectra frame present in frame set. "
355 "Aborting ...");
356 return 1;
357
358 }
359
360 grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
361
362 if (!grating_frame) {
363 cpl_msg_error(_id, "No grating data present in frame set. "
364 "Aborting ...");
365 return 1;
366 }
367
368 slit_frame = giraffe_get_slitgeometry(set);
369
370 if (!slit_frame) {
371 cpl_msg_error(_id, "No slit geometry present in frame set. "
372 "Aborting ...");
373 return 1;
374 }
375
376 wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
377
378 if (!wcal_frame) {
379 cpl_msg_error(_id, "No dispersion solution present in frame set. "
380 "Aborting ...");
381 return 1;
382 }
383
384 atmext_frame = cpl_frameset_find(set, GIFRAME_EXTINCTION);
385
386 if (!atmext_frame) {
387 cpl_msg_error(_id, "No atmospheric extinction table present in "
388 "frame set. Aborting ...");
389 return 1;
390 }
391
392 flxstd_frame = cpl_frameset_find(set, GIFRAME_FLUX_STANDARDS);
393
394 if (!flxstd_frame) {
395 cpl_msg_error(_id, "No flux standards present in frame set. "
396 "Aborting ...");
397 return 1;
398 }
399
400 bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
401
402 if (!bpixel_frame) {
403 cpl_msg_info(_id, "No bad pixel map present in frame set.");
404 }
405
406 mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
407
408 if (!mbias_frame) {
409 cpl_msg_info(_id, "No master bias present in frame set.");
410 }
411
412 mdark_frame = cpl_frameset_find(set, GIFRAME_DARK_MASTER);
413
414 if (!mdark_frame) {
415 cpl_msg_info(_id, "No master dark present in frame set.");
416 }
417
418 slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
419
420 if (!slight_frame) {
421 cpl_msg_info(_id, "No scattered light model present in frame set.");
422 }
423
424 psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
425
426 if (!psfdata_frame) {
427 cpl_msg_info(_id, "No PSF profile parameters present in frame set.");
428 }
429
430
431 /*
432 * Load raw images
433 */
434
435 slist = cx_slist_new();
436
437 standard_frame = cpl_frameset_find(set, GIFRAME_STANDARD);
438
439 for (i = 0; i < nstandard; i++) {
440
441 filename = cpl_frame_get_filename(standard_frame);
442
443 GiImage* raw = giraffe_image_new(CPL_TYPE_DOUBLE);
444
445
446 status = giraffe_image_load(raw, filename, 0);
447
448 if (status) {
449 cpl_msg_error(_id, "Cannot load raw standard frame from '%s'. "
450 "Aborting ...", filename);
451
452 cx_slist_destroy(slist, (cx_free_func) giraffe_image_delete);
453
454 return 1;
455 }
456
457 cx_slist_push_back(slist, raw);
458
459 standard_frame = cpl_frameset_find(set, NULL);
460
461 }
462
463 nstandard = (cxint)cx_slist_size(slist);
464 sstandard = cx_slist_pop_front(slist);
465
466 properties = giraffe_image_get_properties(sstandard);
467 cx_assert(properties != NULL);
468
469 if (nstandard > 1) {
470
471 /*
472 * Create a stacked image from the list of raw images.
473 * Each raw image is disposed when it is no longer needed.
474 */
475
476 cpl_msg_info(_id, "Averaging standard star observations ...");
477
478 exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
479
480 for (i = 1; i < nstandard; i++) {
481
482 cpl_propertylist* _properties;
483
484 GiImage* standard = cx_slist_pop_front(slist);
485
486
487 cpl_image_add(giraffe_image_get(sstandard),
488 giraffe_image_get(standard));
489
490 _properties = giraffe_image_get_properties(standard);
491 cx_assert(_properties != NULL);
492
493 exptime += cpl_propertylist_get_double(_properties,
494 GIALIAS_EXPTIME);
495
496 giraffe_image_delete(standard);
497
498 }
499
500 cpl_image_divide_scalar(giraffe_image_get(sstandard), nstandard);
501 }
502
503 cx_assert(cx_slist_empty(slist));
504 cx_slist_delete(slist);
505 slist = NULL;
506
507
508 if (nstandard > 1) {
509
510 /*
511 * Update stacked standard star image properties
512 */
513
514 cpl_msg_info(_id, "Updating stacked standard star image "
515 "properties ...");
516
517 cpl_propertylist_set_double(properties, GIALIAS_EXPTIME,
518 exptime / nstandard);
519
520 cpl_propertylist_append_double(properties, GIALIAS_EXPTTOT, exptime);
521 cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
522 "Total exposure time of all frames "
523 "combined");
524
525
526 cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
527
528 }
529
530 cpl_propertylist_append_int(properties, GIALIAS_DATANCOM, nstandard);
531 cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM, "Number of "
532 "combined frames");
533
534
535 /*
536 * Prepare for bias subtraction
537 */
538
539 bias_config = giraffe_bias_config_create(config);
540
541 /*
542 * Setup user defined areas to use for the bias computation
543 */
544
545 if (bias_config->method == GIBIAS_METHOD_MASTER ||
546 bias_config->method == GIBIAS_METHOD_ZMASTER) {
547
548 if (!mbias_frame) {
549 cpl_msg_error(_id, "Missing master bias frame! Selected bias "
550 "removal method requires a master bias frame!");
551
552 giraffe_bias_config_destroy(bias_config);
553 giraffe_image_delete(sstandard);
554
555 return 1;
556 }
557 else {
558 filename = cpl_frame_get_filename(mbias_frame);
559
560
561 mbias = giraffe_image_new(CPL_TYPE_DOUBLE);
562 status = giraffe_image_load(mbias, filename, 0);
563
564 if (status) {
565 cpl_msg_error(_id, "Cannot load master bias from '%s'. "
566 "Aborting ...", filename);
567
568 giraffe_bias_config_destroy(bias_config);
569 giraffe_image_delete(sstandard);
570
571 return 1;
572 }
573 }
574 }
575
576
577 /*
578 * Load bad pixel map if it is present in the frame set.
579 */
580
581 if (bpixel_frame) {
582
583 filename = cpl_frame_get_filename(bpixel_frame);
584
585
586 bpixel = giraffe_image_new(CPL_TYPE_INT);
587 status = giraffe_image_load(bpixel, filename, 0);
588
589 if (status) {
590 cpl_msg_error(_id, "Cannot load bad pixel map from '%s'. "
591 "Aborting ...", filename);
592
593 giraffe_image_delete(bpixel);
594 bpixel = NULL;
595
596 if (mbias != NULL) {
598 mbias = NULL;
599 }
600
601 giraffe_bias_config_destroy(bias_config);
602 bias_config = NULL;
603
604 giraffe_image_delete(sstandard);
605 sstandard = NULL;
606
607 return 1;
608 }
609
610 }
611
612
613 /*
614 * Compute and remove the bias from the stacked flat field frame.
615 */
616
617 rstandard = giraffe_image_new(CPL_TYPE_DOUBLE);
618
619 status = giraffe_bias_remove(rstandard, sstandard, mbias, bpixel,
620 biasareas, bias_config);
621
622 giraffe_image_delete(sstandard);
623
624 if (mbias) {
626 mbias = NULL;
627 }
628
629 giraffe_bias_config_destroy(bias_config);
630
631 if (status) {
632 cpl_msg_error(_id, "Bias removal failed. Aborting ...");
633
634 giraffe_image_delete(rstandard);
635 rstandard = NULL;
636
637 if (bpixel != NULL) {
638 giraffe_image_delete(bpixel);
639 bpixel = NULL;
640 }
641
642 return 1;
643 }
644
645
646 /*
647 * Load master dark if it is present in the frame set and correct
648 * the master flat field for the dark current.
649 */
650
651 if (mdark_frame) {
652
653 GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
654
655
656 cpl_msg_info(_id, "Correcting for dark current ...");
657
658 filename = cpl_frame_get_filename(mdark_frame);
659
660 mdark = giraffe_image_new(CPL_TYPE_DOUBLE);
661 status = giraffe_image_load(mdark, filename, 0);
662
663 if (status != 0) {
664 cpl_msg_error(_id, "Cannot load master dark from '%s'. "
665 "Aborting ...", filename);
666
667 giraffe_image_delete(rstandard);
668 rstandard = NULL;
669
670 if (bpixel != NULL) {
671 giraffe_image_delete(bpixel);
672 bpixel = NULL;
673 }
674
675 return 1;
676 }
677
678 status = giraffe_subtract_dark(rstandard, mdark, bpixel, NULL,
679 &dark_config);
680
681 if (status != 0) {
682 cpl_msg_error(_id, "Dark subtraction failed! Aborting ...");
683
685 mdark = NULL;
686
687 giraffe_image_delete(rstandard);
688 rstandard = NULL;
689
690 if (bpixel != NULL) {
691 giraffe_image_delete(bpixel);
692 bpixel = NULL;
693 }
694
695 return 1;
696 }
697
699 mdark = NULL;
700
701 }
702
703
704 /*
705 * Update the reduced standard star observation properties, save the
706 * reduced standard star frame and register it as product.
707 */
708
709 cpl_msg_info(_id, "Writing pre-processed standard star image ...");
710
711 giraffe_image_add_info(rstandard, &info, set);
712
713 rstandard_frame = giraffe_frame_create_image(rstandard,
714 GIFRAME_STANDARD_REDUCED,
715 CPL_FRAME_LEVEL_INTERMEDIATE,
716 TRUE, TRUE);
717
718 if (rstandard_frame == NULL) {
719 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
720
721 giraffe_image_delete(rstandard);
722
723 return 1;
724 }
725
726 cpl_frameset_insert(set, rstandard_frame);
727
728
729 /*
730 * Determine fiber setup
731 */
732
733 standard_frame = cpl_frameset_find(set, GIFRAME_STANDARD);
734
735 cpl_msg_info(_id, "Building fiber setup for frame '%s'.",
736 cpl_frame_get_filename(standard_frame));
737
738 fibers = giraffe_fibers_setup(standard_frame, locy_frame);
739
740 if (!fibers) {
741 cpl_msg_error(_id, "Cannot create fiber setup for frame '%s'! "
742 "Aborting ...", cpl_frame_get_filename(standard_frame));
743
744 if (bpixel) {
745 giraffe_image_delete(bpixel);
746 bpixel = NULL;
747 }
748
749 giraffe_image_delete(rstandard);
750
751 return 1;
752 }
753
754 cpl_msg_info(_id, "Fiber reference setup taken from localization "
755 "frame '%s'.", cpl_frame_get_filename(locy_frame));
756
757
758 /*
759 * Load fiber localization
760 */
761
762 localization = giraffe_localization_new();
763
764 filename = cpl_frame_get_filename(locy_frame);
765 status = 0;
766
767 localization->locy = giraffe_image_new(CPL_TYPE_DOUBLE);
768 status = giraffe_image_load(localization->locy, filename, 0);
769
770 if (status) {
771 cpl_msg_error(_id, "Cannot load localization (centroid "
772 "position) frame from '%s'. Aborting ...",
773 filename);
774
775 giraffe_localization_destroy(localization);
776
777 if (bpixel) {
778 giraffe_image_delete(bpixel);
779 bpixel = NULL;
780 }
781
782 giraffe_table_delete(fibers);
783 giraffe_image_delete(rstandard);
784
785 return 1;
786 }
787
788
789 filename = cpl_frame_get_filename(locw_frame);
790 status = 0;
791
792 localization->locw = giraffe_image_new(CPL_TYPE_DOUBLE);
793 status = giraffe_image_load(localization->locw, filename, 0);
794
795 if (status) {
796 cpl_msg_error(_id, "Cannot load localization (spectrum width) "
797 "frame from '%s'. Aborting ...", filename);
798
799 giraffe_localization_destroy(localization);
800
801 if (bpixel) {
802 giraffe_image_delete(bpixel);
803 bpixel = NULL;
804 }
805
806 giraffe_table_delete(fibers);
807 giraffe_image_delete(rstandard);
808
809 return 1;
810 }
811
812
813 /*
814 * Spectrum extraction
815 */
816
817 if (slight_frame) {
818
819 filename = cpl_frame_get_filename(slight_frame);
820
821
822 slight = giraffe_image_new(CPL_TYPE_DOUBLE);
823 status = giraffe_image_load(slight, filename, 0);
824
825 if (status) {
826 cpl_msg_error(_id, "Cannot load scattered light model from '%s'. "
827 "Aborting ...", filename);
828
829 giraffe_image_delete(slight);
830
831 giraffe_localization_destroy(localization);
832
833 if (bpixel) {
834 giraffe_image_delete(bpixel);
835 bpixel = NULL;
836 }
837
838 giraffe_table_delete(fibers);
839 giraffe_image_delete(rstandard);
840
841 return 1;
842
843 }
844
845 }
846
847
848 extract_config = giraffe_extract_config_create(config);
849
850 if ((extract_config->emethod == GIEXTRACT_OPTIMAL) ||
851 (extract_config->emethod == GIEXTRACT_HORNE)) {
852
853 if (psfdata_frame == NULL) {
854
855 const cxchar* emethod = "Optimal";
856
857 if (extract_config->emethod == GIEXTRACT_HORNE) {
858 emethod = "Horne";
859 }
860
861 cpl_msg_error(_id, "%s spectrum extraction requires PSF "
862 "profile data. Aborting ...", emethod);
863
864 giraffe_extract_config_destroy(extract_config);
865 extract_config = NULL;
866
867 if (slight != NULL) {
868 giraffe_image_delete(slight);
869 slight = NULL;
870 }
871
872 giraffe_localization_destroy(localization);
873 localization = NULL;
874
875 if (bpixel) {
876 giraffe_image_delete(bpixel);
877 bpixel = NULL;
878 }
879
880 giraffe_table_delete(fibers);
881 fibers = NULL;
882
883 giraffe_image_delete(rstandard);
884 rstandard = NULL;
885
886 return 1;
887
888 }
889 else {
890
891 filename = cpl_frame_get_filename(psfdata_frame);
892 status = 0;
893
894 localization->psf = giraffe_psfdata_new();
895 status = giraffe_psfdata_load(localization->psf, filename);
896
897 if (status) {
898 cpl_msg_error(_id, "Cannot load PSF profile data frame from "
899 "'%s'. Aborting ...", filename);
900
901 giraffe_extract_config_destroy(extract_config);
902 extract_config = NULL;
903
904 if (slight != NULL) {
905 giraffe_image_delete(slight);
906 slight = NULL;
907 }
908
909 giraffe_localization_destroy(localization);
910 localization = NULL;
911
912 if (bpixel) {
913 giraffe_image_delete(bpixel);
914 bpixel = NULL;
915 }
916
917 giraffe_table_delete(fibers);
918 fibers = NULL;
919
920 giraffe_image_delete(rstandard);
921 rstandard = NULL;
922
923 return 1;
924
925 }
926
927 }
928
929 }
930
931
932 extraction = giraffe_extraction_new();
933
934 status = giraffe_extract_spectra(extraction, rstandard, fibers,
935 localization, bpixel, slight,
936 extract_config);
937
938 if (status) {
939 cpl_msg_error(_id, "Spectrum extraction failed! Aborting ...");
940
941 giraffe_extraction_destroy(extraction);
942 giraffe_extract_config_destroy(extract_config);
943
944 giraffe_image_delete(slight);
945
946 giraffe_localization_destroy(localization);
947
948 if (bpixel) {
949 giraffe_image_delete(bpixel);
950 bpixel = NULL;
951 }
952
953 giraffe_table_delete(fibers);
954 giraffe_image_delete(rstandard);
955
956 return 1;
957 }
958
959 giraffe_image_delete(slight);
960 slight = NULL;
961
962 if (bpixel) {
963 giraffe_image_delete(bpixel);
964 bpixel = NULL;
965 }
966
967 giraffe_image_delete(rstandard);
968 rstandard = NULL;
969
970 giraffe_extract_config_destroy(extract_config);
971
972
973 /*
974 * Get the relative fiber transmission data from the extracted flat
975 * field and add it to the fiber table.
976 */
977
978 filename = cpl_frame_get_filename(flat_frame);
979
980 cpl_msg_info(_id, "Loading relative fiber transmission data from '%s'.",
981 filename);
982
983 status = giraffe_transmission_attach(fibers, filename);
984
985 if (status != 0) {
986
987 giraffe_extraction_destroy(extraction);
988 giraffe_localization_destroy(localization);
989
990 giraffe_table_delete(grating);
991 giraffe_table_delete(fibers);
992
993 cpl_msg_error(_id, "Cannot load relative fiber transmission data "
994 "from '%s'. Aborting ...", filename);
995
996 return 1;
997
998 }
999
1000
1001 /*
1002 * Apply flat field and/or the relative fiber transmission correction.
1003 */
1004
1005 flat_config = giraffe_flat_config_create(config);
1006
1007 if (flat_config->apply == TRUE) {
1008
1009 const cpl_frame* flat_errors_frame = NULL;
1010
1011 GiImage* flat = NULL;
1012 GiImage* errors = NULL;
1013
1014
1015 filename = cpl_frame_get_filename(flat_frame);
1016
1017 flat = giraffe_image_new(CPL_TYPE_DOUBLE);
1018 status = giraffe_image_load(flat, filename, 0);
1019
1020 if (status) {
1021 cpl_msg_error(_id, "Cannot load flat field spectra from '%s'. "
1022 "Aborting ...", filename);
1023
1025
1026 giraffe_flat_config_destroy(flat_config);
1027
1028 giraffe_extraction_destroy(extraction);
1029 giraffe_localization_destroy(localization);
1030
1031 giraffe_table_delete(grating);
1032 giraffe_table_delete(fibers);
1033
1034 return 1;
1035 }
1036
1037
1038 flat_errors_frame =
1039 cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTERRORS);
1040
1041 if (flat_errors_frame == NULL) {
1042 cpl_msg_warning(_id, "Missing flat field spectra errors "
1043 "frame!");
1044 }
1045 else {
1046
1047 filename = cpl_frame_get_filename(flat_errors_frame);
1048
1049 errors = giraffe_image_new(CPL_TYPE_DOUBLE);
1050 status = giraffe_image_load(errors, filename, 0);
1051
1052 if (status) {
1053 cpl_msg_error(_id, "Cannot load flat field spectra "
1054 "errors from '%s'. Aborting ...",
1055 filename);
1056
1057 giraffe_image_delete(errors);
1059
1060 giraffe_flat_config_destroy(flat_config);
1061
1062 giraffe_extraction_destroy(extraction);
1063 giraffe_localization_destroy(localization);
1064
1065 giraffe_table_delete(grating);
1066 giraffe_table_delete(fibers);
1067
1068 return 1;
1069 }
1070
1071 }
1072
1073 cpl_msg_info(_id, "Applying flat field correction ...");
1074
1075 status = giraffe_flat_apply(extraction, fibers, flat, errors,
1076 flat_config);
1077
1078 if (status) {
1079 cpl_msg_error(_id, "Flat field correction failed! "
1080 "Aborting ...");
1081
1082 giraffe_image_delete(errors);
1084
1085 giraffe_flat_config_destroy(flat_config);
1086
1087 giraffe_extraction_destroy(extraction);
1088 giraffe_localization_destroy(localization);
1089
1090 giraffe_table_delete(grating);
1091 giraffe_table_delete(fibers);
1092
1093 return 1;
1094 }
1095
1096 giraffe_image_delete(errors);
1097 errors = NULL;
1098
1100 flat = NULL;
1101
1102 }
1103
1104 if (flat_config->transmission == TRUE) {
1105
1106 cpl_msg_info(_id, "Applying relative fiber transmission "
1107 "correction");
1108
1109 status = giraffe_transmission_apply(extraction, fibers);
1110
1111 if (status) {
1112
1113 cpl_msg_error(_id, "Relative transmission correction failed! "
1114 "Aborting ...");
1115
1116 giraffe_flat_config_destroy(flat_config);
1117 flat_config = NULL;
1118
1119 giraffe_extraction_destroy(extraction);
1120 giraffe_localization_destroy(localization);
1121
1122 giraffe_table_delete(grating);
1123 giraffe_table_delete(fibers);
1124
1125 return 1;
1126
1127 }
1128
1129 }
1130
1131 giraffe_flat_config_destroy(flat_config);
1132 flat_config = NULL;
1133
1134
1135 /*
1136 * Save the spectrum extraction results and register them as
1137 * products.
1138 */
1139
1140 cpl_msg_info(_id, "Writing extracted spectra ...");
1141
1142 /* Extracted spectra */
1143
1144 giraffe_image_add_info(extraction->spectra, &info, set);
1145
1146 sext_frame = giraffe_frame_create_image(extraction->spectra,
1147 GIFRAME_STANDARD_EXTSPECTRA,
1148 CPL_FRAME_LEVEL_FINAL,
1149 TRUE, TRUE);
1150
1151 if (sext_frame == NULL) {
1152 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1153
1154 giraffe_extraction_destroy(extraction);
1155 giraffe_localization_destroy(localization);
1156
1157 giraffe_table_delete(grating);
1158 giraffe_table_delete(fibers);
1159
1160 return 1;
1161 }
1162
1163 status = giraffe_fiberlist_attach(sext_frame, fibers);
1164
1165 if (status) {
1166 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1167 "Aborting ...", cpl_frame_get_filename(sext_frame));
1168
1169 cpl_frame_delete(sext_frame);
1170
1171 giraffe_extraction_destroy(extraction);
1172 giraffe_localization_destroy(localization);
1173
1174 giraffe_table_delete(grating);
1175 giraffe_table_delete(fibers);
1176
1177 return 1;
1178 }
1179
1180 cpl_frameset_insert(set, sext_frame);
1181
1182 /* Extracted spectra errors */
1183
1184 giraffe_image_add_info(extraction->error, &info, set);
1185
1186 sext_frame = giraffe_frame_create_image(extraction->error,
1187 GIFRAME_STANDARD_EXTERRORS,
1188 CPL_FRAME_LEVEL_FINAL,
1189 TRUE, TRUE);
1190
1191 if (sext_frame == NULL) {
1192 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1193
1194 giraffe_extraction_destroy(extraction);
1195 giraffe_localization_destroy(localization);
1196
1197 giraffe_table_delete(grating);
1198 giraffe_table_delete(fibers);
1199
1200 return 1;
1201 }
1202
1203 status = giraffe_fiberlist_attach(sext_frame, fibers);
1204
1205 if (status) {
1206 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1207 "Aborting ...", cpl_frame_get_filename(sext_frame));
1208
1209 cpl_frame_delete(sext_frame);
1210
1211 giraffe_extraction_destroy(extraction);
1212 giraffe_localization_destroy(localization);
1213
1214 giraffe_table_delete(grating);
1215 giraffe_table_delete(fibers);
1216
1217 return 1;
1218 }
1219
1220 cpl_frameset_insert(set, sext_frame);
1221
1222 /* Extracted spectra pixels */
1223
1224 if (extraction->npixels != NULL) {
1225
1226 giraffe_image_add_info(extraction->npixels, &info, set);
1227
1228 sext_frame = giraffe_frame_create_image(extraction->npixels,
1229 GIFRAME_STANDARD_EXTPIXELS,
1230 CPL_FRAME_LEVEL_FINAL,
1231 TRUE, TRUE);
1232
1233 if (sext_frame == NULL) {
1234 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1235
1236 giraffe_extraction_destroy(extraction);
1237 giraffe_localization_destroy(localization);
1238
1239 giraffe_table_delete(grating);
1240 giraffe_table_delete(fibers);
1241
1242 return 1;
1243 }
1244
1245 status = giraffe_fiberlist_attach(sext_frame, fibers);
1246
1247 if (status) {
1248 cpl_msg_error(_id, "Cannot attach fiber setup to local file "
1249 "'%s'! Aborting ...",
1250 cpl_frame_get_filename(sext_frame));
1251
1252 cpl_frame_delete(sext_frame);
1253
1254 giraffe_extraction_destroy(extraction);
1255 giraffe_localization_destroy(localization);
1256
1257 giraffe_table_delete(grating);
1258 giraffe_table_delete(fibers);
1259
1260 return 1;
1261 }
1262
1263 cpl_frameset_insert(set, sext_frame);
1264
1265 }
1266
1267 /* Extracted spectra centroids */
1268
1269 giraffe_image_add_info(extraction->centroid, &info, set);
1270
1271 sext_frame = giraffe_frame_create_image(extraction->centroid,
1272 GIFRAME_STANDARD_EXTTRACE,
1273 CPL_FRAME_LEVEL_FINAL,
1274 TRUE, TRUE);
1275
1276 if (sext_frame == NULL) {
1277 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1278
1279 giraffe_extraction_destroy(extraction);
1280 giraffe_localization_destroy(localization);
1281
1282 giraffe_table_delete(grating);
1283 giraffe_table_delete(fibers);
1284
1285 return 1;
1286 }
1287
1288 status = giraffe_fiberlist_attach(sext_frame, fibers);
1289
1290 if (status) {
1291 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1292 "Aborting ...", cpl_frame_get_filename(sext_frame));
1293
1294 cpl_frame_delete(sext_frame);
1295
1296 giraffe_extraction_destroy(extraction);
1297 giraffe_localization_destroy(localization);
1298
1299 giraffe_table_delete(grating);
1300 giraffe_table_delete(fibers);
1301
1302 return 1;
1303 }
1304
1305 cpl_frameset_insert(set, sext_frame);
1306
1307 /* Extraction model spectra */
1308
1309 if (extraction->model != NULL) {
1310
1311 giraffe_image_add_info(extraction->model, &info, set);
1312
1313 sext_frame = giraffe_frame_create_image(extraction->model,
1314 GIFRAME_STANDARD_EXTMODEL,
1315 CPL_FRAME_LEVEL_FINAL,
1316 TRUE, TRUE);
1317
1318 if (sext_frame == NULL) {
1319 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1320
1321 giraffe_extraction_destroy(extraction);
1322 giraffe_localization_destroy(localization);
1323
1324 giraffe_table_delete(grating);
1325 giraffe_table_delete(fibers);
1326
1327 return 1;
1328 }
1329
1330 status = giraffe_fiberlist_attach(sext_frame, fibers);
1331
1332 if (status != 0) {
1333 cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1334 "Aborting ...", cpl_frame_get_filename(sext_frame));
1335
1336 cpl_frame_delete(sext_frame);
1337
1338 giraffe_extraction_destroy(extraction);
1339 giraffe_localization_destroy(localization);
1340
1341 giraffe_table_delete(grating);
1342 giraffe_table_delete(fibers);
1343
1344 return 1;
1345 }
1346
1347 cpl_frameset_insert(set, sext_frame);
1348
1349 }
1350
1351
1352 /*
1353 * Load dispersion solution
1354 */
1355
1356
1357 filename = (cxchar *)cpl_frame_get_filename(wcal_frame);
1358
1359 wcalcoeff = giraffe_table_new();
1360 status = giraffe_table_load(wcalcoeff, filename, 1, NULL);
1361
1362 if (status) {
1363 cpl_msg_error(_id, "Cannot load dispersion solution from "
1364 "'%s'. Aborting ...", filename);
1365
1366 giraffe_extraction_destroy(extraction);
1367 giraffe_localization_destroy(localization);
1368
1369 giraffe_table_delete(wcalcoeff);
1370
1371 giraffe_table_delete(grating);
1372 giraffe_table_delete(fibers);
1373
1374 return 1;
1375 }
1376
1377
1378 /*
1379 * Load grating data
1380 */
1381
1382 filename = (cxchar *)cpl_frame_get_filename(grating_frame);
1383
1384 status = 0;
1385
1386 grating = giraffe_table_new();
1387 status = giraffe_table_load(grating, filename, 1, NULL);
1388
1389 if (status) {
1390 cpl_msg_error(_id, "Cannot load grating data from '%s'. "
1391 "Aborting ...", filename);
1392
1393 giraffe_extraction_destroy(extraction);
1394 giraffe_localization_destroy(localization);
1395
1396 giraffe_table_delete(wcalcoeff);
1397
1398 giraffe_table_delete(grating);
1399 giraffe_table_delete(fibers);
1400
1401 return 1;
1402 }
1403
1404
1405 /*
1406 * Load slit geometry data
1407 */
1408
1409
1410 filename = (cxchar *)cpl_frame_get_filename(slit_frame);
1411
1412 slitgeometry = giraffe_slitgeometry_load(fibers, filename, 1, NULL);
1413
1414 if (slitgeometry == NULL) {
1415 cpl_msg_error(_id, "Cannot load slit geometry data from '%s'. "
1416 "Aborting ...", filename);
1417
1418 giraffe_table_delete(wcalcoeff);
1419
1420 giraffe_extraction_destroy(extraction);
1421 giraffe_localization_destroy(localization);
1422
1423 giraffe_table_delete(wcalcoeff);
1424
1425 giraffe_table_delete(grating);
1426 giraffe_table_delete(fibers);
1427
1428 return 1;
1429 }
1430 else {
1431
1432 /*
1433 * Check whether the contains the positions for all fibers
1434 * provided by the fiber setup. If this is not the case
1435 * this is an error.
1436 */
1437
1438 if (giraffe_fiberlist_compare(slitgeometry, fibers) != 1) {
1439 cpl_msg_error(_id, "Slit geometry data from '%s' is not "
1440 "applicable for current fiber setup! "
1441 "Aborting ...", filename);
1442
1443 giraffe_table_delete(slitgeometry);
1444 giraffe_table_delete(wcalcoeff);
1445
1446 giraffe_extraction_destroy(extraction);
1447 giraffe_localization_destroy(localization);
1448
1449 giraffe_table_delete(wcalcoeff);
1450
1451 giraffe_table_delete(grating);
1452 giraffe_table_delete(fibers);
1453
1454 return 1;
1455 }
1456
1457 }
1458
1459
1460 /*
1461 * Spectrum rebinning
1462 */
1463
1464 cpl_msg_info(_id, "Spectrum rebinning");
1465
1466 rebin_config = giraffe_rebin_config_create(config);
1467
1468 rebinning = giraffe_rebinning_new();
1469
1470 status = 0;
1471
1472 status = giraffe_rebin_spectra(rebinning, extraction, fibers,
1473 localization, grating, slitgeometry,
1474 wcalcoeff, rebin_config);
1475
1476 if (status) {
1477 cpl_msg_error(_id, "Rebinning of standard spectra failed! "
1478 "Aborting...");
1479
1480 giraffe_rebinning_destroy(rebinning);
1481
1482 giraffe_extraction_destroy(extraction);
1483 giraffe_localization_destroy(localization);
1484
1485 giraffe_table_delete(wcalcoeff);
1486
1487 giraffe_table_delete(slitgeometry);
1488 giraffe_table_delete(grating);
1489 giraffe_table_delete(fibers);
1490
1491 giraffe_rebin_config_destroy(rebin_config);
1492
1493 return 1;
1494
1495 }
1496
1497 giraffe_extraction_destroy(extraction);
1498 extraction = NULL;
1499
1500 giraffe_localization_destroy(localization);
1501 localization = NULL;
1502
1503 giraffe_rebin_config_destroy(rebin_config);
1504 rebin_config = NULL;
1505
1506
1507 /*
1508 * Save and register the results of the spectrum rebinning.
1509 */
1510
1511 /* Rebinned spectra */
1512
1513 giraffe_image_add_info(rebinning->spectra, &info, set);
1514
1515 rbin_frame = giraffe_frame_create_image(rebinning->spectra,
1516 GIFRAME_STANDARD_RBNSPECTRA,
1517 CPL_FRAME_LEVEL_FINAL,
1518 TRUE, TRUE);
1519
1520 if (rbin_frame == NULL) {
1521 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1522
1523 giraffe_rebinning_destroy(rebinning);
1524
1525 giraffe_table_delete(wcalcoeff);
1526
1527 giraffe_table_delete(slitgeometry);
1528 giraffe_table_delete(grating);
1529 giraffe_table_delete(fibers);
1530
1531 return 1;
1532 }
1533
1534 status = giraffe_fiberlist_attach(rbin_frame, fibers);
1535
1536 if (status) {
1537 cpl_msg_error(_id, "Cannot attach fiber setup to local "
1538 "file '%s'! Aborting ...",
1539 cpl_frame_get_filename(rbin_frame));
1540
1541 giraffe_rebinning_destroy(rebinning);
1542 giraffe_table_delete(wcalcoeff);
1543
1544 giraffe_table_delete(slitgeometry);
1545 giraffe_table_delete(grating);
1546 giraffe_table_delete(fibers);
1547
1548 cpl_frame_delete(rbin_frame);
1549
1550 return 1;
1551 }
1552
1553 cpl_frameset_insert(set, rbin_frame);
1554
1555 /* Rebinned spectra errors */
1556
1557 giraffe_image_add_info(rebinning->errors, &info, set);
1558
1559 rbin_frame = giraffe_frame_create_image(rebinning->errors,
1560 GIFRAME_STANDARD_RBNERRORS,
1561 CPL_FRAME_LEVEL_FINAL,
1562 TRUE, TRUE);
1563
1564 if (rbin_frame == NULL) {
1565 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1566
1567 giraffe_rebinning_destroy(rebinning);
1568
1569 giraffe_table_delete(wcalcoeff);
1570
1571 giraffe_table_delete(slitgeometry);
1572 giraffe_table_delete(grating);
1573 giraffe_table_delete(fibers);
1574
1575 return 1;
1576 }
1577
1578 status = giraffe_fiberlist_attach(rbin_frame, fibers);
1579
1580 if (status) {
1581 cpl_msg_error(_id, "Cannot attach fiber setup to local "
1582 "file '%s'! Aborting ...",
1583 cpl_frame_get_filename(rbin_frame));
1584
1585 giraffe_rebinning_destroy(rebinning);
1586
1587 giraffe_table_delete(wcalcoeff);
1588
1589 giraffe_table_delete(slitgeometry);
1590 giraffe_table_delete(grating);
1591 giraffe_table_delete(fibers);
1592
1593 cpl_frame_delete(rbin_frame);
1594
1595 return 1;
1596 }
1597
1598 cpl_frameset_insert(set, rbin_frame);
1599
1600
1601 /*
1602 * Optional image and data cube construction (only for IFU and Argus)
1603 */
1604
1605 properties = giraffe_image_get_properties(rebinning->spectra);
1606 mode = giraffe_get_mode(properties);
1607
1608
1609 if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
1610
1611 cpl_frame* rimg_frame = NULL;
1612
1613 GiFieldOfView* fov = NULL;
1614
1615 GiFieldOfViewConfig* fov_config = NULL;
1616
1617 GiFieldOfViewCubeFormat cube_format = GIFOV_FORMAT_ESO3D;
1618
1619
1620 fov_config = giraffe_fov_config_create(config);
1621
1622 cube_format = fov_config->format;
1623
1624
1625 cpl_msg_info(_id, "Reconstructing image and data cube from rebinned "
1626 "spectra ...");
1627
1628 fov = giraffe_fov_new();
1629
1630 status = giraffe_fov_build(fov, rebinning, fibers, wcalcoeff, grating,
1631 slitgeometry, fov_config);
1632
1633 if (status) {
1634
1635 if (status == -2) {
1636 cpl_msg_warning(_id, "No reconstructed image was built. "
1637 "Fiber list has no fiber position "
1638 "information.");
1639 }
1640 else {
1641 cpl_msg_error(_id, "Image reconstruction failed! Aborting...");
1642
1643 giraffe_fov_delete(fov);
1644 giraffe_rebinning_destroy(rebinning);
1645
1646 giraffe_table_delete(wcalcoeff);
1647
1648 giraffe_table_delete(slitgeometry);
1649 giraffe_table_delete(grating);
1650 giraffe_table_delete(fibers);
1651
1652 giraffe_fov_config_destroy(fov_config);
1653
1654 return 1;
1655 }
1656
1657 }
1658
1659 giraffe_fov_config_destroy(fov_config);
1660
1661 /*
1662 * Calculate QC params and add to header.
1663 */
1664
1665 cpl_errorstate tempes = cpl_errorstate_get();
1666
1667 double fovFlux = 0.0;
1668 double badcolFlux = 0.0;
1669 double meanFlux = 0.0;
1670 cpl_image* fovimage = giraffe_image_get(fov->fov.spectra);
1671 cpl_propertylist* fovprop = giraffe_image_get_properties(fov->fov.spectra);
1672 int fovnx = cpl_image_get_size_x(fovimage);
1673 int fovny = cpl_image_get_size_y(fovimage);
1674
1675 fovFlux = cpl_image_get_flux(fovimage);
1676 badcolFlux = cpl_image_get_flux_window(fovimage, fovnx- 1, 1, fovnx - 1, fovny);
1677
1678 meanFlux = (fovFlux - badcolFlux) / ((fovnx -1) * fovny);
1679
1680
1681
1682
1683 cpl_propertylist_update_double(fovprop, GIALIAS_QCSIGMEAN, meanFlux);
1684 cpl_propertylist_set_comment(fovprop, GIALIAS_QCSIGMEAN, "Mean of "
1685 "FOV image without bad row");
1686
1687 cpl_errorstate_set(tempes);
1688
1689 /*
1690 * Save and register the results of the image reconstruction.
1691 */
1692
1693 /* Reconstructed image */
1694
1695 giraffe_image_add_info(fov->fov.spectra, &info, set);
1696
1697 rimg_frame = giraffe_frame_create_image(fov->fov.spectra,
1698 GIFRAME_STANDARD_RCSPECTRA,
1699 CPL_FRAME_LEVEL_FINAL,
1700 TRUE, TRUE);
1701
1702 if (rimg_frame == NULL) {
1703 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1704
1705 giraffe_fov_delete(fov);
1706 giraffe_rebinning_destroy(rebinning);
1707
1708 giraffe_table_delete(wcalcoeff);
1709
1710 giraffe_table_delete(slitgeometry);
1711 giraffe_table_delete(grating);
1712 giraffe_table_delete(fibers);
1713
1714 return 1;
1715 }
1716
1717 cpl_frameset_insert(set, rimg_frame);
1718
1719
1720 /* Reconstructed image errors */
1721
1722 giraffe_image_add_info(fov->fov.errors, &info, set);
1723
1724 rimg_frame = giraffe_frame_create_image(fov->fov.errors,
1725 GIFRAME_STANDARD_RCERRORS,
1726 CPL_FRAME_LEVEL_FINAL,
1727 TRUE, TRUE);
1728
1729 if (rimg_frame == NULL) {
1730 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1731
1732 giraffe_fov_delete(fov);
1733 giraffe_rebinning_destroy(rebinning);
1734
1735 giraffe_table_delete(wcalcoeff);
1736
1737 giraffe_table_delete(slitgeometry);
1738 giraffe_table_delete(grating);
1739 giraffe_table_delete(fibers);
1740
1741 return 1;
1742 }
1743
1744 cpl_frameset_insert(set, rimg_frame);
1745
1746
1747 /* Save data cubes according to format selection */
1748
1749 if (cube_format == GIFOV_FORMAT_SINGLE) {
1750
1751 /* Spectrum cube */
1752
1753 if (fov->cubes.spectra != NULL) {
1754
1755 cxint component = 0;
1756
1757 GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
1758
1759
1760 properties = giraffe_image_get_properties(rebinning->spectra);
1761 properties = cpl_propertylist_duplicate(properties);
1762
1763 giraffe_add_frameset_info(properties, set, info.sequence);
1764
1765 rimg_frame = giraffe_frame_create(GIFRAME_STANDARD_CUBE_SPECTRA,
1766 CPL_FRAME_LEVEL_FINAL,
1767 properties,
1768 fov,
1769 &component,
1770 creator);
1771
1772 cpl_propertylist_delete(properties);
1773 properties = NULL;
1774
1775 if (rimg_frame == NULL) {
1776 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1777
1778 giraffe_fov_delete(fov);
1779 fov = NULL;
1780
1781 giraffe_rebinning_destroy(rebinning);
1782 rebinning = NULL;
1783
1784 giraffe_table_delete(wcalcoeff);
1785 wcalcoeff = NULL;
1786
1787 giraffe_table_delete(slitgeometry);
1788 slitgeometry = NULL;
1789
1790 giraffe_table_delete(grating);
1791 grating = NULL;
1792
1793 giraffe_table_delete(fibers);
1794 fibers = NULL;
1795
1796 return 1;
1797 }
1798
1799 status = giraffe_fiberlist_attach(rimg_frame, fibers);
1800
1801 if (status != 0) {
1802 cpl_msg_error(_id, "Cannot attach fiber setup to local "
1803 "file '%s'! Aborting ...",
1804 cpl_frame_get_filename(rimg_frame));
1805
1806 cpl_frame_delete(rimg_frame);
1807
1808 giraffe_fov_delete(fov);
1809 fov = NULL;
1810
1811 giraffe_rebinning_destroy(rebinning);
1812 rebinning = NULL;
1813
1814 giraffe_table_delete(wcalcoeff);
1815 wcalcoeff = NULL;
1816
1817 giraffe_table_delete(slitgeometry);
1818 slitgeometry = NULL;
1819
1820 giraffe_table_delete(grating);
1821 grating = NULL;
1822
1823 giraffe_table_delete(fibers);
1824 fibers = NULL;
1825
1826 return 1;
1827 }
1828
1829 cpl_frameset_insert(set, rimg_frame);
1830
1831 }
1832
1833 /* Error cube */
1834
1835 if (fov->cubes.errors != NULL) {
1836
1837 cxint component = 1;
1838
1839 GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
1840
1841
1842 properties = giraffe_image_get_properties(rebinning->errors);
1843 properties = cpl_propertylist_duplicate(properties);
1844
1845 giraffe_add_frameset_info(properties, set, info.sequence);
1846
1847 rimg_frame = giraffe_frame_create(GIFRAME_STANDARD_CUBE_ERRORS,
1848 CPL_FRAME_LEVEL_FINAL,
1849 properties,
1850 fov,
1851 &component,
1852 creator);
1853
1854 cpl_propertylist_delete(properties);
1855 properties = NULL;
1856
1857 if (rimg_frame == NULL) {
1858 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1859
1860 giraffe_fov_delete(fov);
1861 fov = NULL;
1862
1863 giraffe_rebinning_destroy(rebinning);
1864 rebinning = NULL;
1865
1866 giraffe_table_delete(wcalcoeff);
1867 wcalcoeff = NULL;
1868
1869 giraffe_table_delete(slitgeometry);
1870 slitgeometry = NULL;
1871
1872 giraffe_table_delete(grating);
1873 grating = NULL;
1874
1875 giraffe_table_delete(fibers);
1876 fibers = NULL;
1877
1878 return 1;
1879 }
1880
1881 status = giraffe_fiberlist_attach(rimg_frame, fibers);
1882
1883 if (status != 0) {
1884 cpl_msg_error(_id, "Cannot attach fiber setup to local "
1885 "file '%s'! Aborting ...",
1886 cpl_frame_get_filename(rimg_frame));
1887
1888 cpl_frame_delete(rimg_frame);
1889
1890 giraffe_fov_delete(fov);
1891 fov = NULL;
1892
1893 giraffe_rebinning_destroy(rebinning);
1894 rebinning = NULL;
1895
1896 giraffe_table_delete(wcalcoeff);
1897 wcalcoeff = NULL;
1898
1899 giraffe_table_delete(slitgeometry);
1900 slitgeometry = NULL;
1901
1902 giraffe_table_delete(grating);
1903 grating = NULL;
1904
1905 giraffe_table_delete(fibers);
1906 fibers = NULL;
1907
1908 return 1;
1909 }
1910
1911 cpl_frameset_insert(set, rimg_frame);
1912 }
1913
1914 }
1915 else {
1916
1917 /* Data Cube (ESO 3D format) */
1918
1919 GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes_eso3d;
1920
1921 properties = giraffe_image_get_properties(rebinning->spectra);
1922 properties = cpl_propertylist_duplicate(properties);
1923
1924 giraffe_add_frameset_info(properties, set, info.sequence);
1925
1926 rimg_frame = giraffe_frame_create(GIFRAME_STANDARD_CUBE,
1927 CPL_FRAME_LEVEL_FINAL,
1928 properties,
1929 fov,
1930 NULL,
1931 creator);
1932
1933 cpl_propertylist_delete(properties);
1934 properties = NULL;
1935
1936 if (rimg_frame == NULL) {
1937 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1938
1939 giraffe_fov_delete(fov);
1940 fov = NULL;
1941
1942 giraffe_rebinning_destroy(rebinning);
1943 rebinning = NULL;
1944
1945 giraffe_table_delete(wcalcoeff);
1946 wcalcoeff = NULL;
1947
1948 giraffe_table_delete(slitgeometry);
1949 slitgeometry = NULL;
1950
1951 giraffe_table_delete(grating);
1952 grating = NULL;
1953
1954 giraffe_table_delete(fibers);
1955 fibers = NULL;
1956
1957 return 1;
1958 }
1959
1960 status = giraffe_fiberlist_attach(rimg_frame, fibers);
1961
1962 if (status != 0) {
1963 cpl_msg_error(_id, "Cannot attach fiber setup to local "
1964 "file '%s'! Aborting ...",
1965 cpl_frame_get_filename(rimg_frame));
1966
1967 cpl_frame_delete(rimg_frame);
1968
1969 giraffe_fov_delete(fov);
1970 fov = NULL;
1971
1972 giraffe_rebinning_destroy(rebinning);
1973 rebinning = NULL;
1974
1975 giraffe_table_delete(wcalcoeff);
1976 wcalcoeff = NULL;
1977
1978 giraffe_table_delete(slitgeometry);
1979 slitgeometry = NULL;
1980
1981 giraffe_table_delete(grating);
1982 grating = NULL;
1983
1984 giraffe_table_delete(fibers);
1985 fibers = NULL;
1986
1987 return 1;
1988 }
1989
1990 cpl_frameset_insert(set, rimg_frame);
1991
1992 }
1993
1994 giraffe_fov_delete(fov);
1995 fov = NULL;
1996
1997 }
1998
1999
2000 /*
2001 * Response computation
2002 */
2003
2004 cpl_msg_info(_id, "Computing instrument response function...");
2005
2006 filename = cpl_frame_get_filename(flxstd_frame);
2007
2008 flxstd = giraffe_table_new();
2009 status = giraffe_table_load(flxstd, filename, 1, NULL);
2010
2011 cpl_msg_info(_id, "Flux table loaded ...");
2012
2013 if (status != 0) {
2014 cpl_msg_error(_id, "Cannot load flux standards catalog from "
2015 "'%s'. Aborting ...", filename);
2016
2017 giraffe_rebinning_destroy(rebinning);
2018 rebinning = NULL;
2019
2020 giraffe_table_delete(wcalcoeff);
2021 wcalcoeff = NULL;
2022
2023 giraffe_table_delete(slitgeometry);
2024 slitgeometry = NULL;
2025
2026 giraffe_table_delete(grating);
2027 grating = NULL;
2028
2029 giraffe_table_delete(fibers);
2030 fibers = NULL;
2031
2032 return 1;
2033 }
2034
2035
2036 fxcal_config = giraffe_fxcalibration_config_create(config);
2037
2038 /*
2039 * Search the flux standard catalog for the observed flux standard
2040 * object.
2041 */
2042
2043 refflx = giraffe_select_flux_standard(flxstd, rebinning->spectra, fxcal_config->max_dist);
2044
2045
2046 if (refflx == NULL) {
2047 cpl_msg_error(_id, "No matching flux standard found in the "
2048 "catalog '%s'! Aborting ...", filename);
2049
2050 giraffe_table_delete(flxstd);
2051 flxstd = NULL;
2052
2053 giraffe_rebinning_destroy(rebinning);
2054 rebinning = NULL;
2055
2056 giraffe_table_delete(wcalcoeff);
2057 wcalcoeff = NULL;
2058
2059 giraffe_table_delete(slitgeometry);
2060 slitgeometry = NULL;
2061
2062 giraffe_table_delete(grating);
2063 grating = NULL;
2064
2065 giraffe_table_delete(fibers);
2066 fibers = NULL;
2067
2068 return 1;
2069 }
2070
2071 giraffe_table_delete(flxstd);
2072 flxstd = NULL;
2073
2074
2075 filename = cpl_frame_get_filename(atmext_frame);
2076
2077 atmext = giraffe_table_new();
2078 status = giraffe_table_load(atmext, filename, 1, NULL);
2079
2080 if (status != 0) {
2081 cpl_msg_error(_id, "Cannot load atmospheric extinction data from "
2082 "'%s'. Aborting ...", filename);
2083
2084 giraffe_table_delete(refflx);
2085 refflx = NULL;
2086
2087 giraffe_rebinning_destroy(rebinning);
2088 rebinning = NULL;
2089
2090 giraffe_table_delete(wcalcoeff);
2091 wcalcoeff = NULL;
2092
2093 giraffe_table_delete(slitgeometry);
2094 slitgeometry = NULL;
2095
2096 giraffe_table_delete(grating);
2097 grating = NULL;
2098
2099 giraffe_table_delete(fibers);
2100 fibers = NULL;
2101
2102 return 1;
2103 }
2104
2105 response = giraffe_response_new();
2106
2107 status = giraffe_calibrate_flux(response, rebinning, fibers, NULL,
2108 refflx, atmext, fxcal_config);
2109
2110 if (status != 0) {
2111
2112 cpl_msg_error(_id, "Instrument response computation failed!");
2113
2114 giraffe_response_delete(response);
2115 response = NULL;
2116
2118 fxcal_config = NULL;
2119
2120 giraffe_table_delete(atmext);
2121 atmext = NULL;
2122
2123 giraffe_table_delete(refflx);
2124 refflx = NULL;
2125
2126 giraffe_rebinning_destroy(rebinning);
2127 rebinning = NULL;
2128
2129 giraffe_table_delete(wcalcoeff);
2130 wcalcoeff = NULL;
2131
2132 giraffe_table_delete(slitgeometry);
2133 slitgeometry = NULL;
2134
2135 giraffe_table_delete(grating);
2136 grating = NULL;
2137
2138 giraffe_table_delete(fibers);
2139 fibers = NULL;
2140
2141 return 1;
2142
2143 }
2144
2146 fxcal_config = NULL;
2147
2148 giraffe_table_delete(refflx);
2149 refflx = NULL;
2150
2151 giraffe_table_delete(atmext);
2152 atmext = NULL;
2153
2154
2155 /*
2156 * Save and register the instrument response product
2157 */
2158
2159 giraffe_image_add_info(response->response, &info, set);
2160
2161 rsp_frame = giraffe_frame_create_image(response->response,
2162 GIFRAME_INSTRUMENT_RESPONSE,
2163 CPL_FRAME_LEVEL_FINAL,
2164 TRUE, TRUE);
2165
2166 if (rsp_frame == NULL) {
2167
2168 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
2169
2170 giraffe_response_delete(response);
2171 response = NULL;
2172
2173 giraffe_rebinning_destroy(rebinning);
2174 rebinning = NULL;
2175
2176 giraffe_table_delete(wcalcoeff);
2177 wcalcoeff = NULL;
2178
2179 giraffe_table_delete(slitgeometry);
2180 slitgeometry = NULL;
2181
2182 giraffe_table_delete(grating);
2183 grating = NULL;
2184
2185 giraffe_table_delete(fibers);
2186 fibers = NULL;
2187
2188 return 1;
2189
2190 }
2191
2192 cpl_frameset_insert(set, rsp_frame);
2193
2194
2195 /*
2196 * Save and register the efficiency curve product
2197 */
2198
2199 giraffe_table_add_info(response->efficiency, &info, set);
2200
2201 rsp_frame = giraffe_frame_create_table(response->efficiency,
2202 GIFRAME_EFFICIENCY_CURVE,
2203 CPL_FRAME_LEVEL_FINAL,
2204 TRUE, TRUE);
2205
2206 if (rsp_frame == NULL) {
2207
2208 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
2209
2210 giraffe_response_delete(response);
2211 response = NULL;
2212
2213 giraffe_rebinning_destroy(rebinning);
2214 rebinning = NULL;
2215
2216 giraffe_table_delete(wcalcoeff);
2217 wcalcoeff = NULL;
2218
2219 giraffe_table_delete(slitgeometry);
2220 slitgeometry = NULL;
2221
2222 giraffe_table_delete(grating);
2223 grating = NULL;
2224
2225 giraffe_table_delete(fibers);
2226 fibers = NULL;
2227
2228 return 1;
2229
2230 }
2231
2232 cpl_frameset_insert(set, rsp_frame);
2233
2234 giraffe_response_delete(response);
2235 response = NULL;
2236
2237
2238 /*
2239 * Cleanup
2240 */
2241
2242 giraffe_table_delete(wcalcoeff);
2243
2244 giraffe_table_delete(slitgeometry);
2245 giraffe_table_delete(grating);
2246 giraffe_table_delete(fibers);
2247
2248 giraffe_rebinning_destroy(rebinning);
2249
2250 return 0;
2251
2252}
2253
2254
2255static cxint
2256giqcstandard(cpl_frameset* set)
2257{
2258
2259 const cxchar* const fctid = "giqcstandard";
2260
2261
2262 const cxdouble saturation = 60000.;
2263 const cxdouble wlscale = 0.1;
2264
2265 cxint i = 0;
2266 cxint status = 0;
2267 cxint nbin = 0;
2268 cxint npixel = 0;
2269 cxint nsaturated = 0;
2270
2271 const cxdouble* pixels = NULL;
2272
2273 cxdouble wlmin = 0.;
2274 cxdouble wlmax = 0.;
2275 cxdouble efficiency = 0.;
2276
2277 cpl_propertylist* properties = NULL;
2278 cpl_propertylist* _properties = NULL;
2279 cpl_propertylist* qclog = NULL;
2280
2281 cpl_frame* rframe = NULL;
2282 cpl_frame* pframe = NULL;
2283
2284 cpl_image* _rimage = NULL;
2285
2286 cpl_table* _ptable = NULL;
2287
2288 GiImage* rimage = NULL;
2289
2290 GiTable* ptable = NULL;
2291
2292 GiPaf* qc = NULL;
2293
2294
2295 cpl_msg_info(fctid, "Computing QC1 parameters ...");
2296
2297 qc = giraffe_qclog_open(0);
2298
2299 if (qc == NULL) {
2300 cpl_msg_error(fctid, "Cannot create QC1 log!");
2301 return 1;
2302 }
2303
2304 qclog = giraffe_paf_get_properties(qc);
2305 cx_assert(qclog != NULL);
2306
2307
2308 /*
2309 * Process efficiency table
2310 */
2311
2312 pframe = giraffe_get_frame(set, GIFRAME_EFFICIENCY_CURVE,
2313 CPL_FRAME_GROUP_PRODUCT);
2314
2315 if (pframe == NULL) {
2316 cpl_msg_error(fctid, "Missing product frame (%s)",
2317 GIFRAME_EFFICIENCY_CURVE);
2318
2319 giraffe_paf_delete(qc);
2320 qc = NULL;
2321
2322 return 1;
2323 }
2324
2325 cpl_msg_info(fctid, "Processing product frame '%s' (%s)",
2326 cpl_frame_get_filename(pframe), cpl_frame_get_tag(pframe));
2327
2328 ptable = giraffe_table_new();
2329 status = giraffe_table_load(ptable, cpl_frame_get_filename(pframe), 1,
2330 "EFFICIENCY_CURVE");
2331
2332 if (status != 0) {
2333 cpl_msg_error(fctid, "Could not load efficiency table '%s'! "
2334 "Aborting ...", cpl_frame_get_filename(pframe));
2335
2336 giraffe_table_delete(ptable);
2337 ptable = NULL;
2338
2339 giraffe_paf_delete(qc);
2340 qc = NULL;
2341
2342 return 1;
2343 }
2344
2345
2346 /*
2347 * Load first raw image as reference
2348 */
2349
2350 rframe = cpl_frameset_find(set, GIFRAME_STANDARD);
2351
2352 if (rframe == NULL) {
2353 cpl_msg_error(fctid, "Missing raw frame (%s)", GIFRAME_STANDARD);
2354
2355 giraffe_table_delete(ptable);
2356 ptable = NULL;
2357
2358 giraffe_paf_delete(qc);
2359 qc = NULL;
2360
2361 return 1;
2362 }
2363
2364 rimage = giraffe_image_new(CPL_TYPE_DOUBLE);
2365 status = giraffe_image_load(rimage, cpl_frame_get_filename(rframe), 0);
2366
2367 if (status != 0) {
2368 cpl_msg_error(fctid, "Could not load standard star observation '%s'!",
2369 cpl_frame_get_filename(rframe));
2370
2371 giraffe_image_delete(rimage);
2372 rimage = NULL;
2373
2374 giraffe_table_delete(ptable);
2375 ptable = NULL;
2376
2377 giraffe_paf_delete(qc);
2378 qc = NULL;
2379
2380 return 1;
2381
2382 }
2383
2384 _rimage = giraffe_image_get(rimage);
2385 cx_assert(_rimage != NULL);
2386
2387 properties = giraffe_image_get_properties(rimage);
2388 cx_assert(properties != NULL);
2389
2390 giraffe_propertylist_copy(qclog, "ARCFILE", properties, GIALIAS_ARCFILE);
2391 giraffe_propertylist_copy(qclog, "TPL.ID", properties, GIALIAS_TPLID);
2392
2393 cpl_propertylist_update_string(qclog, "PRO.CATG",
2394 cpl_frame_get_tag(pframe));
2395 cpl_propertylist_set_comment(qclog, "PRO.CATG",
2396 "Pipeline product category");
2397
2398
2399 /*
2400 * Count the number of saturated pixels in the raw frame
2401 */
2402
2403 pixels = cpl_image_get_data(_rimage);
2404 npixel = cpl_image_get_size_x(_rimage) * cpl_image_get_size_y(_rimage);
2405
2406 _rimage = NULL;
2407
2408 for (i = 0; i < npixel; i++) {
2409 if (pixels[i] > saturation) {
2410 ++nsaturated;
2411 }
2412 }
2413
2414 pixels = NULL;
2415
2416 giraffe_image_delete(rimage);
2417 rimage = NULL;
2418
2419
2420 /*
2421 * Compute mean efficiency from a wavelength range around the
2422 * central wavelength.
2423 */
2424
2425 _ptable = giraffe_table_get(ptable);
2426 cx_assert(_ptable != NULL);
2427
2428 properties = giraffe_table_get_properties(ptable);
2429 cx_assert(properties != NULL);
2430
2431 if (cpl_propertylist_has(properties, GIALIAS_GRATWLEN) == FALSE) {
2432
2433 giraffe_table_delete(ptable);
2434 ptable = NULL;
2435
2436 giraffe_paf_delete(qc);
2437 qc = NULL;
2438
2439 cpl_msg_error(fctid, "Missing property '%s'", GIALIAS_GRATWLEN);
2440
2441 return 1;
2442
2443 }
2444 else {
2445
2446 cxdouble wlband = 0.;
2447 cxdouble wl0 = cpl_propertylist_get_double(properties,
2448 GIALIAS_GRATWLEN);
2449
2450
2451 wlmin = cpl_propertylist_get_double(properties, GIALIAS_BINWLMIN);
2452 wlmax = cpl_propertylist_get_double(properties, GIALIAS_BINWLMAX);
2453
2454 cx_assert((wlmin < wl0) && (wl0 < wlmax));
2455
2456 wlband = wlscale * fabs(wlmax - wlmin);
2457
2458 wlmin = CX_MAX(wlmin, (wl0 - wlband));
2459 wlmax = CX_MIN(wlmax, (wl0 + wlband));
2460
2461 cpl_msg_info(fctid, "Computing spectrograph efficiency from "
2462 "wavelength range ]%.1f, %.1f[", wlmin, wlmax);
2463
2464 }
2465
2466 nbin = 0;
2467
2468 for (i = 0; i < cpl_table_get_nrow(_ptable); ++i) {
2469
2470 cxdouble wavelength = cpl_table_get_double(_ptable, "WLEN", i, NULL);
2471
2472 if ((wavelength > wlmin) && (wavelength < wlmax)) {
2473
2474 efficiency += cpl_table_get_double(_ptable, "EFFICIENCY",
2475 i, NULL);
2476 ++nbin;
2477
2478 }
2479
2480 }
2481
2482 efficiency /= (cxdouble)nbin;
2483
2484
2485 cpl_propertylist_update_int(properties, GIALIAS_QCNSAT, nsaturated);
2486 cpl_propertylist_set_comment(properties, GIALIAS_QCNSAT, "Number of "
2487 "saturated pixels in the first raw frame");
2488
2489 giraffe_propertylist_copy(qclog, "QC.OUT1.NSAT.RAW", properties,
2490 GIALIAS_QCNSAT);
2491
2492
2493 cpl_propertylist_update_double(properties, GIALIAS_QCEFFICIENCY,
2494 efficiency);
2495 cpl_propertylist_set_comment(properties, GIALIAS_QCEFFICIENCY,
2496 "Efficiency of the spectrograph.");
2497
2498 giraffe_propertylist_copy(qclog, "QC.EFFICIENCY.MEAN", properties,
2499 GIALIAS_QCEFFICIENCY);
2500
2501
2502 if (cpl_propertylist_has(properties, GIALIAS_SKY_LEVEL) == TRUE) {
2503
2504 cxdouble mean_sky = cpl_propertylist_get_double(properties,
2505 GIALIAS_SKY_LEVEL);
2506
2507 cpl_propertylist_update_double(properties, GIALIAS_QCSKYLEVEL,
2508 mean_sky);
2509 cpl_propertylist_set_comment(properties, GIALIAS_QCSKYLEVEL,
2510 "Mean sky level [ADU]");
2511
2512 giraffe_propertylist_copy(qclog, "QC.SKY.MEAN", properties,
2513 GIALIAS_QCSKYLEVEL);
2514
2515 }
2516
2517
2518 /*
2519 * Write QC1 log and save updated product.
2520 */
2521
2522 _properties = cpl_propertylist_load_regexp(cpl_frame_get_filename(pframe),
2523 0, "^COMMENT$", TRUE);
2524
2525 cpl_propertylist_erase_regexp(_properties, "ESO QC.*", 0);
2526
2527
2528 giraffe_propertylist_copy(_properties, GIALIAS_QCEFFICM, properties,
2529 GIALIAS_QCEFFICIENCY);
2530
2531 giraffe_propertylist_copy(_properties, GIALIAS_QCNUMSAT, properties,
2532 GIALIAS_QCNSAT);
2533
2534 giraffe_qc_airm_see_avg(_properties);
2535
2536 cpl_image_save(NULL, cpl_frame_get_filename(pframe), CPL_BPP_8_UNSIGNED,
2537 _properties, CPL_IO_CREATE);
2538
2539 cpl_propertylist_delete(_properties);
2540 _properties = NULL;
2541
2542 giraffe_table_attach(ptable, cpl_frame_get_filename(pframe), 1, NULL);
2543
2544 giraffe_table_delete(ptable);
2545 ptable = NULL;
2546
2547 giraffe_qclog_close(qc);
2548 qc = NULL;
2549
2550 return 0;
2551
2552}
2553
2554
2555/*
2556 * Build table of contents, i.e. the list of available plugins, for
2557 * this module. This function is exported.
2558 */
2559
2560int
2561cpl_plugin_get_info(cpl_pluginlist* list)
2562{
2563
2564 cpl_recipe* recipe = cx_calloc(1, sizeof *recipe);
2565 cpl_plugin* plugin = &recipe->interface;
2566
2567
2568 cpl_plugin_init(plugin,
2569 CPL_PLUGIN_API,
2570 GIRAFFE_BINARY_VERSION,
2571 CPL_PLUGIN_TYPE_RECIPE,
2572 "gistandard",
2573 "Process a spectro-photometric standard star "
2574 "observation and compute the instrument response curve.",
2575 "For detailed information please refer to the "
2576 "GIRAFFE pipeline user manual.\nIt is available at "
2577 "http://www.eso.org/pipelines.",
2578 "Giraffe Pipeline",
2579 PACKAGE_BUGREPORT,
2581 gistandard_create,
2582 gistandard_exec,
2583 gistandard_destroy);
2584
2585 cpl_pluginlist_append(list, plugin);
2586
2587 return 0;
2588
2589}
GiBiasConfig * giraffe_bias_config_create(cpl_parameterlist *list)
Creates a setup structure for a bias removal task.
Definition: gibias.c:3439
void giraffe_bias_config_add(cpl_parameterlist *list)
Adds parameters for the bias removal.
Definition: gibias.c:3598
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.
Definition: gibias.c:3107
void giraffe_bias_config_destroy(GiBiasConfig *config)
Destroys a bias removal setup structure.
Definition: gibias.c:3570
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.
Definition: gidark.c:480
void giraffe_extract_config_add(cpl_parameterlist *list)
Adds parameters for the spectrum extraction.
Definition: giextract.c:3509
cxint giraffe_extract_spectra(GiExtraction *result, GiImage *image, GiTable *fibers, GiLocalization *sloc, GiImage *bpixel, GiImage *slight, GiExtractConfig *config)
Extracts the spectra from a preprocessed frame.
Definition: giextract.c:2480
GiExtractConfig * giraffe_extract_config_create(cpl_parameterlist *list)
Creates a setup structure for the spectrum extraction.
Definition: giextract.c:3405
void giraffe_extract_config_destroy(GiExtractConfig *config)
Destroys a spectrum extraction setup structure.
Definition: giextract.c:3479
GiTable * giraffe_fibers_setup(const cpl_frame *frame, const cpl_frame *reference)
Setup a fiber list.
Definition: gifibers.c:218
cxint giraffe_fiberlist_compare(const GiTable *fibers, const GiTable *reference)
Compare two fiber lists.
Definition: gifiberutils.c:952
cxint giraffe_fiberlist_attach(cpl_frame *frame, GiTable *fibers)
Attach a fiber table to a frame.
Definition: gifiberutils.c:884
GiFlatConfig * giraffe_flat_config_create(cpl_parameterlist *list)
Creates a setup structure for the flat field correction.
Definition: giflat.c:302
void giraffe_flat_config_destroy(GiFlatConfig *config)
Destroys a flat field setup structure.
Definition: giflat.c:353
void giraffe_flat_config_add(cpl_parameterlist *list)
Adds parameters for the flat field correction.
Definition: giflat.c:376
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.
Definition: giflat.c:238
GiFieldOfViewConfig * giraffe_fov_config_create(cpl_parameterlist *list)
Creates a setup structure for the field of view reconstruction.
Definition: gifov.c:2013
void giraffe_fov_config_destroy(GiFieldOfViewConfig *config)
Destroys a field of view setup structure.
Definition: gifov.c:2068
GiFieldOfView * giraffe_fov_new(void)
Create an empty container for the results of the field of view reconstruction.
Definition: gifov.c:1394
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.
Definition: gifov.c:1674
void giraffe_fov_delete(GiFieldOfView *self)
Deallocate a field of view object and its contents.
Definition: gifov.c:1495
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.
Definition: gifov.c:429
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.
Definition: gifov.c:1531
void giraffe_fov_config_add(cpl_parameterlist *list)
Adds parameters for the image and data cube construction.
Definition: gifov.c:2090
cpl_frame * giraffe_get_frame(const cpl_frameset *set, const cxchar *tag, cpl_frame_group group)
Get a frame from a frame set.
Definition: giframe.c:739
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.
Definition: giframe.c:239
cpl_frame * giraffe_frame_create_image(GiImage *image, const cxchar *tag, cpl_frame_level level, cxbool save, cxbool update)
Create an image product frame.
Definition: giframe.c:395
cpl_frame * giraffe_get_slitgeometry(const cpl_frameset *set)
Get the slit geometry frame from a frame set.
Definition: giframe.c:786
cpl_frame * giraffe_frame_create_table(GiTable *table, const cxchar *tag, cpl_frame_level level, cxbool save, cxbool update)
Create a table product frame.
Definition: giframe.c:543
void giraffe_fxcalibration_config_add(cpl_parameterlist *parameters)
Add flux calibration parameters to a parameter list.
cxint giraffe_calibrate_flux(GiResponse *result, const GiRebinning *spectra, const GiTable *fibers, const GiImage *flat, const GiTable *flux, const GiTable *extinction, const GiFxCalibrationConfig *config)
Compute the response and efficiency curves.
void giraffe_fxcalibration_config_destroy(GiFxCalibrationConfig *self)
Destroy a flux calibration setup structure.
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
Definition: giimage.c:218
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
Definition: giimage.c:282
void giraffe_image_delete(GiImage *self)
Destroys an image.
Definition: giimage.c:181
cxint giraffe_image_add_info(GiImage *image, const GiRecipeInfo *info, const cpl_frameset *set)
Add additional frame information to an image.
Definition: giimage.c:773
GiImage * giraffe_image_new(cpl_type type)
Creates an empty image container.
Definition: giimage.c:65
cxint giraffe_image_load(GiImage *self, const cxchar *filename, cxint position)
Gets image data and properties from a file.
Definition: giimage.c:536
void giraffe_rebin_config_destroy(GiRebinConfig *config)
Destroys a spectrum extraction setup structure.
Definition: girebinning.c:4926
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.
Definition: girebinning.c:4052
GiRebinConfig * giraffe_rebin_config_create(cpl_parameterlist *list)
Creates a setup structure for the rebinning.
Definition: girebinning.c:4826
GiRebinning * giraffe_rebinning_new(void)
Create an empty rebinning results container.
Definition: girebinning.c:4694
void giraffe_rebinning_destroy(GiRebinning *rebinning)
Destroys a rebinning results container and its contents.
Definition: girebinning.c:4788
void giraffe_rebin_config_add(cpl_parameterlist *list)
Adds parameters for the rebinning.
Definition: girebinning.c:4950
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.
Definition: gitable.c:85
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.
Definition: gitable.c:562
cxint giraffe_table_attach(GiTable *self, const cxchar *filename, cxint position, const cxchar *id)
Attach a Giraffe table to a file.
Definition: gitable.c:749
void giraffe_table_delete(GiTable *self)
Destroys a Giraffe table.
Definition: gitable.c:154
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
Definition: gitable.c:433
cpl_propertylist * giraffe_table_get_properties(const GiTable *self)
Gets the table properties.
Definition: gitable.c:489
cxint giraffe_table_add_info(GiTable *table, const GiRecipeInfo *info, const cpl_frameset *set)
Add additional frame information to a table.
Definition: gitable.c:836
cxint giraffe_transmission_attach(GiTable *fibers, const cxchar *filename)
Load relative fiber transmission data from a file and add it to a fiber table.
cxint giraffe_add_frameset_info(cpl_propertylist *plist, const cpl_frameset *set, cxint sequence)
Add frameset specific information to a property list.
Definition: giutils.c:786
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.
Definition: giutils.c:1108
const cxchar * giraffe_get_license(void)
Get the pipeline copyright and license.
Definition: giutils.c:422
GiInstrumentMode giraffe_get_mode(cpl_propertylist *properties)
Determines the instrument mode from a property list.
Definition: giutils.c:444

This file is part of the GIRAFFE Pipeline Reference Manual 2.19.4.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Feb 6 2026 11:30:08 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2004