KMOS Pipeline Reference Manual  1.3.10
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     cpl_parameter *p;
00130 
00131     /* Check that the plugin is part of a valid recipe */
00132     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00133         recipe = (cpl_recipe *)plugin;
00134     else
00135         return -1;
00136 
00137     /* Create the parameters list in the cpl_recipe object */
00138     recipe->parameters = cpl_parameterlist_new();
00139 
00140     /* Fill the parameters list */
00141 
00142     return 0 ;
00143 }
00144 
00145 /*----------------------------------------------------------------------------*/
00151 /*----------------------------------------------------------------------------*/
00152 static int kmos_gen_reflines_exec(cpl_plugin *plugin)
00153 {
00154     cpl_recipe  *recipe;
00155 
00156     /* Get the recipe out of the plugin */
00157     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00158         recipe = (cpl_recipe *)plugin;
00159     else return -1;
00160 
00161     return kmos_gen_reflines(recipe->parameters, recipe->frames);
00162 }
00163 
00164 /*----------------------------------------------------------------------------*/
00170 /*----------------------------------------------------------------------------*/
00171 static int kmos_gen_reflines_destroy(cpl_plugin *plugin)
00172 {
00173     cpl_recipe *recipe;
00174 
00175     /* Get the recipe out of the plugin */
00176     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00177         recipe = (cpl_recipe *)plugin;
00178     else return -1 ;
00179 
00180     cpl_parameterlist_delete(recipe->parameters);
00181     return 0 ;
00182 }
00183 
00184 /*----------------------------------------------------------------------------*/
00191 /*----------------------------------------------------------------------------*/
00192 static int kmos_gen_reflines(
00193         const cpl_parameterlist *   parlist,
00194         cpl_frameset            *   framelist)
00195 {
00196     FILE            *   in ;
00197     char                line[1024];
00198     cpl_frame       *   cur_frame ;
00199     const char      *   cur_fname ;
00200     int                 nentries ;
00201     char                band[1024] ;
00202     int                 det, ref, offset, range, cut ;
00203     double              wave ;
00204     cpl_table       *   tab ;
00205     int                 i ;
00206 
00207     /* Retrieve input parameters */
00208 
00209     /* Identify the RAW and CALIB frames in the input frameset */
00210     if (kmo_dfs_set_groups(framelist, "kmos_gen_reflines") != 1) {
00211         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00212         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00213         return -1 ;
00214     }
00215   
00216     /* Get the config file name */
00217     cur_frame = cpl_frameset_get_position(framelist, 0) ;
00218     cur_fname = cpl_frame_get_filename(cur_frame) ;
00219 
00220     /* Open the file */
00221     if ((in = fopen(cur_fname, "r")) == NULL) {
00222         cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
00223         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00224         return -1 ;
00225     }
00226 
00227     /* Count number of entries */
00228     nentries = 0 ;
00229     while (fgets(line, 1024, in) != NULL) {
00230         if (line[0] != '#' && sscanf(line, "%s %d %lg %d %d %d %d", 
00231                     band, &det, &wave, &ref, &offset, &range, &cut) == 7)  
00232             nentries++ ;
00233     }
00234     if (nentries == 0) {
00235         cpl_msg_error(__func__, "No valid entry in the file") ;
00236         fclose(in) ;
00237         return -1 ;
00238     }
00239         
00240     /* Create the output table */
00241     tab = cpl_table_new(nentries) ;
00242     cpl_table_new_column(tab, "FILTER", CPL_TYPE_STRING) ;
00243     cpl_table_new_column(tab, "DETECTOR", CPL_TYPE_INT) ;
00244     cpl_table_new_column(tab, "WAVELENGTH", CPL_TYPE_DOUBLE) ;
00245     cpl_table_new_column(tab, "REFERENCE", CPL_TYPE_INT) ;
00246     cpl_table_new_column(tab, "OFFSET", CPL_TYPE_INT) ;
00247     cpl_table_new_column(tab, "RANGE", CPL_TYPE_INT) ;
00248     cpl_table_new_column(tab, "CUT", CPL_TYPE_INT) ;
00249     
00250     /* Fill the table */
00251     i = 0 ;
00252     rewind(in) ;
00253     while (fgets(line, 1024, in) != NULL) {
00254         if (line[0] != '#' && sscanf(line, "%s %d %lg %d %d %d %d", 
00255                     band, &det, &wave, &ref, &offset, &range, &cut) == 7) {
00256             cpl_table_set_string(tab, "FILTER", i, band) ;
00257             cpl_table_set_int(tab, "DETECTOR", i, det) ;
00258             cpl_table_set_double(tab, "WAVELENGTH", i, wave) ;
00259             cpl_table_set_int(tab, "REFERENCE", i, ref) ;
00260             cpl_table_set_int(tab, "OFFSET", i, offset) ;
00261             cpl_table_set_int(tab, "RANGE", i, range) ;
00262             cpl_table_set_int(tab, "CUT", i, cut) ;
00263             i++ ;
00264         }
00265     }
00266     fclose(in) ;
00267    
00268     /* Save the table */
00269     cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
00270     if (kmos_gen_reflines_save(tab, parlist, framelist) == -1) {
00271         cpl_msg_error(__func__, "Cannot write the table") ;
00272         cpl_table_delete(tab) ;
00273         return -1 ;
00274     }
00275     cpl_table_delete(tab) ;
00276     return 0 ;
00277 }
00278 
00279 /*----------------------------------------------------------------------------*/
00287 /*----------------------------------------------------------------------------*/
00288 static int kmos_gen_reflines_save(
00289         cpl_table               *   out_table,
00290         const cpl_parameterlist *   parlist,
00291         cpl_frameset            *   set)
00292 {
00293     cpl_propertylist    *   plist ;
00294     cpl_propertylist    *   plist_ext ;
00295 
00296     plist = cpl_propertylist_new();
00297     cpl_propertylist_append_string(plist, "INSTRUME", "KMOS") ;
00298     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, "REF_LINES") ;
00299     plist_ext = cpl_propertylist_new();
00300     cpl_propertylist_append_string(plist_ext, "EXTNAME", "LIST") ;
00301 
00302     if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
00303                 plist_ext, "kmos_gen_reflines", plist, NULL, 
00304                 PACKAGE "/" PACKAGE_VERSION,
00305                 "kmos_gen_reflines.fits") != CPL_ERROR_NONE) {
00306         cpl_msg_error(__func__, "Cannot save the table") ;
00307     cpl_propertylist_delete(plist) ;
00308     cpl_propertylist_delete(plist_ext) ;
00309         return -1 ;
00310     }
00311     cpl_propertylist_delete(plist) ;
00312     cpl_propertylist_delete(plist_ext) ;
00313     
00314     /* Return */
00315     return 0 ;
00316 }