GIRAFFE Pipeline Reference Manual

giscience.c

00001 /* $Id: giscience.c,v 1.40 2009/05/29 13:30:38 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: 2009/05/29 13:30:38 $
00024  * $Revision: 1.40 $
00025  * $Name: giraffe-2_8_8 $
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 "giastrometry.h"
00054 #include "gifov.h"
00055 #include "gimessages.h"
00056 #include "gierror.h"
00057 #include "giutils.h"
00058 
00059 
00060 static cxint giscience(cpl_parameterlist*, cpl_frameset*);
00061 
00062 
00063 /*
00064  * Create the recipe instance, i.e. setup the parameter list for this
00065  * recipe and make it availble to the application using the interface.
00066  */
00067 
00068 static cxint
00069 giscience_create(cpl_plugin* plugin)
00070 {
00071 
00072     cpl_recipe* recipe = (cpl_recipe*)plugin;
00073 
00074     cpl_parameter* p = NULL;
00075 
00076 
00077     giraffe_error_init();
00078 
00079 
00080     /*
00081      * We have to provide the option we accept to the application. We
00082      * need to setup our parameter list and hook it into the recipe
00083      * interface.
00084      */
00085 
00086     recipe->parameters = cpl_parameterlist_new();
00087     cx_assert(recipe->parameters != NULL);
00088 
00089 
00090     /*
00091      * Fill the parameter list.
00092      */
00093 
00094     /* Bias removal */
00095 
00096     giraffe_bias_config_add(recipe->parameters);
00097 
00098     /* Dark subtraction */
00099 
00100     /* TBD */
00101 
00102     /* Spectrum extraction */
00103 
00104     giraffe_extract_config_add(recipe->parameters);
00105 
00106     /* Flat fielding and relative fiber transmission correction */
00107 
00108     giraffe_flat_config_add(recipe->parameters);
00109 
00110     /* Spectrum rebinning */
00111 
00112     giraffe_rebin_config_add(recipe->parameters);
00113 
00114     /* Simultaneous wavelength calibration correction */
00115 
00116     p = cpl_parameter_new_value("giraffe.siwc.apply",
00117                                 CPL_TYPE_BOOL,
00118                                 "Enable simultaneous wavelength calibration "
00119                                 "correction.",
00120                                 "giraffe.siwc",
00121                                 TRUE);
00122 
00123     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "siwc-apply");
00124     cpl_parameterlist_append(recipe->parameters, p);
00125 
00126     giraffe_sgcalibration_config_add(recipe->parameters);
00127 
00128     /* Image reconstruction (IFU and Argus only) */
00129 
00130     giraffe_fov_config_add(recipe->parameters);
00131 
00132     return 0;
00133 
00134 }
00135 
00136 
00137 /*
00138  * Execute the plugin instance given by the interface.
00139  */
00140 
00141 static cxint
00142 giscience_exec(cpl_plugin* plugin)
00143 {
00144 
00145     cpl_recipe* recipe = (cpl_recipe*)plugin;
00146 
00147 
00148     cx_assert(recipe->parameters != NULL);
00149     cx_assert(recipe->frames != NULL);
00150 
00151     return giscience(recipe->parameters, recipe->frames);
00152 
00153 }
00154 
00155 
00156 static cxint
00157 giscience_destroy(cpl_plugin* plugin)
00158 {
00159 
00160     cpl_recipe* recipe = (cpl_recipe*)plugin;
00161 
00162 
00163     /*
00164      * We just destroy what was created during the plugin initialization
00165      * phase, i.e. the parameter list. The frame set is managed by the
00166      * application which called us, so we must not touch it,
00167      */
00168 
00169     cpl_parameterlist_delete(recipe->parameters);
00170 
00171     giraffe_error_clear();
00172 
00173     return 0;
00174 
00175 }
00176 
00177 
00178 /*
00179  * The actual recipe starts here.
00180  */
00181 
00182 static cxint
00183 giscience(cpl_parameterlist* config, cpl_frameset* set)
00184 {
00185 
00186     const cxchar* const _id = "giscience";
00187 
00188 
00189     const cxchar* filename = NULL;
00190 
00191     cxbool siwc = FALSE;
00192     cxbool calsim = FALSE;
00193 
00194     cxint status = 0;
00195 
00196     cxlong i;
00197     cxlong nscience = 0;
00198 
00199     cxdouble exptime = 0.;
00200 
00201     cx_slist* slist = NULL;
00202 
00203     cpl_propertylist* properties = NULL;
00204 
00205     cpl_matrix* biasareas = NULL;
00206 
00207     cpl_frame* science_frame = NULL;
00208     cpl_frame* mbias_frame = NULL;
00209     cpl_frame* mdark_frame = NULL;
00210     cpl_frame* bpixel_frame = NULL;
00211     cpl_frame* slight_frame = NULL;
00212     cpl_frame* locy_frame = NULL;
00213     cpl_frame* locw_frame = NULL;
00214     cpl_frame* psfdata_frame = NULL;
00215     cpl_frame* grating_frame = NULL;
00216     cpl_frame* linemask_frame = NULL;
00217     cpl_frame* slit_frame = NULL;
00218     cpl_frame* wcal_frame = NULL;
00219     cpl_frame* rscience_frame = NULL;
00220     cpl_frame* sext_frame = NULL;
00221     cpl_frame* rbin_frame = NULL;
00222 
00223     cpl_parameter* p = NULL;
00224 
00225     GiImage* mbias = NULL;
00226     GiImage* mdark = NULL;
00227     GiImage* bpixel = NULL;
00228     GiImage* slight = NULL;
00229     GiImage* sscience = NULL;
00230     GiImage* rscience = NULL;
00231 
00232     GiTable* fibers = NULL;
00233     GiTable* slitgeometry = NULL;
00234     GiTable* grating = NULL;
00235     GiTable* wcalcoeff = NULL;
00236 
00237     GiLocalization* localization = NULL;
00238     GiExtraction* extraction = NULL;
00239     GiRebinning* rebinning = NULL;
00240 
00241     GiBiasConfig* bias_config = NULL;
00242     GiExtractConfig* extract_config = NULL;
00243     GiFlatConfig* flat_config = NULL;
00244     GiRebinConfig* rebin_config = NULL;
00245 
00246     GiInstrumentMode mode;
00247 
00248     GiRecipeInfo info = {(cxchar*)_id, 1, NULL};
00249 
00250     GiGroupInfo groups[] = {
00251         {GIFRAME_SCIENCE, CPL_FRAME_GROUP_RAW},
00252         {GIFRAME_BADPIXEL_MAP, CPL_FRAME_GROUP_CALIB},
00253         {GIFRAME_BIAS_MASTER, CPL_FRAME_GROUP_CALIB},
00254         {GIFRAME_DARK_MASTER, CPL_FRAME_GROUP_CALIB},
00255         {GIFRAME_FIBER_FLAT_EXTSPECTRA, CPL_FRAME_GROUP_CALIB},
00256         {GIFRAME_FIBER_FLAT_EXTERRORS, CPL_FRAME_GROUP_CALIB},
00257         {GIFRAME_SCATTERED_LIGHT_MODEL, CPL_FRAME_GROUP_CALIB},
00258         {GIFRAME_LOCALIZATION_CENTROID, CPL_FRAME_GROUP_CALIB},
00259         {GIFRAME_LOCALIZATION_WIDTH, CPL_FRAME_GROUP_CALIB},
00260         {GIFRAME_PSF_CENTROID, CPL_FRAME_GROUP_CALIB},
00261         {GIFRAME_PSF_WIDTH, CPL_FRAME_GROUP_CALIB},
00262         {GIFRAME_PSF_DATA, CPL_FRAME_GROUP_CALIB},
00263         {GIFRAME_WAVELENGTH_SOLUTION, CPL_FRAME_GROUP_CALIB},
00264         {GIFRAME_LINE_MASK, CPL_FRAME_GROUP_CALIB},
00265         {GIFRAME_SLITSETUP, CPL_FRAME_GROUP_CALIB},
00266         {GIFRAME_SLITMASTER, CPL_FRAME_GROUP_CALIB},
00267         {GIFRAME_GRATING, CPL_FRAME_GROUP_CALIB},
00268         {NULL, CPL_FRAME_GROUP_NONE}
00269     };
00270 
00271 
00272 
00273     if (!config) {
00274         cpl_msg_error(_id, "Invalid parameter list! Aborting ...");
00275         return 1;
00276     }
00277 
00278     if (!set) {
00279         cpl_msg_error(_id, "Invalid frame set! Aborting ...");
00280         return 1;
00281     }
00282 
00283     status = giraffe_frameset_set_groups(set, groups);
00284 
00285     if (status != 0) {
00286         cpl_msg_error(_id, "Setting frame group information failed!");
00287         return 1;
00288     }
00289 
00290 
00291     /*
00292      * Verify the frame set contents
00293      */
00294 
00295     nscience = cpl_frameset_count_tags(set, GIFRAME_SCIENCE);
00296 
00297     if (nscience < 1) {
00298         cpl_msg_error(_id, "Too few (%ld) raw frames (%s) present in "
00299                       "frame set! Aborting ...", nscience, GIFRAME_SCIENCE);
00300         return 1;
00301     }
00302 
00303     locy_frame = cpl_frameset_find(set, GIFRAME_PSF_CENTROID);
00304 
00305     if (locy_frame == NULL) {
00306 
00307         locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
00308 
00309         if (locy_frame == NULL) {
00310             cpl_msg_info(_id, "No master localization (centroid position) "
00311                          "present in frame set. Aborting ...");
00312             return 1;
00313         }
00314 
00315     }
00316 
00317     locw_frame = cpl_frameset_find(set, GIFRAME_PSF_WIDTH);
00318 
00319     if (locw_frame == NULL) {
00320 
00321         locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
00322 
00323         if (locw_frame == NULL) {
00324             cpl_msg_info(_id, "No master localization (spectrum width) "
00325                          "present in frame set. Aborting ...");
00326             return 1;
00327         }
00328 
00329     }
00330 
00331     grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
00332 
00333     if (!grating_frame) {
00334         cpl_msg_error(_id, "No grating data present in frame set. "
00335                       "Aborting ...");
00336         return 1;
00337     }
00338 
00339     slit_frame = giraffe_get_slitgeometry(set);
00340 
00341     if (!slit_frame) {
00342         cpl_msg_error(_id, "No slit geometry present in frame set. "
00343                       "Aborting ...");
00344         return 1;
00345     }
00346 
00347     wcal_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
00348 
00349     if (!wcal_frame) {
00350         cpl_msg_error(_id, "No dispersion solution present in frame set. "
00351                       "Aborting ...");
00352         return 1;
00353     }
00354 
00355     linemask_frame = cpl_frameset_find(set, GIFRAME_LINE_MASK);
00356 
00357     if (!linemask_frame) {
00358         cpl_msg_warning(_id, "No reference line mask present in frame set.");
00359     }
00360 
00361     bpixel_frame = cpl_frameset_find(set, GIFRAME_BADPIXEL_MAP);
00362 
00363     if (!bpixel_frame) {
00364         cpl_msg_info(_id, "No bad pixel map present in frame set.");
00365     }
00366 
00367     mbias_frame = cpl_frameset_find(set, GIFRAME_BIAS_MASTER);
00368 
00369     if (!mbias_frame) {
00370         cpl_msg_info(_id, "No master bias present in frame set.");
00371     }
00372 
00373     mdark_frame = cpl_frameset_find(set, GIFRAME_DARK_MASTER);
00374 
00375     if (!mdark_frame) {
00376         cpl_msg_info(_id, "No master dark present in frame set.");
00377     }
00378 
00379     slight_frame = cpl_frameset_find(set, GIFRAME_SCATTERED_LIGHT_MODEL);
00380 
00381     if (!slight_frame) {
00382         cpl_msg_info(_id, "No scattered light model present in frame set.");
00383     }
00384 
00385     psfdata_frame = cpl_frameset_find(set, GIFRAME_PSF_DATA);
00386 
00387     if (!psfdata_frame) {
00388         cpl_msg_info(_id, "No PSF profile parameters present in frame set.");
00389     }
00390 
00391 
00392     /*
00393      * Load raw images
00394      */
00395 
00396     slist = cx_slist_new();
00397 
00398     science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
00399 
00400     for (i = 0; i < nscience; i++) {
00401 
00402         filename = cpl_frame_get_filename(science_frame);
00403 
00404         GiImage* raw = giraffe_image_new(CPL_TYPE_DOUBLE);
00405 
00406 
00407         status = giraffe_image_load(raw, filename, 0);
00408 
00409         if (status) {
00410             cpl_msg_error(_id, "Cannot load raw science frame from '%s'. "
00411                           "Aborting ...", filename);
00412 
00413             cx_slist_destroy(slist, (cx_free_func) giraffe_image_delete);
00414 
00415             return 1;
00416         }
00417 
00418         cx_slist_push_back(slist, raw);
00419 
00420         science_frame = cpl_frameset_find(set, NULL);
00421 
00422     }
00423 
00424     nscience = (cxint)cx_slist_size(slist);
00425     sscience = cx_slist_pop_front(slist);
00426 
00427     properties = giraffe_image_get_properties(sscience);
00428     cx_assert(properties != NULL);
00429 
00430     if (nscience > 1) {
00431 
00432         /*
00433          * Create a stacked science image from the list of raw images.
00434          * Each raw image is disposed when it is no longer needed.
00435          */
00436 
00437         cpl_msg_info(_id, "Averaging science frames ...");
00438 
00439         exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
00440 
00441         for (i = 1; i < nscience; i++) {
00442 
00443             cpl_propertylist* _properties;
00444 
00445             GiImage* science = cx_slist_pop_front(slist);
00446 
00447 
00448             cpl_image_add(giraffe_image_get(sscience),
00449                           giraffe_image_get(science));
00450 
00451             _properties = giraffe_image_get_properties(science);
00452             cx_assert(_properties != NULL);
00453 
00454             exptime += cpl_propertylist_get_double(_properties, GIALIAS_EXPTIME);
00455 
00456             giraffe_image_delete(science);
00457 
00458         }
00459 
00460         cpl_image_divide_scalar(giraffe_image_get(sscience), nscience);
00461     }
00462 
00463     cx_assert(cx_slist_empty(slist));
00464     cx_slist_delete(slist);
00465     slist = NULL;
00466 
00467 
00468     if (nscience > 1) {
00469 
00470         /*
00471          * Update stacked science image properties
00472          */
00473 
00474         cpl_msg_info(_id, "Updating stacked science image properties ...");
00475 
00476         cpl_propertylist_set_double(properties, GIALIAS_EXPTIME,
00477                                     exptime / nscience);
00478 
00479         cpl_propertylist_append_double(properties, GIALIAS_EXPTTOT, exptime);
00480         cpl_propertylist_set_comment(properties, GIALIAS_EXPTTOT,
00481                                      "Total exposure time of all frames "
00482                                      "combined");
00483 
00484         cpl_propertylist_append_int(properties, GIALIAS_DATANCOM, nscience);
00485         cpl_propertylist_set_comment(properties, GIALIAS_DATANCOM,
00486                                      "Number of frames combined");
00487 
00488         cpl_propertylist_erase(properties, GIALIAS_TPLEXPNO);
00489 
00490     }
00491 
00492 
00493     /*
00494      * Prepare for bias subtraction
00495      */
00496 
00497     bias_config = giraffe_bias_config_create(config);
00498 
00499     /*
00500      * Setup user defined areas to use for the bias computation
00501      */
00502 
00503     if (bias_config->method == GIBIAS_METHOD_MASTER ||
00504         bias_config->method == GIBIAS_METHOD_ZMASTER) {
00505 
00506         if (!mbias_frame) {
00507             cpl_msg_error(_id, "Missing master bias frame! Selected bias "
00508                           "removal method requires a master bias frame!");
00509 
00510             giraffe_bias_config_destroy(bias_config);
00511             giraffe_image_delete(sscience);
00512 
00513             return 1;
00514         }
00515         else {
00516             filename = cpl_frame_get_filename(mbias_frame);
00517 
00518 
00519             mbias = giraffe_image_new(CPL_TYPE_DOUBLE);
00520             status = giraffe_image_load(mbias, filename, 0);
00521 
00522             if (status) {
00523                 cpl_msg_error(_id, "Cannot load master bias from '%s'. "
00524                               "Aborting ...", filename);
00525 
00526                 giraffe_bias_config_destroy(bias_config);
00527                 giraffe_image_delete(sscience);
00528 
00529                 return 1;
00530             }
00531         }
00532     }
00533 
00534 
00535     /*
00536      * Load bad pixel map if it is present in the frame set.
00537      */
00538 
00539     if (bpixel_frame) {
00540 
00541         filename = cpl_frame_get_filename(bpixel_frame);
00542 
00543 
00544         bpixel = giraffe_image_new(CPL_TYPE_INT);
00545         status = giraffe_image_load(bpixel, filename, 0);
00546 
00547         if (status) {
00548             cpl_msg_error(_id, "Cannot load bad pixel map from '%s'. "
00549                           "Aborting ...", filename);
00550 
00551             giraffe_image_delete(bpixel);
00552             bpixel = NULL;
00553 
00554             if (mbias != NULL) {
00555                 giraffe_image_delete(mbias);
00556                 mbias = NULL;
00557             }
00558 
00559             giraffe_bias_config_destroy(bias_config);
00560             bias_config = NULL;
00561 
00562             giraffe_image_delete(sscience);
00563             sscience = NULL;
00564 
00565             return 1;
00566         }
00567 
00568     }
00569 
00570 
00571     /*
00572      * Compute and remove the bias from the stacked flat field frame.
00573      */
00574 
00575     rscience = giraffe_image_new(CPL_TYPE_DOUBLE);
00576 
00577     status = giraffe_bias_remove(rscience, sscience, mbias, bpixel, biasareas,
00578                                  bias_config);
00579 
00580     giraffe_image_delete(sscience);
00581 
00582     if (mbias) {
00583         giraffe_image_delete(mbias);
00584         mbias = NULL;
00585     }
00586 
00587     giraffe_bias_config_destroy(bias_config);
00588 
00589     if (status) {
00590         cpl_msg_error(_id, "Bias removal failed. Aborting ...");
00591 
00592         giraffe_image_delete(rscience);
00593         rscience = NULL;
00594 
00595         if (bpixel != NULL) {
00596             giraffe_image_delete(bpixel);
00597             bpixel = NULL;
00598         }
00599 
00600         return 1;
00601     }
00602 
00603 
00604     /*
00605      * Load master dark if it is present in the frame set and correct
00606      * the master flat field for the dark current.
00607      */
00608 
00609     if (mdark_frame) {
00610 
00611         GiDarkConfig dark_config = {GIDARK_METHOD_ZMASTER, 0.};
00612 
00613 
00614         cpl_msg_info(_id, "Correcting for dark current ...");
00615 
00616         filename = cpl_frame_get_filename(mdark_frame);
00617 
00618         mdark = giraffe_image_new(CPL_TYPE_DOUBLE);
00619         status = giraffe_image_load(mdark, filename, 0);
00620 
00621         if (status != 0) {
00622             cpl_msg_error(_id, "Cannot load master dark from '%s'. "
00623                           "Aborting ...", filename);
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         status = giraffe_subtract_dark(rscience, mdark, bpixel, NULL,
00637                                        &dark_config);
00638 
00639         if (status != 0) {
00640             cpl_msg_error(_id, "Dark subtraction failed! Aborting ...");
00641 
00642             giraffe_image_delete(mdark);
00643             mdark = NULL;
00644 
00645             giraffe_image_delete(rscience);
00646             rscience = NULL;
00647 
00648             if (bpixel != NULL) {
00649                 giraffe_image_delete(bpixel);
00650                 bpixel = NULL;
00651             }
00652 
00653             return 1;
00654         }
00655 
00656         giraffe_image_delete(mdark);
00657         mdark = NULL;
00658 
00659     }
00660 
00661 
00662     /*
00663      * Update the reduced science properties, save the reduced science frame
00664      * and register it as product.
00665      */
00666 
00667     cpl_msg_info(_id, "Writing pre-processed science image ...");
00668 
00669     giraffe_image_add_info(rscience, &info, set);
00670 
00671     rscience_frame = giraffe_frame_create_image(rscience,
00672                                              GIFRAME_SCIENCE_REDUCED,
00673                                              CPL_FRAME_LEVEL_INTERMEDIATE,
00674                                              TRUE, TRUE);
00675 
00676     if (rscience_frame == NULL) {
00677         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
00678 
00679         giraffe_image_delete(rscience);
00680 
00681         return 1;
00682     }
00683 
00684     cpl_frameset_insert(set, rscience_frame);
00685 
00686 
00687     /*
00688      * Determine fiber setup
00689      */
00690 
00691     science_frame = cpl_frameset_find(set, GIFRAME_SCIENCE);
00692 
00693     cpl_msg_info(_id, "Building fiber setup for frame '%s'.",
00694                  cpl_frame_get_filename(science_frame));
00695 
00696     fibers = giraffe_fibers_setup(science_frame, locy_frame);
00697 
00698     if (!fibers) {
00699         cpl_msg_error(_id, "Cannot create fiber setup for frame '%s'! "
00700                       "Aborting ...", cpl_frame_get_filename(science_frame));
00701 
00702         if (bpixel) {
00703             giraffe_image_delete(bpixel);
00704             bpixel = NULL;
00705         }
00706 
00707         giraffe_image_delete(rscience);
00708         rscience = NULL;
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(rscience);
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(rscience);
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(rscience);
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(rscience);
00843             rscience = 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(rscience);
00880                 rscience = 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, rscience, 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(rscience);
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(rscience);
00927     rscience = 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_SCIENCE_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_SCIENCE_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_SCIENCE_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_SCIENCE_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_SCIENCE_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 = giraffe_rebin_spectra(rebinning, extraction, fibers,
01485                                    localization, grating, slitgeometry,
01486                                    wcalcoeff, rebin_config);
01487 
01488     if (status) {
01489         cpl_msg_error(_id, "Rebinning of science spectra failed! Aborting...");
01490 
01491         giraffe_rebinning_destroy(rebinning);
01492 
01493         giraffe_extraction_destroy(extraction);
01494         giraffe_localization_destroy(localization);
01495 
01496         giraffe_table_delete(wcalcoeff);
01497 
01498         giraffe_table_delete(slitgeometry);
01499         giraffe_table_delete(grating);
01500         giraffe_table_delete(fibers);
01501 
01502         giraffe_rebin_config_destroy(rebin_config);
01503 
01504         return 1;
01505 
01506     }
01507 
01508 
01509     /*
01510      * Optionally compute and apply spectral shifts from the simultaneous
01511      * calibration fibers. This is only done if the simultaneous calibration
01512      * fibers were used.
01513      */
01514 
01515     p = cpl_parameterlist_find(config, "giraffe.siwc.apply");
01516     cx_assert(p != NULL);
01517 
01518     siwc = cpl_parameter_get_bool(p);
01519     p = NULL;
01520 
01521     properties = giraffe_image_get_properties(rebinning->spectra);
01522     cx_assert(properties != NULL);
01523 
01524     if (cpl_propertylist_has(properties, GIALIAS_STSCTAL) == TRUE) {
01525         calsim = cpl_propertylist_get_bool(properties, GIALIAS_STSCTAL);
01526     }
01527 
01528     if ((siwc == TRUE) && (calsim == TRUE) && (linemask_frame != NULL)) {
01529 
01530         GiTable* linemask = giraffe_table_new();
01531 
01532         GiSGCalConfig* siwc_config = NULL;
01533 
01534 
01535         siwc_config = giraffe_sgcalibration_config_create(config);
01536 
01537         if (siwc_config == NULL) {
01538 
01539             giraffe_table_delete(linemask);
01540             linemask = NULL;
01541 
01542             giraffe_rebinning_destroy(rebinning);
01543 
01544             giraffe_extraction_destroy(extraction);
01545             giraffe_localization_destroy(localization);
01546 
01547             giraffe_table_delete(wcalcoeff);
01548 
01549             giraffe_table_delete(slitgeometry);
01550             giraffe_table_delete(grating);
01551             giraffe_table_delete(fibers);
01552 
01553             giraffe_rebin_config_destroy(rebin_config);
01554 
01555             return 1;
01556 
01557         }
01558 
01559         filename = cpl_frame_get_filename(linemask_frame);
01560 
01561         status = giraffe_table_load(linemask, filename, 1, NULL);
01562 
01563         if (status) {
01564             cpl_msg_error(_id, "Cannot load line reference mask from '%s'. "
01565                           "Aborting ...", filename);
01566 
01567             giraffe_sgcalibration_config_destroy(siwc_config);
01568             siwc_config = NULL;
01569 
01570             giraffe_table_delete(linemask);
01571             linemask = NULL;
01572 
01573             giraffe_rebinning_destroy(rebinning);
01574 
01575             giraffe_extraction_destroy(extraction);
01576             giraffe_localization_destroy(localization);
01577 
01578             giraffe_table_delete(wcalcoeff);
01579 
01580             giraffe_table_delete(slitgeometry);
01581             giraffe_table_delete(grating);
01582             giraffe_table_delete(fibers);
01583 
01584             giraffe_rebin_config_destroy(rebin_config);
01585 
01586             return 1;
01587 
01588         }
01589 
01590 
01591         status = giraffe_compute_offsets(fibers, rebinning, grating,
01592                                          linemask, siwc_config);
01593 
01594         if (status != 0) {
01595             cpl_msg_error(_id, "Applying simultaneous wavelength "
01596                           "calibration correction failed! Aborting...");
01597 
01598             giraffe_sgcalibration_config_destroy(siwc_config);
01599             siwc_config = NULL;
01600 
01601             giraffe_table_delete(linemask);
01602             linemask = NULL;
01603 
01604             giraffe_rebinning_destroy(rebinning);
01605 
01606             giraffe_extraction_destroy(extraction);
01607             giraffe_localization_destroy(localization);
01608 
01609             giraffe_table_delete(wcalcoeff);
01610 
01611             giraffe_table_delete(slitgeometry);
01612             giraffe_table_delete(grating);
01613             giraffe_table_delete(fibers);
01614 
01615             giraffe_rebin_config_destroy(rebin_config);
01616 
01617             return 1;
01618 
01619         }
01620 
01621         giraffe_sgcalibration_config_destroy(siwc_config);
01622         siwc_config = NULL;
01623 
01624         giraffe_table_delete(linemask);
01625         linemask = NULL;
01626 
01627         giraffe_rebinning_destroy(rebinning);
01628         rebinning = giraffe_rebinning_new();
01629 
01630         status = giraffe_rebin_spectra(rebinning, extraction, fibers,
01631                                        localization, grating, slitgeometry,
01632                                        wcalcoeff, rebin_config);
01633 
01634         if (status) {
01635             cpl_msg_error(_id, "Rebinning of science spectra failed! "
01636                           "Aborting...");
01637 
01638             giraffe_rebinning_destroy(rebinning);
01639 
01640             giraffe_extraction_destroy(extraction);
01641             giraffe_localization_destroy(localization);
01642 
01643             giraffe_table_delete(wcalcoeff);
01644 
01645             giraffe_table_delete(slitgeometry);
01646             giraffe_table_delete(grating);
01647             giraffe_table_delete(fibers);
01648 
01649             giraffe_rebin_config_destroy(rebin_config);
01650 
01651             return 1;
01652 
01653         }
01654 
01655         status = giraffe_add_rvcorrection(fibers, rebinning->spectra);
01656 
01657         switch (status) {
01658         case 0:
01659             {
01660                 break;
01661             }
01662 
01663         case 1:
01664             {
01665                 cpl_msg_warning(_id, "Missing observation time properties! "
01666                                 "Barycentric correction computation "
01667                                 "skipped!");
01668                 status = 0;
01669                 break;
01670             }
01671         case 2:
01672             {
01673                 cpl_msg_warning(_id, "Missing telescope location properties! "
01674                                 "Barycentric correction computation "
01675                                 "skipped!");
01676                 status = 0;
01677                 break;
01678             }
01679         case 3:
01680             {
01681                 cpl_msg_warning(_id, "Object positions are not available "
01682                                 "Barycentric correction computation "
01683                                 "skipped!");
01684                 status = 0;
01685                 break;
01686             }
01687         default:
01688             {
01689                 cpl_msg_error(_id, "Barycentric correction computation "
01690                               "failed! Aborting...");
01691 
01692                 giraffe_rebinning_destroy(rebinning);
01693 
01694                 giraffe_extraction_destroy(extraction);
01695                 giraffe_localization_destroy(localization);
01696 
01697                 giraffe_table_delete(wcalcoeff);
01698 
01699                 giraffe_table_delete(slitgeometry);
01700                 giraffe_table_delete(grating);
01701                 giraffe_table_delete(fibers);
01702 
01703                 giraffe_rebin_config_destroy(rebin_config);
01704 
01705                 return 1;
01706                 break;
01707             }
01708 
01709         }
01710 
01711     }
01712 
01713     giraffe_extraction_destroy(extraction);
01714     extraction = NULL;
01715 
01716     giraffe_localization_destroy(localization);
01717     localization = NULL;
01718 
01719     giraffe_rebin_config_destroy(rebin_config);
01720     rebin_config = NULL;
01721 
01722 
01723     /*
01724      * Save and register the results of the spectrum rebinning.
01725      */
01726 
01727     /* Rebinned spectra */
01728 
01729     giraffe_image_add_info(rebinning->spectra, &info, set);
01730 
01731     rbin_frame = giraffe_frame_create_image(rebinning->spectra,
01732                                             GIFRAME_SCIENCE_RBNSPECTRA,
01733                                             CPL_FRAME_LEVEL_FINAL,
01734                                             TRUE, TRUE);
01735 
01736     if (rbin_frame == NULL) {
01737         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01738 
01739         giraffe_rebinning_destroy(rebinning);
01740 
01741         giraffe_table_delete(wcalcoeff);
01742 
01743         giraffe_table_delete(slitgeometry);
01744         giraffe_table_delete(grating);
01745         giraffe_table_delete(fibers);
01746 
01747         return 1;
01748     }
01749 
01750     status = giraffe_fiberlist_attach(rbin_frame, fibers);
01751 
01752     if (status) {
01753         cpl_msg_error(_id, "Cannot attach fiber setup to local "
01754                       "file '%s'! Aborting ...",
01755                       cpl_frame_get_filename(rbin_frame));
01756 
01757         giraffe_rebinning_destroy(rebinning);
01758         giraffe_table_delete(wcalcoeff);
01759 
01760         giraffe_table_delete(slitgeometry);
01761         giraffe_table_delete(grating);
01762         giraffe_table_delete(fibers);
01763 
01764         cpl_frame_delete(rbin_frame);
01765 
01766         return 1;
01767     }
01768 
01769     cpl_frameset_insert(set, rbin_frame);
01770 
01771     /* Rebinned spectra errors */
01772 
01773     giraffe_image_add_info(rebinning->errors, &info, set);
01774 
01775     rbin_frame = giraffe_frame_create_image(rebinning->errors,
01776                                             GIFRAME_SCIENCE_RBNERRORS,
01777                                             CPL_FRAME_LEVEL_FINAL,
01778                                             TRUE, TRUE);
01779 
01780     if (rbin_frame == NULL) {
01781         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01782 
01783         giraffe_rebinning_destroy(rebinning);
01784 
01785         giraffe_table_delete(wcalcoeff);
01786 
01787         giraffe_table_delete(slitgeometry);
01788         giraffe_table_delete(grating);
01789         giraffe_table_delete(fibers);
01790 
01791         return 1;
01792     }
01793 
01794     status = giraffe_fiberlist_attach(rbin_frame, fibers);
01795 
01796     if (status) {
01797         cpl_msg_error(_id, "Cannot attach fiber setup to local "
01798                       "file '%s'! Aborting ...",
01799                       cpl_frame_get_filename(rbin_frame));
01800 
01801         giraffe_rebinning_destroy(rebinning);
01802 
01803         giraffe_table_delete(wcalcoeff);
01804 
01805         giraffe_table_delete(slitgeometry);
01806         giraffe_table_delete(grating);
01807         giraffe_table_delete(fibers);
01808 
01809         cpl_frame_delete(rbin_frame);
01810 
01811         return 1;
01812     }
01813 
01814     cpl_frameset_insert(set, rbin_frame);
01815 
01816 
01817     /*
01818      * Optional image and data cube construction (only for IFU and Argus)
01819      */
01820 
01821     properties = giraffe_image_get_properties(rebinning->spectra);
01822     mode = giraffe_get_mode(properties);
01823 
01824 
01825     if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
01826 
01827         cpl_frame* rimg_frame = NULL;
01828 
01829         GiFieldOfView* fov = NULL;
01830         GiFieldOfViewConfig* fov_config = NULL;
01831 
01832 
01833         fov_config = giraffe_fov_config_create(config);
01834 
01835         cpl_msg_info(_id, "Reconstructing image and data cube from rebinned "
01836                      "spectra ...");
01837 
01838         fov = giraffe_fov_new();
01839 
01840         status = giraffe_fov_build(fov, rebinning, fibers, wcalcoeff, grating,
01841                                    slitgeometry, fov_config);
01842 
01843         if (status) {
01844 
01845             if (status == -2) {
01846                 cpl_msg_warning(_id, "No reconstructed image was built. "
01847                                 "Fiber list has no fiber position "
01848                                 "information.");
01849             }
01850             else {
01851                 cpl_msg_error(_id, "Image reconstruction failed! Aborting...");
01852 
01853                 giraffe_fov_delete(fov);
01854                 giraffe_rebinning_destroy(rebinning);
01855 
01856                 giraffe_table_delete(wcalcoeff);
01857 
01858                 giraffe_table_delete(slitgeometry);
01859                 giraffe_table_delete(grating);
01860                 giraffe_table_delete(fibers);
01861 
01862                 giraffe_fov_config_destroy(fov_config);
01863 
01864                 return 1;
01865             }
01866 
01867         }
01868 
01869         giraffe_fov_config_destroy(fov_config);
01870 
01871 
01872         /*
01873          * Save and register the results of the image reconstruction.
01874          */
01875 
01876         /* Reconstructed image */
01877 
01878         giraffe_image_add_info(fov->fov.spectra, &info, set);
01879 
01880         rimg_frame = giraffe_frame_create_image(fov->fov.spectra,
01881                                                 GIFRAME_SCIENCE_RCSPECTRA,
01882                                                 CPL_FRAME_LEVEL_FINAL,
01883                                                 TRUE, TRUE);
01884 
01885         if (rimg_frame == NULL) {
01886             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01887 
01888             giraffe_fov_delete(fov);
01889             giraffe_rebinning_destroy(rebinning);
01890 
01891             giraffe_table_delete(wcalcoeff);
01892 
01893             giraffe_table_delete(slitgeometry);
01894             giraffe_table_delete(grating);
01895             giraffe_table_delete(fibers);
01896 
01897             return 1;
01898         }
01899 
01900         cpl_frameset_insert(set, rimg_frame);
01901 
01902 
01903         /* Reconstructed image errors */
01904 
01905         giraffe_image_add_info(fov->fov.errors, &info, set);
01906 
01907         rimg_frame = giraffe_frame_create_image(fov->fov.errors,
01908                                                 GIFRAME_SCIENCE_RCERRORS,
01909                                                 CPL_FRAME_LEVEL_FINAL,
01910                                                 TRUE, TRUE);
01911 
01912         if (rimg_frame == NULL) {
01913             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01914 
01915             giraffe_fov_delete(fov);
01916             giraffe_rebinning_destroy(rebinning);
01917 
01918             giraffe_table_delete(wcalcoeff);
01919 
01920             giraffe_table_delete(slitgeometry);
01921             giraffe_table_delete(grating);
01922             giraffe_table_delete(fibers);
01923 
01924             return 1;
01925         }
01926 
01927         cpl_frameset_insert(set, rimg_frame);
01928 
01929 
01930         /* Spectrum cube */
01931 
01932         if (fov->cubes.spectra != NULL) {
01933 
01934             cxint component = 0;
01935 
01936             GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
01937 
01938             properties = giraffe_image_get_properties(rebinning->spectra);
01939             properties = cpl_propertylist_duplicate(properties);
01940 
01941             giraffe_add_frameset_info(properties, set, info.sequence);
01942 
01943             rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_SPECTRA,
01944                                               CPL_FRAME_LEVEL_FINAL,
01945                                               properties,
01946                                               fov,
01947                                               &component,
01948                                               creator);
01949 
01950             cpl_propertylist_delete(properties);
01951             properties = NULL;
01952 
01953             if (rimg_frame == NULL) {
01954                 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01955 
01956                 giraffe_fov_delete(fov);
01957                 fov = NULL;
01958 
01959                 giraffe_rebinning_destroy(rebinning);
01960                 rebinning = NULL;
01961 
01962                 giraffe_table_delete(wcalcoeff);
01963                 wcalcoeff = NULL;
01964 
01965                 giraffe_table_delete(slitgeometry);
01966                 slitgeometry = NULL;
01967 
01968                 giraffe_table_delete(grating);
01969                 grating = NULL;
01970 
01971                 giraffe_table_delete(fibers);
01972                 fibers = NULL;
01973 
01974                 return 1;
01975             }
01976 
01977 
01978             status = giraffe_fiberlist_attach(rimg_frame, fibers);
01979 
01980             if (status != 0) {
01981                 cpl_msg_error(_id, "Cannot attach fiber setup to local "
01982                               "file '%s'! Aborting ...",
01983                               cpl_frame_get_filename(rimg_frame));
01984 
01985                 cpl_frame_delete(rimg_frame);
01986 
01987                 giraffe_fov_delete(fov);
01988                 fov = NULL;
01989 
01990                 giraffe_rebinning_destroy(rebinning);
01991                 rebinning = NULL;
01992 
01993                 giraffe_table_delete(wcalcoeff);
01994                 wcalcoeff = NULL;
01995 
01996                 giraffe_table_delete(slitgeometry);
01997                 slitgeometry = NULL;
01998 
01999                 giraffe_table_delete(grating);
02000                 grating = NULL;
02001 
02002                 giraffe_table_delete(fibers);
02003                 fibers = NULL;
02004 
02005                 return 1;
02006             }
02007 
02008             cpl_frameset_insert(set, rimg_frame);
02009 
02010         }
02011 
02012         /* Error cube */
02013 
02014         if (fov->cubes.errors != NULL) {
02015 
02016             cxint component = 1;
02017 
02018             GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
02019 
02020 
02021             properties = giraffe_image_get_properties(rebinning->errors);
02022             properties = cpl_propertylist_duplicate(properties);
02023 
02024             giraffe_add_frameset_info(properties, set, info.sequence);
02025 
02026             rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_ERRORS,
02027                                               CPL_FRAME_LEVEL_FINAL,
02028                                               properties,
02029                                               fov,
02030                                               &component,
02031                                               creator);
02032 
02033             cpl_propertylist_delete(properties);
02034             properties = NULL;
02035 
02036             if (rimg_frame == NULL) {
02037                 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
02038 
02039                 giraffe_fov_delete(fov);
02040                 fov = NULL;
02041 
02042                 giraffe_rebinning_destroy(rebinning);
02043                 rebinning = NULL;
02044 
02045                 giraffe_table_delete(wcalcoeff);
02046                 wcalcoeff = NULL;
02047 
02048                 giraffe_table_delete(slitgeometry);
02049                 slitgeometry = NULL;
02050 
02051                 giraffe_table_delete(grating);
02052                 grating = NULL;
02053 
02054                 giraffe_table_delete(fibers);
02055                 fibers = NULL;
02056 
02057                 return 1;
02058             }
02059 
02060             status = giraffe_fiberlist_attach(rimg_frame, fibers);
02061 
02062             if (status != 0) {
02063                 cpl_msg_error(_id, "Cannot attach fiber setup to local "
02064                               "file '%s'! Aborting ...",
02065                               cpl_frame_get_filename(rimg_frame));
02066 
02067                 cpl_frame_delete(rimg_frame);
02068 
02069                 giraffe_fov_delete(fov);
02070                 fov = NULL;
02071 
02072                 giraffe_rebinning_destroy(rebinning);
02073                 rebinning = NULL;
02074 
02075                 giraffe_table_delete(wcalcoeff);
02076                 wcalcoeff = NULL;
02077 
02078                 giraffe_table_delete(slitgeometry);
02079                 slitgeometry = NULL;
02080 
02081                 giraffe_table_delete(grating);
02082                 grating = NULL;
02083 
02084                 giraffe_table_delete(fibers);
02085                 fibers = NULL;
02086 
02087                 return 1;
02088             }
02089 
02090             cpl_frameset_insert(set, rimg_frame);
02091         }
02092 
02093         giraffe_fov_delete(fov);
02094         fov = NULL;
02095 
02096     }
02097 
02098 
02099     /*
02100      * Cleanup
02101      */
02102 
02103     giraffe_table_delete(wcalcoeff);
02104 
02105     giraffe_table_delete(slitgeometry);
02106     giraffe_table_delete(grating);
02107     giraffe_table_delete(fibers);
02108 
02109     giraffe_rebinning_destroy(rebinning);
02110 
02111     return 0;
02112 
02113 }
02114 
02115 
02116 /*
02117  * Build table of contents, i.e. the list of available plugins, for
02118  * this module. This function is exported.
02119  */
02120 
02121 int
02122 cpl_plugin_get_info(cpl_pluginlist* list)
02123 {
02124 
02125     cpl_recipe* recipe = cx_calloc(1, sizeof *recipe);
02126     cpl_plugin* plugin = &recipe->interface;
02127 
02128 
02129     cpl_plugin_init(plugin,
02130                     CPL_PLUGIN_API,
02131                     GIRAFFE_BINARY_VERSION,
02132                     CPL_PLUGIN_TYPE_RECIPE,
02133                     "giscience",
02134                     "Process a science observation.",
02135                     "For detailed information please refer to the "
02136                     "GIRAFFE pipeline user manual.\nIt is available at "
02137                     "http://www.eso.org/pipelines.",
02138                     "Giraffe Pipeline",
02139                     PACKAGE_BUGREPORT,
02140                     giraffe_get_license(),
02141                     giscience_create,
02142                     giscience_exec,
02143                     giscience_destroy);
02144 
02145     cpl_pluginlist_append(list, plugin);
02146 
02147     return 0;
02148 
02149 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.8.8.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Mar 4 10:50:27 2011 by doxygen 1.6.3 written by Dimitri van Heesch, © 1997-2004