CRIRES Pipeline Reference Manual  2.3.15
crires_test_model_anneal.c
1 /* $Id: crires_test_model_anneal.c,v 1.7 2012-01-12 17:59:31 bristowp Exp $
2  *
3  * This file is part of the CRIRES Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: bristowp $
23  * $Date: 2012-01-12 17:59:31 $
24  * $Revision: 1.7 $
25  * $Name: not supported by cvs2svn $
26  */
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 /*-----------------------------------------------------------------------------
31  Includes
32  -----------------------------------------------------------------------------*/
33 
34 #include <cpl.h>
35 #include <math.h>
36 
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 
41 #include "irplib_utils.h"
42 
43 #include "crires_model_anneal.h"
44 #include "crires_dfs.h"
45 #include "crires_utils.h"
46 #include "crires_pfits.h"
47 
48 /*-----------------------------------------------------------------------------
49  Functions prototypes
50  -----------------------------------------------------------------------------*/
51 
52 static int crires_test_model_anneal_create(cpl_plugin *) ;
53 static int crires_test_model_anneal_exec(cpl_plugin *) ;
54 static int crires_test_model_anneal_destroy(cpl_plugin *) ;
55 static int crires_test_model_anneal(cpl_parameterlist *, cpl_frameset *) ;
56 static int crires_test_model_anneal_save(const cpl_table *, cpl_parameterlist *,
57  cpl_frameset *) ;
58 
59 /*-----------------------------------------------------------------------------
60  Static variables
61  -----------------------------------------------------------------------------*/
62 
63 static char crires_test_model_anneal_description[] =
64 "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";
65 
66 /*-----------------------------------------------------------------------------
67  Functions code
68  -----------------------------------------------------------------------------*/
69 
70 /*----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------*/
80 int cpl_plugin_get_info(cpl_pluginlist * list)
81 {
82  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
83  cpl_plugin * plugin = &recipe->interface ;
84 
85  cpl_plugin_init(plugin,
86  CPL_PLUGIN_API,
87  CRIRES_BINARY_VERSION,
88  CPL_PLUGIN_TYPE_RECIPE,
89  "crires_test_model_anneal",
90  "Compute optimum config given wvlens and obs detec posns",
91  crires_test_model_anneal_description,
92  "Paul Bristow",
93  "bristowp@eso.org",
94  crires_get_license(),
95  crires_test_model_anneal_create,
96  crires_test_model_anneal_exec,
97  crires_test_model_anneal_destroy) ;
98 
99  cpl_pluginlist_append(list, plugin) ;
100 
101  return 0;
102 }
103 
104 /*----------------------------------------------------------------------------*/
112 /*----------------------------------------------------------------------------*/
113 static int crires_test_model_anneal_create(cpl_plugin * plugin)
114 {
115  cpl_recipe * recipe ;
116 
117  /* Check that the plugin is part of a valid recipe */
118  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
119  recipe = (cpl_recipe *)plugin ;
120  else return -1 ;
121 
122  /* Create the parameters list in the cpl_recipe object */
123  recipe->parameters = cpl_parameterlist_new() ;
124 
125  /* Return */
126  return 0;
127 }
128 /*----------------------------------------------------------------------------*/
134 /*----------------------------------------------------------------------------*/
135 static int crires_test_model_anneal_exec(cpl_plugin * plugin)
136 {
137  cpl_recipe * recipe ;
138 
139  /* Get the recipe out of the plugin */
140  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
141  recipe = (cpl_recipe *)plugin ;
142  else return -1 ;
143 
144  return crires_test_model_anneal(recipe->parameters, recipe->frames) ;
145 }
146 
147 /*----------------------------------------------------------------------------*/
153 /*----------------------------------------------------------------------------*/
154 static int crires_test_model_anneal_destroy(cpl_plugin * plugin)
155 {
156  cpl_recipe * recipe ;
157 
158  /* Get the recipe out of the plugin */
159  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
160  recipe = (cpl_recipe *)plugin ;
161  else return -1 ;
162 
163  cpl_parameterlist_delete(recipe->parameters) ;
164  return 0 ;
165 }
166 
167 /*----------------------------------------------------------------------------*/
174 /*----------------------------------------------------------------------------*/
175 static int crires_test_model_anneal(
176  cpl_parameterlist * parlist,
177  cpl_frameset * frameset)
178 {
179  cpl_frame * cri_config ;
180  cpl_frame * meas_coord ;
181  cpl_table * config_table ;
182 
183  /* Identify the RAW and CALIB frames in the input frameset */
184  if (crires_dfs_set_groups(frameset, NULL)) {
185  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
186  return -1 ;
187  }
188 
189  /* Retrieve calibration data */
190  cri_config = cpl_frameset_find(frameset, CRIRES_CALPRO_MODEL_CONFIG) ;
191  meas_coord = cpl_frameset_find(frameset, "CALPRO_MODEL_MEASCOORD") ;
192 
193  /* Apply the computation here */
194  if ((config_table = crires_model_anneal_reduce(
195  cpl_frame_get_filename(cri_config),
196  cpl_frame_get_filename(meas_coord))) == NULL) {
197  cpl_msg_error(__func__, "Failed computing the model") ;
198  return -1 ;
199  }
200 
201  /* Save the result */
202  cpl_msg_info(__func__, "Save the products") ;
203  cpl_msg_indent_more() ;
204  if (crires_test_model_anneal_save((const cpl_table *)config_table, parlist,
205  frameset) == -1) {
206  cpl_msg_error(__func__, "Cannot save products") ;
207  cpl_msg_indent_less() ;
208  cpl_table_delete(config_table) ;
209  return -1 ;
210  }
211  cpl_table_delete(config_table) ;
212  cpl_msg_indent_less() ;
213 
214  /* Return */
215  if (cpl_error_get_code())
216  return -1 ;
217  else
218  return 0 ;
219 }
220 
221 /*----------------------------------------------------------------------------*/
229 /*----------------------------------------------------------------------------*/
230 static int crires_test_model_anneal_save(
231  const cpl_table * out_table,
232  cpl_parameterlist * parlist,
233  cpl_frameset * set)
234 {
235  char name_o[512] ;
236  cpl_propertylist * plist ;
237  //cpl_frame * ref_frame ;
238  cpl_frame * product_frame ;
239 
240  /* Get the reference frame */
241  //ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
242 
243  /* Set the file name */
244  sprintf(name_o, "crires_test_model_anneal_save.fits") ;
245  cpl_msg_info(__func__, "Writing %s" , name_o) ;
246 
247  /* Get FITS header from reference file */
248  plist = cpl_propertylist_new();
249  cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
250 
251  /* Create product frame */
252  product_frame = cpl_frame_new() ;
253  cpl_frame_set_filename(product_frame, name_o) ;
254  cpl_frame_set_tag(product_frame, CRIRES_CALPRO_MODEL_CONFIG) ;
255  cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
256  cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
257  cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
258 
259  /* Add DataFlow keywords */
260  if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
261  "crires_test_model_anneal", PACKAGE "/" PACKAGE_VERSION,
262  "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
263  cpl_msg_warning(__func__, "Problem in the product DFS-compliance") ;
264  cpl_error_reset() ;
265  }
266 
267  /* Save the file */
268  if (cpl_table_save(out_table, plist, NULL, name_o,
269  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
270  cpl_msg_error(__func__, "Cannot save the product");
271  cpl_frame_delete(product_frame) ;
272  cpl_propertylist_delete(plist) ;
273  return -1 ;
274  }
275  cpl_propertylist_delete(plist) ;
276 
277  /* Log the saved file in the input frameset */
278  cpl_frameset_insert(set, product_frame);
279  /* Return */
280  return 0 ;
281 }
282