CRIRES Pipeline Reference Manual  2.3.2
crires_util_conversion.c
00001 /* $Id: crires_util_conversion.c,v 1.37 2012-09-19 14:10: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: 2012-09-19 14:10:27 $
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 
00038 #include "crires_photom.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041                                 Define
00042  -----------------------------------------------------------------------------*/
00043 
00044 #define RECIPE_STRING "crires_util_conversion"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int crires_util_conversion_save(const cpl_table **, 
00051         const cpl_parameterlist *, cpl_frameset *) ;
00052 
00053 static char crires_util_conversion_description[] = 
00054 "This recipe accepts 2 parameters:\n"
00055 "First parameter:   the table of the extracted spectrum in wavelength.\n"
00056 "                   (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n"
00057 "Second parameter:  the table with the standard star photospheric flux.\n"
00058 "                   (PRO TYPE = "CRIRES_PROTYPE_PHO_FLUX")\n"
00059 "\n"
00060 "This recipe produces 1 file:\n"
00061 "First product:     the table with the conversion signal.\n"
00062 "                   (PRO TYPE = "CRIRES_PROTYPE_CONVERS")\n" ;
00063 
00064 CRIRES_RECIPE_DEFINE(crires_util_conversion,
00065         CRIRES_PARAM_DISPLAY,
00066         "Compute the conversion factor",
00067         crires_util_conversion_description) ;
00068 
00069 /*-----------------------------------------------------------------------------
00070                             Static variables
00071  -----------------------------------------------------------------------------*/
00072 
00073 static struct {
00074     /* Inputs */
00075     int             display ;
00076     /* Outputs */
00077     int             win_mode ;
00078     double          conv_med[CRIRES_NB_DETECTORS] ;
00079     double          thro_med[CRIRES_NB_DETECTORS] ;
00080 } crires_util_conversion_config ;
00081 
00082 /*-----------------------------------------------------------------------------
00083                                 Functions code
00084  -----------------------------------------------------------------------------*/
00085 
00086 /*----------------------------------------------------------------------------*/
00093 /*----------------------------------------------------------------------------*/
00094 static int crires_util_conversion(
00095         cpl_frameset            *   frameset,
00096         const cpl_parameterlist *   parlist)
00097 {
00098     cpl_frame           *   fr ;
00099     const char          *   sval ;
00100     cpl_propertylist    *   plist ;
00101     cpl_table           *   ext_tab[CRIRES_NB_DETECTORS] ;
00102     cpl_table           *   std_flux ;
00103     cpl_bivector        *   std_biv ;
00104     int                     i, j ;
00105 
00106     /* Initialise */
00107     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00108         crires_util_conversion_config.conv_med[i] = -1.0 ;
00109         crires_util_conversion_config.thro_med[i] = -1.0 ;
00110         ext_tab[i] = NULL ;
00111     }
00112  
00113     /* Retrieve input parameters */
00114     crires_util_conversion_config.display = crires_parameterlist_get_int(
00115             parlist, RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00116     
00117     /* Identify the RAW and CALIB frames in the input frameset */
00118     if (crires_dfs_set_groups(frameset, NULL)) {
00119         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00120         return -1 ;
00121     }
00122 
00123     /* The second frame must be the std star flux */
00124     cpl_msg_info(__func__, "Second frame validity check") ;
00125     cpl_msg_indent_more() ;
00126     fr = cpl_frameset_get_position(frameset, 1);
00127     if ((std_flux = crires_load_table_check(cpl_frame_get_filename(fr), 1,
00128                     CRIRES_PROTYPE_PHO_FLUX, -1, -1, 0)) == NULL) {
00129         cpl_msg_error(__func__, "Second frame PRO TYPE must be %s",
00130                 CRIRES_PROTYPE_PHO_FLUX) ;
00131         cpl_msg_indent_less() ;
00132         return -1 ;
00133     }
00134     cpl_msg_indent_less() ;
00135  
00136     /* Get the requested star */
00137     cpl_msg_info(__func__, "Standard star photospheric flux retrieval") ;
00138     cpl_msg_indent_more() ;
00139     if ((std_biv = crires_photom_conv_get_star(std_flux, 
00140         cpl_frame_get_filename(cpl_frameset_get_position(frameset, 0))))==NULL){
00141         cpl_msg_error(__func__, "Cannot find the star") ;
00142         cpl_msg_indent_less() ;
00143         cpl_table_delete(std_flux) ;
00144         return -1 ;
00145     }
00146     cpl_table_delete(std_flux) ;
00147     cpl_msg_indent_less() ;
00148    
00149     /* Windowing mode ? */
00150     fr = cpl_frameset_get_position(frameset, 0);
00151     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
00152         return -1 ;
00153     sval = crires_pfits_get_ncorrs(plist) ;
00154     if (!strcmp(sval, "FowlerNsampGRstWin")) {
00155         crires_util_conversion_config.win_mode = 1 ;
00156     } else {
00157         crires_util_conversion_config.win_mode = 0 ;
00158     }
00159     cpl_propertylist_delete(plist) ;
00160 
00161     /* Loop on the CRIRES_NB_DETECTORS detectors */
00162     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00163         /* Skip some detectors in windowing mode */
00164         if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
00165                 (crires_util_conversion_config.win_mode == 1)) {
00166             continue ;
00167         }
00168 
00169         cpl_msg_info(__func__, "Conversion factor for chip %d", i+1) ;
00170         
00171         /* The first frame must be the extracted spectrum in wavelength */
00172         cpl_msg_info(__func__, "Load the extracted spectrum") ;
00173         cpl_msg_indent_more() ;
00174         fr = cpl_frameset_get_position(frameset, 0);
00175         if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
00176                         i+1, CRIRES_PROTYPE_SPEC_WL, -1, -1, 0)) == NULL) {
00177             cpl_msg_indent_less() ;
00178             continue ;
00179         }
00180         cpl_msg_indent_less() ;
00181            
00182         /* Compute */
00183         cpl_msg_info(__func__, "Conversion factor computation") ;
00184         cpl_msg_indent_more() ;
00185         if (crires_photom_conv_engine(ext_tab[i], std_biv, i+1,
00186                     crires_util_conversion_config.display==i+1) == -1) {
00187             cpl_msg_error(__func__, "Cannot compute conversion factor") ;
00188             cpl_msg_indent_less() ;
00189             cpl_bivector_delete(std_biv) ;
00190             for (j=0 ; j<i ; j++) {
00191                 if (ext_tab[j] != NULL) cpl_table_delete(ext_tab[j]) ;
00192             }
00193             return -1 ;
00194         }
00195         cpl_msg_indent_less() ;
00196         
00197         /* Compute the Median */
00198         crires_util_conversion_config.conv_med[i] =
00199             cpl_table_get_column_median(ext_tab[i], CRIRES_COL_CONVERSION_RECT);
00200         crires_util_conversion_config.thro_med[i] =
00201             cpl_table_get_column_median(ext_tab[i], CRIRES_COL_THROUGHPUT) ;
00202     }
00203     cpl_bivector_delete(std_biv) ;
00204     
00205     /* Reconstruct chips in windowing mode using detector 2 */
00206     if (crires_util_conversion_config.win_mode == 1) {
00207         if (ext_tab[1] != NULL) {
00208             ext_tab[0] = cpl_table_duplicate(ext_tab[1]) ;
00209             cpl_table_set_size(ext_tab[0], 0) ;
00210             ext_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(ext_tab[0]) ;
00211         }
00212     }
00213 
00214     /* Save the product */
00215     cpl_msg_info(__func__, "Save the product") ;
00216     cpl_msg_indent_more() ;
00217     if (crires_util_conversion_save((const cpl_table **)ext_tab, parlist, 
00218                 frameset)) {
00219         cpl_msg_error(__func__, "Cannot save the product") ;
00220         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00221             if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00222         }
00223         cpl_msg_indent_less() ;
00224         return -1 ;
00225     }
00226     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00227         if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00228     }
00229     cpl_msg_indent_less() ;
00230 
00231     /* Return */
00232     if (cpl_error_get_code()) return -1 ;
00233     else return 0 ;
00234 }
00235 
00236 /*----------------------------------------------------------------------------*/
00244 /*----------------------------------------------------------------------------*/
00245 static int crires_util_conversion_save(
00246         const cpl_table         **  out,
00247         const cpl_parameterlist *   parlist,
00248         cpl_frameset            *   set)
00249 {
00250     cpl_propertylist    **  qclists ;
00251     const cpl_frame     *   ref_frame ;
00252     cpl_propertylist    *   inputlist ;
00253     const char          *   recipe_name = "crires_util_conversion" ;
00254     int                     i ;
00255 
00256     /* Get the reference frame */
00257     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00258 
00259     /* Create the QC lists */
00260     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00261     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00262         qclists[i] = cpl_propertylist_new() ;
00263         cpl_propertylist_append_double(qclists[i], "ESO QC CONVMED",
00264                 crires_util_conversion_config.conv_med[i]) ;
00265         cpl_propertylist_append_double(qclists[i], "ESO QC THROMED",
00266                 crires_util_conversion_config.thro_med[i]) ;
00267 
00268         /* Propagate some keywords from input raw frame extensions */
00269         inputlist = cpl_propertylist_load_regexp(
00270                 cpl_frame_get_filename(ref_frame), i+1,
00271                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00272         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00273                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00274         cpl_propertylist_delete(inputlist) ;
00275     }
00276     
00277     /* Write the table */
00278     crires_table_save(set,
00279             parlist,
00280             set,
00281             out,
00282             recipe_name,
00283             CRIRES_EXTRACT_CONV_TAB,
00284             CRIRES_PROTYPE_CONVERS,
00285             NULL,
00286             (const cpl_propertylist **)qclists,
00287             PACKAGE "/" PACKAGE_VERSION,
00288             "crires_util_conversion.fits") ;
00289 
00290     /* Free and return */
00291     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00292         cpl_propertylist_delete(qclists[i]) ;
00293     }
00294     cpl_free(qclists) ;
00295     return  0;
00296 }
00297