crires_util_wlinit.c

00001 /* $Id: crires_util_wlinit.c,v 1.37 2010/04/23 09:00:27 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: 2010/04/23 09:00:27 $
00024  * $Revision: 1.37 $
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 #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 ;
00102     cpl_polynomial      *   pol[CRIRES_NB_DETECTORS] ;
00103     int                     i, j ;
00104     
00105     /* Retrieve input parameters */
00106     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00107             CRIRES_PARAM_WAVES) ;
00108     if (sscanf(sval, "%lg,%lg,%lg,%lg,%lg,%lg,%lg,%lg",
00109                     &crires_util_wlinit_config.wmin[0],
00110                     &crires_util_wlinit_config.wmax[0],
00111                     &crires_util_wlinit_config.wmin[1],
00112                     &crires_util_wlinit_config.wmax[1],
00113                     &crires_util_wlinit_config.wmin[2],
00114                     &crires_util_wlinit_config.wmax[2],
00115                     &crires_util_wlinit_config.wmin[3],
00116                     &crires_util_wlinit_config.wmax[3])!=2*CRIRES_NB_DETECTORS){
00117         return -1 ;
00118     }
00119 
00120     /* Identify the RAW and CALIB frames in the input frameset */
00121     if (crires_dfs_set_groups(frameset, NULL)) {
00122         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00123         return -1 ;
00124     }
00125 
00126     /* Get the detector illumination period */
00127     crires_util_wlinit_config.period =
00128         crires_get_detector_illum_period(
00129                 cpl_frame_get_filename(cpl_frameset_get_frame(frameset, 0))) ;
00130     if (crires_util_wlinit_config.period == CRIRES_ILLUM_UNKNOWN) {
00131         cpl_msg_error(__func__,
00132                 "Cannot determine the detector illumination period") ;
00133         return -1 ;
00134     } else {
00135         crires_display_detector_illum(crires_util_wlinit_config.period) ;
00136     }
00137 
00138     /* Loop on the spectra to calibrate */
00139     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00140         cpl_msg_info(__func__, "Wavelength calibration for chip %d", i+1) ;
00141 
00142         /* Initialise */
00143         pol[i] = NULL ;
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     cpl_msg_indent_less() ;
00162 
00163     /* Compute the QC parameters */
00164     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00165         if (pol[i] != NULL) {
00166             crires_util_wlinit_config.qc_wlcent[i] =
00167                 cpl_polynomial_eval_1d(pol[i], (double)512, NULL) ;
00168             crires_util_wlinit_config.qc_wldisp[i] =
00169                 (cpl_polynomial_eval_1d(pol[i], (double)1024, NULL) -
00170                  cpl_polynomial_eval_1d(pol[i], (double)1, NULL)) / 1024 ;
00171         }
00172     }
00173     
00174     /* Create output tables */
00175     wl_tab = crires_wlcalib_gen_wltab((const cpl_polynomial **)pol) ;
00176     wl_map = crires_wlcalib_gen_wlmap((const cpl_table **)wl_tab) ;
00177     
00178     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00179         if (pol[i] != NULL) cpl_polynomial_delete(pol[i]) ;
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             cpl_table_delete(wl_tab[i]) ;
00192         }
00193         cpl_free(wl_tab) ;
00194         return -1 ;
00195     }
00196     cpl_msg_indent_less() ;
00197     cpl_imagelist_delete(wl_map) ;
00198     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00199         cpl_table_delete(wl_tab[i]) ;
00200     }
00201     cpl_free(wl_tab) ;
00202 
00203     /* Return */
00204     if (cpl_error_get_code()) return -1 ;
00205     else return 0 ;
00206 }
00207 
00208 /*----------------------------------------------------------------------------*/
00217 /*----------------------------------------------------------------------------*/
00218 static int crires_util_wlinit_save(
00219         const cpl_imagelist     *   ilist,
00220         const cpl_table         **  tab,
00221         const cpl_parameterlist *   parlist,
00222         cpl_frameset            *   set)
00223 {
00224     cpl_propertylist    **  qclists ;
00225     const cpl_frame     *   ref_frame ;
00226     cpl_propertylist    *   inputlist ;
00227     const char          *   recipe_name = "crires_util_wlinit" ;
00228     int                     i ;
00229 
00230     /* Get the reference frame */
00231     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00232 
00233     /* Create the QC lists */
00234     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00235     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00236         qclists[i] = cpl_propertylist_new() ;
00237         cpl_propertylist_append_double(qclists[i], "ESO QC CENTWL",
00238                 crires_util_wlinit_config.qc_wlcent[i]) ;
00239         cpl_propertylist_append_double(qclists[i], "ESO QC DISPWL",
00240                 crires_util_wlinit_config.qc_wldisp[i]) ;
00241 
00242         /* Propagate some keywords from input raw frame extensions */
00243         inputlist = cpl_propertylist_load_regexp(
00244                 cpl_frame_get_filename(ref_frame), i+1,
00245                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00246         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00247                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00248         cpl_propertylist_delete(inputlist) ;
00249     }
00250 
00251     /* Write the image */
00252     crires_image_save(set,
00253             parlist,
00254             set,
00255             ilist,
00256             recipe_name,
00257             CRIRES_WL_MAP_IMA,
00258             CRIRES_PROTYPE_WL_MAP,
00259             crires_util_wlinit_config.period,
00260             NULL,
00261             (const cpl_propertylist **)qclists,
00262             PACKAGE "/" PACKAGE_VERSION,
00263             "crires_util_wlinit_ima.fits") ;
00264 
00265     /* Write the table */
00266     crires_table_save(set,
00267             parlist,
00268             set,
00269             tab,
00270             recipe_name,
00271             CRIRES_CALPRO_WAVE,
00272             CRIRES_PROTYPE_WL_POLY,
00273             NULL,
00274             (const cpl_propertylist **)qclists,
00275             PACKAGE "/" PACKAGE_VERSION,
00276             "crires_util_wlinit_tab.fits") ;
00277 
00278     /* Free and return */
00279     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00280         cpl_propertylist_delete(qclists[i]) ;
00281     }
00282     cpl_free(qclists) ;
00283     return  0;
00284 }
00285 

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