crires_util_conversion.c

00001 /* $Id: crires_util_conversion.c,v 1.36 2010/04/23 08:58:43 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 08:58:43 $
00024  * $Revision: 1.36 $
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_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     double          conv_med[CRIRES_NB_DETECTORS] ;
00078     double          thro_med[CRIRES_NB_DETECTORS] ;
00079 } crires_util_conversion_config ;
00080 
00081 /*-----------------------------------------------------------------------------
00082                                 Functions code
00083  -----------------------------------------------------------------------------*/
00084 
00085 /*----------------------------------------------------------------------------*/
00092 /*----------------------------------------------------------------------------*/
00093 static int crires_util_conversion(
00094         cpl_frameset            *   frameset,
00095         const cpl_parameterlist *   parlist)
00096 {
00097     cpl_frame       *   fr ;
00098     cpl_table       *   ext_tab[CRIRES_NB_DETECTORS] ;
00099     cpl_table       *   std_flux ;
00100     cpl_bivector    *   std_biv ;
00101     int                 i, j ;
00102     
00103     /* Retrieve input parameters */
00104     crires_util_conversion_config.display = crires_parameterlist_get_int(
00105             parlist, RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00106     
00107     /* Identify the RAW and CALIB frames in the input frameset */
00108     if (crires_dfs_set_groups(frameset, NULL)) {
00109         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00110         return -1 ;
00111     }
00112 
00113     /* The second frame must be the std star flux */
00114     cpl_msg_info(__func__, "Second frame validity check") ;
00115     cpl_msg_indent_more() ;
00116     fr = cpl_frameset_get_frame(frameset, 1);
00117     if ((std_flux = crires_load_table_check(cpl_frame_get_filename(fr), 1,
00118                     CRIRES_PROTYPE_PHO_FLUX, -1, -1, 0)) == NULL) {
00119         cpl_msg_error(__func__, "Second frame PRO TYPE must be %s",
00120                 CRIRES_PROTYPE_PHO_FLUX) ;
00121         cpl_msg_indent_less() ;
00122         return -1 ;
00123     }
00124     cpl_msg_indent_less() ;
00125  
00126     /* Get the requested star */
00127     cpl_msg_info(__func__, "Standard star photospheric flux retrieval") ;
00128     cpl_msg_indent_more() ;
00129     if ((std_biv = crires_photom_conv_get_star(std_flux, 
00130         cpl_frame_get_filename(cpl_frameset_get_frame(frameset, 0))))==NULL) {
00131         cpl_msg_error(__func__, "Cannot find the star") ;
00132         cpl_msg_indent_less() ;
00133         cpl_table_delete(std_flux) ;
00134         return -1 ;
00135     }
00136     cpl_table_delete(std_flux) ;
00137     cpl_msg_indent_less() ;
00138     
00139     /* Loop on the CRIRES_NB_DETECTORS detectors */
00140     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00141         cpl_msg_info(__func__, "Conversion factor for chip %d", i+1) ;
00142         
00143         /* Initialise */
00144         ext_tab[i] = NULL ;
00145         crires_util_conversion_config.conv_med[i] = -1.0 ;
00146         crires_util_conversion_config.thro_med[i] = -1.0 ;
00147 
00148         /* The first frame must be the extracted spectrum in wavelength */
00149         cpl_msg_info(__func__, "Load the extracted spectrum") ;
00150         cpl_msg_indent_more() ;
00151         fr = cpl_frameset_get_frame(frameset, 0);
00152         if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
00153                         i+1, CRIRES_PROTYPE_SPEC_WL, -1, -1, 0)) == NULL) {
00154             cpl_msg_indent_less() ;
00155             continue ;
00156         }
00157         cpl_msg_indent_less() ;
00158            
00159         /* Compute */
00160         cpl_msg_info(__func__, "Conversion factor computation") ;
00161         cpl_msg_indent_more() ;
00162         if (crires_photom_conv_engine(ext_tab[i], std_biv, i+1,
00163                     crires_util_conversion_config.display==i+1) == -1) {
00164             cpl_msg_error(__func__, "Cannot compute conversion factor") ;
00165             cpl_msg_indent_less() ;
00166             cpl_bivector_delete(std_biv) ;
00167             for (j=0 ; j<i ; j++) {
00168                 if (ext_tab[j] != NULL) cpl_table_delete(ext_tab[j]) ;
00169             }
00170             return -1 ;
00171         }
00172         cpl_msg_indent_less() ;
00173         
00174         /* Compute the Median */
00175         crires_util_conversion_config.conv_med[i] =
00176             cpl_table_get_column_median(ext_tab[i], CRIRES_COL_CONVERSION_RECT);
00177         crires_util_conversion_config.thro_med[i] =
00178             cpl_table_get_column_median(ext_tab[i], CRIRES_COL_THROUGHPUT) ;
00179     }
00180     cpl_bivector_delete(std_biv) ;
00181         
00182     /* Save the product */
00183     cpl_msg_info(__func__, "Save the product") ;
00184     cpl_msg_indent_more() ;
00185     if (crires_util_conversion_save((const cpl_table **)ext_tab, parlist, 
00186                 frameset)) {
00187         cpl_msg_error(__func__, "Cannot save the product") ;
00188         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00189             if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00190         }
00191         cpl_msg_indent_less() ;
00192         return -1 ;
00193     }
00194     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00195         if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00196     }
00197     cpl_msg_indent_less() ;
00198 
00199     /* Return */
00200     if (cpl_error_get_code()) return -1 ;
00201     else return 0 ;
00202 }
00203 
00204 /*----------------------------------------------------------------------------*/
00212 /*----------------------------------------------------------------------------*/
00213 static int crires_util_conversion_save(
00214         const cpl_table         **  out,
00215         const cpl_parameterlist *   parlist,
00216         cpl_frameset            *   set)
00217 {
00218     cpl_propertylist    **  qclists ;
00219     const cpl_frame     *   ref_frame ;
00220     cpl_propertylist    *   inputlist ;
00221     const char          *   recipe_name = "crires_util_conversion" ;
00222     int                     i ;
00223 
00224     /* Get the reference frame */
00225     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00226 
00227     /* Create the QC lists */
00228     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00229     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00230         qclists[i] = cpl_propertylist_new() ;
00231         cpl_propertylist_append_double(qclists[i], "ESO QC CONVMED",
00232                 crires_util_conversion_config.conv_med[i]) ;
00233         cpl_propertylist_append_double(qclists[i], "ESO QC THROMED",
00234                 crires_util_conversion_config.thro_med[i]) ;
00235 
00236         /* Propagate some keywords from input raw frame extensions */
00237         inputlist = cpl_propertylist_load_regexp(
00238                 cpl_frame_get_filename(ref_frame), i+1,
00239                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00240         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00241                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00242         cpl_propertylist_delete(inputlist) ;
00243     }
00244     
00245     /* Write the table */
00246     crires_table_save(set,
00247             parlist,
00248             set,
00249             out,
00250             recipe_name,
00251             CRIRES_EXTRACT_CONV_TAB,
00252             CRIRES_PROTYPE_CONVERS,
00253             NULL,
00254             (const cpl_propertylist **)qclists,
00255             PACKAGE "/" PACKAGE_VERSION,
00256             "crires_util_conversion.fits") ;
00257 
00258     /* Free and return */
00259     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00260         cpl_propertylist_delete(qclists[i]) ;
00261     }
00262     cpl_free(qclists) ;
00263     return  0;
00264 }
00265 

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