33#include "irplib_wlxcorr.h"
35#include "cr2res_utils.h"
36#include "cr2res_extract.h"
37#include "cr2res_pfits.h"
38#include "cr2res_dfs.h"
45#define RECIPE_STRING "cr2res_util_plot"
51int cpl_plugin_get_info(cpl_pluginlist * list);
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,
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 *);
72static char cr2res_util_plot_description[] =
"\
74 This utility detects the type of the first passed file, and plots \n\
75 relevant graphs accordingly. \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\
86 Library Functions used \n\
104int cpl_plugin_get_info(cpl_pluginlist * list)
106 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
107 cpl_plugin * plugin = &recipe->interface;
109 if (cpl_plugin_init(plugin,
111 CR2RES_BINARY_VERSION,
112 CPL_PLUGIN_TYPE_RECIPE,
115 cr2res_util_plot_description,
116 CR2RES_PIPELINE_AUTHORS,
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);
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);
143static int cr2res_util_plot_create(cpl_plugin * plugin)
149 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
150 recipe = (cpl_recipe *)plugin;
155 recipe->parameters = cpl_parameterlist_new();
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);
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);
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);
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);
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);
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);
210static int cr2res_util_plot_exec(cpl_plugin * plugin)
215 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
216 recipe = (cpl_recipe *)plugin;
219 return cr2res_util_plot(recipe->frames, recipe->parameters);
229static int cr2res_util_plot_destroy(cpl_plugin * plugin)
234 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
235 recipe = (cpl_recipe *)plugin;
238 cpl_parameterlist_delete(recipe->parameters);
250static int cr2res_util_plot(
251 cpl_frameset * frameset,
252 const cpl_parameterlist * parlist)
254 const cpl_parameter * param;
255 int reduce_det, reduce_trace, reduce_order, adjust ;
257 const char * fname1 ;
258 const char * fname2 ;
259 const char * drstype ;
260 cpl_propertylist * plist ;
262 cpl_table * sel_tab ;
263 cpl_bivector * spectrum ;
264 cpl_bivector * spectrum_err ;
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);
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() ;
292 plist = cpl_propertylist_load(fname1, 0) ;
296 if (!strcmp(drstype, CR2RES_DRSTYPE_CATALOG)) {
298 title =
"t 'Emission lines' w lines" ;
299 tab1 = cpl_table_load(fname1, 1, 0) ;
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) ;
314 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
315 title,
"", tab1, CR2RES_COL_WAVELENGTH, CR2RES_COL_EMISSION) ;
316 cpl_table_delete(tab1) ;
320 if (!strcmp(drstype, CR2RES_EXTRACT_1D_DRSTYPE)) {
323 if (reduce_det < 1) {
324 cpl_msg_error(__func__,
"Please specify a detector - abort") ;
325 cpl_propertylist_delete(plist) ;
331 if (fname2 == NULL) {
332 cr2res_util_plot_spec_1d(tab1, NULL, adjust, reduce_order,
333 reduce_trace, xmin, xmax) ;
336 cpl_bivector *ref_spectrum;
337 cpl_bivector *ref_spectrum_extract;
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) ;
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) ;
354 cpl_bivector_delete(spectrum_err) ;
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) ;
370 cpl_bivector_delete(spectrum) ;
371 cpl_bivector_delete(ref_spectrum_extract) ;
373 cpl_table_delete(tab1) ;
377 if (!strcmp(drstype, CR2RES_SLIT_FUNC_DRSTYPE)) {
380 if (reduce_det < 1) {
381 cpl_msg_error(__func__,
"Please specify a detector - abort") ;
382 cpl_propertylist_delete(plist) ;
388 cr2res_util_plot_slit_func(tab1, reduce_order,reduce_trace);
389 cpl_table_delete(tab1) ;
393 cpl_propertylist_delete(plist) ;
396 if (cpl_error_get_code()) {
403static int cr2res_util_plot_slit_func(
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") ;
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") ;
429static int cr2res_util_plot_slit_func_one(
432 const char * options,
437 if (tab == NULL)
return -1 ;
439 cpl_plot_column(options, title,
"", tab, NULL, col) ;
443static int cr2res_util_plot_spec_1d(
455 cpl_table * tab_loc ;
456 cpl_table * sel_tab ;
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") ;
470 tab_loc = cpl_table_duplicate(tab) ;
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) ;
480 tab_loc = cpl_table_duplicate(tab) ;
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") ;
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") ;
493 cpl_table_delete(tab_loc) ;
500static int cr2res_util_plot_spec_1d_one(
503 const char * wave_col,
506 const char * options,
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") ;
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,
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));
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)) ;
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]) ;
543 cpl_plot_column(options, title,
"", tab, wave_col, y_col) ;
char * cr2res_dfs_SLIT_FUNC_colname(int order_idx, int trace)
Get the SLIT_FUNC table column name for a given order/trace.
char * cr2res_dfs_SPEC_ERR_colname(int order_idx, int trace)
Get the ERR column name for a given order/trace.
char * cr2res_dfs_WAVELENGTH_colname(int order_idx, int trace)
Get the WAVELENGTH column name for a given order/trace.
char * cr2res_dfs_SPEC_colname(int order_idx, int trace)
Get the SPEC column name for a given order/trace.
cpl_table * cr2res_io_load_EXTRACT_1D(const char *filename, int detector)
Load a table from a EXTRACT_1D.
cpl_table * cr2res_io_load_SLIT_FUNC(const char *filename, int detector)
Load a table from a SLIT_FUNC.
cpl_bivector * cr2res_io_load_EMISSION_LINES(const char *filename)
Load an EMISSION_LINES bivector.
const char * cr2res_pfits_get_drstype(const cpl_propertylist *plist)
find out the DRS.TYPE
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.