visir_util_spc_txt2fits.c

00001 /* $Id: visir_util_spc_txt2fits.c,v 1.46 2009/01/29 08:56:58 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: 2009/01/29 08:56:58 $
00024  * $Revision: 1.46 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 
00038 #include "visir_recipe.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041                             Recipe defines
00042  -----------------------------------------------------------------------------*/
00043 
00044 #define RECIPE_STRING "visir_util_spc_txt2fits"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Private Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 static cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset *,
00050                                                    const cpl_parameterlist *,
00051                                                    const char *,
00052                                                    const cpl_table *);
00053 
00054 VISIR_RECIPE_DEFINE(visir_util_spc_txt2fits, VISIR_PARAM_QEFF,
00055                     "Generate spectrum calibration FITS tables",
00056                     "This recipe shall be used to generate spectrum "
00057                     "calibration tables.\n"
00058                     "The sof file shall consist of 1 line with the name of an "
00059                     "ASCII-file\n"
00060                     "currently tagged with either " VISIR_SPC_LINES_ASCII " or "
00061                     VISIR_SPC_QEFF_ASCII ".\n"
00062                     "The file must comprise two columns:\n"
00063                     "1st: Must be wavelengths in increasing order in units "
00064                     "of meter\n"
00065                     "2nd: For " VISIR_SPC_LINES_ASCII "-files must be the "
00066                     "atmospheric emission, "
00067                     "while\n"
00068                     "     for " VISIR_SPC_QEFF_ASCII "-files must be the "
00069                     "quantum efficiency of "
00070                     "the detector.\n"
00071                     "A " VISIR_SPC_LINES_ASCII "-file will generate a "
00072                     VISIR_CALIB_LINES_SPC
00073                     "-file, and \n"
00074                     "a " VISIR_SPC_QEFF_ASCII "-file will generate a "
00075                     VISIR_CALIB_QEFF_SPC
00076                     "-file.\n"
00077                     "The current " VISIR_CALIB_LINES_SPC "- and "
00078                     VISIR_CALIB_QEFF_SPC
00079                     "-files are\n"
00080                     "generated using the ASCII-files in the catalogs/ "
00081                     "directory of the VISIR\n"
00082                     "source-code distribution.");
00083 
00084 /*-----------------------------------------------------------------------------
00085                             Static variables
00086  -----------------------------------------------------------------------------*/
00087 
00088 static struct {
00089     /* Inputs */
00090     double qeff;
00091 
00092 } visir_util_spc_txt2fits_config;
00093 
00094 /*----------------------------------------------------------------------------*/
00098 /*----------------------------------------------------------------------------*/
00099 
00100 /*-----------------------------------------------------------------------------
00101                                 Functions code
00102  -----------------------------------------------------------------------------*/
00103 
00104 /*----------------------------------------------------------------------------*/
00114 /*----------------------------------------------------------------------------*/
00115 static int visir_util_spc_txt2fits(cpl_frameset            * framelist,
00116                                    const cpl_parameterlist * parlist)
00117 {
00118     const char       * label2 = NULL; /* Avoid uninit warning */
00119     const char       * procat;
00120     irplib_framelist * allframes = NULL;
00121     irplib_framelist * rawframes = NULL;
00122     const cpl_frame  * frame;
00123     cpl_bivector     * spectrum = NULL;
00124     cpl_table        * tab = NULL;
00125     FILE             * stream = NULL;
00126     int                nvals;
00127     
00128 
00129     if (cpl_error_get_code()) return cpl_error_get_code();
00130 
00131     /* Identify the RAW frames in the input frameset */
00132     skip_if (visir_dfs_set_groups(framelist));
00133 
00134     /* Objects observation */
00135     allframes = irplib_framelist_cast(framelist);
00136     skip_if(allframes == NULL);
00137     rawframes = irplib_framelist_extract_regexp(allframes, "^("
00138                                                 VISIR_SPC_LINES_ASCII "|"
00139                                                 VISIR_SPC_QEFF_ASCII ")$",
00140                                                 CPL_FALSE);
00141     skip_if (rawframes == NULL);
00142 
00143     frame = irplib_framelist_get_const(rawframes, 0);
00144 
00145     stream = fopen(cpl_frame_get_filename(frame), "r");
00146 
00147     skip_if (stream == NULL);
00148 
00149     spectrum = cpl_bivector_new(2481); /* Some 1st guess at actual length */
00150     skip_if( visir_bivector_load(spectrum, stream));
00151 
00152     if (strcmp(cpl_frame_get_tag(frame), VISIR_SPC_LINES_ASCII)) {
00153         /* The file is a quantum efficiency file */
00154 
00155         /* Retrieve input parameters */
00156         visir_util_spc_txt2fits_config.qeff =
00157             visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_QEFF);
00158 
00159         if (visir_util_spc_txt2fits_config.qeff > 1) {
00160             cpl_msg_error(cpl_func, "--qeff efficiency may not exceed 1");
00161             skip_if (1);
00162         }
00163         if (visir_util_spc_txt2fits_config.qeff <= 0) {
00164             cpl_msg_error(cpl_func, "--qeff efficiency must be positive");
00165             skip_if (1);
00166         }
00167 
00168         procat = VISIR_SPEC_CAL_QEFF_PROCATG;
00169         label2 = "Efficiency";
00170 
00171        skip_if(cpl_vector_multiply_scalar(cpl_bivector_get_y(spectrum), 
00172                                           visir_util_spc_txt2fits_config.qeff));
00173 
00174     } else {
00175         /* The file is a sky lines file */
00176        procat = VISIR_SPEC_CAL_LINES_PROCATG;
00177        label2 = "Emission";
00178     }
00179 
00180     /* Get the number of values in the model */
00181     nvals = cpl_bivector_get_size(spectrum);
00182 
00183     /* Allocate the data container */
00184     tab = cpl_table_new(nvals);
00185 
00186     skip_if( tab == NULL );
00187 
00188     skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_x_data(spectrum),
00189                                    "Wavelength"));
00190     skip_if (cpl_table_set_column_unit(tab, "Wavelength", "m"));
00191 
00192     skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_y_data(spectrum),
00193                                    label2));
00194 
00195     skip_if (cpl_table_set_column_unit(tab, label2, "[0;1] (Unitless)"));
00196 
00197     /* Save the table */
00198     cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
00199     skip_if (visir_util_spc_txt2fits_save(framelist, parlist, procat, tab));
00200 
00201     end_skip;
00202 
00203     if (stream != NULL) fclose(stream);
00204     cpl_bivector_delete(spectrum);
00205     if (tab) {
00206         cpl_table_unwrap(tab, "Wavelength");
00207         cpl_table_unwrap(tab, label2);
00208         cpl_table_delete(tab);
00209     }
00210     irplib_framelist_delete(allframes);
00211     irplib_framelist_delete(rawframes);
00212 
00213     return cpl_error_get_code();
00214 }
00215 
00216 /*----------------------------------------------------------------------------*/
00227 /*----------------------------------------------------------------------------*/
00228 static
00229 cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset            * set,
00230                                             const cpl_parameterlist * parlist,
00231                                             const char              * procat,
00232                                             const cpl_table         * tab)
00233 {
00234 
00235     cpl_propertylist * applist = cpl_propertylist_new();
00236 
00237 
00238     bug_if(cpl_propertylist_append_string(applist, "INSTRUME", "VISIR"));
00239 
00240     skip_if (irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING, procat,
00241                                applist, NULL, visir_pipe_id,
00242                                RECIPE_STRING CPL_DFS_FITS));
00243 
00244     end_skip;
00245 
00246     cpl_propertylist_delete(applist);
00247 
00248     return cpl_error_get_code();
00249 }

Generated on Thu Mar 24 11:59:40 2011 for VISIR Pipeline Reference Manual by  doxygen 1.5.8