GIRAFFE Pipeline Reference Manual

giscience.c

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

This file is part of the GIRAFFE Pipeline Reference Manual 2.5.1.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Tue Mar 18 10:47:43 2008 by doxygen 1.4.6 written by Dimitri van Heesch, © 1997-2004