Main Page   File List  

visir_util_spc_txt2fits.c

00001 /* $Id: visir_util_spc_txt2fits.c,v 1.24 2006/01/04 16:27:23 llundin Exp $
00002  *
00003  * This file is part of the VISIR Pipeline
00004  * Copyright (C) 2002,2003 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  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2006/01/04 16:27:23 $
00024  * $Revision: 1.24 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 
00038 #include "irplib_utils.h"
00039 
00040 #include "visir_utils.h"
00041 #include "visir_pfits.h"
00042 #include "visir_dfs.h"
00043 #include "visir_inputs.h"
00044 
00045 /*-----------------------------------------------------------------------------
00046                             Functions prototypes
00047  -----------------------------------------------------------------------------*/
00048 
00049 static int visir_util_spc_txt2fits_create(cpl_plugin *);
00050 static int visir_util_spc_txt2fits_exec(cpl_plugin *);
00051 static int visir_util_spc_txt2fits_destroy(cpl_plugin *);
00052 static int visir_util_spc_txt2fits(cpl_parameterlist *, cpl_frameset *);
00053 static int visir_util_spc_txt2fits_save(const cpl_table *,
00054                                         const cpl_parameterlist *,
00055                                         const char *,
00056                                         cpl_frameset *);
00057 
00058 /*-----------------------------------------------------------------------------
00059                             Static variables
00060  -----------------------------------------------------------------------------*/
00061 
00062 static const char * recipename = "visir_util_spc_txt2fits";
00063 
00064 static struct {
00065     /* Inputs */
00066     double qeff;
00067 
00068 } visir_util_spc_txt2fits_config;
00069 
00070 static char visir_util_spc_txt2fits_description[] =
00071 "This recipe shall be used to generate spectrum calibration tables.\n"
00072 "The sof file shall consist of 1 line with the name of an ASCII-file\n"
00073 "currently tagged with either " VISIR_SPC_LINES_ASCII " or "
00074  VISIR_SPC_QEFF_ASCII ".\n"
00075 "The file must comprise two columns:\n"
00076 "1st: Must be wavelengths in increasing order in units of meter\n"
00077 "2nd: For " VISIR_SPC_LINES_ASCII "-files must be the atmospheric emission, "
00078 "while\n"
00079 "     for " VISIR_SPC_QEFF_ASCII "-files must be the quantum efficiency of the "
00080 "detector.\n"
00081 "A " VISIR_SPC_LINES_ASCII "-file will generate a " VISIR_CALIB_LINES_SPC
00082 "-file, and \n"
00083 "a " VISIR_SPC_QEFF_ASCII "-file will generate a " VISIR_CALIB_QEFF_SPC
00084 "-file.\n"
00085 "The current " VISIR_CALIB_LINES_SPC "- and " VISIR_CALIB_QEFF_SPC
00086 "-files are\n"
00087 "generated using the ASCII-files in the catalogs/ directory of the VISIR\n"
00088 "source-code distribution.";
00089 
00090 /*-----------------------------------------------------------------------------
00091                                 Functions code
00092  -----------------------------------------------------------------------------*/
00093 
00094 /*----------------------------------------------------------------------------*/
00103 /*----------------------------------------------------------------------------*/
00104 int cpl_plugin_get_info(cpl_pluginlist * list)
00105 {
00106     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00107     cpl_plugin  *   plugin = &recipe->interface;
00108 
00109 
00110     if (cpl_plugin_init(plugin,
00111                     CPL_PLUGIN_API,
00112                     VISIR_BINARY_VERSION,
00113                     CPL_PLUGIN_TYPE_RECIPE,
00114                     recipename,
00115                     "Generate spectrum calibration FITS tables",
00116                     visir_util_spc_txt2fits_description,
00117                     "Lars Lundin",
00118                     PACKAGE_BUGREPORT,
00119                     visir_get_license(),
00120                     visir_util_spc_txt2fits_create,
00121                     visir_util_spc_txt2fits_exec,
00122                     visir_util_spc_txt2fits_destroy)) return 1;
00123 
00124     if (cpl_pluginlist_append(list, plugin)) return 1;
00125     
00126     return 0;
00127 }
00128 
00129 /*----------------------------------------------------------------------------*/
00138 /*----------------------------------------------------------------------------*/
00139 static int visir_util_spc_txt2fits_create(cpl_plugin * plugin)
00140 {
00141     cpl_recipe * recipe = (cpl_recipe *)plugin;
00142     cpl_parameter * p;
00143 
00144     /* Verify plugin type */
00145     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00146 
00147     /* Create the parameters list in the cpl_recipe object */
00148     recipe->parameters = cpl_parameterlist_new();
00149 
00150     /* Fill the parameters list */
00151     /* --qeff */
00152     p = cpl_parameter_new_value("visir.visir_util_spc_txt2fits.qeff", 
00153                                 CPL_TYPE_DOUBLE, "The overall Quantum-"
00154                                 "efficiency. The default value comes from "
00155                                 "PH. Galdemard measurements. This option is "
00156                                 "ignored for all but " VISIR_SPC_QEFF_ASCII
00157                                 "-tagged files, while the efficiencies in such "
00158                                 "files are multiplied by this overall "
00159                                 "efficiency",
00160                                 "visir.visir_util_spc_txt2fits", 0.72);
00161     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "qeff");
00162     cpl_parameterlist_append(recipe->parameters, p);
00163     
00164     return 0;
00165 }
00166 
00167 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 static int visir_util_spc_txt2fits_exec(cpl_plugin * plugin)
00175 {
00176     cpl_recipe * recipe = (cpl_recipe *)plugin;
00177 
00178     /* Verify plugin type */
00179     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00180 
00181     return visir_util_spc_txt2fits(recipe->parameters, recipe->frames);
00182 }
00183 
00184 /*----------------------------------------------------------------------------*/
00190 /*----------------------------------------------------------------------------*/
00191 static int visir_util_spc_txt2fits_destroy(cpl_plugin * plugin)
00192 {
00193     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00194 
00195     /* Verify plugin type */
00196     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00197     cpl_parameterlist_delete(recipe->parameters);
00198     return  0;
00199 }
00200 
00201 /*----------------------------------------------------------------------------*/
00211 /*----------------------------------------------------------------------------*/
00212 static int visir_util_spc_txt2fits(
00213         cpl_parameterlist     *   parlist,
00214         cpl_frameset    *   framelist)
00215 {
00216     const char      * label2;
00217     const char      * procat;
00218     cpl_frameset    * rawframes = NULL;
00219     const cpl_frame * frame;
00220     cpl_bivector    * spectrum = NULL;
00221     cpl_table       * tab = NULL;
00222     FILE            * stream = NULL;
00223     int               nvals;
00224     
00225 
00226     if (cpl_error_get_code()) return cpl_error_get_code();
00227 
00228     /* Identify the RAW frames in the input frameset */
00229     skip_if (visir_dfs_set_groups(framelist));
00230 
00231     /* Objects observation */
00232     rawframes = irplib_frameset_extract_regexp(framelist, "^("
00233                                               VISIR_SPC_LINES_ASCII "|"
00234                                               VISIR_SPC_QEFF_ASCII ")$");
00235     skip_if (rawframes == NULL);
00236 
00237     frame = cpl_frameset_get_first(rawframes);
00238 
00239     stream = fopen(cpl_frame_get_filename(frame), "r");
00240 
00241     skip_if (stream == NULL);
00242 
00243     spectrum = cpl_bivector_new(2481); /* Some 1st guess at actual length */
00244     skip_if( visir_bivector_load(spectrum, stream));
00245 
00246     if (strcmp(cpl_frame_get_tag(frame), VISIR_SPC_LINES_ASCII)) {
00247         /* The file is a quantum efficiency file */
00248 
00249         /* Retrieve input parameters */
00250         const cpl_parameter * par = cpl_parameterlist_find(parlist,
00251                                           "visir.visir_util_spc_txt2fits.qeff");
00252 
00253         visir_util_spc_txt2fits_config.qeff = cpl_parameter_get_double(par);
00254 
00255         if (visir_util_spc_txt2fits_config.qeff > 1) {
00256             cpl_msg_error(__func__, "--qeff efficiency may not exceed 1");
00257             skip_if (1);
00258         }
00259         if (visir_util_spc_txt2fits_config.qeff <= 0) {
00260             cpl_msg_error(__func__, "--qeff efficiency must be positive");
00261             skip_if (1);
00262         }
00263 
00264         procat = VISIR_SPEC_CAL_QEFF_PROCATG;
00265         label2 = "Efficiency";
00266 
00267        skip_if(cpl_vector_multiply_scalar(cpl_bivector_get_y(spectrum), 
00268                                           visir_util_spc_txt2fits_config.qeff));
00269 
00270     } else {
00271         /* The file is a sky lines file */
00272        procat = VISIR_SPEC_CAL_LINES_PROCATG;
00273        label2 = "Emission";
00274     }
00275 
00276     /* Get the number of values in the model */
00277     nvals = cpl_bivector_get_size(spectrum);
00278 
00279     /* Allocate the data container */
00280     tab = cpl_table_new(nvals);
00281 
00282     skip_if( tab == NULL );
00283 
00284     skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_x_data(spectrum),
00285                                    "Wavelength"));
00286     skip_if (cpl_table_set_column_unit(tab, "Wavelength", "m"));
00287 
00288     skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_y_data(spectrum),
00289                                    label2));
00290 
00291     skip_if (cpl_table_set_column_unit(tab, label2, "[0;1] (Unitless)"));
00292 
00293     /* Save the table */
00294     cpl_msg_info(__func__, "Saving the table with %d rows", nvals);
00295     if (visir_util_spc_txt2fits_save(tab, parlist, procat, framelist)) {
00296         cpl_msg_error(__func__, "Could not save products");
00297         skip_if(1);
00298     }
00299 
00300     end_skip;
00301 
00302     if (stream != NULL) fclose(stream);
00303     cpl_bivector_delete(spectrum);
00304     if (tab) {
00305         cpl_table_unwrap(tab, "Wavelength");
00306         cpl_table_unwrap(tab, label2);
00307         cpl_table_delete(tab);
00308     }
00309     cpl_frameset_delete(rawframes);
00310 
00311     return cpl_error_get_code();
00312 }
00313 
00314 /*----------------------------------------------------------------------------*/
00325 /*----------------------------------------------------------------------------*/
00326 static int visir_util_spc_txt2fits_save(
00327         const cpl_table         * tab,
00328         const cpl_parameterlist * parlist,
00329         const char              * procat,
00330         cpl_frameset            * set)
00331 {
00332 
00333     return visir_table_save(parlist, set, tab, recipename, procat, NULL, "");
00334 }

Generated on Mon Jan 23 12:16:37 2006 for VISIR Pipeline Reference Manual by doxygen1.2.18