crires_util_wlassign.c

00001 /* $Id: crires_util_wlassign.c,v 1.33 2009/05/08 09:37: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: 2009/05/08 09:37:12 $
00024  * $Revision: 1.33 $
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 #include "crires_wlcalib.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041                                 Define
00042  -----------------------------------------------------------------------------*/
00043 
00044 #define RECIPE_STRING "crires_util_wlassign"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int crires_util_wlassign_save(const cpl_table **, 
00051         const cpl_parameterlist *, cpl_frameset *) ;
00052 
00053 static char crires_util_wlassign_description[] = 
00054 "This recipe accepts 2 parameters:\n"
00055 "First parameter:   the table of the extracted spectrum in pixels\n"
00056 "                   (PRO TYPE = "CRIRES_PROTYPE_SPEC_PIX")\n"
00057 "Second parameter:  the table with the wavelength solution(s)\n"
00058 "                   (PRO TYPE = "CRIRES_PROTYPE_WL_POLY")\n"
00059 "\n"
00060 "This recipe produces 1 file:\n"
00061 "First product:     the table with the extracted spectrum in wavelength.\n"
00062 "                   (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n" ;
00063 
00064 CRIRES_RECIPE_DEFINE(crires_util_wlassign,
00065         CRIRES_PARAM_DISPLAY,
00066         "Put the wavelength in the extracted table",
00067         crires_util_wlassign_description) ;
00068 
00069 /*-----------------------------------------------------------------------------
00070                             Static variables
00071  -----------------------------------------------------------------------------*/
00072 
00073 static struct {
00074     /* Inputs */
00075     int             display ;
00076     /* Outputs */
00077 } crires_util_wlassign_config ;
00078 
00079 /*-----------------------------------------------------------------------------
00080                                 Functions code
00081  -----------------------------------------------------------------------------*/
00082 
00083 /*----------------------------------------------------------------------------*/
00090 /*----------------------------------------------------------------------------*/
00091 static int crires_util_wlassign(
00092         cpl_frameset            *   frameset,
00093         const cpl_parameterlist *   parlist)
00094 {
00095     cpl_frame       *   fr ;
00096     cpl_table       *   ext_tab[CRIRES_NB_DETECTORS] ;
00097     cpl_polynomial  *   wl_pol[CRIRES_NB_DETECTORS] ;
00098     cpl_table       *   wl_sol ;
00099     int                 nbrows ;
00100     double              wl ;
00101     int                 i, j ;
00102     
00103     /* Retrieve input parameters */
00104     crires_util_wlassign_config.display = crires_parameterlist_get_int(parlist,
00105             RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00106     
00107     /* Identify the RAW and CALIB frames in the input frameset */
00108     if (crires_dfs_set_groups(frameset, NULL)) {
00109         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00110         return -1 ;
00111     }
00112 
00113     /* Loop on the CRIRES_NB_DETECTORS detectors */
00114     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00115         cpl_msg_info(__func__, "Wavelength assignment for chip %d", i+1) ;
00116         
00117         /* Initialise */
00118         ext_tab[i] = NULL ;
00119         wl_pol[i] = NULL ;
00120 
00121         /* The first frame must be the extracted spectrum in pixels */
00122         cpl_msg_info(__func__, "Load the table") ;
00123         cpl_msg_indent_more() ;
00124         fr = cpl_frameset_get_frame(frameset, 0);
00125         if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr), 
00126                         i+1, CRIRES_PROTYPE_SPEC_PIX, -1, -1, 0)) == NULL) {
00127             cpl_msg_warning(__func__, "Empty extension") ;
00128             cpl_msg_indent_less() ;
00129             continue ;
00130         }
00131         cpl_msg_indent_less() ;
00132 
00133         /* The second frame must be the wavelength solution */
00134         cpl_msg_info(__func__, "Wavelength retrieval") ;
00135         cpl_msg_indent_more() ;
00136         fr = cpl_frameset_get_frame(frameset, 1);
00137         if ((wl_sol = crires_load_table_check(cpl_frame_get_filename(fr), i+1, 
00138                         CRIRES_PROTYPE_WL_POLY, -1, -1, 0)) == NULL) {
00139             cpl_msg_error(__func__, "No wavelength solution found") ;
00140             cpl_msg_indent_less() ;
00141             for (j=0 ; j<i ; j++) {
00142                 if (ext_tab[j] != NULL) cpl_table_delete(ext_tab[j]) ;
00143             }
00144             return -1 ;
00145         }
00146         cpl_msg_indent_less() ;
00147 
00148         /* Generate the polynomial */
00149         cpl_msg_info(__func__, "Solution polynomial generation") ;
00150         wl_pol[i]=crires_wlcalib_gen_wlpoly(wl_sol) ;
00151         cpl_table_delete(wl_sol) ;
00152         
00153         /* Create the output table */
00154         cpl_msg_info(__func__, "Wavelength column computation") ;
00155         cpl_table_new_column(ext_tab[i], CRIRES_COL_WAVELENGTH,CPL_TYPE_DOUBLE);
00156         cpl_table_new_column(ext_tab[i], CRIRES_COL_WAVELENGTH_MODEL,
00157                 CPL_TYPE_DOUBLE);
00158         nbrows = cpl_table_get_nrow(ext_tab[i]) ;
00159         for (j=0 ; j<nbrows ; j++) {
00160             if (wl_pol[i] != NULL) {
00161                 wl = cpl_polynomial_eval_1d(wl_pol[i], (double)(j+1), NULL) ;
00162             } else {
00163                 wl = 0.0 ;
00164             }
00165             cpl_table_set_double(ext_tab[i], CRIRES_COL_WAVELENGTH, j, wl);
00166             cpl_table_set_double(ext_tab[i], CRIRES_COL_WAVELENGTH_MODEL, j, 
00167                     0.0);
00168         }
00169         if (wl_pol[i] != NULL) cpl_polynomial_delete(wl_pol[i]) ;
00170 
00171         /* Plot if requested */
00172         if(crires_util_wlassign_config.display == i+1) {
00173             irplib_table_plot(
00174 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity OPT (ADU/sec)';",
00175                     "t 'Extracted spectrum OPT' w lines", "", ext_tab[i],
00176                     CRIRES_COL_WAVELENGTH, CRIRES_COL_EXTRACT_INT_OPT) ;
00177             irplib_table_plot(
00178 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity RECT (ADU/sec)';",
00179                     "t 'Extracted spectrum RECT' w lines", "", ext_tab[i],
00180                     CRIRES_COL_WAVELENGTH, CRIRES_COL_EXTRACT_INT_RECT) ;
00181         }
00182     }
00183     
00184     /* Save the product */
00185     cpl_msg_info(__func__, "Save the product") ;
00186     cpl_msg_indent_more() ;
00187     if (crires_util_wlassign_save((const cpl_table **)ext_tab, parlist, 
00188                 frameset)) {
00189         cpl_msg_error(__func__, "Cannot save the product") ;
00190         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00191             if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00192         }
00193         cpl_msg_indent_less() ;
00194         return -1 ;
00195     }
00196     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00197         if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00198     }
00199     cpl_msg_indent_less() ;
00200 
00201     /* Return */
00202     if (cpl_error_get_code()) return -1 ;
00203     else return 0 ;
00204 }
00205 
00206 /*----------------------------------------------------------------------------*/
00214 /*----------------------------------------------------------------------------*/
00215 static int crires_util_wlassign_save(
00216         const cpl_table         **  out,
00217         const cpl_parameterlist *   parlist,
00218         cpl_frameset            *   set)
00219 {
00220     const char          *   recipe_name = "crires_util_wlassign" ;
00221 
00222     /* Write the table */
00223     crires_table_save(set,
00224             parlist,
00225             set,
00226             out,
00227             recipe_name,
00228             CRIRES_OBS_EXTRACT_WL_TAB,
00229             CRIRES_PROTYPE_SPEC_WL,
00230             NULL,
00231             NULL,
00232             PACKAGE "/" PACKAGE_VERSION,
00233             "crires_util_wlassign.fits") ;
00234 
00235     return  0;
00236 }

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