CRIRES Pipeline Reference Manual  2.3.2
crires_util_profile.c
00001 /* $Id: crires_util_profile.c,v 1.35 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.35 $
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_extract.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041                                 Define
00042  -----------------------------------------------------------------------------*/
00043 
00044 #define RECIPE_STRING "crires_util_profile"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int crires_util_profile_save(const cpl_table **, 
00051         const cpl_parameterlist *, cpl_frameset *) ;
00052 
00053 static char crires_util_profile_description[] = 
00054 "This recipe accepts 1 parameter:\n"
00055 "First parameter:   the profile produced by the spectrum extraction.\n"
00056 "                   (PRO TYPE = "CRIRES_PROTYPE_PROFILE")\n"
00057 "\n"
00058 "This recipe produces 1 file:\n"
00059 "First product:     the table with the FWHMs values of the profile.\n"
00060 "                   (PRO TYPE = "CRIRES_PROTYPE_FWHM")\n" ;
00061 
00062 CRIRES_RECIPE_DEFINE(crires_util_profile,
00063         CRIRES_PARAM_DISPLAY,
00064         "Compute the FWHM of the spectrum along lambda",
00065         crires_util_profile_description) ;
00066 
00067 /*-----------------------------------------------------------------------------
00068                             Static variables
00069  -----------------------------------------------------------------------------*/
00070 
00071 static struct {
00072     /* Inputs */
00073     int                 display ;
00074     /* Outputs */
00075     int                 win_mode ;
00076     crires_illum_period period ;
00077 } crires_util_profile_config ;
00078 
00079 /*-----------------------------------------------------------------------------
00080                                 Functions code
00081  -----------------------------------------------------------------------------*/
00082 
00083 /*----------------------------------------------------------------------------*/
00090 /*----------------------------------------------------------------------------*/
00091 static int crires_util_profile(
00092         cpl_frameset            *   frameset,
00093         const cpl_parameterlist *   parlist)
00094 {
00095     cpl_frame           *   fr ;
00096     const char          *   fname ;
00097     cpl_imagelist       *   profile ;
00098     cpl_propertylist    *   plist ;
00099     const char          *   sval ;
00100     cpl_table           *   tab[CRIRES_NB_DETECTORS] ;
00101     cpl_bivector        *   centers_fwhms ;
00102     int                     nbrows, ly ;
00103     int                     i, j ;
00104     
00105     /* Initialise */
00106     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00107         tab[i] = NULL ;
00108     }
00109     ly = -1 ;
00110     
00111     /* Retrieve input parameters */
00112     crires_util_profile_config.display = crires_parameterlist_get_int(parlist,
00113             RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00114     
00115     /* Identify the RAW and CALIB frames in the input frameset */
00116     if (crires_dfs_set_groups(frameset, NULL)) {
00117         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00118         return -1 ;
00119     }
00120 
00121     /* Get the detector illumination period */
00122     fr = cpl_frameset_get_position(frameset, 0);
00123     crires_util_profile_config.period =
00124         crires_get_detector_illum_period(cpl_frame_get_filename(fr)) ;
00125     if (crires_util_profile_config.period == CRIRES_ILLUM_UNKNOWN) {
00126         cpl_msg_error(__func__,
00127                 "Cannot determine the detector illumination period") ;
00128         return -1 ;
00129     }
00130 
00131     /* Windowing mode ? */
00132     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
00133         return -1 ;
00134     sval = crires_pfits_get_ncorrs(plist) ;
00135     if (!strcmp(sval, "FowlerNsampGRstWin")) {
00136         crires_util_profile_config.period = CRIRES_ILLUM_FULL_DETECTOR ;
00137         crires_util_profile_config.win_mode = 1 ;
00138     } else {
00139         crires_util_profile_config.win_mode = 0 ;
00140     }
00141     cpl_propertylist_delete(plist) ;
00142 
00143     /* Display the Detector illumination */
00144     crires_display_detector_illum(crires_util_profile_config.period) ;
00145 
00146     /* The first frame must be the profile images */
00147     cpl_msg_info(__func__, "Input image loading") ;
00148     fname = cpl_frame_get_filename(fr) ;
00149     profile = crires_load_file(fname, crires_util_profile_config.period, 
00150             CPL_TYPE_FLOAT) ;
00151 
00152     /* Get the FWHMs */
00153     cpl_msg_info(__func__, "FWHM computation") ;
00154     cpl_msg_indent_more() ;
00155     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00156         /* Skip some detectors in windowing mode */
00157         if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
00158                 (crires_util_profile_config.win_mode == 1)) {
00159             continue ;
00160         }
00161         cpl_msg_info(__func__, "Chip number %d", i+1) ;
00162         cpl_msg_indent_more() ;
00163         if ((centers_fwhms = crires_extract_spatial_fwhm(
00164                         cpl_imagelist_get(profile, i))) == NULL) {
00165             cpl_msg_error(__func__, 
00166                     "Cannot get the FWHM for chip number %d", i+1) ;
00167             continue ;
00168         }
00169         cpl_msg_indent_less() ;
00170 
00171         /* Correct for the position */
00172         if (i+1 == 1) 
00173             ly = crires_get_detector_ly1(crires_util_profile_config.period) ;
00174         if (i+1 == 2) 
00175             ly = crires_get_detector_ly2(crires_util_profile_config.period) ;
00176         if (i+1 == 3) 
00177             ly = crires_get_detector_ly3(crires_util_profile_config.period) ;
00178         if (i+1 == 4) 
00179             ly = crires_get_detector_ly4(crires_util_profile_config.period) ;
00180         /* If ly not found, use the bottom of the image */
00181         if (ly < 0) ly = 1 ;
00182         cpl_vector_add_scalar(cpl_bivector_get_x(centers_fwhms),(double)(ly-1));
00183 
00184         /* Plot if requested */
00185         if(crires_util_profile_config.display == i+1) {
00186             cpl_plot_vector(
00187 "set grid;set xlabel 'Position (pixels)';set ylabel 'Y positions (pixels)';",
00188             "t 'Y positions' w lines", "", cpl_bivector_get_x(centers_fwhms));
00189             cpl_plot_vector(
00190         "set grid;set xlabel 'Position (pixels)';set ylabel 'FWHM (pixels)';",
00191                 "t 'FWHMs' w lines", "", cpl_bivector_get_y(centers_fwhms));
00192         }
00193        
00194         /* Create a table from the vector */
00195         nbrows = cpl_image_get_size_x(cpl_imagelist_get(profile, i)) ;
00196         tab[i] = cpl_table_new(nbrows) ;
00197         cpl_table_new_column(tab[i], CRIRES_COL_WL_DETPOSX, CPL_TYPE_INT) ;
00198         cpl_table_new_column(tab[i], CRIRES_COL_DETPOSY, CPL_TYPE_DOUBLE) ;
00199         cpl_table_new_column(tab[i], CRIRES_COL_FWHM, CPL_TYPE_DOUBLE) ;
00200         for (j=0 ; j<nbrows ; j++) {
00201             cpl_table_set_int(tab[i], CRIRES_COL_WL_DETPOSX, j, j+1) ;
00202             cpl_table_set_double(tab[i], CRIRES_COL_DETPOSY, j,
00203                     cpl_vector_get(cpl_bivector_get_x(centers_fwhms), j));
00204             cpl_table_set_double(tab[i], CRIRES_COL_FWHM, j,
00205                     cpl_vector_get(cpl_bivector_get_y(centers_fwhms), j));
00206         }
00207         cpl_bivector_delete(centers_fwhms) ;
00208     }
00209     cpl_msg_indent_less() ;
00210     cpl_imagelist_delete(profile) ;
00211         
00212     /* Reconstruct chips in windowing mode using detector 2 */
00213     if (crires_util_profile_config.win_mode == 1) {
00214         if (tab[1] != NULL) {
00215             tab[0] = cpl_table_duplicate(tab[1]) ;
00216             cpl_table_set_size(tab[0], 0) ;
00217             tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(tab[0]) ;
00218         }
00219     }
00220 
00221     /* Save the product */
00222     cpl_msg_info(__func__, "Save the product") ;
00223     cpl_msg_indent_more() ;
00224     if (crires_util_profile_save((const cpl_table **)tab, parlist, frameset)) {
00225         cpl_msg_error(__func__, "Cannot save the product") ;
00226         cpl_msg_indent_less() ;
00227         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) cpl_table_delete(tab[i]) ;
00228         return -1 ;
00229     }
00230     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) cpl_table_delete(tab[i]) ;
00231     cpl_msg_indent_less() ;
00232 
00233     /* Return */
00234     if (cpl_error_get_code()) return -1 ;
00235     else return 0 ;
00236 }
00237 
00238 /*----------------------------------------------------------------------------*/
00246 /*----------------------------------------------------------------------------*/
00247 static int crires_util_profile_save(
00248         const cpl_table         **  out,
00249         const cpl_parameterlist *   parlist,
00250         cpl_frameset            *   set)
00251 {
00252     const char          *   recipe_name = "crires_util_profile" ;
00253 
00254     /* Write the table */
00255     crires_table_save(set,
00256             parlist,
00257             set,
00258             out,
00259             recipe_name,
00260             CRIRES_FWHM_TAB,
00261             CRIRES_PROTYPE_FWHM,
00262             NULL,
00263             NULL,
00264             PACKAGE "/" PACKAGE_VERSION,
00265             "crires_util_profile.fits") ;
00266 
00267     return  0;
00268 }