GIRAFFE Pipeline Reference Manual

giscience.c

00001 /* $Id: giscience.c,v 1.42 2012/08/17 14:13:28 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: 2012/08/17 14:13:28 $
00024  * $Revision: 1.42 $
00025  * $Name: giraffe-2_10 $
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 
01525     if (cpl_propertylist_has(properties, GIALIAS_STSCTAL) == TRUE) {
01526         calsim = cpl_propertylist_get_bool(properties, GIALIAS_STSCTAL);
01527     }
01528 
01529 
01530     if ((siwc == TRUE) && (calsim == TRUE) && (linemask_frame != NULL)) {
01531 
01532         GiTable* linemask = giraffe_table_new();
01533 
01534         GiSGCalConfig* siwc_config = NULL;
01535 
01536 
01537         siwc_config = giraffe_sgcalibration_config_create(config);
01538 
01539         if (siwc_config == NULL) {
01540 
01541             giraffe_table_delete(linemask);
01542             linemask = NULL;
01543 
01544             giraffe_rebinning_destroy(rebinning);
01545 
01546             giraffe_extraction_destroy(extraction);
01547             giraffe_localization_destroy(localization);
01548 
01549             giraffe_table_delete(wcalcoeff);
01550 
01551             giraffe_table_delete(slitgeometry);
01552             giraffe_table_delete(grating);
01553             giraffe_table_delete(fibers);
01554 
01555             giraffe_rebin_config_destroy(rebin_config);
01556 
01557             return 1;
01558 
01559         }
01560 
01561         filename = cpl_frame_get_filename(linemask_frame);
01562 
01563         status = giraffe_table_load(linemask, filename, 1, NULL);
01564 
01565         if (status) {
01566             cpl_msg_error(_id, "Cannot load line reference mask from '%s'. "
01567                           "Aborting ...", filename);
01568 
01569             giraffe_sgcalibration_config_destroy(siwc_config);
01570             siwc_config = NULL;
01571 
01572             giraffe_table_delete(linemask);
01573             linemask = NULL;
01574 
01575             giraffe_rebinning_destroy(rebinning);
01576 
01577             giraffe_extraction_destroy(extraction);
01578             giraffe_localization_destroy(localization);
01579 
01580             giraffe_table_delete(wcalcoeff);
01581 
01582             giraffe_table_delete(slitgeometry);
01583             giraffe_table_delete(grating);
01584             giraffe_table_delete(fibers);
01585 
01586             giraffe_rebin_config_destroy(rebin_config);
01587 
01588             return 1;
01589 
01590         }
01591 
01592 
01593         status = giraffe_compute_offsets(fibers, rebinning, grating,
01594                                          linemask, siwc_config);
01595 
01596         if (status != 0) {
01597             cpl_msg_error(_id, "Applying simultaneous wavelength "
01598                           "calibration correction failed! Aborting...");
01599 
01600             giraffe_sgcalibration_config_destroy(siwc_config);
01601             siwc_config = NULL;
01602 
01603             giraffe_table_delete(linemask);
01604             linemask = NULL;
01605 
01606             giraffe_rebinning_destroy(rebinning);
01607 
01608             giraffe_extraction_destroy(extraction);
01609             giraffe_localization_destroy(localization);
01610 
01611             giraffe_table_delete(wcalcoeff);
01612 
01613             giraffe_table_delete(slitgeometry);
01614             giraffe_table_delete(grating);
01615             giraffe_table_delete(fibers);
01616 
01617             giraffe_rebin_config_destroy(rebin_config);
01618 
01619             return 1;
01620 
01621         }
01622 
01623         giraffe_sgcalibration_config_destroy(siwc_config);
01624         siwc_config = NULL;
01625 
01626         giraffe_table_delete(linemask);
01627         linemask = NULL;
01628 
01629         giraffe_rebinning_destroy(rebinning);
01630         rebinning = giraffe_rebinning_new();
01631 
01632         status = giraffe_rebin_spectra(rebinning, extraction, fibers,
01633                                        localization, grating, slitgeometry,
01634                                        wcalcoeff, rebin_config);
01635 
01636         if (status) {
01637             cpl_msg_error(_id, "Rebinning of science spectra failed! "
01638                           "Aborting...");
01639 
01640             giraffe_rebinning_destroy(rebinning);
01641 
01642             giraffe_extraction_destroy(extraction);
01643             giraffe_localization_destroy(localization);
01644 
01645             giraffe_table_delete(wcalcoeff);
01646 
01647             giraffe_table_delete(slitgeometry);
01648             giraffe_table_delete(grating);
01649             giraffe_table_delete(fibers);
01650 
01651             giraffe_rebin_config_destroy(rebin_config);
01652 
01653             return 1;
01654 
01655         }
01656 
01657     }
01658 
01659     giraffe_extraction_destroy(extraction);
01660     extraction = NULL;
01661 
01662     giraffe_localization_destroy(localization);
01663     localization = NULL;
01664 
01665     giraffe_rebin_config_destroy(rebin_config);
01666     rebin_config = NULL;
01667 
01668 
01669     /*
01670      * Compute barycentric correction for each object spectrum (fiber)
01671      */
01672 
01673     status = giraffe_add_rvcorrection(fibers, rebinning->spectra);
01674 
01675     switch (status) {
01676     case 0:
01677         {
01678             break;
01679         }
01680 
01681     case 1:
01682         {
01683             cpl_msg_warning(_id, "Missing observation time properties! "
01684                             "Barycentric correction computation "
01685                             "skipped!");
01686             status = 0;
01687             break;
01688         }
01689     case 2:
01690         {
01691             cpl_msg_warning(_id, "Missing telescope location properties! "
01692                             "Barycentric correction computation "
01693                             "skipped!");
01694             status = 0;
01695             break;
01696         }
01697     case 3:
01698         {
01699             cpl_msg_warning(_id, "Object positions are not available "
01700                             "Barycentric correction computation "
01701                             "skipped!");
01702             status = 0;
01703             break;
01704         }
01705     default:
01706         {
01707             cpl_msg_error(_id, "Barycentric correction computation "
01708                           "failed! Aborting...");
01709 
01710             giraffe_rebinning_destroy(rebinning);
01711 
01712             giraffe_table_delete(wcalcoeff);
01713 
01714             giraffe_table_delete(slitgeometry);
01715             giraffe_table_delete(grating);
01716             giraffe_table_delete(fibers);
01717 
01718             return 1;
01719             break;
01720         }
01721 
01722     }
01723 
01724 
01725     /*
01726      * Save and register the results of the spectrum rebinning.
01727      */
01728 
01729     /* Rebinned spectra */
01730 
01731     giraffe_image_add_info(rebinning->spectra, &info, set);
01732 
01733     rbin_frame = giraffe_frame_create_image(rebinning->spectra,
01734                                             GIFRAME_SCIENCE_RBNSPECTRA,
01735                                             CPL_FRAME_LEVEL_FINAL,
01736                                             TRUE, TRUE);
01737 
01738     if (rbin_frame == NULL) {
01739         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01740 
01741         giraffe_rebinning_destroy(rebinning);
01742 
01743         giraffe_table_delete(wcalcoeff);
01744 
01745         giraffe_table_delete(slitgeometry);
01746         giraffe_table_delete(grating);
01747         giraffe_table_delete(fibers);
01748 
01749         return 1;
01750     }
01751 
01752     status = giraffe_fiberlist_attach(rbin_frame, fibers);
01753 
01754     if (status) {
01755         cpl_msg_error(_id, "Cannot attach fiber setup to local "
01756                       "file '%s'! Aborting ...",
01757                       cpl_frame_get_filename(rbin_frame));
01758 
01759         giraffe_rebinning_destroy(rebinning);
01760         giraffe_table_delete(wcalcoeff);
01761 
01762         giraffe_table_delete(slitgeometry);
01763         giraffe_table_delete(grating);
01764         giraffe_table_delete(fibers);
01765 
01766         cpl_frame_delete(rbin_frame);
01767 
01768         return 1;
01769     }
01770 
01771     cpl_frameset_insert(set, rbin_frame);
01772 
01773     /* Rebinned spectra errors */
01774 
01775     giraffe_image_add_info(rebinning->errors, &info, set);
01776 
01777     rbin_frame = giraffe_frame_create_image(rebinning->errors,
01778                                             GIFRAME_SCIENCE_RBNERRORS,
01779                                             CPL_FRAME_LEVEL_FINAL,
01780                                             TRUE, TRUE);
01781 
01782     if (rbin_frame == NULL) {
01783         cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01784 
01785         giraffe_rebinning_destroy(rebinning);
01786 
01787         giraffe_table_delete(wcalcoeff);
01788 
01789         giraffe_table_delete(slitgeometry);
01790         giraffe_table_delete(grating);
01791         giraffe_table_delete(fibers);
01792 
01793         return 1;
01794     }
01795 
01796     status = giraffe_fiberlist_attach(rbin_frame, fibers);
01797 
01798     if (status) {
01799         cpl_msg_error(_id, "Cannot attach fiber setup to local "
01800                       "file '%s'! Aborting ...",
01801                       cpl_frame_get_filename(rbin_frame));
01802 
01803         giraffe_rebinning_destroy(rebinning);
01804 
01805         giraffe_table_delete(wcalcoeff);
01806 
01807         giraffe_table_delete(slitgeometry);
01808         giraffe_table_delete(grating);
01809         giraffe_table_delete(fibers);
01810 
01811         cpl_frame_delete(rbin_frame);
01812 
01813         return 1;
01814     }
01815 
01816     cpl_frameset_insert(set, rbin_frame);
01817 
01818 
01819     /*
01820      * Optional image and data cube construction (only for IFU and Argus)
01821      */
01822 
01823     properties = giraffe_image_get_properties(rebinning->spectra);
01824     mode = giraffe_get_mode(properties);
01825 
01826 
01827     if (mode == GIMODE_IFU || mode == GIMODE_ARGUS) {
01828 
01829         cpl_frame* rimg_frame = NULL;
01830 
01831         GiFieldOfView* fov = NULL;
01832 
01833         GiFieldOfViewConfig* fov_config = NULL;
01834 
01835         GiFieldOfViewCubeFormat cube_format = GIFOV_FORMAT_ESO3D;
01836 
01837 
01838         fov_config = giraffe_fov_config_create(config);
01839 
01840         cube_format = fov_config->format;
01841 
01842 
01843         cpl_msg_info(_id, "Reconstructing image and data cube from rebinned "
01844                      "spectra ...");
01845 
01846         fov = giraffe_fov_new();
01847 
01848         status = giraffe_fov_build(fov, rebinning, fibers, wcalcoeff, grating,
01849                                    slitgeometry, fov_config);
01850 
01851         if (status) {
01852 
01853             if (status == -2) {
01854                 cpl_msg_warning(_id, "No reconstructed image was built. "
01855                                 "Fiber list has no fiber position "
01856                                 "information.");
01857             }
01858             else {
01859                 cpl_msg_error(_id, "Image reconstruction failed! Aborting...");
01860 
01861                 giraffe_fov_delete(fov);
01862                 giraffe_rebinning_destroy(rebinning);
01863 
01864                 giraffe_table_delete(wcalcoeff);
01865 
01866                 giraffe_table_delete(slitgeometry);
01867                 giraffe_table_delete(grating);
01868                 giraffe_table_delete(fibers);
01869 
01870                 giraffe_fov_config_destroy(fov_config);
01871 
01872                 return 1;
01873             }
01874 
01875         }
01876 
01877         giraffe_fov_config_destroy(fov_config);
01878 
01879 
01880         /*
01881          * Save and register the results of the image reconstruction.
01882          */
01883 
01884         /* Reconstructed image */
01885 
01886         giraffe_image_add_info(fov->fov.spectra, &info, set);
01887 
01888         rimg_frame = giraffe_frame_create_image(fov->fov.spectra,
01889                                                 GIFRAME_SCIENCE_RCSPECTRA,
01890                                                 CPL_FRAME_LEVEL_FINAL,
01891                                                 TRUE, TRUE);
01892 
01893         if (rimg_frame == NULL) {
01894             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01895 
01896             giraffe_fov_delete(fov);
01897             giraffe_rebinning_destroy(rebinning);
01898 
01899             giraffe_table_delete(wcalcoeff);
01900 
01901             giraffe_table_delete(slitgeometry);
01902             giraffe_table_delete(grating);
01903             giraffe_table_delete(fibers);
01904 
01905             return 1;
01906         }
01907 
01908         cpl_frameset_insert(set, rimg_frame);
01909 
01910 
01911         /* Reconstructed image errors */
01912 
01913         giraffe_image_add_info(fov->fov.errors, &info, set);
01914 
01915         rimg_frame = giraffe_frame_create_image(fov->fov.errors,
01916                                                 GIFRAME_SCIENCE_RCERRORS,
01917                                                 CPL_FRAME_LEVEL_FINAL,
01918                                                 TRUE, TRUE);
01919 
01920         if (rimg_frame == NULL) {
01921             cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01922 
01923             giraffe_fov_delete(fov);
01924             giraffe_rebinning_destroy(rebinning);
01925 
01926             giraffe_table_delete(wcalcoeff);
01927 
01928             giraffe_table_delete(slitgeometry);
01929             giraffe_table_delete(grating);
01930             giraffe_table_delete(fibers);
01931 
01932             return 1;
01933         }
01934 
01935         cpl_frameset_insert(set, rimg_frame);
01936 
01937 
01938         /* Save data cubes according to format selection */
01939 
01940         if (cube_format == GIFOV_FORMAT_SINGLE) {
01941 
01942             /* Spectrum cube */
01943 
01944             if (fov->cubes.spectra != NULL) {
01945 
01946                 cxint component = 0;
01947 
01948                 GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
01949 
01950 
01951                 properties = giraffe_image_get_properties(rebinning->spectra);
01952                 properties = cpl_propertylist_duplicate(properties);
01953 
01954                 giraffe_add_frameset_info(properties, set, info.sequence);
01955 
01956                 rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_SPECTRA,
01957                                                   CPL_FRAME_LEVEL_FINAL,
01958                                                   properties,
01959                                                   fov,
01960                                                   &component,
01961                                                   creator);
01962 
01963                 cpl_propertylist_delete(properties);
01964                 properties = NULL;
01965 
01966                 if (rimg_frame == NULL) {
01967                     cpl_msg_error(_id, "Cannot create local file! Aborting ...");
01968 
01969                     giraffe_fov_delete(fov);
01970                     fov = NULL;
01971 
01972                     giraffe_rebinning_destroy(rebinning);
01973                     rebinning = NULL;
01974 
01975                     giraffe_table_delete(wcalcoeff);
01976                     wcalcoeff = NULL;
01977 
01978                     giraffe_table_delete(slitgeometry);
01979                     slitgeometry = NULL;
01980 
01981                     giraffe_table_delete(grating);
01982                     grating = NULL;
01983 
01984                     giraffe_table_delete(fibers);
01985                     fibers = NULL;
01986 
01987                     return 1;
01988                 }
01989 
01990                 status = giraffe_fiberlist_attach(rimg_frame, fibers);
01991 
01992                 if (status != 0) {
01993                     cpl_msg_error(_id, "Cannot attach fiber setup to local "
01994                                   "file '%s'! Aborting ...",
01995                                   cpl_frame_get_filename(rimg_frame));
01996 
01997                     cpl_frame_delete(rimg_frame);
01998 
01999                     giraffe_fov_delete(fov);
02000                     fov = NULL;
02001 
02002                     giraffe_rebinning_destroy(rebinning);
02003                     rebinning = NULL;
02004 
02005                     giraffe_table_delete(wcalcoeff);
02006                     wcalcoeff = NULL;
02007 
02008                     giraffe_table_delete(slitgeometry);
02009                     slitgeometry = NULL;
02010 
02011                     giraffe_table_delete(grating);
02012                     grating = NULL;
02013 
02014                     giraffe_table_delete(fibers);
02015                     fibers = NULL;
02016 
02017                     return 1;
02018                 }
02019 
02020                 cpl_frameset_insert(set, rimg_frame);
02021 
02022             }
02023 
02024             /* Error cube */
02025 
02026             if (fov->cubes.errors != NULL) {
02027 
02028                 cxint component = 1;
02029 
02030                 GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes;
02031 
02032 
02033                 properties = giraffe_image_get_properties(rebinning->errors);
02034                 properties = cpl_propertylist_duplicate(properties);
02035 
02036                 giraffe_add_frameset_info(properties, set, info.sequence);
02037 
02038                 rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE_ERRORS,
02039                                                   CPL_FRAME_LEVEL_FINAL,
02040                                                   properties,
02041                                                   fov,
02042                                                   &component,
02043                                                   creator);
02044 
02045                 cpl_propertylist_delete(properties);
02046                 properties = NULL;
02047 
02048                 if (rimg_frame == NULL) {
02049                     cpl_msg_error(_id, "Cannot create local file! Aborting ...");
02050 
02051                     giraffe_fov_delete(fov);
02052                     fov = NULL;
02053 
02054                     giraffe_rebinning_destroy(rebinning);
02055                     rebinning = NULL;
02056 
02057                     giraffe_table_delete(wcalcoeff);
02058                     wcalcoeff = NULL;
02059 
02060                     giraffe_table_delete(slitgeometry);
02061                     slitgeometry = NULL;
02062 
02063                     giraffe_table_delete(grating);
02064                     grating = NULL;
02065 
02066                     giraffe_table_delete(fibers);
02067                     fibers = NULL;
02068 
02069                     return 1;
02070                 }
02071 
02072                 status = giraffe_fiberlist_attach(rimg_frame, fibers);
02073 
02074                 if (status != 0) {
02075                     cpl_msg_error(_id, "Cannot attach fiber setup to local "
02076                                   "file '%s'! Aborting ...",
02077                                   cpl_frame_get_filename(rimg_frame));
02078 
02079                     cpl_frame_delete(rimg_frame);
02080 
02081                     giraffe_fov_delete(fov);
02082                     fov = NULL;
02083 
02084                     giraffe_rebinning_destroy(rebinning);
02085                     rebinning = NULL;
02086 
02087                     giraffe_table_delete(wcalcoeff);
02088                     wcalcoeff = NULL;
02089 
02090                     giraffe_table_delete(slitgeometry);
02091                     slitgeometry = NULL;
02092 
02093                     giraffe_table_delete(grating);
02094                     grating = NULL;
02095 
02096                     giraffe_table_delete(fibers);
02097                     fibers = NULL;
02098 
02099                     return 1;
02100                 }
02101 
02102                 cpl_frameset_insert(set, rimg_frame);
02103             }
02104 
02105         }
02106         else {
02107 
02108             /* Data Cube (ESO 3D format) */
02109 
02110             GiFrameCreator creator = (GiFrameCreator) giraffe_fov_save_cubes_eso3d;
02111 
02112             properties = giraffe_image_get_properties(rebinning->spectra);
02113             properties = cpl_propertylist_duplicate(properties);
02114 
02115             giraffe_add_frameset_info(properties, set, info.sequence);
02116 
02117             rimg_frame = giraffe_frame_create(GIFRAME_SCIENCE_CUBE,
02118                                               CPL_FRAME_LEVEL_FINAL,
02119                                               properties,
02120                                               fov,
02121                                               NULL,
02122                                               creator);
02123 
02124             cpl_propertylist_delete(properties);
02125             properties = NULL;
02126 
02127             if (rimg_frame == NULL) {
02128                 cpl_msg_error(_id, "Cannot create local file! Aborting ...");
02129 
02130                 giraffe_fov_delete(fov);
02131                 fov = NULL;
02132 
02133                 giraffe_rebinning_destroy(rebinning);
02134                 rebinning = NULL;
02135 
02136                 giraffe_table_delete(wcalcoeff);
02137                 wcalcoeff = NULL;
02138 
02139                 giraffe_table_delete(slitgeometry);
02140                 slitgeometry = NULL;
02141 
02142                 giraffe_table_delete(grating);
02143                 grating = NULL;
02144 
02145                 giraffe_table_delete(fibers);
02146                 fibers = NULL;
02147 
02148                 return 1;
02149             }
02150 
02151             status = giraffe_fiberlist_attach(rimg_frame, fibers);
02152 
02153             if (status != 0) {
02154                 cpl_msg_error(_id, "Cannot attach fiber setup to local "
02155                               "file '%s'! Aborting ...",
02156                               cpl_frame_get_filename(rimg_frame));
02157 
02158                 cpl_frame_delete(rimg_frame);
02159 
02160                 giraffe_fov_delete(fov);
02161                 fov = NULL;
02162 
02163                 giraffe_rebinning_destroy(rebinning);
02164                 rebinning = NULL;
02165 
02166                 giraffe_table_delete(wcalcoeff);
02167                 wcalcoeff = NULL;
02168 
02169                 giraffe_table_delete(slitgeometry);
02170                 slitgeometry = NULL;
02171 
02172                 giraffe_table_delete(grating);
02173                 grating = NULL;
02174 
02175                 giraffe_table_delete(fibers);
02176                 fibers = NULL;
02177 
02178                 return 1;
02179             }
02180 
02181             cpl_frameset_insert(set, rimg_frame);
02182 
02183         }
02184 
02185         giraffe_fov_delete(fov);
02186         fov = NULL;
02187 
02188     }
02189 
02190 
02191     /*
02192      * Cleanup
02193      */
02194 
02195     giraffe_table_delete(wcalcoeff);
02196 
02197     giraffe_table_delete(slitgeometry);
02198     giraffe_table_delete(grating);
02199     giraffe_table_delete(fibers);
02200 
02201     giraffe_rebinning_destroy(rebinning);
02202 
02203     return 0;
02204 
02205 }
02206 
02207 
02208 /*
02209  * Build table of contents, i.e. the list of available plugins, for
02210  * this module. This function is exported.
02211  */
02212 
02213 int
02214 cpl_plugin_get_info(cpl_pluginlist* list)
02215 {
02216 
02217     cpl_recipe* recipe = cx_calloc(1, sizeof *recipe);
02218     cpl_plugin* plugin = &recipe->interface;
02219 
02220 
02221     cpl_plugin_init(plugin,
02222                     CPL_PLUGIN_API,
02223                     GIRAFFE_BINARY_VERSION,
02224                     CPL_PLUGIN_TYPE_RECIPE,
02225                     "giscience",
02226                     "Process a science observation.",
02227                     "For detailed information please refer to the "
02228                     "GIRAFFE pipeline user manual.\nIt is available at "
02229                     "http://www.eso.org/pipelines.",
02230                     "Giraffe Pipeline",
02231                     PACKAGE_BUGREPORT,
02232                     giraffe_get_license(),
02233                     giscience_create,
02234                     giscience_exec,
02235                     giscience_destroy);
02236 
02237     cpl_pluginlist_append(list, plugin);
02238 
02239     return 0;
02240 
02241 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.10.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Thu Mar 7 14:11:02 2013 by doxygen 1.4.7 written by Dimitri van Heesch, © 1997-2004