GIRAFFE Pipeline Reference Manual

giscience.c

00001 /* $Id: giscience.c,v 1.35.2.1 2008/06/09 15:08:48 rpalsa Exp $
00002  *
00003  * This file is part of the GIRAFFE Pipeline
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: rpalsa $
00023  * $Date: 2008/06/09 15:08:48 $
00024  * $Revision: 1.35.2.1 $
00025  * $Name: giraffe-2_5_3 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <cxslist.h>
00033 
00034 #include <cpl_recipe.h>
00035 #include <cpl_plugininfo.h>
00036 #include <cpl_parameterlist.h>
00037 #include <cpl_frameset.h>
00038 #include <cpl_msg.h>
00039 
00040 #include "gialias.h"
00041 #include "giframe.h"
00042 #include "gifibers.h"
00043 #include "gifiberutils.h"
00044 #include "gislitgeometry.h"
00045 #include "gipsfdata.h"
00046 #include "gibias.h"
00047 #include "gidark.h"
00048 #include "giextract.h"
00049 #include "giflat.h"
00050 #include "gitransmission.h"
00051 #include "girebinning.h"
00052 #include "gisgcalibration.h"
00053 #include "gireconstruct.h"
00054 #include "gimessages.h"
00055 #include "gierror.h"
00056 #include "giutils.h"
00057 
00058 
00059 static cxint giscience(cpl_parameterlist*, cpl_frameset*);
00060 
00061 
00062 /*
00063  * Create the recipe instance, i.e. setup the parameter list for this
00064  * recipe and make it availble to the application using the interface.
00065  */
00066 
00067 static cxint
00068 giscience_create(cpl_plugin* plugin)
00069 {
00070 
00071     cpl_recipe* recipe = (cpl_recipe*)plugin;
00072 
00073     cpl_parameter* p = NULL;
00074 
00075 
00076     giraffe_error_init();
00077 
00078 
00079     /*
00080      * We have to provide the option we accept to the application. We
00081      * need to setup our parameter list and hook it into the recipe
00082      * interface.
00083      */
00084 
00085     recipe->parameters = cpl_parameterlist_new();
00086     cx_assert(recipe->parameters != NULL);
00087 
00088 
00089     /*
00090      * Fill the parameter list.
00091      */
00092 
00093     /* Bias removal */
00094 
00095     giraffe_bias_config_add(recipe->parameters);
00096 
00097     /* Dark subtraction */
00098 
00099     /* TBD */
00100 
00101     /* Spectrum extraction */
00102 
00103     giraffe_extract_config_add(recipe->parameters);
00104 
00105     /* Flat fielding and relative fiber transmission correction */
00106 
00107     giraffe_flat_config_add(recipe->parameters);
00108 
00109     /* Spectrum rebinning */
00110 
00111     giraffe_rebin_config_add(recipe->parameters);
00112 
00113     /* Simultaneous wavelength calibration correction */
00114 
00115     p = cpl_parameter_new_value("giraffe.siwc.apply",
00116                                 CPL_TYPE_BOOL,
00117                                 "Enable simultaneous wavelength calibration "
00118                                 "correction.",
00119                                 "giraffe.siwc",
00120                                 TRUE);
00121 
00122     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "siwc-apply");
00123     cpl_parameterlist_append(recipe->parameters, p);
00124 
00125     giraffe_sgcalibration_config_add(recipe->parameters);
00126 
00127     /* Image reconstruction (IFU and Argus only) */
00128 
00129     giraffe_reconstruct_config_add(recipe->parameters);
00130 
00131     return 0;
00132 
00133 }
00134 
00135 
00136 /*
00137  * Execute the plugin instance given by the interface.
00138  */
00139 
00140 static cxint
00141 giscience_exec(cpl_plugin* plugin)
00142 {
00143 
00144     cpl_recipe* recipe = (cpl_recipe*)plugin;
00145 
00146 
00147     cx_assert(recipe->parameters != NULL);
00148     cx_assert(recipe->frames != NULL);
00149 
00150     return giscience(recipe->parameters, recipe->frames);
00151 
00152 }
00153 
00154 
00155 static cxint
00156 giscience_destroy(cpl_plugin* plugin)
00157 {
00158 
00159     cpl_recipe* recipe = (cpl_recipe*)plugin;
00160 
00161 
00162     /*
00163      * We just destroy what was created during the plugin initialization
00164      * phase, i.e. the parameter list. The frame set is managed by the
00165      * application which called us, so we must not touch it,
00166      */
00167 
00168     cpl_parameterlist_delete(recipe->parameters);
00169 
00170     giraffe_error_clear();
00171 
00172     return 0;
00173 
00174 }
00175 
00176 
00177 /*
00178  * The actual recipe starts here.
00179  */
00180 
00181 static cxint
00182 giscience(cpl_parameterlist* config, cpl_frameset* set)
00183 {
00184 
00185     const cxchar* const _id = "giscience";
00186 
00187 
00188     const cxchar* filename = NULL;
00189 
00190     cxbool siwc = FALSE;
00191     cxbool calsim = FALSE;
00192 
00193     cxint status = 0;
00194 
00195     cxlong i;
00196     cxlong nscience = 0;
00197 
00198     cxdouble exptime = 0.;
00199 
00200     cx_slist* slist = NULL;
00201 
00202     cpl_propertylist* properties = NULL;
00203 
00204     cpl_matrix* biasareas = NULL;
00205 
00206     cpl_frame* science_frame = NULL;
00207     cpl_frame* mbias_frame = NULL;
00208     cpl_frame* mdark_frame = NULL;
00209     cpl_frame* bpixel_frame = NULL;
00210     cpl_frame* slight_frame = NULL;
00211     cpl_frame* locy_frame = NULL;
00212     cpl_frame* locw_frame = NULL;
00213     cpl_frame* psfdata_frame = NULL;
00214     cpl_frame* grating_frame = NULL;
00215     cpl_frame* linemask_frame = NULL;
00216     cpl_frame* slit_frame = NULL;
00217     cpl_frame* wcal_frame = NULL;
00218     cpl_frame* rscience_frame = NULL;
00219     cpl_frame* sext_frame = NULL;
00220     cpl_frame* rbin_frame = NULL;
00221 
00222     cpl_parameter* p = NULL;
00223 
00224     GiImage* mbias = NULL;
00225     GiImage* mdark = NULL;
00226     GiImage* bpixel = NULL;
00227     GiImage* slight = NULL;
00228     GiImage* sscience = NULL;
00229     GiImage* rscience = NULL;
00230 
00231     GiTable* fibers = NULL;
00232     GiTable* slitgeometry = NULL;
00233     GiTable* grating = NULL;
00234     GiTable* wcalcoeff = NULL;
00235 
00236     GiLocalization* localization = NULL;
00237     GiExtraction* extraction = NULL;
00238     GiRebinning* rebinning = NULL;
00239 
00240     GiBiasConfig* bias_config = NULL;
00241     GiExtractConfig* extract_config = NULL;
00242     GiFlatConfig* flat_config = NULL;
00243     GiRebinConfig* rebin_config = NULL;
00244 
00245     GiInstrumentMode mode;
00246 
00247     GiRecipeInfo info = {(cxchar*)_id, 1, NULL};
00248 
00249     GiGroupInfo groups[] = {
00250         {GIFRAME_SCIENCE, CPL_FRAME_GROUP_RAW},
00251         {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
00252         {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
00253         {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
00254         {GIFRAME_FIBER_FLAT_EXTSPECTRA, CPL_FRAME_GROUP_CALIB},
00255         {GIFRAME_FIBER_FLAT_EXTERRORS, CPL_FRAME_GROUP_CALIB},
00256         {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
00257         {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
00258         {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
00259         {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
00260         {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
00261         {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
00262         {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
00263         {GIFRAME_LINE_MASK, CPL_FRAME_GROUP_CALIB},
00264         {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
00265         {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
00266         {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
00267         {NULL, CPL_FRAME_GROUP_NONE}
00268     };
00269 
00270 
00271 
00272     if (!config) {
00273         cpl_msg_error(_id, "Invalid parameter list! Aborting ...");
00274         return 1;
00275     }
00276 
00277     if (!set) {
00278         cpl_msg_error(_id, "Invalid frame set! Aborting ...");
00279         return 1;
00280     }
00281 
00282     status = giraffe_frameset_set_groups(set, groups);
00283 
00284     if (status != 0) {
00285         cpl_msg_error(_id, "Setting frame group information failed!");
00286         return 1;
00287     }
00288 
00289 
00290     /*
00291      * Verify the frame set contents
00292      */
00293 
00294     nscience = cpl_frameset_count_tags(set, GIFRAME_SCIENCE);
00295 
00296     if (nscience < 1) {
00297         cpl_msg_error(_id, "Too few (%ld) raw frames (%s) present in "
00298                       "frame set! Aborting ...", nscience, GIFRAME_SCIENCE);
00299         return 1;
00300     }
00301 
00302     locy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
00303 
00304     if (locy_frame == NULL) {
00305 
00306         locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
00307 
00308         if (locy_frame == NULL) {
00309             cpl_msg_info(_id, "No master localization (centroid position) "
00310                          "present in frame set. Aborting ...");
00311             return 1;
00312         }
00313 
00314     }
00315 
00316     locw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
00317 
00318     if (locw_frame == NULL) {
00319 
00320         locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
00321 
00322         if (locw_frame == NULL) {
00323             cpl_msg_info(_id, "No master localization (spectrum width) "
00324                          "present in frame set. Aborting ...");
00325             return 1;
00326         }
00327 
00328     }
00329 
00330     grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
00331 
00332     if (!grating_frame) {
00333         cpl_msg_error(_id, "No grating data present in frame set. "
00334                       "Aborting ...");
00335         return 1;
00336     }
00337 
00338     slit_frame = giraffe_get_slitgeometry(set);
00339 
00340     if (!slit_frame) {
00341         cpl_msg_error(_id, "No slit geometry present in frame set. "
00342                       "Aborting ...");
00343         return 1;
00344     }
00345 
00346     wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
00347 
00348     if (!wcal_frame) {
00349         cpl_msg_error(_id, "No dispersion solution present in frame set. "
00350                       "Aborting ...");
00351         return 1;
00352     }
00353 
00354     linemask_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
00355 
00356     if (!linemask_frame) {
00357         cpl_msg_error(_id, "No reference line mask present in frame set.");
00358     }
00359 
00360     bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
00361 
00362     if (!bpixel_frame) {
00363         cpl_msg_info(_id, "No bad pixel map present in frame set.");
00364     }
00365 
00366     mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
00367 
00368     if (!mbias_frame) {
00369         cpl_msg_info(_id, "No master bias present in frame set.");
00370     }
00371 
00372     mdark_frame = cpl_frameset_find(set, GIFRAME_DARK_MASTER);
00373 
00374     if (!mdark_frame) {
00375         cpl_msg_info(_id, "No master dark present in frame set.");
00376     }
00377 
00378     slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
00379 
00380     if (!slight_frame) {
00381         cpl_msg_info(_id, "No scattered light model present in frame set.");
00382     }
00383 
00384     psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
00385 
00386     if (!psfdata_frame) {
00387         cpl_msg_info(_id, "No PSF profile parameters present in frame set.");
00388     }
00389 
00390     linemask_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
00391 
00392     if (!linemask_frame) {
00393         cpl_msg_error(_id, "No reference line mask present in frame set.");
00394     }
00395 
00396 
00397     /*
00398      * Load raw images
00399      */
00400 
00401     slist = cx_slist_new();
00402 
00403     science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
00404 
00405     for (i = 0; i < nscience; i++) {
00406 
00407         filename = cpl_frame_get_filename(science_frame);
00408 
00409         GiImage* raw = giraffe_image_new(CPL_TYPE_DOUBLE);
00410 
00411 
00412         status = giraffe_image_load(raw, filename, 0);
00413 
00414         if (status) {
00415             cpl_msg_error(_id, "Cannot load raw science frame from '%s'. "
00416                           "Aborting ...", filename);
00417 
00418             cx_slist_destroy(slist, (cx_free_func) giraffe_image_delete);
00419 
00420             return 1;
00421         }
00422 
00423         cx_slist_push_back(slist, raw);
00424 
00425         science_frame = cpl_frameset_find(set, NULL);
00426 
00427     }
00428 
00429     nscience = (cxint)cx_slist_size(slist);
00430     sscience = cx_slist_pop_front(slist);
00431 
00432     properties = giraffe_image_get_properties(sscience);
00433     cx_assert(properties != NULL);
00434 
00435     if (nscience > 1) {
00436 
00437         /*
00438          * Create a stacked science image from the list of raw images.
00439          * Each raw image is disposed when it is no longer needed.
00440          */
00441 
00442         cpl_msg_info(_id, "Averaging science frames ...");
00443 
00444         exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
00445 
00446         for (i = 1; i < nscience; i++) {
00447 
00448             cpl_propertylist* _properties;
00449 
00450             GiImage* science = cx_slist_pop_front(slist);
00451 
00452 
00453             cpl_image_add(giraffe_image_get(sscience),
00454                           giraffe_image_get(science));
00455 
00456             _properties = giraffe_image_get_properties(science);
00457             cx_assert(_properties != NULL);
00458 
00459             exptime += cpl_propertylist_get_double(_properties, GIALIAS_EXPTIME);
00460 
00461             giraffe_image_delete(science);
00462 
00463         }
00464 
00465         cpl_image_divide_scalar(giraffe_image_get(sscience), nscience);
00466     }
00467 
00468     cx_assert(cx_slist_empty(slist));
00469     cx_slist_delete(slist);
00470     slist = NULL;
00471 
00472 
00473     if (nscience > 1) {
00474 
00475         /*
00476          * Update stacked science image properties
00477          */
00478 
00479         cpl_msg_info(_id, "Updating stacked science image properties ...");
00480 
00481         cpl_propertylist_set_double(properties, GIALIAS_EXPTIME,
00482                                     exptime / nscience);
00483 
00484         cpl_propertylist_append_double(properties, GIALIAS_EXPTTOT, exptime);
00485         cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
00486                                      "Total exposure time of all frames "
00487                                      "combined");
00488 
00489         cpl_propertylist_append_int(properties, GIALIAS_DATANCOM, nscience);
00490         cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM,
00491                                      "Number of frames combined");
00492 
00493         cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
00494 
00495     }
00496 
00497 
00498     /*
00499      * Prepare for bias subtraction
00500      */
00501 
00502     bias_config = giraffe_bias_config_create(config);
00503 
00504     /*
00505      * Setup user defined areas to use for the bias computation
00506      */
00507 
00508     if (strcmp(bias_config->areas, "None")) {
00509 
00510         cpl_msg_warning(_id, "User defined bias areas are not yet "
00511                         "supported. Using image pre- and overscan areas!");
00512 
00513         biasareas = NULL;
00514 
00515     }
00516 
00517 
00518     if (bias_config->method == GIBIAS_METHOD_MASTER ||
00519         bias_config->method == GIBIAS_METHOD_ZMASTER) {
00520 
00521         if (!mbias_frame) {
00522             cpl_msg_error(_id, "Missing master bias frame! Selected bias "
00523                           "removal method requires a master bias frame!");
00524 
00525             if (biasareas) {
00526                 cpl_matrix_delete(biasareas);
00527                 biasareas = NULL;
00528             }
00529 
00530             giraffe_bias_config_destroy(bias_config);
00531             giraffe_image_delete(sscience);
00532 
00533             return 1;
00534         }
00535         else {
00536             filename = cpl_frame_get_filename(mbias_frame);
00537 
00538 
00539             mbias = giraffe_image_new(CPL_TYPE_DOUBLE);
00540             status = giraffe_image_load(mbias, filename, 0);
00541 
00542             if (status) {
00543                 cpl_msg_error(_id, "Cannot load master bias from '%s'. "
00544                               "Aborting ...", filename);
00545 
00546                 if (biasareas) {
00547                     cpl_matrix_delete(biasareas);
00548                     biasareas = NULL;
00549                 }
00550 
00551                 giraffe_bias_config_destroy(bias_config);
00552                 giraffe_image_delete(sscience);
00553 
00554                 return 1;
00555             }
00556         }
00557     }
00558 
00559 
00560     /*
00561      * Load bad pixel map if it is present in the frame set.
00562      */
00563 
00564     if (bpixel_frame) {
00565 
00566         filename = cpl_frame_get_filename(bpixel_frame);
00567 
00568 
00569         bpixel = giraffe_image_new(CPL_TYPE_INT);
00570         status = giraffe_image_load(bpixel, filename, 0);
00571 
00572         if (status) {
00573             cpl_msg_error(_id, "Cannot load bad pixel map from '%s'. "
00574                           "Aborting ...", filename);
00575 
00576             giraffe_image_delete(bpixel);
00577             bpixel = NULL;
00578 
00579             if (biasareas) {
00580                 cpl_matrix_delete(biasareas);
00581                 biasareas = NULL;
00582             }
00583 
00584             if (mbias != NULL) {
00585                 giraffe_image_delete(mbias);
00586                 mbias = NULL;
00587             }
00588 
00589             giraffe_bias_config_destroy(bias_config);
00590             bias_config = NULL;
00591 
00592             giraffe_image_delete(sscience);
00593             sscience = NULL;
00594 
00595             return 1;
00596         }
00597 
00598     }
00599 
00600 
00601     /*
00602      * Compute and remove the bias from the stacked flat field frame.
00603      */
00604 
00605     rscience = giraffe_image_new(CPL_TYPE_DOUBLE);
00606 
00607     status = giraffe_bias_remove(rscience, sscience, mbias, bpixel, biasareas,
00608                                  bias_config);
00609 
00610     giraffe_image_delete(sscience);
00611 
00612     if (mbias) {
00613         giraffe_image_delete(mbias);
00614         mbias = NULL;
00615     }
00616 
00617     if (biasareas != NULL) {
00618         cpl_matrix_delete(biasareas);
00619         biasareas = NULL;
00620     }
00621 
00622     giraffe_bias_config_destroy(bias_config);
00623 
00624     if (status) {
00625         cpl_msg_error(_id, "Bias removal failed. Aborting ...");
00626 
00627         giraffe_image_delete(rscience);
00628         rscience = NULL;
00629 
00630         if (bpixel != NULL) {
00631             giraffe_image_delete(bpixel);
00632             bpixel = NULL;
00633         }
00634 
00635         return 1;
00636     }
00637 
00638 
00639     /*
00640      * Load master dark if it is present in the frame set and correct
00641      * the master flat field for the dark current.
00642      */
00643 
00644     if (mdark_frame) {
00645 
00646         GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
00647 
00648 
00649         cpl_msg_info(_id, "Correcting for dark current ...");
00650 
00651         filename = cpl_frame_get_filename(mdark_frame);
00652 
00653         mdark = giraffe_image_new(CPL_TYPE_DOUBLE);
00654         status = giraffe_image_load(mdark, filename, 0);
00655 
00656         if (status != 0) {
00657             cpl_msg_error(_id, "Cannot load master dark from '%s'. "
00658                           "Aborting ...", filename);
00659 
00660             giraffe_image_delete(rscience);
00661             rscience = NULL;
00662 
00663             if (bpixel != NULL) {
00664                 giraffe_image_delete(bpixel);
00665                 bpixel = NULL;
00666             }
00667 
00668             return 1;
00669         }
00670 
00671         status = giraffe_subtract_dark(rscience, mdark, bpixel, NULL,
00672                                        &dark_config);
00673 
00674         if (status != 0) {
00675             cpl_msg_error(_id, "Dark subtraction failed! Aborting ...");
00676 
00677             giraffe_image_delete(mdark);
00678             mdark = NULL;
00679 
00680             giraffe_image_delete(rscience);
00681             rscience = NULL;
00682 
00683             if (bpixel != NULL) {
00684                 giraffe_image_delete(bpixel);
00685                 bpixel = NULL;
00686             }
00687 
00688             return 1;
00689         }
00690 
00691         giraffe_image_delete(mdark);
00692         mdark = NULL;
00693 
00694     }
00695 
00696 
00697     /*
00698      * Update the reduced science properties, save the reduced science frame
00699      * and register it as product.
00700      */
00701 
00702     cpl_msg_info(_id, "Writing pre-processed science image ...");
00703 
00704     giraffe_image_add_info(rscience, &info, set);
00705 
00706     rscience_frame = giraffe_frame_create_image(rscience,
00707                                              GIFRAME_SCIENCE_REDUCED,
00708                                              CPL_FRAME_LEVEL_INTERMEDIATE,
00709                                              TRUE, TRUE);
00710 
00711     if (rscience_frame == NULL) {
00712         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
00713 
00714         giraffe_image_delete(rscience);
00715 
00716         return 1;
00717     }
00718 
00719     cpl_frameset_insert(set, rscience_frame);
00720 
00721 
00722     /*
00723      * Determine fiber setup
00724      */
00725 
00726     science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
00727 
00728     cpl_msg_info(_id, "Building fiber setup for frame '%s'.",
00729                  cpl_frame_get_filename(science_frame));
00730 
00731     fibers = giraffe_fibers_setup(science_frame, locy_frame);
00732 
00733     if (!fibers) {
00734         cpl_msg_error(_id, "Cannot create fiber setup for frame '%s'! "
00735                       "Aborting ...", cpl_frame_get_filename(science_frame));
00736 
00737         if (bpixel) {
00738             giraffe_image_delete(bpixel);
00739             bpixel = NULL;
00740         }
00741 
00742         giraffe_image_delete(rscience);
00743         rscience = NULL;
00744 
00745         return 1;
00746     }
00747 
00748     cpl_msg_info(_id, "Fiber reference setup taken from localization "
00749                  "frame '%s'.", cpl_frame_get_filename(locy_frame));
00750 
00751 
00752     /*
00753      * Load fiber localization
00754      */
00755 
00756     localization = giraffe_localization_new();
00757 
00758     filename = cpl_frame_get_filename(locy_frame);
00759     status = 0;
00760 
00761     localization->locy  = giraffe_image_new(CPL_TYPE_DOUBLE);
00762     status = giraffe_image_load(localization->locy, filename, 0);
00763 
00764     if (status) {
00765         cpl_msg_error(_id, "Cannot load localization (centroid "
00766                       "position) frame from '%s'. Aborting ...",
00767                       filename);
00768 
00769         giraffe_localization_destroy(localization);
00770 
00771         if (bpixel) {
00772             giraffe_image_delete(bpixel);
00773             bpixel = NULL;
00774         }
00775 
00776         giraffe_table_delete(fibers);
00777         giraffe_image_delete(rscience);
00778 
00779         return 1;
00780     }
00781 
00782 
00783     filename = cpl_frame_get_filename(locw_frame);
00784     status = 0;
00785 
00786     localization->locw  = giraffe_image_new(CPL_TYPE_DOUBLE);
00787     status = giraffe_image_load(localization->locw, filename, 0);
00788 
00789     if (status) {
00790         cpl_msg_error(_id, "Cannot load localization (spectrum width) "
00791                       "frame from '%s'. Aborting ...", filename);
00792 
00793         giraffe_localization_destroy(localization);
00794 
00795         if (bpixel) {
00796             giraffe_image_delete(bpixel);
00797             bpixel = NULL;
00798         }
00799 
00800         giraffe_table_delete(fibers);
00801         giraffe_image_delete(rscience);
00802 
00803         return 1;
00804     }
00805 
00806 
00807     /*
00808      * Spectrum extraction
00809      */
00810 
00811     if (slight_frame) {
00812 
00813         filename = cpl_frame_get_filename(slight_frame);
00814 
00815 
00816         slight = giraffe_image_new(CPL_TYPE_DOUBLE);
00817         status = giraffe_image_load(slight, filename, 0);
00818 
00819         if (status) {
00820             cpl_msg_error(_id, "Cannot load scattered light model from '%s'. "
00821                           "Aborting ...", filename);
00822 
00823             giraffe_image_delete(slight);
00824 
00825             giraffe_localization_destroy(localization);
00826 
00827             if (bpixel) {
00828                 giraffe_image_delete(bpixel);
00829                 bpixel = NULL;
00830             }
00831 
00832             giraffe_table_delete(fibers);
00833             giraffe_image_delete(rscience);
00834 
00835             return 1;
00836 
00837         }
00838 
00839     }
00840 
00841 
00842     extract_config = giraffe_extract_config_create(config);
00843 
00844     if ((extract_config->emethod == GIEXTRACT_OPTIMAL) ||
00845         (extract_config->emethod == GIEXTRACT_HORNE)) {
00846 
00847         if (psfdata_frame == NULL) {
00848 
00849             const cxchar* emethod = "Optimal";
00850 
00851             if (extract_config->emethod == GIEXTRACT_HORNE) {
00852                 emethod = "Horne";
00853             }
00854 
00855             cpl_msg_error(_id, "%s spectrum extraction requires PSF "
00856                           "profile data. Aborting ...", emethod);
00857 
00858             giraffe_extract_config_destroy(extract_config);
00859             extract_config = NULL;
00860 
00861             if (slight != NULL) {
00862                 giraffe_image_delete(slight);
00863                 slight = NULL;
00864             }
00865 
00866             giraffe_localization_destroy(localization);
00867             localization = NULL;
00868 
00869             if (bpixel) {
00870                 giraffe_image_delete(bpixel);
00871                 bpixel = NULL;
00872             }
00873 
00874             giraffe_table_delete(fibers);
00875             fibers = NULL;
00876 
00877             giraffe_image_delete(rscience);
00878             rscience = NULL;
00879 
00880             return 1;
00881 
00882         }
00883         else {
00884 
00885             filename = cpl_frame_get_filename(psfdata_frame);
00886             status = 0;
00887 
00888             localization->psf  = giraffe_psfdata_new();
00889             status = giraffe_psfdata_load(localization->psf, filename);
00890 
00891             if (status) {
00892                 cpl_msg_error(_id, "Cannot load PSF profile data frame from "
00893                               "'%s'. Aborting ...", filename);
00894 
00895                 giraffe_extract_config_destroy(extract_config);
00896                 extract_config = NULL;
00897 
00898                 if (slight != NULL) {
00899                     giraffe_image_delete(slight);
00900                     slight = NULL;
00901                 }
00902 
00903                 giraffe_localization_destroy(localization);
00904                 localization = NULL;
00905 
00906                 if (bpixel) {
00907                     giraffe_image_delete(bpixel);
00908                     bpixel = NULL;
00909                 }
00910 
00911                 giraffe_table_delete(fibers);
00912                 fibers = NULL;
00913 
00914                 giraffe_image_delete(rscience);
00915                 rscience = NULL;
00916 
00917                 return 1;
00918 
00919             }
00920 
00921         }
00922 
00923     }
00924 
00925 
00926     extraction = giraffe_extraction_new();
00927 
00928     status = giraffe_extract_spectra(extraction, rscience, fibers,
00929                                      localization, bpixel, slight,
00930                                      extract_config);
00931 
00932     if (status) {
00933         cpl_msg_error(_id, "Spectrum extraction failed! Aborting ...");
00934 
00935         giraffe_extraction_destroy(extraction);
00936         giraffe_extract_config_destroy(extract_config);
00937 
00938         giraffe_image_delete(slight);
00939 
00940         giraffe_localization_destroy(localization);
00941 
00942         if (bpixel) {
00943             giraffe_image_delete(bpixel);
00944             bpixel = NULL;
00945         }
00946 
00947         giraffe_table_delete(fibers);
00948         giraffe_image_delete(rscience);
00949 
00950         return 1;
00951     }
00952 
00953     giraffe_image_delete(slight);
00954     slight = NULL;
00955 
00956     if (bpixel) {
00957         giraffe_image_delete(bpixel);
00958         bpixel = NULL;
00959     }
00960 
00961     giraffe_image_delete(rscience);
00962     rscience = NULL;
00963 
00964     giraffe_extract_config_destroy(extract_config);
00965 
00966 
00967     /*
00968      * Apply flat field and apply the relative fiber transmission correction.
00969      */
00970 
00971     flat_config = giraffe_flat_config_create(config);
00972 
00973     if (flat_config->load == TRUE) {
00974 
00975         cpl_frame* flat_frame = NULL;
00976 
00977         GiImage* flat = NULL;
00978 
00979 
00980         flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTSPECTRA);
00981 
00982         if (flat_frame == NULL) {
00983             cpl_msg_error(_id, "Missing flat field spectra frame!");
00984 
00985             giraffe_flat_config_destroy(flat_config);
00986 
00987             giraffe_extraction_destroy(extraction);
00988             giraffe_localization_destroy(localization);
00989 
00990             giraffe_table_delete(wcalcoeff);
00991 
00992             giraffe_table_delete(grating);
00993             giraffe_table_delete(fibers);
00994 
00995             return 1;
00996         }
00997 
00998         filename = cpl_frame_get_filename(flat_frame);
00999 
01000         flat = giraffe_image_new(CPL_TYPE_DOUBLE);
01001         status = giraffe_image_load(flat, filename, 0);
01002 
01003         if (status) {
01004             cpl_msg_error(_id, "Cannot load flat field spectra from '%s'. "
01005                           "Aborting ...", filename);
01006 
01007             giraffe_image_delete(flat);
01008 
01009             giraffe_flat_config_destroy(flat_config);
01010 
01011             giraffe_extraction_destroy(extraction);
01012             giraffe_localization_destroy(localization);
01013 
01014             giraffe_table_delete(wcalcoeff);
01015 
01016             giraffe_table_delete(grating);
01017             giraffe_table_delete(fibers);
01018 
01019             return 1;
01020         }
01021 
01022         if (flat_config->apply == TRUE) {
01023 
01024             GiImage* errors = NULL;
01025 
01026 
01027             flat_frame = cpl_frameset_find(set, GIFRAME_FIBER_FLAT_EXTERRORS);
01028 
01029             if (flat_frame == NULL) {
01030                 cpl_msg_warning(_id, "Missing flat field spectra errors "
01031                                 "frame!");
01032             }
01033             else {
01034 
01035                 filename = cpl_frame_get_filename(flat_frame);
01036 
01037                 errors = giraffe_image_new(CPL_TYPE_DOUBLE);
01038                 status = giraffe_image_load(errors, filename, 0);
01039 
01040                 if (status) {
01041                     cpl_msg_error(_id, "Cannot load flat field spectra "
01042                                   "errors from '%s'. Aborting ...",
01043                                   filename);
01044 
01045                     giraffe_image_delete(errors);
01046                     giraffe_image_delete(flat);
01047 
01048                     giraffe_flat_config_destroy(flat_config);
01049 
01050                     giraffe_extraction_destroy(extraction);
01051                     giraffe_localization_destroy(localization);
01052 
01053                     giraffe_table_delete(wcalcoeff);
01054 
01055                     giraffe_table_delete(grating);
01056                     giraffe_table_delete(fibers);
01057 
01058                     return 1;
01059                 }
01060 
01061             }
01062 
01063             cpl_msg_info(_id, "Applying flat field correction ...");
01064 
01065             status = giraffe_flat_apply(extraction, fibers, flat, errors,
01066                                         flat_config);
01067 
01068             if (status) {
01069                 cpl_msg_error(_id, "Flat field correction failed! "
01070                               "Aborting ...");
01071 
01072                 giraffe_image_delete(errors);
01073                 giraffe_image_delete(flat);
01074 
01075                 giraffe_flat_config_destroy(flat_config);
01076 
01077                 giraffe_extraction_destroy(extraction);
01078                 giraffe_localization_destroy(localization);
01079 
01080                 giraffe_table_delete(wcalcoeff);
01081 
01082                 giraffe_table_delete(grating);
01083                 giraffe_table_delete(fibers);
01084 
01085                 return 1;
01086             }
01087 
01088             giraffe_image_delete(errors);
01089             errors = NULL;
01090 
01091         }
01092 
01093         if (flat_config->transmission == TRUE) {
01094 
01095             const cxchar* _filename = cpl_frame_get_filename(flat_frame);
01096 
01097             GiTable* _fibers = NULL;
01098 
01099 
01100             cpl_msg_info(_id, "Loading fiber setup for frame '%s'.",
01101                          _filename);
01102 
01103             _fibers = giraffe_fiberlist_load(_filename, 1, "FIBER_SETUP");
01104 
01105             if (!_fibers) {
01106                 cpl_msg_error(_id, "Cannot create fiber setup for "
01107                               "frame '%s'! Aborting ...", _filename);
01108 
01109                 giraffe_image_delete(flat);
01110 
01111                 giraffe_flat_config_destroy(flat_config);
01112 
01113                 giraffe_extraction_destroy(extraction);
01114                 giraffe_localization_destroy(localization);
01115 
01116                 giraffe_table_delete(wcalcoeff);
01117 
01118                 giraffe_table_delete(grating);
01119                 giraffe_table_delete(fibers);
01120 
01121                 return 1;
01122             }
01123 
01124             cpl_msg_info(_id, "Applying relative fiber transmission "
01125                          "correction");
01126 
01127             status = giraffe_transmission_setup(fibers, _fibers);
01128             giraffe_table_delete(_fibers);
01129 
01130             if (status == 0) {
01131                 status = giraffe_transmission_apply(extraction, fibers);
01132             }
01133 
01134             if (status) {
01135 
01136                 cpl_msg_error(_id, "Relative transmission correction failed! "
01137                               "Aborting ...");
01138 
01139                 giraffe_image_delete(flat);
01140 
01141                 giraffe_flat_config_destroy(flat_config);
01142 
01143                 giraffe_extraction_destroy(extraction);
01144                 giraffe_localization_destroy(localization);
01145 
01146                 giraffe_table_delete(wcalcoeff);
01147 
01148                 giraffe_table_delete(grating);
01149                 giraffe_table_delete(fibers);
01150 
01151                 return 1;
01152 
01153             }
01154 
01155         }
01156 
01157         giraffe_image_delete(flat);
01158 
01159     }
01160 
01161     giraffe_flat_config_destroy(flat_config);
01162 
01163 
01164     /*
01165      * Save the spectrum extraction results and register them as
01166      * products.
01167      */
01168 
01169     cpl_msg_info(_id, "Writing extracted spectra ...");
01170 
01171     /* Extracted spectra */
01172 
01173     giraffe_image_add_info(extraction->spectra, &info, set);
01174 
01175     sext_frame = giraffe_frame_create_image(extraction->spectra,
01176                                             GIFRAME_SCIENCE_EXTSPECTRA,
01177                                             CPL_FRAME_LEVEL_FINAL,
01178                                             TRUE, TRUE);
01179 
01180     if (sext_frame == NULL) {
01181         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01182 
01183         giraffe_extraction_destroy(extraction);
01184         giraffe_localization_destroy(localization);
01185 
01186         giraffe_table_delete(wcalcoeff);
01187 
01188         giraffe_table_delete(grating);
01189         giraffe_table_delete(fibers);
01190 
01191         return 1;
01192     }
01193 
01194     status = giraffe_fiberlist_attach(sext_frame, fibers);
01195 
01196     if (status) {
01197         cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
01198                       "Aborting ...", cpl_frame_get_filename(sext_frame));
01199 
01200         cpl_frame_delete(sext_frame);
01201 
01202         giraffe_extraction_destroy(extraction);
01203         giraffe_localization_destroy(localization);
01204 
01205         giraffe_table_delete(wcalcoeff);
01206 
01207         giraffe_table_delete(grating);
01208         giraffe_table_delete(fibers);
01209 
01210         return 1;
01211     }
01212 
01213     cpl_frameset_insert(set, sext_frame);
01214 
01215     /* Extracted spectra errors */
01216 
01217     giraffe_image_add_info(extraction->error, &info, set);
01218 
01219     sext_frame = giraffe_frame_create_image(extraction->error,
01220                                             GIFRAME_SCIENCE_EXTERRORS,
01221                                             CPL_FRAME_LEVEL_FINAL,
01222                                             TRUE, TRUE);
01223 
01224     if (sext_frame == NULL) {
01225         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01226 
01227         giraffe_extraction_destroy(extraction);
01228         giraffe_localization_destroy(localization);
01229 
01230         giraffe_table_delete(wcalcoeff);
01231 
01232         giraffe_table_delete(grating);
01233         giraffe_table_delete(fibers);
01234 
01235         return 1;
01236     }
01237 
01238     status = giraffe_fiberlist_attach(sext_frame, fibers);
01239 
01240     if (status) {
01241         cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
01242                       "Aborting ...", cpl_frame_get_filename(sext_frame));
01243 
01244         cpl_frame_delete(sext_frame);
01245 
01246         giraffe_extraction_destroy(extraction);
01247         giraffe_localization_destroy(localization);
01248 
01249         giraffe_table_delete(wcalcoeff);
01250 
01251         giraffe_table_delete(grating);
01252         giraffe_table_delete(fibers);
01253 
01254         return 1;
01255     }
01256 
01257     cpl_frameset_insert(set, sext_frame);
01258 
01259     /* Extracted spectra pixels */
01260 
01261     if (extraction->npixels != NULL) {
01262 
01263         giraffe_image_add_info(extraction->npixels, &info, set);
01264 
01265         sext_frame = giraffe_frame_create_image(extraction->npixels,
01266                                                 GIFRAME_SCIENCE_EXTPIXELS,
01267                                                 CPL_FRAME_LEVEL_FINAL,
01268                                                 TRUE, TRUE);
01269 
01270         if (sext_frame == NULL) {
01271             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01272 
01273             giraffe_extraction_destroy(extraction);
01274             giraffe_localization_destroy(localization);
01275 
01276             giraffe_table_delete(wcalcoeff);
01277 
01278             giraffe_table_delete(grating);
01279             giraffe_table_delete(fibers);
01280 
01281             return 1;
01282         }
01283 
01284         status = giraffe_fiberlist_attach(sext_frame, fibers);
01285 
01286         if (status) {
01287             cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
01288                         "Aborting ...", cpl_frame_get_filename(sext_frame));
01289 
01290             cpl_frame_delete(sext_frame);
01291 
01292             giraffe_extraction_destroy(extraction);
01293             giraffe_localization_destroy(localization);
01294 
01295             giraffe_table_delete(wcalcoeff);
01296 
01297             giraffe_table_delete(grating);
01298             giraffe_table_delete(fibers);
01299 
01300             return 1;
01301         }
01302 
01303         cpl_frameset_insert(set, sext_frame);
01304 
01305     }
01306 
01307     /* Extracted spectra centroids */
01308 
01309     giraffe_image_add_info(extraction->centroid, &info, set);
01310 
01311     sext_frame = giraffe_frame_create_image(extraction->centroid,
01312                                             GIFRAME_SCIENCE_EXTTRACE,
01313                                             CPL_FRAME_LEVEL_FINAL,
01314                                             TRUE, TRUE);
01315 
01316     if (sext_frame == NULL) {
01317         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01318 
01319         giraffe_extraction_destroy(extraction);
01320         giraffe_localization_destroy(localization);
01321 
01322         giraffe_table_delete(wcalcoeff);
01323 
01324         giraffe_table_delete(grating);
01325         giraffe_table_delete(fibers);
01326 
01327         return 1;
01328     }
01329 
01330     status = giraffe_fiberlist_attach(sext_frame, fibers);
01331 
01332     if (status) {
01333         cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
01334                       "Aborting ...", cpl_frame_get_filename(sext_frame));
01335 
01336         cpl_frame_delete(sext_frame);
01337 
01338         giraffe_extraction_destroy(extraction);
01339         giraffe_localization_destroy(localization);
01340 
01341         giraffe_table_delete(wcalcoeff);
01342 
01343         giraffe_table_delete(grating);
01344         giraffe_table_delete(fibers);
01345 
01346         return 1;
01347     }
01348 
01349     cpl_frameset_insert(set, sext_frame);
01350 
01351     /* Extraction model spectra */
01352 
01353     if (extraction->model != NULL) {
01354 
01355         giraffe_image_add_info(extraction->model, &info, set);
01356 
01357         sext_frame = giraffe_frame_create_image(extraction->model,
01358                                                 GIFRAME_SCIENCE_EXTMODEL,
01359                                                 CPL_FRAME_LEVEL_FINAL,
01360                                                 TRUE, TRUE);
01361 
01362         if (sext_frame == NULL) {
01363             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01364 
01365             giraffe_extraction_destroy(extraction);
01366             giraffe_localization_destroy(localization);
01367 
01368             giraffe_table_delete(wcalcoeff);
01369 
01370             giraffe_table_delete(grating);
01371             giraffe_table_delete(fibers);
01372 
01373             return 1;
01374         }
01375 
01376         status = giraffe_fiberlist_attach(sext_frame, fibers);
01377 
01378         if (status != 0) {
01379             cpl_msg_error(_id, "Cannot attach fiber setup to local file '%s'! "
01380                           "Aborting ...", cpl_frame_get_filename(sext_frame));
01381 
01382             cpl_frame_delete(sext_frame);
01383 
01384             giraffe_extraction_destroy(extraction);
01385             giraffe_localization_destroy(localization);
01386 
01387             giraffe_table_delete(wcalcoeff);
01388 
01389             giraffe_table_delete(grating);
01390             giraffe_table_delete(fibers);
01391 
01392             return 1;
01393         }
01394 
01395         cpl_frameset_insert(set, sext_frame);
01396 
01397     }
01398 
01399 
01400     /*
01401      * Load dispersion solution
01402      */
01403 
01404 
01405     filename = (cxchar *)cpl_frame_get_filename(wcal_frame);
01406 
01407     wcalcoeff = giraffe_table_new();
01408     status = giraffe_table_load(wcalcoeff, filename, 1, NULL);
01409 
01410     if (status) {
01411         cpl_msg_error(_id, "Cannot load dispersion solution from "
01412                       "'%s'. Aborting ...", filename);
01413 
01414         giraffe_extraction_destroy(extraction);
01415         giraffe_localization_destroy(localization);
01416 
01417         giraffe_table_delete(wcalcoeff);
01418 
01419         giraffe_table_delete(grating);
01420         giraffe_table_delete(fibers);
01421 
01422         return 1;
01423     }
01424 
01425 
01426     /*
01427      * Load grating data
01428      */
01429 
01430     filename = (cxchar *)cpl_frame_get_filename(grating_frame);
01431 
01432     status = 0;
01433 
01434     grating = giraffe_table_new();
01435     status = giraffe_table_load(grating, filename, 1, NULL);
01436 
01437     if (status) {
01438         cpl_msg_error(_id, "Cannot load grating data from '%s'. "
01439                       "Aborting ...", filename);
01440 
01441         giraffe_extraction_destroy(extraction);
01442         giraffe_localization_destroy(localization);
01443 
01444         giraffe_table_delete(wcalcoeff);
01445 
01446         giraffe_table_delete(grating);
01447         giraffe_table_delete(fibers);
01448 
01449         return 1;
01450     }
01451 
01452 
01453     /*
01454      * Load slit geometry data
01455      */
01456 
01457 
01458     filename = (cxchar *)cpl_frame_get_filename(slit_frame);
01459 
01460     slitgeometry = giraffe_slitgeometry_load(fibers, filename, 1, NULL);
01461 
01462     if (slitgeometry == NULL) {
01463         cpl_msg_error(_id, "Cannot load slit geometry data from '%s'. "
01464                       "Aborting ...", filename);
01465 
01466         giraffe_table_delete(wcalcoeff);
01467 
01468         giraffe_extraction_destroy(extraction);
01469         giraffe_localization_destroy(localization);
01470 
01471         giraffe_table_delete(wcalcoeff);
01472 
01473         giraffe_table_delete(grating);
01474         giraffe_table_delete(fibers);
01475 
01476         return 1;
01477     }
01478     else {
01479 
01480         /*
01481          * Check whether the contains the positions for all fibers
01482          * provided by the fiber setup. If this is not the case
01483          * this is an error.
01484          */
01485 
01486         if (giraffe_fiberlist_compare(slitgeometry, fibers) != 1) {
01487             cpl_msg_error(_id, "Slit geometry data from '%s' is not "
01488                     "applicable for current fiber setup! "
01489                             "Aborting ...", filename);
01490 
01491             giraffe_table_delete(slitgeometry);
01492             giraffe_table_delete(wcalcoeff);
01493 
01494             giraffe_extraction_destroy(extraction);
01495             giraffe_localization_destroy(localization);
01496 
01497             giraffe_table_delete(wcalcoeff);
01498 
01499             giraffe_table_delete(grating);
01500             giraffe_table_delete(fibers);
01501 
01502             return 1;
01503         }
01504 
01505     }
01506 
01507 
01508 
01509     /*
01510      * Spectrum rebinning
01511      */
01512 
01513     cpl_msg_info(_id, "Spectrum rebinning");
01514 
01515     rebin_config = giraffe_rebin_config_create(config);
01516 
01517     rebinning = giraffe_rebinning_new();
01518 
01519     status = giraffe_rebin_spectra(rebinning, extraction, fibers,
01520                                    localization, grating, slitgeometry,
01521                                    wcalcoeff, rebin_config);
01522 
01523     if (status) {
01524         cpl_msg_error(_id, "Rebinning of science spectra failed! Aborting...");
01525 
01526         giraffe_rebinning_destroy(rebinning);
01527 
01528         giraffe_extraction_destroy(extraction);
01529         giraffe_localization_destroy(localization);
01530 
01531         giraffe_table_delete(wcalcoeff);
01532 
01533         giraffe_table_delete(slitgeometry);
01534         giraffe_table_delete(grating);
01535         giraffe_table_delete(fibers);
01536 
01537         giraffe_rebin_config_destroy(rebin_config);
01538 
01539         return 1;
01540 
01541     }
01542 
01543 
01544     /*
01545      * Optionally compute and apply spectral shifts from the simultaneous
01546      * calibration fibers. This is only done if the simultaneous calibration
01547      * fibers were used.
01548      */
01549 
01550     p = cpl_parameterlist_find(config, "giraffe.siwc.apply");
01551     cx_assert(p != NULL);
01552 
01553     siwc = cpl_parameter_get_bool(p);
01554     p = NULL;
01555 
01556     properties = giraffe_image_get_properties(rebinning->spectra);
01557     cx_assert(properties != NULL);
01558 
01559     if (cpl_propertylist_has(properties, GIALIAS_STSCTAL) == TRUE) {
01560         calsim = cpl_propertylist_get_bool(properties, GIALIAS_STSCTAL);
01561     }
01562 
01563     if ((siwc == TRUE) && (calsim == TRUE) && (linemask_frame != NULL)) {
01564 
01565         GiTable* linemask = giraffe_table_new();
01566 
01567         GiSGCalConfig* siwc_config = NULL;
01568 
01569 
01570         siwc_config = giraffe_sgcalibration_config_create(config);
01571 
01572         if (siwc_config == NULL) {
01573 
01574             giraffe_table_delete(linemask);
01575             linemask = NULL;
01576 
01577             giraffe_rebinning_destroy(rebinning);
01578 
01579             giraffe_extraction_destroy(extraction);
01580             giraffe_localization_destroy(localization);
01581 
01582             giraffe_table_delete(wcalcoeff);
01583 
01584             giraffe_table_delete(slitgeometry);
01585             giraffe_table_delete(grating);
01586             giraffe_table_delete(fibers);
01587 
01588             giraffe_rebin_config_destroy(rebin_config);
01589 
01590             return 1;
01591 
01592         }
01593 
01594         filename = cpl_frame_get_filename(linemask_frame);
01595 
01596         status = giraffe_table_load(linemask, filename, 1, NULL);
01597 
01598         if (status) {
01599             cpl_msg_error(_id, "Cannot load line reference mask from '%s'. "
01600                           "Aborting ...", filename);
01601 
01602             giraffe_sgcalibration_config_destroy(siwc_config);
01603             siwc_config = NULL;
01604 
01605             giraffe_table_delete(linemask);
01606             linemask = NULL;
01607 
01608             giraffe_rebinning_destroy(rebinning);
01609 
01610             giraffe_extraction_destroy(extraction);
01611             giraffe_localization_destroy(localization);
01612 
01613             giraffe_table_delete(wcalcoeff);
01614 
01615             giraffe_table_delete(slitgeometry);
01616             giraffe_table_delete(grating);
01617             giraffe_table_delete(fibers);
01618 
01619             giraffe_rebin_config_destroy(rebin_config);
01620 
01621             return 1;
01622 
01623         }
01624 
01625 
01626         status = giraffe_compute_offsets(fibers, rebinning, grating,
01627                                          linemask, siwc_config);
01628 
01629         if (status != 0) {
01630             cpl_msg_error(_id, "Applying simultaneous wavelength "
01631                           "calibration correction failed! Aborting...");
01632 
01633             giraffe_sgcalibration_config_destroy(siwc_config);
01634             siwc_config = NULL;
01635 
01636             giraffe_table_delete(linemask);
01637             linemask = NULL;
01638 
01639             giraffe_rebinning_destroy(rebinning);
01640 
01641             giraffe_extraction_destroy(extraction);
01642             giraffe_localization_destroy(localization);
01643 
01644             giraffe_table_delete(wcalcoeff);
01645 
01646             giraffe_table_delete(slitgeometry);
01647             giraffe_table_delete(grating);
01648             giraffe_table_delete(fibers);
01649 
01650             giraffe_rebin_config_destroy(rebin_config);
01651 
01652             return 1;
01653 
01654         }
01655 
01656         giraffe_sgcalibration_config_destroy(siwc_config);
01657         siwc_config = NULL;
01658 
01659         giraffe_table_delete(linemask);
01660         linemask = NULL;
01661 
01662         giraffe_rebinning_destroy(rebinning);
01663         rebinning = giraffe_rebinning_new();
01664 
01665         status = giraffe_rebin_spectra(rebinning, extraction, fibers,
01666                                        localization, grating, slitgeometry,
01667                                        wcalcoeff, rebin_config);
01668 
01669         if (status) {
01670             cpl_msg_error(_id, "Rebinning of science spectra failed! "
01671                           "Aborting...");
01672 
01673             giraffe_rebinning_destroy(rebinning);
01674 
01675             giraffe_extraction_destroy(extraction);
01676             giraffe_localization_destroy(localization);
01677 
01678             giraffe_table_delete(wcalcoeff);
01679 
01680             giraffe_table_delete(slitgeometry);
01681             giraffe_table_delete(grating);
01682             giraffe_table_delete(fibers);
01683 
01684             giraffe_rebin_config_destroy(rebin_config);
01685 
01686             return 1;
01687 
01688         }
01689 
01690     }
01691 
01692     giraffe_extraction_destroy(extraction);
01693     extraction = NULL;
01694 
01695     giraffe_localization_destroy(localization);
01696     localization = NULL;
01697 
01698     giraffe_rebin_config_destroy(rebin_config);
01699     rebin_config = NULL;
01700 
01701 
01702     /*
01703      * Save and register the results of the spectrum rebinning.
01704      */
01705 
01706     /* Rebinned spectra */
01707 
01708     giraffe_image_add_info(rebinning->spectra, &info, set);
01709 
01710     rbin_frame = giraffe_frame_create_image(rebinning->spectra,
01711                                             GIFRAME_SCIENCE_RBNSPECTRA,
01712                                             CPL_FRAME_LEVEL_FINAL,
01713                                             TRUE, TRUE);
01714 
01715     if (rbin_frame == NULL) {
01716         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01717 
01718         giraffe_rebinning_destroy(rebinning);
01719 
01720         giraffe_table_delete(wcalcoeff);
01721 
01722         giraffe_table_delete(slitgeometry);
01723         giraffe_table_delete(grating);
01724         giraffe_table_delete(fibers);
01725 
01726         return 1;
01727     }
01728 
01729     status = giraffe_fiberlist_attach(rbin_frame, fibers);
01730 
01731     if (status) {
01732         cpl_msg_error(_id, "Cannot attach fiber setup to local "
01733                       "file '%s'! Aborting ...",
01734                       cpl_frame_get_filename(rbin_frame));
01735 
01736         giraffe_rebinning_destroy(rebinning);
01737         giraffe_table_delete(wcalcoeff);
01738 
01739         giraffe_table_delete(slitgeometry);
01740         giraffe_table_delete(grating);
01741         giraffe_table_delete(fibers);
01742 
01743         cpl_frame_delete(rbin_frame);
01744 
01745         return 1;
01746     }
01747 
01748     cpl_frameset_insert(set, rbin_frame);
01749 
01750     /* Rebinned spectra errors */
01751 
01752     giraffe_image_add_info(rebinning->errors, &info, set);
01753 
01754     rbin_frame = giraffe_frame_create_image(rebinning->errors,
01755                                             GIFRAME_SCIENCE_RBNERRORS,
01756                                             CPL_FRAME_LEVEL_FINAL,
01757                                             TRUE, TRUE);
01758 
01759     if (rbin_frame == NULL) {
01760         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01761 
01762         giraffe_rebinning_destroy(rebinning);
01763 
01764         giraffe_table_delete(wcalcoeff);
01765 
01766         giraffe_table_delete(slitgeometry);
01767         giraffe_table_delete(grating);
01768         giraffe_table_delete(fibers);
01769 
01770         return 1;
01771     }
01772 
01773     status = giraffe_fiberlist_attach(rbin_frame, fibers);
01774 
01775     if (status) {
01776         cpl_msg_error(_id, "Cannot attach fiber setup to local "
01777                       "file '%s'! Aborting ...",
01778                       cpl_frame_get_filename(rbin_frame));
01779 
01780         giraffe_rebinning_destroy(rebinning);
01781 
01782         giraffe_table_delete(wcalcoeff);
01783 
01784         giraffe_table_delete(slitgeometry);
01785         giraffe_table_delete(grating);
01786         giraffe_table_delete(fibers);
01787 
01788         cpl_frame_delete(rbin_frame);
01789 
01790         return 1;
01791     }
01792 
01793     cpl_frameset_insert(set, rbin_frame);
01794 
01795 
01796     /*
01797      * Optional image and data cube construction (only for IFU and Argus)
01798      */
01799 
01800     properties = giraffe_image_get_properties(rebinning->spectra);
01801     mode = giraffe_get_mode(properties);
01802 
01803 
01804     if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
01805 
01806         cpl_frame* rimg_frame = NULL;
01807 
01808         GiReconstruction* reconstruction = NULL;
01809 
01810         GiReconstructConfig* reconstruct_config = NULL;
01811 
01812 
01813         reconstruct_config = giraffe_reconstruct_config_create(config);
01814 
01815         cpl_msg_info(_id, "Reconstructing image and data cube from rebinned "
01816                      "spectra ...");
01817 
01818         reconstruction = giraffe_reconstruction_new();
01819 
01820         status = giraffe_reconstruction_build(reconstruction, rebinning,
01821                                               fibers, wcalcoeff,
01822                                               grating, slitgeometry,
01823                                               reconstruct_config);
01824 
01825         if (status) {
01826 
01827             if (status == -2) {
01828                 cpl_msg_warning(_id, "No reconstructed image was built. "
01829                                 "Fiber list has no fiber position "
01830                                 "information.");
01831             }
01832             else {
01833                 cpl_msg_error(_id, "Image reconstruction failed! Aborting...");
01834 
01835                 giraffe_reconstruction_destroy(reconstruction);
01836                 giraffe_rebinning_destroy(rebinning);
01837 
01838                 giraffe_table_delete(wcalcoeff);
01839 
01840                 giraffe_table_delete(slitgeometry);
01841                 giraffe_table_delete(grating);
01842                 giraffe_table_delete(fibers);
01843 
01844                 giraffe_reconstruct_config_destroy(reconstruct_config);
01845 
01846                 return 1;
01847             }
01848 
01849         }
01850 
01851         giraffe_reconstruct_config_destroy(reconstruct_config);
01852 
01853 
01854         /*
01855          * Save and register the results of the image reconstruction.
01856          */
01857 
01858         /* Reconstructed image */
01859 
01860         giraffe_image_add_info(reconstruction->spectra, &info, set);
01861 
01862         rimg_frame = giraffe_frame_create_image(reconstruction->spectra,
01863                                                 GIFRAME_SCIENCE_RCSPECTRA,
01864                                                 CPL_FRAME_LEVEL_FINAL,
01865                                                 TRUE, TRUE);
01866 
01867         if (rimg_frame == NULL) {
01868             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01869 
01870             giraffe_reconstruction_destroy(reconstruction);
01871             giraffe_rebinning_destroy(rebinning);
01872 
01873             giraffe_table_delete(wcalcoeff);
01874 
01875             giraffe_table_delete(slitgeometry);
01876             giraffe_table_delete(grating);
01877             giraffe_table_delete(fibers);
01878 
01879             return 1;
01880         }
01881 
01882         cpl_frameset_insert(set, rimg_frame);
01883 
01884         /* Reconstructed image errors */
01885 
01886         giraffe_image_add_info(reconstruction->errors, &info, set);
01887 
01888         rimg_frame = giraffe_frame_create_image(reconstruction->errors,
01889                                                 GIFRAME_SCIENCE_RCERRORS,
01890                                                 CPL_FRAME_LEVEL_FINAL,
01891                                                 TRUE, TRUE);
01892 
01893         if (rimg_frame == NULL) {
01894             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01895 
01896             giraffe_reconstruction_destroy(reconstruction);
01897             giraffe_rebinning_destroy(rebinning);
01898 
01899             giraffe_table_delete(wcalcoeff);
01900 
01901             giraffe_table_delete(slitgeometry);
01902             giraffe_table_delete(grating);
01903             giraffe_table_delete(fibers);
01904 
01905             return 1;
01906         }
01907 
01908         cpl_frameset_insert(set, rimg_frame);
01909 
01910         /* Spectrum cube */
01911 
01912         if (reconstruction->cube_spectra != NULL) {
01913 
01914             GiFrameCreator creator = (GiFrameCreator) giraffe_cube_save;
01915 
01916             properties = giraffe_image_get_properties(rebinning->spectra);
01917 
01918             rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_SPECTRA,
01919                                               CPL_FRAME_LEVEL_FINAL,
01920                                               properties,
01921                                               reconstruction->cube_spectra,
01922                                               NULL,
01923                                               creator);
01924 
01925             if (rimg_frame == NULL) {
01926                 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01927 
01928                 giraffe_reconstruction_destroy(reconstruction);
01929                 reconstruction = NULL;
01930 
01931                 giraffe_rebinning_destroy(rebinning);
01932                 rebinning = NULL;
01933 
01934                 giraffe_table_delete(wcalcoeff);
01935                 wcalcoeff = NULL;
01936 
01937                 giraffe_table_delete(slitgeometry);
01938                 slitgeometry = NULL;
01939 
01940                 giraffe_table_delete(grating);
01941                 grating = NULL;
01942 
01943                 giraffe_table_delete(fibers);
01944                 fibers = NULL;
01945 
01946                 return 1;
01947             }
01948 
01949             status = giraffe_fiberlist_attach(rimg_frame, fibers);
01950 
01951             if (status != 0) {
01952                 cpl_msg_error(_id, "Cannot attach fiber setup to local "
01953                               "file '%s'! Aborting ...",
01954                               cpl_frame_get_filename(rimg_frame));
01955 
01956                 cpl_frame_delete(rimg_frame);
01957 
01958                 giraffe_reconstruction_destroy(reconstruction);
01959                 reconstruction = NULL;
01960 
01961                 giraffe_rebinning_destroy(rebinning);
01962                 rebinning = NULL;
01963 
01964                 giraffe_table_delete(wcalcoeff);
01965                 wcalcoeff = NULL;
01966 
01967                 giraffe_table_delete(slitgeometry);
01968                 slitgeometry = NULL;
01969 
01970                 giraffe_table_delete(grating);
01971                 grating = NULL;
01972 
01973                 giraffe_table_delete(fibers);
01974                 fibers = NULL;
01975 
01976                 return 1;
01977             }
01978 
01979             cpl_frameset_insert(set, rimg_frame);
01980 
01981         }
01982 
01983         /* Error cube */
01984 
01985         if (reconstruction->cube_errors != NULL) {
01986 
01987             GiFrameCreator creator = (GiFrameCreator) giraffe_cube_save;
01988 
01989             properties = giraffe_image_get_properties(rebinning->errors);
01990 
01991             rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_ERRORS,
01992                                               CPL_FRAME_LEVEL_FINAL,
01993                                               properties,
01994                                               reconstruction->cube_errors,
01995                                               NULL,
01996                                               creator);
01997 
01998             if (rimg_frame == NULL) {
01999                 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
02000 
02001                 giraffe_reconstruction_destroy(reconstruction);
02002                 reconstruction = NULL;
02003 
02004                 giraffe_rebinning_destroy(rebinning);
02005                 rebinning = NULL;
02006 
02007                 giraffe_table_delete(wcalcoeff);
02008                 wcalcoeff = NULL;
02009 
02010                 giraffe_table_delete(slitgeometry);
02011                 slitgeometry = NULL;
02012 
02013                 giraffe_table_delete(grating);
02014                 grating = NULL;
02015 
02016                 giraffe_table_delete(fibers);
02017                 fibers = NULL;
02018 
02019                 return 1;
02020             }
02021 
02022             status = giraffe_fiberlist_attach(rimg_frame, fibers);
02023 
02024             if (status != 0) {
02025                 cpl_msg_error(_id, "Cannot attach fiber setup to local "
02026                               "file '%s'! Aborting ...",
02027                               cpl_frame_get_filename(rimg_frame));
02028 
02029                 cpl_frame_delete(rimg_frame);
02030 
02031                 giraffe_reconstruction_destroy(reconstruction);
02032                 reconstruction = NULL;
02033 
02034                 giraffe_rebinning_destroy(rebinning);
02035                 rebinning = NULL;
02036 
02037                 giraffe_table_delete(wcalcoeff);
02038                 wcalcoeff = NULL;
02039 
02040                 giraffe_table_delete(slitgeometry);
02041                 slitgeometry = NULL;
02042 
02043                 giraffe_table_delete(grating);
02044                 grating = NULL;
02045 
02046                 giraffe_table_delete(fibers);
02047                 fibers = NULL;
02048 
02049                 return 1;
02050             }
02051 
02052             cpl_frameset_insert(set, rimg_frame);
02053         }
02054 
02055         giraffe_reconstruction_destroy(reconstruction);
02056 
02057     }
02058 
02059 
02060     /*
02061      * Cleanup
02062      */
02063 
02064 
02065     giraffe_table_delete(wcalcoeff);
02066 
02067     giraffe_table_delete(slitgeometry);
02068     giraffe_table_delete(grating);
02069     giraffe_table_delete(fibers);
02070 
02071     giraffe_rebinning_destroy(rebinning);
02072 
02073     return 0;
02074 
02075 }
02076 
02077 
02078 /*
02079  * Build table of contents, i.e. the list of available plugins, for
02080  * this module. This function is exported.
02081  */
02082 
02083 int
02084 cpl_plugin_get_info(cpl_pluginlist* list)
02085 {
02086 
02087     cpl_recipe* recipe = cx_calloc(1, sizeof *recipe);
02088     cpl_plugin* plugin = &recipe->interface;
02089 
02090 
02091     cpl_plugin_init(plugin,
02092                     CPL_PLUGIN_API,
02093                     GIRAFFE_BINARY_VERSION,
02094                     CPL_PLUGIN_TYPE_RECIPE,
02095                     "giscience",
02096                     "Process a science observation.",
02097                     "For detailed information please refer to the "
02098                     "GIRAFFE pipeline user manual.\nIt is available at "
02099                     "http://www.eso.org/pipelines.",
02100                     "Giraffe Pipeline",
02101                     PACKAGE_BUGREPORT,
02102                     giraffe_get_license(),
02103                     giscience_create,
02104                     giscience_exec,
02105                     giscience_destroy);
02106 
02107     cpl_pluginlist_append(list, plugin);
02108 
02109     return 0;
02110 
02111 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.5.3.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Jul 18 09:49:47 2008 by doxygen 1.4.6 written by Dimitri van Heesch, © 1997-2004