56 #if defined HAVE_SETENV && HAVE_SETENV
57 #if defined HAVE_DECL_SETENV && !HAVE_DECL_SETENV
58 int setenv(
const char *name,
const char *value,
int overwrite);
66 #include <uves_plot.h>
68 #include <uves_dump.h>
69 #include <uves_utils_wrappers.h>
70 #include <uves_error.h>
73 #include <irplib_utils.h>
85 static char *title_string(
const char *title,
int npoints);
89 #define MAXTITLELENGTH 10000
90 #define RECOVER_FROM_ERROR(EXTERNAL_COMMAND) do { \
91 if (cpl_error_get_code() != CPL_ERROR_NONE) \
93 uves_msg_error("Could not send plot to " \
97 cpl_error_get_message(), \
98 cpl_error_get_where()); \
104 static char title[MAXTITLELENGTH];
105 static bool plotting_enabled =
false;
108 static const char *plotter =
"";
130 uves_plot_initialize(
const char *plotter_command)
132 char *test_cmd = NULL;
133 char *first_word = NULL;
135 plotting_enabled = (strcmp(plotter_command,
"no") != 0);
141 if (plotting_enabled)
143 const char *env =
"CPL_PLOTTER";
151 first_word = uves_sprintf(
"%s ", plotter_command);
153 assure( strtok(first_word,
" ") != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
154 "Error splitting string '%s'", first_word);
156 test_cmd = uves_sprintf(
"which %s > /dev/null", first_word);
158 #if defined HAVE_SETENV && HAVE_SETENV
160 if (setenv(env, plotter_command, 1) != 0)
163 "Plotting disabled!", env);
164 plotting_enabled =
false;
172 else if (system(test_cmd) != 0)
176 plotting_enabled =
false;
184 plotter = plotter_command;
187 uves_msg_warning(
"setenv() is not available on this platform. You have to manually "
188 "set the environment variable '%s' to '%s'", env, plotter_command);
190 plotter = plotter_command;
197 cpl_free(first_word);
199 return cpl_error_get_code();
219 uves_plot_image_rows(
const cpl_image *image,
int first_row,
int last_row,
int step,
220 const char *xtitle,
const char *ytitle,
const char *format, ...)
225 char *options = NULL;
226 const char *post =
"";
227 cpl_image *thresholded = NULL;
229 assure( image != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
230 if (xtitle == NULL) xtitle =
"";
231 if (ytitle == NULL) ytitle =
"";
232 assure( 1 <= first_row && first_row <= last_row &&
233 last_row <= cpl_image_get_size_y(image),
234 CPL_ERROR_ILLEGAL_INPUT,
235 "Illegal rows: %d - %d; rows in image = %" CPL_SIZE_FORMAT
"",
236 first_row, last_row, cpl_image_get_size_y(image));
238 assure( step >= 1, CPL_ERROR_ILLEGAL_INPUT,
239 "Illegal step size: %d", step);
241 if (plotting_enabled)
243 const char *pre_format;
247 pre_format =
"set grid; set xlabel '%s'; set ylabel '%s';";
248 pre = cpl_calloc(strlen(pre_format) +
249 strlen(xtitle) + strlen(ytitle) + 1,
251 sprintf(pre, pre_format, xtitle, ytitle);
254 va_start(al, format);
255 vsnprintf(title, MAXTITLELENGTH - 1, format, al);
257 title[MAXTITLELENGTH - 1] =
'\0';
259 options = title_string(title, cpl_image_get_size_x(image));
262 thresholded = cpl_image_duplicate(image);
263 for (row = first_row; row <= last_row; row++)
265 int nx = cpl_image_get_size_x(thresholded);
266 double median = cpl_image_get_median_window(thresholded,
269 double stdev = cpl_image_get_stdev_window(thresholded,
273 double locut = median - 3*stdev;
274 double hicut = median + 3*stdev;
278 for (x = 1; x <= nx; x++)
281 cpl_image_get(thresholded, x, row, &pis_rejected);
282 if (data < locut) data = locut;
283 if (data > hicut) data = hicut;
284 cpl_image_set(thresholded, x, row, data);
288 cpl_plot_image_row(pre,
289 (strcmp(options,
"t '%s'") == 0) ?
"" : options,
292 first_row, last_row, step);
294 RECOVER_FROM_ERROR(plotter);
298 uves_free_image(&thresholded);
302 return cpl_error_get_code();
325 uves_plot_image_columns(
const cpl_image *image,
int first_column,
int last_column,
int step,
326 const char *xtitle,
const char *ytitle,
const char *format, ...)
331 char *options = NULL;
332 const char *post =
"";
333 cpl_image *thresholded = NULL;
335 assure( image != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
336 if (xtitle == NULL) xtitle =
"";
337 if (ytitle == NULL) ytitle =
"";
338 assure( 1 <= first_column && first_column <= last_column &&
339 last_column <= cpl_image_get_size_x(image),
340 CPL_ERROR_ILLEGAL_INPUT,
341 "Illegal columns: %d - %d; columns in image = %" CPL_SIZE_FORMAT
"",
342 first_column, last_column, cpl_image_get_size_x(image));
344 assure( step >= 1, CPL_ERROR_ILLEGAL_INPUT,
345 "Illegal step size: %d", step);
347 if (plotting_enabled)
349 const char *pre_format;
353 pre_format =
"set grid; set xlabel '%s'; set ylabel '%s';";
354 pre = cpl_calloc(strlen(pre_format) +
355 strlen(xtitle) + strlen(ytitle) + 1,
357 sprintf(pre, pre_format, xtitle, ytitle);
359 va_start(al, format);
360 vsnprintf(title, MAXTITLELENGTH - 1, format, al);
362 title[MAXTITLELENGTH - 1] =
'\0';
364 options = title_string(title, cpl_image_get_size_y(image));
367 thresholded = cpl_image_duplicate(image);
368 for (col = first_column; col <= last_column; col++)
370 int ny = cpl_image_get_size_x(thresholded);
371 double median = cpl_image_get_median_window(thresholded,
374 double stdev = cpl_image_get_stdev_window(thresholded,
378 double locut = median - 3*stdev;
379 double hicut = median + 3*stdev;
383 for (y = 1; y <= ny; y++)
385 double data = cpl_image_get(thresholded, col, y, &pis_rejected);
386 if (data < locut) data = locut;
387 if (data > hicut) data = hicut;
388 cpl_image_set(thresholded, col, y, data);
393 check( cpl_plot_image_col(pre,
394 (strcmp(options,
"t '%s'") == 0) ?
"" : options,
397 first_column, last_column, step),
398 "Error plotting image");
400 RECOVER_FROM_ERROR(plotter);
404 uves_free_image(&thresholded);
408 return cpl_error_get_code();
427 uves_plot_bivectors(cpl_bivector **bivectors,
char **titles,
428 int N,
const char *xtitle,
432 char **options = NULL;
433 const char *post =
"";
435 options = cpl_calloc(N,
sizeof(
char *));
438 if (plotting_enabled)
447 for (i = 0; i < N; i++)
449 npoints += cpl_bivector_get_size(bivectors[i]);
451 for (i = 0; i < N; i++)
453 options[i] = title_string(titles[i], npoints);
458 double datamax = cpl_vector_get_max(cpl_bivector_get_y(bivectors[0]));
459 double datamin = cpl_vector_get_min(cpl_bivector_get_y(bivectors[0]));
461 double locut = datamin - 0.2*(datamax-datamin);
462 double hicut = datamax + 0.2*(datamax-datamin);
464 for (i = 0; i < N; i++)
467 for (j = 0; j < cpl_bivector_get_size(bivectors[i]); j++)
469 if (cpl_bivector_get_y_data(bivectors[i])[j] < locut)
471 cpl_bivector_get_y_data(bivectors[i])[j] = locut;
473 if (cpl_bivector_get_y_data(bivectors[i])[j] > hicut)
475 cpl_bivector_get_y_data(bivectors[i])[j] = hicut;
483 bivectors[0] = bivectors[N-1];
484 bivectors[N-1] = temp;
487 options[0] = options[N-1];
488 options[N-1] = temps;
491 "set grid; set xlabel '%s'; set ylabel '%s';", xtitle, ytitle);
493 cpl_plot_bivectors(pre,
494 (
const char **)options,
496 (
const cpl_bivector **)bivectors, N);
498 RECOVER_FROM_ERROR(plotter);
505 for (i = 0; i < N; i++)
507 cpl_free(options[i]);
532 uves_plot_table(
const cpl_table *table,
const char *colx,
const char *coly,
533 const char *format, ...)
538 char *options = NULL;
539 const char *post =
"";
540 cpl_table *thresholded = NULL;
542 assure( table != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
543 assure( colx != NULL, CPL_ERROR_NULL_INPUT,
"Null x column");
544 assure( coly != NULL, CPL_ERROR_NULL_INPUT,
"Null y column");
545 assure( cpl_table_has_column(table, colx), CPL_ERROR_ILLEGAL_INPUT,
546 "No such column: '%s'", colx);
547 assure( cpl_table_has_column(table, coly), CPL_ERROR_ILLEGAL_INPUT,
548 "No such column: '%s'", coly);
550 assure( cpl_table_get_column_type(table, colx) == CPL_TYPE_INT ||
551 cpl_table_get_column_type(table, colx) == CPL_TYPE_FLOAT ||
552 cpl_table_get_column_type(table, colx) == CPL_TYPE_DOUBLE,
553 CPL_ERROR_TYPE_MISMATCH,
554 "Column '%s' has type '%s'. Numerical type expected",
558 assure( cpl_table_get_column_type(table, coly) == CPL_TYPE_INT ||
559 cpl_table_get_column_type(table, coly) == CPL_TYPE_FLOAT ||
560 cpl_table_get_column_type(table, coly) == CPL_TYPE_DOUBLE,
561 CPL_ERROR_TYPE_MISMATCH,
562 "Column '%s' has type '%s'. Numerical type expected",
566 if (plotting_enabled)
568 const char *pre_format;
571 va_start(al, format);
572 vsnprintf(title, MAXTITLELENGTH - 1, format, al);
574 title[MAXTITLELENGTH - 1] =
'\0';
576 options = title_string(title, cpl_table_get_nrow(table));
579 pre_format =
"set grid; set xlabel '%s'; set ylabel '%s';";
580 pre = cpl_calloc(strlen(pre_format) + strlen(colx) + strlen(coly) + 1,
583 sprintf(pre, pre_format, colx, coly);
588 double median, sigma, locut, hicut;
591 median = cpl_table_get_column_median(table, coly);
592 sigma = cpl_table_get_column_stdev(table, coly);
594 locut = median - 3*sigma;
595 hicut = median + 3*sigma;
598 thresholded = cpl_table_new(cpl_table_get_nrow(table));
599 cpl_table_duplicate_column(thresholded, coly, table, coly);
600 cpl_table_duplicate_column(thresholded, colx, table, colx);
602 for (i = 0; i < cpl_table_get_nrow(thresholded); i++)
604 double data = cpl_table_get(thresholded, coly, i, NULL);
606 if (data < locut && data > hicut)
608 cpl_table_set_invalid(thresholded, coly, i);
615 (strcmp(options,
"t '%s'") == 0) ?
"" : options,
617 thresholded, colx, coly);
619 RECOVER_FROM_ERROR(plotter);
623 uves_free_table(&thresholded);
627 return cpl_error_get_code();
643 title_string(
const char *plot_title,
int npoints)
648 const char *options = (npoints > 100) ?
649 "w points pointsize 1" :
650 "w linespoints pointsize 1";
653 size_t length = strlen(
"t '' ") + strlen(plot_title) + strlen(options) + 1;
654 char *result = cpl_calloc(length,
sizeof(
char));
656 snprintf(result, length,
"t '%s' %s", plot_title, options);
#define uves_msg_warning(...)
Print an warning message.
const char * uves_tostring_cpl_type(cpl_type t)
Convert a CPL type to a string.
#define uves_msg_debug(...)
Print a debug message.