GIRAFFE Pipeline Reference Manual

gifibers.c

00001 /* $Id$
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$
00023  * $Date$
00024  * $Revision$
00025  * $Name$
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <cxmemory.h>
00033 #include <cxmessages.h>
00034 #include <cxstrutils.h>
00035 
00036 #include <cpl_msg.h>
00037 #include <cpl_parameterlist.h>
00038 
00039 #include "giframe.h"
00040 #include "gifiberutils.h"
00041 #include "gifibers.h"
00042 
00043 
00079 GiTable *
00080 giraffe_fibers_select(const cpl_frame *frame, const GiTable *reference,
00081                       GiFibersConfig *config)
00082 {
00083 
00084     const cxchar *fctid = "giraffe_fibers_select";
00085 
00086     const cxchar *filename;
00087 
00088     cxint nspec = 0;
00089     cxint *spectra = NULL;
00090 
00091     cpl_table *_fibers;
00092 
00093     GiTable *fibers;
00094 
00095 
00096     if (!frame || !config) {
00097         return NULL;
00098     }
00099 
00100     filename = cpl_frame_get_filename(frame);
00101     cx_assert(filename != NULL);
00102 
00103 
00104     if (config->spectra && *config->spectra != '\0') {
00105 
00106         if (strcmp(config->spectra, "setup") == 0) {
00107 
00108             if (reference != NULL) {
00109                 spectra = giraffe_create_spectrum_selection(filename, reference,
00110                                                             &nspec);
00111             }
00112 
00113             if (!spectra) {
00114                 cpl_msg_error(fctid, "Invalid fiber setup!");
00115                 return NULL;
00116             }
00117 
00118         }
00119         else {
00120 
00121             spectra = giraffe_parse_spectrum_selection(config->spectra,
00122                                                        &nspec);
00123             if (!spectra) {
00124                 cpl_msg_error(fctid, "Invalid selection string `%s'!",
00125                               config->spectra);
00126                 return NULL;
00127             }
00128 
00129         }
00130 
00131         if (config->nspec > 0) {
00132 
00133             /*
00134              * Both, the number of spectra and a selection list were
00135              * given
00136              */
00137 
00138             if (config->nspec < nspec) {
00139 
00140                 spectra = cx_realloc(spectra,
00141                                      config->nspec * sizeof(cxint));
00142                 nspec = config->nspec;
00143 
00144                 cpl_msg_warning(fctid, "Requested number of spectra (%d) "
00145                                 "is less than number of listed spectra "
00146                                 "(%d). Using %d spectra.", config->nspec,
00147                                 nspec, config->nspec);
00148 
00149             }
00150             else {
00151                 if (config->nspec > nspec) {
00152 
00153                     cpl_msg_warning(fctid, "Number of requested spectra "
00154                                     "(%d) exceeds the number of listed "
00155                                     "spectra (%d). Using all spectra in "
00156                                     "the list!", config->nspec, nspec);
00157 
00158                 }
00159             }
00160         }
00161     }
00162     else {
00163 
00164         if (config->nspec > 0) {
00165 
00166             /*
00167              * No selection list, but the number of spectra to process
00168              * was given.
00169              */
00170 
00171             register cxint i;
00172 
00173             nspec = config->nspec;
00174             spectra = cx_malloc(nspec * sizeof(cxint));
00175 
00176             /*
00177              * Fiber positions in the image are counted starting from 1!
00178              */
00179 
00180             for (i = 0; i < nspec; i++) {
00181                 spectra[i] = i + 1;
00182             }
00183         }
00184     }
00185 
00186     _fibers = giraffe_fiberlist_create(filename, nspec, spectra);
00187 
00188     fibers = giraffe_table_new();
00189     giraffe_table_set(fibers, _fibers);
00190 
00191     cpl_table_delete(_fibers);
00192 
00193 
00194     /*
00195      * Cleanup
00196      */
00197 
00198     if (spectra) {
00199         cx_free(spectra);
00200     }
00201 
00202     return fibers;
00203 
00204 }
00205 
00206 
00225 GiTable *
00226 giraffe_fibers_setup(const cpl_frame *frame, const cpl_frame *reference)
00227 {
00228 
00229     const cxchar *fctid = "giraffe_fibers_setup";
00230 
00231     cxchar *filename = NULL;
00232 
00233     cpl_table *_fibers =NULL;
00234 
00235     GiTable *fibers = NULL;
00236 
00237 
00238     if (frame == NULL) {
00239         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00240         return NULL;
00241     }
00242 
00243     filename = (cxchar *)cpl_frame_get_filename(frame);
00244 
00245     if (filename == NULL) {
00246         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00247         return NULL;
00248     }
00249 
00250     _fibers = giraffe_fiberlist_create(filename, 0, NULL);
00251 
00252     if (_fibers == NULL) {
00253         return NULL;
00254     }
00255 
00256     fibers = giraffe_table_new();
00257     giraffe_table_set(fibers, _fibers);
00258 
00259     cpl_table_delete(_fibers);
00260     _fibers = NULL;
00261 
00262 
00263     /*
00264      * Associate the newly created fiber setup list with a reference list
00265      * if it was given.
00266      */
00267 
00268     if (reference != NULL) {
00269 
00270         cxint status;
00271 
00272         GiTable *rfibers = 0;
00273 
00274 
00275         filename = (cxchar *)cpl_frame_get_filename(reference);
00276 
00277         if (filename == NULL) {
00278             
00279             giraffe_table_delete(fibers);
00280             cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00281             return NULL;
00282             
00283         }
00284 
00285         rfibers = giraffe_fiberlist_load(filename, 1, GIFRAME_FIBER_SETUP);
00286 
00287         if (rfibers == NULL) {
00288 
00289             giraffe_table_delete(fibers);
00290             return NULL;
00291             
00292         }
00293 
00294         status = giraffe_fiberlist_associate(fibers, rfibers);
00295 
00296         if (status) {
00297             giraffe_table_delete(fibers);
00298             giraffe_table_delete(rfibers);
00299             
00300             return NULL;
00301         }
00302 
00303         giraffe_table_delete(rfibers);
00304 
00305     }
00306 
00307     return fibers;
00308 
00309 }
00310 
00311 
00322 GiFibersConfig *
00323 giraffe_fibers_config_create(cpl_parameterlist *list)
00324 {
00325 
00326     cpl_parameter *p;
00327 
00328     GiFibersConfig *config = NULL;
00329 
00330 
00331     if (!list) {
00332         return NULL;
00333     }
00334 
00335     config = cx_calloc(1, sizeof *config);
00336 
00337 
00338     /*
00339      * Some defaults
00340      */
00341 
00342     config->nspec = 0;
00343     config->spectra = NULL;
00344 
00345 
00346     p = cpl_parameterlist_find(list, "giraffe.fibers.nspectra");
00347     config->nspec = cpl_parameter_get_int(p);
00348 
00349 
00350     p = cpl_parameterlist_find(list, "giraffe.fibers.spectra");
00351     config->spectra = cx_strdup(cpl_parameter_get_string(p));
00352 
00353     return config;
00354 
00355 }
00356 
00357 
00370 void
00371 giraffe_fibers_config_destroy(GiFibersConfig *config)
00372 {
00373 
00374     if (config) {
00375         if (config->spectra) {
00376             cx_free(config->spectra);
00377             config->spectra = NULL;
00378         }
00379 
00380         cx_free(config);
00381     }
00382 
00383     return;
00384 }
00385 
00386 
00398 void
00399 giraffe_fibers_config_add(cpl_parameterlist *list)
00400 {
00401 
00402     cpl_parameter *p;
00403 
00404 
00405     if (!list) {
00406         return;
00407     }
00408 
00409     p = cpl_parameter_new_value("giraffe.fibers.spectra",
00410                                 CPL_TYPE_STRING,
00411                                 "Index list of spectra to use for "
00412                                 "localization (e.g. 2,10,30-40,55).",
00413                                 "giraffe.fibers",
00414                                 "");
00415     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fiber-splist");
00416     cpl_parameterlist_append(list, p);
00417 
00418     p = cpl_parameter_new_range("giraffe.fibers.nspectra",
00419                                 CPL_TYPE_INT,
00420                                 "Number of spectra to localize.",
00421                                 "giraffe.fibers",
00422                                 0, 0, CX_MAXINT - 1);
00423     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fiber-nspec");
00424     cpl_parameterlist_append(list, p);
00425 
00426     return;
00427 
00428 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.11.1.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri May 24 12:29:02 2013 by doxygen 1.4.7 written by Dimitri van Heesch, © 1997-2004