CRIRES Pipeline Reference Manual  2.3.2
crires_util_genlines.c
00001 /* $Id: crires_util_genlines.c,v 1.16 2011-11-24 08:27:46 yjung Exp $
00002  *
00003  * This file is part of the CRIRES 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: yjung $
00023  * $Date: 2011-11-24 08:27:46 $
00024  * $Revision: 1.16 $
00025  * $Name: not supported by cvs2svn $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "crires_recipe.h"
00037 
00038 /*-----------------------------------------------------------------------------
00039                                 Define
00040  -----------------------------------------------------------------------------*/
00041 
00042 #define RECIPE_STRING "crires_util_genlines"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static int crires_util_genlines_save(cpl_table *, const cpl_parameterlist *, 
00049         cpl_frameset *);
00050 
00051 static char crires_util_genlines_description[] =
00052 "This recipe is used to generate spectrum calibration tables.\n"
00053 "The sof file contains the names of the input ASCII file\n"
00054 "tagged with "CRIRES_UTIL_GENLINES_RAW".\n"
00055 "The ASCII file must contain two columns:\n"
00056 "1st: Wavelengths in increasing order (the unit is corrected by\n"
00057 "     the factor option to obtain nanometers).\n"
00058 "2nd: The atmospheric emission.\n"
00059 "The ASCII files are in the catalogs/ directory of the CRIRES distribution.\n"
00060 "This recipe produces 1 file:\n"
00061 "First product:     the table with the lines.\n"
00062 "                   (PRO TYPE = "CRIRES_PROTYPE_CATALOG")\n" ;
00063 
00064 CRIRES_RECIPE_DEFINE(crires_util_genlines,
00065         CRIRES_PARAM_FILL_BLANKS    |
00066         CRIRES_PARAM_PLOT           |
00067         CRIRES_PARAM_LINES_MODE     |
00068         CRIRES_PARAM_WL_FACTOR,
00069         "Generate spectrum calibration FITS tables",
00070         crires_util_genlines_description) ;
00071 
00072 /*-----------------------------------------------------------------------------
00073                             Static variables
00074  -----------------------------------------------------------------------------*/
00075 
00076 static struct {
00077     /* Inputs */
00078     int             fill_blanks ; 
00079     int             display ;
00080     int             mode ;
00081     double          wl_factor ;
00082     /* Outputs */
00083 } crires_util_genlines_config ;
00084 
00085 /*-----------------------------------------------------------------------------
00086                                 Functions code
00087  -----------------------------------------------------------------------------*/
00088 
00089 /*----------------------------------------------------------------------------*/
00098 /*----------------------------------------------------------------------------*/
00099 static int crires_util_genlines(
00100         cpl_frameset            *   framelist,
00101         const cpl_parameterlist *   parlist)
00102 {
00103     cpl_bivector    *   bivec ;
00104     double          *   pbivec_x ;
00105     double          *   pbivec_y ;
00106     cpl_bivector    *   bivec_fill ;
00107     double          *   pbivec_fill_x ;
00108     double          *   pbivec_fill_y ;
00109     cpl_frame       *   cur_frame ;
00110     int                 nvals, nb_new_vals ;
00111     double              wavel ;
00112     cpl_table       *   tab ;
00113     int                 i ;
00114 
00115     /* Retrieve input parameters */
00116     crires_util_genlines_config.fill_blanks = crires_parameterlist_get_bool(
00117             parlist, RECIPE_STRING, CRIRES_PARAM_FILL_BLANKS) ;
00118     crires_util_genlines_config.display = crires_parameterlist_get_bool(
00119                         parlist, RECIPE_STRING, CRIRES_PARAM_PLOT) ;
00120     crires_util_genlines_config.mode = crires_parameterlist_get_int(
00121             parlist, RECIPE_STRING, CRIRES_PARAM_LINES_MODE) ;
00122     crires_util_genlines_config.wl_factor = crires_parameterlist_get_double( 
00123             parlist, RECIPE_STRING, CRIRES_PARAM_WL_FACTOR) ;
00124    
00125     /* Identify the RAW and CALIB frames in the input frameset */
00126     if (crires_dfs_set_groups(framelist, NULL)) {
00127         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00128         return -1 ;
00129     }
00130    
00131     /* Load the file */
00132     cur_frame = cpl_frameset_get_position(framelist, 0) ;
00133     if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
00134         cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
00135         return -1 ;
00136     }
00137     nvals = cpl_bivector_get_size(bivec) ;
00138     
00139      /* If fill_blanks is requested */
00140     if (crires_util_genlines_config.fill_blanks) {
00141         nb_new_vals = 3 * nvals ;
00142         bivec_fill = cpl_bivector_new(nb_new_vals) ;
00143         pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill) ;
00144         pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill) ;
00145         pbivec_x = cpl_bivector_get_x_data(bivec) ;
00146         pbivec_y = cpl_bivector_get_y_data(bivec) ;
00147         for (i=0 ; i<nvals ; i++) {
00148             wavel = pbivec_x[i] * crires_util_genlines_config.wl_factor ;
00149             pbivec_fill_x[3*i] = wavel - 0.01 ;
00150             pbivec_fill_y[3*i] = 0.0 ;
00151             pbivec_fill_x[3*i+1] = wavel ;
00152             pbivec_fill_y[3*i+1] = pbivec_y[i] ;
00153             pbivec_fill_x[3*i+2] = wavel + 0.01 ;
00154             pbivec_fill_y[3*i+2] = 0.0 ;
00155         }
00156         cpl_bivector_delete(bivec);
00157         bivec = bivec_fill ;
00158         bivec_fill = NULL ;
00159         nvals = cpl_bivector_get_size(bivec) ;
00160     } else {
00161         cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
00162                 crires_util_genlines_config.wl_factor) ;
00163     }
00164 
00165     /* Display if requested */
00166     if (crires_util_genlines_config.display) {
00167         cpl_plot_bivector(
00168                 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00169                 "t 'Catalog lines' w lines", "", bivec);
00170     }
00171     
00172     /* Allocate the data container */
00173     tab = cpl_table_new(nvals) ;
00174     cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
00175             CRIRES_COL_WAVELENGTH) ;
00176     cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec), 
00177             CRIRES_COL_EMISSION) ;
00178    
00179     /* Save the table */
00180     cpl_msg_info(__func__, "Saving the table with %d rows", nvals) ;
00181     if (crires_util_genlines_save(tab, parlist, framelist) == -1) {
00182         cpl_msg_error(__func__, "Cannot write the table") ;
00183         cpl_bivector_delete(bivec) ;
00184         cpl_table_unwrap(tab, CRIRES_COL_WAVELENGTH) ;
00185         cpl_table_unwrap(tab, CRIRES_COL_EMISSION) ;
00186         cpl_table_delete(tab) ;
00187         return -1 ;
00188     }
00189     cpl_bivector_delete(bivec) ;
00190     cpl_table_unwrap(tab, CRIRES_COL_WAVELENGTH) ;
00191     cpl_table_unwrap(tab, CRIRES_COL_EMISSION) ;
00192     cpl_table_delete(tab) ;
00193     return 0 ;
00194 }
00195 
00196 /*----------------------------------------------------------------------------*/
00204 /*----------------------------------------------------------------------------*/
00205 static int crires_util_genlines_save(
00206         cpl_table               *   out_table,
00207         const cpl_parameterlist *   parlist,
00208         cpl_frameset            *   set)
00209 {
00210     cpl_propertylist    *   plist ;
00211     const char          *   procat ;
00212 
00213     if (crires_util_genlines_config.mode == 1) 
00214         procat = CRIRES_CALPRO_HITRAN_CAT ;
00215     else if (crires_util_genlines_config.mode == 2)
00216         procat = CRIRES_CALPRO_OH_CAT ;
00217     else if (crires_util_genlines_config.mode == 3)
00218         procat = CRIRES_CALPRO_THAR_CAT ;
00219     else if (crires_util_genlines_config.mode == 4)
00220         procat = CRIRES_CALPRO_N2O_CAT ;
00221     else if (crires_util_genlines_config.mode == 5)
00222         procat = CRIRES_CALPRO_MODEL_WAVEEG ;
00223     else 
00224         procat = "UNKNOWN" ;
00225 
00226     plist = cpl_propertylist_new();
00227     cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
00228     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, procat) ;
00229     cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, 
00230             CRIRES_PROTYPE_CATALOG) ;
00231 
00232     if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
00233                 NULL, "crires_util_genlines", plist, NULL,
00234                 PACKAGE "/" PACKAGE_VERSION,
00235                 "crires_util_genlines.fits") != CPL_ERROR_NONE) {
00236         cpl_msg_error(__func__, "Cannot save the table") ;
00237         return -1 ;
00238     }
00239     cpl_propertylist_delete(plist) ;
00240 
00241     /* Return */
00242     return 0 ;
00243 }