CR2RE Pipeline Reference Manual 1.6.2
cr2res_util_plot.c
1/*
2 * This file is part of the CR2RES 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#include "hdrl.h"
33#include "irplib_wlxcorr.h"
34
35#include "cr2res_utils.h"
36#include "cr2res_extract.h"
37#include "cr2res_pfits.h"
38#include "cr2res_dfs.h"
39#include "cr2res_io.h"
40
41/*-----------------------------------------------------------------------------
42 Define
43 -----------------------------------------------------------------------------*/
44
45#define RECIPE_STRING "cr2res_util_plot"
46
47/*-----------------------------------------------------------------------------
48 Plugin registration
49 -----------------------------------------------------------------------------*/
50
51int cpl_plugin_get_info(cpl_pluginlist * list);
52
53/*-----------------------------------------------------------------------------
54 Functions prototypes
55 -----------------------------------------------------------------------------*/
56static int cr2res_util_plot_slit_func(cpl_table *, int, int) ;
57static int cr2res_util_plot_slit_func_one(cpl_table *, const char *,
58 const char *, const char *) ;
59static int cr2res_util_plot_spec_1d(cpl_table *, cpl_table *, int, int,
60 int, double, double);
61static int cr2res_util_plot_spec_1d_one(cpl_table *, cpl_table *,
62 const char *, int, const char *, const char *, const char *) ;
63static int cr2res_util_plot_create(cpl_plugin *);
64static int cr2res_util_plot_exec(cpl_plugin *);
65static int cr2res_util_plot_destroy(cpl_plugin *);
66static int cr2res_util_plot(cpl_frameset *, const cpl_parameterlist *);
67
68/*-----------------------------------------------------------------------------
69 Static variables
70 -----------------------------------------------------------------------------*/
71
72static char cr2res_util_plot_description[] = "\
73Plotting \n\
74 This utility detects the type of the first passed file, and plots \n\
75 relevant graphs accordingly. \n\
76 \n\
77 Inputs \n\
78 first.fits " CR2RES_DRSTYPE_CATALOG " [1] \n\
79 or " CR2RES_EXTRACT_1D_DRSTYPE " \n\
80 second.fits " CR2RES_DRSTYPE_CATALOG " [0 to 1] \n\
81 Outputs \n\
82 - \n\
83 \n\
84 Algorithm \n\
85 \n\
86 Library Functions used \n\
87" ;
88
89/*-----------------------------------------------------------------------------
90 Function code
91 -----------------------------------------------------------------------------*/
92
93/*----------------------------------------------------------------------------*/
103/*----------------------------------------------------------------------------*/
104int cpl_plugin_get_info(cpl_pluginlist * list)
105{
106 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
107 cpl_plugin * plugin = &recipe->interface;
108
109 if (cpl_plugin_init(plugin,
110 CPL_PLUGIN_API,
111 CR2RES_BINARY_VERSION,
112 CPL_PLUGIN_TYPE_RECIPE,
113 RECIPE_STRING,
114 "Plotting utility",
115 cr2res_util_plot_description,
116 CR2RES_PIPELINE_AUTHORS,
117 PACKAGE_BUGREPORT,
119 cr2res_util_plot_create,
120 cr2res_util_plot_exec,
121 cr2res_util_plot_destroy)) {
122 cpl_msg_error(cpl_func, "Plugin initialization failed");
123 (void)cpl_error_set_where(cpl_func);
124 return 1;
125 }
126 if (cpl_pluginlist_append(list, plugin)) {
127 cpl_msg_error(cpl_func, "Error adding plugin to list");
128 (void)cpl_error_set_where(cpl_func);
129 return 1;
130 }
131 return 0;
132}
133
134/*----------------------------------------------------------------------------*/
142/*----------------------------------------------------------------------------*/
143static int cr2res_util_plot_create(cpl_plugin * plugin)
144{
145 cpl_recipe * recipe;
146 cpl_parameter * p;
147
148 /* Check that the plugin is part of a valid recipe */
149 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
150 recipe = (cpl_recipe *)plugin;
151 else
152 return -1;
153
154 /* Create the parameters list in the cpl_recipe object */
155 recipe->parameters = cpl_parameterlist_new();
156
157 /* Fill the parameters list */
158 p = cpl_parameter_new_value("cr2res.cr2res_util_plot.xmin",
159 CPL_TYPE_DOUBLE, "Minimum x value to plot",
160 "cr2res.cr2res_util_plot", -1.0);
161 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xmin");
162 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
163 cpl_parameterlist_append(recipe->parameters, p);
164
165 p = cpl_parameter_new_value("cr2res.cr2res_util_plot.xmax",
166 CPL_TYPE_DOUBLE, "Maximum x value to plot",
167 "cr2res.cr2res_util_plot", -1.0);
168 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xmax");
169 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
170 cpl_parameterlist_append(recipe->parameters, p);
171
172 p = cpl_parameter_new_value("cr2res.cr2res_util_plot.detector",
173 CPL_TYPE_INT, "Only reduce the specified detector",
174 "cr2res.cr2res_util_plot", 0);
175 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detector");
176 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
177 cpl_parameterlist_append(recipe->parameters, p);
178
179 p = cpl_parameter_new_value("cr2res.cr2res_util_plot.order",
180 CPL_TYPE_INT, "Only reduce the specified order",
181 "cr2res.cr2res_util_plot", -1);
182 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "order");
183 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
184 cpl_parameterlist_append(recipe->parameters, p);
185
186 p = cpl_parameter_new_value("cr2res.cr2res_util_plot.trace_nb",
187 CPL_TYPE_INT, "Only reduce the specified trace number",
188 "cr2res.cr2res_util_plot", -1);
189 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trace_nb");
190 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
191 cpl_parameterlist_append(recipe->parameters, p);
192
193 p = cpl_parameter_new_value("cr2res.cr2res_util_plot.adjust_level",
194 CPL_TYPE_BOOL, "Flag to adjust the level with 2 plots",
195 "cr2res.cr2res_util_plot", TRUE);
196 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "adjust_level");
197 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
198 cpl_parameterlist_append(recipe->parameters, p);
199
200 return 0;
201}
202
203/*----------------------------------------------------------------------------*/
209/*----------------------------------------------------------------------------*/
210static int cr2res_util_plot_exec(cpl_plugin * plugin)
211{
212 cpl_recipe *recipe;
213
214 /* Get the recipe out of the plugin */
215 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
216 recipe = (cpl_recipe *)plugin;
217 else return -1;
218
219 return cr2res_util_plot(recipe->frames, recipe->parameters);
220}
221
222/*----------------------------------------------------------------------------*/
228/*----------------------------------------------------------------------------*/
229static int cr2res_util_plot_destroy(cpl_plugin * plugin)
230{
231 cpl_recipe *recipe;
232
233 /* Get the recipe out of the plugin */
234 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
235 recipe = (cpl_recipe *)plugin;
236 else return -1 ;
237
238 cpl_parameterlist_delete(recipe->parameters);
239 return 0 ;
240}
241
242/*----------------------------------------------------------------------------*/
249/*----------------------------------------------------------------------------*/
250static int cr2res_util_plot(
251 cpl_frameset * frameset,
252 const cpl_parameterlist * parlist)
253{
254 const cpl_parameter * param;
255 int reduce_det, reduce_trace, reduce_order, adjust ;
256 double xmin, xmax ;
257 const char * fname1 ;
258 const char * fname2 ;
259 const char * drstype ;
260 cpl_propertylist * plist ;
261 cpl_table * tab1 ;
262 cpl_table * sel_tab ;
263 cpl_bivector * spectrum ;
264 cpl_bivector * spectrum_err ;
265
266 /* RETRIEVE INPUT PARAMETERS */
267 param = cpl_parameterlist_find_const(parlist,
268 "cr2res.cr2res_util_plot.xmin");
269 xmin = cpl_parameter_get_double(param);
270 param = cpl_parameterlist_find_const(parlist,
271 "cr2res.cr2res_util_plot.xmax");
272 xmax = cpl_parameter_get_double(param);
273 param = cpl_parameterlist_find_const(parlist,
274 "cr2res.cr2res_util_plot.detector");
275 reduce_det = cpl_parameter_get_int(param);
276 param = cpl_parameterlist_find_const(parlist,
277 "cr2res.cr2res_util_plot.order");
278 reduce_order = cpl_parameter_get_int(param);
279 param = cpl_parameterlist_find_const(parlist,
280 "cr2res.cr2res_util_plot.trace_nb");
281 reduce_trace = cpl_parameter_get_int(param);
282 param = cpl_parameterlist_find_const(parlist,
283 "cr2res.cr2res_util_plot.adjust_level");
284 adjust = cpl_parameter_get_bool(param);
285
286 /* Retrieve raw frames */
287 fname1 = cpl_frame_get_filename(cpl_frameset_get_position(frameset, 0)) ;
288 fname2 = cpl_frame_get_filename(cpl_frameset_get_position(frameset, 1)) ;
289 if (fname2 == NULL) cpl_error_reset() ;
290
291 /* Read the PRO.TYPE of the first frame */
292 plist = cpl_propertylist_load(fname1, 0) ;
293 drstype = cr2res_pfits_get_drstype(plist) ;
294
295 /* CR2RES_DRSTYPE_CATALOG */
296 if (!strcmp(drstype, CR2RES_DRSTYPE_CATALOG)) {
297 const char *title;
298 title = "t 'Emission lines' w lines" ;
299 tab1 = cpl_table_load(fname1, 1, 0) ;
300
301 /* Sub selection of the catalog */
302 if (xmin > 0.0 && xmax >.0) {
303 cpl_table_and_selected_double(tab1, CR2RES_COL_WAVELENGTH,
304 CPL_GREATER_THAN, xmin) ;
305 cpl_table_and_selected_double(tab1, CR2RES_COL_WAVELENGTH,
306 CPL_LESS_THAN, xmax) ;
307 sel_tab = cpl_table_extract_selected(tab1) ;
308 cpl_table_delete(tab1) ;
309 tab1 = sel_tab ;
310 }
311
312 /* Plot */
313 cpl_plot_column(
314 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
315 title, "", tab1, CR2RES_COL_WAVELENGTH, CR2RES_COL_EMISSION) ;
316 cpl_table_delete(tab1) ;
317 }
318
319 /* CR2RES_EXTRACT_1D_DRSTYPE */
320 if (!strcmp(drstype, CR2RES_EXTRACT_1D_DRSTYPE)) {
321
322 /* Only support a single detector */
323 if (reduce_det < 1) {
324 cpl_msg_error(__func__, "Please specify a detector - abort") ;
325 cpl_propertylist_delete(plist) ;
326 return -1 ;
327 }
328
329 /* Load the table */
330 tab1 = cr2res_io_load_EXTRACT_1D(fname1, reduce_det) ;
331 if (fname2 == NULL) {
332 cr2res_util_plot_spec_1d(tab1, NULL, adjust, reduce_order,
333 reduce_trace, xmin, xmax) ;
334 }
335 else {
336 cpl_bivector *ref_spectrum;
337 cpl_bivector *ref_spectrum_extract;
338 double *px;
339 if (reduce_order < 1 || reduce_trace < 1) {
340 cpl_msg_error(__func__, "Please specify a order/trace - abort");
341 cpl_propertylist_delete(plist) ;
342 cpl_table_delete(tab1) ;
343 return -1 ;
344 }
345
346 /* Get the Spectrum */
347 if (cr2res_extract_EXTRACT1D_get_spectrum(tab1, reduce_order,
348 reduce_trace, &spectrum, &spectrum_err)) {
349 cpl_msg_error(__func__, "Cannot get the extracted spectrum") ;
350 cpl_propertylist_delete(plist) ;
351 cpl_table_delete(tab1) ;
352 return -1 ;
353 }
354 cpl_bivector_delete(spectrum_err) ;
355
356 /* Get the catalog */
357 ref_spectrum = cr2res_io_load_EMISSION_LINES(fname2) ;
358
359 /* Extract the catalog in the proper Wave range */
360 px = cpl_bivector_get_x_data(spectrum) ;
361 ref_spectrum_extract = irplib_wlxcorr_cat_extract(ref_spectrum,
362 px[0], px[cpl_bivector_get_size(spectrum)-1]) ;
363 cpl_bivector_delete(ref_spectrum) ;
364
365 /* Plot */
366 cr2res_plot_wavecal_result(spectrum, ref_spectrum_extract, "",
367 xmin, xmax) ;
368
369 /* Free */
370 cpl_bivector_delete(spectrum) ;
371 cpl_bivector_delete(ref_spectrum_extract) ;
372 }
373 cpl_table_delete(tab1) ;
374 }
375
376 /* CR2RES_SLIT_FUNC_DRSTYPE */
377 if (!strcmp(drstype, CR2RES_SLIT_FUNC_DRSTYPE)) {
378
379 /* Only support a single detector */
380 if (reduce_det < 1) {
381 cpl_msg_error(__func__, "Please specify a detector - abort") ;
382 cpl_propertylist_delete(plist) ;
383 return -1 ;
384 }
385
386 /* Load the table */
387 tab1 = cr2res_io_load_SLIT_FUNC(fname1, reduce_det) ;
388 cr2res_util_plot_slit_func(tab1, reduce_order,reduce_trace);
389 cpl_table_delete(tab1) ;
390 }
391
392 /* Delete */
393 cpl_propertylist_delete(plist) ;
394
395 /* Return */
396 if (cpl_error_get_code()) {
397 return -1 ;
398 } else {
399 return 0 ;
400 }
401}
402
403static int cr2res_util_plot_slit_func(
404 cpl_table * tab,
405 int order,
406 int trace)
407{
408 char * col ;
409
410 /* Check entries */
411 if (cpl_table_get_nrow(tab) == 0) return -1 ;
412 if (order < 1 || trace < 1) {
413 cpl_msg_error(__func__, "Please specify the order/trace") ;
414 return -1 ;
415 }
416
417 /* Get column names */
418 col = cr2res_dfs_SLIT_FUNC_colname(order, trace) ;
419
420 /* SPECTRUM */
421 cr2res_util_plot_slit_func_one(tab, col,
422"set grid;set xlabel 'slit pos (pix)';set ylabel 'Intensity (ADU/sec)';",
423 "t 'Slit Function' w lines") ;
424
425 cpl_free(col);
426 return 0 ;
427}
428
429static int cr2res_util_plot_slit_func_one(
430 cpl_table * tab,
431 const char * col,
432 const char * options,
433 const char * title)
434{
435 //int nrows ;
436 /* Check inputs */
437 if (tab == NULL) return -1 ;
438 //nrows = cpl_table_get_nrow(tab) ;
439 cpl_plot_column(options, title, "", tab, NULL, col) ;
440 return 0 ;
441}
442
443static int cr2res_util_plot_spec_1d(
444 cpl_table * tab,
445 cpl_table * tab_opt,
446 int adjust,
447 int order,
448 int trace,
449 double xmin,
450 double xmax)
451{
452 char * wl_col ;
453 char * spec_col ;
454 char * err_col ;
455 cpl_table * tab_loc ;
456 cpl_table * sel_tab ;
457
458 /* Check entries */
459 if (cpl_table_get_nrow(tab) == 0) return -1 ;
460 if (order < 1 || trace < 1) {
461 cpl_msg_error(__func__, "Please specify the order/trace") ;
462 return -1 ;
463 }
464
465 /* Get column names */
466 spec_col = cr2res_dfs_SPEC_colname(order, trace) ;
467 err_col = cr2res_dfs_SPEC_ERR_colname(order, trace) ;
468 wl_col = cr2res_dfs_WAVELENGTH_colname(order, trace) ;
469
470 tab_loc = cpl_table_duplicate(tab) ;
471
472 /* Table Selection */
473 if (xmin > 0.0 && xmax >.0) {
474 cpl_table_and_selected_double(tab_loc, wl_col, CPL_GREATER_THAN, xmin) ;
475 cpl_table_and_selected_double(tab_loc, wl_col, CPL_LESS_THAN, xmax) ;
476 sel_tab = cpl_table_extract_selected(tab_loc) ;
477 cpl_table_delete(tab_loc) ;
478 tab_loc = sel_tab ;
479 } else {
480 tab_loc = cpl_table_duplicate(tab) ;
481 }
482
483 /* SPECTRUM */
484 cr2res_util_plot_spec_1d_one(tab_loc, tab_opt, wl_col, adjust, spec_col,
485"set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity (ADU/sec)';",
486 "t 'Extracted Spectrum' w lines") ;
487
488 /* ERROR */
489 cr2res_util_plot_spec_1d_one(tab_loc, tab_opt, wl_col, adjust, err_col,
490"set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity (ADU/sec)';",
491 "t 'Error Spectrum' w lines") ;
492
493 cpl_table_delete(tab_loc) ;
494 cpl_free(spec_col);
495 cpl_free(err_col);
496 cpl_free(wl_col);
497 return 0 ;
498}
499
500static int cr2res_util_plot_spec_1d_one(
501 cpl_table * tab,
502 cpl_table * tab_opt,
503 const char * wave_col,
504 int adjust_level,
505 const char * y_col,
506 const char * options,
507 const char * title)
508{
509 int nrows ;
510
511 /* Check inputs */
512 if (tab == NULL) return -1 ;
513 nrows = cpl_table_get_nrow(tab) ;
514 if (tab_opt != NULL) {
515 if (cpl_table_get_nrow(tab_opt) != nrows) {
516 cpl_msg_error(__func__,
517 "The two tables must have the same number of rows") ;
518 return -1 ;
519 }
520 }
521 if (tab_opt != NULL) {
522 cpl_vector **vectors;
523 vectors = cpl_malloc(3*sizeof(cpl_vector*)) ;
524 vectors[0]=cpl_vector_wrap(nrows,cpl_table_get_data_double(tab,
525 wave_col));
526 vectors[1] = cpl_vector_wrap(nrows,
527 cpl_table_get_data_double(tab, y_col)) ;
528 vectors[2] = cpl_vector_wrap(nrows,
529 cpl_table_get_data_double(tab_opt, y_col));
530 if (adjust_level) {
531 double mean1, mean2;
532 mean1 = cpl_vector_get_mean(vectors[1]) ;
533 mean2 = cpl_vector_get_mean(vectors[2]) ;
534 cpl_vector_multiply_scalar(vectors[2], fabs(mean1/mean2)) ;
535 }
536 cpl_plot_vectors(options, title, "", (const cpl_vector **)vectors, 3);
537 cpl_vector_unwrap(vectors[0]) ;
538 cpl_vector_unwrap(vectors[1]) ;
539 cpl_vector_unwrap(vectors[2]) ;
540 cpl_free(vectors) ;
541 }
542 else {
543 cpl_plot_column(options, title, "", tab, wave_col, y_col) ;
544 }
545 return 0 ;
546}
547
char * cr2res_dfs_SLIT_FUNC_colname(int order_idx, int trace)
Get the SLIT_FUNC table column name for a given order/trace.
Definition: cr2res_dfs.c:432
char * cr2res_dfs_SPEC_ERR_colname(int order_idx, int trace)
Get the ERR column name for a given order/trace.
Definition: cr2res_dfs.c:414
char * cr2res_dfs_WAVELENGTH_colname(int order_idx, int trace)
Get the WAVELENGTH column name for a given order/trace.
Definition: cr2res_dfs.c:396
char * cr2res_dfs_SPEC_colname(int order_idx, int trace)
Get the SPEC column name for a given order/trace.
Definition: cr2res_dfs.c:378
int cr2res_extract_EXTRACT1D_get_spectrum(const cpl_table *tab, int order, int trace_nb, cpl_bivector **spec, cpl_bivector **spec_err)
Get a Spectrum and its error from the EXTRACT_1D table.
cpl_table * cr2res_io_load_EXTRACT_1D(const char *filename, int detector)
Load a table from a EXTRACT_1D.
Definition: cr2res_io.c:1296
cpl_table * cr2res_io_load_SLIT_FUNC(const char *filename, int detector)
Load a table from a SLIT_FUNC.
Definition: cr2res_io.c:1139
cpl_bivector * cr2res_io_load_EMISSION_LINES(const char *filename)
Load an EMISSION_LINES bivector.
Definition: cr2res_io.c:903
const char * cr2res_pfits_get_drstype(const cpl_propertylist *plist)
find out the DRS.TYPE
Definition: cr2res_pfits.c:86
int cr2res_plot_wavecal_result(const cpl_bivector *extracted_spec, const cpl_bivector *catalog, const char *title, double wmin, double wmax)
Plot the spectrum with the catalog.
const char * cr2res_get_license(void)
Get the pipeline copyright and license.