sofi_util_genlines.c

00001 /* $Id: sofi_util_genlines.c,v 1.9 2012/01/12 12:52:56 llundin Exp $
00002  *
00003  * This file is part of the SOFI 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: llundin $
00023  * $Date: 2012/01/12 12:52:56 $
00024  * $Revision: 1.9 $
00025  * $Name: sofi-1_5_5 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 
00038 #include "irplib_utils.h"
00039 
00040 #include "sofi_utils.h"
00041 #include "sofi_pfits.h"
00042 #include "sofi_dfs.h"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static int sofi_util_genlines_create(cpl_plugin *);
00049 static int sofi_util_genlines_exec(cpl_plugin *);
00050 static int sofi_util_genlines_destroy(cpl_plugin *);
00051 static int sofi_util_genlines(cpl_parameterlist *, cpl_frameset *);
00052 static int sofi_util_genlines_save(cpl_table *, cpl_parameterlist *, 
00053         cpl_frameset *);
00054 
00055 /*-----------------------------------------------------------------------------
00056                             Static variables
00057  -----------------------------------------------------------------------------*/
00058 
00059 static struct {
00060     /* Inputs */
00061     int             fill_blanks;
00062     int             display;
00063     int             mode;
00064     double          wl_factor;
00065     /* Outputs */
00066 } sofi_util_genlines_config;
00067 
00068 static char sofi_util_genlines_description[] =
00069 "This recipe is used to generate spectrum calibration tables.\n"
00070 "The sof file contains the names of the input ASCII file\n"
00071 "tagged with "SOFI_UTIL_GENLINES_RAW".\n"
00072 "The ASCII file must contain two columns:\n"
00073 "1st: Wavelengths in increasing order (the unit is corrected by\n"
00074 "     the factor option to obtain nanometers).\n"
00075 "2nd: The atmospheric emission.\n"
00076 "The ASCII files are in the catalogs/ directory of the SOFI distribution.\n"
00077 "This recipe produces 1 file:\n"
00078 "First product:     the table with the lines.\n"
00079 "                   (PRO CATG = "SOFI_UTIL_GENLINES_OH_CAT") or\n"
00080 "                   (PRO CATG = "SOFI_UTIL_GENLINES_XE_CAT") or\n"
00081 "                   (PRO CATG = "SOFI_UTIL_GENLINES_NE_CAT")\n";
00082 
00083 /*-----------------------------------------------------------------------------
00084                                 Functions code
00085  -----------------------------------------------------------------------------*/
00086 
00087 /*----------------------------------------------------------------------------*/
00095 /*----------------------------------------------------------------------------*/
00096 int cpl_plugin_get_info(cpl_pluginlist * list)
00097 {
00098     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00099     cpl_plugin  *   plugin = &recipe->interface;
00100 
00101     cpl_plugin_init(plugin,
00102                     CPL_PLUGIN_API,
00103                     SOFI_BINARY_VERSION,
00104                     CPL_PLUGIN_TYPE_RECIPE,
00105                     "sofi_util_genlines",
00106                     "Generate spectrum calibration FITS tables",
00107                     sofi_util_genlines_description,
00108                     "Yves Jung",
00109                     "yjung@eso.org",
00110                     sofi_get_license(),
00111                     sofi_util_genlines_create,
00112                     sofi_util_genlines_exec,
00113                     sofi_util_genlines_destroy);
00114 
00115     cpl_pluginlist_append(list, plugin);
00116     
00117     return 0;
00118 }
00119 
00120 /*----------------------------------------------------------------------------*/
00129 /*----------------------------------------------------------------------------*/
00130 static int sofi_util_genlines_create(cpl_plugin * plugin)
00131 {
00132     cpl_recipe      * recipe;
00133     cpl_parameter   * p;
00134 
00135     /* Get the recipe out of the plugin */
00136     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00137         recipe = (cpl_recipe *)plugin;
00138     else return -1;
00139 
00140     /* Create the parameters list in the cpl_recipe object */
00141     recipe->parameters = cpl_parameterlist_new();
00142 
00143     /* Fill the parameters list */
00144     /* --fill_blanks */
00145     p = cpl_parameter_new_value("sofi.sofi_util_genlines.fill_blanks",
00146             CPL_TYPE_BOOL, "flag to fill blanks", "sofi.sofi_util_genlines",
00147             FALSE);
00148     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fill_blanks");
00149     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00150     cpl_parameterlist_append(recipe->parameters, p);
00151     /* --display */
00152     p = cpl_parameter_new_value("sofi.sofi_util_genlines.display",
00153             CPL_TYPE_BOOL, "flag to plot", "sofi.sofi_util_genlines",
00154             FALSE);
00155     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "display");
00156     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00157     cpl_parameterlist_append(recipe->parameters, p);
00158     /* --mode */
00159     p = cpl_parameter_new_value("sofi.sofi_util_genlines.mode",
00160             CPL_TYPE_INT, "1-OH, 2-XE, 3-NE", 
00161             "sofi.sofi_util_genlines", 1);
00162     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mode");
00163     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00164     cpl_parameterlist_append(recipe->parameters, p);
00165     /* --wl_factor */
00166     p = cpl_parameter_new_value("sofi.sofi_util_genlines.wl_factor",
00167             CPL_TYPE_DOUBLE, "The factor used to multiply the wl", 
00168             "sofi.sofi_util_genlines", 1.0);
00169     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wl_factor");
00170     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00171     cpl_parameterlist_append(recipe->parameters, p);
00172  
00173     return 0;
00174 }
00175 
00176 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 static int sofi_util_genlines_exec(cpl_plugin * plugin)
00184 {
00185     cpl_recipe  *   recipe;
00186 
00187     /* Get the recipe out of the plugin */
00188     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00189         recipe = (cpl_recipe *)plugin;
00190     else return -1;
00191 
00192     return sofi_util_genlines(recipe->parameters, recipe->frames);
00193 }
00194 
00195 /*----------------------------------------------------------------------------*/
00201 /*----------------------------------------------------------------------------*/
00202 static int sofi_util_genlines_destroy(cpl_plugin * plugin)
00203 {
00204     cpl_recipe  *   recipe;
00205 
00206     /* Get the recipe out of the plugin */
00207     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00208         recipe = (cpl_recipe *)plugin;
00209     else return -1;
00210 
00211     cpl_parameterlist_delete(recipe->parameters);
00212     return 0;
00213 }
00214 
00215 /*----------------------------------------------------------------------------*/
00224 /*----------------------------------------------------------------------------*/
00225 static int sofi_util_genlines(
00226         cpl_parameterlist   *   parlist,
00227         cpl_frameset        *   framelist)
00228 {
00229     cpl_bivector    *   bivec;
00230     double          *   pbivec_x;
00231     double          *   pbivec_y;
00232     cpl_bivector    *   bivec_fill;
00233     double          *   pbivec_fill_x;
00234     double          *   pbivec_fill_y;
00235     cpl_frame       *   cur_frame;
00236     int                 nvals, nb_new_vals;
00237     double              wavel;
00238     cpl_table       *   tab;
00239     cpl_parameter   *   par;
00240     int                 i;
00241 
00242     cpl_ensure_code(!cpl_error_get_code(), cpl_error_get_code());
00243 
00244     /* Initialise */
00245     par = NULL;
00246     
00247     /* Retrieve input parameters */
00248     /* Fill blanks */
00249     par = cpl_parameterlist_find(parlist, 
00250             "sofi.sofi_util_genlines.fill_blanks");
00251     sofi_util_genlines_config.fill_blanks = cpl_parameter_get_bool(par);
00252     /* Fill blanks */
00253     par = cpl_parameterlist_find(parlist,"sofi.sofi_util_genlines.display");
00254     sofi_util_genlines_config.display = cpl_parameter_get_bool(par);
00255     /* Mode */
00256     par = cpl_parameterlist_find(parlist,"sofi.sofi_util_genlines.mode");
00257     sofi_util_genlines_config.mode = cpl_parameter_get_int(par);
00258     /* Wavelength factor */
00259     par=cpl_parameterlist_find(parlist,"sofi.sofi_util_genlines.wl_factor");
00260     sofi_util_genlines_config.wl_factor = cpl_parameter_get_double(par);
00261    
00262     /* Identify the RAW and CALIB frames in the input frameset */
00263     if (sofi_dfs_set_groups(framelist)) {
00264         cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
00265         return -1;
00266     }
00267    
00268     /* Load the file */
00269     cur_frame = cpl_frameset_get_frame(framelist, 0);
00270     if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
00271         cpl_msg_error(cpl_func, "Cannot load the file in the bivector");
00272         return -1;
00273     }
00274     nvals = cpl_bivector_get_size(bivec);
00275     
00276      /* If fill_blanks is requested */
00277     if (sofi_util_genlines_config.fill_blanks) {
00278         nb_new_vals = 3 * nvals;
00279         bivec_fill = cpl_bivector_new(nb_new_vals);
00280         pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill);
00281         pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill);
00282         pbivec_x = cpl_bivector_get_x_data(bivec);
00283         pbivec_y = cpl_bivector_get_y_data(bivec);
00284         for (i=0 ; i<nvals ; i++) {
00285             wavel = pbivec_x[i] * sofi_util_genlines_config.wl_factor;
00286             pbivec_fill_x[3*i] = wavel - 0.01;
00287             pbivec_fill_y[3*i] = 0.0;
00288             pbivec_fill_x[3*i+1] = wavel;
00289             pbivec_fill_y[3*i+1] = pbivec_y[i];
00290             pbivec_fill_x[3*i+2] = wavel + 0.01;
00291             pbivec_fill_y[3*i+2] = 0.0;
00292         }
00293         cpl_bivector_delete(bivec);
00294         bivec = bivec_fill;
00295         bivec_fill = NULL;
00296         nvals = cpl_bivector_get_size(bivec);
00297     } else {
00298         cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
00299                 sofi_util_genlines_config.wl_factor);
00300     }
00301 
00302     /* Display if requested */
00303     if (sofi_util_genlines_config.display) {
00304         cpl_plot_bivector(
00305                 "set grid;set xlabel 'Wavelength (A)';set ylabel 'Emission';",
00306                 "t 'Catalog lines' w lines", "", bivec);
00307     }
00308     
00309     /* Allocate the data container */
00310     tab = cpl_table_new(nvals);
00311     cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
00312             SOFI_COL_WAVELENGTH);
00313     cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec), 
00314             SOFI_COL_EMISSION);
00315    
00316     /* Save the table */
00317     cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
00318     if (sofi_util_genlines_save(tab, parlist, framelist) == -1) {
00319         cpl_msg_error(cpl_func, "Cannot write the table");
00320         cpl_bivector_delete(bivec);
00321         cpl_table_unwrap(tab, SOFI_COL_WAVELENGTH);
00322         cpl_table_unwrap(tab, SOFI_COL_EMISSION);
00323         cpl_table_delete(tab);
00324         return -1;
00325     }
00326     cpl_bivector_delete(bivec);
00327     cpl_table_unwrap(tab, SOFI_COL_WAVELENGTH);
00328     cpl_table_unwrap(tab, SOFI_COL_EMISSION);
00329     cpl_table_delete(tab);
00330     return 0;
00331 }
00332 
00333 /*----------------------------------------------------------------------------*/
00341 /*----------------------------------------------------------------------------*/
00342 static int sofi_util_genlines_save(
00343         cpl_table           *   out_table,
00344         cpl_parameterlist   *   parlist,
00345         cpl_frameset        *   set)
00346 {
00347     char                    name_o[512];
00348     cpl_propertylist    *   plist;
00349     cpl_frame           *   product_frame;
00350 
00351     /* Set the file name */
00352     sprintf(name_o, "sofi_util_genlines_save.fits");
00353     cpl_msg_info(cpl_func, "Writing %s" , name_o);
00354 
00355     /* Get FITS header from reference file */
00356     plist = cpl_propertylist_new();
00357     cpl_propertylist_append_string(plist, "INSTRUME", "SOFI");
00358             
00359     /* Create product frame */
00360     product_frame = cpl_frame_new();
00361     cpl_frame_set_filename(product_frame, name_o);
00362     if (sofi_util_genlines_config.mode == 1) 
00363         cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_OH_CAT);
00364     else if (sofi_util_genlines_config.mode == 2)
00365         cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_XE_CAT);
00366     else if (sofi_util_genlines_config.mode == 3)
00367         cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_NE_CAT);
00368     else 
00369         cpl_frame_set_tag(product_frame, "UNKNOWN");
00370 
00371     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
00372     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
00373     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
00374 
00375     /* Add DataFlow keywords */
00376     if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00377             "sofi_util_genlines", PACKAGE "/" PACKAGE_VERSION,
00378         "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
00379         cpl_msg_warning(cpl_func, "Problem in the product DFS-compliance");
00380         cpl_error_reset();
00381     }
00382 
00383     /* Save the file */
00384     if (cpl_table_save(out_table, plist, NULL, name_o,
00385                 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00386         cpl_msg_error(cpl_func, "Cannot save the product");
00387         cpl_frame_delete(product_frame);
00388         cpl_propertylist_delete(plist);
00389         return -1;
00390     }
00391     cpl_propertylist_delete(plist);
00392 
00393     /* Log the saved file in the input frameset */
00394     cpl_frameset_insert(set, product_frame);
00395     /* Return */
00396     return 0;
00397 }

Generated on Thu Mar 7 16:21:06 2013 for SOFI Pipeline Reference Manual by  doxygen 1.5.8