CRIRES Pipeline Reference Manual  2.3.2
crires_util_wlinit.c
00001 /* $Id: crires_util_wlinit.c,v 1.39 2012-09-20 12:06:34 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 12:06:34 $
00024  * $Revision: 1.39 $
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 
00038 #include "crires_wlcalib.h"
00039 #include "crires_wlestimate.h"
00040 
00041 /*-----------------------------------------------------------------------------
00042                                 Define
00043  -----------------------------------------------------------------------------*/
00044 
00045 #define RECIPE_STRING "crires_util_wlinit"
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Functions prototypes
00049  -----------------------------------------------------------------------------*/
00050 
00051 static int crires_util_wlinit_save(const cpl_imagelist *, const cpl_table **, 
00052         const cpl_parameterlist *, cpl_frameset *) ;
00053 
00054 static char crires_util_wlinit_description[] = 
00055 "This recipe accepts 1 parameters:\n"
00056 "First parameter:   Any FITS file containing the WLEN settings in its header.\n"
00057 "\n"
00058 "This recipe produces 2 files:\n"
00059 "First product:     the image with the wavelength values.\n"
00060 "                   (PRO TYPE = "CRIRES_PROTYPE_WL_MAP")\n"
00061 "Second product:    the table with the wavelength polynomial coefficients.\n"
00062 "                   (PRO TYPE = "CRIRES_PROTYPE_WL_POLY")\n" ;
00063 
00064 CRIRES_RECIPE_DEFINE(crires_util_wlinit,
00065         CRIRES_PARAM_WAVES,
00066         "Wavelength calibration from the initial guess",
00067         crires_util_wlinit_description) ;
00068 
00069 /*-----------------------------------------------------------------------------
00070                             Static variables
00071  -----------------------------------------------------------------------------*/
00072 
00073 static struct {
00074     /* Inputs */
00075     double                  wmin[CRIRES_NB_DETECTORS] ;
00076     double                  wmax[CRIRES_NB_DETECTORS] ;
00077     /* Outputs */
00078     crires_illum_period     period ;
00079     double                  qc_wlcent[CRIRES_NB_DETECTORS] ;
00080     double                  qc_wldisp[CRIRES_NB_DETECTORS] ;
00081 } crires_util_wlinit_config ;
00082 
00083 /*-----------------------------------------------------------------------------
00084                                 Functions code
00085  -----------------------------------------------------------------------------*/
00086 
00087 /*----------------------------------------------------------------------------*/
00094 /*----------------------------------------------------------------------------*/
00095 static int crires_util_wlinit(
00096         cpl_frameset            *   frameset,
00097         const cpl_parameterlist *   parlist)
00098 {
00099     const char          *   sval ;
00100     cpl_imagelist       *   wl_map ;
00101     cpl_table           *   wl_tab[CRIRES_NB_DETECTORS] ;
00102     cpl_polynomial      *   pol[CRIRES_NB_DETECTORS] ;
00103     int                     pix ;
00104     int                     i, j ;
00105     
00106     /* Retrieve input parameters */
00107     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00108             CRIRES_PARAM_WAVES) ;
00109     if (sscanf(sval, "%lg,%lg,%lg,%lg,%lg,%lg,%lg,%lg",
00110                     &crires_util_wlinit_config.wmin[0],
00111                     &crires_util_wlinit_config.wmax[0],
00112                     &crires_util_wlinit_config.wmin[1],
00113                     &crires_util_wlinit_config.wmax[1],
00114                     &crires_util_wlinit_config.wmin[2],
00115                     &crires_util_wlinit_config.wmax[2],
00116                     &crires_util_wlinit_config.wmin[3],
00117                     &crires_util_wlinit_config.wmax[3])!=2*CRIRES_NB_DETECTORS){
00118         return -1 ;
00119     }
00120 
00121     /* Identify the RAW and CALIB frames in the input frameset */
00122     if (crires_dfs_set_groups(frameset, NULL)) {
00123         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00124         return -1 ;
00125     }
00126 
00127     /* Get the detector illumination period */
00128     crires_util_wlinit_config.period =
00129         crires_get_detector_illum_period(
00130                 cpl_frame_get_filename(cpl_frameset_get_position(frameset, 0)));
00131     if (crires_util_wlinit_config.period == CRIRES_ILLUM_UNKNOWN) {
00132         cpl_msg_error(__func__,
00133                 "Cannot determine the detector illumination period") ;
00134         return -1 ;
00135     } else {
00136         crires_display_detector_illum(crires_util_wlinit_config.period) ;
00137     }
00138 
00139     /* Loop on the spectra to calibrate */
00140     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00141         cpl_msg_info(__func__, "Wavelength calibration for chip %d", i+1) ;
00142 
00143         /* Initialise */
00144         crires_util_wlinit_config.qc_wlcent[i] = -1.0 ;
00145         crires_util_wlinit_config.qc_wldisp[i] = -1.0 ;
00146 
00147         /* Get the wavelength estimate */
00148         if ((pol[i] = crires_wlestimate_compute(
00149                         crires_util_wlinit_config.wmin[i],
00150                         crires_util_wlinit_config.wmax[i])) == NULL) {
00151             if ((pol[i] = crires_wlestimate_get(cpl_frame_get_filename(
00152                         cpl_frameset_get_position(frameset,0)), i+1)) == NULL) {
00153                 cpl_msg_error(__func__, "Cannot get the calibration") ;
00154                 cpl_msg_indent_less() ;
00155                 for (j=0 ; j<i ; j++) cpl_polynomial_delete(pol[j]) ;
00156                 return -1 ;
00157             }
00158         }
00159 
00160         /* Create the table */
00161         wl_tab[i] = crires_wlcalib_gen_wltab_one(
00162                 (const cpl_polynomial *)pol[i], -1, -1.0) ;
00163         if (pol[i] != NULL) cpl_polynomial_delete(pol[i]) ;
00164     }
00165     cpl_msg_indent_less() ;
00166 
00167     /* Create the map */
00168     wl_map = crires_wlcalib_gen_wlmap((const cpl_table **)wl_tab) ;
00169     
00170     /* Compute the QC parameters */
00171     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00172         crires_util_wlinit_config.qc_wlcent[i] =
00173             cpl_image_get(cpl_imagelist_get(wl_map, i),
00174                     512, 256, &pix) ;
00175         crires_util_wlinit_config.qc_wldisp[i] =
00176             ((cpl_image_get(cpl_imagelist_get(wl_map, i), 1024, 256, &pix)) -
00177              (cpl_image_get(cpl_imagelist_get(wl_map, i), 1, 256, &pix)))
00178             / 1023 ;
00179     }
00180     
00181     /* Save the products */
00182     cpl_msg_info(__func__, "Save the products") ;
00183     cpl_msg_indent_more() ;
00184     if (crires_util_wlinit_save(wl_map, (const cpl_table **)wl_tab, parlist, 
00185                 frameset)) {
00186         cpl_msg_error(__func__, "Cannot save the products") ;
00187         cpl_msg_indent_less() ;
00188         cpl_imagelist_delete(wl_map) ;
00189         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00190             if (wl_tab[i] != NULL) cpl_table_delete(wl_tab[i]) ;
00191         }
00192         return -1 ;
00193     }
00194     cpl_msg_indent_less() ;
00195     cpl_imagelist_delete(wl_map) ;
00196     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00197         if (wl_tab[i] != NULL) cpl_table_delete(wl_tab[i]) ;
00198     }
00199 
00200     /* Return */
00201     if (cpl_error_get_code()) return -1 ;
00202     else return 0 ;
00203 }
00204 
00205 /*----------------------------------------------------------------------------*/
00214 /*----------------------------------------------------------------------------*/
00215 static int crires_util_wlinit_save(
00216         const cpl_imagelist     *   ilist,
00217         const cpl_table         **  tab,
00218         const cpl_parameterlist *   parlist,
00219         cpl_frameset            *   set)
00220 {
00221     cpl_propertylist    **  qclists ;
00222     const cpl_frame     *   ref_frame ;
00223     cpl_propertylist    *   inputlist ;
00224     const char          *   recipe_name = "crires_util_wlinit" ;
00225     int                     i ;
00226 
00227     /* Get the reference frame */
00228     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00229 
00230     /* Create the QC lists */
00231     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00232     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00233         qclists[i] = cpl_propertylist_new() ;
00234         cpl_propertylist_append_double(qclists[i], "ESO QC CENTWL",
00235                 crires_util_wlinit_config.qc_wlcent[i]) ;
00236         cpl_propertylist_append_double(qclists[i], "ESO QC DISPWL",
00237                 crires_util_wlinit_config.qc_wldisp[i]) ;
00238 
00239         /* Propagate some keywords from input raw frame extensions */
00240         inputlist = cpl_propertylist_load_regexp(
00241                 cpl_frame_get_filename(ref_frame), i+1,
00242                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00243         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00244                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00245         cpl_propertylist_delete(inputlist) ;
00246     }
00247 
00248     /* Write the image */
00249     crires_image_save(set,
00250             parlist,
00251             set,
00252             ilist,
00253             recipe_name,
00254             CRIRES_WL_MAP_IMA,
00255             CRIRES_PROTYPE_WL_MAP,
00256             crires_util_wlinit_config.period,
00257             NULL,
00258             (const cpl_propertylist **)qclists,
00259             PACKAGE "/" PACKAGE_VERSION,
00260             "crires_util_wlinit_ima.fits") ;
00261 
00262     /* Write the table */
00263     crires_table_save(set,
00264             parlist,
00265             set,
00266             tab,
00267             recipe_name,
00268             CRIRES_CALPRO_WAVE,
00269             CRIRES_PROTYPE_WL_POLY,
00270             NULL,
00271             (const cpl_propertylist **)qclists,
00272             PACKAGE "/" PACKAGE_VERSION,
00273             "crires_util_wlinit_tab.fits") ;
00274 
00275     /* Free and return */
00276     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00277         cpl_propertylist_delete(qclists[i]) ;
00278     }
00279     cpl_free(qclists) ;
00280     return  0;
00281 }
00282