IIINSTRUMENT Pipeline Reference Manual  6.2.2
isaac_util_genlines.c
1 /* $Id: isaac_util_genlines.c,v 1.9 2013-03-12 08:06:48 llundin Exp $
2  *
3  * This file is part of the ISAAC 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:06:48 $
24  * $Revision: 1.9 $
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 "isaac_utils.h"
41 #include "isaac_pfits.h"
42 #include "isaac_dfs.h"
43 
44 /*-----------------------------------------------------------------------------
45  Functions prototypes
46  -----------------------------------------------------------------------------*/
47 
48 static int isaac_util_genlines_create(cpl_plugin *);
49 static int isaac_util_genlines_exec(cpl_plugin *);
50 static int isaac_util_genlines_destroy(cpl_plugin *);
51 static int isaac_util_genlines(cpl_parameterlist *, cpl_frameset *);
52 static int isaac_util_genlines_save(cpl_table *, cpl_parameterlist *,
53  cpl_frameset *);
54 
55 /*-----------------------------------------------------------------------------
56  Static variables
57  -----------------------------------------------------------------------------*/
58 
59 static struct {
60  /* Inputs */
61  int fill_blanks;
62  int display;
63  int mode;
64  double wl_factor;
65  /* Outputs */
66 } isaac_util_genlines_config;
67 
68 static char isaac_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 "ISAAC_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 ISAAC distribution.\n"
77 "This recipe produces 1 file:\n"
78 "First product: the table with the lines.\n"
79 " (PRO CATG = "ISAAC_UTIL_GENLINES_OH_CAT") or\n"
80 " (PRO CATG = "ISAAC_UTIL_GENLINES_XE_CAT") or\n"
81 " (PRO CATG = "ISAAC_UTIL_GENLINES_AR_CAT")\n";
82 
83 /*-----------------------------------------------------------------------------
84  Functions code
85  -----------------------------------------------------------------------------*/
86 
87 /*----------------------------------------------------------------------------*/
95 /*----------------------------------------------------------------------------*/
96 int 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  ISAAC_BINARY_VERSION,
104  CPL_PLUGIN_TYPE_RECIPE,
105  "isaac_util_genlines",
106  "Generate spectrum calibration FITS tables",
107  isaac_util_genlines_description,
108  "Lars Lundin",
109  PACKAGE_BUGREPORT,
111  isaac_util_genlines_create,
112  isaac_util_genlines_exec,
113  isaac_util_genlines_destroy);
114 
115  cpl_pluginlist_append(list, plugin);
116 
117  return 0;
118 }
119 
120 /*----------------------------------------------------------------------------*/
129 /*----------------------------------------------------------------------------*/
130 static int isaac_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("isaac.isaac_util_genlines.fill_blanks",
146  CPL_TYPE_BOOL, "flag to fill blanks", "isaac.isaac_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("isaac.isaac_util_genlines.display",
153  CPL_TYPE_BOOL, "flag to plot", "isaac.isaac_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("isaac.isaac_util_genlines.mode",
160  CPL_TYPE_INT, "1-OH, 2-XE, 3-AR",
161  "isaac.isaac_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("isaac.isaac_util_genlines.wl_factor",
167  CPL_TYPE_DOUBLE, "The factor used to multiply the wl",
168  "isaac.isaac_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 /*----------------------------------------------------------------------------*/
183 static int isaac_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 isaac_util_genlines(recipe->parameters, recipe->frames);
193 }
194 
195 /*----------------------------------------------------------------------------*/
201 /*----------------------------------------------------------------------------*/
202 static int isaac_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 /*----------------------------------------------------------------------------*/
225 static int isaac_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  /* Initialise */
243  par = NULL;
244 
245  /* Retrieve input parameters */
246  /* Fill blanks */
247  par = cpl_parameterlist_find(parlist,
248  "isaac.isaac_util_genlines.fill_blanks");
249  isaac_util_genlines_config.fill_blanks = cpl_parameter_get_bool(par);
250  /* Fill blanks */
251  par = cpl_parameterlist_find(parlist,"isaac.isaac_util_genlines.display");
252  isaac_util_genlines_config.display = cpl_parameter_get_bool(par);
253  /* Mode */
254  par = cpl_parameterlist_find(parlist,"isaac.isaac_util_genlines.mode");
255  isaac_util_genlines_config.mode = cpl_parameter_get_int(par);
256  /* Wavelength factor */
257  par=cpl_parameterlist_find(parlist,"isaac.isaac_util_genlines.wl_factor");
258  isaac_util_genlines_config.wl_factor = cpl_parameter_get_double(par);
259 
260  /* Identify the RAW and CALIB frames in the input frameset */
261  if (isaac_dfs_set_groups(framelist)) {
262  cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
263  return CPL_ERROR_UNSPECIFIED;
264  }
265 
266  /* Load the file */
267  cur_frame = cpl_frameset_get_position(framelist, 0);
268  if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
269  cpl_msg_error(cpl_func, "Cannot load the file in the bivector");
270  return CPL_ERROR_UNSPECIFIED;
271  }
272  nvals = cpl_bivector_get_size(bivec);
273 
274  /* If fill_blanks is requested */
275  if (isaac_util_genlines_config.fill_blanks) {
276  nb_new_vals = 3 * nvals;
277  bivec_fill = cpl_bivector_new(nb_new_vals);
278  pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill);
279  pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill);
280  pbivec_x = cpl_bivector_get_x_data(bivec);
281  pbivec_y = cpl_bivector_get_y_data(bivec);
282  for (i=0; i<nvals; i++) {
283  wavel = pbivec_x[i] * isaac_util_genlines_config.wl_factor;
284  pbivec_fill_x[3*i] = wavel - 0.01;
285  pbivec_fill_y[3*i] = 0.0;
286  pbivec_fill_x[3*i+1] = wavel;
287  pbivec_fill_y[3*i+1] = pbivec_y[i];
288  pbivec_fill_x[3*i+2] = wavel + 0.01;
289  pbivec_fill_y[3*i+2] = 0.0;
290  }
291  cpl_bivector_delete(bivec);
292  bivec = bivec_fill;
293  bivec_fill = NULL;
294  nvals = cpl_bivector_get_size(bivec);
295  } else {
296  cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
297  isaac_util_genlines_config.wl_factor);
298  }
299 
300  /* Display if requested */
301  if (isaac_util_genlines_config.display) {
302  cpl_plot_bivector(
303  "set grid;set xlabel 'Wavelength (A)';set ylabel 'Emission';",
304  "t 'Catalog lines' w lines", "", bivec);
305  }
306 
307  /* Allocate the data container */
308  tab = cpl_table_new(nvals);
309  cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
310  ISAAC_COL_WAVELENGTH);
311  cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec),
312  ISAAC_COL_EMISSION);
313 
314  /* Save the table */
315  cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
316  if (isaac_util_genlines_save(tab, parlist, framelist) == -1) {
317  cpl_msg_error(cpl_func, "Cannot write the table");
318  cpl_bivector_delete(bivec);
319  cpl_table_unwrap(tab, ISAAC_COL_WAVELENGTH);
320  cpl_table_unwrap(tab, ISAAC_COL_EMISSION);
321  cpl_table_delete(tab);
322  return CPL_ERROR_UNSPECIFIED;
323  }
324  cpl_bivector_delete(bivec);
325  cpl_table_unwrap(tab, ISAAC_COL_WAVELENGTH);
326  cpl_table_unwrap(tab, ISAAC_COL_EMISSION);
327  cpl_table_delete(tab);
328  return 0;
329 }
330 
331 /*----------------------------------------------------------------------------*/
339 /*----------------------------------------------------------------------------*/
340 static int isaac_util_genlines_save(
341  cpl_table * out_table,
342  cpl_parameterlist * parlist,
343  cpl_frameset * set)
344 {
345  char name_o[512];
346  cpl_propertylist * plist;
347  cpl_frame * product_frame;
348 
349  /* Set the file name */
350  sprintf(name_o, "isaac_util_genlines_save.fits");
351  cpl_msg_info(cpl_func, "Writing %s" , name_o);
352 
353  /* Get FITS header from reference file */
354  plist = cpl_propertylist_new();
355  cpl_propertylist_append_string(plist, "INSTRUME", "ISAAC");
356 
357  /* Create product frame */
358  product_frame = cpl_frame_new();
359  cpl_frame_set_filename(product_frame, name_o);
360  if (isaac_util_genlines_config.mode == 1)
361  cpl_frame_set_tag(product_frame, ISAAC_UTIL_GENLINES_OH_CAT);
362  else if (isaac_util_genlines_config.mode == 2)
363  cpl_frame_set_tag(product_frame, ISAAC_UTIL_GENLINES_XE_CAT);
364  else if (isaac_util_genlines_config.mode == 3)
365  cpl_frame_set_tag(product_frame, ISAAC_UTIL_GENLINES_AR_CAT);
366  else
367  cpl_frame_set_tag(product_frame, "UNKNOWN");
368 
369  cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
370  cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
371  cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
372 
373  /* Add DataFlow keywords */
374  if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
375  "isaac_util_genlines", PACKAGE "/" PACKAGE_VERSION,
376  "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
377  cpl_msg_warning(cpl_func, "Problem in the product DFS-compliance");
378  cpl_error_reset();
379  }
380 
381  /* Save the file */
382  if (cpl_table_save(out_table, plist, NULL, name_o,
383  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
384  cpl_msg_error(cpl_func, "Cannot save the product");
385  cpl_frame_delete(product_frame);
386  cpl_propertylist_delete(plist);
387  return CPL_ERROR_UNSPECIFIED;
388  }
389  cpl_propertylist_delete(plist);
390 
391  /* Log the saved file in the input frameset */
392  cpl_frameset_insert(set, product_frame);
393  /* Return */
394  return 0;
395 }
int isaac_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: isaac_dfs.c:60
const char * isaac_get_license(void)
Get the pipeline copyright and license.
Definition: isaac_utils.c:62