GIRAFFE Pipeline Reference Manual

girebinn.c

00001 /* $Id: girebinn.c,v 1.11 2006/08/14 14:21:22 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: 2006/08/14 14:21:22 $
00024  * $Revision: 1.11 $
00025  * $Name: giraffe-2_5_3 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <cxtypes.h>
00033 #include <cxmessages.h>
00034 
00035 #include <cpl_recipe.h>
00036 #include <cpl_plugininfo.h>
00037 #include <cpl_parameterlist.h>
00038 #include <cpl_frameset.h>
00039 
00040 #include "gimessages.h"
00041 #include "giframe.h"
00042 #include "gifibers.h"
00043 #include "gibias.h"
00044 #include "giextract.h"
00045 #include "giutils.h"
00046 #include "giwavecalib.h"
00047 #include "girebinning.h"
00048 
00049 
00050 static cxint girebin(cpl_parlist *config, cpl_frameset *set);
00051 
00052 /*
00053  * Create the recipe instance, i.e. setup the parameter list for this
00054  * recipe and make it availble to the application using the interface.
00055  */
00056 
00057 static cxint
00058 girebin_create(cpl_plugin *plugin)
00059 {
00060 
00061     cpl_recipe *recipe = (cpl_recipe *)plugin;
00062 
00063     /*
00064      * We have to provide the option we accept to the application. We
00065      * need to setup our parameter list and hook it into the recipe
00066      * interface.
00067      */
00068 
00069     recipe->parameters = cpl_parlist_new();
00070     cx_assert(recipe->parameters != NULL);
00071 
00072     /*
00073      * Fill the parameter list.
00074      */
00075 
00076     /* Wavelength Rebinning */
00077     giraffe_rebinning_config_add(recipe->parameters);
00078 
00079     return 0;
00080 
00081 }
00082 
00083 /*
00084  * Execute the plugin instance given by the interface.
00085  */
00086 
00087 static cxint
00088 girebin_exec(cpl_plugin *plugin)
00089 {
00090 
00091     cpl_recipe *recipe = (cpl_recipe *)plugin;
00092 
00093     cx_assert(recipe->parameters != NULL);
00094     cx_assert(recipe->frames != NULL);
00095 
00096     return girebin(recipe->parameters, recipe->frames);
00097 
00098 }
00099 
00100 
00101 static cxint
00102 girebin_destroy(cpl_plugin *plugin)
00103 {
00104 
00105     cpl_recipe *recipe = (cpl_recipe *)plugin;
00106 
00107     /*
00108      * We just destroy what was created during the plugin initialization
00109      * phase, i.e. the parameter list. The frame set is managed by the
00110      * application which called us, so we must not touch it,
00111      */
00112 
00113     cpl_parlist_delete(recipe->parameters); recipe->parameters = NULL;
00114 
00115     return 0;
00116 
00117 }
00118 
00119 /*
00120  * The actual recipe starts here.
00121  */
00122 
00123 static cxint
00124 girebin(cpl_parlist *config, cpl_frameset *set)
00125 {
00126 
00127     /*************************************************************************
00128                                      Variables
00129     *************************************************************************/
00130 
00131     const cxchar* fctid = "girebin";
00132 
00133     cpl_frame *extrsp_frame    = NULL;
00134     cpl_frame *extrsperr_frame = NULL;
00135     cpl_frame *locy_frame      = NULL;
00136     cpl_frame *locw_frame      = NULL;
00137     cpl_frame *residuals_frame = NULL;
00138     cpl_frame *grating_frame   = NULL;
00139     cpl_frame *slitgeo_frame   = NULL;
00140 
00141 
00142     GiImage   *extrsp          = NULL;
00143     GiImage   *extrsperr       = NULL;
00144     GiImage   *locy            = NULL;
00145     GiImage   *locw            = NULL;
00146 
00147     GiTable   *grating         = NULL;
00148     GiTable   *slitgeo         = NULL;
00149     GiTable   *residuals       = NULL;
00150 
00151     GiLocalization *localization = NULL;
00152     GiExtraction   *extraction   = NULL;
00153 
00154     GiWcalSolution *wavcoeff = NULL;
00155 
00156     cxint           status = 0;
00157 
00158 
00159    /*************************************************************************
00160                                     Initialization
00161     *************************************************************************/
00162 
00163     if (!config) {
00164         cpl_msg_error(fctid, "Invalid parameter list! Aborting ...");
00165         return 1;
00166     }
00167 
00168     if (!set) {
00169         cpl_msg_error(fctid, "Invalid frame set! Aborting ...");
00170         return 1;
00171     }
00172 
00173     /*************************************************************************
00174                                     Preprocessing
00175     *************************************************************************/
00176 
00177     /*
00178      *  Verify the frame set contents
00179      */
00180 
00181     extrsp_frame = cpl_frameset_find(set, GIFRAME_ARC_LAMP_EXTSPECTRA);
00182 
00183     if (!extrsp_frame) {
00184         cpl_msg_info(fctid, "No extracted spectrum present in frame set.");
00185         return 1;
00186     }
00187 
00188     extrsperr_frame = cpl_frameset_find(set, GIFRAME_ARC_LAMP_EXTERRORS);
00189 
00190     if (!extrsperr_frame) {
00191         cpl_msg_info(fctid, "No extracted spectrum error present in "
00192                      "frame set.");
00193         return 1;
00194     }
00195 
00196     locy_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_CENTROID);
00197 
00198     if (!locy_frame) {
00199         cpl_msg_error(fctid, "No master localization (centroid position) "
00200                      "present in frame set.");
00201         return 1;
00202     }
00203 
00204     locw_frame = cpl_frameset_find(set, GIFRAME_LOCALIZATION_WIDTH);
00205 
00206     if (!locw_frame) {
00207         cpl_msg_error(fctid, "No master localization (spectrum width) "
00208                      "present in frame set.");
00209         return 1;
00210     }
00211 
00212     residuals_frame = cpl_frameset_find(set, GIFRAME_WAVELENGTH_SOLUTION);
00213 
00214     if (!residuals_frame) {
00215         cpl_msg_error(fctid, "No residuals frame present in frame set.");
00216         return 1;
00217     }
00218 
00219     grating_frame = cpl_frameset_find(set, GIFRAME_GRATING);
00220 
00221     if (!grating_frame) {
00222         cpl_msg_error(fctid, "No grating table present in frame set, "
00223                       "aborting...");
00224         return 1;
00225     }
00226 
00227     slitgeo_frame = cpl_frameset_find(set, GIFRAME_SLITGEOMETRY);
00228 
00229     if (!slitgeo_frame) {
00230         cpl_msg_error(fctid, "No static slit geometry table present in frame "
00231                      "set.");
00232         return 1;
00233     }
00234 
00235     /*************************************************************************
00236                                      Processing
00237     *************************************************************************/
00238 
00239     cpl_msg_info(fctid, "Recipe Step : Initialization");
00240 
00241     /*
00242      * Load spectrum localization.
00243      */
00244 
00245     localization = giraffe_localization_new();
00246 
00247     {
00248 
00249         const cxchar *filename = cpl_frame_get_filename(locy_frame);
00250 
00251         cxint status = 0;
00252 
00253         locy   = giraffe_image_new(CPL_TYPE_DOUBLE);
00254         status = giraffe_image_load(locy, filename, 0);
00255 
00256         if (status) {
00257             cpl_msg_error(fctid, "Cannot load localization centroid frame "
00258                           "from '%s'. Aborting ...", filename);
00259 
00260             giraffe_image_delete(locy);
00261             giraffe_localization_destroy(localization);
00262             return 1;
00263 
00264         }
00265     }
00266 
00267     {
00268 
00269         const cxchar *filename = cpl_frame_get_filename(locw_frame);
00270 
00271         cxint status = 0;
00272 
00273         locw  = giraffe_image_new(CPL_TYPE_DOUBLE);
00274         status = giraffe_image_load(locw, filename, 0);
00275 
00276         if (status) {
00277             cpl_msg_error(fctid, "Cannot load localization width frame "
00278                           "from '%s'. Aborting ...", filename);
00279 
00280             giraffe_image_delete(locw);
00281             giraffe_image_delete(locy);
00282             giraffe_localization_destroy(localization);
00283 
00284             return 1;
00285 
00286         }
00287     }
00288 
00289     localization->locy = locy;
00290     localization->locw = locw;
00291 
00292     /*
00293      * Load extracted spectrum.
00294      */
00295 
00296     extraction = giraffe_extraction_new();
00297 
00298     {
00299 
00300         const cxchar *filename = cpl_frame_get_filename(extrsp_frame);
00301 
00302         cxint status = 0;
00303 
00304         extrsp = giraffe_image_new(CPL_TYPE_DOUBLE);
00305         status = giraffe_image_load(extrsp, filename, 0);
00306 
00307         if (status) {
00308             cpl_msg_error(fctid, "Cannot load extracted spectrum from '%s'. "
00309                           "Aborting ...", filename);
00310 
00311             giraffe_image_delete(extrsp);
00312             giraffe_extraction_destroy(extraction);
00313             giraffe_image_delete(locw);
00314             giraffe_image_delete(locy);
00315             giraffe_localization_destroy(localization);
00316             return 1;
00317 
00318         }
00319     }
00320 
00321     {
00322 
00323         const cxchar *filename = cpl_frame_get_filename(extrsperr_frame);
00324 
00325         cxint status = 0;
00326 
00327         extrsperr = giraffe_image_new(CPL_TYPE_DOUBLE);
00328         status    = giraffe_image_load(extrsperr, filename, 0);
00329 
00330         if (status) {
00331             cpl_msg_error(fctid, "Cannot load extracted spectrum error "
00332                           "frame from '%s'. Aborting ...", filename);
00333 
00334             giraffe_image_delete(extrsperr);
00335             giraffe_image_delete(extrsp);
00336             giraffe_extraction_destroy(extraction);
00337             giraffe_image_delete(locw);
00338             giraffe_image_delete(locy);
00339             giraffe_localization_destroy(localization);
00340 
00341             return 1;
00342 
00343         }
00344     }
00345 
00346     extraction->spectra = extrsp;
00347     extraction->error   = extrsperr;
00348 
00349     /*
00350      * Load grating table.
00351      */
00352 
00353     {
00354 
00355         const cxchar *filename = cpl_frame_get_filename(grating_frame);
00356 
00357         cxint status = 0;
00358 
00359         grating = giraffe_table_new();
00360         status = giraffe_table_load(grating, filename, 1, NULL);
00361 
00362         if (status) {
00363             cpl_msg_error(fctid, "Cannot load grating table from '%s'. "
00364                           "Aborting ...", filename);
00365 
00366 
00367             giraffe_table_delete(grating);
00368             giraffe_image_delete(extrsperr);
00369             giraffe_image_delete(extrsp);
00370             giraffe_extraction_destroy(extraction);
00371             giraffe_image_delete(locw);
00372             giraffe_image_delete(locy);
00373             giraffe_localization_destroy(localization);
00374 
00375             return 1;
00376         }
00377     }
00378 
00379     /*
00380      * Load slitgeo table.
00381      */
00382 
00383     {
00384 
00385         const cxchar *filename = cpl_frame_get_filename(slitgeo_frame);
00386 
00387         cxint status = 0;
00388 
00389         slitgeo = giraffe_table_new();
00390         status = giraffe_table_load(slitgeo, filename, 1, NULL);
00391 
00392         if (status) {
00393             cpl_msg_error(fctid, "Cannot load slitgeo table from '%s'. "
00394                           "Aborting ...", filename);
00395 
00396             giraffe_table_delete(slitgeo);
00397             giraffe_table_delete(grating);
00398             giraffe_image_delete(extrsperr);
00399             giraffe_image_delete(extrsp);
00400             giraffe_extraction_destroy(extraction);
00401             giraffe_image_delete(locw);
00402             giraffe_image_delete(locy);
00403             giraffe_localization_destroy(localization);
00404 
00405             return 1;
00406         }
00407     }
00408 
00409     /*
00410      * Load wavecalibration solution.
00411      */
00412 
00413     {
00414 
00415         const cxchar *filename = cpl_frame_get_filename(residuals_frame);
00416 
00417         cxint status = 0;
00418 
00419         residuals = giraffe_table_new();
00420         status    = giraffe_table_load(residuals, filename, 1, NULL);
00421 
00422         if (status) {
00423             cpl_msg_error(fctid, "Cannot load residuals table from '%s'. "
00424                           "Aborting ...", filename);
00425 
00426             giraffe_table_delete(residuals);
00427             giraffe_table_delete(slitgeo);
00428             giraffe_table_delete(grating);
00429             giraffe_image_delete(extrsperr);
00430             giraffe_image_delete(extrsp);
00431             giraffe_extraction_destroy(extraction);
00432             giraffe_image_delete(locw);
00433             giraffe_image_delete(locy);
00434             giraffe_localization_destroy(localization);
00435 
00436             return 1;
00437 
00438         }
00439 
00440     }
00441 
00442     wavcoeff = giraffe_wcalsolution_create(residuals);
00443 
00444     {
00445 
00446         GiRebinningConfig *rebin_config;
00447         GiRebinning       *rebinning;
00448 
00449         rebinning = giraffe_rebinning_new();
00450 
00451         rebin_config = giraffe_rebinning_config_create(config);
00452 
00453         status =
00454             giraffe_rebin_spectra(rebinning, extraction, localization,
00455                                   grating, slitgeo, wavcoeff, rebin_config);
00456 
00457         status =
00458             giraffe_image_save_framedisk(
00459                 rebinning->spectra,
00460                 set,
00461                 "rebinned_spectra",
00462                 TRUE
00463             );
00464 
00465         status =
00466             giraffe_image_save_framedisk(
00467                 rebinning->errors,
00468                 set,
00469                 "rebinned_spectra_errors",
00470                 TRUE
00471             );
00472 
00473         giraffe_rebinning_destroy(rebinning);
00474         giraffe_rebinning_config_destroy(rebin_config);
00475 
00476     }
00477 
00478     giraffe_wcalsolution_delete(wavcoeff);
00479 
00480     /*************************************************************************
00481                                     Postprocessing
00482     *************************************************************************/
00483 
00484     /*************************************************************************
00485                                    Deinitialization
00486     *************************************************************************/
00487 
00488 
00489     giraffe_table_delete(grating); grating = NULL;
00490     giraffe_table_delete(slitgeo); slitgeo = NULL;
00491     giraffe_table_delete(residuals); residuals = NULL;
00492 
00493     giraffe_localization_destroy(localization);
00494     localization = NULL;
00495     locy = NULL;
00496     locw = NULL;
00497 
00498     giraffe_extraction_destroy(extraction);
00499     extrsp = NULL;
00500     extrsperr = NULL;
00501     extraction = NULL;
00502 
00503     return 0;
00504 
00505 }
00506 
00507 /*
00508  * Build table of contents, i.e. the list of available plugins, for
00509  * this module. This function is exported.
00510  */
00511 
00512 int
00513 cpl_plugin_get_info(cpl_pluginlist *list)
00514 {
00515 
00516     cpl_recipe *recipe = cx_calloc(1, sizeof *recipe);
00517     cpl_plugin *plugin = &recipe->interface;
00518 
00519     cpl_plugin_init(plugin,
00520                     CPL_PLUGIN_API,
00521                     GIRAFFE_BINARY_VERSION,
00522                     CPL_PLUGIN_TYPE_RECIPE,
00523                     "girebin",
00524                     "Rebinning",
00525                     "This recipe allows you to rebin an extracted spectra "
00526                     "frame along the spectra direction.",
00527                     "Michael Kiesgen",
00528                     "mkiesgen@eso.org",
00529                     giraffe_get_license(),
00530                     girebin_create,
00531                     girebin_exec,
00532                     girebin_destroy);
00533 
00534     cpl_pluginlist_append(list, plugin);
00535 
00536     return 0;
00537 
00538 }
00539 

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