File: uves_plot.c

package info (click to toggle)
cpl-plugin-uves 6.1.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,128 kB
  • sloc: ansic: 171,056; sh: 4,359; python: 3,002; makefile: 1,322
file content (662 lines) | stat: -rw-r--r-- 23,456 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*                                                                              *
 *   This file is part of the ESO UVES Pipeline                                 *
 *   Copyright (C) 2004,2005 European Southern Observatory                      *
 *                                                                              *
 *   This library is free software; you can redistribute it and/or modify       *
 *   it under the terms of the GNU General Public License as published by       *
 *   the Free Software Foundation; either version 2 of the License, or          *
 *   (at your option) any later version.                                        *
 *                                                                              *
 *   This program is distributed in the hope that it will be useful,            *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 *   GNU General Public License for more details.                               *
 *                                                                              *
 *   You should have received a copy of the GNU General Public License          *
 *   along with this program; if not, write to the Free Software                *
 *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
 *                                                                              */

/*
 * $Author: amodigli $
 * $Date: 2012-03-02 16:49:48 $
 * $Revision: 1.38 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*----------------------------------------------------------------------------*/
/**
 * @defgroup uves_plot  Plot CPL tables/images
 *
 * Plot CPL tables and/or images using an external plotter specified by the
 * user. This is useful for debugging, and might also
 * be useful for the user. This module provides only
 * basic functionality for plotting CPL objects; for more advanced 
 * purposes, a dedicated FITS viewing tool is recommended.
 *
 * This module depends on the availability of the system call setenv.
 * This is *not* defined in ANSI-C (getenv() is; setenv() is not) and
 * therefore might fail for various reasons on different platforms. Note that
 * the plotting/imaging functionality can be easily switched off by setting
 * the plotting/imaging commands to 'no'.
 *
 * If a plotting call fails for
 * 'external' reasons (i.e. when communication with the external process), a warning
 * message is printed, but success status is returned. A failure exit status is
 * returned only in cases of invalid input (null pointers etc.). 
 *
 */
/*----------------------------------------------------------------------------*/

/* If we can link to setenv but it is not declared, then declare it manually */
#if defined HAVE_SETENV && HAVE_SETENV
#if defined HAVE_DECL_SETENV && !HAVE_DECL_SETENV
int setenv(const char *name, const char *value, int overwrite);
#endif
#endif

/*-----------------------------------------------------------------------------
                                Includes
 -----------------------------------------------------------------------------*/

#include <uves_plot.h>

#include <uves_dump.h>
#include <uves_utils_wrappers.h>
#include <uves_error.h>
#include <uves_msg.h>

#include <irplib_utils.h>

#include <cpl.h>

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>   /* setenv */

/*-----------------------------------------------------------------------------
                            Functions prototypes
 -----------------------------------------------------------------------------*/
static char *title_string(const char *title, int npoints);
/*-----------------------------------------------------------------------------
                            Defines
 -----------------------------------------------------------------------------*/
#define MAXTITLELENGTH 10000
#define RECOVER_FROM_ERROR(EXTERNAL_COMMAND) do {  \
    if (cpl_error_get_code() != CPL_ERROR_NONE)    \
    {                                              \
       uves_msg_error("Could not send plot to "    \
              "command '%s': "             \
               "%s in '%s'",               \
                       EXTERNAL_COMMAND,           \
               cpl_error_get_message(),    \
               cpl_error_get_where());     \
       cpl_error_reset();                          \
       goto cleanup;                               \
    } } while (false)


static char title[MAXTITLELENGTH];
static bool plotting_enabled = false;           /* If caller forgets to call 
                           the initializer, plotting
                           will be disabled */
static const char *plotter = "";

/**@{*/

