CRIRES Pipeline Reference Manual  2.3.2
crires_util_genstd.c
00001 /* $Id: crires_util_genstd.c,v 1.12 2011-11-24 08:27:46 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: 2011-11-24 08:27:46 $
00024  * $Revision: 1.12 $
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 /*-----------------------------------------------------------------------------
00039                                 Define
00040  -----------------------------------------------------------------------------*/
00041 
00042 #define RECIPE_STRING "crires_util_genstd"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static int crires_util_genstd_save(cpl_table *, const cpl_parameterlist *, 
00049         cpl_frameset *);
00050 
00051 static char crires_util_genstd_description[] =
00052 "This recipe is used to generate standard star photospheric flux tables.\n"
00053 "The sof consists of file names tagged with "CRIRES_UTIL_GENSTD_RAW".\n"
00054 "The specified files are named after the standard star they represent\n"
00055 "(e.g. HIP61007.txt).\n"
00056 "The first line of the file must contain the RA and DEC (hh mm ss).\n"
00057 "(e.g. # 13 20 35.818        -36 42 44.26).\n"
00058 "The rest of the file must contain two columns:\n"
00059 "1st: Wavelengths in increasing order (the unit is corrected by\n"
00060 "     the factor option to obtain nanometers).\n"
00061 "2nd: The atmospheric emission.\n"
00062 "The file is generated using the ASCII files in the catalogs/stdstar\n"
00063 "directory of the CRIRES source-code distribution."
00064 "\n"
00065 "This recipe produces 1 file for each input file:\n"
00066 "First product:     the table with the photospheric flux of the std.\n"
00067 "                   (PRO TYPE = "CRIRES_PROTYPE_PHO_FLUX")\n" ;
00068 
00069 CRIRES_RECIPE_DEFINE(crires_util_genstd,
00070         CRIRES_PARAM_PLOT,
00071         "Generate standard star FITS tables",
00072         crires_util_genstd_description) ;
00073 
00074 /*-----------------------------------------------------------------------------
00075                             Static variables
00076  -----------------------------------------------------------------------------*/
00077 
00078 static struct {
00079     /* Inputs */
00080     int             display ;
00081     /* Outputs */
00082 } crires_util_genstd_config ;
00083 
00084 /*-----------------------------------------------------------------------------
00085                                 Functions code
00086  -----------------------------------------------------------------------------*/
00087 
00088 /*----------------------------------------------------------------------------*/
00097 /*----------------------------------------------------------------------------*/
00098 static int crires_util_genstd(
00099         cpl_frameset            *   framelist,
00100         const cpl_parameterlist *   parlist)
00101 {
00102     FILE            *   in ;
00103     char                line[1024] ;
00104     cpl_bivector    *   bivec_ref ;
00105     cpl_bivector    *   bivec ;
00106     int                 ra1, ra2, dec1, dec2;
00107     double              ra3, dec3;
00108     double              ra, dec ;
00109     char                isign;
00110     cpl_frame       *   cur_frame ;
00111     const char      *   cur_fname ;
00112     cpl_table       *   tab ;
00113     int                 nvals, nframes, nvals_ref ;
00114     double          *   pwave ;
00115     double          *   pemiss ;
00116     double          *   pwave_ref ;
00117     cpl_array       *   array ;
00118     int                 i, j ;
00119 
00120     /* Retrieve input parameters */
00121     crires_util_genstd_config.display = crires_parameterlist_get_bool(
00122             parlist, RECIPE_STRING, CRIRES_PARAM_PLOT) ;
00123  
00124     /* Identify the RAW and CALIB frames in the input frameset */
00125     if (crires_dfs_set_groups(framelist, NULL)) {
00126         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00127         return -1 ;
00128     }
00129     nframes = cpl_frameset_get_size(framelist) ;
00130   
00131     /* Get the first star */
00132     cur_frame = cpl_frameset_get_position(framelist, 0) ;
00133     cur_fname = cpl_frame_get_filename(cur_frame) ;
00134     if ((bivec_ref = cpl_bivector_read(cur_fname))==NULL){
00135         cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
00136         return -1 ;
00137     }
00138     pwave_ref = cpl_bivector_get_x_data(bivec_ref) ;
00139     nvals_ref = cpl_bivector_get_size(bivec_ref) ;
00140 
00141     /* Create the table */
00142     tab = cpl_table_new(nframes+1) ;
00143     cpl_table_new_column(tab, CRIRES_COL_STDNAME, CPL_TYPE_STRING) ;
00144     cpl_table_new_column(tab, CRIRES_COL_RA, CPL_TYPE_DOUBLE) ;
00145     cpl_table_new_column(tab, CRIRES_COL_DEC, CPL_TYPE_DOUBLE) ;
00146     cpl_table_new_column_array(tab, CRIRES_COL_PHOTOFLUX, CPL_TYPE_DOUBLE, 
00147             nvals_ref) ;
00148     
00149     /* Write the wavelength */
00150     cpl_table_set_string(tab, CRIRES_COL_STDNAME, 0, "WAVE") ;
00151     cpl_table_set_double(tab, CRIRES_COL_RA, 0, -1.0) ;
00152     cpl_table_set_double(tab, CRIRES_COL_DEC, 0, -1.0) ;
00153     array = cpl_array_wrap_double(pwave_ref, nvals_ref) ;
00154     cpl_table_set_array(tab, CRIRES_COL_PHOTOFLUX, 0, array) ;
00155     cpl_array_unwrap(array) ;
00156 
00157     /* Loop on the input frames */
00158     for (i=0 ; i<nframes ; i++) {
00159         
00160         /* Get the frame */
00161         cur_frame = cpl_frameset_get_position(framelist, i) ;
00162         cur_fname = cpl_frame_get_filename(cur_frame) ;
00163 
00164         /* Get the RA / DEC */
00165         if ((in = fopen(cur_fname, "r")) == NULL) {
00166             cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
00167             cpl_table_delete(tab) ;
00168             cpl_bivector_delete(bivec_ref) ;
00169             return -1 ;
00170         }
00171         if (fgets(line, 1024, in) == NULL) {
00172             fclose(in) ;
00173             cpl_table_delete(tab) ;
00174             cpl_bivector_delete(bivec_ref) ;
00175             return -1 ;
00176         }
00177         if (sscanf(line, "#%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
00178                     &dec1,  &dec2, &dec3) != 7) {
00179             cpl_msg_error(__func__, "Invalid first line in file %s", cur_fname);
00180             fclose(in) ;
00181             cpl_table_delete(tab) ;
00182             cpl_bivector_delete(bivec_ref) ;
00183             return -1 ;
00184         }
00185         fclose(in) ;
00186         ra = crires_ra_hms2deg(ra1, ra2, ra3) ;
00187         dec = crires_dec_hms2deg(dec1, dec2, dec3) ;
00188         if (isign == '-') dec *= -1.0 ;
00189      
00190         /* Load the file */
00191         if ((bivec = cpl_bivector_read(cur_fname))==NULL){
00192             cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
00193             cpl_bivector_delete(bivec_ref) ;
00194             cpl_table_delete(tab) ;
00195             return -1 ;
00196         }
00197         pwave = cpl_bivector_get_x_data(bivec) ;
00198         pemiss = cpl_bivector_get_y_data(bivec) ;
00199         nvals = cpl_bivector_get_size(bivec) ;
00200 
00201         /* Check the file size */
00202         if (nvals != nvals_ref) {
00203             cpl_msg_error(__func__, "Invalid file size: %s", cur_fname) ;
00204             cpl_bivector_delete(bivec_ref) ;
00205             cpl_bivector_delete(bivec) ;
00206             cpl_table_delete(tab) ;
00207             return -1 ;
00208         }
00209 
00210         /* Check that the wavelength bins are the same */
00211         for (j=0 ; j<nvals ; j++) {
00212             if (pwave[j] != pwave_ref[j]) {
00213                 cpl_msg_error(__func__, "Invalid bins in %s", cur_fname) ;
00214                 cpl_bivector_delete(bivec_ref) ;
00215                 cpl_bivector_delete(bivec) ;
00216                 cpl_table_delete(tab) ;
00217                 return -1 ;
00218             }
00219         }
00220             
00221         /* Display if requested */
00222         if (crires_util_genstd_config.display) {
00223             cpl_plot_bivector(
00224                 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Flux (jy)';",
00225                 "t 'Photospheric flux' w lines", "", bivec);
00226         }
00227         
00228         /* Write the star name */
00229         cpl_table_set_string(tab, CRIRES_COL_STDNAME, i+1,
00230                 crires_get_root_name(crires_get_base_name(cur_fname))) ;
00231         
00232         /* Write the RA/DEC  */
00233         cpl_table_set_double(tab, CRIRES_COL_RA, i+1, ra) ;
00234         cpl_table_set_double(tab, CRIRES_COL_DEC, i+1, dec) ;
00235 
00236         /* Write the signal */
00237         array = cpl_array_wrap_double(pemiss, nvals) ;
00238         cpl_table_set_array(tab, CRIRES_COL_PHOTOFLUX, i+1, array) ;
00239         cpl_array_unwrap(array) ;
00240     
00241         cpl_bivector_delete(bivec) ;
00242     }
00243     cpl_bivector_delete(bivec_ref) ;
00244     
00245     /* Save the table */
00246     cpl_msg_info(__func__, "Save the table") ;
00247     if (crires_util_genstd_save(tab, parlist, framelist) == -1) {
00248         cpl_msg_error(__func__, "Cannot write the table") ;
00249         cpl_table_delete(tab) ;
00250         return -1 ;
00251     }
00252     cpl_table_delete(tab) ;
00253 
00254     return 0 ;
00255 }
00256 
00257 /*----------------------------------------------------------------------------*/
00265 /*----------------------------------------------------------------------------*/
00266 static int crires_util_genstd_save(
00267         cpl_table               *   out_table,
00268         const cpl_parameterlist *   parlist,
00269         cpl_frameset            *   set)
00270 {
00271     cpl_propertylist    *   plist ;
00272 
00273     plist = cpl_propertylist_new();
00274     cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
00275     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
00276             CRIRES_CALPRO_STD_PHOTOFLUX) ;
00277     cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00278             CRIRES_PROTYPE_PHO_FLUX) ;
00279 
00280     if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
00281                 NULL, "crires_util_genstd", plist, NULL, 
00282                 PACKAGE "/" PACKAGE_VERSION,
00283                 "crires_util_genstd.fits") != CPL_ERROR_NONE) {
00284         cpl_msg_error(__func__, "Cannot save the table") ;
00285         return -1 ;
00286     }
00287     cpl_propertylist_delete(plist) ;
00288 
00289     /* Return */
00290     return 0 ;
00291 }