pmm  1.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
gnuplot_i.h
Go to the documentation of this file.
1 
2 /*-------------------------------------------------------------------------*/
3 /**
4  @file gnuplot_i.h
5  @author N. Devillard
6  @date Sep 1998, Oct 2004, Sept 2005, Nov 2005, Apr 2006, Dec 2009
7  @version $Revision: 1.11.4 $
8  @brief C interface to gnuplot.
9 
10  gnuplot is a freely available, command-driven graphical display tool for
11  Unix. It compiles and works quite well on a number of Unix flavours as
12  well as other operating systems. The following module enables sending
13  display requests to gnuplot through simple C calls.
14 
15  set_zlabel, splot and hardcopy_colour functions are based on existing code,
16  and added on 12 October 2004 by Robert Bradley (robert.bradley@merton.ox.ac.uk).
17  OS X and limited Windows support added 11-13th September 2005 - on Windows,
18  pgnuplot and wgnuplot.exe must be in the current directory.
19 
20  gnuplot_splot_grid added 2nd April 2006 (R. Bradley).
21 
22  gnuplot_set_axes ported from modifications to original 1.11 script Dec 2009
23  (R. Higgins)
24 
25  gnuplot_prompt Apr 2010 (R. Higgins)
26 */
27 /*--------------------------------------------------------------------------*/
28 
29 /*
30  $Id: gnuplot_i.h,v 1.11 2003/01/27 08:58:04 ndevilla Exp $
31  $Author: ndevilla $
32  $Date: 2003/01/27 08:58:04 $
33  $Revision: 1.11.2 $
34  */
35 
36 #ifndef _GNUPLOT_PIPES_H_
37 #define _GNUPLOT_PIPES_H_
38 
39 /*---------------------------------------------------------------------------
40  Includes
41  ---------------------------------------------------------------------------*/
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <stdarg.h>
48 #include <fcntl.h>
49 #include <time.h>
50 
51 /** Maximal number of simultaneous temporary files */
52 #define GP_MAX_TMP_FILES 64
53 /** Maximal size of a temporary file name */
54 #define GP_TMP_NAME_SIZE 512
55 
56 
57 /*---------------------------------------------------------------------------
58  New Types
59  ---------------------------------------------------------------------------*/
60 
61 /*-------------------------------------------------------------------------*/
62 /**
63  @typedef gnuplot_ctrl
64  @brief gnuplot session handle (opaque type).
65 
66  This structure holds all necessary information to talk to a gnuplot
67  session. It is built and returned by gnuplot_init() and later used
68  by all functions in this module to communicate with the session, then
69  meant to be closed by gnuplot_close().
70 
71  This structure is meant to remain opaque, you normally do not need
72  to know what is contained in there.
73  */
74 /*-------------------------------------------------------------------------*/
75 
76 typedef struct _GNUPLOT_CTRL_ {
77  /** Pipe to gnuplot process */
78  FILE * gnucmd ;
79 
80  /** Number of currently active plots */
81  int nplots ;
82  /** Current plotting style */
83  char pstyle[32] ;
84  /** Axes to plot on */
85  char axes[10] ;
86  /* Save terminal name (used by hardcopy functions) */
87  char term[32] ;
88 
89  /** Name of temporary files */
91  /** Number of temporary files */
92  int ntmp ;
93 } gnuplot_ctrl ;
94 
95 /*
96 gnuplot_point: Simple point struct to allow return of points to the
97 gnuplot_plot_obj_xy function by callback functions.
98 */
99 
100 typedef struct _GNUPLOT_POINT_ {
101  double x;
102  double y;
103  double z;
104 } gnuplot_point;
105 
106 
107 /*---------------------------------------------------------------------------
108  Function ANSI C prototypes
109  ---------------------------------------------------------------------------*/
110 
111 
112 /*-------------------------------------------------------------------------*/
113 /**
114  @brief Find out where a command lives in your PATH.
115  @param pname Name of the program to look for.
116  @return pointer to statically allocated character string.
117 
118  This is the C equivalent to the 'which' command in Unix. It parses
119  out your PATH environment variable to find out where a command
120  lives. The returned character string is statically allocated within
121  this function, i.e. there is no need to free it. Beware that the
122  contents of this string will change from one call to the next,
123  though (as all static variables in a function).
124 
125  The input character string must be the name of a command without
126  prefixing path of any kind, i.e. only the command name. The returned
127  string is the path in which a command matching the same name was
128  found.
129 
130  Examples (assuming there is a prog named 'hello' in the cwd):
131 
132  @verbatim
133  gnuplot_get_program_path("hello") returns "."
134  gnuplot_get_program_path("ls") returns "/bin"
135  gnuplot_get_program_path("csh") returns "/usr/bin"
136  gnuplot_get_program_path("/bin/ls") returns NULL
137  @endverbatim
138 
139  */
140 /*-------------------------------------------------------------------------*/
141 char * gnuplot_get_program_path(char * pname);
142 
143 /*-------------------------------------------------------------------------*/
144 /**
145  @brief Opens up a gnuplot session, ready to receive commands.
146  @return Newly allocated gnuplot control structure.
147 
148  This opens up a new gnuplot session, ready for input. The struct
149  controlling a gnuplot session should remain opaque and only be
150  accessed through the provided functions.
151 
152  The session must be closed using gnuplot_close().
153  */
154 /*--------------------------------------------------------------------------*/
155 gnuplot_ctrl * gnuplot_init(void);
156 
157 /*-------------------------------------------------------------------------*/
158 /**
159  @brief Closes a gnuplot session previously opened by gnuplot_init()
160  @param handle Gnuplot session control handle.
161  @return void
162 
163  Kills the child PID and deletes all opened temporary files.
164  It is mandatory to call this function to close the handle, otherwise
165  temporary files are not cleaned and child process might survive.
166 
167  */
168 /*--------------------------------------------------------------------------*/
169 void gnuplot_close(gnuplot_ctrl * handle);
170 
171 /*-------------------------------------------------------------------------*/
172 /**
173  @brief Sends a command to an active gnuplot session.
174  @param handle Gnuplot session control handle
175  @param cmd Command to send, same as a printf statement.
176 
177  This sends a string to an active gnuplot session, to be executed.
178  There is strictly no way to know if the command has been
179  successfully executed or not.
180  The command syntax is the same as printf.
181 
182  Examples:
183 
184  @code
185  gnuplot_cmd(g, "plot %d*x", 23.0);
186  gnuplot_cmd(g, "plot %g * cos(%g * x)", 32.0, -3.0);
187  @endcode
188 
189  Since the communication to the gnuplot process is run through
190  a standard Unix pipe, it is only unidirectional. This means that
191  it is not possible for this interface to query an error status
192  back from gnuplot.
193  */
194 /*--------------------------------------------------------------------------*/
195 void gnuplot_cmd(gnuplot_ctrl * handle, char * cmd, ...);
196 
197 /*-------------------------------------------------------------------------*/
198 /**
199  @brief Read commands from standard in and send them to gnuplot handle
200  @param handle Gnuplot session control handle
201 
202  This reads commands from a simple interactive prompt and sends them to
203  be executed via the gnuplot handle. There is no indicatio nof whether a
204  command is executed successfully or not and no checking of input command
205  syntax.
206 
207  Ctrl+D ends the interactive session.
208  */
209 /*--------------------------------------------------------------------------*/
210 void gnuplot_prompt(gnuplot_ctrl * handle);
211 
212 /*-------------------------------------------------------------------------*/
213 /**
214  @brief Change the plotting style of a gnuplot session.
215  @param h Gnuplot session control handle
216  @param plot_style Plotting-style to use (character string)
217  @return void
218 
219  The provided plotting style is a character string. It must be one of
220  the following:
221 
222  - lines
223  - points
224  - linespoints
225  - impulses
226  - dots
227  - steps
228  - errorbars
229  - boxes
230  - boxeserrorbars
231  */
232 /*--------------------------------------------------------------------------*/
233 void gnuplot_setstyle(gnuplot_ctrl * h, char * plot_style);
234 
235 /*-------------------------------------------------------------------------*/
236 /**
237  @brief Change the plotting axes of a gnuplot session.
238  @param h Gnuplot session control handle
239  @param plot_axes Plotting-axes to use (character string)
240  @return void
241 
242  The provided plotting axes is a character string. It must be one of
243  the following:
244 
245  - x1y1
246  - x1y2
247  - x2y1
248  - x2y2
249  */
250 /*--------------------------------------------------------------------------*/
251 void gnuplot_setaxes(gnuplot_ctrl * h, char * plot_axes);
252 
253 
254 /*-------------------------------------------------------------------------*/
255 /**
256  @brief Change the terminal of a gnuplot session.
257  @param h Gnuplot session control handle
258  @param terminal Terminal name (character string)
259  @return void
260 
261  No attempt is made to check the validity of the terminal name. This function
262  simply makes a note of it and calls gnuplot_cmd to change the name
263  */
264 /*--------------------------------------------------------------------------*/
265 
266 void gnuplot_setterm(gnuplot_ctrl * h, char * terminal);
267 
268 /*-------------------------------------------------------------------------*/
269 /**
270  @brief Sets the x label of a gnuplot session.
271  @param h Gnuplot session control handle.
272  @param label Character string to use for X label.
273  @return void
274 
275  Sets the x label for a gnuplot session.
276  */
277 /*--------------------------------------------------------------------------*/
278 void gnuplot_set_xlabel(gnuplot_ctrl * h, char * label);
279 
280 
281 /*-------------------------------------------------------------------------*/
282 /**
283  @brief Sets the y label of a gnuplot session.
284  @param h Gnuplot session control handle.
285  @param label Character string to use for Y label.
286  @return void
287 
288  Sets the y label for a gnuplot session.
289  */
290 /*--------------------------------------------------------------------------*/
291 void gnuplot_set_ylabel(gnuplot_ctrl * h, char * label);
292 
293 /*-------------------------------------------------------------------------*/
294 /**
295  @brief Sets the z label of a gnuplot session.
296  @param h Gnuplot session control handle.
297  @param label Character string to use for Z label.
298  @return void
299 
300  Sets the z label for a gnuplot session.
301  */
302 /*--------------------------------------------------------------------------*/
303 void gnuplot_set_zlabel(gnuplot_ctrl * h, char * label);
304 
305 /*-------------------------------------------------------------------------*/
306 /**
307  @brief Resets a gnuplot session (next plot will erase previous ones).
308  @param h Gnuplot session control handle.
309  @return void
310 
311  Resets a gnuplot session, i.e. the next plot will erase all previous
312  ones.
313  */
314 /*--------------------------------------------------------------------------*/
316 
317 
318 /*-------------------------------------------------------------------------*/
319 /**
320  @brief Plots a 2d graph from a list of doubles.
321  @param handle Gnuplot session control handle.
322  @param d Array of doubles.
323  @param n Number of values in the passed array.
324  @param title Title of the plot.
325  @return void
326 
327  Plots out a 2d graph from a list of doubles. The x-coordinate is the
328  index of the double in the list, the y coordinate is the double in
329  the list.
330 
331  Example:
332 
333  @code
334  gnuplot_ctrl *h ;
335  double d[50] ;
336  int i ;
337 
338  h = gnuplot_init() ;
339  for (i=0 ; i<50 ; i++) {
340  d[i] = (double)(i*i) ;
341  }
342  gnuplot_plot_x(h, d, 50, "parabola") ;
343  sleep(2) ;
344  gnuplot_close(h) ;
345  @endcode
346  */
347 /*--------------------------------------------------------------------------*/
348 void gnuplot_plot_x(gnuplot_ctrl * handle, double * d, int n, char * title);
349 
350 /*-------------------------------------------------------------------------*/
351 /**
352  @brief Plot a 2d graph from a list of points.
353  @param handle Gnuplot session control handle.
354  @param x Pointer to a list of x coordinates.
355  @param y Pointer to a list of y coordinates.
356  @param n Number of doubles in x (assumed the same as in y).
357  @param title Title of the plot.
358  @return void
359 
360  Plots out a 2d graph from a list of points. Provide points through a list
361  of x and a list of y coordinates. Both provided arrays are assumed to
362  contain the same number of values.
363 
364  @code
365  gnuplot_ctrl *h ;
366  double x[50] ;
367  double y[50] ;
368  int i ;
369 
370  h = gnuplot_init() ;
371  for (i=0 ; i<50 ; i++) {
372  x[i] = (double)(i)/10.0 ;
373  y[i] = x[i] * x[i] ;
374  }
375  gnuplot_plot_xy(h, x, y, 50, "parabola") ;
376  sleep(2) ;
377  gnuplot_close(h) ;
378  @endcode
379  */
380 /*--------------------------------------------------------------------------*/
381 void gnuplot_plot_xy(
382  gnuplot_ctrl * handle,
383  double * x,
384  double * y,
385  int n,
386  char * title
387 ) ;
388 
389 /*-------------------------------------------------------------------------*/
390 /**
391  @brief Plot a 3d graph from a list of points.
392  @param handle Gnuplot session control handle.
393  @param x Pointer to a list of x coordinates.
394  @param y Pointer to a list of y coordinates.
395  @param z Pointer to a list of z coordinates.
396  @param n Number of doubles in x (assumed the same as in y and z).
397  @param title Title of the plot.
398  @return void
399 
400  Plots out a 3d graph from a list of points. Provide points through lists
401  of x, y and z coordinates. Both provided arrays are assumed to contain the
402  same number of values.
403 */
404 int gnuplot_splot(
405  gnuplot_ctrl * handle,
406  double * x,
407  double * y,
408  double * z,
409  int n,
410  char * title
411 ) ;
412 
413 /*-------------------------------------------------------------------------*/
414 /**
415  @brief Plot a 3d graph from a grid of points.
416  @param handle Gnuplot session control handle.
417  @param points Pointer to a grid of points (rows,cols).
418  @param rows Number of rows (y points).
419  @param cols Number of columns (x points).
420  @param title Title of the plot.
421  @return void
422 
423  gnuplot_splot_grid(handle,(double *) points,rows,cols,title);
424  Based on gnuplot_splot, modifications by Robert Bradley 2/4/2006
425 
426  Plots a 3d graph from a grid of points, passed in the form of a 2D array [x,y].
427  */
428 /*--------------------------------------------------------------------------*/
429 
430 int gnuplot_splot_grid(gnuplot_ctrl *handle, double *points, int rows, int cols, char *title);
431 
432 /*-------------------------------------------------------------------------*/
433 /**
434  @brief Plot contours from a list of points.
435  @param handle Gnuplot session control handle.
436  @param x Pointer to a list of x coordinates. (length=nx*ny)
437  @param y Pointer to a list of y coordinates. (length=nx*ny)
438  @param z Pointer to a list of z coordinates. (length=nx*ny)
439  @param nx Number of doubles in x-direction
440  @param ny Number of doubles in y-direction
441  @param title Title of the plot.
442  @return void
443 
444  gnuplot_contour_plot(handle,x,y,z,n,title);
445  Based on gnuplot_splot, modifications by Robert Bradley 23/11/2005
446 
447  Plots a contour plot from a list of points, passed in the form of three arrays x, y and z.
448 
449  @code
450  gnuplot_ctrl *h ;
451  double x[50] ; y[50] ; z[50];
452  int i ;
453 
454  h = gnuplot_init() ;
455  int count=100;
456  double x[count*count],y[count*count],z[count*count];
457  int i,j;
458  for (i=0;i<count;i++) {
459  for (j=0;j<count;j++) {
460  x[count*i+j]=i;
461  y[count*i+j]=j;
462  z[count*i+j]=1000*sqrt(square(i-count/2)+square(j-count/2));
463  }
464  }
465  gnuplot_setstyle(h,"lines");
466  gnuplot_contour_plot(h,x,y,z,count,count,"Points");
467  sleep(2) ;
468  gnuplot_close(h) ;
469  @endcode
470  */
471 /*--------------------------------------------------------------------------*/
472 
473 int gnuplot_contour_plot(gnuplot_ctrl *handle, double *x, double *y, double *z, int nx, int ny, char *title);
474 
475 /*-------------------------------------------------------------------------*/
476 /**
477  @brief Plot a 3d graph using callback functions to return the points
478  @param handle Gnuplot session control handle.
479  @param obj Pointer to an arbitrary object.
480  @param getPoint Pointer to a callback function.
481  @param n Number of doubles in x (y and z must be the the same).
482  @param title Title of the plot.
483  @return void
484 
485 Calback:
486 
487 void getPoint(void *object,gnuplot_point *point,int index,int pointCount);
488  @param obj Pointer to an arbitrary object
489  @param point Pointer to the returned point struct (double x,y,z)
490  @param i Index of the current point (0 to n-1)
491  @param n Number of points
492  @return void
493  */
494 /*--------------------------------------------------------------------------*/
495 
496 int gnuplot_splot_obj(gnuplot_ctrl *handle,
497  void *obj,
498  void (*getPoint)(void*,gnuplot_point*,int,int),
499  int n,
500  char *title);
501 
502 /*
503  @brief Plot a 2d graph using a callback function to return points.
504  @param handle Gnuplot session control handle.
505  @param obj Pointer to an arbitrary object.
506  @param getPoint Pointer to a callback function.
507  @param n Number of points.
508  @param title Title of the plot.
509  @return void
510 
511 The callback function is of the following form, and is called once for each
512 point plotted:
513 
514 void getPoint(void *object,gnuplot_point *point,int index,int pointCount);
515  @param obj Pointer to an arbitrary object
516  @param point Pointer to the returned point struct (double x,y,z)
517  @param i Index of the current point (0 to n-1)
518  @param n Number of points
519  @return void
520 */
521 
523  gnuplot_ctrl *handle,
524  void *obj,
525  void (*getPoint)(void*,gnuplot_point*,int,int),
526  int n,
527  char *title
528 );
529 
530 /*-------------------------------------------------------------------------*/
531 /**
532  @brief Open a new session, plot a signal, close the session.
533  @param title Plot title
534  @param style Plot style
535  @param label_x Label for X
536  @param label_y Label for Y
537  @param x Array of X coordinates
538  @param y Array of Y coordinates (can be NULL)
539  @param n Number of values in x and y.
540  @return
541 
542  This function opens a new gnuplot session, plots the provided
543  signal as an X or XY signal depending on a provided y, waits for
544  a carriage return on stdin and closes the session.
545 
546  It is Ok to provide an empty title, empty style, or empty labels for
547  X and Y. Defaults are provided in this case.
548  */
549 /*--------------------------------------------------------------------------*/
550 void gnuplot_plot_once(
551  char * title,
552  char * style,
553  char * label_x,
554  char * label_y,
555  double * x,
556  double * y,
557  int n
558 );
559 
560 /*-------------------------------------------------------------------------*/
561 /**
562  @brief Plot a slope on a gnuplot session.
563  @param handle Gnuplot session control handle.
564  @param a Slope.
565  @param b Intercept.
566  @param title Title of the plot.
567  @return void
568  @doc
569 
570  Plot a slope on a gnuplot session. The provided slope has an
571  equation of the form y=ax+b
572 
573  Example:
574 
575  @code
576  gnuplot_ctrl * h ;
577  double a, b ;
578 
579  h = gnuplot_init() ;
580  gnuplot_plot_slope(h, 1.0, 0.0, "unity slope") ;
581  sleep(2) ;
582  gnuplot_close(h) ;
583  @endcode
584  */
585 /*--------------------------------------------------------------------------*/
586 void gnuplot_plot_slope(
587  gnuplot_ctrl * handle,
588  double a,
589  double b,
590  char * title
591 ) ;
592 
593 /*-------------------------------------------------------------------------*/
594 /**
595  @brief Plot a curve of given equation y=f(x).
596  @param h Gnuplot session control handle.
597  @param equation Equation to plot.
598  @param title Title of the plot.
599  @return void
600 
601  Plots out a curve of given equation. The general form of the
602  equation is y=f(x), you only provide the f(x) side of the equation.
603 
604  Example:
605 
606  @code
607  gnuplot_ctrl *h ;
608  char eq[80] ;
609 
610  h = gnuplot_init() ;
611  strcpy(eq, "sin(x) * cos(2*x)") ;
612  gnuplot_plot_equation(h, eq, "sine wave", normal) ;
613  gnuplot_close(h) ;
614  @endcode
615  */
616 /*--------------------------------------------------------------------------*/
617 void gnuplot_plot_equation(gnuplot_ctrl * h, char * equation, char * title) ;
618 
619 
620 
621 /*-------------------------------------------------------------------------*/
622 /**
623  @brief Save a graph as a postscript file on the hard disk
624  @param h Gnuplot session control handle.
625  @param filename Filname to save as
626  @return void
627 
628  A wrapper for the gnuplot_cmd function that sets the terminal
629  the terminal to postscript, replots the raph and then resets
630  the terminal to x11.
631 
632  Example:
633 
634  @code
635  gnuplot_ctrl *h ;
636  char eq[80] ;
637 
638  h = gnuplot_init() ;
639  strcpy(eq, "sin(x) * cos(2*x)") ;
640  gnuplot_plot_equation(h, eq, "sine wave", normal) ;
641  gnuplot_hardcopy(h, "sinewave.ps");
642  gnuplot_close(h) ;
643  @endcode
644  */
645 /*--------------------------------------------------------------------------*/
646 void gnuplot_hardcopy(gnuplot_ctrl * h, char * filename);
647 // gnuplot_hardcopy_colour added Oct 2004 by Robert Bradley
648 void gnuplot_hardcopy_colour(gnuplot_ctrl * h, char * filename);
649 
650 
651 #endif