1 | |
---|
2 | /*-------------------------------------------------------------------------*/ |
---|
3 | /** |
---|
4 | @file gnuplot_i.h |
---|
5 | @author N. Devillard |
---|
6 | @date Sep 1998 |
---|
7 | @version $Revision: 1.11 $ |
---|
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 | */ |
---|
16 | /*--------------------------------------------------------------------------*/ |
---|
17 | |
---|
18 | /* |
---|
19 | $Id: gnuplot_i.h,v 1.11 2003/01/27 08:58:04 ndevilla Exp $ |
---|
20 | $Author: ndevilla $ |
---|
21 | $Date: 2003/01/27 08:58:04 $ |
---|
22 | $Revision: 1.11 $ |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef _GNUPLOT_PIPES_H_ |
---|
26 | #define _GNUPLOT_PIPES_H_ |
---|
27 | |
---|
28 | /*--------------------------------------------------------------------------- |
---|
29 | Includes |
---|
30 | ---------------------------------------------------------------------------*/ |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <string.h> |
---|
35 | #include <unistd.h> |
---|
36 | #include <stdarg.h> |
---|
37 | |
---|
38 | |
---|
39 | /** Maximal number of simultaneous temporary files */ |
---|
40 | #define GP_MAX_TMP_FILES 64 |
---|
41 | /** Maximal size of a temporary file name */ |
---|
42 | #define GP_TMP_NAME_SIZE 512 |
---|
43 | |
---|
44 | |
---|
45 | /*--------------------------------------------------------------------------- |
---|
46 | New Types |
---|
47 | ---------------------------------------------------------------------------*/ |
---|
48 | |
---|
49 | /*-------------------------------------------------------------------------*/ |
---|
50 | /** |
---|
51 | @typedef gnuplot_ctrl |
---|
52 | @brief gnuplot session handle (opaque type). |
---|
53 | |
---|
54 | This structure holds all necessary information to talk to a gnuplot |
---|
55 | session. It is built and returned by gnuplot_init() and later used |
---|
56 | by all functions in this module to communicate with the session, then |
---|
57 | meant to be closed by gnuplot_close(). |
---|
58 | |
---|
59 | This structure is meant to remain opaque, you normally do not need |
---|
60 | to know what is contained in there. |
---|
61 | */ |
---|
62 | /*-------------------------------------------------------------------------*/ |
---|
63 | |
---|
64 | typedef struct _GNUPLOT_CTRL_ { |
---|
65 | /** Pipe to gnuplot process */ |
---|
66 | FILE * gnucmd ; |
---|
67 | |
---|
68 | /** Number of currently active plots */ |
---|
69 | int nplots ; |
---|
70 | /** Current plotting style */ |
---|
71 | char pstyle[32] ; |
---|
72 | |
---|
73 | /** Name of temporary files */ |
---|
74 | char to_delete[GP_MAX_TMP_FILES][GP_TMP_NAME_SIZE] ; |
---|
75 | /** Number of temporary files */ |
---|
76 | int ntmp ; |
---|
77 | } gnuplot_ctrl ; |
---|
78 | |
---|
79 | /*--------------------------------------------------------------------------- |
---|
80 | Function ANSI C prototypes |
---|
81 | ---------------------------------------------------------------------------*/ |
---|
82 | |
---|
83 | |
---|
84 | /*-------------------------------------------------------------------------*/ |
---|
85 | /** |
---|
86 | @brief Find out where a command lives in your PATH. |
---|
87 | @param pname Name of the program to look for. |
---|
88 | @return pointer to statically allocated character string. |
---|
89 | |
---|
90 | This is the C equivalent to the 'which' command in Unix. It parses |
---|
91 | out your PATH environment variable to find out where a command |
---|
92 | lives. The returned character string is statically allocated within |
---|
93 | this function, i.e. there is no need to free it. Beware that the |
---|
94 | contents of this string will change from one call to the next, |
---|
95 | though (as all static variables in a function). |
---|
96 | |
---|
97 | The input character string must be the name of a command without |
---|
98 | prefixing path of any kind, i.e. only the command name. The returned |
---|
99 | string is the path in which a command matching the same name was |
---|
100 | found. |
---|
101 | |
---|
102 | Examples (assuming there is a prog named 'hello' in the cwd): |
---|
103 | |
---|
104 | @verbatim |
---|
105 | gnuplot_get_program_path("hello") returns "." |
---|
106 | gnuplot_get_program_path("ls") returns "/bin" |
---|
107 | gnuplot_get_program_path("csh") returns "/usr/bin" |
---|
108 | gnuplot_get_program_path("/bin/ls") returns NULL |
---|
109 | @endverbatim |
---|
110 | |
---|
111 | */ |
---|
112 | /*-------------------------------------------------------------------------*/ |
---|
113 | char * gnuplot_get_program_path(char * pname); |
---|
114 | |
---|
115 | /*-------------------------------------------------------------------------*/ |
---|
116 | /** |
---|
117 | @brief Opens up a gnuplot session, ready to receive commands. |
---|
118 | @return Newly allocated gnuplot control structure. |
---|
119 | |
---|
120 | This opens up a new gnuplot session, ready for input. The struct |
---|
121 | controlling a gnuplot session should remain opaque and only be |
---|
122 | accessed through the provided functions. |
---|
123 | |
---|
124 | The session must be closed using gnuplot_close(). |
---|
125 | */ |
---|
126 | /*--------------------------------------------------------------------------*/ |
---|
127 | gnuplot_ctrl * gnuplot_init(void); |
---|
128 | |
---|
129 | /*-------------------------------------------------------------------------*/ |
---|
130 | /** |
---|
131 | @brief Closes a gnuplot session previously opened by gnuplot_init() |
---|
132 | @param handle Gnuplot session control handle. |
---|
133 | @return void |
---|
134 | |
---|
135 | Kills the child PID and deletes all opened temporary files. |
---|
136 | It is mandatory to call this function to close the handle, otherwise |
---|
137 | temporary files are not cleaned and child process might survive. |
---|
138 | |
---|
139 | */ |
---|
140 | /*--------------------------------------------------------------------------*/ |
---|
141 | void gnuplot_close(gnuplot_ctrl * handle); |
---|
142 | |
---|
143 | /*-------------------------------------------------------------------------*/ |
---|
144 | /** |
---|
145 | @brief Sends a command to an active gnuplot session. |
---|
146 | @param handle Gnuplot session control handle |
---|
147 | @param cmd Command to send, same as a printf statement. |
---|
148 | |
---|
149 | This sends a string to an active gnuplot session, to be executed. |
---|
150 | There is strictly no way to know if the command has been |
---|
151 | successfully executed or not. |
---|
152 | The command syntax is the same as printf. |
---|
153 | |
---|
154 | Examples: |
---|
155 | |
---|
156 | @code |
---|
157 | gnuplot_cmd(g, "plot %d*x", 23.0); |
---|
158 | gnuplot_cmd(g, "plot %g * cos(%g * x)", 32.0, -3.0); |
---|
159 | @endcode |
---|
160 | |
---|
161 | Since the communication to the gnuplot process is run through |
---|
162 | a standard Unix pipe, it is only unidirectional. This means that |
---|
163 | it is not possible for this interface to query an error status |
---|
164 | back from gnuplot. |
---|
165 | */ |
---|
166 | /*--------------------------------------------------------------------------*/ |
---|
167 | void gnuplot_cmd(gnuplot_ctrl * handle, char * cmd, ...); |
---|
168 | |
---|
169 | /*-------------------------------------------------------------------------*/ |
---|
170 | /** |
---|
171 | @brief Change the plotting style of a gnuplot session. |
---|
172 | @param h Gnuplot session control handle |
---|
173 | @param plot_style Plotting-style to use (character string) |
---|
174 | @return void |
---|
175 | |
---|
176 | The provided plotting style is a character string. It must be one of |
---|
177 | the following: |
---|
178 | |
---|
179 | - lines |
---|
180 | - points |
---|
181 | - linespoints |
---|
182 | - impulses |
---|
183 | - dots |
---|
184 | - steps |
---|
185 | - errorbars |
---|
186 | - boxes |
---|
187 | - boxeserrorbars |
---|
188 | */ |
---|
189 | /*--------------------------------------------------------------------------*/ |
---|
190 | void gnuplot_setstyle(gnuplot_ctrl * h, char * plot_style); |
---|
191 | |
---|
192 | /*-------------------------------------------------------------------------*/ |
---|
193 | /** |
---|
194 | @brief Sets the x label of a gnuplot session. |
---|
195 | @param h Gnuplot session control handle. |
---|
196 | @param label Character string to use for X label. |
---|
197 | @return void |
---|
198 | |
---|
199 | Sets the x label for a gnuplot session. |
---|
200 | */ |
---|
201 | /*--------------------------------------------------------------------------*/ |
---|
202 | void gnuplot_set_xlabel(gnuplot_ctrl * h, char * label); |
---|
203 | |
---|
204 | |
---|
205 | /*-------------------------------------------------------------------------*/ |
---|
206 | /** |
---|
207 | @brief Sets the y label of a gnuplot session. |
---|
208 | @param h Gnuplot session control handle. |
---|
209 | @param label Character string to use for Y label. |
---|
210 | @return void |
---|
211 | |
---|
212 | Sets the y label for a gnuplot session. |
---|
213 | */ |
---|
214 | /*--------------------------------------------------------------------------*/ |
---|
215 | void gnuplot_set_ylabel(gnuplot_ctrl * h, char * label); |
---|
216 | |
---|
217 | /*-------------------------------------------------------------------------*/ |
---|
218 | /** |
---|
219 | @brief Resets a gnuplot session (next plot will erase previous ones). |
---|
220 | @param h Gnuplot session control handle. |
---|
221 | @return void |
---|
222 | |
---|
223 | Resets a gnuplot session, i.e. the next plot will erase all previous |
---|
224 | ones. |
---|
225 | */ |
---|
226 | /*--------------------------------------------------------------------------*/ |
---|
227 | void gnuplot_resetplot(gnuplot_ctrl * h); |
---|
228 | |
---|
229 | |
---|
230 | /*-------------------------------------------------------------------------*/ |
---|
231 | /** |
---|
232 | @brief Plots a 2d graph from a list of doubles. |
---|
233 | @param handle Gnuplot session control handle. |
---|
234 | @param d Array of doubles. |
---|
235 | @param n Number of values in the passed array. |
---|
236 | @param title Title of the plot. |
---|
237 | @return void |
---|
238 | |
---|
239 | Plots out a 2d graph from a list of doubles. The x-coordinate is the |
---|
240 | index of the double in the list, the y coordinate is the double in |
---|
241 | the list. |
---|
242 | |
---|
243 | Example: |
---|
244 | |
---|
245 | @code |
---|
246 | gnuplot_ctrl *h ; |
---|
247 | double d[50] ; |
---|
248 | int i ; |
---|
249 | |
---|
250 | h = gnuplot_init() ; |
---|
251 | for (i=0 ; i<50 ; i++) { |
---|
252 | d[i] = (double)(i*i) ; |
---|
253 | } |
---|
254 | gnuplot_plot_x(h, d, 50, "parabola") ; |
---|
255 | sleep(2) ; |
---|
256 | gnuplot_close(h) ; |
---|
257 | @endcode |
---|
258 | */ |
---|
259 | /*--------------------------------------------------------------------------*/ |
---|
260 | void gnuplot_plot_x(gnuplot_ctrl * handle, double * d, int n, char * title); |
---|
261 | |
---|
262 | /*-------------------------------------------------------------------------*/ |
---|
263 | /** |
---|
264 | @brief Plot a 2d graph from a list of points. |
---|
265 | @param handle Gnuplot session control handle. |
---|
266 | @param x Pointer to a list of x coordinates. |
---|
267 | @param y Pointer to a list of y coordinates. |
---|
268 | @param n Number of doubles in x (assumed the same as in y). |
---|
269 | @param title Title of the plot. |
---|
270 | @return void |
---|
271 | |
---|
272 | Plots out a 2d graph from a list of points. Provide points through a list |
---|
273 | of x and a list of y coordinates. Both provided arrays are assumed to |
---|
274 | contain the same number of values. |
---|
275 | |
---|
276 | @code |
---|
277 | gnuplot_ctrl *h ; |
---|
278 | double x[50] ; |
---|
279 | double y[50] ; |
---|
280 | int i ; |
---|
281 | |
---|
282 | h = gnuplot_init() ; |
---|
283 | for (i=0 ; i<50 ; i++) { |
---|
284 | x[i] = (double)(i)/10.0 ; |
---|
285 | y[i] = x[i] * x[i] ; |
---|
286 | } |
---|
287 | gnuplot_plot_xy(h, x, y, 50, "parabola") ; |
---|
288 | sleep(2) ; |
---|
289 | gnuplot_close(h) ; |
---|
290 | @endcode |
---|
291 | */ |
---|
292 | /*--------------------------------------------------------------------------*/ |
---|
293 | void gnuplot_plot_xy( |
---|
294 | gnuplot_ctrl * handle, |
---|
295 | double * x, |
---|
296 | double * y, |
---|
297 | int n, |
---|
298 | char * title |
---|
299 | ) ; |
---|
300 | |
---|
301 | //////////////////////////////////////////////////////hakim s function//////////////////////////////////// |
---|
302 | /*-------------------------------------------------------------------------*/ |
---|
303 | /** |
---|
304 | @brief Plot a 2d graph from a list of points. |
---|
305 | @param handle Gnuplot session control handle. |
---|
306 | @param x Pointer to a list of x coordinates. |
---|
307 | @param y Pointer to a list of y coordinates. |
---|
308 | @param n Number of doubles in x (assumed the same as in y). |
---|
309 | @param title Title of the plot. |
---|
310 | @return void |
---|
311 | |
---|
312 | Plots out a 2d graph from a list of points. Provide points through a list |
---|
313 | of x and a list of y coordinates. Both provided arrays are assumed to |
---|
314 | contain the same number of values. |
---|
315 | |
---|
316 | |
---|
317 | */ |
---|
318 | /*--------------------------------------------------------------------------*/ |
---|
319 | void gnuplot_plot_datey( |
---|
320 | gnuplot_ctrl * handle, |
---|
321 | char x[][10], |
---|
322 | double * y, |
---|
323 | int n, |
---|
324 | char * title |
---|
325 | ) ; |
---|
326 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
327 | |
---|
328 | |
---|
329 | |
---|
330 | /*-------------------------------------------------------------------------*/ |
---|
331 | /** |
---|
332 | @brief Open a new session, plot a signal, close the session. |
---|
333 | @param title Plot title |
---|
334 | @param style Plot style |
---|
335 | @param label_x Label for X |
---|
336 | @param label_y Label for Y |
---|
337 | @param x Array of X coordinates |
---|
338 | @param y Array of Y coordinates (can be NULL) |
---|
339 | @param n Number of values in x and y. |
---|
340 | @return |
---|
341 | |
---|
342 | This function opens a new gnuplot session, plots the provided |
---|
343 | signal as an X or XY signal depending on a provided y, waits for |
---|
344 | a carriage return on stdin and closes the session. |
---|
345 | |
---|
346 | It is Ok to provide an empty title, empty style, or empty labels for |
---|
347 | X and Y. Defaults are provided in this case. |
---|
348 | */ |
---|
349 | /*--------------------------------------------------------------------------*/ |
---|
350 | void gnuplot_plot_once( |
---|
351 | char * title, |
---|
352 | char * style, |
---|
353 | char * label_x, |
---|
354 | char * label_y, |
---|
355 | double * x, |
---|
356 | double * y, |
---|
357 | int n |
---|
358 | ); |
---|
359 | |
---|
360 | /*-------------------------------------------------------------------------*/ |
---|
361 | /** |
---|
362 | @brief Plot a slope on a gnuplot session. |
---|
363 | @param handle Gnuplot session control handle. |
---|
364 | @param a Slope. |
---|
365 | @param b Intercept. |
---|
366 | @param title Title of the plot. |
---|
367 | @return void |
---|
368 | |
---|
369 | Plot a slope on a gnuplot session. The provided slope has an |
---|
370 | equation of the form y=ax+b |
---|
371 | |
---|
372 | Example: |
---|
373 | |
---|
374 | @code |
---|
375 | gnuplot_ctrl * h ; |
---|
376 | double a, b ; |
---|
377 | |
---|
378 | h = gnuplot_init() ; |
---|
379 | gnuplot_plot_slope(h, 1.0, 0.0, "unity slope") ; |
---|
380 | sleep(2) ; |
---|
381 | gnuplot_close(h) ; |
---|
382 | @endcode |
---|
383 | */ |
---|
384 | /*--------------------------------------------------------------------------*/ |
---|
385 | void gnuplot_plot_slope( |
---|
386 | gnuplot_ctrl * handle, |
---|
387 | double a, |
---|
388 | double b, |
---|
389 | char * title |
---|
390 | ) ; |
---|
391 | |
---|
392 | /*-------------------------------------------------------------------------*/ |
---|
393 | /** |
---|
394 | @brief Plot a curve of given equation y=f(x). |
---|
395 | @param h Gnuplot session control handle. |
---|
396 | @param equation Equation to plot. |
---|
397 | @param title Title of the plot. |
---|
398 | @return void |
---|
399 | |
---|
400 | Plots out a curve of given equation. The general form of the |
---|
401 | equation is y=f(x), you only provide the f(x) side of the equation. |
---|
402 | |
---|
403 | Example: |
---|
404 | |
---|
405 | @code |
---|
406 | gnuplot_ctrl *h ; |
---|
407 | char eq[80] ; |
---|
408 | |
---|
409 | h = gnuplot_init() ; |
---|
410 | strcpy(eq, "sin(x) * cos(2*x)") ; |
---|
411 | gnuplot_plot_equation(h, eq, "sine wave", normal) ; |
---|
412 | gnuplot_close(h) ; |
---|
413 | @endcode |
---|
414 | */ |
---|
415 | /*--------------------------------------------------------------------------*/ |
---|
416 | void gnuplot_plot_equation(gnuplot_ctrl * h, char * equation, char * title) ; |
---|
417 | |
---|
418 | #endif |
---|