crires_util_genconfig.c

00001 /* $Id: crires_util_genconfig.c,v 1.19 2011/03/22 09:17:12 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/03/22 09:17:12 $
00024  * $Revision: 1.19 $
00025  * $Name: crire-2_1_1 $
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_genconfig"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static int crires_util_genconfig_save(cpl_table *, const cpl_parameterlist *, 
00049         cpl_frameset *);
00050 
00051 static char crires_util_genconfig_description[] =
00052 "This recipe is used to generate the model configuration file.\n"
00053 "The sof file contains the names of the input ASCII file\n"
00054 "tagged with "CRIRES_UTIL_GENCONFIG_RAW".\n"
00055 "The ASCII file must contain five columns:\n"
00056 "1st: The best guess value\n"
00057 "2nd: The low limit\n"
00058 "3th: The high limit\n"
00059 "4th: The Flag to recompute or not\n"
00060 "5th: Name of the Parameter\n"
00061 "The ASCII files are in the catalogs/ directory of the CRIRES distribution.\n"
00062 "This recipe produces 1 file:\n"
00063 "First product:     the table with the configuration for the model.\n"
00064 "                   (PRO TYPE = "CRIRES_PROTYPE_MOD_CONF")\n" ;
00065 
00066 CRIRES_RECIPE_DEFINE(crires_util_genconfig,
00067         CRIRES_PARAM_CONFIG_MODE,
00068         "Generate spectrum calibration FITS tables",
00069         crires_util_genconfig_description) ;
00070 
00071 /*-----------------------------------------------------------------------------
00072                             Static variables
00073  -----------------------------------------------------------------------------*/
00074 
00075 static struct {
00076     /* Input */
00077     int         mode ; 
00078 } crires_util_genconfig_config ;
00079 
00080 /*-----------------------------------------------------------------------------
00081                                 Functions code
00082  -----------------------------------------------------------------------------*/
00083 
00084 /*----------------------------------------------------------------------------*/
00091 /*----------------------------------------------------------------------------*/
00092 static int crires_util_genconfig(
00093         cpl_frameset            *   framelist,
00094         const cpl_parameterlist *   parlist)
00095 {
00096     FILE            *   in ;
00097     char                line[1024];
00098     cpl_frame       *   cur_frame ;
00099     const char      *   cur_fname ;
00100     int                 nentries ;
00101     char                name[1024] ;
00102     double              best, low, high ;
00103     int                 flag ;
00104     cpl_table       *   tab ;
00105     int                 i ;
00106 
00107     /* Retrieve input parameters */
00108     crires_util_genconfig_config.mode = crires_parameterlist_get_int(parlist,
00109             RECIPE_STRING, CRIRES_PARAM_CONFIG_MODE) ;
00110 
00111     /* Identify the RAW and CALIB frames in the input frameset */
00112     if (crires_dfs_set_groups(framelist, NULL)) {
00113         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00114         return -1 ;
00115     }
00116   
00117     /* Get the config file name */
00118     cur_frame = cpl_frameset_get_frame(framelist, 0) ;
00119     cur_fname = cpl_frame_get_filename(cur_frame) ;
00120 
00121     /* Open the file */
00122     if ((in = fopen(cur_fname, "r")) == NULL) {
00123         cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
00124         return -1 ;
00125     }
00126 
00127     /* Count number of entries */
00128     nentries = 0 ;
00129     while (fgets(line, 1024, in) != NULL) {
00130         if (line[0] != '#' && sscanf(line, "%lg %lg %lg %d %s", 
00131                     &best, &low, &high, &flag, name) == 5)  nentries++ ;
00132     }
00133     if (nentries == 0) {
00134         cpl_msg_error(__func__, "No valid entryin the file") ;
00135         fclose(in) ;
00136         return -1 ;
00137     }
00138         
00139     /* Create the output table */
00140     tab = cpl_table_new(nentries) ;
00141     cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_BEST, CPL_TYPE_DOUBLE) ;
00142     cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_LOW, CPL_TYPE_DOUBLE) ;
00143     cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_HIGH, CPL_TYPE_DOUBLE) ;
00144     cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_FLAG, CPL_TYPE_INT) ;
00145     cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_NAME, CPL_TYPE_STRING) ;
00146     
00147     /* Fill the table */
00148     i = 0 ;
00149     rewind(in) ;
00150     while (fgets(line, 1024, in) != NULL) {
00151         if (line[0] != '#' && sscanf(line, "%lg %lg %lg %d %s", 
00152                     &best, &low, &high, &flag, name) == 5) {
00153             cpl_table_set_string(tab, CRIRES_COL_MODEL_CONF_NAME, i, name) ;
00154             cpl_table_set_double(tab, CRIRES_COL_MODEL_CONF_BEST, i, best) ;
00155             cpl_table_set_double(tab, CRIRES_COL_MODEL_CONF_LOW, i, low) ;
00156             cpl_table_set_double(tab, CRIRES_COL_MODEL_CONF_HIGH, i, high) ;
00157             cpl_table_set_int(tab, CRIRES_COL_MODEL_CONF_FLAG, i, flag) ;
00158             i++ ;
00159         }
00160     }
00161     fclose(in) ;
00162    
00163     /* Save the table */
00164     cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
00165     if (crires_util_genconfig_save(tab, parlist, framelist) == -1) {
00166         cpl_msg_error(__func__, "Cannot write the table") ;
00167         cpl_table_delete(tab) ;
00168         return -1 ;
00169     }
00170     cpl_table_delete(tab) ;
00171     return 0 ;
00172 }
00173 
00174 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 static int crires_util_genconfig_save(
00184         cpl_table               *   out_table,
00185         const cpl_parameterlist *   parlist,
00186         cpl_frameset            *   set)
00187 {
00188     cpl_propertylist    *   plist ;
00189     const char          *   procat ;
00190 
00191     if (crires_util_genconfig_config.mode == 1) 
00192         procat = CRIRES_CALPRO_MODEL_REFINE_CONF ;
00193     else if (crires_util_genconfig_config.mode == 2)
00194         procat = CRIRES_CALPRO_MODEL_CONFIG ;
00195     else {
00196         cpl_msg_error(__func__, "Unsupported mode") ;
00197         return -1 ;
00198     }
00199 
00200     plist = cpl_propertylist_new();
00201     cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
00202     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, procat) ;
00203     cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00204             CRIRES_PROTYPE_MOD_CONF) ;
00205     cpl_propertylist_append_string(plist, "ESO PRO CFG-IDENT",
00206             "AlwaysOK") ;
00207 
00208     if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
00209                 NULL, "crires_util_genconfig", plist, NULL, 
00210                 PACKAGE "/" PACKAGE_VERSION,
00211                 "crires_util_genconfig.fits") != CPL_ERROR_NONE) {
00212         cpl_msg_error(__func__, "Cannot save the table") ;
00213         return -1 ;
00214     }
00215     cpl_propertylist_delete(plist) ;
00216     
00217     /* Return */
00218     return 0 ;
00219 }

Generated on 22 Mar 2011 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1