/*-----------------------------------------------------------------------------
                        Implementation
 -----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/**
   @brief    Set plotting/imaging commands
   @param    plotter_command       The plotter command
   @return   CPL_ERROR_NONE iff okay

   The specified commands are written to the environment variables
   CPL_PLOTTER and IRPLIB_IMAGER, which are used by the irplib.

   Some additional checks are made to see if the specified commands are
   available on the current platform. See code for details.
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_plot_initialize(const char *plotter_command)
{
    char *test_cmd = NULL;
    char *first_word = NULL;

    plotting_enabled = (strcmp(plotter_command, "no") != 0);
    
    /* Note that 'setenv' is *not* ANSI C. If it does not exist, tell user to
     *  define the environment variable him-/herself.
     */

    if (plotting_enabled)
    {
        const char *env = "CPL_PLOTTER";

        /* Check if 'which x' returns non-zero.
           x is the first word of plotting command.
           Note: this assumes the environment understands
           'which' and '> /dev/null'. If not,
           plotting will be disabled.
        */
        first_word = uves_sprintf("%s ", plotter_command);
        
        assure( strtok(first_word, " ") != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
            "Error splitting string '%s'", first_word);
        
        test_cmd = uves_sprintf("which %s > /dev/null", first_word);
        
#if defined HAVE_SETENV && HAVE_SETENV

        if (setenv(env, plotter_command, 1) != 0)
        {
            uves_msg_warning("Could not set environment variable '%s'. "
                     "Plotting disabled!", env);
            plotting_enabled = false;
        }
        /* popen may return non-NULL even when the external command
           is not available. This causes the recipe to crash when writing
           to an invalid FILE pointer.
           Therefore, check (using 'which') that the
           command is available.
        */
        else if (system(test_cmd) != 0)
        {
            uves_msg_debug("Command '%s' returned non-zero", test_cmd);
            uves_msg_warning("Command '%s' failed. Plotting disabled!", test_cmd);
            plotting_enabled = false;
        }
        else
        {
            /* Setenv succeeded, remember command */
            uves_msg_debug("setenv %s='%s' succeeded", env, plotter_command);
            uves_msg_debug("Command '%s' returned zero", test_cmd);

            plotter = plotter_command;
        }
#else
        uves_msg_warning("setenv() is not available on this platform. You have to manually "
                 "set the environment variable '%s' to '%s'", env, plotter_command);

        plotter = plotter_command;

#endif
    }   
   
  cleanup:
    cpl_free(test_cmd);
    cpl_free(first_word);

    return cpl_error_get_code();
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Plot one image row
   @param    image           Image to plot
   @param    first_row       First row to plot (counting from 1)
   @param    last_row        Last row to plot (counting from 1)
   @param    step            Plot only every n'th row where n = @em step
   @param    xtitle          Label on x-axis
   @param    ytitle          Label on y-axis
   @param    format          Title of plot, a printf-style format string
   @return   CPL_ERROR_NONE iff okay

   The input is sent to the previously specified plotter command.

