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: crire-2_3_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 #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_frame(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(
00152                     cpl_frame_get_filename(cpl_frameset_get_frame(frameset,0)),
00153                     i+1)) == NULL) {
00154                 cpl_msg_error(__func__, "Cannot get the calibration") ;
00155                 cpl_msg_indent_less() ;
00156                 for (j=0 ; j<i ; j++) cpl_polynomial_delete(pol[j]) ;
00157                 return -1 ;
00158             }
00159         }
00160 
00161         /* Create the table */
00162         wl_tab[i] = crires_wlcalib_gen_wltab_one(
00163                 (const cpl_polynomial *)pol[i], -1, -1.0) ;
00164         if (pol[i] != NULL) cpl_polynomial_delete(pol[i]) ;
00165     }
00166     cpl_msg_indent_less() ;
00167 
00168     /* Create the map */
00169     wl_map = crires_wlcalib_gen_wlmap((const cpl_table **)wl_tab) ;
00170     
00171     /* Compute the QC parameters */
00172     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00173         crires_util_wlinit_config.qc_wlcent[i] =
00174             cpl_image_get(cpl_imagelist_get(wl_map, i),
00175                     512, 256, &pix) ;
00176         crires_util_wlinit_config.qc_wldisp[i] =
00177             ((cpl_image_get(cpl_imagelist_get(wl_map, i), 1024, 256, &pix)) -
00178              (cpl_image_get(cpl_imagelist_get(wl_map, i), 1, 256, &pix)))
00179             / 1023 ;
00180     }
00181     
00182     /* Save the products */
00183     cpl_msg_info(__func__, "Save the products") ;
00184     cpl_msg_indent_more() ;
00185     if (crires_util_wlinit_save(wl_map, (const cpl_table **)wl_tab, parlist, 
00186                 frameset)) {
00187         cpl_msg_error(__func__, "Cannot save the products") ;
00188         cpl_msg_indent_less() ;
00189         cpl_imagelist_delete(wl_map) ;
00190         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00191             if (wl_tab[i] != NULL) cpl_table_delete(wl_tab[i]) ;
00192         }
00193         return -1 ;
00194     }
00195     cpl_msg_indent_less() ;
00196     cpl_imagelist_delete(wl_map) ;
00197     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00198         if (wl_tab[i] != NULL) cpl_table_delete(wl_tab[i]) ;
00199     }
00200 
00201     /* Return */
00202     if (cpl_error_get_code()) return -1 ;
00203     else return 0 ;
00204 }
00205 
00206 /*----------------------------------------------------------------------------*/
00215 /*----------------------------------------------------------------------------*/
00216 static int crires_util_wlinit_save(
00217         const cpl_imagelist     *   ilist,
00218         const cpl_table         **  tab,
00219         const cpl_parameterlist *   parlist,
00220         cpl_frameset            *   set)
00221 {
00222     cpl_propertylist    **  qclists ;
00223     const cpl_frame     *   ref_frame ;
00224     cpl_propertylist    *   inputlist ;
00225     const char          *   recipe_name = "crires_util_wlinit" ;
00226     int                     i ;
00227 
00228     /* Get the reference frame */
00229     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00230 
00231     /* Create the QC lists */
00232     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00233     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00234         qclists[i] = cpl_propertylist_new() ;
00235         cpl_propertylist_append_double(qclists[i], "ESO QC CENTWL",
00236                 crires_util_wlinit_config.qc_wlcent[i]) ;
00237         cpl_propertylist_append_double(qclists[i], "ESO QC DISPWL",
00238                 crires_util_wlinit_config.qc_wldisp[i]) ;
00239 
00240         /* Propagate some keywords from input raw frame extensions */
00241         inputlist = cpl_propertylist_load_regexp(
00242                 cpl_frame_get_filename(ref_frame), i+1,
00243                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00244         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00245                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00246         cpl_propertylist_delete(inputlist) ;
00247     }
00248 
00249     /* Write the image */
00250     crires_image_save(set,
00251             parlist,
00252             set,
00253             ilist,
00254             recipe_name,
00255             CRIRES_WL_MAP_IMA,
00256             CRIRES_PROTYPE_WL_MAP,
00257             crires_util_wlinit_config.period,
00258             NULL,
00259             (const cpl_propertylist **)qclists,
00260             PACKAGE "/" PACKAGE_VERSION,
00261             "crires_util_wlinit_ima.fits") ;
00262 
00263     /* Write the table */
00264     crires_table_save(set,
00265             parlist,
00266             set,
00267             tab,
00268             recipe_name,
00269             CRIRES_CALPRO_WAVE,
00270             CRIRES_PROTYPE_WL_POLY,
00271             NULL,
00272             (const cpl_propertylist **)qclists,
00273             PACKAGE "/" PACKAGE_VERSION,
00274             "crires_util_wlinit_tab.fits") ;
00275 
00276     /* Free and return */
00277     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00278         cpl_propertylist_delete(qclists[i]) ;
00279     }
00280     cpl_free(qclists) ;
00281     return  0;
00282 }
00283 

Generated on 9 Oct 2012 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1