crires_model_fix.c

00001 /* $Id: crires_model_fix.c,v 1.22 2011/03/22 09:17:12 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/03/22 09:17:12 $
00024  * $Revision: 1.22 $
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_model_refining.h"
00039 #include "crires_model_kernel.h"
00040 
00041 /*-----------------------------------------------------------------------------
00042                                 Define
00043  -----------------------------------------------------------------------------*/
00044 
00045 #define RECIPE_STRING "crires_model_fix"
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Functions prototypes
00049  -----------------------------------------------------------------------------*/
00050 
00051 static int crires_model_fix_parse(const char *, cpl_vector **, cpl_vector **, 
00052         int **, cpl_vector **) ;
00053 static int crires_model_fix_save(cpl_table *, const cpl_parameterlist *, 
00054         cpl_frameset *) ;
00055 
00056 static char crires_model_fix_description[] =
00057 "crires_model_fix -- Model Fixing recipe\n"
00058 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00059 "raw-file.fits "CRIRES_MODEL_FIX_RAW" or\n"
00060 "WLS-file.txt "CRIRES_CALPRO_MODEL_FIX_TAB" like:\n"
00061 "  The TXT file is something like:\n"
00062 "  #   pos_x   pos_y   chip    wls\n"
00063 "      500     100     3       1025.0\n"
00064 "      600     100     3       1035.0\n"
00065 "      700     100     3       1045.0\n"
00066 "      900     100     3       1065.0\n"
00067 "config-file.fits "CRIRES_CALPRO_MODEL_CONFIG" or\n"
00068 "config-file.fits "CRIRES_CALPRO_MODEL_REFINE_CONF".\n" ;
00069 
00070 CRIRES_RECIPE_DEFINE(crires_model_fix,
00071         0,
00072         "Model Fixing recipe",
00073         crires_model_fix_description) ;
00074 
00075 /*-----------------------------------------------------------------------------
00076                                 Functions code
00077  -----------------------------------------------------------------------------*/
00078 
00079 /*----------------------------------------------------------------------------*/
00086 /*----------------------------------------------------------------------------*/
00087 static int crires_model_fix(
00088         cpl_frameset            *   frameset,
00089         const cpl_parameterlist *   parlist)
00090 {
00091     cpl_frameset    *   rawframes ;
00092     const char      *   config ;
00093     const char      *   user_wls ;
00094     cpl_frame       *   ref_frame ;
00095     const char      *   ref_fname ;
00096     cpl_vector      *   pos_x ;
00097     cpl_vector      *   pos_y ;
00098     int             *   chip ;
00099     cpl_vector      *   wls ;
00100     cpl_table       *   new_conf ;
00101 
00102     /* The Model is switched off */
00103     if (crires_model_off()) {
00104         return 0 ;
00105     }
00106 
00107     /* Initialise */
00108     rawframes = NULL ;
00109     
00110     /* Retrieve input parameters */
00111  
00112     /* Identify the RAW and CALIB frames in the input frameset */
00113     if (crires_dfs_set_groups(frameset, "crires_model_fix")) {
00114         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00115         return -1 ;
00116     }
00117 
00118     /* Retrieve calibration data */
00119     config = crires_extract_filename(frameset, CRIRES_CALPRO_MODEL_CONFIG) ;
00120     if (config == NULL) {
00121         config = crires_extract_filename(frameset, 
00122                 CRIRES_CALPRO_MODEL_REFINE_CONF) ;
00123     }
00124     if (config == NULL) {
00125         cpl_msg_error(__func__, "Model configuration file is missing") ;
00126         return -1 ;
00127     }
00128     user_wls = crires_extract_filename(frameset, CRIRES_CALPRO_MODEL_FIX_TAB) ;
00129     if (user_wls == NULL) {
00130         cpl_msg_error(__func__, "User specified wavelengths are missing") ;
00131         return -1 ;
00132     }
00133 
00134     /* Retrieve raw frames */
00135     if ((rawframes = crires_extract_frameset(frameset, 
00136                     CRIRES_MODEL_FIX_RAW)) == NULL) {
00137         cpl_msg_error(__func__, "No raw frame in input") ;
00138         return -1 ;
00139     }
00140     ref_frame = cpl_frameset_get_frame(rawframes, 0) ;
00141     ref_fname = cpl_frame_get_filename(ref_frame) ;
00142 
00143     /* Check the model config file validity vs. the current data  */
00144     if (crires_model_config_check(config, ref_fname) != 0) {
00145         cpl_msg_error(__func__, 
00146                 "The model configuration file version is wrong") ;
00147         cpl_frameset_delete(rawframes) ;
00148         return -1 ;
00149     }
00150 
00151     /* Read the input TXT file */
00152     cpl_msg_info(__func__, "Parse the passed TXT file") ;
00153     if (crires_model_fix_parse(user_wls, &pos_x, &pos_y, &chip, &wls)) {
00154         cpl_msg_error(__func__, "Cannot parse the input txt file") ;
00155         cpl_frameset_delete(rawframes) ;
00156         return -1 ;
00157     }
00158 
00159     /* Compute the new configuration file */
00160     cpl_msg_info(__func__, "Compute the new configuration file") ;
00161     cpl_msg_indent_more() ;
00162     if ((new_conf = crires_model_refining_fix(pos_x, pos_y, chip, wls,
00163                     ref_fname, config)) == NULL) {
00164         cpl_msg_error(__func__, "Cannot compute the new configuration file");
00165         cpl_frameset_delete(rawframes) ;
00166         cpl_vector_delete(pos_x) ;
00167         cpl_vector_delete(pos_y) ;
00168         cpl_free(chip) ;
00169         cpl_vector_delete(wls) ;
00170         cpl_msg_indent_less() ;
00171         return -1; 
00172     }
00173     cpl_msg_indent_less() ;
00174 
00175     /* Clean */
00176     cpl_vector_delete(pos_x) ;
00177     cpl_vector_delete(pos_y) ;
00178     cpl_free(chip) ;
00179     cpl_vector_delete(wls) ;
00180     cpl_frameset_delete(rawframes) ;
00181     
00182     /* Save the new configuration file */
00183     cpl_msg_info(__func__, "Save the new configuration file") ;
00184     if (crires_model_fix_save(new_conf, parlist, frameset) == -1) {
00185         cpl_msg_error(__func__, "Cannot save the product");
00186         cpl_table_delete(new_conf) ;
00187         return -1 ;
00188     }
00189     cpl_table_delete(new_conf) ;
00190    
00191     /* Return */
00192     if (cpl_error_get_code()) return -1 ;
00193     else return 0 ;
00194 }
00195 
00196 /*----------------------------------------------------------------------------*/
00204 /*----------------------------------------------------------------------------*/
00205 static int crires_model_fix_save(
00206         cpl_table               *   out_table,
00207         const cpl_parameterlist *   parlist,
00208         cpl_frameset            *   set)
00209 {
00210     cpl_propertylist    *   plist ;
00211 
00212     plist = cpl_propertylist_new();
00213     cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
00214     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
00215             CRIRES_CALPRO_MODEL_CONFIG) ;
00216     cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00217             CRIRES_PROTYPE_MOD_CONF) ;
00218 
00219     /* Use standard saving function */
00220     if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
00221                 NULL, "crires_model_fix", plist, NULL, 
00222                 PACKAGE "/" PACKAGE_VERSION,
00223                 "crires_model_fix.fits") != CPL_ERROR_NONE) {
00224         cpl_msg_error(__func__, "Cannot save the table") ;
00225         return -1 ;
00226     }
00227     cpl_propertylist_delete(plist) ;
00228 
00229     /* Return */
00230     return 0 ;
00231 }
00232 
00233 /*----------------------------------------------------------------------------*/
00251 /*----------------------------------------------------------------------------*/
00252 static int crires_model_fix_parse(
00253         const char  *   txt_file,
00254         cpl_vector  **  pos_x,
00255         cpl_vector  **  pos_y,
00256         int         **  chip,
00257         cpl_vector  **  wls)
00258 {
00259     FILE        *   in;
00260     int             size ;
00261     char            line[1024];
00262     double          x, y, wl;
00263     int             ch ;
00264 
00265     /* Test entries */
00266     if (txt_file == NULL) return -1 ;
00267     if (pos_x == NULL) return -1 ;
00268     if (pos_y == NULL) return -1 ;
00269     if (chip == NULL) return -1 ;
00270     if (wls == NULL) return -1 ;
00271 
00272     /* Open the file */
00273     in = fopen(txt_file, "r");
00274     if (in == NULL) return -1 ;
00275 
00276     /* Get the size */
00277     size = 0 ;
00278     while (fgets(line, 1024, in) != NULL) {
00279         if (line[0] != '#' && sscanf(line, "%lg %lg %d %lg", &x, &y,
00280                     &ch, &wl) == 4) {
00281             size ++ ;
00282         }
00283     }
00284 
00285     /* Create and fill the vectors */
00286     *pos_x = cpl_vector_new(size);
00287     *pos_y = cpl_vector_new(size);
00288     *chip = cpl_malloc(size*sizeof(int)) ;
00289     *wls = cpl_vector_new(size);
00290 
00291     size = 0 ;
00292     rewind(in) ;
00293     while (fgets(line, 1024, in) != NULL) {
00294         if (line[0] != '#' && sscanf(line, "%lg %lg %d %lg", &x, &y,
00295                     &ch, &wl) == 4) {
00296             cpl_vector_set(*pos_x, size, x);
00297             cpl_vector_set(*pos_y, size, y);
00298             (*chip)[size] = ch ;
00299             cpl_vector_set(*wls, size, wl);
00300             size ++ ;
00301         }
00302     }
00303     fclose(in);
00304 
00305     return 0 ;
00306 }

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