*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_plot_image_rows(const cpl_image *image, int first_row, int last_row, int step, 
             const char *xtitle, const char *ytitle, const char *format, ...)
{
    va_list al;
    
    char *pre = NULL;
    char *options = NULL;
    const char *post = "";
    cpl_image *thresholded = NULL;

    assure( image != NULL, CPL_ERROR_NULL_INPUT, "Null image");
    if (xtitle == NULL) xtitle = "";
    if (ytitle == NULL) ytitle = "";
    assure( 1 <= first_row && first_row <= last_row && 
        last_row <= cpl_image_get_size_y(image), 
        CPL_ERROR_ILLEGAL_INPUT,
        "Illegal rows: %d - %d; rows in image = %" CPL_SIZE_FORMAT "",
        first_row, last_row, cpl_image_get_size_y(image));
    
    assure( step >= 1, CPL_ERROR_ILLEGAL_INPUT,
        "Illegal step size: %d", step);

    if (plotting_enabled)
    {
        const char *pre_format;
        int row;
        
        /* Create pre string */
        pre_format = "set grid; set xlabel '%s'; set ylabel '%s';";
        pre = cpl_calloc(strlen(pre_format) + 
                 strlen(xtitle) + strlen(ytitle) + 1,
                 sizeof(char));
        sprintf(pre, pre_format, xtitle, ytitle);

        
        va_start(al, format);
        vsnprintf(title, MAXTITLELENGTH - 1, format, al);
        va_end(al);
        title[MAXTITLELENGTH - 1] = '\0';
        
        options = title_string(title, cpl_image_get_size_x(image));

        /* Threshold each row */
        thresholded = cpl_image_duplicate(image);
        for (row = first_row; row <= last_row; row++)
        {
            int nx = cpl_image_get_size_x(thresholded);
            double median = cpl_image_get_median_window(thresholded,
                                1, first_row,
                                nx, last_row);
            double stdev = cpl_image_get_stdev_window(thresholded,
                                  1, first_row,
                                  nx, last_row);

            double locut = median - 3*stdev;
            double hicut = median + 3*stdev;
            
            int x, pis_rejected;

            for (x = 1; x <= nx; x++)
            {
                double data = 
                cpl_image_get(thresholded, x, row, &pis_rejected);
                if (data < locut) data = locut;
                if (data > hicut) data = hicut;
                cpl_image_set(thresholded, x, row, data);
            }
        }
        
        cpl_plot_image_row(pre,
                  (strcmp(options, "t '%s'") == 0) ? "" : options, 
                  post, 
                  thresholded,
                  first_row, last_row, step);
        
        RECOVER_FROM_ERROR(plotter);
    }
        
  cleanup:
    uves_free_image(&thresholded);
    cpl_free(pre);
    cpl_free(options);

    return cpl_error_get_code();
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Plot one image column
   @param    image           Image to plot
   @param    first_column    First column to plot (counting from 1)
   @param    last_column     Last column to plot (counting from 1)
   @param    step            Plot only every n'th column where n = @em step
   @param    xtitle          Label on x-axis
   @param    ytitle          Label on y-axis
   @param    format          Title of plot, a printf-style format string
   @return   CPL_ERROR_NONE iff okay

   The input is sent to the previously specified plotter command.

   Each row is thresholded to median +- 3 sigma before plotting.

   See also @c uves_plot_image_rows().
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_plot_image_columns(const cpl_image *image, int first_column, int last_column, int step,
            const char *xtitle, const char *ytitle, const char *format, ...)
{
    va_list al;
    
    char *pre = NULL;
    char *options = NULL;
    const char *post = "";
    cpl_image *thresholded = NULL;

    assure( image != NULL, CPL_ERROR_NULL_INPUT, "Null image");
    if (xtitle == NULL) xtitle = "";
    if (ytitle == NULL) ytitle = "";
    assure( 1 <= first_column && first_column <= last_column &&
        last_column <= cpl_image_get_size_x(image), 
        CPL_ERROR_ILLEGAL_INPUT,
        "Illegal columns: %d - %d; columns in image = %" CPL_SIZE_FORMAT "",
        first_column, last_column, cpl_image_get_size_x(image));
    
    assure( step >= 1, CPL_ERROR_ILLEGAL_INPUT,
        "Illegal step size: %d", step);
    
    if (plotting_enabled)
    {
        const char *pre_format;
        int col;

        /* Create pre string */
        pre_format = "set grid; set xlabel '%s'; set ylabel '%s';";
        pre = cpl_calloc(strlen(pre_format) + 
                 strlen(xtitle) + strlen(ytitle) + 1,
                 sizeof(char));
        sprintf(pre, pre_format, xtitle, ytitle);
        
        va_start(al, format);
        vsnprintf(title, MAXTITLELENGTH - 1, format, al);
        va_end(al);
        title[MAXTITLELENGTH - 1] = '\0';
        
        options = title_string(title, cpl_image_get_size_y(image));

        /* Threshold each column */
        thresholded = cpl_image_duplicate(image);
        for (col = first_column; col <= last_column; col++)
        {
            int ny = cpl_image_get_size_x(thresholded);
            double median = cpl_image_get_median_window(thresholded,
                                first_column, 1,
                                last_column, ny);
            double stdev = cpl_image_get_stdev_window(thresholded,
                                  first_column, 1,
                                  last_column, ny);

            double locut = median - 3*stdev;
            double hicut = median + 3*stdev;
            
            int y, pis_rejected;

            for (y = 1; y <= ny; y++)
            {
                double data = cpl_image_get(thresholded, col, y, &pis_rejected);
                if (data < locut) data = locut;
                if (data > hicut) data = hicut;
                cpl_image_set(thresholded, col, y, data);
            }
        }
        
        
        check( cpl_plot_image_col(pre,
                     (strcmp(options, "t '%s'") == 0) ? "" : options, 
                     post, 
                     image,
                     first_column, last_column, step), 
           "Error plotting image");
        
        RECOVER_FROM_ERROR(plotter);
    }
    
  cleanup:
    uves_free_image(&thresholded);
    cpl_free(pre);
    cpl_free(options);

    return cpl_error_get_code();
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Plot bivectors
   @param    bivectors    to plot. The values in all arrays
                          will be thresholded. The threshold limits
              are computed based on the values in the first bivector
   @param    titles       array of titles, same length as bivector array
   @param    N            number of bivectors  
   @param    xtitle       x label
   @param    ytitle       y label
   @return   CPL_ERROR_NONE iff okay

   The input is sent to the previously specified plotter command.
*/
/*----------------------------------------------------------------------------*/
void
uves_plot_bivectors(cpl_bivector **bivectors, char **titles, 
            int N, const char *xtitle,
            const char *ytitle)
{
    char *pre = NULL;
    char **options = NULL;
    const char *post = "";

    options = cpl_calloc(N, sizeof(char *)); /* Initialized to NULL */
    assure_mem( options );

    if (plotting_enabled)
    {
        int npoints, i;
        cpl_bivector *temp;
        char *temps;

        /* Create options strings */
        
        npoints = 0;
        for (i = 0; i < N; i++)
        {
            npoints += cpl_bivector_get_size(bivectors[i]);
        }
        for (i = 0; i < N; i++)
        {        
            options[i] = title_string(titles[i], npoints);
        }
        
        
        {
        double datamax = cpl_vector_get_max(cpl_bivector_get_y(bivectors[0]));
        double datamin = cpl_vector_get_min(cpl_bivector_get_y(bivectors[0]));

        double locut = datamin - 0.2*(datamax-datamin);
        double hicut = datamax + 0.2*(datamax-datamin);
        
        for (i = 0; i < N; i++)
            {
            int j;
            for (j = 0; j < cpl_bivector_get_size(bivectors[i]); j++)
                {
                if (cpl_bivector_get_y_data(bivectors[i])[j] < locut)
                    {
                    cpl_bivector_get_y_data(bivectors[i])[j] = locut;
                    }
                if (cpl_bivector_get_y_data(bivectors[i])[j] > hicut)
                    {
                    cpl_bivector_get_y_data(bivectors[i])[j] = hicut;
                    }
                }
            }
        }

        /* Swap first/last bivectors */
        temp = bivectors[0];
        bivectors[0] = bivectors[N-1];
        bivectors[N-1] = temp;
        
        temps = options[0];
        options[0] = options[N-1];
        options[N-1] = temps;
        
        pre = uves_sprintf(
        "set grid; set xlabel '%s'; set ylabel '%s';", xtitle, ytitle);
        
        cpl_plot_bivectors(pre,
                  (const char **)options,
                  post,
                  (const cpl_bivector **)bivectors, N);
        
        RECOVER_FROM_ERROR(plotter);
    }

  cleanup:
    cpl_free(pre);
    {
    int i;
    for (i = 0; i < N; i++)
        {        
        cpl_free(options[i]);
        }
    }
    cpl_free(options);
    return;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Plot two table columns
   @param    table        Table to plot
   @param    colx         Name of x-column
   @param    coly         Name of y-column
   @param    format       Title of plot, a printf-style format string
   @return   CPL_ERROR_NONE iff okay

   The column names are also used as the label names, and the column
   comment strings are not used.

   Values outside median +- 3sigma are not plotted.

   The input is sent to the previously specified plotter command.
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_plot_table(const cpl_table *table, const char *colx, const char *coly, 
        const char *format, ...)
{
    va_list al;
    
    char *pre = NULL;
    char *options = NULL;
    const char *post = "";
    cpl_table *thresholded = NULL;

    assure( table != NULL, CPL_ERROR_NULL_INPUT, "Null table");
    assure( colx  != NULL, CPL_ERROR_NULL_INPUT, "Null x column");
    assure( coly  != NULL, CPL_ERROR_NULL_INPUT, "Null y column");
    assure( cpl_table_has_column(table, colx), CPL_ERROR_ILLEGAL_INPUT,
        "No such column: '%s'", colx);
    assure( cpl_table_has_column(table, coly), CPL_ERROR_ILLEGAL_INPUT,
        "No such column: '%s'", coly);
    
    assure( cpl_table_get_column_type(table, colx) == CPL_TYPE_INT    ||
        cpl_table_get_column_type(table, colx) == CPL_TYPE_FLOAT  ||
        cpl_table_get_column_type(table, colx) == CPL_TYPE_DOUBLE, 
        CPL_ERROR_TYPE_MISMATCH,
        "Column '%s' has type '%s'. Numerical type expected",
        colx,
        uves_tostring_cpl_type(cpl_table_get_column_type(table, colx)));
    
    assure( cpl_table_get_column_type(table, coly) == CPL_TYPE_INT    ||
        cpl_table_get_column_type(table, coly) == CPL_TYPE_FLOAT  ||
        cpl_table_get_column_type(table, coly) == CPL_TYPE_DOUBLE, 
        CPL_ERROR_TYPE_MISMATCH,
        "Column '%s' has type '%s'. Numerical type expected",
        coly,
        uves_tostring_cpl_type(cpl_table_get_column_type(table, coly)));
    
    if (plotting_enabled)
    {
        const char *pre_format;

        /* Create options string */
        va_start(al, format);
        vsnprintf(title, MAXTITLELENGTH - 1, format, al);
        va_end(al);
        title[MAXTITLELENGTH - 1] = '\0';
        
        options = title_string(title, cpl_table_get_nrow(table));
        
        /* Create pre string */
        pre_format = "set grid; set xlabel '%s'; set ylabel '%s';";
        pre = cpl_calloc(strlen(pre_format) + strlen(colx) + strlen(coly) + 1, 
                 sizeof(char));  
                             /* It's a couple of bytes more than enough */
        sprintf(pre, pre_format, colx, coly);


        /* Threshold y-values to median +- 3 sigma before plotting */
        {
        double median, sigma, locut, hicut;
        int i;
        
        median = cpl_table_get_column_median(table, coly);
        sigma  = cpl_table_get_column_stdev(table, coly);
        
        locut = median - 3*sigma;
        hicut = median + 3*sigma;
        
        /* Copy the data we need, then threshold */
        thresholded = cpl_table_new(cpl_table_get_nrow(table));
        cpl_table_duplicate_column(thresholded, coly, table, coly);
        cpl_table_duplicate_column(thresholded, colx, table, colx);

        for (i = 0; i < cpl_table_get_nrow(thresholded); i++)
            {
            double data = cpl_table_get(thresholded, coly, i, NULL); /* polymorphic */
            
            if (data < locut && data > hicut)
                {
                cpl_table_set_invalid(thresholded, coly, i);
                }
            }

        }
        cpl_table_erase_invalid(thresholded);

        cpl_plot_column(pre,
                  (strcmp(options, "t '%s'") == 0) ? "" : options, 
                  post,
                  thresholded, colx, coly);

        RECOVER_FROM_ERROR(plotter);
    }

  cleanup:
    uves_free_table(&thresholded);
    cpl_free(pre);
    cpl_free(options);

    return cpl_error_get_code();
}


/*----------------------------------------------------------------------------*/
/**
   @brief    Generate a gnuplot-syntax title string
   @param    plot_title      Title of plot
   @param    npoints    Number of points in plot
   @return   The dynamically allocated initialization string

   Also the plotting style (isolated points or connected points)
   is defined, depending on the number of points in the plot.
*/
/*----------------------------------------------------------------------------*/
static char *
title_string(const char *plot_title, int npoints)
{
    /* Option to choose plotting style
     * depending on the number of points 
     */
    const char *options = (npoints > 100) ?
    "w points pointsize 1" :
    "w linespoints pointsize 1";
    /* If less than, say, 100 points, connect them with lines */

    size_t length = strlen("t '' ") + strlen(plot_title) + strlen(options) + 1;
    char *result = cpl_calloc(length, sizeof(char));
    
    snprintf(result, length, "t '%s' %s", plot_title, options);
    
    return result;
}

/**@}*/