130 #if defined HAVE_SETENV && HAVE_SETENV
131 #if defined HAVE_DECL_SETENV && !HAVE_DECL_SETENV
132 int setenv(
const char *name,
const char *value,
int overwrite);
140 #include <uves_plot.h>
142 #include <uves_dump.h>
143 #include <uves_utils_wrappers.h>
144 #include <uves_error.h>
145 #include <uves_msg.h>
147 #include <irplib_utils.h>
159 static char *title_string(
const char *title,
int npoints);
163 #define MAXTITLELENGTH 10000
164 #define RECOVER_FROM_ERROR(EXTERNAL_COMMAND) do { \
165 if (cpl_error_get_code() != CPL_ERROR_NONE) \
167 uves_msg_error("Could not send plot to " \
171 cpl_error_get_message(), \
172 cpl_error_get_where()); \
178 static char title[MAXTITLELENGTH];
179 static bool plotting_enabled =
false;
182 static const char *plotter =
"";
204 uves_plot_initialize(
const char *plotter_command)
206 char *test_cmd = NULL;
207 char *first_word = NULL;
209 plotting_enabled = (strcmp(plotter_command,
"no") != 0);
215 if (plotting_enabled)
217 const char *env =
"CPL_PLOTTER";
225 first_word = uves_sprintf(
"%s ", plotter_command);
227 assure( strtok(first_word,
" ") != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
228 "Error splitting string '%s'", first_word);
230 test_cmd = uves_sprintf(
"which %s > /dev/null", first_word);
232 #if defined HAVE_SETENV && HAVE_SETENV
234 if (setenv(env, plotter_command, 1) != 0)
237 "Plotting disabled!", env);
238 plotting_enabled =
false;
246 else if (system(test_cmd) != 0)
250 plotting_enabled =
false;
258 plotter = plotter_command;
261 uves_msg_warning(
"setenv() is not available on this platform. You have to manually "
262 "set the environment variable '%s' to '%s'", env, plotter_command);
264 plotter = plotter_command;
271 cpl_free(first_word);
273 return cpl_error_get_code();
293 uves_plot_image_rows(
const cpl_image *image,
int first_row,
int last_row,
int step,
294 const char *xtitle,
const char *ytitle,
const char *format, ...)
299 char *options = NULL;
300 const char *post =
"";
301 cpl_image *thresholded = NULL;
303 assure( image != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
304 if (xtitle == NULL) xtitle =
"";
305 if (ytitle == NULL) ytitle =
"";
306 assure( 1 <= first_row && first_row <= last_row &&
307 last_row <= cpl_image_get_size_y(image),
308 CPL_ERROR_ILLEGAL_INPUT,
309 "Illegal rows: %d - %d; rows in image = %" CPL_SIZE_FORMAT
"",
310 first_row, last_row, cpl_image_get_size_y(image));
312 assure( step >= 1, CPL_ERROR_ILLEGAL_INPUT,
313 "Illegal step size: %d", step);
315 if (plotting_enabled)
317 const char *pre_format;
321 pre_format =
"set grid; set xlabel '%s'; set ylabel '%s';";
322 pre = cpl_calloc(strlen(pre_format) +
323 strlen(xtitle) + strlen(ytitle) + 1,
325 sprintf(pre, pre_format, xtitle, ytitle);
328 va_start(al, format);
329 vsnprintf(title, MAXTITLELENGTH - 1, format, al);
331 title[MAXTITLELENGTH - 1] =
'\0';
333 options = title_string(title, cpl_image_get_size_x(image));
336 thresholded = cpl_image_duplicate(image);
337 for (row = first_row; row <= last_row; row++)
339 int nx = cpl_image_get_size_x(thresholded);
340 double median = cpl_image_get_median_window(thresholded,
343 double stdev = cpl_image_get_stdev_window(thresholded,
347 double locut = median - 3*stdev;
348 double hicut = median + 3*stdev;
352 for (x = 1; x <= nx; x++)
355 cpl_image_get(thresholded, x, row, &pis_rejected);
356 if (data < locut) data = locut;
357 if (data > hicut) data = hicut;
358 cpl_image_set(thresholded, x, row, data);
362 cpl_plot_image_row(pre,
363 (strcmp(options,
"t '%s'") == 0) ?
"" : options,
366 first_row, last_row, step);
368 RECOVER_FROM_ERROR(plotter);
372 uves_free_image(&thresholded);
376 return cpl_error_get_code();
399 uves_plot_image_columns(
const cpl_image *image,
int first_column,
int last_column,
int step,
400 const char *xtitle,
const char *ytitle,
const char *format, ...)
405 char *options = NULL;
406 const char *post =
"";
407 cpl_image *thresholded = NULL;
409 assure( image != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
410 if (xtitle == NULL) xtitle =
"";
411 if (ytitle == NULL) ytitle =
"";
412 assure( 1 <= first_column && first_column <= last_column &&
413 last_column <= cpl_image_get_size_x(image),
414 CPL_ERROR_ILLEGAL_INPUT,
415 "Illegal columns: %d - %d; columns in image = %" CPL_SIZE_FORMAT
"",
416 first_column, last_column, cpl_image_get_size_x(image));
418 assure( step >= 1, CPL_ERROR_ILLEGAL_INPUT,
419 "Illegal step size: %d", step);
421 if (plotting_enabled)
423 const char *pre_format;
427 pre_format =
"set grid; set xlabel '%s'; set ylabel '%s';";
428 pre = cpl_calloc(strlen(pre_format) +
429 strlen(xtitle) + strlen(ytitle) + 1,
431 sprintf(pre, pre_format, xtitle, ytitle);
433 va_start(al, format);
434 vsnprintf(title, MAXTITLELENGTH - 1, format, al);
436 title[MAXTITLELENGTH - 1] =
'\0';
438 options = title_string(title, cpl_image_get_size_y(image));
441 thresholded = cpl_image_duplicate(image);
442 for (col = first_column; col <= last_column; col++)
444 int ny = cpl_image_get_size_x(thresholded);
445 double median = cpl_image_get_median_window(thresholded,
448 double stdev = cpl_image_get_stdev_window(thresholded,
452 double locut = median - 3*stdev;
453 double hicut = median + 3*stdev;
457 for (y = 1; y <= ny; y++)
459 double data = cpl_image_get(thresholded, col, y, &pis_rejected);
460 if (data < locut) data = locut;
461 if (data > hicut) data = hicut;
462 cpl_image_set(thresholded, col, y, data);
467 check( cpl_plot_image_col(pre,
468 (strcmp(options,
"t '%s'") == 0) ?
"" : options,
471 first_column, last_column, step),
472 "Error plotting image");
474 RECOVER_FROM_ERROR(plotter);
478 uves_free_image(&thresholded);
482 return cpl_error_get_code();
501 uves_plot_bivectors(cpl_bivector **bivectors,
char **titles,
502 int N,
const char *xtitle,
506 char **options = NULL;
507 const char *post =
"";
509 options = cpl_calloc(N,
sizeof(
char *));
512 if (plotting_enabled)
521 for (i = 0; i < N; i++)
523 npoints += cpl_bivector_get_size(bivectors[i]);
525 for (i = 0; i < N; i++)
527 options[i] = title_string(titles[i], npoints);
532 double datamax = cpl_vector_get_max(cpl_bivector_get_y(bivectors[0]));
533 double datamin = cpl_vector_get_min(cpl_bivector_get_y(bivectors[0]));
535 double locut = datamin - 0.2*(datamax-datamin);
536 double hicut = datamax + 0.2*(datamax-datamin);
538 for (i = 0; i < N; i++)
541 for (j = 0; j < cpl_bivector_get_size(bivectors[i]); j++)
543 if (cpl_bivector_get_y_data(bivectors[i])[j] < locut)
545 cpl_bivector_get_y_data(bivectors[i])[j] = locut;
547 if (cpl_bivector_get_y_data(bivectors[i])[j] > hicut)
549 cpl_bivector_get_y_data(bivectors[i])[j] = hicut;
557 bivectors[0] = bivectors[N-1];
558 bivectors[N-1] = temp;
561 options[0] = options[N-1];
562 options[N-1] = temps;
565 "set grid; set xlabel '%s'; set ylabel '%s';", xtitle, ytitle);
567 cpl_plot_bivectors(pre,
568 (
const char **)options,
570 (
const cpl_bivector **)bivectors, N);
572 RECOVER_FROM_ERROR(plotter);
579 for (i = 0; i < N; i++)
581 cpl_free(options[i]);
606 uves_plot_table(
const cpl_table *table,
const char *colx,
const char *coly,
607 const char *format, ...)
612 char *options = NULL;
613 const char *post =
"";
614 cpl_table *thresholded = NULL;
616 assure( table != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
617 assure( colx != NULL, CPL_ERROR_NULL_INPUT,
"Null x column");
618 assure( coly != NULL, CPL_ERROR_NULL_INPUT,
"Null y column");
619 assure( cpl_table_has_column(table, colx), CPL_ERROR_ILLEGAL_INPUT,
620 "No such column: '%s'", colx);
621 assure( cpl_table_has_column(table, coly), CPL_ERROR_ILLEGAL_INPUT,
622 "No such column: '%s'", coly);
624 assure( cpl_table_get_column_type(table, colx) == CPL_TYPE_INT ||
625 cpl_table_get_column_type(table, colx) == CPL_TYPE_FLOAT ||
626 cpl_table_get_column_type(table, colx) == CPL_TYPE_DOUBLE,
627 CPL_ERROR_TYPE_MISMATCH,
628 "Column '%s' has type '%s'. Numerical type expected",
632 assure( cpl_table_get_column_type(table, coly) == CPL_TYPE_INT ||
633 cpl_table_get_column_type(table, coly) == CPL_TYPE_FLOAT ||
634 cpl_table_get_column_type(table, coly) == CPL_TYPE_DOUBLE,
635 CPL_ERROR_TYPE_MISMATCH,
636 "Column '%s' has type '%s'. Numerical type expected",
640 if (plotting_enabled)
642 const char *pre_format;
645 va_start(al, format);
646 vsnprintf(title, MAXTITLELENGTH - 1, format, al);
648 title[MAXTITLELENGTH - 1] =
'\0';
650 options = title_string(title, cpl_table_get_nrow(table));
653 pre_format =
"set grid; set xlabel '%s'; set ylabel '%s';";
654 pre = cpl_calloc(strlen(pre_format) + strlen(colx) + strlen(coly) + 1,
657 sprintf(pre, pre_format, colx, coly);
662 double median, sigma, locut, hicut;
665 median = cpl_table_get_column_median(table, coly);
666 sigma = cpl_table_get_column_stdev(table, coly);
668 locut = median - 3*sigma;
669 hicut = median + 3*sigma;
672 thresholded = cpl_table_new(cpl_table_get_nrow(table));
673 cpl_table_duplicate_column(thresholded, coly, table, coly);
674 cpl_table_duplicate_column(thresholded, colx, table, colx);
676 for (i = 0; i < cpl_table_get_nrow(thresholded); i++)
678 double data = cpl_table_get(thresholded, coly, i, NULL);
680 if (data < locut && data > hicut)
682 cpl_table_set_invalid(thresholded, coly, i);
689 (strcmp(options,
"t '%s'") == 0) ?
"" : options,
691 thresholded, colx, coly);
693 RECOVER_FROM_ERROR(plotter);
697 uves_free_table(&thresholded);
701 return cpl_error_get_code();
717 title_string(
const char *plot_title,
int npoints)
722 const char *options = (npoints > 100) ?
723 "w points pointsize 1" :
724 "w linespoints pointsize 1";
727 size_t length = strlen(
"t '' ") + strlen(plot_title) + strlen(options) + 1;
728 char *result = cpl_calloc(length,
sizeof(
char));
730 snprintf(result, length,
"t '%s' %s", plot_title, options);