1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
44 #ifdef HAVE_SYS_TIME_H
59 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
61 gmx_ctime_r(const time_t *clock, char *buf, int n);
64 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
68 xvg_format = output_env_get_xvg_format(oenv);
70 return (xvg_format == exvgXMGRACE || xvg_format == exvgXMGR);
73 static char *xvgrstr(const char *gmx, const output_env_t oenv,
74 char *buf, int buflen)
76 /* Supported greek letter names and corresponding xmgrace/xmgr symbols */
77 const char *sym[] = { "beta", "chi", "delta", "eta", "lambda", "mu", "omega", "phi", "psi", "rho", "theta", NULL };
78 const char symc[] = { 'b', 'c', 'd', 'h', 'l', 'm', 'w', 'f', 'y', 'r', 'q', '\0' };
84 xvgf = output_env_get_xvg_format(oenv);
85 bXVGR = (xvgf == exvgXMGRACE || xvgf == exvgXMGR);
89 while (gmx[g] != '\0')
91 /* Check with the largest string we have ("lambda"), add one for \0 */
92 if (b + 6 + 1 >= buflen)
94 gmx_fatal(FARGS, "Output buffer length in xvgstr (%d) too small to process xvg input string '%s'", buflen, gmx);
113 else if (gmx[g] == 'S')
127 else if (gmx[g] == 'N')
129 /* End sub/superscript */
144 else if (gmx[g] == '4')
146 /* Backward compatibility for xmgr normal font "\4" */
150 sprintf(buf+b, "%s", "\\f{}");
153 sprintf(buf+b, "%s", "\\4");
162 else if (gmx[g] == '8')
164 /* Backward compatibility for xmgr symbol font "\8" */
168 sprintf(buf+b, "%s", "\\x");
171 sprintf(buf+b, "%s", "\\8");
182 /* Check for special symbol */
184 while (sym[i] != NULL &&
185 gmx_strncasecmp(sym[i], gmx+g, strlen(sym[i])) != 0)
199 sprintf(buf+b, "%s%c%s", "\\x", c, "\\f{}");
202 sprintf(buf+b, "%s%c%s", "\\8", c, "\\4");
205 strncat(buf+b, gmx+g, strlen(sym[i]));
207 if (gmx[g+strlen(sym[i])] != ' ')
219 /* Unknown escape sequence, this should not happen.
220 * We do not generate a fatal error, since that might
221 * stop otherwise functioning code from working.
222 * Copy the backslash to the output and continue processing.
239 void xvgr_header(FILE *fp, const char *title, const char *xaxis,
240 const char *yaxis, int exvg_graph_type,
241 const output_env_t oenv)
243 char pukestr[100], buf[STRLEN];
246 if (output_env_get_print_xvgr_codes(oenv))
249 gmx_ctime_r(&t, buf, STRLEN);
250 fprintf(fp, "# This file was created %s", buf);
251 fprintf(fp, "# by the following command:\n# %s\n#\n", command_line());
252 fprintf(fp, "# %s is part of G R O M A C S:\n#\n", ShortProgram());
253 bromacs(pukestr, 99);
254 fprintf(fp, "# %s\n#\n", pukestr);
255 fprintf(fp, "@ title \"%s\"\n", xvgrstr(title, oenv, buf, STRLEN));
256 fprintf(fp, "@ xaxis label \"%s\"\n",
257 xvgrstr(xaxis, oenv, buf, STRLEN));
258 fprintf(fp, "@ yaxis label \"%s\"\n",
259 xvgrstr(yaxis, oenv, buf, STRLEN));
260 switch (exvg_graph_type)
263 if (output_env_get_xvg_format(oenv) == exvgXMGR)
265 fprintf(fp, "@TYPE nxy\n");
269 fprintf(fp, "@TYPE xy\n");
273 fprintf(fp, "@TYPE xydy\n");
276 fprintf(fp, "@TYPE xydydy\n");
282 FILE *xvgropen_type(const char *fn, const char *title, const char *xaxis,
283 const char *yaxis, int exvg_graph_type,
284 const output_env_t oenv)
289 fp = gmx_fio_fopen(fn, "w");
291 xvgr_header(fp, title, xaxis, yaxis, exvg_graph_type, oenv);
296 FILE *xvgropen(const char *fn, const char *title, const char *xaxis,
297 const char *yaxis, const output_env_t oenv)
299 return xvgropen_type(fn, title, xaxis, yaxis, exvggtXNY, oenv);
308 void xvgr_subtitle(FILE *out, const char *subtitle, const output_env_t oenv)
312 if (output_env_get_print_xvgr_codes(oenv))
314 fprintf(out, "@ subtitle \"%s\"\n", xvgrstr(subtitle, oenv, buf, STRLEN));
318 void xvgr_view(FILE *out, real xmin, real ymin, real xmax, real ymax,
319 const output_env_t oenv)
321 if (output_env_get_print_xvgr_codes(oenv))
323 fprintf(out, "@ view %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
327 void xvgr_world(FILE *out, real xmin, real ymin, real xmax, real ymax,
328 const output_env_t oenv)
330 if (output_env_get_print_xvgr_codes(oenv))
332 fprintf(out, "@ world xmin %g\n"
335 "@ world ymax %g\n", xmin, ymin, xmax, ymax);
339 void xvgr_legend(FILE *out, int nsets, const char** setname,
340 const output_env_t oenv)
345 if (output_env_get_print_xvgr_codes(oenv))
347 xvgr_view(out, 0.15, 0.15, 0.75, 0.85, oenv);
348 fprintf(out, "@ legend on\n");
349 fprintf(out, "@ legend box on\n");
350 fprintf(out, "@ legend loctype view\n");
351 fprintf(out, "@ legend %g, %g\n", 0.78, 0.8);
352 fprintf(out, "@ legend length %d\n", 2);
353 for (i = 0; (i < nsets); i++)
357 if (output_env_get_xvg_format(oenv) == exvgXMGR)
359 fprintf(out, "@ legend string %d \"%s\"\n",
360 i, xvgrstr(setname[i], oenv, buf, STRLEN));
364 fprintf(out, "@ s%d legend \"%s\"\n",
365 i, xvgrstr(setname[i], oenv, buf, STRLEN));
372 void xvgr_new_dataset(FILE *out, int nr_first, int nsets,
373 const char **setname,
374 const output_env_t oenv)
379 if (output_env_get_print_xvgr_codes(oenv))
382 for (i = 0; (i < nsets); i++)
386 if (output_env_get_xvg_format(oenv) == exvgXMGR)
388 fprintf(out, "@ legend string %d \"%s\"\n",
389 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
393 fprintf(out, "@ s%d legend \"%s\"\n",
394 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
405 void xvgr_line_props(FILE *out, int NrSet, int LineStyle, int LineColor,
406 const output_env_t oenv)
408 if (output_env_get_print_xvgr_codes(oenv))
410 fprintf(out, "@ with g0\n");
411 fprintf(out, "@ s%d linestyle %d\n", NrSet, LineStyle);
412 fprintf(out, "@ s%d color %d\n", NrSet, LineColor);
416 static const char *LocTypeStr[] = { "view", "world" };
417 static const char *BoxFillStr[] = { "none", "color", "pattern" };
419 void xvgr_box(FILE *out,
421 real xmin, real ymin, real xmax, real ymax,
422 int LineStyle, int LineWidth, int LineColor,
423 int BoxFill, int BoxColor, int BoxPattern, const output_env_t oenv)
425 if (output_env_get_print_xvgr_codes(oenv))
427 fprintf(out, "@with box\n");
428 fprintf(out, "@ box on\n");
429 fprintf(out, "@ box loctype %s\n", LocTypeStr[LocType]);
430 fprintf(out, "@ box %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
431 fprintf(out, "@ box linestyle %d\n", LineStyle);
432 fprintf(out, "@ box linewidth %d\n", LineWidth);
433 fprintf(out, "@ box color %d\n", LineColor);
434 fprintf(out, "@ box fill %s\n", BoxFillStr[BoxFill]);
435 fprintf(out, "@ box fill color %d\n", BoxColor);
436 fprintf(out, "@ box fill pattern %d\n", BoxPattern);
437 fprintf(out, "@box def\n");
441 /* reads a line into ptr, adjusting len and renewing ptr if neccesary */
442 static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
445 int len_remaining = *len; /* remaining amount of allocated bytes in buf */
446 int curp = 0; /* current position in buf to read into */
450 if (len_remaining < 2)
452 if (*len + STRLEN < maxlen)
454 /* This line is longer than len characters, let's increase len! */
456 len_remaining += STRLEN;
461 /*something is wrong, we'll just keep reading and return NULL*/
462 len_remaining = STRLEN;
466 if (fgets(*ptr + curp, len_remaining, fp) == NULL)
468 /* if last line, skip */
471 curp += len_remaining-1; /* overwrite the nul char in next iteration */
474 while ((strchr(*ptr, '\n') == NULL) && (!feof(fp)));
476 if (*len + STRLEN >= maxlen)
478 return NULL; /* this line was too long */
483 /* We reached EOF before '\n', skip this last line. */
487 /* now remove newline */
488 int slen = strlen(*ptr);
489 if ((*ptr)[slen-1] == '\n')
491 (*ptr)[slen-1] = '\0';
498 static int wordcount(char *ptr)
504 if (strlen(ptr) == 0)
508 /* fprintf(stderr,"ptr='%s'\n",ptr); */
510 for (i = 0; (ptr[i] != '\0'); i++)
512 is[cur] = isspace(ptr[i]);
513 if ((i > 0) && (is[cur] && !is[prev]))
522 static char *read_xvgr_string(const char *line)
524 const char *ptr0, *ptr1;
527 ptr0 = strchr(line, '"');
531 ptr1 = strchr(ptr0, '"');
535 str[ptr1-ptr0] = '\0';
550 int read_xvg_legend(const char *fn, double ***y, int *ny,
551 char **subtitle, char ***legend)
554 char *ptr, *ptr0, *ptr1;
557 int k, line = 0, nny, nx, maxx, rval, legend_nalloc, set, nchar;
566 fp = gmx_fio_fopen(fn, "r");
569 if (subtitle != NULL)
579 while ((ptr = fgets3(fp, &tmpbuf, &len, 10*STRLEN)) != NULL && ptr[0] != '&')
590 if (strncmp(ptr, "subtitle", 8) == 0)
593 if (subtitle != NULL)
595 *subtitle = read_xvgr_string(ptr);
598 else if (strncmp(ptr, "legend string", 13) == 0)
601 sscanf(ptr, "%d%n", &set, &nchar);
604 else if (ptr[0] == 's')
607 sscanf(ptr, "%d%n", &set, &nchar);
610 if (strncmp(ptr, "legend", 6) == 0)
621 if (set >= legend_nalloc)
623 legend_nalloc = set + 1;
624 srenew(*legend, legend_nalloc);
625 (*legend)[set] = read_xvgr_string(ptr);
630 else if (ptr[0] != '#')
634 (*ny) = nny = wordcount(ptr);
635 /* fprintf(stderr,"There are %d columns in your file\n",nny);*/
644 /* Allocate column space */
648 for (k = 0; (k < nny); k++)
653 /* Initiate format string */
657 /* fprintf(stderr,"ptr='%s'\n",ptr);*/
658 for (k = 0; (k < nny); k++)
662 rval = sscanf(ptr, fmt, &lf);
663 /* fprintf(stderr,"rval = %d\n",rval);*/
664 if ((rval == EOF) || (rval == 0))
669 srenew(fmt, 3*(nny+1)+1);
670 srenew(base, 3*nny+1);
675 fprintf(stderr, "Only %d columns on line %d in file %s\n",
677 for (; (k < nny); k++)
690 if (legend_nalloc > 0)
692 if (*ny - 1 > legend_nalloc)
694 srenew(*legend, *ny-1);
695 for (set = legend_nalloc; set < *ny-1; set++)
697 (*legend)[set] = NULL;
705 int read_xvg(const char *fn, double ***y, int *ny)
707 return read_xvg_legend(fn, y, ny, NULL, NULL);
710 void write_xvg(const char *fn, const char *title, int nx, int ny, real **y,
711 const char **leg, const output_env_t oenv)
716 fp = xvgropen(fn, title, "X", "Y", oenv);
719 xvgr_legend(fp, ny-1, leg, oenv);
721 for (i = 0; (i < nx); i++)
723 for (j = 0; (j < ny); j++)
725 fprintf(fp, " %12.5e", y[j][i]);
732 real **read_xvg_time(const char *fn,
733 gmx_bool bHaveT, gmx_bool bTB, real tb, gmx_bool bTE, real te,
734 int nsets_in, int *nset, int *nval, real *dt, real **t)
737 #define MAXLINELEN 16384
738 char line0[MAXLINELEN];
740 int t_nalloc, *val_nalloc, a, narg, n, sin, set, nchar;
741 double dbl, tend = 0;
742 gmx_bool bEndOfSet, bTimeInRange, bFirstLine = TRUE;
751 fp = gmx_fio_fopen(fn, "r");
752 for (sin = 0; sin < nsets_in; sin++)
760 narg = bHaveT ? 2 : 1;
764 while (!bEndOfSet && fgets(line0, MAXLINELEN, fp))
767 /* Remove whitespace */
768 while (line[0] == ' ' || line[0] == '\t')
772 bEndOfSet = (line[0] == '&');
773 if (line[0] != '#' && line[0] != '@' && line[0] != '\n' && !bEndOfSet)
775 if (bFirstLine && bHaveT)
777 /* Check the first line that should contain data */
778 a = sscanf(line, "%lf%lf", &dbl, &dbl);
781 gmx_fatal(FARGS, "Expected a number in %s on line:\n%s", fn, line0);
785 fprintf(stderr, "Found only 1 number on line, "
786 "assuming no time is present.\n");
797 while ((a < narg || (nsets_in == 1 && n == 0)) &&
798 sscanf(line, "%lf%n", &dbl, &nchar) == 1 && bTimeInRange)
800 /* Use set=-1 as the time "set" */
803 if (!bHaveT || (a > 0))
823 if (set == -1 && ((bTB && dbl < tb) || (bTE && dbl > te)))
825 bTimeInRange = FALSE;
840 srenew(val_nalloc, *nset);
851 t_nalloc = over_alloc_small(n);
852 srenew(*t, t_nalloc);
856 /* else we should check the time of the next sets with set 0 */
860 if (n >= val_nalloc[set])
862 val_nalloc[set] = over_alloc_small(n);
863 srenew(val[set], val_nalloc[set]);
865 val[set][n] = (real)dbl;
871 if (line0[strlen(line0)-1] != '\n')
873 fprintf(stderr, "File %s does not end with a newline, ignoring the last line\n", fn);
875 else if (bTimeInRange)
879 fprintf(stderr, "Ignoring invalid line in %s:\n%s", fn, line0);
885 fprintf(stderr, "Invalid line in %s:\n%s"
886 "Using zeros for the last %d sets\n",
904 for (a = 0; a < n; a++)
911 *dt = (real)((*t)[n-1]-(*t)[0])/(n-1.0);
922 fprintf(stderr, "Set %d is shorter (%d) than the previous set (%d)\n",
925 fprintf(stderr, "Will use only the first %d points of every set\n",