1 | |
---|
2 | |
---|
3 | /*-------------------------------------------------------------------------*/ |
---|
4 | /** |
---|
5 | @file gnuplot_i.c |
---|
6 | @author N. Devillard |
---|
7 | @date Sep 1998 |
---|
8 | @version $Revision: 2.10 $ |
---|
9 | @brief C interface to gnuplot. |
---|
10 | |
---|
11 | gnuplot is a freely available, command-driven graphical display tool for |
---|
12 | Unix. It compiles and works quite well on a number of Unix flavours as |
---|
13 | well as other operating systems. The following module enables sending |
---|
14 | display requests to gnuplot through simple C calls. |
---|
15 | |
---|
16 | */ |
---|
17 | /*--------------------------------------------------------------------------*/ |
---|
18 | |
---|
19 | /* |
---|
20 | $Id: gnuplot_i.c,v 2.10 2003/01/27 08:58:04 ndevilla Exp $ |
---|
21 | $Author: ndevilla $ |
---|
22 | $Date: 2003/01/27 08:58:04 $ |
---|
23 | $Revision: 2.10 $ |
---|
24 | */ |
---|
25 | |
---|
26 | /*--------------------------------------------------------------------------- |
---|
27 | Includes |
---|
28 | ---------------------------------------------------------------------------*/ |
---|
29 | |
---|
30 | #include "gnuplot_i.h" |
---|
31 | |
---|
32 | /*--------------------------------------------------------------------------- |
---|
33 | Defines |
---|
34 | ---------------------------------------------------------------------------*/ |
---|
35 | |
---|
36 | /** Maximal size of a gnuplot command */ |
---|
37 | #define GP_CMD_SIZE 2048 |
---|
38 | /** Maximal size of a plot title */ |
---|
39 | #define GP_TITLE_SIZE 80 |
---|
40 | /** Maximal size for an equation */ |
---|
41 | #define GP_EQ_SIZE 512 |
---|
42 | /** Maximal size of a name in the PATH */ |
---|
43 | #define PATH_MAXNAMESZ 4096 |
---|
44 | |
---|
45 | /** Define P_tmpdir if not defined (this is normally a POSIX symbol) */ |
---|
46 | #ifndef P_tmpdir |
---|
47 | #define P_tmpdir "." |
---|
48 | #endif |
---|
49 | |
---|
50 | /*--------------------------------------------------------------------------- |
---|
51 | Function codes |
---|
52 | ---------------------------------------------------------------------------*/ |
---|
53 | |
---|
54 | |
---|
55 | /*-------------------------------------------------------------------------*/ |
---|
56 | /** |
---|
57 | @brief Find out where a command lives in your PATH. |
---|
58 | @param pname Name of the program to look for. |
---|
59 | @return pointer to statically allocated character string. |
---|
60 | |
---|
61 | This is the C equivalent to the 'which' command in Unix. It parses |
---|
62 | out your PATH environment variable to find out where a command |
---|
63 | lives. The returned character string is statically allocated within |
---|
64 | this function, i.e. there is no need to free it. Beware that the |
---|
65 | contents of this string will change from one call to the next, |
---|
66 | though (as all static variables in a function). |
---|
67 | |
---|
68 | The input character string must be the name of a command without |
---|
69 | prefixing path of any kind, i.e. only the command name. The returned |
---|
70 | string is the path in which a command matching the same name was |
---|
71 | found. |
---|
72 | |
---|
73 | Examples (assuming there is a prog named 'hello' in the cwd): |
---|
74 | |
---|
75 | @verbatim |
---|
76 | gnuplot_get_program_path("hello") returns "." |
---|
77 | gnuplot_get_program_path("ls") returns "/bin" |
---|
78 | gnuplot_get_program_path("csh") returns "/usr/bin" |
---|
79 | gnuplot_get_program_path("/bin/ls") returns NULL |
---|
80 | @endverbatim |
---|
81 | |
---|
82 | */ |
---|
83 | /*-------------------------------------------------------------------------*/ |
---|
84 | char * gnuplot_get_program_path(char * pname) |
---|
85 | { |
---|
86 | int i, j, lg; |
---|
87 | char * path; |
---|
88 | static char buf[PATH_MAXNAMESZ]; |
---|
89 | |
---|
90 | /* Trivial case: try in CWD */ |
---|
91 | sprintf(buf, "./%s", pname) ; |
---|
92 | if (access(buf, X_OK)==0) { |
---|
93 | sprintf(buf, "."); |
---|
94 | return buf ; |
---|
95 | } |
---|
96 | /* Try out in all paths given in the PATH variable */ |
---|
97 | buf[0] = 0; |
---|
98 | path = getenv("PATH") ; |
---|
99 | if (path!=NULL) { |
---|
100 | for (i=0; path[i]; ) { |
---|
101 | for (j=i ; (path[j]) && (path[j]!=':') ; j++); |
---|
102 | lg = j - i; |
---|
103 | strncpy(buf, path + i, lg); |
---|
104 | if (lg == 0) buf[lg++] = '.'; |
---|
105 | buf[lg++] = '/'; |
---|
106 | strcpy(buf + lg, pname); |
---|
107 | if (access(buf, X_OK) == 0) { |
---|
108 | /* Found it! */ |
---|
109 | break ; |
---|
110 | } |
---|
111 | buf[0] = 0; |
---|
112 | i = j; |
---|
113 | if (path[i] == ':') i++ ; |
---|
114 | } |
---|
115 | } else { |
---|
116 | fprintf(stderr, "PATH variable not set\n"); |
---|
117 | } |
---|
118 | /* If the buffer is still empty, the command was not found */ |
---|
119 | if (buf[0] == 0) return NULL ; |
---|
120 | /* Otherwise truncate the command name to yield path only */ |
---|
121 | lg = strlen(buf) - 1 ; |
---|
122 | while (buf[lg]!='/') { |
---|
123 | buf[lg]=0 ; |
---|
124 | lg -- ; |
---|
125 | } |
---|
126 | buf[lg] = 0; |
---|
127 | return buf ; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | /*-------------------------------------------------------------------------*/ |
---|
133 | /** |
---|
134 | @brief Opens up a gnuplot session, ready to receive commands. |
---|
135 | @return Newly allocated gnuplot control structure. |
---|
136 | |
---|
137 | This opens up a new gnuplot session, ready for input. The struct |
---|
138 | controlling a gnuplot session should remain opaque and only be |
---|
139 | accessed through the provided functions. |
---|
140 | |
---|
141 | The session must be closed using gnuplot_close(). |
---|
142 | */ |
---|
143 | /*--------------------------------------------------------------------------*/ |
---|
144 | |
---|
145 | gnuplot_ctrl * gnuplot_init(void) |
---|
146 | { |
---|
147 | gnuplot_ctrl * handle ; |
---|
148 | |
---|
149 | if (getenv("DISPLAY") == NULL) { |
---|
150 | fprintf(stderr, "cannot find DISPLAY variable: is it set?\n") ; |
---|
151 | } |
---|
152 | if (gnuplot_get_program_path("gnuplot")==NULL) { |
---|
153 | fprintf(stderr, "cannot find gnuplot in your PATH"); |
---|
154 | return NULL ; |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * Structure initialization: |
---|
159 | */ |
---|
160 | handle = (gnuplot_ctrl*)malloc(sizeof(gnuplot_ctrl)) ; |
---|
161 | handle->nplots = 0 ; |
---|
162 | gnuplot_setstyle(handle, "points") ; |
---|
163 | handle->ntmp = 0 ; |
---|
164 | |
---|
165 | handle->gnucmd = popen("gnuplot", "w") ; |
---|
166 | if (handle->gnucmd == NULL) { |
---|
167 | fprintf(stderr, "error starting gnuplot\n") ; |
---|
168 | free(handle) ; |
---|
169 | return NULL ; |
---|
170 | } |
---|
171 | return handle; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | /*-------------------------------------------------------------------------*/ |
---|
176 | /** |
---|
177 | @brief Closes a gnuplot session previously opened by gnuplot_init() |
---|
178 | @param handle Gnuplot session control handle. |
---|
179 | @return void |
---|
180 | |
---|
181 | Kills the child PID and deletes all opened temporary files. |
---|
182 | It is mandatory to call this function to close the handle, otherwise |
---|
183 | temporary files are not cleaned and child process might survive. |
---|
184 | |
---|
185 | */ |
---|
186 | /*--------------------------------------------------------------------------*/ |
---|
187 | |
---|
188 | void gnuplot_close(gnuplot_ctrl * handle) |
---|
189 | { |
---|
190 | int i ; |
---|
191 | |
---|
192 | if (pclose(handle->gnucmd) == -1) { |
---|
193 | fprintf(stderr, "problem closing communication to gnuplot\n") ; |
---|
194 | return ; |
---|
195 | } |
---|
196 | if (handle->ntmp) { |
---|
197 | for (i=0 ; i<handle->ntmp ; i++) { |
---|
198 | remove(handle->to_delete[i]) ; |
---|
199 | } |
---|
200 | } |
---|
201 | free(handle) ; |
---|
202 | return ; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | /*-------------------------------------------------------------------------*/ |
---|
207 | /** |
---|
208 | @brief Sends a command to an active gnuplot session. |
---|
209 | @param handle Gnuplot session control handle |
---|
210 | @param cmd Command to send, same as a printf statement. |
---|
211 | |
---|
212 | This sends a string to an active gnuplot session, to be executed. |
---|
213 | There is strictly no way to know if the command has been |
---|
214 | successfully executed or not. |
---|
215 | The command syntax is the same as printf. |
---|
216 | |
---|
217 | Examples: |
---|
218 | |
---|
219 | @code |
---|
220 | gnuplot_cmd(g, "plot %d*x", 23.0); |
---|
221 | gnuplot_cmd(g, "plot %g * cos(%g * x)", 32.0, -3.0); |
---|
222 | @endcode |
---|
223 | |
---|
224 | Since the communication to the gnuplot process is run through |
---|
225 | a standard Unix pipe, it is only unidirectional. This means that |
---|
226 | it is not possible for this interface to query an error status |
---|
227 | back from gnuplot. |
---|
228 | */ |
---|
229 | /*--------------------------------------------------------------------------*/ |
---|
230 | |
---|
231 | void gnuplot_cmd(gnuplot_ctrl * handle, char * cmd, ...) |
---|
232 | { |
---|
233 | va_list ap ; |
---|
234 | char local_cmd[GP_CMD_SIZE]; |
---|
235 | |
---|
236 | va_start(ap, cmd); |
---|
237 | vsprintf(local_cmd, cmd, ap); |
---|
238 | va_end(ap); |
---|
239 | |
---|
240 | strcat(local_cmd, "\n"); |
---|
241 | |
---|
242 | fputs(local_cmd, handle->gnucmd) ; |
---|
243 | fflush(handle->gnucmd) ; |
---|
244 | return ; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | /*-------------------------------------------------------------------------*/ |
---|
249 | /** |
---|
250 | @brief Change the plotting style of a gnuplot session. |
---|
251 | @param h Gnuplot session control handle |
---|
252 | @param plot_style Plotting-style to use (character string) |
---|
253 | @return void |
---|
254 | |
---|
255 | The provided plotting style is a character string. It must be one of |
---|
256 | the following: |
---|
257 | |
---|
258 | - lines |
---|
259 | - points |
---|
260 | - linespoints |
---|
261 | - impulses |
---|
262 | - dots |
---|
263 | - steps |
---|
264 | - errorbars |
---|
265 | - boxes |
---|
266 | - boxeserrorbars |
---|
267 | */ |
---|
268 | /*--------------------------------------------------------------------------*/ |
---|
269 | |
---|
270 | void gnuplot_setstyle(gnuplot_ctrl * h, char * plot_style) |
---|
271 | { |
---|
272 | if (strcmp(plot_style, "lines") && |
---|
273 | strcmp(plot_style, "points") && |
---|
274 | strcmp(plot_style, "linespoints") && |
---|
275 | strcmp(plot_style, "impulses") && |
---|
276 | strcmp(plot_style, "dots") && |
---|
277 | strcmp(plot_style, "steps") && |
---|
278 | strcmp(plot_style, "errorbars") && |
---|
279 | strcmp(plot_style, "boxes") && |
---|
280 | strcmp(plot_style, "boxerrorbars")) { |
---|
281 | fprintf(stderr, "warning: unknown requested style: using points\n") ; |
---|
282 | strcpy(h->pstyle, "points") ; |
---|
283 | } else { |
---|
284 | strcpy(h->pstyle, plot_style) ; |
---|
285 | } |
---|
286 | return ; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | /*-------------------------------------------------------------------------*/ |
---|
291 | /** |
---|
292 | @brief Sets the x label of a gnuplot session. |
---|
293 | @param h Gnuplot session control handle. |
---|
294 | @param label Character string to use for X label. |
---|
295 | @return void |
---|
296 | |
---|
297 | Sets the x label for a gnuplot session. |
---|
298 | */ |
---|
299 | /*--------------------------------------------------------------------------*/ |
---|
300 | |
---|
301 | void gnuplot_set_xlabel(gnuplot_ctrl * h, char * label) |
---|
302 | { |
---|
303 | char cmd[GP_CMD_SIZE] ; |
---|
304 | |
---|
305 | sprintf(cmd, "set xlabel \"%s\"", label) ; |
---|
306 | gnuplot_cmd(h, cmd) ; |
---|
307 | return ; |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | /*-------------------------------------------------------------------------*/ |
---|
312 | /** |
---|
313 | @brief Sets the y label of a gnuplot session. |
---|
314 | @param h Gnuplot session control handle. |
---|
315 | @param label Character string to use for Y label. |
---|
316 | @return void |
---|
317 | |
---|
318 | Sets the y label for a gnuplot session. |
---|
319 | */ |
---|
320 | /*--------------------------------------------------------------------------*/ |
---|
321 | |
---|
322 | void gnuplot_set_ylabel(gnuplot_ctrl * h, char * label) |
---|
323 | { |
---|
324 | char cmd[GP_CMD_SIZE] ; |
---|
325 | |
---|
326 | sprintf(cmd, "set ylabel \"%s\"", label) ; |
---|
327 | gnuplot_cmd(h, cmd) ; |
---|
328 | return ; |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | /*-------------------------------------------------------------------------*/ |
---|
333 | /** |
---|
334 | @brief Resets a gnuplot session (next plot will erase previous ones). |
---|
335 | @param h Gnuplot session control handle. |
---|
336 | @return void |
---|
337 | |
---|
338 | Resets a gnuplot session, i.e. the next plot will erase all previous |
---|
339 | ones. |
---|
340 | */ |
---|
341 | /*--------------------------------------------------------------------------*/ |
---|
342 | |
---|
343 | void gnuplot_resetplot(gnuplot_ctrl * h) |
---|
344 | { |
---|
345 | int i ; |
---|
346 | if (h->ntmp) { |
---|
347 | for (i=0 ; i<h->ntmp ; i++) { |
---|
348 | remove(h->to_delete[i]) ; |
---|
349 | } |
---|
350 | } |
---|
351 | h->ntmp = 0 ; |
---|
352 | h->nplots = 0 ; |
---|
353 | return ; |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | |
---|
358 | /*-------------------------------------------------------------------------*/ |
---|
359 | /** |
---|
360 | @brief Plots a 2d graph from a list of doubles. |
---|
361 | @param handle Gnuplot session control handle. |
---|
362 | @param d Array of doubles. |
---|
363 | @param n Number of values in the passed array. |
---|
364 | @param title Title of the plot. |
---|
365 | @return void |
---|
366 | |
---|
367 | Plots out a 2d graph from a list of doubles. The x-coordinate is the |
---|
368 | index of the double in the list, the y coordinate is the double in |
---|
369 | the list. |
---|
370 | |
---|
371 | Example: |
---|
372 | |
---|
373 | @code |
---|
374 | gnuplot_ctrl *h ; |
---|
375 | double d[50] ; |
---|
376 | int i ; |
---|
377 | |
---|
378 | h = gnuplot_init() ; |
---|
379 | for (i=0 ; i<50 ; i++) { |
---|
380 | d[i] = (double)(i*i) ; |
---|
381 | } |
---|
382 | gnuplot_plot_x(h, d, 50, "parabola") ; |
---|
383 | sleep(2) ; |
---|
384 | gnuplot_close(h) ; |
---|
385 | @endcode |
---|
386 | */ |
---|
387 | /*--------------------------------------------------------------------------*/ |
---|
388 | |
---|
389 | void gnuplot_plot_x( |
---|
390 | gnuplot_ctrl * handle, |
---|
391 | double * d, |
---|
392 | int n, |
---|
393 | char * title |
---|
394 | ) |
---|
395 | { |
---|
396 | int i ; |
---|
397 | int tmpfd ; |
---|
398 | char name[128] ; |
---|
399 | char cmd[GP_CMD_SIZE] ; |
---|
400 | char line[GP_CMD_SIZE] ; |
---|
401 | |
---|
402 | |
---|
403 | if (handle==NULL || d==NULL || (n<1)) return ; |
---|
404 | |
---|
405 | /* Open one more temporary file? */ |
---|
406 | if (handle->ntmp == GP_MAX_TMP_FILES - 1) { |
---|
407 | fprintf(stderr, |
---|
408 | "maximum # of temporary files reached (%d): cannot open more", |
---|
409 | GP_MAX_TMP_FILES) ; |
---|
410 | return ; |
---|
411 | } |
---|
412 | |
---|
413 | /* Open temporary file for output */ |
---|
414 | sprintf(name, "%s/gnuplot-i-XXXXXX", P_tmpdir); |
---|
415 | if ((tmpfd=mkstemp(name))==-1) { |
---|
416 | fprintf(stderr,"cannot create temporary file: exiting plot") ; |
---|
417 | return ; |
---|
418 | } |
---|
419 | |
---|
420 | /* Store file name in array for future deletion */ |
---|
421 | strcpy(handle->to_delete[handle->ntmp], name) ; |
---|
422 | handle->ntmp ++ ; |
---|
423 | /* Write data to this file */ |
---|
424 | for (i=0 ; i<n ; i++) { |
---|
425 | sprintf(line, "%g\n", d[i]); |
---|
426 | write(tmpfd, line, strlen(line)); |
---|
427 | } |
---|
428 | close(tmpfd) ; |
---|
429 | |
---|
430 | /* Command to be sent to gnuplot */ |
---|
431 | if (handle->nplots > 0) { |
---|
432 | strcpy(cmd, "replot") ; |
---|
433 | } else { |
---|
434 | strcpy(cmd, "plot") ; |
---|
435 | } |
---|
436 | |
---|
437 | if (title == NULL) { |
---|
438 | sprintf(line, "%s \"%s\" with %s", cmd, name, handle->pstyle) ; |
---|
439 | } else { |
---|
440 | sprintf(line, "%s \"%s\" title \"%s\" with %s", cmd, name, |
---|
441 | title, handle->pstyle) ; |
---|
442 | } |
---|
443 | |
---|
444 | /* send command to gnuplot */ |
---|
445 | gnuplot_cmd(handle, line) ; |
---|
446 | handle->nplots++ ; |
---|
447 | return ; |
---|
448 | } |
---|
449 | |
---|
450 | |
---|
451 | |
---|
452 | /*-------------------------------------------------------------------------*/ |
---|
453 | /** |
---|
454 | @brief Plot a 2d graph from a list of points. |
---|
455 | @param handle Gnuplot session control handle. |
---|
456 | @param x Pointer to a list of x coordinates. |
---|
457 | @param y Pointer to a list of y coordinates. |
---|
458 | @param n Number of doubles in x (assumed the same as in y). |
---|
459 | @param title Title of the plot. |
---|
460 | @return void |
---|
461 | |
---|
462 | Plots out a 2d graph from a list of points. Provide points through a list |
---|
463 | of x and a list of y coordinates. Both provided arrays are assumed to |
---|
464 | contain the same number of values. |
---|
465 | |
---|
466 | @code |
---|
467 | gnuplot_ctrl *h ; |
---|
468 | double x[50] ; |
---|
469 | double y[50] ; |
---|
470 | int i ; |
---|
471 | |
---|
472 | h = gnuplot_init() ; |
---|
473 | for (i=0 ; i<50 ; i++) { |
---|
474 | x[i] = (double)(i)/10.0 ; |
---|
475 | y[i] = x[i] * x[i] ; |
---|
476 | } |
---|
477 | gnuplot_plot_xy(h, x, y, 50, "parabola") ; |
---|
478 | sleep(2) ; |
---|
479 | gnuplot_close(h) ; |
---|
480 | @endcode |
---|
481 | */ |
---|
482 | /*--------------------------------------------------------------------------*/ |
---|
483 | |
---|
484 | void gnuplot_plot_xy( |
---|
485 | gnuplot_ctrl * handle, |
---|
486 | double * x, |
---|
487 | double * y, |
---|
488 | int n, |
---|
489 | char * title |
---|
490 | ) |
---|
491 | { |
---|
492 | int i ; |
---|
493 | int tmpfd ; |
---|
494 | char name[128] ; |
---|
495 | char cmd[GP_CMD_SIZE] ; |
---|
496 | char line[GP_CMD_SIZE] ; |
---|
497 | |
---|
498 | if (handle==NULL || x==NULL || y==NULL || (n<1)) return ; |
---|
499 | |
---|
500 | /* Open one more temporary file? */ |
---|
501 | if (handle->ntmp == GP_MAX_TMP_FILES - 1) { |
---|
502 | fprintf(stderr, |
---|
503 | "maximum # of temporary files reached (%d): cannot open more", |
---|
504 | GP_MAX_TMP_FILES) ; |
---|
505 | return ; |
---|
506 | } |
---|
507 | |
---|
508 | /* Open temporary file for output */ |
---|
509 | sprintf(name, "%s/gnuplot-i-XXXXXX", P_tmpdir); |
---|
510 | if ((tmpfd=mkstemp(name))==-1) { |
---|
511 | fprintf(stderr,"cannot create temporary file: exiting plot") ; |
---|
512 | return ; |
---|
513 | } |
---|
514 | /* Store file name in array for future deletion */ |
---|
515 | strcpy(handle->to_delete[handle->ntmp], name) ; |
---|
516 | handle->ntmp ++ ; |
---|
517 | |
---|
518 | /* Write data to this file */ |
---|
519 | for (i=0 ; i<n; i++) { |
---|
520 | sprintf(line, "%g %g\n", x[i], y[i]) ; |
---|
521 | write(tmpfd, line, strlen(line)); |
---|
522 | } |
---|
523 | close(tmpfd) ; |
---|
524 | |
---|
525 | /* Command to be sent to gnuplot */ |
---|
526 | if (handle->nplots > 0) { |
---|
527 | strcpy(cmd, "replot") ; |
---|
528 | } else { |
---|
529 | strcpy(cmd, "plot") ; |
---|
530 | } |
---|
531 | |
---|
532 | if (title == NULL) { |
---|
533 | sprintf(line, "%s \"%s\" with %s", cmd, name, handle->pstyle) ; |
---|
534 | } else { |
---|
535 | sprintf(line, "%s \"%s\" title \"%s\" with %s", cmd, name, |
---|
536 | title, handle->pstyle) ; |
---|
537 | } |
---|
538 | |
---|
539 | /* send command to gnuplot */ |
---|
540 | gnuplot_cmd(handle, line) ; |
---|
541 | handle->nplots++ ; |
---|
542 | return ; |
---|
543 | } |
---|
544 | |
---|
545 | |
---|
546 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
547 | void gnuplot_plot_datey( |
---|
548 | gnuplot_ctrl * handle, |
---|
549 | char x[][10], |
---|
550 | double * y, |
---|
551 | int n, |
---|
552 | char * title |
---|
553 | ) |
---|
554 | { |
---|
555 | //printf( "-----> date recupere %s \n", (char*)x) ; |
---|
556 | |
---|
557 | int i ; |
---|
558 | int tmpfd ; |
---|
559 | char name[128] ; |
---|
560 | char cmd[GP_CMD_SIZE] ; |
---|
561 | char line[GP_CMD_SIZE] ; |
---|
562 | |
---|
563 | if (handle==NULL || x==NULL || y==NULL || (n<1)) return ; |
---|
564 | |
---|
565 | /* Open one more temporary file? */ |
---|
566 | if (handle->ntmp == GP_MAX_TMP_FILES - 1) { |
---|
567 | fprintf(stderr, |
---|
568 | "maximum # of temporary files reached (%d): cannot open more", |
---|
569 | GP_MAX_TMP_FILES) ; |
---|
570 | return ; |
---|
571 | } |
---|
572 | |
---|
573 | /* Open temporary file for output */ |
---|
574 | sprintf(name, "%s/gnuplot-i-XXXXXX", P_tmpdir); |
---|
575 | if ((tmpfd=mkstemp(name))==-1) { |
---|
576 | fprintf(stderr,"cannot create temporary file: exiting plot") ; |
---|
577 | return ; |
---|
578 | } |
---|
579 | /* Store file name in array for future deletion */ |
---|
580 | strcpy(handle->to_delete[handle->ntmp], name) ; |
---|
581 | handle->ntmp ++ ; |
---|
582 | |
---|
583 | /* Write data to this file */ |
---|
584 | //char* date; |
---|
585 | //strncpy(date, x[0],10); |
---|
586 | //printf( "-----> date recupere %s \n", (char*)x) ; |
---|
587 | for (i=0 ; i<n; i++) { |
---|
588 | |
---|
589 | //printf("%s %g\n",x[i], y[i]); |
---|
590 | sprintf(line, "%s %g\n", x[i], y[i]) ; |
---|
591 | //sprintf(line, "%s %g\n", "v", y[i]) ; |
---|
592 | write(tmpfd, line, strlen(line)); |
---|
593 | //strncpy(date, x[i+10],10); |
---|
594 | } |
---|
595 | close(tmpfd) ; |
---|
596 | |
---|
597 | /* Command to be sent to gnuplot */ |
---|
598 | if (handle->nplots > 0) { |
---|
599 | strcpy(cmd, "replot") ; |
---|
600 | } else { |
---|
601 | strcpy(cmd, "plot") ; |
---|
602 | } |
---|
603 | |
---|
604 | if (title == NULL) { |
---|
605 | //sprintf(line, "%s \"%s\" with using 1:2 %s ", cmd, name, handle->pstyle) ; |
---|
606 | sprintf(line, "%s \"%s\" using 1:2 ", cmd, name); |
---|
607 | } else { |
---|
608 | sprintf(line, "%s \"%s\" using 1:2 ", cmd, name); |
---|
609 | /* sprintf(line, "%s \"%s\" title \"%s\" using 1:2 with %s ", cmd, name, |
---|
610 | title, handle->pstyle) ;*/ |
---|
611 | } |
---|
612 | |
---|
613 | /* send command to gnuplot */ |
---|
614 | |
---|
615 | |
---|
616 | gnuplot_cmd(handle, "set terminal png"); |
---|
617 | gnuplot_cmd(handle, "set output \"/tmp/sine.png\""); |
---|
618 | gnuplot_setstyle(handle,"lines"); |
---|
619 | gnuplot_cmd(handle, "set xdata time"); |
---|
620 | gnuplot_cmd(handle, "set timefmt \"%Y%m%d\""); |
---|
621 | //gnuplot_cmd(handle, "set xrange [\"19980401\":\"19980423\"]"); |
---|
622 | fflush(stdout); |
---|
623 | |
---|
624 | gnuplot_cmd(handle, line) ; |
---|
625 | handle->nplots++ ; |
---|
626 | return ; |
---|
627 | } |
---|
628 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
629 | |
---|
630 | |
---|
631 | |
---|
632 | |
---|
633 | |
---|
634 | |
---|
635 | |
---|
636 | /*-------------------------------------------------------------------------*/ |
---|
637 | /** |
---|
638 | @brief Open a new session, plot a signal, close the session. |
---|
639 | @param title Plot title |
---|
640 | @param style Plot style |
---|
641 | @param label_x Label for X |
---|
642 | @param label_y Label for Y |
---|
643 | @param x Array of X coordinates |
---|
644 | @param y Array of Y coordinates (can be NULL) |
---|
645 | @param n Number of values in x and y. |
---|
646 | @return |
---|
647 | |
---|
648 | This function opens a new gnuplot session, plots the provided |
---|
649 | signal as an X or XY signal depending on a provided y, waits for |
---|
650 | a carriage return on stdin and closes the session. |
---|
651 | |
---|
652 | It is Ok to provide an empty title, empty style, or empty labels for |
---|
653 | X and Y. Defaults are provided in this case. |
---|
654 | */ |
---|
655 | /*--------------------------------------------------------------------------*/ |
---|
656 | |
---|
657 | void gnuplot_plot_once( |
---|
658 | char * title, |
---|
659 | char * style, |
---|
660 | char * label_x, |
---|
661 | char * label_y, |
---|
662 | double * x, |
---|
663 | double * y, |
---|
664 | int n |
---|
665 | ) |
---|
666 | { |
---|
667 | gnuplot_ctrl * handle ; |
---|
668 | |
---|
669 | if (x==NULL || n<1) return ; |
---|
670 | |
---|
671 | if ((handle = gnuplot_init()) == NULL) return ; |
---|
672 | if (style!=NULL) { |
---|
673 | gnuplot_setstyle(handle, style); |
---|
674 | } else { |
---|
675 | gnuplot_setstyle(handle, "lines"); |
---|
676 | } |
---|
677 | if (label_x!=NULL) { |
---|
678 | gnuplot_set_xlabel(handle, label_x); |
---|
679 | } else { |
---|
680 | gnuplot_set_xlabel(handle, "X"); |
---|
681 | } |
---|
682 | if (label_y!=NULL) { |
---|
683 | gnuplot_set_ylabel(handle, label_y); |
---|
684 | } else { |
---|
685 | gnuplot_set_ylabel(handle, "Y"); |
---|
686 | } |
---|
687 | if (y==NULL) { |
---|
688 | gnuplot_plot_x(handle, x, n, title); |
---|
689 | } else { |
---|
690 | gnuplot_plot_xy(handle, x, y, n, title); |
---|
691 | } |
---|
692 | //printf("press ENTER to continue\n"); |
---|
693 | while (getchar()!='\n') {} |
---|
694 | gnuplot_close(handle); |
---|
695 | return ; |
---|
696 | } |
---|
697 | |
---|
698 | |
---|
699 | |
---|
700 | |
---|
701 | /*-------------------------------------------------------------------------*/ |
---|
702 | /** |
---|
703 | @brief Plot a slope on a gnuplot session. |
---|
704 | @param handle Gnuplot session control handle. |
---|
705 | @param a Slope. |
---|
706 | @param b Intercept. |
---|
707 | @param title Title of the plot. |
---|
708 | @return void |
---|
709 | |
---|
710 | Plot a slope on a gnuplot session. The provided slope has an |
---|
711 | equation of the form y=ax+b |
---|
712 | |
---|
713 | Example: |
---|
714 | |
---|
715 | @code |
---|
716 | gnuplot_ctrl * h ; |
---|
717 | double a, b ; |
---|
718 | |
---|
719 | h = gnuplot_init() ; |
---|
720 | gnuplot_plot_slope(h, 1.0, 0.0, "unity slope") ; |
---|
721 | sleep(2) ; |
---|
722 | gnuplot_close(h) ; |
---|
723 | @endcode |
---|
724 | */ |
---|
725 | /*--------------------------------------------------------------------------*/ |
---|
726 | |
---|
727 | |
---|
728 | void gnuplot_plot_slope( |
---|
729 | gnuplot_ctrl * handle, |
---|
730 | double a, |
---|
731 | double b, |
---|
732 | char * title |
---|
733 | ) |
---|
734 | { |
---|
735 | char stitle[GP_TITLE_SIZE] ; |
---|
736 | char cmd[GP_CMD_SIZE] ; |
---|
737 | |
---|
738 | if (title == NULL) { |
---|
739 | strcpy(stitle, "no title") ; |
---|
740 | } else { |
---|
741 | strcpy(stitle, title) ; |
---|
742 | } |
---|
743 | |
---|
744 | if (handle->nplots > 0) { |
---|
745 | sprintf(cmd, "replot %g * x + %g title \"%s\" with %s", |
---|
746 | a, b, title, handle->pstyle) ; |
---|
747 | } else { |
---|
748 | sprintf(cmd, "plot %g * x + %g title \"%s\" with %s", |
---|
749 | a, b, title, handle->pstyle) ; |
---|
750 | } |
---|
751 | gnuplot_cmd(handle, cmd) ; |
---|
752 | handle->nplots++ ; |
---|
753 | return ; |
---|
754 | } |
---|
755 | |
---|
756 | |
---|
757 | /*-------------------------------------------------------------------------*/ |
---|
758 | /** |
---|
759 | @brief Plot a curve of given equation y=f(x). |
---|
760 | @param h Gnuplot session control handle. |
---|
761 | @param equation Equation to plot. |
---|
762 | @param title Title of the plot. |
---|
763 | @return void |
---|
764 | |
---|
765 | Plots out a curve of given equation. The general form of the |
---|
766 | equation is y=f(x), you only provide the f(x) side of the equation. |
---|
767 | |
---|
768 | Example: |
---|
769 | |
---|
770 | @code |
---|
771 | gnuplot_ctrl *h ; |
---|
772 | char eq[80] ; |
---|
773 | |
---|
774 | h = gnuplot_init() ; |
---|
775 | strcpy(eq, "sin(x) * cos(2*x)") ; |
---|
776 | gnuplot_plot_equation(h, eq, "sine wave", normal) ; |
---|
777 | gnuplot_close(h) ; |
---|
778 | @endcode |
---|
779 | */ |
---|
780 | /*--------------------------------------------------------------------------*/ |
---|
781 | |
---|
782 | void gnuplot_plot_equation( |
---|
783 | gnuplot_ctrl * h, |
---|
784 | char * equation, |
---|
785 | char * title |
---|
786 | ) |
---|
787 | { |
---|
788 | char cmd[GP_CMD_SIZE]; |
---|
789 | char plot_str[GP_EQ_SIZE] ; |
---|
790 | char title_str[GP_TITLE_SIZE] ; |
---|
791 | |
---|
792 | if (title == NULL) { |
---|
793 | strcpy(title_str, "no title") ; |
---|
794 | } else { |
---|
795 | strcpy(title_str, title) ; |
---|
796 | } |
---|
797 | if (h->nplots > 0) { |
---|
798 | strcpy(plot_str, "replot") ; |
---|
799 | } else { |
---|
800 | strcpy(plot_str, "plot") ; |
---|
801 | } |
---|
802 | |
---|
803 | sprintf(cmd, "%s %s title \"%s\" with %s", |
---|
804 | plot_str, equation, title_str, h->pstyle) ; |
---|
805 | gnuplot_cmd(h, cmd) ; |
---|
806 | h->nplots++ ; |
---|
807 | return ; |
---|
808 | } |
---|
809 | |
---|
810 | /* vim: set ts=4 et sw=4 tw=75 */ |
---|