CRIRES Pipeline Reference Manual  2.3.2
crires_test_model_anneal.c
00001 /* $Id: crires_test_model_anneal.c,v 1.7 2012-01-12 17:59:31 bristowp 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: bristowp $
00023  * $Date: 2012-01-12 17:59:31 $
00024  * $Revision: 1.7 $
00025  * $Name: not supported by cvs2svn $
00026  */
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 /*-----------------------------------------------------------------------------
00031                                 Includes
00032  -----------------------------------------------------------------------------*/
00033 
00034 #include <cpl.h>
00035 #include <math.h>
00036 
00037 #include <stdlib.h>
00038 #include <stdio.h> 
00039 #include <string.h> 
00040 
00041 #include "irplib_utils.h"
00042 
00043 #include "crires_model_anneal.h"
00044 #include "crires_dfs.h"
00045 #include "crires_utils.h"
00046 #include "crires_pfits.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049                             Functions prototypes
00050  -----------------------------------------------------------------------------*/
00051 
00052 static int crires_test_model_anneal_create(cpl_plugin *) ;
00053 static int crires_test_model_anneal_exec(cpl_plugin *) ;
00054 static int crires_test_model_anneal_destroy(cpl_plugin *) ;
00055 static int crires_test_model_anneal(cpl_parameterlist *, cpl_frameset *) ;
00056 static int crires_test_model_anneal_save(const cpl_table *, cpl_parameterlist *, 
00057         cpl_frameset *) ;
00058 
00059 /*-----------------------------------------------------------------------------
00060                             Static variables
00061  -----------------------------------------------------------------------------*/
00062 
00063 static char crires_test_model_anneal_description[] =
00064 "This recipe facilitates the optimisation of the physical model\nconfiguration given a set of observed detector positions for known\ncalibration features.\nThe files listed in the Set Of Frames (sof-file) must be tagged:\n  start-cfg.fits     CALPRO_MODEL_CONFIG\n  matched-coords.dat CALPRO_MODEL_MEASCOORD\n";
00065 
00066 /*-----------------------------------------------------------------------------
00067                                 Functions code
00068  -----------------------------------------------------------------------------*/
00069 
00070 /*----------------------------------------------------------------------------*/
00079 /*----------------------------------------------------------------------------*/
00080 int cpl_plugin_get_info(cpl_pluginlist * list)
00081 {
00082     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00083     cpl_plugin  *   plugin = &recipe->interface ;
00084 
00085     cpl_plugin_init(plugin,
00086                     CPL_PLUGIN_API,
00087                     CRIRES_BINARY_VERSION,
00088                     CPL_PLUGIN_TYPE_RECIPE,
00089                     "crires_test_model_anneal",
00090                     "Compute optimum config given wvlens and obs detec posns",
00091                     crires_test_model_anneal_description,
00092                     "Paul Bristow",
00093                     "bristowp@eso.org",
00094                     crires_get_license(),
00095                     crires_test_model_anneal_create,
00096                     crires_test_model_anneal_exec,
00097                     crires_test_model_anneal_destroy) ;
00098 
00099     cpl_pluginlist_append(list, plugin) ;
00100     
00101     return 0;
00102 }
00103 
00104 /*----------------------------------------------------------------------------*/
00112 /*----------------------------------------------------------------------------*/
00113 static int crires_test_model_anneal_create(cpl_plugin * plugin)
00114 {
00115     cpl_recipe      * recipe ;
00116 
00117     /* Check that the plugin is part of a valid recipe */
00118     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00119         recipe = (cpl_recipe *)plugin ;
00120     else return -1 ;
00121 
00122     /* Create the parameters list in the cpl_recipe object */
00123     recipe->parameters = cpl_parameterlist_new() ; 
00124 
00125     /* Return */
00126     return 0;
00127 }
00128 /*----------------------------------------------------------------------------*/
00134 /*----------------------------------------------------------------------------*/
00135 static int crires_test_model_anneal_exec(cpl_plugin * plugin)
00136 {
00137     cpl_recipe  *   recipe ;
00138     
00139     /* Get the recipe out of the plugin */
00140     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00141         recipe = (cpl_recipe *)plugin ;
00142     else return -1 ;
00143 
00144     return crires_test_model_anneal(recipe->parameters, recipe->frames) ;
00145 }
00146 
00147 /*----------------------------------------------------------------------------*/
00153 /*----------------------------------------------------------------------------*/
00154 static int crires_test_model_anneal_destroy(cpl_plugin * plugin)
00155 {
00156     cpl_recipe  *   recipe ;
00157     
00158     /* Get the recipe out of the plugin */
00159     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00160         recipe = (cpl_recipe *)plugin ;
00161     else return -1 ;
00162 
00163     cpl_parameterlist_delete(recipe->parameters) ; 
00164     return 0 ;
00165 }
00166 
00167 /*----------------------------------------------------------------------------*/
00174 /*----------------------------------------------------------------------------*/
00175 static int crires_test_model_anneal(
00176         cpl_parameterlist   *   parlist, 
00177         cpl_frameset        *   frameset)
00178 {
00179     cpl_frame           *   cri_config ;
00180     cpl_frame           *   meas_coord ;
00181     cpl_table           *   config_table ;
00182 
00183     /* Identify the RAW and CALIB frames in the input frameset */
00184     if (crires_dfs_set_groups(frameset, NULL)) {
00185         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00186         return -1 ;
00187     }
00188  
00189     /* Retrieve calibration data */
00190     cri_config  = cpl_frameset_find(frameset, CRIRES_CALPRO_MODEL_CONFIG) ;
00191     meas_coord  = cpl_frameset_find(frameset, "CALPRO_MODEL_MEASCOORD") ;
00192 
00193     /* Apply the computation here */
00194     if ((config_table = crires_model_anneal_reduce(
00195                     cpl_frame_get_filename(cri_config),
00196                     cpl_frame_get_filename(meas_coord))) == NULL) {
00197         cpl_msg_error(__func__, "Failed computing the model") ;
00198         return -1 ;
00199     }
00200 
00201     /* Save the result */
00202     cpl_msg_info(__func__, "Save the products") ;
00203     cpl_msg_indent_more() ;
00204     if (crires_test_model_anneal_save((const cpl_table *)config_table, parlist, 
00205                 frameset) == -1) {
00206         cpl_msg_error(__func__, "Cannot save products") ;
00207         cpl_msg_indent_less() ;
00208         cpl_table_delete(config_table) ;
00209         return -1 ;
00210     }
00211     cpl_table_delete(config_table) ;
00212     cpl_msg_indent_less() ;
00213 
00214     /* Return */
00215     if (cpl_error_get_code()) 
00216         return -1 ;
00217     else 
00218         return 0 ;
00219 }
00220 
00221 /*----------------------------------------------------------------------------*/
00229 /*----------------------------------------------------------------------------*/
00230 static int crires_test_model_anneal_save(
00231         const cpl_table     *   out_table,
00232         cpl_parameterlist   *   parlist,
00233         cpl_frameset        *   set)
00234 {
00235     char                    name_o[512] ;
00236     cpl_propertylist    *   plist ;
00237     //cpl_frame           *   ref_frame ;
00238     cpl_frame           *   product_frame ;
00239 
00240     /* Get the reference frame */
00241     //ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00242 
00243     /* Set the file name */
00244     sprintf(name_o, "crires_test_model_anneal_save.fits") ;
00245     cpl_msg_info(__func__, "Writing %s" , name_o) ;
00246 
00247     /* Get FITS header from reference file */
00248     plist = cpl_propertylist_new();
00249     cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
00250 
00251     /* Create product frame */
00252     product_frame = cpl_frame_new() ;
00253     cpl_frame_set_filename(product_frame, name_o) ;
00254     cpl_frame_set_tag(product_frame, CRIRES_CALPRO_MODEL_CONFIG) ;
00255     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
00256     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
00257     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
00258 
00259     /* Add DataFlow keywords */
00260     if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00261             "crires_test_model_anneal", PACKAGE "/" PACKAGE_VERSION,
00262             "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
00263         cpl_msg_warning(__func__, "Problem in the product DFS-compliance") ;
00264         cpl_error_reset() ;
00265     }
00266 
00267     /* Save the file */
00268     if (cpl_table_save(out_table, plist, NULL, name_o,
00269                 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00270         cpl_msg_error(__func__, "Cannot save the product");
00271         cpl_frame_delete(product_frame) ;
00272         cpl_propertylist_delete(plist) ;
00273         return -1 ;
00274     }
00275     cpl_propertylist_delete(plist) ;
00276 
00277     /* Log the saved file in the input frameset */
00278     cpl_frameset_insert(set, product_frame);
00279     /* Return */
00280     return 0 ;
00281 }
00282