GIRAFFE Pipeline Reference Manual

gistandard.c

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

This file is part of the GIRAFFE Pipeline Reference Manual 2.5.2.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Jun 13 14:36:24 2008 by doxygen 1.4.6 written by Dimitri van Heesch, © 1997-2004