IIINSTRUMENT Pipeline Reference Manual 1.5.16
sofi_util_genlines.c
1/* $Id: sofi_util_genlines.c,v 1.10 2013-03-12 08:04:50 llundin Exp $
2 *
3 * This file is part of the SOFI 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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: llundin $
23 * $Date: 2013-03-12 08:04:50 $
24 * $Revision: 1.10 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include <cpl.h>
37
38#include "irplib_utils.h"
39
40#include "sofi_utils.h"
41#include "sofi_pfits.h"
42#include "sofi_dfs.h"
43
44/*-----------------------------------------------------------------------------
45 Functions prototypes
46 -----------------------------------------------------------------------------*/
47
48static int sofi_util_genlines_create(cpl_plugin *);
49static int sofi_util_genlines_exec(cpl_plugin *);
50static int sofi_util_genlines_destroy(cpl_plugin *);
51static int sofi_util_genlines(cpl_parameterlist *, cpl_frameset *);
52static int sofi_util_genlines_save(cpl_table *, cpl_parameterlist *,
53 cpl_frameset *);
54
55/*-----------------------------------------------------------------------------
56 Static variables
57 -----------------------------------------------------------------------------*/
58
59static struct {
60 /* Inputs */
61 int fill_blanks;
62 int display;
63 int mode;
64 double wl_factor;
65 /* Outputs */
66} sofi_util_genlines_config;
67
68static char sofi_util_genlines_description[] =
69"This recipe is used to generate spectrum calibration tables.\n"
70"The sof file contains the names of the input ASCII file\n"
71"tagged with "SOFI_UTIL_GENLINES_RAW".\n"
72"The ASCII file must contain two columns:\n"
73"1st: Wavelengths in increasing order (the unit is corrected by\n"
74" the factor option to obtain nanometers).\n"
75"2nd: The atmospheric emission.\n"
76"The ASCII files are in the catalogs/ directory of the SOFI distribution.\n"
77"This recipe produces 1 file:\n"
78"First product: the table with the lines.\n"
79" (PRO CATG = "SOFI_UTIL_GENLINES_OH_CAT") or\n"
80" (PRO CATG = "SOFI_UTIL_GENLINES_XE_CAT") or\n"
81" (PRO CATG = "SOFI_UTIL_GENLINES_NE_CAT")\n";
82
83/*-----------------------------------------------------------------------------
84 Functions code
85 -----------------------------------------------------------------------------*/
86
87/*----------------------------------------------------------------------------*/
95/*----------------------------------------------------------------------------*/
96int cpl_plugin_get_info(cpl_pluginlist * list)
97{
98 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe));
99 cpl_plugin * plugin = &recipe->interface;
100
101 cpl_plugin_init(plugin,
102 CPL_PLUGIN_API,
103 SOFI_BINARY_VERSION,
104 CPL_PLUGIN_TYPE_RECIPE,
105 "sofi_util_genlines",
106 "Generate spectrum calibration FITS tables",
107 sofi_util_genlines_description,
108 "Yves Jung",
109 "yjung@eso.org",
111 sofi_util_genlines_create,
112 sofi_util_genlines_exec,
113 sofi_util_genlines_destroy);
114
115 cpl_pluginlist_append(list, plugin);
116
117 return 0;
118}
119
120/*----------------------------------------------------------------------------*/
129/*----------------------------------------------------------------------------*/
130static int sofi_util_genlines_create(cpl_plugin * plugin)
131{
132 cpl_recipe * recipe;
133 cpl_parameter * p;
134
135 /* Get the recipe out of the plugin */
136 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
137 recipe = (cpl_recipe *)plugin;
138 else return CPL_ERROR_UNSPECIFIED;
139
140 /* Create the parameters list in the cpl_recipe object */
141 recipe->parameters = cpl_parameterlist_new();
142
143 /* Fill the parameters list */
144 /* --fill_blanks */
145 p = cpl_parameter_new_value("sofi.sofi_util_genlines.fill_blanks",
146 CPL_TYPE_BOOL, "flag to fill blanks", "sofi.sofi_util_genlines",
147 FALSE);
148 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fill_blanks");
149 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
150 cpl_parameterlist_append(recipe->parameters, p);
151 /* --display */
152 p = cpl_parameter_new_value("sofi.sofi_util_genlines.display",
153 CPL_TYPE_BOOL, "flag to plot", "sofi.sofi_util_genlines",
154 FALSE);
155 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "display");
156 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
157 cpl_parameterlist_append(recipe->parameters, p);
158 /* --mode */
159 p = cpl_parameter_new_value("sofi.sofi_util_genlines.mode",
160 CPL_TYPE_INT, "1-OH, 2-XE, 3-NE",
161 "sofi.sofi_util_genlines", 1);
162 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mode");
163 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
164 cpl_parameterlist_append(recipe->parameters, p);
165 /* --wl_factor */
166 p = cpl_parameter_new_value("sofi.sofi_util_genlines.wl_factor",
167 CPL_TYPE_DOUBLE, "The factor used to multiply the wl",
168 "sofi.sofi_util_genlines", 1.0);
169 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wl_factor");
170 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
171 cpl_parameterlist_append(recipe->parameters, p);
172
173 return 0;
174}
175
176/*----------------------------------------------------------------------------*/
182/*----------------------------------------------------------------------------*/
183static int sofi_util_genlines_exec(cpl_plugin * plugin)
184{
185 cpl_recipe * recipe;
186
187 /* Get the recipe out of the plugin */
188 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
189 recipe = (cpl_recipe *)plugin;
190 else return CPL_ERROR_UNSPECIFIED;
191
192 return sofi_util_genlines(recipe->parameters, recipe->frames);
193}
194
195/*----------------------------------------------------------------------------*/
201/*----------------------------------------------------------------------------*/
202static int sofi_util_genlines_destroy(cpl_plugin * plugin)
203{
204 cpl_recipe * recipe;
205
206 /* Get the recipe out of the plugin */
207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
208 recipe = (cpl_recipe *)plugin;
209 else return CPL_ERROR_UNSPECIFIED;
210
211 cpl_parameterlist_delete(recipe->parameters);
212 return 0;
213}
214
215/*----------------------------------------------------------------------------*/
224/*----------------------------------------------------------------------------*/
225static int sofi_util_genlines(
226 cpl_parameterlist * parlist,
227 cpl_frameset * framelist)
228{
229 cpl_bivector * bivec;
230 double * pbivec_x;
231 double * pbivec_y;
232 cpl_bivector * bivec_fill;
233 double * pbivec_fill_x;
234 double * pbivec_fill_y;
235 cpl_frame * cur_frame;
236 int nvals, nb_new_vals;
237 double wavel;
238 cpl_table * tab;
239 cpl_parameter * par;
240 int i;
241
242 cpl_ensure_code(!cpl_error_get_code(), cpl_error_get_code());
243
244 /* Initialise */
245 par = NULL;
246
247 /* Retrieve input parameters */
248 /* Fill blanks */
249 par = cpl_parameterlist_find(parlist,
250 "sofi.sofi_util_genlines.fill_blanks");
251 sofi_util_genlines_config.fill_blanks = cpl_parameter_get_bool(par);
252 /* Fill blanks */
253 par = cpl_parameterlist_find(parlist,"sofi.sofi_util_genlines.display");
254 sofi_util_genlines_config.display = cpl_parameter_get_bool(par);
255 /* Mode */
256 par = cpl_parameterlist_find(parlist,"sofi.sofi_util_genlines.mode");
257 sofi_util_genlines_config.mode = cpl_parameter_get_int(par);
258 /* Wavelength factor */
259 par=cpl_parameterlist_find(parlist,"sofi.sofi_util_genlines.wl_factor");
260 sofi_util_genlines_config.wl_factor = cpl_parameter_get_double(par);
261
262 /* Identify the RAW and CALIB frames in the input frameset */
263 if (sofi_dfs_set_groups(framelist)) {
264 cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
265 return CPL_ERROR_UNSPECIFIED;
266 }
267
268 /* Load the file */
269 cur_frame = cpl_frameset_get_position(framelist, 0);
270 if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
271 cpl_msg_error(cpl_func, "Cannot load the file in the bivector");
272 return CPL_ERROR_UNSPECIFIED;
273 }
274 nvals = cpl_bivector_get_size(bivec);
275
276 /* If fill_blanks is requested */
277 if (sofi_util_genlines_config.fill_blanks) {
278 nb_new_vals = 3 * nvals;
279 bivec_fill = cpl_bivector_new(nb_new_vals);
280 pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill);
281 pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill);
282 pbivec_x = cpl_bivector_get_x_data(bivec);
283 pbivec_y = cpl_bivector_get_y_data(bivec);
284 for (i=0 ; i<nvals ; i++) {
285 wavel = pbivec_x[i] * sofi_util_genlines_config.wl_factor;
286 pbivec_fill_x[3*i] = wavel - 0.01;
287 pbivec_fill_y[3*i] = 0.0;
288 pbivec_fill_x[3*i+1] = wavel;
289 pbivec_fill_y[3*i+1] = pbivec_y[i];
290 pbivec_fill_x[3*i+2] = wavel + 0.01;
291 pbivec_fill_y[3*i+2] = 0.0;
292 }
293 cpl_bivector_delete(bivec);
294 bivec = bivec_fill;
295 bivec_fill = NULL;
296 nvals = cpl_bivector_get_size(bivec);
297 } else {
298 cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
299 sofi_util_genlines_config.wl_factor);
300 }
301
302 /* Display if requested */
303 if (sofi_util_genlines_config.display) {
304 cpl_plot_bivector(
305 "set grid;set xlabel 'Wavelength (A)';set ylabel 'Emission';",
306 "t 'Catalog lines' w lines", "", bivec);
307 }
308
309 /* Allocate the data container */
310 tab = cpl_table_new(nvals);
311 cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
312 SOFI_COL_WAVELENGTH);
313 cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec),
314 SOFI_COL_EMISSION);
315
316 /* Save the table */
317 cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
318 if (sofi_util_genlines_save(tab, parlist, framelist) == -1) {
319 cpl_msg_error(cpl_func, "Cannot write the table");
320 cpl_bivector_delete(bivec);
321 cpl_table_unwrap(tab, SOFI_COL_WAVELENGTH);
322 cpl_table_unwrap(tab, SOFI_COL_EMISSION);
323 cpl_table_delete(tab);
324 return CPL_ERROR_UNSPECIFIED;
325 }
326 cpl_bivector_delete(bivec);
327 cpl_table_unwrap(tab, SOFI_COL_WAVELENGTH);
328 cpl_table_unwrap(tab, SOFI_COL_EMISSION);
329 cpl_table_delete(tab);
330 return 0;
331}
332
333/*----------------------------------------------------------------------------*/
341/*----------------------------------------------------------------------------*/
342static int sofi_util_genlines_save(
343 cpl_table * out_table,
344 cpl_parameterlist * parlist,
345 cpl_frameset * set)
346{
347 char name_o[512];
348 cpl_propertylist * plist;
349 cpl_frame * product_frame;
350
351 /* Set the file name */
352 sprintf(name_o, "sofi_util_genlines_save.fits");
353 cpl_msg_info(cpl_func, "Writing %s" , name_o);
354
355 /* Get FITS header from reference file */
356 plist = cpl_propertylist_new();
357 cpl_propertylist_append_string(plist, "INSTRUME", "SOFI");
358
359 /* Create product frame */
360 product_frame = cpl_frame_new();
361 cpl_frame_set_filename(product_frame, name_o);
362 if (sofi_util_genlines_config.mode == 1)
363 cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_OH_CAT);
364 else if (sofi_util_genlines_config.mode == 2)
365 cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_XE_CAT);
366 else if (sofi_util_genlines_config.mode == 3)
367 cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_NE_CAT);
368 else
369 cpl_frame_set_tag(product_frame, "UNKNOWN");
370
371 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
372 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
373 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
374
375 /* Add DataFlow keywords */
376 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
377 "sofi_util_genlines", PACKAGE "/" PACKAGE_VERSION,
378 "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
379 cpl_msg_warning(cpl_func, "Problem in the product DFS-compliance");
380 cpl_error_reset();
381 }
382
383 /* Save the file */
384 if (cpl_table_save(out_table, plist, NULL, name_o,
385 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
386 cpl_msg_error(cpl_func, "Cannot save the product");
387 cpl_frame_delete(product_frame);
388 cpl_propertylist_delete(plist);
389 return CPL_ERROR_UNSPECIFIED;
390 }
391 cpl_propertylist_delete(plist);
392
393 /* Log the saved file in the input frameset */
394 cpl_frameset_insert(set, product_frame);
395 /* Return */
396 return 0;
397}
int sofi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition sofi_dfs.c:60
const char * sofi_get_license(void)
Get the pipeline copyright and license.
Definition sofi_utils.c:60