KMOS Pipeline Reference Manual 4.5.10
kmos_gen_reflines.c
1/*
2 * This file is part of the KMOS Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <string.h>
29#include <math.h>
30
31#include <cpl.h>
32
33#include "kmo_dfs.h"
34
35/*-----------------------------------------------------------------------------
36 Functions prototypes
37 -----------------------------------------------------------------------------*/
38
39static int kmos_gen_reflines_save(cpl_table *, const cpl_parameterlist *,
40 cpl_frameset *);
41
42static int kmos_gen_reflines_create(cpl_plugin *);
43static int kmos_gen_reflines_exec(cpl_plugin *);
44static int kmos_gen_reflines_destroy(cpl_plugin *);
45static int kmos_gen_reflines(const cpl_parameterlist *, cpl_frameset *);
46
47/*-----------------------------------------------------------------------------
48 * Static variables
49 *----------------------------------------------------------------------------*/
50
51static char kmos_gen_reflines_description[] =
52"This recipe is used to generate the REFLINES calibration file.\n"
53"The sof file contains the name of the input ASCII file\n"
54"tagged with "KMOS_GEN_REFLINES_RAW".\n"
55"The ASCII file must contain seven columns like the output of:\n"
56" dtfits -d -s ' ' kmos_wave_ref_table.fits\n"
57"The six column titles are:\n"
58"FILTER|DETECTOR|WAVELENGTH|REFERENCE| OFFSET| RANGE| CUT\n"
59"The entries are like:\n"
60" HK 3 1.79196 0 210 20 577\n"
61" HK 3 2.25365 4 427 15 71\n"
62" HK 3 2.06129 -1 1313 50 140\n"
63" HK 3 2.32666 4 594 15 32\n"
64" IZ 1 0.912547 -1 775 80 4000\n"
65" IZ 1 0.966044 -1 1150 80 2000\n"
66" IZ 1 1.04729 -1 1730 80 200\n"
67" IZ 1 1.06765 2 128 40 80\n"
68"...\n"
69"This recipe produces 1 file:\n"
70"First product: the table with the configuration for the model.\n" ;
71
72/*----------------------------------------------------------------------------*/
76/*----------------------------------------------------------------------------*/
77
80/*-----------------------------------------------------------------------------
81 Functions code
82 -----------------------------------------------------------------------------*/
83
84/*----------------------------------------------------------------------------*/
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 KMOS_BINARY_VERSION,
102 CPL_PLUGIN_TYPE_RECIPE,
103 "kmos_gen_reflines",
104 "Create REFLINES calibration file",
105 kmos_gen_reflines_description,
106 "Yves Jung",
107 "https://support.eso.org/",
108 kmos_get_license(),
109 kmos_gen_reflines_create,
110 kmos_gen_reflines_exec,
111 kmos_gen_reflines_destroy);
112
113 cpl_pluginlist_append(list, plugin);
114
115 return 0;
116}
117/*----------------------------------------------------------------------------*/
125/*----------------------------------------------------------------------------*/
126static int kmos_gen_reflines_create(cpl_plugin *plugin)
127{
128 cpl_recipe *recipe;
129
130 /* Check that the plugin is part of a valid recipe */
131 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
132 recipe = (cpl_recipe *)plugin;
133 else
134 return -1;
135
136 /* Create the parameters list in the cpl_recipe object */
137 recipe->parameters = cpl_parameterlist_new();
138
139 /* Fill the parameters list */
140
141 return 0 ;
142}
143
144/*----------------------------------------------------------------------------*/
150/*----------------------------------------------------------------------------*/
151static int kmos_gen_reflines_exec(cpl_plugin *plugin)
152{
153 cpl_recipe *recipe;
154
155 /* Get the recipe out of the plugin */
156 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
157 recipe = (cpl_recipe *)plugin;
158 else return -1;
159
160 return kmos_gen_reflines(recipe->parameters, recipe->frames);
161}
162
163/*----------------------------------------------------------------------------*/
169/*----------------------------------------------------------------------------*/
170static int kmos_gen_reflines_destroy(cpl_plugin *plugin)
171{
172 cpl_recipe *recipe;
173
174 /* Get the recipe out of the plugin */
175 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
176 recipe = (cpl_recipe *)plugin;
177 else return -1 ;
178
179 cpl_parameterlist_delete(recipe->parameters);
180 return 0 ;
181}
182
183/*----------------------------------------------------------------------------*/
190/*----------------------------------------------------------------------------*/
191static int kmos_gen_reflines(
192 const 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 band[1024] ;
201 int det, ref, offset, range, cut ;
202 double wave ;
203 cpl_table * tab ;
204 int i ;
205
206 /* Retrieve input parameters */
207
208 /* Identify the RAW and CALIB frames in the input frameset */
209 if (kmo_dfs_set_groups(framelist) != 1) {
210 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
211 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
212 return -1 ;
213 }
214
215 /* Get the config file name */
216 cur_frame = cpl_frameset_get_position(framelist, 0) ;
217 cur_fname = cpl_frame_get_filename(cur_frame) ;
218
219 /* Open the file */
220 if ((in = fopen(cur_fname, "r")) == NULL) {
221 cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
222 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
223 return -1 ;
224 }
225
226 /* Count number of entries */
227 nentries = 0 ;
228 while (fgets(line, 1024, in) != NULL) {
229 if (line[0] != '#' && sscanf(line, "%1023s %d %lg %d %d %d %d",
230 band, &det, &wave, &ref, &offset, &range, &cut) == 7)
231 nentries++ ;
232 }
233 if (nentries == 0) {
234 cpl_msg_error(__func__, "No valid entry in the file") ;
235 fclose(in) ;
236 return -1 ;
237 }
238
239 /* Create the output table */
240 tab = cpl_table_new(nentries) ;
241 cpl_table_new_column(tab, "FILTER", CPL_TYPE_STRING) ;
242 cpl_table_new_column(tab, "DETECTOR", CPL_TYPE_INT) ;
243 cpl_table_new_column(tab, "WAVELENGTH", CPL_TYPE_DOUBLE) ;
244 cpl_table_new_column(tab, "REFERENCE", CPL_TYPE_INT) ;
245 cpl_table_new_column(tab, "OFFSET", CPL_TYPE_INT) ;
246 cpl_table_new_column(tab, "RANGE", CPL_TYPE_INT) ;
247 cpl_table_new_column(tab, "CUT", CPL_TYPE_INT) ;
248
249 /* Fill the table */
250 i = 0 ;
251 rewind(in) ;
252 while (fgets(line, 1024, in) != NULL) {
253 if (line[0] != '#' && sscanf(line, "%1023s %d %lg %d %d %d %d",
254 band, &det, &wave, &ref, &offset, &range, &cut) == 7) {
255 cpl_table_set_string(tab, "FILTER", i, band) ;
256 cpl_table_set_int(tab, "DETECTOR", i, det) ;
257 cpl_table_set_double(tab, "WAVELENGTH", i, wave) ;
258 cpl_table_set_int(tab, "REFERENCE", i, ref) ;
259 cpl_table_set_int(tab, "OFFSET", i, offset) ;
260 cpl_table_set_int(tab, "RANGE", i, range) ;
261 cpl_table_set_int(tab, "CUT", i, cut) ;
262 i++ ;
263 }
264 }
265 fclose(in) ;
266
267 /* Save the table */
268 cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
269 if (kmos_gen_reflines_save(tab, parlist, framelist) == -1) {
270 cpl_msg_error(__func__, "Cannot write the table") ;
271 cpl_table_delete(tab) ;
272 return -1 ;
273 }
274 cpl_table_delete(tab) ;
275 return 0 ;
276}
277
278/*----------------------------------------------------------------------------*/
286/*----------------------------------------------------------------------------*/
287static int kmos_gen_reflines_save(
288 cpl_table * out_table,
289 const cpl_parameterlist * parlist,
290 cpl_frameset * set)
291{
292 cpl_propertylist * plist ;
293 cpl_propertylist * plist_ext ;
294
295 plist = cpl_propertylist_new();
296 cpl_propertylist_append_string(plist, "INSTRUME", "KMOS") ;
297 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, "REF_LINES") ;
298 plist_ext = cpl_propertylist_new();
299 cpl_propertylist_append_string(plist_ext, "EXTNAME", "LIST") ;
300
301 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
302 plist_ext, "kmos_gen_reflines", plist, NULL,
303 PACKAGE "/" PACKAGE_VERSION,
304 "kmos_gen_reflines.fits") != CPL_ERROR_NONE) {
305 cpl_msg_error(__func__, "Cannot save the table") ;
306 cpl_propertylist_delete(plist) ;
307 cpl_propertylist_delete(plist_ext) ;
308 return -1 ;
309 }
310 cpl_propertylist_delete(plist) ;
311 cpl_propertylist_delete(plist_ext) ;
312
313 /* Return */
314 return 0 ;
315}
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.