X-shooter Pipeline Reference Manual 3.8.15
xsh_util_genconfig.c
Go to the documentation of this file.
1/* $Id: xsh_util_genconfig.c,v 1.10 2011-12-02 14:15:46 amodigli Exp $
2 *
3 * This file is part of the ESO X-shooter Pipeline
4 * Copyright (C) 2006 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: amodigli $
23 * $Date: 2011-12-02 14:15:46 $
24 * $Revision: 1.10 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
41/*-----------------------------------------------------------------------------
42 Includes
43 -----------------------------------------------------------------------------*/
44
45#include <cpl.h>
46
47#include "xsh_utils.h"
48#include "xsh_pfits.h"
49#include "xsh_dfs.h"
50
51/*-----------------------------------------------------------------------------
52 Functions prototypes
53 -----------------------------------------------------------------------------*/
54
55static int xsh_util_genconfig_create(cpl_plugin *);
56static int xsh_util_genconfig_exec(cpl_plugin *);
57static int xsh_util_genconfig_destroy(cpl_plugin *);
58static int xsh_util_genconfig(cpl_parameterlist *, cpl_frameset *);
59static int xsh_util_genconfig_save(cpl_table *, cpl_parameterlist *,
60 cpl_frameset *);
61
62/*-----------------------------------------------------------------------------
63 Static variables
64 -----------------------------------------------------------------------------*/
65
67"This recipe is used to generate the model configuration file.\n"
68"The sof file contains the names of the input ASCII file\n"
69"tagged with "XSH_UTIL_GENCONFIG_RAW".\n"
70"The ASCII file must contain five columns:\n"
71"1st: The best guess value\n"
72"2nd: The low limit\n"
73"3th: The high limit\n"
74"4th: The Flag to recompute or not\n"
75"5th: Name of the Parameter\n"
76"The ASCII files are in the catalogs/ directory of the XSH distribution.\n"
77"This recipe produces 1 file:\n"
78"First product: the table with the configuration for the model.\n"
79" (PRO CATG = XSH_MOD_CFG_TAB_ARM)\n" ;
80
81/*-----------------------------------------------------------------------------
82 Functions code
83 -----------------------------------------------------------------------------*/
84
85/*----------------------------------------------------------------------------*/
93/*----------------------------------------------------------------------------*/
94int cpl_plugin_get_info(cpl_pluginlist * list)
95{
96 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe));
97 cpl_plugin * plugin = &recipe->interface;
98
99 cpl_plugin_init(plugin,
100 CPL_PLUGIN_API,
101 XSH_BINARY_VERSION,
102 CPL_PLUGIN_TYPE_RECIPE,
103 "xsh_util_genconfig",
104 "Generate spectrum calibration FITS tables",
106 "Yves Jung",
107 "yjung@eso.org",
112
113 cpl_pluginlist_append(list, plugin);
114
115 return 0 ;
116}
117
118/*----------------------------------------------------------------------------*/
127/*----------------------------------------------------------------------------*/
128static int xsh_util_genconfig_create(cpl_plugin * plugin)
129{
130 cpl_recipe * recipe ;
131
132 /* Get the recipe out of the plugin */
133 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
134 recipe = (cpl_recipe *)plugin ;
135 else return -1 ;
136
137 /* Create the parameters list in the cpl_recipe object */
138 recipe->parameters = cpl_parameterlist_new() ;
139
140 return 0 ;
141}
142
143/*----------------------------------------------------------------------------*/
149/*----------------------------------------------------------------------------*/
150static int xsh_util_genconfig_exec(cpl_plugin * plugin)
151{
152 cpl_recipe * recipe ;
153
154 /* Get the recipe out of the plugin */
155 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
156 recipe = (cpl_recipe *)plugin ;
157 else return -1 ;
158
159 return xsh_util_genconfig(recipe->parameters, recipe->frames) ;
160}
161
162/*----------------------------------------------------------------------------*/
168/*----------------------------------------------------------------------------*/
169static int xsh_util_genconfig_destroy(cpl_plugin * plugin)
170{
171 cpl_recipe * recipe ;
172
173 /* Get the recipe out of the plugin */
174 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
175 recipe = (cpl_recipe *)plugin ;
176 else return -1 ;
177
178 cpl_parameterlist_delete(recipe->parameters) ;
179 return 0 ;
180}
181
182/*----------------------------------------------------------------------------*/
190/*----------------------------------------------------------------------------*/
192 cpl_parameterlist * parlist,
193 cpl_frameset * framelist)
194{
195 FILE * in ;
196 char line[1024];
197 cpl_frame * cur_frame ;
198 const char * cur_fname ;
199 int nentries ;
200 char name[1024] ;
201 double best, low, high ;
202 int flag ;
203 cpl_table * tab ;
204 int i ;
205
206 /* Identify the RAW and CALIB frames in the input frameset
207 if (xsh_dfs_set_groups(framelist)) {
208 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
209 return -1 ;
210 }*/
211
212 /* Get the config file name */
213 cur_frame = cpl_frameset_get_frame(framelist, 0) ;
214 cur_fname = cpl_frame_get_filename(cur_frame) ;
215
216 /* Open the file */
217 if ((in = fopen(cur_fname, "r")) == NULL) {
218 cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
219 return -1 ;
220 }
221
222 /* Count number of entries */
223 nentries = 0 ;
224 while (fgets(line, 1024, in) != NULL) {
225 /* Note: update the string format (the %__s) if name changes size.
226 * Remember: field width must equal 1 less than sizeof(name). */
227 if (line[0] != '#' && sscanf(line, "%64lg %64lg %64lg %64d %1023s",
228 &best, &low, &high, &flag, name) == 5) nentries++ ;
229 }
230 if (nentries == 0) {
231 cpl_msg_error(__func__, "No valid entry in the file") ;
232 fclose(in) ;
233 return -1 ;
234 }
235
236 /* Create the output table */
237 tab = cpl_table_new(nentries) ;
238 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_BEST, CPL_TYPE_DOUBLE) ;
239 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_LOW, CPL_TYPE_DOUBLE) ;
240 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_HIGH, CPL_TYPE_DOUBLE) ;
241 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_FLAG, CPL_TYPE_INT) ;
242 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_NAME, CPL_TYPE_STRING) ;
243
244 /* Fill the table */
245 i = 0 ;
246 rewind(in) ;
247 while (fgets(line, 1024, in) != NULL) {
248 if (line[0] != '#' && sscanf(line, "%64lg %64lg %64lg %64d %1023s",
249 &best, &low, &high, &flag, name) == 5) {
250 cpl_table_set_string(tab, XSH_COL_MODEL_CONF_NAME, i, name) ;
251 cpl_table_set_double(tab, XSH_COL_MODEL_CONF_BEST, i, best) ;
252 cpl_table_set_double(tab, XSH_COL_MODEL_CONF_LOW, i, low) ;
253 cpl_table_set_double(tab, XSH_COL_MODEL_CONF_HIGH, i, high) ;
254 cpl_table_set_int(tab, XSH_COL_MODEL_CONF_FLAG, i, flag) ;
255 i++ ;
256 }
257 }
258 fclose(in) ;
259
260 /* Save the table */
261 cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
262 if (xsh_util_genconfig_save(tab, parlist, framelist) == -1) {
263 cpl_msg_error(__func__, "Cannot write the table") ;
264 cpl_table_delete(tab) ;
265 return -1 ;
266 }
267 cpl_table_delete(tab) ;
268 return 0 ;
269}
270
271/*----------------------------------------------------------------------------*/
279/*---------------------------------------------------------------------------*/
281 cpl_table * out_table,
282 cpl_parameterlist * parlist,
283 cpl_frameset * set)
284{
285 char name_o[512] ;
286 cpl_propertylist * plist ;
287 cpl_frame * product_frame ;
288
289 /* Get the reference frame
290 ref_frame = irplib_frameset_get_first_from_group(set,
291 CPL_FRAME_GROUP_RAW) ;
292 */
293
294 /* Set the file name */
295 sprintf(name_o, "xsh_util_genconfig_save.fits") ;
296 cpl_msg_info(__func__, "Writing %s" , name_o) ;
297
298 /* Get FITS header from reference file */
299 plist = cpl_propertylist_new();
300 cpl_propertylist_append_string(plist, "INSTRUME", "XSH") ;
301
302 /* Create product frame */
303 product_frame = cpl_frame_new() ;
304 cpl_frame_set_filename(product_frame, name_o) ;
305 cpl_frame_set_tag(product_frame, XSH_MOD_CFG) ;
306 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
307 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
308 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
309
310 /* Add DataFlow keywords */
311 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
312 "xsh_util_genconfig", PACKAGE "/" PACKAGE_VERSION,
313 "PRO-1.15",NULL)!=CPL_ERROR_NONE) {
314 cpl_msg_warning(__func__, "Problem in the product DFS-compliance") ;
315 cpl_error_reset() ;
316 }
317
318 /* Save the file */
319 if (cpl_table_save(out_table, plist, NULL, name_o,
320 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
321 cpl_msg_error(__func__, "Cannot save the product");
322 cpl_frame_delete(product_frame) ;
323 cpl_propertylist_delete(plist) ;
324 return -1 ;
325 }
326 cpl_propertylist_delete(plist) ;
327
328 /* Log the saved file in the input frameset */
329 cpl_frameset_insert(set, product_frame);
330 /* Return */
331 return 0 ;
332}
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_2dmap.c:151
static int xsh_util_genconfig_save(cpl_table *, cpl_parameterlist *, cpl_frameset *)
Save the product of the recipe.
static char xsh_util_genconfig_description[]
static int xsh_util_genconfig_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
static int xsh_util_genconfig_create(cpl_plugin *)
Setup the recipe options
static int xsh_util_genconfig(cpl_parameterlist *, cpl_frameset *)
The FITS file creation occurs here.
static int xsh_util_genconfig_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
#define XSH_UTIL_GENCONFIG_RAW
Definition: xsh_dfs.h:1298
#define XSH_MOD_CFG
Definition: xsh_dfs.h:1261
#define XSH_COL_MODEL_CONF_HIGH
Definition: xsh_dfs.h:1296
#define XSH_COL_MODEL_CONF_BEST
Definition: xsh_dfs.h:1294
#define XSH_COL_MODEL_CONF_LOW
Definition: xsh_dfs.h:1295
#define XSH_COL_MODEL_CONF_NAME
Definition: xsh_dfs.h:1292
#define XSH_COL_MODEL_CONF_FLAG
Definition: xsh_dfs.h:1297