CRIRES Pipeline Reference Manual  2.3.2
crires_util_wlassign.c
00001 /* $Id: crires_util_wlassign.c,v 1.37 2012-09-20 13:09:54 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: 2012-09-20 13:09:54 $
00024  * $Revision: 1.37 $
00025  * $Name: not supported by cvs2svn $
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 #include "crires_wlcalib.h"
00038 
00039 /*-----------------------------------------------------------------------------
00040                                 Define
00041  -----------------------------------------------------------------------------*/
00042 
00043 #define RECIPE_STRING "crires_util_wlassign"
00044 
00045 /*-----------------------------------------------------------------------------
00046                             Functions prototypes
00047  -----------------------------------------------------------------------------*/
00048 
00049 static int crires_util_wlassign_save(const cpl_table **, 
00050         const cpl_parameterlist *, cpl_frameset *) ;
00051 
00052 static char crires_util_wlassign_description[] = 
00053 "This recipe accepts 2 parameters:\n"
00054 "First parameter:   the table of the extracted spectrum in pixels\n"
00055 "                   (PRO TYPE = "CRIRES_PROTYPE_SPEC_PIX")\n"
00056 "Second parameter:  the table with the wavelength solution(s)\n"
00057 "                   (PRO TYPE = "CRIRES_PROTYPE_WL_POLY")\n"
00058 "\n"
00059 "This recipe produces 1 file:\n"
00060 "First product:     the table with the extracted spectrum in wavelength.\n"
00061 "                   (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n" ;
00062 
00063 CRIRES_RECIPE_DEFINE(crires_util_wlassign,
00064         CRIRES_PARAM_DISPLAY,
00065         "Put the wavelength in the extracted table",
00066         crires_util_wlassign_description) ;
00067 
00068 /*-----------------------------------------------------------------------------
00069                             Static variables
00070  -----------------------------------------------------------------------------*/
00071 
00072 static struct {
00073     /* Inputs */
00074     int             display ;
00075     /* Outputs */
00076     int             win_mode ;
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_propertylist    *   plist ;
00097     const char          *   sval ;
00098     cpl_table           *   wl_tab ;
00099     cpl_image           *   wl_map ;
00100     cpl_table           *   ext_tab[CRIRES_NB_DETECTORS] ;
00101     int                     nbrows, pix, spec_pos ;
00102     double                  wl ;
00103     int                     i, j ;
00104    
00105     /* Initialise */
00106     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00107         ext_tab[i] = NULL ;
00108     }
00109 
00110     /* Retrieve input parameters */
00111     crires_util_wlassign_config.display = crires_parameterlist_get_int(parlist,
00112             RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00113     
00114     /* Identify the RAW and CALIB frames in the input frameset */
00115     if (crires_dfs_set_groups(frameset, NULL)) {
00116         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00117         return -1 ;
00118     }
00119 
00120     /* Windowing mode ? */
00121     fr = cpl_frameset_get_position(frameset, 0);
00122     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
00123         return -1 ;
00124     sval = crires_pfits_get_ncorrs(plist) ;
00125     if (!strcmp(sval, "FowlerNsampGRstWin")) {
00126         crires_util_wlassign_config.win_mode = 1 ;
00127     } else {
00128         crires_util_wlassign_config.win_mode = 0 ;
00129     }
00130     cpl_propertylist_delete(plist) ;
00131 
00132     /* Loop on the detectors */
00133     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00134         /* Skip some detectors in windowing mode */
00135         if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
00136                 (crires_util_wlassign_config.win_mode == 1)) {
00137             continue ;
00138         }
00139         cpl_msg_info(__func__, "Wavelength assignment for chip %d", i+1) ;
00140 
00141         /* First Read the Wavelength calibration from the second frame */
00142         cpl_msg_info(__func__, "Wavelength retrieval") ;
00143         fr = cpl_frameset_get_position(frameset, 1);
00144         if ((wl_tab=crires_load_table_check(cpl_frame_get_filename(fr), i+1, 
00145                         CRIRES_PROTYPE_WL_POLY, -1, -1, 0)) == NULL) {
00146             cpl_msg_error(__func__, "Cannot load Wavelength from chip %d",i+1) ;
00147             return -1 ;
00148         }
00149     
00150         /* Create the wave map */
00151         if ((wl_map = crires_wlcalib_gen_wlmap_one_chip(
00152                         (const cpl_table *)wl_tab)) == NULL) {
00153             cpl_msg_error(__func__, "Cannot compute the Wavelength Map") ;
00154             cpl_table_delete(wl_tab);
00155             return -1 ;
00156         }
00157         cpl_table_delete(wl_tab);
00158 
00159         /* The first frame must be the extracted spectrum in pixels */
00160         fr = cpl_frameset_get_position(frameset, 0);
00161         if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr), 
00162                         i+1, CRIRES_PROTYPE_SPEC_PIX, -1, -1, 0)) == NULL) {
00163             cpl_msg_warning(__func__, "Empty extension") ;
00164             continue ;
00165         }
00166 
00167         /* Create the output table */
00168         cpl_msg_info(__func__, "Wavelength column computation") ;
00169         cpl_table_new_column(ext_tab[i], CRIRES_COL_WAVELENGTH,CPL_TYPE_DOUBLE);
00170         cpl_table_new_column(ext_tab[i], CRIRES_COL_WAVELENGTH_MODEL,
00171                 CPL_TYPE_DOUBLE);
00172         nbrows = cpl_table_get_nrow(ext_tab[i]) ;
00173         spec_pos = crires_get_y_spec_position(cpl_frame_get_filename(fr), i+1);
00174         for (j=0 ; j<nbrows ; j++) {
00175             wl = cpl_image_get(wl_map, j+1, spec_pos, &pix) ;
00176             cpl_table_set_double(ext_tab[i], CRIRES_COL_WAVELENGTH, j, wl);
00177             cpl_table_set_double(ext_tab[i], CRIRES_COL_WAVELENGTH_MODEL, j, 
00178                     0.0);
00179         }
00180         cpl_image_delete(wl_map) ;
00181 
00182         /* Plot if requested */
00183         if(crires_util_wlassign_config.display == i+1) {
00184             cpl_plot_column(
00185 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity OPT (ADU/sec)';",
00186                     "t 'Extracted spectrum OPT' w lines", "", ext_tab[i],
00187                     CRIRES_COL_WAVELENGTH, CRIRES_COL_EXTRACT_INT_OPT) ;
00188             cpl_plot_column(
00189 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity RECT (ADU/sec)';",
00190                     "t 'Extracted spectrum RECT' w lines", "", ext_tab[i],
00191                     CRIRES_COL_WAVELENGTH, CRIRES_COL_EXTRACT_INT_RECT) ;
00192         }
00193     }
00194    
00195     /* Reconstruct chips in windowing mode using detector 2 */
00196     if (crires_util_wlassign_config.win_mode == 1) {
00197         if (ext_tab[1] != NULL) {
00198             ext_tab[0] = cpl_table_duplicate(ext_tab[1]) ;
00199             cpl_table_set_size(ext_tab[0], 0) ;
00200             ext_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(ext_tab[0]) ;
00201         }
00202     }
00203 
00204     /* Save the product */
00205     cpl_msg_info(__func__, "Save the product") ;
00206     cpl_msg_indent_more() ;
00207     if (crires_util_wlassign_save((const cpl_table **)ext_tab, parlist, 
00208                 frameset)) {
00209         cpl_msg_error(__func__, "Cannot save the product") ;
00210         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00211             if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00212         }
00213         cpl_msg_indent_less() ;
00214         return -1 ;
00215     }
00216     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00217         if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00218     }
00219     cpl_msg_indent_less() ;
00220 
00221     /* Return */
00222     if (cpl_error_get_code()) return -1 ;
00223     else return 0 ;
00224 }
00225 
00226 /*----------------------------------------------------------------------------*/
00234 /*----------------------------------------------------------------------------*/
00235 static int crires_util_wlassign_save(
00236         const cpl_table         **  out,
00237         const cpl_parameterlist *   parlist,
00238         cpl_frameset            *   set)
00239 {
00240     const char          *   recipe_name = "crires_util_wlassign" ;
00241 
00242     /* Write the table */
00243     crires_table_save(set,
00244             parlist,
00245             set,
00246             out,
00247             recipe_name,
00248             CRIRES_OBS_EXTRACT_WL_TAB,
00249             CRIRES_PROTYPE_SPEC_WL,
00250             NULL,
00251             NULL,
00252             PACKAGE "/" PACKAGE_VERSION,
00253             "crires_util_wlassign.fits") ;
00254 
00255     return  0;
00256 }