KMOS Pipeline Reference Manual  1.3.11
kmos_gen_reflines.c
00001 /* 
00002  * This file is part of the KMOS Pipeline
00003  * Copyright (C) 2002,2003 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 /*-----------------------------------------------------------------------------
00025                                 Includes
00026  -----------------------------------------------------------------------------*/
00027 
00028 #include <string.h>
00029 #include <math.h>
00030 
00031 #include <cpl.h>
00032 
00033 #include "kmo_dfs.h"
00034 
00035 /*-----------------------------------------------------------------------------
00036                             Functions prototypes
00037  -----------------------------------------------------------------------------*/
00038 
00039 static int kmos_gen_reflines_save(cpl_table *, const cpl_parameterlist *, 
00040         cpl_frameset *);
00041 
00042 static int kmos_gen_reflines_create(cpl_plugin *);
00043 static int kmos_gen_reflines_exec(cpl_plugin *);
00044 static int kmos_gen_reflines_destroy(cpl_plugin *);
00045 static int kmos_gen_reflines(const cpl_parameterlist *, cpl_frameset *);
00046 
00047 /*-----------------------------------------------------------------------------
00048  *                          Static variables
00049  *----------------------------------------------------------------------------*/
00050 
00051 static char kmos_gen_reflines_description[] =
00052 "This recipe is used to generate the REFLINES calibration file.\n"
00053 "The sof file contains the name of the input ASCII file\n"
00054 "tagged with "KMOS_GEN_REFLINES_RAW".\n"
00055 "The ASCII file must contain seven columns like the output of:\n"
00056 "    dtfits -d -s ' ' kmos_wave_ref_table.fits\n"
00057 "The six column titles are:\n"
00058 "FILTER|DETECTOR|WAVELENGTH|REFERENCE| OFFSET|  RANGE|    CUT\n"
00059 "The entries are like:\n"
00060 "    HK       3   1.79196        0    210     20    577\n"
00061 "    HK       3   2.25365        4    427     15     71\n"
00062 "    HK       3   2.06129       -1   1313     50    140\n"
00063 "    HK       3   2.32666        4    594     15     32\n"
00064 "    IZ       1  0.912547       -1    775     80   4000\n"
00065 "    IZ       1  0.966044       -1   1150     80   2000\n"
00066 "    IZ       1   1.04729       -1   1730     80    200\n"
00067 "    IZ       1   1.06765        2    128     40     80\n"
00068 "...\n"
00069 "This recipe produces 1 file:\n"
00070 "First product:     the table with the configuration for the model.\n" ;
00071 
00072 /*----------------------------------------------------------------------------*/
00076 /*----------------------------------------------------------------------------*/
00077 
00080 /*-----------------------------------------------------------------------------
00081                                 Functions code
00082  -----------------------------------------------------------------------------*/
00083 
00084 /*----------------------------------------------------------------------------*/
00093 /*----------------------------------------------------------------------------*/
00094 int cpl_plugin_get_info(cpl_pluginlist *list)
00095 {
00096     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00097     cpl_plugin *plugin = &recipe->interface;
00098 
00099     cpl_plugin_init(plugin,
00100             CPL_PLUGIN_API,
00101             KMOS_BINARY_VERSION,
00102             CPL_PLUGIN_TYPE_RECIPE,
00103             "kmos_gen_reflines",
00104             "Create REFLINES calibration file",
00105             kmos_gen_reflines_description,
00106             "Yves Jung",
00107             "usd-help@eso.org",
00108             kmos_get_license(),
00109             kmos_gen_reflines_create,
00110             kmos_gen_reflines_exec,
00111             kmos_gen_reflines_destroy);
00112 
00113     cpl_pluginlist_append(list, plugin);
00114 
00115     return 0;
00116 }
00117 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 static int kmos_gen_reflines_create(cpl_plugin *plugin)
00127 {
00128     cpl_recipe *recipe;
00129 
00130     /* Check that the plugin is part of a valid recipe */
00131     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00132         recipe = (cpl_recipe *)plugin;
00133     else
00134         return -1;
00135 
00136     /* Create the parameters list in the cpl_recipe object */
00137     recipe->parameters = cpl_parameterlist_new();
00138 
00139     /* Fill the parameters list */
00140 
00141     return 0 ;
00142 }
00143 
00144 /*----------------------------------------------------------------------------*/
00150 /*----------------------------------------------------------------------------*/
00151 static int kmos_gen_reflines_exec(cpl_plugin *plugin)
00152 {
00153     cpl_recipe  *recipe;
00154 
00155     /* Get the recipe out of the plugin */
00156     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00157         recipe = (cpl_recipe *)plugin;
00158     else return -1;
00159 
00160     return kmos_gen_reflines(recipe->parameters, recipe->frames);
00161 }
00162 
00163 /*----------------------------------------------------------------------------*/
00169 /*----------------------------------------------------------------------------*/
00170 static int kmos_gen_reflines_destroy(cpl_plugin *plugin)
00171 {
00172     cpl_recipe *recipe;
00173 
00174     /* Get the recipe out of the plugin */
00175     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00176         recipe = (cpl_recipe *)plugin;
00177     else return -1 ;
00178 
00179     cpl_parameterlist_delete(recipe->parameters);
00180     return 0 ;
00181 }
00182 
00183 /*----------------------------------------------------------------------------*/
00190 /*----------------------------------------------------------------------------*/
00191 static int kmos_gen_reflines(
00192         const cpl_parameterlist *   parlist,
00193         cpl_frameset            *   framelist)
00194 {
00195     FILE            *   in ;
00196     char                line[1024];
00197     cpl_frame       *   cur_frame ;
00198     const char      *   cur_fname ;
00199     int                 nentries ;
00200     char                band[1024] ;
00201     int                 det, ref, offset, range, cut ;
00202     double              wave ;
00203     cpl_table       *   tab ;
00204     int                 i ;
00205 
00206     /* Retrieve input parameters */
00207 
00208     /* Identify the RAW and CALIB frames in the input frameset */
00209     if (kmo_dfs_set_groups(framelist, "kmos_gen_reflines") != 1) {
00210         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00211         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00212         return -1 ;
00213     }
00214   
00215     /* Get the config file name */
00216     cur_frame = cpl_frameset_get_position(framelist, 0) ;
00217     cur_fname = cpl_frame_get_filename(cur_frame) ;
00218 
00219     /* Open the file */
00220     if ((in = fopen(cur_fname, "r")) == NULL) {
00221         cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
00222         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00223         return -1 ;
00224     }
00225 
00226     /* Count number of entries */
00227     nentries = 0 ;
00228     while (fgets(line, 1024, in) != NULL) {
00229         if (line[0] != '#' && sscanf(line, "%s %d %lg %d %d %d %d", 
00230                     band, &det, &wave, &ref, &offset, &range, &cut) == 7)  
00231             nentries++ ;
00232     }
00233     if (nentries == 0) {
00234         cpl_msg_error(__func__, "No valid entry in the file") ;
00235         fclose(in) ;
00236         return -1 ;
00237     }
00238         
00239     /* Create the output table */
00240     tab = cpl_table_new(nentries) ;
00241     cpl_table_new_column(tab, "FILTER", CPL_TYPE_STRING) ;
00242     cpl_table_new_column(tab, "DETECTOR", CPL_TYPE_INT) ;
00243     cpl_table_new_column(tab, "WAVELENGTH", CPL_TYPE_DOUBLE) ;
00244     cpl_table_new_column(tab, "REFERENCE", CPL_TYPE_INT) ;
00245     cpl_table_new_column(tab, "OFFSET", CPL_TYPE_INT) ;
00246     cpl_table_new_column(tab, "RANGE", CPL_TYPE_INT) ;
00247     cpl_table_new_column(tab, "CUT", CPL_TYPE_INT) ;
00248     
00249     /* Fill the table */
00250     i = 0 ;
00251     rewind(in) ;
00252     while (fgets(line, 1024, in) != NULL) {
00253         if (line[0] != '#' && sscanf(line, "%s %d %lg %d %d %d %d", 
00254                     band, &det, &wave, &ref, &offset, &range, &cut) == 7) {
00255             cpl_table_set_string(tab, "FILTER", i, band) ;
00256             cpl_table_set_int(tab, "DETECTOR", i, det) ;
00257             cpl_table_set_double(tab, "WAVELENGTH", i, wave) ;
00258             cpl_table_set_int(tab, "REFERENCE", i, ref) ;
00259             cpl_table_set_int(tab, "OFFSET", i, offset) ;
00260             cpl_table_set_int(tab, "RANGE", i, range) ;
00261             cpl_table_set_int(tab, "CUT", i, cut) ;
00262             i++ ;
00263         }
00264     }
00265     fclose(in) ;
00266    
00267     /* Save the table */
00268     cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
00269     if (kmos_gen_reflines_save(tab, parlist, framelist) == -1) {
00270         cpl_msg_error(__func__, "Cannot write the table") ;
00271         cpl_table_delete(tab) ;
00272         return -1 ;
00273     }
00274     cpl_table_delete(tab) ;
00275     return 0 ;
00276 }
00277 
00278 /*----------------------------------------------------------------------------*/
00286 /*----------------------------------------------------------------------------*/
00287 static int kmos_gen_reflines_save(
00288         cpl_table               *   out_table,
00289         const cpl_parameterlist *   parlist,
00290         cpl_frameset            *   set)
00291 {
00292     cpl_propertylist    *   plist ;
00293     cpl_propertylist    *   plist_ext ;
00294 
00295     plist = cpl_propertylist_new();
00296     cpl_propertylist_append_string(plist, "INSTRUME", "KMOS") ;
00297     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, "REF_LINES") ;
00298     plist_ext = cpl_propertylist_new();
00299     cpl_propertylist_append_string(plist_ext, "EXTNAME", "LIST") ;
00300 
00301     if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
00302                 plist_ext, "kmos_gen_reflines", plist, NULL, 
00303                 PACKAGE "/" PACKAGE_VERSION,
00304                 "kmos_gen_reflines.fits") != CPL_ERROR_NONE) {
00305         cpl_msg_error(__func__, "Cannot save the table") ;
00306     cpl_propertylist_delete(plist) ;
00307     cpl_propertylist_delete(plist_ext) ;
00308         return -1 ;
00309     }
00310     cpl_propertylist_delete(plist) ;
00311     cpl_propertylist_delete(plist_ext) ;
00312     
00313     /* Return */
00314     return 0 ;
00315 }