GIRAFFE Pipeline Reference Manual

giscience.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include <cxslist.h>
33 
34 #include <cpl_recipe.h>
35 #include <cpl_plugininfo.h>
36 #include <cpl_parameterlist.h>
37 #include <cpl_frameset.h>
38 #include <cpl_msg.h>
39 
40 #include "gialias.h"
41 #include "giframe.h"
42 #include "gifibers.h"
43 #include "gifiberutils.h"
44 #include "gislitgeometry.h"
45 #include "gipsfdata.h"
46 #include "gibias.h"
47 #include "gidark.h"
48 #include "giextract.h"
49 #include "giflat.h"
50 #include "gitransmission.h"
51 #include "girebinning.h"
52 #include "gisgcalibration.h"
53 #include "giastrometry.h"
54 #include "gifov.h"
55 #include "gimessages.h"
56 #include "gierror.h"
57 #include "giutils.h"
58 
59 
60 static cxint giscience(cpl_parameterlist*, cpl_frameset*);
61 
62 
63 /*
64  * Create the recipe instance, i.e. setup the parameter list for this
65  * recipe and make it availble to the application using the interface.
66  */
67 
68 static cxint
69 giscience_create(cpl_plugin* plugin)
70 {
71 
72  cpl_recipe* recipe = (cpl_recipe*)plugin;
73 
74  cpl_parameter* p = NULL;
75 
76 
77  giraffe_error_init();
78 
79 
80  /*
81  * We have to provide the option we accept to the application. We
82  * need to setup our parameter list and hook it into the recipe
83  * interface.
84  */
85 
86  recipe->parameters = cpl_parameterlist_new();
87  cx_assert(recipe->parameters != NULL);
88 
89 
90  /*
91  * Fill the parameter list.
92  */
93 
94  /* Bias removal */
95 
96  giraffe_bias_config_add(recipe->parameters);
97 
98  /* Dark subtraction */
99 
100  /* TBD */
101 
102  /* Spectrum extraction */
103 
104  giraffe_extract_config_add(recipe->parameters);
105 
106  /* Flat fielding and relative fiber transmission correction */
107 
108  giraffe_flat_config_add(recipe->parameters);
109 
110  /* Spectrum rebinning */
111 
112  giraffe_rebin_config_add(recipe->parameters);
113 
114  /* Simultaneous wavelength calibration correction */
115 
116  p = cpl_parameter_new_value("giraffe.siwc.apply",
117  CPL_TYPE_BOOL,
118  "Enable simultaneous wavelength calibration "
119  "correction.",
120  "giraffe.siwc",
121  TRUE);
122 
123  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "siwc-apply");
124  cpl_parameterlist_append(recipe->parameters, p);
125 
126  giraffe_sgcalibration_config_add(recipe->parameters);
127 
128  /* Image reconstruction (IFU and Argus only) */
129 
130  giraffe_fov_config_add(recipe->parameters);
131 
132  return 0;
133 
134 }
135 
136 
137 /*
138  * Execute the plugin instance given by the interface.
139  */
140 
141 static cxint
142 giscience_exec(cpl_plugin* plugin)
143 {
144 
145  cpl_recipe* recipe = (cpl_recipe*)plugin;
146 
147 
148  cx_assert(recipe->parameters != NULL);
149  cx_assert(recipe->frames != NULL);
150 
151  return giscience(recipe->parameters, recipe->frames);
152 
153 }
154 
155 
156 static cxint
157 giscience_destroy(cpl_plugin* plugin)
158 {
159 
160  cpl_recipe* recipe = (cpl_recipe*)plugin;
161 
162 
163  /*
164  * We just destroy what was created during the plugin initialization
165  * phase, i.e. the parameter list. The frame set is managed by the
166  * application which called us, so we must not touch it,
167  */
168 
169  cpl_parameterlist_delete(recipe->parameters);
170 
171  giraffe_error_clear();
172 
173  return 0;
174 
175 }
176 
177 
178 /*
179  * The actual recipe starts here.
180  */
181 
182 static cxint
183 giscience(cpl_parameterlist* config, cpl_frameset* set)
184 {
185 
186  const cxchar* const _id = "giscience";
187 
188 
189  const cxchar* filename = NULL;
190 
191  cxbool siwc = FALSE;
192  cxbool calsim = FALSE;
193 
194  cxint status = 0;
195 
196  cxlong i;
197  cxlong nscience = 0;
198 
199  cxdouble exptime = 0.;
200 
201  cx_slist* slist = NULL;
202 
203  cpl_propertylist* properties = NULL;
204 
205  cpl_matrix* biasareas = NULL;
206 
207  cpl_frame* science_frame = NULL;
208  cpl_frame* mbias_frame = NULL;
209  cpl_frame* mdark_frame = NULL;
210  cpl_frame* bpixel_frame = NULL;
211  cpl_frame* slight_frame = NULL;
212  cpl_frame* locy_frame = NULL;
213  cpl_frame* locw_frame = NULL;
214  cpl_frame* psfdata_frame = NULL;
215  cpl_frame* grating_frame = NULL;
216  cpl_frame* linemask_frame = NULL;
217  cpl_frame* slit_frame = NULL;
218  cpl_frame* wcal_frame = NULL;
219  cpl_frame* rscience_frame = NULL;
220  cpl_frame* sext_frame = NULL;
221  cpl_frame* rbin_frame = NULL;
222 
223  cpl_parameter* p = NULL;
224 
225  GiImage* mbias = NULL;
226  GiImage* mdark = NULL;
227  GiImage* bpixel = NULL;
228  GiImage* slight = NULL;
229  GiImage* sscience = NULL;
230  GiImage* rscience = NULL;
231 
232  GiTable* fibers = NULL;
233  GiTable* slitgeometry = NULL;
234  GiTable* grating = NULL;
235  GiTable* wcalcoeff = NULL;
236 
237  GiLocalization* localization = NULL;
238  GiExtraction* extraction = NULL;
239  GiRebinning* rebinning = NULL;
240 
241  GiBiasConfig* bias_config = NULL;
242  GiExtractConfig* extract_config = NULL;
243  GiFlatConfig* flat_config = NULL;
244  GiRebinConfig* rebin_config = NULL;
245 
246  GiInstrumentMode mode;
247 
248  GiRecipeInfo info = {(cxchar*)_id, 1, NULL};
249 
250  GiGroupInfo groups[] = {
251  {GIFRAME_SCIENCE, CPL_FRAME_GROUP_RAW},
252  {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
253  {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
254  {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
255  {GIFRAME_FIBER_FLAT_EXTSPECTRA, CPL_FRAME_GROUP_CALIB},
256  {GIFRAME_FIBER_FLAT_EXTERRORS, CPL_FRAME_GROUP_CALIB},
257  {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
258  {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
259  {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
260  {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
261  {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
262  {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
263  {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
264  {GIFRAME_LINE_MASK, CPL_FRAME_GROUP_CALIB},
265  {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
266  {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
267  {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
268  {NULL, CPL_FRAME_GROUP_NONE}
269  };
270 
271 
272 
273  if (!config) {
274  cpl_msg_error(_id, "Invalid parameter list! Aborting ...");
275  return 1;
276  }
277 
278  if (!set) {
279  cpl_msg_error(_id, "Invalid frame set! Aborting ...");
280  return 1;
281  }
282 
283  status = giraffe_frameset_set_groups(set, groups);
284 
285  if (status != 0) {
286  cpl_msg_error(_id, "Setting frame group information failed!");
287  return 1;
288  }
289 
290 
291  /*
292  * Verify the frame set contents
293  */
294 
295  nscience = cpl_frameset_count_tags(set, GIFRAME_SCIENCE);
296 
297  if (nscience < 1) {
298  cpl_msg_error(_id, "Too few (%ld) raw frames (%s) present in "
299  "frame set! Aborting ...", nscience, GIFRAME_SCIENCE);
300  return 1;
301  }
302 
303  locy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
304 
305  if (locy_frame == NULL) {
306 
307  locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
308 
309  if (locy_frame == NULL) {
310  cpl_msg_info(_id, "No master localization (centroid position) "
311  "present in frame set. Aborting ...");
312  return 1;
313  }
314 
315  }
316 
317  locw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
318 
319  if (locw_frame == NULL) {
320 
321  locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
322 
323  if (locw_frame == NULL) {
324  cpl_msg_info(_id, "No master localization (spectrum width) "
325  "present in frame set. Aborting ...");
326  return 1;
327  }
328 
329  }
330 
331  grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
332 
333  if (!grating_frame) {
334  cpl_msg_error(_id, "No grating data present in frame set. "
335  "Aborting ...");
336  return 1;
337  }
338 
339  slit_frame = giraffe_get_slitgeometry(set);
340 
341  if (!slit_frame) {
342  cpl_msg_error(_id, "No slit geometry present in frame set. "
343  "Aborting ...");
344  return 1;
345  }
346 
347  wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
348 
349  if (!wcal_frame) {
350  cpl_msg_error(_id, "No dispersion solution present in frame set. "
351  "Aborting ...");
352  return 1;
353  }
354 
355  linemask_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
356 
357  if (!linemask_frame) {
358  cpl_msg_warning(_id, "No reference line mask present in frame set.");
359  }
360 
361  bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
362 
363  if (!bpixel_frame) {
364  cpl_msg_info(_id, "No bad pixel map present in frame set.");
365  }
366 
367  mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
368 
369  if (!mbias_frame) {
370  cpl_msg_info(_id, "No master bias present in frame set.");
371  }
372 
373  mdark_frame = cpl_frameset_find(set, GIFRAME_DARK_MASTER);
374 
375  if (!mdark_frame) {
376  cpl_msg_info(_id, "No master dark present in frame set.");
377  }
378 
379  slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
380 
381  if (!slight_frame) {
382  cpl_msg_info(_id, "No scattered light model present in frame set.");
383  }
384 
385  psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
386 
387  if (!psfdata_frame) {
388  cpl_msg_info(_id, "No PSF profile parameters present in frame set.");
389  }
390 
391 
392  /*
393  * Load raw images
394  */
395 
396  slist = cx_slist_new();
397 
398  science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
399 
400  for (i = 0; i < nscience; i++) {
401 
402  filename = cpl_frame_get_filename(science_frame);
403 
404  GiImage* raw = giraffe_image_new(CPL_TYPE_DOUBLE);
405 
406 
407  status = giraffe_image_load(raw, filename, 0);
408 
409  if (status) {
410  cpl_msg_error(_id, "Cannot load raw science frame from '%s'. "
411  "Aborting ...", filename);
412 
413  cx_slist_destroy(slist, (cx_free_func) giraffe_image_delete);
414 
415  return 1;
416  }
417 
418  cx_slist_push_back(slist, raw);
419 
420  science_frame = cpl_frameset_find(set, NULL);
421 
422  }
423 
424  nscience = (cxint)cx_slist_size(slist);
425  sscience = cx_slist_pop_front(slist);
426 
427  properties = giraffe_image_get_properties(sscience);
428  cx_assert(properties != NULL);
429 
430  if (nscience > 1) {
431 
432  /*
433  * Create a stacked science image from the list of raw images.
434  * Each raw image is disposed when it is no longer needed.
435  */
436 
437  cpl_msg_info(_id, "Averaging science frames ...");
438 
439  exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
440 
441  for (i = 1; i < nscience; i++) {
442 
443  cpl_propertylist* _properties;
444 
445  GiImage* science = cx_slist_pop_front(slist);
446 
447 
448  cpl_image_add(giraffe_image_get(sscience),
449  giraffe_image_get(science));
450 
451  _properties = giraffe_image_get_properties(science);
452  cx_assert(_properties != NULL);
453 
454  exptime += cpl_propertylist_get_double(_properties, GIALIAS_EXPTIME);
455 
456  giraffe_image_delete(science);
457 
458  }
459 
460  cpl_image_divide_scalar(giraffe_image_get(sscience), nscience);
461  }
462 
463  cx_assert(cx_slist_empty(slist));
464  cx_slist_delete(slist);
465  slist = NULL;
466 
467 
468  if (nscience > 1) {
469 
470  /*
471  * Update stacked science image properties
472  */
473 
474  cpl_msg_info(_id, "Updating stacked science image properties ...");
475 
476  cpl_propertylist_set_double(properties, GIALIAS_EXPTIME,
477  exptime / nscience);
478 
479  cpl_propertylist_append_double(properties, GIALIAS_EXPTTOT, exptime);
480  cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
481  "Total exposure time of all frames "
482  "combined");
483 
484  cpl_propertylist_append_int(properties, GIALIAS_DATANCOM, nscience);
485  cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM,
486  "Number of frames combined");
487 
488  cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
489 
490  }
491 
492 
493  /*
494  * Prepare for bias subtraction
495  */
496 
497  bias_config = giraffe_bias_config_create(config);
498 
499  /*
500  * Setup user defined areas to use for the bias computation
501  */
502 
503  if (bias_config->method == GIBIAS_METHOD_MASTER ||
504  bias_config->method == GIBIAS_METHOD_ZMASTER) {
505 
506  if (!mbias_frame) {
507  cpl_msg_error(_id, "Missing master bias frame! Selected bias "
508  "removal method requires a master bias frame!");
509 
510  giraffe_bias_config_destroy(bias_config);
511  giraffe_image_delete(sscience);
512 
513  return 1;
514  }
515  else {
516  filename = cpl_frame_get_filename(mbias_frame);
517 
518 
519  mbias = giraffe_image_new(CPL_TYPE_DOUBLE);
520  status = giraffe_image_load(mbias, filename, 0);
521 
522  if (status) {
523  cpl_msg_error(_id, "Cannot load master bias from '%s'. "
524  "Aborting ...", filename);
525 
526  giraffe_bias_config_destroy(bias_config);
527  giraffe_image_delete(sscience);
528 
529  return 1;
530  }
531  }
532  }
533 
534 
535  /*
536  * Load bad pixel map if it is present in the frame set.
537  */
538 
539  if (bpixel_frame) {
540 
541  filename = cpl_frame_get_filename(bpixel_frame);
542 
543 
544  bpixel = giraffe_image_new(CPL_TYPE_INT);
545  status = giraffe_image_load(bpixel, filename, 0);
546 
547  if (status) {
548  cpl_msg_error(_id, "Cannot load bad pixel map from '%s'. "
549  "Aborting ...", filename);
550 
551  giraffe_image_delete(bpixel);
552  bpixel = NULL;
553 
554  if (mbias != NULL) {
555  giraffe_image_delete(mbias);
556  mbias = NULL;
557  }
558 
559  giraffe_bias_config_destroy(bias_config);
560  bias_config = NULL;
561 
562  giraffe_image_delete(sscience);
563  sscience = NULL;
564 
565  return 1;
566  }
567 
568  }
569 
570 
571  /*
572  * Compute and remove the bias from the stacked flat field frame.
573  */
574 
575  rscience = giraffe_image_new(CPL_TYPE_DOUBLE);
576 
577  status = giraffe_bias_remove(rscience, sscience, mbias, bpixel, biasareas,
578  bias_config);
579 
580  giraffe_image_delete(sscience);
581 
582  if (mbias) {
583  giraffe_image_delete(mbias);
584  mbias = NULL;
585  }
586 
587  giraffe_bias_config_destroy(bias_config);
588 
589  if (status) {
590  cpl_msg_error(_id, "Bias removal failed. Aborting ...");
591 
592  giraffe_image_delete(rscience);
593  rscience = NULL;
594 
595  if (bpixel != NULL) {
596  giraffe_image_delete(bpixel);
597  bpixel = NULL;
598  }
599 
600  return 1;
601  }
602 
603 
604  /*
605  * Load master dark if it is present in the frame set and correct
606  * the master flat field for the dark current.
607  */
608 
609  if (mdark_frame) {
610 
611  GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
612 
613 
614  cpl_msg_info(_id, "Correcting for dark current ...");
615 
616  filename = cpl_frame_get_filename(mdark_frame);
617 
618  mdark = giraffe_image_new(CPL_TYPE_DOUBLE);
619  status = giraffe_image_load(mdark, filename, 0);
620 
621  if (status != 0) {
622  cpl_msg_error(_id, "Cannot load master dark from '%s'. "
623  "Aborting ...", filename);
624 
625  giraffe_image_delete(rscience);
626  rscience = NULL;
627 
628  if (bpixel != NULL) {
629  giraffe_image_delete(bpixel);
630  bpixel = NULL;
631  }
632 
633  return 1;
634  }
635 
636  status = giraffe_subtract_dark(rscience, mdark, bpixel, NULL,
637  &dark_config);
638 
639  if (status != 0) {
640  cpl_msg_error(_id, "Dark subtraction failed! Aborting ...");
641 
642  giraffe_image_delete(mdark);
643  mdark = NULL;
644 
645  giraffe_image_delete(rscience);
646  rscience = NULL;
647 
648  if (bpixel != NULL) {
649  giraffe_image_delete(bpixel);
650  bpixel = NULL;
651  }
652 
653  return 1;
654  }
655 
656  giraffe_image_delete(mdark);
657  mdark = NULL;
658 
659  }
660 
661 
662  /*
663  * Update the reduced science properties, save the reduced science frame
664  * and register it as product.
665  */
666 
667  cpl_msg_info(_id, "Writing pre-processed science image ...");
668 
669  giraffe_image_add_info(rscience, &info, set);
670 
671  rscience_frame = giraffe_frame_create_image(rscience,
672  GIFRAME_SCIENCE_REDUCED,
673  CPL_FRAME_LEVEL_INTERMEDIATE,
674  TRUE, TRUE);
675 
676  if (rscience_frame == NULL) {
677  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
678 
679  giraffe_image_delete(rscience);
680 
681  return 1;
682  }
683 
684  cpl_frameset_insert(set, rscience_frame);
685 
686 
687  /*
688  * Determine fiber setup
689  */
690 
691  science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
692 
693  cpl_msg_info(_id, "Building fiber setup for frame '%s'.",
694  cpl_frame_get_filename(science_frame));
695 
696  fibers = giraffe_fibers_setup(science_frame, locy_frame);
697 
698  if (!fibers) {
699  cpl_msg_error(_id, "Cannot create fiber setup for frame '%s'! "
700  "Aborting ...", cpl_frame_get_filename(science_frame));
701 
702  if (bpixel) {
703  giraffe_image_delete(bpixel);
704  bpixel = NULL;
705  }
706 
707  giraffe_image_delete(rscience);
708  rscience = NULL;
709 
710  return 1;
711  }
712 
713  cpl_msg_info(_id, "Fiber reference setup taken from localization "
714  "frame '%s'.", cpl_frame_get_filename(locy_frame));
715 
716 
717  /*
718  * Load fiber localization
719  */
720 
721  localization = giraffe_localization_new();
722 
723  filename = cpl_frame_get_filename(locy_frame);
724  status = 0;
725 
726  localization->locy = giraffe_image_new(CPL_TYPE_DOUBLE);
727  status = giraffe_image_load(localization->locy, filename, 0);
728 
729  if (status) {
730  cpl_msg_error(_id, "Cannot load localization (centroid "
731  "position) frame from '%s'. Aborting ...",
732  filename);
733 
734  giraffe_localization_destroy(localization);
735 
736  if (bpixel) {
737  giraffe_image_delete(bpixel);
738  bpixel = NULL;
739  }
740 
741  giraffe_table_delete(fibers);
742  giraffe_image_delete(rscience);
743 
744  return 1;
745  }
746 
747 
748  filename = cpl_frame_get_filename(locw_frame);
749  status = 0;
750 
751  localization->locw = giraffe_image_new(CPL_TYPE_DOUBLE);
752  status = giraffe_image_load(localization->locw, filename, 0);
753 
754  if (status) {
755  cpl_msg_error(_id, "Cannot load localization (spectrum width) "
756  "frame from '%s'. Aborting ...", filename);
757 
758  giraffe_localization_destroy(localization);
759 
760  if (bpixel) {
761  giraffe_image_delete(bpixel);
762  bpixel = NULL;
763  }
764 
765  giraffe_table_delete(fibers);
766  giraffe_image_delete(rscience);
767 
768  return 1;
769  }
770 
771 
772  /*
773  * Spectrum extraction
774  */
775 
776  if (slight_frame) {
777 
778  filename = cpl_frame_get_filename(slight_frame);
779 
780 
781  slight = giraffe_image_new(CPL_TYPE_DOUBLE);
782  status = giraffe_image_load(slight, filename, 0);
783 
784  if (status) {
785  cpl_msg_error(_id, "Cannot load scattered light model from '%s'. "
786  "Aborting ...", filename);
787 
788  giraffe_image_delete(slight);
789 
790  giraffe_localization_destroy(localization);
791 
792  if (bpixel) {
793  giraffe_image_delete(bpixel);
794  bpixel = NULL;
795  }
796 
797  giraffe_table_delete(fibers);
798  giraffe_image_delete(rscience);
799 
800  return 1;
801 
802  }
803 
804  }
805 
806 
807  extract_config = giraffe_extract_config_create(config);
808 
809  if ((extract_config->emethod == GIEXTRACT_OPTIMAL) ||
810  (extract_config->emethod == GIEXTRACT_HORNE)) {
811 
812  if (psfdata_frame == NULL) {
813 
814  const cxchar* emethod = "Optimal";
815 
816  if (extract_config->emethod == GIEXTRACT_HORNE) {
817  emethod = "Horne";
818  }
819 
820  cpl_msg_error(_id, "%s spectrum extraction requires PSF "
821  "profile data. Aborting ...", emethod);
822 
823  giraffe_extract_config_destroy(extract_config);
824  extract_config = NULL;
825 
826  if (slight != NULL) {
827  giraffe_image_delete(slight);
828  slight = NULL;
829  }
830 
831  giraffe_localization_destroy(localization);
832  localization = NULL;
833 
834  if (bpixel) {
835  giraffe_image_delete(bpixel);
836  bpixel = NULL;
837  }
838 
839  giraffe_table_delete(fibers);
840  fibers = NULL;
841 
842  giraffe_image_delete(rscience);
843  rscience = NULL;
844 
845  return 1;
846 
847  }
848  else {
849 
850  filename = cpl_frame_get_filename(psfdata_frame);
851  status = 0;
852 
853  localization->psf = giraffe_psfdata_new();
854  status = giraffe_psfdata_load(localization->psf, filename);
855 
856  if (status) {
857  cpl_msg_error(_id, "Cannot load PSF profile data frame from "
858  "'%s'. Aborting ...", filename);
859 
860  giraffe_extract_config_destroy(extract_config);
861  extract_config = NULL;
862 
863  if (slight != NULL) {
864  giraffe_image_delete(slight);
865  slight = NULL;
866  }
867 
868  giraffe_localization_destroy(localization);
869  localization = NULL;
870 
871  if (bpixel) {
872  giraffe_image_delete(bpixel);
873  bpixel = NULL;
874  }
875 
876  giraffe_table_delete(fibers);
877  fibers = NULL;
878 
879  giraffe_image_delete(rscience);
880  rscience = NULL;
881 
882  return 1;
883 
884  }
885 
886  }
887 
888  }
889 
890 
891  extraction = giraffe_extraction_new();
892 
893  status = giraffe_extract_spectra(extraction, rscience, fibers,
894  localization, bpixel, slight,
895  extract_config);
896 
897  if (status) {
898  cpl_msg_error(_id, "Spectrum extraction failed! Aborting ...");
899 
900  giraffe_extraction_destroy(extraction);
901  giraffe_extract_config_destroy(extract_config);
902 
903  giraffe_image_delete(slight);
904 
905  giraffe_localization_destroy(localization);
906 
907  if (bpixel) {
908  giraffe_image_delete(bpixel);
909  bpixel = NULL;
910  }
911 
912  giraffe_table_delete(fibers);
913  giraffe_image_delete(rscience);
914 
915  return 1;
916  }
917 
918  giraffe_image_delete(slight);
919  slight = NULL;
920 
921  if (bpixel) {
922  giraffe_image_delete(bpixel);
923  bpixel = NULL;
924  }
925 
926  giraffe_image_delete(rscience);
927  rscience = NULL;
928 
929  giraffe_extract_config_destroy(extract_config);
930 
931 
932  /*
933  * Apply flat field and apply the relative fiber transmission correction.
934  */
935 
936  flat_config = giraffe_flat_config_create(config);
937 
938  if (flat_config->load == TRUE) {
939 
940  cpl_frame* flat_frame = NULL;
941 
942  GiImage* flat = NULL;
943 
944 
945  flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTSPECTRA);
946 
947  if (flat_frame == NULL) {
948  cpl_msg_error(_id, "Missing flat field spectra frame!");
949 
950  giraffe_flat_config_destroy(flat_config);
951 
952  giraffe_extraction_destroy(extraction);
953  giraffe_localization_destroy(localization);
954 
955  giraffe_table_delete(wcalcoeff);
956 
957  giraffe_table_delete(grating);
958  giraffe_table_delete(fibers);
959 
960  return 1;
961  }
962 
963  filename = cpl_frame_get_filename(flat_frame);
964 
965  flat = giraffe_image_new(CPL_TYPE_DOUBLE);
966  status = giraffe_image_load(flat, filename, 0);
967 
968  if (status) {
969  cpl_msg_error(_id, "Cannot load flat field spectra from '%s'. "
970  "Aborting ...", filename);
971 
972  giraffe_image_delete(flat);
973 
974  giraffe_flat_config_destroy(flat_config);
975 
976  giraffe_extraction_destroy(extraction);
977  giraffe_localization_destroy(localization);
978 
979  giraffe_table_delete(wcalcoeff);
980 
981  giraffe_table_delete(grating);
982  giraffe_table_delete(fibers);
983 
984  return 1;
985  }
986 
987  if (flat_config->apply == TRUE) {
988 
989  GiImage* errors = NULL;
990 
991 
992  flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTERRORS);
993 
994  if (flat_frame == NULL) {
995  cpl_msg_warning(_id, "Missing flat field spectra errors "
996  "frame!");
997  }
998  else {
999 
1000  filename = cpl_frame_get_filename(flat_frame);
1001 
1002  errors = giraffe_image_new(CPL_TYPE_DOUBLE);
1003  status = giraffe_image_load(errors, filename, 0);
1004 
1005  if (status) {
1006  cpl_msg_error(_id, "Cannot load flat field spectra "
1007  "errors from '%s'. Aborting ...",
1008  filename);
1009 
1010  giraffe_image_delete(errors);
1011  giraffe_image_delete(flat);
1012 
1013  giraffe_flat_config_destroy(flat_config);
1014 
1015  giraffe_extraction_destroy(extraction);
1016  giraffe_localization_destroy(localization);
1017 
1018  giraffe_table_delete(wcalcoeff);
1019 
1020  giraffe_table_delete(grating);
1021  giraffe_table_delete(fibers);
1022 
1023  return 1;
1024  }
1025 
1026  }
1027 
1028  cpl_msg_info(_id, "Applying flat field correction ...");
1029 
1030  status = giraffe_flat_apply(extraction, fibers, flat, errors,
1031  flat_config);
1032 
1033  if (status) {
1034  cpl_msg_error(_id, "Flat field correction failed! "
1035  "Aborting ...");
1036 
1037  giraffe_image_delete(errors);
1038  giraffe_image_delete(flat);
1039 
1040  giraffe_flat_config_destroy(flat_config);
1041 
1042  giraffe_extraction_destroy(extraction);
1043  giraffe_localization_destroy(localization);
1044 
1045  giraffe_table_delete(wcalcoeff);
1046 
1047  giraffe_table_delete(grating);
1048  giraffe_table_delete(fibers);
1049 
1050  return 1;
1051  }
1052 
1053  giraffe_image_delete(errors);
1054  errors = NULL;
1055 
1056  }
1057 
1058  if (flat_config->transmission == TRUE) {
1059 
1060  const cxchar* _filename = cpl_frame_get_filename(flat_frame);
1061 
1062  GiTable* _fibers = NULL;
1063 
1064 
1065  cpl_msg_info(_id, "Loading fiber setup for frame '%s'.",
1066  _filename);
1067 
1068  _fibers = giraffe_fiberlist_load(_filename, 1, "FIBER_SETUP");
1069 
1070  if (!_fibers) {
1071  cpl_msg_error(_id, "Cannot create fiber setup for "
1072  "frame '%s'! Aborting ...", _filename);
1073 
1074  giraffe_image_delete(flat);
1075 
1076  giraffe_flat_config_destroy(flat_config);
1077 
1078  giraffe_extraction_destroy(extraction);
1079  giraffe_localization_destroy(localization);
1080 
1081  giraffe_table_delete(wcalcoeff);
1082 
1083  giraffe_table_delete(grating);
1084  giraffe_table_delete(fibers);
1085 
1086  return 1;
1087  }
1088 
1089  cpl_msg_info(_id, "Applying relative fiber transmission "
1090  "correction");
1091 
1092  status = giraffe_transmission_setup(fibers, _fibers);
1093  giraffe_table_delete(_fibers);
1094 
1095  if (status == 0) {
1096  status = giraffe_transmission_apply(extraction, fibers);
1097  }
1098 
1099  if (status) {
1100 
1101  cpl_msg_error(_id, "Relative transmission correction failed! "
1102  "Aborting ...");
1103 
1104  giraffe_image_delete(flat);
1105 
1106  giraffe_flat_config_destroy(flat_config);
1107 
1108  giraffe_extraction_destroy(extraction);
1109  giraffe_localization_destroy(localization);
1110 
1111  giraffe_table_delete(wcalcoeff);
1112 
1113  giraffe_table_delete(grating);
1114  giraffe_table_delete(fibers);
1115 
1116  return 1;
1117 
1118  }
1119 
1120  }
1121 
1122  giraffe_image_delete(flat);
1123 
1124  }
1125 
1126  giraffe_flat_config_destroy(flat_config);
1127 
1128 
1129  /*
1130  * Save the spectrum extraction results and register them as
1131  * products.
1132  */
1133 
1134  cpl_msg_info(_id, "Writing extracted spectra ...");
1135 
1136  /* Extracted spectra */
1137 
1138  giraffe_image_add_info(extraction->spectra, &info, set);
1139 
1140  sext_frame = giraffe_frame_create_image(extraction->spectra,
1141  GIFRAME_SCIENCE_EXTSPECTRA,
1142  CPL_FRAME_LEVEL_FINAL,
1143  TRUE, TRUE);
1144 
1145  if (sext_frame == NULL) {
1146  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1147 
1148  giraffe_extraction_destroy(extraction);
1149  giraffe_localization_destroy(localization);
1150 
1151  giraffe_table_delete(wcalcoeff);
1152 
1153  giraffe_table_delete(grating);
1154  giraffe_table_delete(fibers);
1155 
1156  return 1;
1157  }
1158 
1159  status = giraffe_fiberlist_attach(sext_frame, fibers);
1160 
1161  if (status) {
1162  cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1163  "Aborting ...", cpl_frame_get_filename(sext_frame));
1164 
1165  cpl_frame_delete(sext_frame);
1166 
1167  giraffe_extraction_destroy(extraction);
1168  giraffe_localization_destroy(localization);
1169 
1170  giraffe_table_delete(wcalcoeff);
1171 
1172  giraffe_table_delete(grating);
1173  giraffe_table_delete(fibers);
1174 
1175  return 1;
1176  }
1177 
1178  cpl_frameset_insert(set, sext_frame);
1179 
1180  /* Extracted spectra errors */
1181 
1182  giraffe_image_add_info(extraction->error, &info, set);
1183 
1184  sext_frame = giraffe_frame_create_image(extraction->error,
1185  GIFRAME_SCIENCE_EXTERRORS,
1186  CPL_FRAME_LEVEL_FINAL,
1187  TRUE, TRUE);
1188 
1189  if (sext_frame == NULL) {
1190  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1191 
1192  giraffe_extraction_destroy(extraction);
1193  giraffe_localization_destroy(localization);
1194 
1195  giraffe_table_delete(wcalcoeff);
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(wcalcoeff);
1215 
1216  giraffe_table_delete(grating);
1217  giraffe_table_delete(fibers);
1218 
1219  return 1;
1220  }
1221 
1222  cpl_frameset_insert(set, sext_frame);
1223 
1224  /* Extracted spectra pixels */
1225 
1226  if (extraction->npixels != NULL) {
1227 
1228  giraffe_image_add_info(extraction->npixels, &info, set);
1229 
1230  sext_frame = giraffe_frame_create_image(extraction->npixels,
1231  GIFRAME_SCIENCE_EXTPIXELS,
1232  CPL_FRAME_LEVEL_FINAL,
1233  TRUE, TRUE);
1234 
1235  if (sext_frame == NULL) {
1236  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1237 
1238  giraffe_extraction_destroy(extraction);
1239  giraffe_localization_destroy(localization);
1240 
1241  giraffe_table_delete(wcalcoeff);
1242 
1243  giraffe_table_delete(grating);
1244  giraffe_table_delete(fibers);
1245 
1246  return 1;
1247  }
1248 
1249  status = giraffe_fiberlist_attach(sext_frame, fibers);
1250 
1251  if (status) {
1252  cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1253  "Aborting ...", cpl_frame_get_filename(sext_frame));
1254 
1255  cpl_frame_delete(sext_frame);
1256 
1257  giraffe_extraction_destroy(extraction);
1258  giraffe_localization_destroy(localization);
1259 
1260  giraffe_table_delete(wcalcoeff);
1261 
1262  giraffe_table_delete(grating);
1263  giraffe_table_delete(fibers);
1264 
1265  return 1;
1266  }
1267 
1268  cpl_frameset_insert(set, sext_frame);
1269 
1270  }
1271 
1272  /* Extracted spectra centroids */
1273 
1274  giraffe_image_add_info(extraction->centroid, &info, set);
1275 
1276  sext_frame = giraffe_frame_create_image(extraction->centroid,
1277  GIFRAME_SCIENCE_EXTTRACE,
1278  CPL_FRAME_LEVEL_FINAL,
1279  TRUE, TRUE);
1280 
1281  if (sext_frame == NULL) {
1282  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1283 
1284  giraffe_extraction_destroy(extraction);
1285  giraffe_localization_destroy(localization);
1286 
1287  giraffe_table_delete(wcalcoeff);
1288 
1289  giraffe_table_delete(grating);
1290  giraffe_table_delete(fibers);
1291 
1292  return 1;
1293  }
1294 
1295  status = giraffe_fiberlist_attach(sext_frame, fibers);
1296 
1297  if (status) {
1298  cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1299  "Aborting ...", cpl_frame_get_filename(sext_frame));
1300 
1301  cpl_frame_delete(sext_frame);
1302 
1303  giraffe_extraction_destroy(extraction);
1304  giraffe_localization_destroy(localization);
1305 
1306  giraffe_table_delete(wcalcoeff);
1307 
1308  giraffe_table_delete(grating);
1309  giraffe_table_delete(fibers);
1310 
1311  return 1;
1312  }
1313 
1314  cpl_frameset_insert(set, sext_frame);
1315 
1316  /* Extraction model spectra */
1317 
1318  if (extraction->model != NULL) {
1319 
1320  giraffe_image_add_info(extraction->model, &info, set);
1321 
1322  sext_frame = giraffe_frame_create_image(extraction->model,
1323  GIFRAME_SCIENCE_EXTMODEL,
1324  CPL_FRAME_LEVEL_FINAL,
1325  TRUE, TRUE);
1326 
1327  if (sext_frame == NULL) {
1328  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1329 
1330  giraffe_extraction_destroy(extraction);
1331  giraffe_localization_destroy(localization);
1332 
1333  giraffe_table_delete(wcalcoeff);
1334 
1335  giraffe_table_delete(grating);
1336  giraffe_table_delete(fibers);
1337 
1338  return 1;
1339  }
1340 
1341  status = giraffe_fiberlist_attach(sext_frame, fibers);
1342 
1343  if (status != 0) {
1344  cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
1345  "Aborting ...", cpl_frame_get_filename(sext_frame));
1346 
1347  cpl_frame_delete(sext_frame);
1348 
1349  giraffe_extraction_destroy(extraction);
1350  giraffe_localization_destroy(localization);
1351 
1352  giraffe_table_delete(wcalcoeff);
1353 
1354  giraffe_table_delete(grating);
1355  giraffe_table_delete(fibers);
1356 
1357  return 1;
1358  }
1359 
1360  cpl_frameset_insert(set, sext_frame);
1361 
1362  }
1363 
1364 
1365  /*
1366  * Load dispersion solution
1367  */
1368 
1369 
1370  filename = (cxchar *)cpl_frame_get_filename(wcal_frame);
1371 
1372  wcalcoeff = giraffe_table_new();
1373  status = giraffe_table_load(wcalcoeff, filename, 1, NULL);
1374 
1375  if (status) {
1376  cpl_msg_error(_id, "Cannot load dispersion solution from "
1377  "'%s'. Aborting ...", filename);
1378 
1379  giraffe_extraction_destroy(extraction);
1380  giraffe_localization_destroy(localization);
1381 
1382  giraffe_table_delete(wcalcoeff);
1383 
1384  giraffe_table_delete(grating);
1385  giraffe_table_delete(fibers);
1386 
1387  return 1;
1388  }
1389 
1390 
1391  /*
1392  * Load grating data
1393  */
1394 
1395  filename = (cxchar *)cpl_frame_get_filename(grating_frame);
1396 
1397  status = 0;
1398 
1399  grating = giraffe_table_new();
1400  status = giraffe_table_load(grating, filename, 1, NULL);
1401 
1402  if (status) {
1403  cpl_msg_error(_id, "Cannot load grating data from '%s'. "
1404  "Aborting ...", filename);
1405 
1406  giraffe_extraction_destroy(extraction);
1407  giraffe_localization_destroy(localization);
1408 
1409  giraffe_table_delete(wcalcoeff);
1410 
1411  giraffe_table_delete(grating);
1412  giraffe_table_delete(fibers);
1413 
1414  return 1;
1415  }
1416 
1417 
1418  /*
1419  * Load slit geometry data
1420  */
1421 
1422 
1423  filename = (cxchar *)cpl_frame_get_filename(slit_frame);
1424 
1425  slitgeometry = giraffe_slitgeometry_load(fibers, filename, 1, NULL);
1426 
1427  if (slitgeometry == NULL) {
1428  cpl_msg_error(_id, "Cannot load slit geometry data from '%s'. "
1429  "Aborting ...", filename);
1430 
1431  giraffe_table_delete(wcalcoeff);
1432 
1433  giraffe_extraction_destroy(extraction);
1434  giraffe_localization_destroy(localization);
1435 
1436  giraffe_table_delete(wcalcoeff);
1437 
1438  giraffe_table_delete(grating);
1439  giraffe_table_delete(fibers);
1440 
1441  return 1;
1442  }
1443  else {
1444 
1445  /*
1446  * Check whether the contains the positions for all fibers
1447  * provided by the fiber setup. If this is not the case
1448  * this is an error.
1449  */
1450 
1451  if (giraffe_fiberlist_compare(slitgeometry, fibers) != 1) {
1452  cpl_msg_error(_id, "Slit geometry data from '%s' is not "
1453  "applicable for current fiber setup! "
1454  "Aborting ...", filename);
1455 
1456  giraffe_table_delete(slitgeometry);
1457  giraffe_table_delete(wcalcoeff);
1458 
1459  giraffe_extraction_destroy(extraction);
1460  giraffe_localization_destroy(localization);
1461 
1462  giraffe_table_delete(wcalcoeff);
1463 
1464  giraffe_table_delete(grating);
1465  giraffe_table_delete(fibers);
1466 
1467  return 1;
1468  }
1469 
1470  }
1471 
1472 
1473 
1474  /*
1475  * Spectrum rebinning
1476  */
1477 
1478  cpl_msg_info(_id, "Spectrum rebinning");
1479 
1480  rebin_config = giraffe_rebin_config_create(config);
1481 
1482  rebinning = giraffe_rebinning_new();
1483 
1484  status = giraffe_rebin_spectra(rebinning, extraction, fibers,
1485  localization, grating, slitgeometry,
1486  wcalcoeff, rebin_config);
1487 
1488  if (status) {
1489  cpl_msg_error(_id, "Rebinning of science spectra failed! Aborting...");
1490 
1491  giraffe_rebinning_destroy(rebinning);
1492 
1493  giraffe_extraction_destroy(extraction);
1494  giraffe_localization_destroy(localization);
1495 
1496  giraffe_table_delete(wcalcoeff);
1497 
1498  giraffe_table_delete(slitgeometry);
1499  giraffe_table_delete(grating);
1500  giraffe_table_delete(fibers);
1501 
1502  giraffe_rebin_config_destroy(rebin_config);
1503 
1504  return 1;
1505 
1506  }
1507 
1508 
1509  /*
1510  * Optionally compute and apply spectral shifts from the simultaneous
1511  * calibration fibers. This is only done if the simultaneous calibration
1512  * fibers were used.
1513  */
1514 
1515  p = cpl_parameterlist_find(config, "giraffe.siwc.apply");
1516  cx_assert(p != NULL);
1517 
1518  siwc = cpl_parameter_get_bool(p);
1519  p = NULL;
1520 
1521  properties = giraffe_image_get_properties(rebinning->spectra);
1522  cx_assert(properties != NULL);
1523 
1524 
1525  if (cpl_propertylist_has(properties, GIALIAS_STSCTAL) == TRUE) {
1526  calsim = cpl_propertylist_get_bool(properties, GIALIAS_STSCTAL);
1527  }
1528 
1529 
1530  if ((siwc == TRUE) && (calsim == TRUE) && (linemask_frame != NULL)) {
1531 
1532  GiTable* linemask = giraffe_table_new();
1533 
1534  GiSGCalConfig* siwc_config = NULL;
1535 
1536 
1537  siwc_config = giraffe_sgcalibration_config_create(config);
1538 
1539  if (siwc_config == NULL) {
1540 
1541  giraffe_table_delete(linemask);
1542  linemask = NULL;
1543 
1544  giraffe_rebinning_destroy(rebinning);
1545 
1546  giraffe_extraction_destroy(extraction);
1547  giraffe_localization_destroy(localization);
1548 
1549  giraffe_table_delete(wcalcoeff);
1550 
1551  giraffe_table_delete(slitgeometry);
1552  giraffe_table_delete(grating);
1553  giraffe_table_delete(fibers);
1554 
1555  giraffe_rebin_config_destroy(rebin_config);
1556 
1557  return 1;
1558 
1559  }
1560 
1561  filename = cpl_frame_get_filename(linemask_frame);
1562 
1563  status = giraffe_table_load(linemask, filename, 1, NULL);
1564 
1565  if (status) {
1566  cpl_msg_error(_id, "Cannot load line reference mask from '%s'. "
1567  "Aborting ...", filename);
1568 
1570  siwc_config = NULL;
1571 
1572  giraffe_table_delete(linemask);
1573  linemask = NULL;
1574 
1575  giraffe_rebinning_destroy(rebinning);
1576 
1577  giraffe_extraction_destroy(extraction);
1578  giraffe_localization_destroy(localization);
1579 
1580  giraffe_table_delete(wcalcoeff);
1581 
1582  giraffe_table_delete(slitgeometry);
1583  giraffe_table_delete(grating);
1584  giraffe_table_delete(fibers);
1585 
1586  giraffe_rebin_config_destroy(rebin_config);
1587 
1588  return 1;
1589 
1590  }
1591 
1592 
1593  status = giraffe_compute_offsets(fibers, rebinning, grating,
1594  linemask, siwc_config);
1595 
1596  if (status != 0) {
1597  cpl_msg_error(_id, "Applying simultaneous wavelength "
1598  "calibration correction failed! Aborting...");
1599 
1601  siwc_config = NULL;
1602 
1603  giraffe_table_delete(linemask);
1604  linemask = NULL;
1605 
1606  giraffe_rebinning_destroy(rebinning);
1607 
1608  giraffe_extraction_destroy(extraction);
1609  giraffe_localization_destroy(localization);
1610 
1611  giraffe_table_delete(wcalcoeff);
1612 
1613  giraffe_table_delete(slitgeometry);
1614  giraffe_table_delete(grating);
1615  giraffe_table_delete(fibers);
1616 
1617  giraffe_rebin_config_destroy(rebin_config);
1618 
1619  return 1;
1620 
1621  }
1622 
1624  siwc_config = NULL;
1625 
1626  giraffe_table_delete(linemask);
1627  linemask = NULL;
1628 
1629  giraffe_rebinning_destroy(rebinning);
1630  rebinning = giraffe_rebinning_new();
1631 
1632  status = giraffe_rebin_spectra(rebinning, extraction, fibers,
1633  localization, grating, slitgeometry,
1634  wcalcoeff, rebin_config);
1635 
1636  if (status) {
1637  cpl_msg_error(_id, "Rebinning of science spectra failed! "
1638  "Aborting...");
1639 
1640  giraffe_rebinning_destroy(rebinning);
1641 
1642  giraffe_extraction_destroy(extraction);
1643  giraffe_localization_destroy(localization);
1644 
1645  giraffe_table_delete(wcalcoeff);
1646 
1647  giraffe_table_delete(slitgeometry);
1648  giraffe_table_delete(grating);
1649  giraffe_table_delete(fibers);
1650 
1651  giraffe_rebin_config_destroy(rebin_config);
1652 
1653  return 1;
1654 
1655  }
1656 
1657  }
1658 
1659  giraffe_extraction_destroy(extraction);
1660  extraction = NULL;
1661 
1662  giraffe_localization_destroy(localization);
1663  localization = NULL;
1664 
1665  giraffe_rebin_config_destroy(rebin_config);
1666  rebin_config = NULL;
1667 
1668 
1669  /*
1670  * Compute barycentric correction for each object spectrum (fiber)
1671  */
1672 
1673  status = giraffe_add_rvcorrection(fibers, rebinning->spectra);
1674 
1675  switch (status) {
1676  case 0:
1677  {
1678  break;
1679  }
1680 
1681  case 1:
1682  {
1683  cpl_msg_warning(_id, "Missing observation time properties! "
1684  "Barycentric correction computation "
1685  "skipped!");
1686  status = 0;
1687  break;
1688  }
1689  case 2:
1690  {
1691  cpl_msg_warning(_id, "Missing telescope location properties! "
1692  "Barycentric correction computation "
1693  "skipped!");
1694  status = 0;
1695  break;
1696  }
1697  case 3:
1698  {
1699  cpl_msg_warning(_id, "Object positions are not available "
1700  "Barycentric correction computation "
1701  "skipped!");
1702  status = 0;
1703  break;
1704  }
1705  default:
1706  {
1707  cpl_msg_error(_id, "Barycentric correction computation "
1708  "failed! Aborting...");
1709 
1710  giraffe_rebinning_destroy(rebinning);
1711 
1712  giraffe_table_delete(wcalcoeff);
1713 
1714  giraffe_table_delete(slitgeometry);
1715  giraffe_table_delete(grating);
1716  giraffe_table_delete(fibers);
1717 
1718  return 1;
1719  break;
1720  }
1721 
1722  }
1723 
1724 
1725  /*
1726  * Save and register the results of the spectrum rebinning.
1727  */
1728 
1729  /* Rebinned spectra */
1730 
1731  giraffe_image_add_info(rebinning->spectra, &info, set);
1732 
1733  rbin_frame = giraffe_frame_create_image(rebinning->spectra,
1734  GIFRAME_SCIENCE_RBNSPECTRA,
1735  CPL_FRAME_LEVEL_FINAL,
1736  TRUE, TRUE);
1737 
1738  if (rbin_frame == NULL) {
1739  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1740 
1741  giraffe_rebinning_destroy(rebinning);
1742 
1743  giraffe_table_delete(wcalcoeff);
1744 
1745  giraffe_table_delete(slitgeometry);
1746  giraffe_table_delete(grating);
1747  giraffe_table_delete(fibers);
1748 
1749  return 1;
1750  }
1751 
1752  status = giraffe_fiberlist_attach(rbin_frame, fibers);
1753 
1754  if (status) {
1755  cpl_msg_error(_id, "Cannot attach fiber setup to local "
1756  "file '%s'! Aborting ...",
1757  cpl_frame_get_filename(rbin_frame));
1758 
1759  giraffe_rebinning_destroy(rebinning);
1760  giraffe_table_delete(wcalcoeff);
1761 
1762  giraffe_table_delete(slitgeometry);
1763  giraffe_table_delete(grating);
1764  giraffe_table_delete(fibers);
1765 
1766  cpl_frame_delete(rbin_frame);
1767 
1768  return 1;
1769  }
1770 
1771  cpl_frameset_insert(set, rbin_frame);
1772 
1773  /* Rebinned spectra errors */
1774 
1775  giraffe_image_add_info(rebinning->errors, &info, set);
1776 
1777  rbin_frame = giraffe_frame_create_image(rebinning->errors,
1778  GIFRAME_SCIENCE_RBNERRORS,
1779  CPL_FRAME_LEVEL_FINAL,
1780  TRUE, TRUE);
1781 
1782  if (rbin_frame == NULL) {
1783  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1784 
1785  giraffe_rebinning_destroy(rebinning);
1786 
1787  giraffe_table_delete(wcalcoeff);
1788 
1789  giraffe_table_delete(slitgeometry);
1790  giraffe_table_delete(grating);
1791  giraffe_table_delete(fibers);
1792 
1793  return 1;
1794  }
1795 
1796  status = giraffe_fiberlist_attach(rbin_frame, fibers);
1797 
1798  if (status) {
1799  cpl_msg_error(_id, "Cannot attach fiber setup to local "
1800  "file '%s'! Aborting ...",
1801  cpl_frame_get_filename(rbin_frame));
1802 
1803  giraffe_rebinning_destroy(rebinning);
1804 
1805  giraffe_table_delete(wcalcoeff);
1806 
1807  giraffe_table_delete(slitgeometry);
1808  giraffe_table_delete(grating);
1809  giraffe_table_delete(fibers);
1810 
1811  cpl_frame_delete(rbin_frame);
1812 
1813  return 1;
1814  }
1815 
1816  cpl_frameset_insert(set, rbin_frame);
1817 
1818 
1819  /*
1820  * Optional image and data cube construction (only for IFU and Argus)
1821  */
1822 
1823  properties = giraffe_image_get_properties(rebinning->spectra);
1824  mode = giraffe_get_mode(properties);
1825 
1826 
1827  if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
1828 
1829  cpl_frame* rimg_frame = NULL;
1830 
1831  GiFieldOfView* fov = NULL;
1832 
1833  GiFieldOfViewConfig* fov_config = NULL;
1834 
1835  GiFieldOfViewCubeFormat cube_format = GIFOV_FORMAT_ESO3D;
1836 
1837 
1838  fov_config = giraffe_fov_config_create(config);
1839 
1840  cube_format = fov_config->format;
1841 
1842 
1843  cpl_msg_info(_id, "Reconstructing image and data cube from rebinned "
1844  "spectra ...");
1845 
1846  fov = giraffe_fov_new();
1847 
1848  status = giraffe_fov_build(fov, rebinning, fibers, wcalcoeff, grating,
1849  slitgeometry, fov_config);
1850 
1851  if (status) {
1852 
1853  if (status == -2) {
1854  cpl_msg_warning(_id, "No reconstructed image was built. "
1855  "Fiber list has no fiber position "
1856  "information.");
1857  }
1858  else {
1859  cpl_msg_error(_id, "Image reconstruction failed! Aborting...");
1860 
1861  giraffe_fov_delete(fov);
1862  giraffe_rebinning_destroy(rebinning);
1863 
1864  giraffe_table_delete(wcalcoeff);
1865 
1866  giraffe_table_delete(slitgeometry);
1867  giraffe_table_delete(grating);
1868  giraffe_table_delete(fibers);
1869 
1870  giraffe_fov_config_destroy(fov_config);
1871 
1872  return 1;
1873  }
1874 
1875  }
1876 
1877  giraffe_fov_config_destroy(fov_config);
1878 
1879 
1880  /*
1881  * Save and register the results of the image reconstruction.
1882  */
1883 
1884  /* Reconstructed image */
1885 
1886  giraffe_image_add_info(fov->fov.spectra, &info, set);
1887 
1888  rimg_frame = giraffe_frame_create_image(fov->fov.spectra,
1889  GIFRAME_SCIENCE_RCSPECTRA,
1890  CPL_FRAME_LEVEL_FINAL,
1891  TRUE, TRUE);
1892 
1893  if (rimg_frame == NULL) {
1894  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1895 
1896  giraffe_fov_delete(fov);
1897  giraffe_rebinning_destroy(rebinning);
1898 
1899  giraffe_table_delete(wcalcoeff);
1900 
1901  giraffe_table_delete(slitgeometry);
1902  giraffe_table_delete(grating);
1903  giraffe_table_delete(fibers);
1904 
1905  return 1;
1906  }
1907 
1908  cpl_frameset_insert(set, rimg_frame);
1909 
1910 
1911  /* Reconstructed image errors */
1912 
1913  giraffe_image_add_info(fov->fov.errors, &info, set);
1914 
1915  rimg_frame = giraffe_frame_create_image(fov->fov.errors,
1916  GIFRAME_SCIENCE_RCERRORS,
1917  CPL_FRAME_LEVEL_FINAL,
1918  TRUE, TRUE);
1919 
1920  if (rimg_frame == NULL) {
1921  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
1922 
1923  giraffe_fov_delete(fov);
1924  giraffe_rebinning_destroy(rebinning);
1925 
1926  giraffe_table_delete(wcalcoeff);
1927 
1928  giraffe_table_delete(slitgeometry);
1929  giraffe_table_delete(grating);
1930  giraffe_table_delete(fibers);
1931 
1932  return 1;
1933  }
1934 
1935  cpl_frameset_insert(set, rimg_frame);
1936 
1937 
1938  /* Save data cubes according to format selection */
1939 
1940  if (cube_format == GIFOV_FORMAT_SINGLE) {
1941 
1942  /* Spectrum cube */
1943 
1944  if (fov->cubes.spectra != NULL) {
1945 
1946  cxint component = 0;
1947 
1948  GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
1949 
1950 
1951  properties = giraffe_image_get_properties(rebinning->spectra);
1952  properties = cpl_propertylist_duplicate(properties);
1953 
1954  giraffe_add_frameset_info(properties, set, info.sequence);
1955 
1956  rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_SPECTRA,
1957  CPL_FRAME_LEVEL_FINAL,
1958  properties,
1959  fov,
1960  &component,
1961  creator);
1962 
1963  cpl_propertylist_delete(properties);
1964  properties = NULL;
1965 
1966  if (rimg_frame == NULL) {
1967  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
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  status = giraffe_fiberlist_attach(rimg_frame, fibers);
1991 
1992  if (status != 0) {
1993  cpl_msg_error(_id, "Cannot attach fiber setup to local "
1994  "file '%s'! Aborting ...",
1995  cpl_frame_get_filename(rimg_frame));
1996 
1997  cpl_frame_delete(rimg_frame);
1998 
1999  giraffe_fov_delete(fov);
2000  fov = NULL;
2001 
2002  giraffe_rebinning_destroy(rebinning);
2003  rebinning = NULL;
2004 
2005  giraffe_table_delete(wcalcoeff);
2006  wcalcoeff = NULL;
2007 
2008  giraffe_table_delete(slitgeometry);
2009  slitgeometry = NULL;
2010 
2011  giraffe_table_delete(grating);
2012  grating = NULL;
2013 
2014  giraffe_table_delete(fibers);
2015  fibers = NULL;
2016 
2017  return 1;
2018  }
2019 
2020  cpl_frameset_insert(set, rimg_frame);
2021 
2022  }
2023 
2024  /* Error cube */
2025 
2026  if (fov->cubes.errors != NULL) {
2027 
2028  cxint component = 1;
2029 
2030  GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
2031 
2032 
2033  properties = giraffe_image_get_properties(rebinning->errors);
2034  properties = cpl_propertylist_duplicate(properties);
2035 
2036  giraffe_add_frameset_info(properties, set, info.sequence);
2037 
2038  rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_ERRORS,
2039  CPL_FRAME_LEVEL_FINAL,
2040  properties,
2041  fov,
2042  &component,
2043  creator);
2044 
2045  cpl_propertylist_delete(properties);
2046  properties = NULL;
2047 
2048  if (rimg_frame == NULL) {
2049  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
2050 
2051  giraffe_fov_delete(fov);
2052  fov = NULL;
2053 
2054  giraffe_rebinning_destroy(rebinning);
2055  rebinning = NULL;
2056 
2057  giraffe_table_delete(wcalcoeff);
2058  wcalcoeff = NULL;
2059 
2060  giraffe_table_delete(slitgeometry);
2061  slitgeometry = NULL;
2062 
2063  giraffe_table_delete(grating);
2064  grating = NULL;
2065 
2066  giraffe_table_delete(fibers);
2067  fibers = NULL;
2068 
2069  return 1;
2070  }
2071 
2072  status = giraffe_fiberlist_attach(rimg_frame, fibers);
2073 
2074  if (status != 0) {
2075  cpl_msg_error(_id, "Cannot attach fiber setup to local "
2076  "file '%s'! Aborting ...",
2077  cpl_frame_get_filename(rimg_frame));
2078 
2079  cpl_frame_delete(rimg_frame);
2080 
2081  giraffe_fov_delete(fov);
2082  fov = NULL;
2083 
2084  giraffe_rebinning_destroy(rebinning);
2085  rebinning = NULL;
2086 
2087  giraffe_table_delete(wcalcoeff);
2088  wcalcoeff = NULL;
2089 
2090  giraffe_table_delete(slitgeometry);
2091  slitgeometry = NULL;
2092 
2093  giraffe_table_delete(grating);
2094  grating = NULL;
2095 
2096  giraffe_table_delete(fibers);
2097  fibers = NULL;
2098 
2099  return 1;
2100  }
2101 
2102  cpl_frameset_insert(set, rimg_frame);
2103  }
2104 
2105  }
2106  else {
2107 
2108  /* Data Cube (ESO 3D format) */
2109 
2110  GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes_eso3d;
2111 
2112  properties = giraffe_image_get_properties(rebinning->spectra);
2113  properties = cpl_propertylist_duplicate(properties);
2114 
2115  giraffe_add_frameset_info(properties, set, info.sequence);
2116 
2117  rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE,
2118  CPL_FRAME_LEVEL_FINAL,
2119  properties,
2120  fov,
2121  NULL,
2122  creator);
2123 
2124  cpl_propertylist_delete(properties);
2125  properties = NULL;
2126 
2127  if (rimg_frame == NULL) {
2128  cpl_msg_error(_id, "Cannot create local file! Aborting ...");
2129 
2130  giraffe_fov_delete(fov);
2131  fov = NULL;
2132 
2133  giraffe_rebinning_destroy(rebinning);
2134  rebinning = NULL;
2135 
2136  giraffe_table_delete(wcalcoeff);
2137  wcalcoeff = NULL;
2138 
2139  giraffe_table_delete(slitgeometry);
2140  slitgeometry = NULL;
2141 
2142  giraffe_table_delete(grating);
2143  grating = NULL;
2144 
2145  giraffe_table_delete(fibers);
2146  fibers = NULL;
2147 
2148  return 1;
2149  }
2150 
2151  status = giraffe_fiberlist_attach(rimg_frame, fibers);
2152 
2153  if (status != 0) {
2154  cpl_msg_error(_id, "Cannot attach fiber setup to local "
2155  "file '%s'! Aborting ...",
2156  cpl_frame_get_filename(rimg_frame));
2157 
2158  cpl_frame_delete(rimg_frame);
2159 
2160  giraffe_fov_delete(fov);
2161  fov = NULL;
2162 
2163  giraffe_rebinning_destroy(rebinning);
2164  rebinning = NULL;
2165 
2166  giraffe_table_delete(wcalcoeff);
2167  wcalcoeff = NULL;
2168 
2169  giraffe_table_delete(slitgeometry);
2170  slitgeometry = NULL;
2171 
2172  giraffe_table_delete(grating);
2173  grating = NULL;
2174 
2175  giraffe_table_delete(fibers);
2176  fibers = NULL;
2177 
2178  return 1;
2179  }
2180 
2181  cpl_frameset_insert(set, rimg_frame);
2182 
2183  }
2184 
2185  giraffe_fov_delete(fov);
2186  fov = NULL;
2187 
2188  }
2189 
2190 
2191  /*
2192  * Cleanup
2193  */
2194 
2195  giraffe_table_delete(wcalcoeff);
2196 
2197  giraffe_table_delete(slitgeometry);
2198  giraffe_table_delete(grating);
2199  giraffe_table_delete(fibers);
2200 
2201  giraffe_rebinning_destroy(rebinning);
2202 
2203  return 0;
2204 
2205 }
2206 
2207 
2208 /*
2209  * Build table of contents, i.e. the list of available plugins, for
2210  * this module. This function is exported.
2211  */
2212 
2213 int
2214 cpl_plugin_get_info(cpl_pluginlist* list)
2215 {
2216 
2217  cpl_recipe* recipe = cx_calloc(1, sizeof *recipe);
2218  cpl_plugin* plugin = &recipe->interface;
2219 
2220 
2221  cpl_plugin_init(plugin,
2222  CPL_PLUGIN_API,
2223  GIRAFFE_BINARY_VERSION,
2224  CPL_PLUGIN_TYPE_RECIPE,
2225  "giscience",
2226  "Process a science observation.",
2227  "For detailed information please refer to the "
2228  "GIRAFFE pipeline user manual.\nIt is available at "
2229  "http://www.eso.org/pipelines.",
2230  "Giraffe Pipeline",
2231  PACKAGE_BUGREPORT,
2233  giscience_create,
2234  giscience_exec,
2235  giscience_destroy);
2236 
2237  cpl_pluginlist_append(list, plugin);
2238 
2239  return 0;
2240 
2241 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.12.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Mar 24 2014 11:43:53 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2004