CRIRES Pipeline Reference Manual  2.3.2
crires_util_sensitivity.c
00001 /* $Id: crires_util_sensitivity.c,v 1.32 2012-09-20 12:15:03 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:15:03 $
00024  * $Revision: 1.32 $
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_photom.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041                                 Define
00042  -----------------------------------------------------------------------------*/
00043 
00044 #define RECIPE_STRING "crires_util_sensitivity"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int crires_util_sensitivity_save(const cpl_table **, 
00051         const cpl_parameterlist *, cpl_frameset *) ;
00052 
00053 static char crires_util_sensitivity_description[] = 
00054 "This recipe accepts 1 parameter:\n"
00055 "First parameter:   the table with the conversion factor.\n"
00056 "                   (PRO TYPE = "CRIRES_PROTYPE_CONVERS")\n"
00057 "\n"
00058 "This recipe produces 1 file:\n"
00059 "First product:     the table with the sensitivity signal.\n"
00060 "                   (PRO TYPE = "CRIRES_PROTYPE_SENSIT")\n" ;
00061 
00062 CRIRES_RECIPE_DEFINE(crires_util_sensitivity,
00063         CRIRES_PARAM_DISPLAY        |
00064         CRIRES_PARAM_EXPTIME,
00065         "Compute the sensitivity",
00066         crires_util_sensitivity_description) ;
00067 
00068 /*-----------------------------------------------------------------------------
00069                             Static variables
00070  -----------------------------------------------------------------------------*/
00071 
00072 static struct {
00073     /* Inputs */
00074     int             display ;
00075     double          exptime ;
00076     /* Outputs */
00077     int             win_mode ;
00078     double          sens_med[CRIRES_NB_DETECTORS] ;
00079 } crires_util_sensitivity_config ;
00080 
00081 /*-----------------------------------------------------------------------------
00082                                 Functions code
00083  -----------------------------------------------------------------------------*/
00084 
00085 /*----------------------------------------------------------------------------*/
00092 /*----------------------------------------------------------------------------*/
00093 static int crires_util_sensitivity(
00094         cpl_frameset            *   frameset,
00095         const cpl_parameterlist *   parlist)
00096 {
00097     const char          *   sval ;
00098     cpl_propertylist    *   plist ;
00099     cpl_frame           *   fr ;
00100     cpl_table           *   conv_tab[CRIRES_NB_DETECTORS] ;
00101     double                  exptime_comp ;
00102     int                     i ;
00103 
00104     /* Initialise */
00105     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00106         crires_util_sensitivity_config.sens_med[i] = -1.0 ;
00107         conv_tab[i] = NULL ;
00108     } 
00109 
00110     /* Retrieve input parameters */
00111     crires_util_sensitivity_config.display = crires_parameterlist_get_int(
00112             parlist, RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00113     crires_util_sensitivity_config.exptime = crires_parameterlist_get_double(
00114             parlist, RECIPE_STRING, CRIRES_PARAM_EXPTIME) ;
00115 
00116     /* Identify the RAW and CALIB frames in the input frameset */
00117     if (crires_dfs_set_groups(frameset, NULL)) {
00118         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00119         return -1 ;
00120     }
00121 
00122     /* Windowing mode ? */
00123     fr = cpl_frameset_get_position(frameset, 0);
00124     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
00125         return -1 ;
00126     sval = crires_pfits_get_ncorrs(plist) ;
00127     if (!strcmp(sval, "FowlerNsampGRstWin")) {
00128         crires_util_sensitivity_config.win_mode = 1 ;
00129     } else {
00130         crires_util_sensitivity_config.win_mode = 0 ;
00131     }
00132     cpl_propertylist_delete(plist) ;
00133 
00134     /* Loop on the CRIRES_NB_DETECTORS detectors */
00135     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00136         /* Skip some detectors in windowing mode */
00137         if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
00138                 (crires_util_sensitivity_config.win_mode == 1)) {
00139             continue ;
00140         }
00141 
00142         cpl_msg_info(__func__, "Sensitivity computation for chip %d", i+1) ;
00143 
00144         /* The first frame must be the conversion table */
00145         cpl_msg_info(__func__, "Load the extracted table") ;
00146         cpl_msg_indent_more() ;
00147         fr = cpl_frameset_get_position(frameset, 0);
00148         if ((conv_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
00149                         i+1, CRIRES_PROTYPE_CONVERS, -1, -1, 0)) == NULL) {
00150             cpl_msg_indent_less() ;
00151             continue ;
00152         }
00153         cpl_msg_indent_less() ;
00154 
00155         /* Compute */
00156         cpl_msg_info(__func__, "Sensitivity computation") ;
00157         cpl_msg_indent_more() ;
00158         if ((exptime_comp = crires_photom_sens_engine(conv_tab[i], 
00159                 cpl_frame_get_filename(fr),
00160                 crires_util_sensitivity_config.exptime,
00161                 crires_util_sensitivity_config.display==i+1)) < 0.0 ) {
00162             cpl_msg_error(__func__, "Cannot compute sensitivity") ;
00163             cpl_msg_indent_less() ;
00164             cpl_table_delete(conv_tab[i]) ;
00165             conv_tab[i] = NULL ;
00166             continue ;
00167         }
00168         cpl_msg_indent_less() ;
00169         crires_util_sensitivity_config.exptime = exptime_comp ;
00170 
00171         /* Compute the Median */
00172         crires_util_sensitivity_config.sens_med[i] = 
00173             cpl_table_get_column_median(conv_tab[i], CRIRES_COL_SENSITIVITY) ;
00174     }
00175    
00176     /* Reconstruct chips in windowing mode using detector 2 */
00177     if (crires_util_sensitivity_config.win_mode == 1) {
00178         if (conv_tab[1] != NULL) {
00179             conv_tab[0] = cpl_table_duplicate(conv_tab[1]) ;
00180             cpl_table_set_size(conv_tab[0], 0) ;
00181             conv_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(conv_tab[0]) ;
00182         }
00183     }
00184 
00185     /* Save the product */
00186     cpl_msg_info(__func__, "Save the product") ;
00187     cpl_msg_indent_more() ;
00188     if (crires_util_sensitivity_save((const cpl_table **)conv_tab, parlist, 
00189                 frameset)) {
00190         cpl_msg_error(__func__, "Cannot save the product") ;
00191         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00192             if (conv_tab[i] != NULL) cpl_table_delete(conv_tab[i]) ;
00193         }
00194         cpl_msg_indent_less() ;
00195         return -1 ;
00196     }
00197     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00198         if (conv_tab[i] != NULL) cpl_table_delete(conv_tab[i]) ;
00199     }
00200     cpl_msg_indent_less() ;
00201 
00202     /* Return */
00203     if (cpl_error_get_code()) return -1 ;
00204     else return 0 ;
00205 }
00206 
00207 /*----------------------------------------------------------------------------*/
00215 /*----------------------------------------------------------------------------*/
00216 static int crires_util_sensitivity_save(
00217         const cpl_table         **  out,
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_sensitivity" ;
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 SENSMED",
00235                 crires_util_sensitivity_config.sens_med[i]) ;
00236         cpl_propertylist_append_double(qclists[i], "ESO QC EXPTIME",
00237                 crires_util_sensitivity_config.exptime) ;
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 table */
00249     crires_table_save(set,
00250             parlist,
00251             set,
00252             out,
00253             recipe_name,
00254             CRIRES_EXTRACT_SENS_TAB,
00255             CRIRES_PROTYPE_SENSIT,
00256             NULL, 
00257             (const cpl_propertylist **)qclists,
00258             PACKAGE "/" PACKAGE_VERSION,
00259             "crires_util_sensitivity.fits") ;
00260 
00261     /* Free and return */
00262     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00263         cpl_propertylist_delete(qclists[i]) ;
00264     }
00265     cpl_free(qclists) ;
00266     return  0;
00267 }
00268