2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gromacs/fileio/xvgr.h"
45 #ifdef HAVE_SYS_TIME_H
49 #include "gromacs/legacyheaders/copyrite.h"
50 #include "gromacs/legacyheaders/oenv.h"
52 #include "gromacs/fileio/gmxfio.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/smalloc.h"
60 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
64 xvg_format = output_env_get_xvg_format(oenv);
66 return (xvg_format == exvgXMGRACE || xvg_format == exvgXMGR);
69 static char *xvgrstr(const char *gmx, const output_env_t oenv,
70 char *buf, int buflen)
72 /* Supported greek letter names and corresponding xmgrace/xmgr symbols */
73 const char *sym[] = { "beta", "chi", "delta", "eta", "lambda", "mu", "omega", "phi", "psi", "rho", "theta", NULL };
74 const char symc[] = { 'b', 'c', 'd', 'h', 'l', 'm', 'w', 'f', 'y', 'r', 'q', '\0' };
80 xvgf = output_env_get_xvg_format(oenv);
81 bXVGR = (xvgf == exvgXMGRACE || xvgf == exvgXMGR);
85 while (gmx[g] != '\0')
87 /* Check with the largest string we have ("lambda"), add one for \0 */
88 if (b + 6 + 1 >= buflen)
90 gmx_fatal(FARGS, "Output buffer length in xvgstr (%d) too small to process xvg input string '%s'", buflen, gmx);
109 else if (gmx[g] == 'S')
123 else if (gmx[g] == 'N')
125 /* End sub/superscript */
140 else if (gmx[g] == '4')
142 /* Backward compatibility for xmgr normal font "\4" */
146 sprintf(buf+b, "%s", "\\f{}");
149 sprintf(buf+b, "%s", "\\4");
158 else if (gmx[g] == '8')
160 /* Backward compatibility for xmgr symbol font "\8" */
164 sprintf(buf+b, "%s", "\\x");
167 sprintf(buf+b, "%s", "\\8");
178 /* Check for special symbol */
180 while (sym[i] != NULL &&
181 gmx_strncasecmp(sym[i], gmx+g, strlen(sym[i])) != 0)
195 sprintf(buf+b, "%s%c%s", "\\x", c, "\\f{}");
198 sprintf(buf+b, "%s%c%s", "\\8", c, "\\4");
201 strncat(buf+b, gmx+g, strlen(sym[i]));
203 if (gmx[g+strlen(sym[i])] != ' ')
215 /* Unknown escape sequence, this should not happen.
216 * We do not generate a fatal error, since that might
217 * stop otherwise functioning code from working.
218 * Copy the backslash to the output and continue processing.
235 void xvgr_header(FILE *fp, const char *title, const char *xaxis,
236 const char *yaxis, int exvg_graph_type,
237 const output_env_t oenv)
239 char pukestr[100], buf[STRLEN];
242 if (output_env_get_print_xvgr_codes(oenv))
245 gmx_ctime_r(&t, buf, STRLEN);
246 fprintf(fp, "# This file was created %s", buf);
249 gmx::BinaryInformationSettings settings;
250 settings.generatedByHeader(true);
251 settings.linePrefix("# ");
252 gmx::printBinaryInformation(fp, output_env_get_program_context(oenv),
255 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
256 fprintf(fp, "# %s is part of G R O M A C S:\n#\n",
257 output_env_get_program_display_name(oenv));
258 bromacs(pukestr, 99);
259 fprintf(fp, "# %s\n#\n", pukestr);
260 fprintf(fp, "@ title \"%s\"\n", xvgrstr(title, oenv, buf, STRLEN));
261 fprintf(fp, "@ xaxis label \"%s\"\n",
262 xvgrstr(xaxis, oenv, buf, STRLEN));
263 fprintf(fp, "@ yaxis label \"%s\"\n",
264 xvgrstr(yaxis, oenv, buf, STRLEN));
265 switch (exvg_graph_type)
268 if (output_env_get_xvg_format(oenv) == exvgXMGR)
270 fprintf(fp, "@TYPE nxy\n");
274 fprintf(fp, "@TYPE xy\n");
278 fprintf(fp, "@TYPE xydy\n");
281 fprintf(fp, "@TYPE xydydy\n");
287 FILE *xvgropen_type(const char *fn, const char *title, const char *xaxis,
288 const char *yaxis, int exvg_graph_type,
289 const output_env_t oenv)
293 fp = gmx_fio_fopen(fn, "w");
295 xvgr_header(fp, title, xaxis, yaxis, exvg_graph_type, oenv);
300 FILE *xvgropen(const char *fn, const char *title, const char *xaxis,
301 const char *yaxis, const output_env_t oenv)
303 return xvgropen_type(fn, title, xaxis, yaxis, exvggtXNY, oenv);
312 void xvgr_subtitle(FILE *out, const char *subtitle, const output_env_t oenv)
316 if (output_env_get_print_xvgr_codes(oenv))
318 fprintf(out, "@ subtitle \"%s\"\n", xvgrstr(subtitle, oenv, buf, STRLEN));
322 void xvgr_view(FILE *out, real xmin, real ymin, real xmax, real ymax,
323 const output_env_t oenv)
325 if (output_env_get_print_xvgr_codes(oenv))
327 fprintf(out, "@ view %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
331 void xvgr_world(FILE *out, real xmin, real ymin, real xmax, real ymax,
332 const output_env_t oenv)
334 if (output_env_get_print_xvgr_codes(oenv))
336 fprintf(out, "@ world xmin %g\n"
339 "@ world ymax %g\n", xmin, ymin, xmax, ymax);
343 void xvgr_legend(FILE *out, int nsets, const char** setname,
344 const output_env_t oenv)
349 if (output_env_get_print_xvgr_codes(oenv))
351 xvgr_view(out, 0.15, 0.15, 0.75, 0.85, oenv);
352 fprintf(out, "@ legend on\n");
353 fprintf(out, "@ legend box on\n");
354 fprintf(out, "@ legend loctype view\n");
355 fprintf(out, "@ legend %g, %g\n", 0.78, 0.8);
356 fprintf(out, "@ legend length %d\n", 2);
357 for (i = 0; (i < nsets); i++)
361 if (output_env_get_xvg_format(oenv) == exvgXMGR)
363 fprintf(out, "@ legend string %d \"%s\"\n",
364 i, xvgrstr(setname[i], oenv, buf, STRLEN));
368 fprintf(out, "@ s%d legend \"%s\"\n",
369 i, xvgrstr(setname[i], oenv, buf, STRLEN));
376 void xvgr_new_dataset(FILE *out, int nr_first, int nsets,
377 const char **setname,
378 const output_env_t oenv)
383 if (output_env_get_print_xvgr_codes(oenv))
386 for (i = 0; (i < nsets); i++)
390 if (output_env_get_xvg_format(oenv) == exvgXMGR)
392 fprintf(out, "@ legend string %d \"%s\"\n",
393 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
397 fprintf(out, "@ s%d legend \"%s\"\n",
398 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
409 void xvgr_line_props(FILE *out, int NrSet, int LineStyle, int LineColor,
410 const output_env_t oenv)
412 if (output_env_get_print_xvgr_codes(oenv))
414 fprintf(out, "@ with g0\n");
415 fprintf(out, "@ s%d linestyle %d\n", NrSet, LineStyle);
416 fprintf(out, "@ s%d color %d\n", NrSet, LineColor);
420 static const char *LocTypeStr[] = { "view", "world" };
421 static const char *BoxFillStr[] = { "none", "color", "pattern" };
423 void xvgr_box(FILE *out,
425 real xmin, real ymin, real xmax, real ymax,
426 int LineStyle, int LineWidth, int LineColor,
427 int BoxFill, int BoxColor, int BoxPattern, const output_env_t oenv)
429 if (output_env_get_print_xvgr_codes(oenv))
431 fprintf(out, "@with box\n");
432 fprintf(out, "@ box on\n");
433 fprintf(out, "@ box loctype %s\n", LocTypeStr[LocType]);
434 fprintf(out, "@ box %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
435 fprintf(out, "@ box linestyle %d\n", LineStyle);
436 fprintf(out, "@ box linewidth %d\n", LineWidth);
437 fprintf(out, "@ box color %d\n", LineColor);
438 fprintf(out, "@ box fill %s\n", BoxFillStr[BoxFill]);
439 fprintf(out, "@ box fill color %d\n", BoxColor);
440 fprintf(out, "@ box fill pattern %d\n", BoxPattern);
441 fprintf(out, "@box def\n");
445 /* reads a line into ptr, adjusting len and renewing ptr if neccesary */
446 static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
448 int len_remaining = *len; /* remaining amount of allocated bytes in buf */
449 int curp = 0; /* current position in buf to read into */
453 if (len_remaining < 2)
455 if (*len + STRLEN < maxlen)
457 /* This line is longer than len characters, let's increase len! */
459 len_remaining += STRLEN;
464 /*something is wrong, we'll just keep reading and return NULL*/
465 len_remaining = STRLEN;
469 if (fgets(*ptr + curp, len_remaining, fp) == NULL)
471 /* if last line, skip */
474 curp += len_remaining-1; /* overwrite the nul char in next iteration */
477 while ((strchr(*ptr, '\n') == NULL) && (!feof(fp)));
479 if (*len + STRLEN >= maxlen)
481 return NULL; /* this line was too long */
486 /* We reached EOF before '\n', skip this last line. */
490 /* now remove newline */
491 int slen = strlen(*ptr);
492 if ((*ptr)[slen-1] == '\n')
494 (*ptr)[slen-1] = '\0';
501 static int wordcount(char *ptr)
507 if (strlen(ptr) == 0)
511 /* fprintf(stderr,"ptr='%s'\n",ptr); */
513 for (i = 0; (ptr[i] != '\0'); i++)
515 is[cur] = isspace(ptr[i]);
516 if ((i > 0) && (is[cur] && !is[prev]))
525 static char *read_xvgr_string(const char *line)
527 const char *ptr0, *ptr1;
530 ptr0 = strchr(line, '"');
534 ptr1 = strchr(ptr0, '"');
538 str[ptr1-ptr0] = '\0';
553 int read_xvg_legend(const char *fn, double ***y, int *ny,
554 char **subtitle, char ***legend)
560 int k, line = 0, nny, nx, maxx, rval, legend_nalloc, set, nchar;
569 fp = gmx_fio_fopen(fn, "r");
572 if (subtitle != NULL)
582 while ((ptr = fgets3(fp, &tmpbuf, &len, 10*STRLEN)) != NULL && ptr[0] != '&')
593 if (strncmp(ptr, "subtitle", 8) == 0)
596 if (subtitle != NULL)
598 *subtitle = read_xvgr_string(ptr);
601 else if (strncmp(ptr, "legend string", 13) == 0)
604 sscanf(ptr, "%d%n", &set, &nchar);
607 else if (ptr[0] == 's')
610 sscanf(ptr, "%d%n", &set, &nchar);
613 if (strncmp(ptr, "legend", 6) == 0)
624 if (set >= legend_nalloc)
626 legend_nalloc = set + 1;
627 srenew(*legend, legend_nalloc);
628 (*legend)[set] = read_xvgr_string(ptr);
633 else if (ptr[0] != '#')
637 (*ny) = nny = wordcount(ptr);
638 /* fprintf(stderr,"There are %d columns in your file\n",nny);*/
647 /* Allocate column space */
651 for (k = 0; (k < nny); k++)
656 /* Initiate format string */
660 /* fprintf(stderr,"ptr='%s'\n",ptr);*/
661 for (k = 0; (k < nny); k++)
665 rval = sscanf(ptr, fmt, &lf);
666 /* fprintf(stderr,"rval = %d\n",rval);*/
667 if ((rval == EOF) || (rval == 0))
672 srenew(fmt, 3*(nny+1)+1);
673 srenew(base, 3*nny+1);
678 fprintf(stderr, "Only %d columns on line %d in file %s\n",
680 for (; (k < nny); k++)
693 if (legend_nalloc > 0)
695 if (*ny - 1 > legend_nalloc)
697 srenew(*legend, *ny-1);
698 for (set = legend_nalloc; set < *ny-1; set++)
700 (*legend)[set] = NULL;
708 int read_xvg(const char *fn, double ***y, int *ny)
710 return read_xvg_legend(fn, y, ny, NULL, NULL);
713 void write_xvg(const char *fn, const char *title, int nx, int ny, real **y,
714 const char **leg, const output_env_t oenv)
719 fp = xvgropen(fn, title, "X", "Y", oenv);
722 xvgr_legend(fp, ny-1, leg, oenv);
724 for (i = 0; (i < nx); i++)
726 for (j = 0; (j < ny); j++)
728 fprintf(fp, " %12.5e", y[j][i]);
735 real **read_xvg_time(const char *fn,
736 gmx_bool bHaveT, gmx_bool bTB, real tb, gmx_bool bTE, real te,
737 int nsets_in, int *nset, int *nval, real *dt, real **t)
740 #define MAXLINELEN 16384
741 char line0[MAXLINELEN];
743 int t_nalloc, *val_nalloc, a, narg, n, sin, set, nchar;
745 gmx_bool bEndOfSet, bTimeInRange, bFirstLine = TRUE;
754 fp = gmx_fio_fopen(fn, "r");
755 for (sin = 0; sin < nsets_in; sin++)
763 narg = bHaveT ? 2 : 1;
767 while (!bEndOfSet && fgets(line0, MAXLINELEN, fp))
770 /* Remove whitespace */
771 while (line[0] == ' ' || line[0] == '\t')
775 bEndOfSet = (line[0] == '&');
776 if (line[0] != '#' && line[0] != '@' && line[0] != '\n' && !bEndOfSet)
778 if (bFirstLine && bHaveT)
780 /* Check the first line that should contain data */
781 a = sscanf(line, "%lf%lf", &dbl, &dbl);
784 gmx_fatal(FARGS, "Expected a number in %s on line:\n%s", fn, line0);
788 fprintf(stderr, "Found only 1 number on line, "
789 "assuming no time is present.\n");
800 while ((a < narg || (nsets_in == 1 && n == 0)) &&
801 sscanf(line, "%lf%n", &dbl, &nchar) == 1 && bTimeInRange)
803 /* Use set=-1 as the time "set" */
806 if (!bHaveT || (a > 0))
826 if (set == -1 && ((bTB && dbl < tb) || (bTE && dbl > te)))
828 bTimeInRange = FALSE;
843 srenew(val_nalloc, *nset);
854 t_nalloc = over_alloc_small(n);
855 srenew(*t, t_nalloc);
859 /* else we should check the time of the next sets with set 0 */
863 if (n >= val_nalloc[set])
865 val_nalloc[set] = over_alloc_small(n);
866 srenew(val[set], val_nalloc[set]);
868 val[set][n] = (real)dbl;
874 if (line0[strlen(line0)-1] != '\n')
876 fprintf(stderr, "File %s does not end with a newline, ignoring the last line\n", fn);
878 else if (bTimeInRange)
882 fprintf(stderr, "Ignoring invalid line in %s:\n%s", fn, line0);
888 fprintf(stderr, "Invalid line in %s:\n%s"
889 "Using zeros for the last %d sets\n",
907 for (a = 0; a < n; a++)
914 *dt = (real)((*t)[n-1]-(*t)[0])/(n-1.0);
925 fprintf(stderr, "Set %d is shorter (%d) than the previous set (%d)\n",
928 fprintf(stderr, "Will use only the first %d points of every set\n",