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.
47 #ifdef HAVE_SYS_TIME_H
51 #include "gromacs/fileio/gmxfio.h"
52 #include "gromacs/legacyheaders/copyrite.h"
53 #include "gromacs/legacyheaders/oenv.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "gromacs/utility/futil.h"
59 #include "gromacs/utility/smalloc.h"
61 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
65 xvg_format = output_env_get_xvg_format(oenv);
67 return (xvg_format == exvgXMGRACE || xvg_format == exvgXMGR);
70 static char *xvgrstr(const char *gmx, const output_env_t oenv,
71 char *buf, int buflen)
73 /* Supported greek letter names and corresponding xmgrace/xmgr symbols */
74 const char *sym[] = { "beta", "chi", "delta", "eta", "lambda", "mu", "omega", "phi", "psi", "rho", "theta", NULL };
75 const char symc[] = { 'b', 'c', 'd', 'h', 'l', 'm', 'w', 'f', 'y', 'r', 'q', '\0' };
81 xvgf = output_env_get_xvg_format(oenv);
82 bXVGR = (xvgf == exvgXMGRACE || xvgf == exvgXMGR);
86 while (gmx[g] != '\0')
88 /* Check with the largest string we have ("lambda"), add one for \0 */
89 if (b + 6 + 1 >= buflen)
91 gmx_fatal(FARGS, "Output buffer length in xvgstr (%d) too small to process xvg input string '%s'", buflen, gmx);
110 else if (gmx[g] == 'S')
124 else if (gmx[g] == 'N')
126 /* End sub/superscript */
141 else if (gmx[g] == '4')
143 /* Backward compatibility for xmgr normal font "\4" */
147 sprintf(buf+b, "%s", "\\f{}");
150 sprintf(buf+b, "%s", "\\4");
159 else if (gmx[g] == '8')
161 /* Backward compatibility for xmgr symbol font "\8" */
165 sprintf(buf+b, "%s", "\\x");
168 sprintf(buf+b, "%s", "\\8");
179 /* Check for special symbol */
181 while (sym[i] != NULL &&
182 gmx_strncasecmp(sym[i], gmx+g, strlen(sym[i])) != 0)
196 sprintf(buf+b, "%s%c%s", "\\x", c, "\\f{}");
199 sprintf(buf+b, "%s%c%s", "\\8", c, "\\4");
202 strncat(buf+b, gmx+g, strlen(sym[i]));
204 if (gmx[g+strlen(sym[i])] != ' ')
216 /* Unknown escape sequence, this should not happen.
217 * We do not generate a fatal error, since that might
218 * stop otherwise functioning code from working.
219 * Copy the backslash to the output and continue processing.
236 void xvgr_header(FILE *fp, const char *title, const char *xaxis,
237 const char *yaxis, int exvg_graph_type,
238 const output_env_t oenv)
240 char pukestr[100], buf[STRLEN];
243 if (output_env_get_print_xvgr_codes(oenv))
246 gmx_ctime_r(&t, buf, STRLEN);
247 fprintf(fp, "# This file was created %s", buf);
250 gmx::BinaryInformationSettings settings;
251 settings.generatedByHeader(true);
252 settings.linePrefix("# ");
253 gmx::printBinaryInformation(fp, output_env_get_program_context(oenv),
256 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
257 fprintf(fp, "# %s is part of G R O M A C S:\n#\n",
258 output_env_get_program_display_name(oenv));
259 bromacs(pukestr, 99);
260 fprintf(fp, "# %s\n#\n", pukestr);
261 fprintf(fp, "@ title \"%s\"\n", xvgrstr(title, oenv, buf, STRLEN));
262 fprintf(fp, "@ xaxis label \"%s\"\n",
263 xvgrstr(xaxis, oenv, buf, STRLEN));
264 fprintf(fp, "@ yaxis label \"%s\"\n",
265 xvgrstr(yaxis, oenv, buf, STRLEN));
266 switch (exvg_graph_type)
269 if (output_env_get_xvg_format(oenv) == exvgXMGR)
271 fprintf(fp, "@TYPE nxy\n");
275 fprintf(fp, "@TYPE xy\n");
279 fprintf(fp, "@TYPE xydy\n");
282 fprintf(fp, "@TYPE xydydy\n");
288 FILE *xvgropen_type(const char *fn, const char *title, const char *xaxis,
289 const char *yaxis, int exvg_graph_type,
290 const output_env_t oenv)
294 fp = gmx_fio_fopen(fn, "w");
296 xvgr_header(fp, title, xaxis, yaxis, exvg_graph_type, oenv);
301 FILE *xvgropen(const char *fn, const char *title, const char *xaxis,
302 const char *yaxis, const output_env_t oenv)
304 return xvgropen_type(fn, title, xaxis, yaxis, exvggtXNY, oenv);
313 void xvgr_subtitle(FILE *out, const char *subtitle, const output_env_t oenv)
317 if (output_env_get_print_xvgr_codes(oenv))
319 fprintf(out, "@ subtitle \"%s\"\n", xvgrstr(subtitle, oenv, buf, STRLEN));
323 void xvgr_view(FILE *out, real xmin, real ymin, real xmax, real ymax,
324 const output_env_t oenv)
326 if (output_env_get_print_xvgr_codes(oenv))
328 fprintf(out, "@ view %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
332 void xvgr_world(FILE *out, real xmin, real ymin, real xmax, real ymax,
333 const output_env_t oenv)
335 if (output_env_get_print_xvgr_codes(oenv))
337 fprintf(out, "@ world xmin %g\n"
340 "@ world ymax %g\n", xmin, ymin, xmax, ymax);
344 void xvgr_legend(FILE *out, int nsets, const char** setname,
345 const output_env_t oenv)
350 if (output_env_get_print_xvgr_codes(oenv))
352 xvgr_view(out, 0.15, 0.15, 0.75, 0.85, oenv);
353 fprintf(out, "@ legend on\n");
354 fprintf(out, "@ legend box on\n");
355 fprintf(out, "@ legend loctype view\n");
356 fprintf(out, "@ legend %g, %g\n", 0.78, 0.8);
357 fprintf(out, "@ legend length %d\n", 2);
358 for (i = 0; (i < nsets); i++)
362 if (output_env_get_xvg_format(oenv) == exvgXMGR)
364 fprintf(out, "@ legend string %d \"%s\"\n",
365 i, xvgrstr(setname[i], oenv, buf, STRLEN));
369 fprintf(out, "@ s%d legend \"%s\"\n",
370 i, xvgrstr(setname[i], oenv, buf, STRLEN));
377 void xvgr_new_dataset(FILE *out, int nr_first, int nsets,
378 const char **setname,
379 const output_env_t oenv)
384 if (output_env_get_print_xvgr_codes(oenv))
387 for (i = 0; (i < nsets); i++)
391 if (output_env_get_xvg_format(oenv) == exvgXMGR)
393 fprintf(out, "@ legend string %d \"%s\"\n",
394 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
398 fprintf(out, "@ s%d legend \"%s\"\n",
399 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
410 void xvgr_line_props(FILE *out, int NrSet, int LineStyle, int LineColor,
411 const output_env_t oenv)
413 if (output_env_get_print_xvgr_codes(oenv))
415 fprintf(out, "@ with g0\n");
416 fprintf(out, "@ s%d linestyle %d\n", NrSet, LineStyle);
417 fprintf(out, "@ s%d color %d\n", NrSet, LineColor);
421 static const char *LocTypeStr[] = { "view", "world" };
422 static const char *BoxFillStr[] = { "none", "color", "pattern" };
424 void xvgr_box(FILE *out,
426 real xmin, real ymin, real xmax, real ymax,
427 int LineStyle, int LineWidth, int LineColor,
428 int BoxFill, int BoxColor, int BoxPattern, const output_env_t oenv)
430 if (output_env_get_print_xvgr_codes(oenv))
432 fprintf(out, "@with box\n");
433 fprintf(out, "@ box on\n");
434 fprintf(out, "@ box loctype %s\n", LocTypeStr[LocType]);
435 fprintf(out, "@ box %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
436 fprintf(out, "@ box linestyle %d\n", LineStyle);
437 fprintf(out, "@ box linewidth %d\n", LineWidth);
438 fprintf(out, "@ box color %d\n", LineColor);
439 fprintf(out, "@ box fill %s\n", BoxFillStr[BoxFill]);
440 fprintf(out, "@ box fill color %d\n", BoxColor);
441 fprintf(out, "@ box fill pattern %d\n", BoxPattern);
442 fprintf(out, "@box def\n");
446 /* reads a line into ptr, adjusting len and renewing ptr if neccesary */
447 static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
449 int len_remaining = *len; /* remaining amount of allocated bytes in buf */
450 int curp = 0; /* current position in buf to read into */
454 if (len_remaining < 2)
456 if (*len + STRLEN < maxlen)
458 /* This line is longer than len characters, let's increase len! */
460 len_remaining += STRLEN;
465 /*something is wrong, we'll just keep reading and return NULL*/
466 len_remaining = STRLEN;
470 if (fgets(*ptr + curp, len_remaining, fp) == NULL)
472 /* if last line, skip */
475 curp += len_remaining-1; /* overwrite the nul char in next iteration */
478 while ((strchr(*ptr, '\n') == NULL) && (!feof(fp)));
480 if (*len + STRLEN >= maxlen)
482 return NULL; /* this line was too long */
487 /* We reached EOF before '\n', skip this last line. */
491 /* now remove newline */
492 int slen = strlen(*ptr);
493 if ((*ptr)[slen-1] == '\n')
495 (*ptr)[slen-1] = '\0';
502 static int wordcount(char *ptr)
508 if (strlen(ptr) == 0)
512 /* fprintf(stderr,"ptr='%s'\n",ptr); */
514 for (i = 0; (ptr[i] != '\0'); i++)
516 is[cur] = isspace(ptr[i]);
517 if ((i > 0) && (is[cur] && !is[prev]))
526 static char *read_xvgr_string(const char *line)
528 const char *ptr0, *ptr1;
531 ptr0 = strchr(line, '"');
535 ptr1 = strchr(ptr0, '"');
538 str = gmx_strdup(ptr0);
539 str[ptr1-ptr0] = '\0';
543 str = gmx_strdup("");
548 str = gmx_strdup("");
554 int read_xvg_legend(const char *fn, double ***y, int *ny,
555 char **subtitle, char ***legend)
561 int k, line = 0, nny, nx, maxx, rval, legend_nalloc, set, nchar;
570 fp = gmx_fio_fopen(fn, "r");
573 if (subtitle != NULL)
583 while ((ptr = fgets3(fp, &tmpbuf, &len, 10*STRLEN)) != NULL && ptr[0] != '&')
594 if (strncmp(ptr, "subtitle", 8) == 0)
597 if (subtitle != NULL)
599 *subtitle = read_xvgr_string(ptr);
602 else if (strncmp(ptr, "legend string", 13) == 0)
605 sscanf(ptr, "%d%n", &set, &nchar);
608 else if (ptr[0] == 's')
611 sscanf(ptr, "%d%n", &set, &nchar);
614 if (strncmp(ptr, "legend", 6) == 0)
625 if (set >= legend_nalloc)
627 legend_nalloc = set + 1;
628 srenew(*legend, legend_nalloc);
629 (*legend)[set] = read_xvgr_string(ptr);
634 else if (ptr[0] != '#')
638 (*ny) = nny = wordcount(ptr);
639 /* fprintf(stderr,"There are %d columns in your file\n",nny);*/
648 /* Allocate column space */
652 for (k = 0; (k < nny); k++)
657 /* Initiate format string */
661 /* fprintf(stderr,"ptr='%s'\n",ptr);*/
662 for (k = 0; (k < nny); k++)
666 rval = sscanf(ptr, fmt, &lf);
667 /* fprintf(stderr,"rval = %d\n",rval);*/
668 if ((rval == EOF) || (rval == 0))
673 srenew(fmt, 3*(nny+1)+1);
674 srenew(base, 3*nny+1);
679 fprintf(stderr, "Only %d columns on line %d in file %s\n",
681 for (; (k < nny); k++)
694 if (legend_nalloc > 0)
696 if (*ny - 1 > legend_nalloc)
698 srenew(*legend, *ny-1);
699 for (set = legend_nalloc; set < *ny-1; set++)
701 (*legend)[set] = NULL;
709 int read_xvg(const char *fn, double ***y, int *ny)
711 return read_xvg_legend(fn, y, ny, NULL, NULL);
714 void write_xvg(const char *fn, const char *title, int nx, int ny, real **y,
715 const char **leg, const output_env_t oenv)
720 fp = xvgropen(fn, title, "X", "Y", oenv);
723 xvgr_legend(fp, ny-1, leg, oenv);
725 for (i = 0; (i < nx); i++)
727 for (j = 0; (j < ny); j++)
729 fprintf(fp, " %12.5e", y[j][i]);
736 real **read_xvg_time(const char *fn,
737 gmx_bool bHaveT, gmx_bool bTB, real tb, gmx_bool bTE, real te,
738 int nsets_in, int *nset, int *nval, real *dt, real **t)
741 #define MAXLINELEN 16384
742 char line0[MAXLINELEN];
744 int t_nalloc, *val_nalloc, a, narg, n, sin, set, nchar;
746 gmx_bool bEndOfSet, bTimeInRange, bFirstLine = TRUE;
755 fp = gmx_fio_fopen(fn, "r");
756 for (sin = 0; sin < nsets_in; sin++)
764 narg = bHaveT ? 2 : 1;
768 while (!bEndOfSet && fgets(line0, MAXLINELEN, fp))
771 /* Remove whitespace */
772 while (line[0] == ' ' || line[0] == '\t')
776 bEndOfSet = (line[0] == '&');
777 if (line[0] != '#' && line[0] != '@' && line[0] != '\n' && !bEndOfSet)
779 if (bFirstLine && bHaveT)
781 /* Check the first line that should contain data */
782 a = sscanf(line, "%lf%lf", &dbl, &dbl);
785 gmx_fatal(FARGS, "Expected a number in %s on line:\n%s", fn, line0);
789 fprintf(stderr, "Found only 1 number on line, "
790 "assuming no time is present.\n");
801 while ((a < narg || (nsets_in == 1 && n == 0)) &&
802 sscanf(line, "%lf%n", &dbl, &nchar) == 1 && bTimeInRange)
804 /* Use set=-1 as the time "set" */
807 if (!bHaveT || (a > 0))
827 if (set == -1 && ((bTB && dbl < tb) || (bTE && dbl > te)))
829 bTimeInRange = FALSE;
844 srenew(val_nalloc, *nset);
855 t_nalloc = over_alloc_small(n);
856 srenew(*t, t_nalloc);
860 /* else we should check the time of the next sets with set 0 */
864 if (n >= val_nalloc[set])
866 val_nalloc[set] = over_alloc_small(n);
867 srenew(val[set], val_nalloc[set]);
869 val[set][n] = (real)dbl;
875 if (line0[strlen(line0)-1] != '\n')
877 fprintf(stderr, "File %s does not end with a newline, ignoring the last line\n", fn);
879 else if (bTimeInRange)
883 fprintf(stderr, "Ignoring invalid line in %s:\n%s", fn, line0);
889 fprintf(stderr, "Invalid line in %s:\n%s"
890 "Using zeros for the last %d sets\n",
908 for (a = 0; a < n; a++)
915 *dt = (real)((*t)[n-1]-(*t)[0])/(n-1.0);
926 fprintf(stderr, "Set %d is shorter (%d) than the previous set (%d)\n",
929 fprintf(stderr, "Will use only the first %d points of every set\n",