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 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
63 xvg_format = output_env_get_xvg_format(oenv);
65 return (xvg_format == exvgXMGRACE || xvg_format == exvgXMGR);
68 static char *xvgrstr(const char *gmx, const output_env_t oenv,
69 char *buf, int buflen)
71 /* Supported greek letter names and corresponding xmgrace/xmgr symbols */
72 const char *sym[] = { "beta", "chi", "delta", "eta", "lambda", "mu", "omega", "phi", "psi", "rho", "theta", NULL };
73 const char symc[] = { 'b', 'c', 'd', 'h', 'l', 'm', 'w', 'f', 'y', 'r', 'q', '\0' };
79 xvgf = output_env_get_xvg_format(oenv);
80 bXVGR = (xvgf == exvgXMGRACE || xvgf == exvgXMGR);
84 while (gmx[g] != '\0')
86 /* Check with the largest string we have ("lambda"), add one for \0 */
87 if (b + 6 + 1 >= buflen)
89 gmx_fatal(FARGS, "Output buffer length in xvgstr (%d) too small to process xvg input string '%s'", buflen, gmx);
108 else if (gmx[g] == 'S')
122 else if (gmx[g] == 'N')
124 /* End sub/superscript */
139 else if (gmx[g] == '4')
141 /* Backward compatibility for xmgr normal font "\4" */
145 sprintf(buf+b, "%s", "\\f{}");
148 sprintf(buf+b, "%s", "\\4");
157 else if (gmx[g] == '8')
159 /* Backward compatibility for xmgr symbol font "\8" */
163 sprintf(buf+b, "%s", "\\x");
166 sprintf(buf+b, "%s", "\\8");
177 /* Check for special symbol */
179 while (sym[i] != NULL &&
180 gmx_strncasecmp(sym[i], gmx+g, strlen(sym[i])) != 0)
194 sprintf(buf+b, "%s%c%s", "\\x", c, "\\f{}");
197 sprintf(buf+b, "%s%c%s", "\\8", c, "\\4");
200 strncat(buf+b, gmx+g, strlen(sym[i]));
202 if (gmx[g+strlen(sym[i])] != ' ')
214 /* Unknown escape sequence, this should not happen.
215 * We do not generate a fatal error, since that might
216 * stop otherwise functioning code from working.
217 * Copy the backslash to the output and continue processing.
234 void xvgr_header(FILE *fp, const char *title, const char *xaxis,
235 const char *yaxis, int exvg_graph_type,
236 const output_env_t oenv)
238 char pukestr[100], buf[STRLEN];
241 if (output_env_get_print_xvgr_codes(oenv))
244 gmx_ctime_r(&t, buf, STRLEN);
245 fprintf(fp, "# This file was created %s", buf);
246 fprintf(fp, "# by the following command:\n# %s\n#\n", command_line());
247 fprintf(fp, "# %s is part of G R O M A C S:\n#\n", ShortProgram());
248 bromacs(pukestr, 99);
249 fprintf(fp, "# %s\n#\n", pukestr);
250 fprintf(fp, "@ title \"%s\"\n", xvgrstr(title, oenv, buf, STRLEN));
251 fprintf(fp, "@ xaxis label \"%s\"\n",
252 xvgrstr(xaxis, oenv, buf, STRLEN));
253 fprintf(fp, "@ yaxis label \"%s\"\n",
254 xvgrstr(yaxis, oenv, buf, STRLEN));
255 switch (exvg_graph_type)
258 if (output_env_get_xvg_format(oenv) == exvgXMGR)
260 fprintf(fp, "@TYPE nxy\n");
264 fprintf(fp, "@TYPE xy\n");
268 fprintf(fp, "@TYPE xydy\n");
271 fprintf(fp, "@TYPE xydydy\n");
277 FILE *xvgropen_type(const char *fn, const char *title, const char *xaxis,
278 const char *yaxis, int exvg_graph_type,
279 const output_env_t oenv)
283 fp = gmx_fio_fopen(fn, "w");
285 xvgr_header(fp, title, xaxis, yaxis, exvg_graph_type, oenv);
290 FILE *xvgropen(const char *fn, const char *title, const char *xaxis,
291 const char *yaxis, const output_env_t oenv)
293 return xvgropen_type(fn, title, xaxis, yaxis, exvggtXNY, oenv);
302 void xvgr_subtitle(FILE *out, const char *subtitle, const output_env_t oenv)
306 if (output_env_get_print_xvgr_codes(oenv))
308 fprintf(out, "@ subtitle \"%s\"\n", xvgrstr(subtitle, oenv, buf, STRLEN));
312 void xvgr_view(FILE *out, real xmin, real ymin, real xmax, real ymax,
313 const output_env_t oenv)
315 if (output_env_get_print_xvgr_codes(oenv))
317 fprintf(out, "@ view %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
321 void xvgr_world(FILE *out, real xmin, real ymin, real xmax, real ymax,
322 const output_env_t oenv)
324 if (output_env_get_print_xvgr_codes(oenv))
326 fprintf(out, "@ world xmin %g\n"
329 "@ world ymax %g\n", xmin, ymin, xmax, ymax);
333 void xvgr_legend(FILE *out, int nsets, const char** setname,
334 const output_env_t oenv)
339 if (output_env_get_print_xvgr_codes(oenv))
341 xvgr_view(out, 0.15, 0.15, 0.75, 0.85, oenv);
342 fprintf(out, "@ legend on\n");
343 fprintf(out, "@ legend box on\n");
344 fprintf(out, "@ legend loctype view\n");
345 fprintf(out, "@ legend %g, %g\n", 0.78, 0.8);
346 fprintf(out, "@ legend length %d\n", 2);
347 for (i = 0; (i < nsets); i++)
351 if (output_env_get_xvg_format(oenv) == exvgXMGR)
353 fprintf(out, "@ legend string %d \"%s\"\n",
354 i, xvgrstr(setname[i], oenv, buf, STRLEN));
358 fprintf(out, "@ s%d legend \"%s\"\n",
359 i, xvgrstr(setname[i], oenv, buf, STRLEN));
366 void xvgr_new_dataset(FILE *out, int nr_first, int nsets,
367 const char **setname,
368 const output_env_t oenv)
373 if (output_env_get_print_xvgr_codes(oenv))
376 for (i = 0; (i < nsets); i++)
380 if (output_env_get_xvg_format(oenv) == exvgXMGR)
382 fprintf(out, "@ legend string %d \"%s\"\n",
383 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
387 fprintf(out, "@ s%d legend \"%s\"\n",
388 i+nr_first, xvgrstr(setname[i], oenv, buf, STRLEN));
399 void xvgr_line_props(FILE *out, int NrSet, int LineStyle, int LineColor,
400 const output_env_t oenv)
402 if (output_env_get_print_xvgr_codes(oenv))
404 fprintf(out, "@ with g0\n");
405 fprintf(out, "@ s%d linestyle %d\n", NrSet, LineStyle);
406 fprintf(out, "@ s%d color %d\n", NrSet, LineColor);
410 static const char *LocTypeStr[] = { "view", "world" };
411 static const char *BoxFillStr[] = { "none", "color", "pattern" };
413 void xvgr_box(FILE *out,
415 real xmin, real ymin, real xmax, real ymax,
416 int LineStyle, int LineWidth, int LineColor,
417 int BoxFill, int BoxColor, int BoxPattern, const output_env_t oenv)
419 if (output_env_get_print_xvgr_codes(oenv))
421 fprintf(out, "@with box\n");
422 fprintf(out, "@ box on\n");
423 fprintf(out, "@ box loctype %s\n", LocTypeStr[LocType]);
424 fprintf(out, "@ box %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
425 fprintf(out, "@ box linestyle %d\n", LineStyle);
426 fprintf(out, "@ box linewidth %d\n", LineWidth);
427 fprintf(out, "@ box color %d\n", LineColor);
428 fprintf(out, "@ box fill %s\n", BoxFillStr[BoxFill]);
429 fprintf(out, "@ box fill color %d\n", BoxColor);
430 fprintf(out, "@ box fill pattern %d\n", BoxPattern);
431 fprintf(out, "@box def\n");
435 /* reads a line into ptr, adjusting len and renewing ptr if neccesary */
436 static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
438 int len_remaining = *len; /* remaining amount of allocated bytes in buf */
439 int curp = 0; /* current position in buf to read into */
443 if (len_remaining < 2)
445 if (*len + STRLEN < maxlen)
447 /* This line is longer than len characters, let's increase len! */
449 len_remaining += STRLEN;
454 /*something is wrong, we'll just keep reading and return NULL*/
455 len_remaining = STRLEN;
459 if (fgets(*ptr + curp, len_remaining, fp) == NULL)
461 /* if last line, skip */
464 curp += len_remaining-1; /* overwrite the nul char in next iteration */
467 while ((strchr(*ptr, '\n') == NULL) && (!feof(fp)));
469 if (*len + STRLEN >= maxlen)
471 return NULL; /* this line was too long */
476 /* We reached EOF before '\n', skip this last line. */
480 /* now remove newline */
481 int slen = strlen(*ptr);
482 if ((*ptr)[slen-1] == '\n')
484 (*ptr)[slen-1] = '\0';
491 static int wordcount(char *ptr)
497 if (strlen(ptr) == 0)
501 /* fprintf(stderr,"ptr='%s'\n",ptr); */
503 for (i = 0; (ptr[i] != '\0'); i++)
505 is[cur] = isspace(ptr[i]);
506 if ((i > 0) && (is[cur] && !is[prev]))
515 static char *read_xvgr_string(const char *line)
517 const char *ptr0, *ptr1;
520 ptr0 = strchr(line, '"');
524 ptr1 = strchr(ptr0, '"');
528 str[ptr1-ptr0] = '\0';
543 int read_xvg_legend(const char *fn, double ***y, int *ny,
544 char **subtitle, char ***legend)
550 int k, line = 0, nny, nx, maxx, rval, legend_nalloc, set, nchar;
559 fp = gmx_fio_fopen(fn, "r");
562 if (subtitle != NULL)
572 while ((ptr = fgets3(fp, &tmpbuf, &len, 10*STRLEN)) != NULL && ptr[0] != '&')
583 if (strncmp(ptr, "subtitle", 8) == 0)
586 if (subtitle != NULL)
588 *subtitle = read_xvgr_string(ptr);
591 else if (strncmp(ptr, "legend string", 13) == 0)
594 sscanf(ptr, "%d%n", &set, &nchar);
597 else if (ptr[0] == 's')
600 sscanf(ptr, "%d%n", &set, &nchar);
603 if (strncmp(ptr, "legend", 6) == 0)
614 if (set >= legend_nalloc)
616 legend_nalloc = set + 1;
617 srenew(*legend, legend_nalloc);
618 (*legend)[set] = read_xvgr_string(ptr);
623 else if (ptr[0] != '#')
627 (*ny) = nny = wordcount(ptr);
628 /* fprintf(stderr,"There are %d columns in your file\n",nny);*/
637 /* Allocate column space */
641 for (k = 0; (k < nny); k++)
646 /* Initiate format string */
650 /* fprintf(stderr,"ptr='%s'\n",ptr);*/
651 for (k = 0; (k < nny); k++)
655 rval = sscanf(ptr, fmt, &lf);
656 /* fprintf(stderr,"rval = %d\n",rval);*/
657 if ((rval == EOF) || (rval == 0))
662 srenew(fmt, 3*(nny+1)+1);
663 srenew(base, 3*nny+1);
668 fprintf(stderr, "Only %d columns on line %d in file %s\n",
670 for (; (k < nny); k++)
683 if (legend_nalloc > 0)
685 if (*ny - 1 > legend_nalloc)
687 srenew(*legend, *ny-1);
688 for (set = legend_nalloc; set < *ny-1; set++)
690 (*legend)[set] = NULL;
698 int read_xvg(const char *fn, double ***y, int *ny)
700 return read_xvg_legend(fn, y, ny, NULL, NULL);
703 void write_xvg(const char *fn, const char *title, int nx, int ny, real **y,
704 const char **leg, const output_env_t oenv)
709 fp = xvgropen(fn, title, "X", "Y", oenv);
712 xvgr_legend(fp, ny-1, leg, oenv);
714 for (i = 0; (i < nx); i++)
716 for (j = 0; (j < ny); j++)
718 fprintf(fp, " %12.5e", y[j][i]);
725 real **read_xvg_time(const char *fn,
726 gmx_bool bHaveT, gmx_bool bTB, real tb, gmx_bool bTE, real te,
727 int nsets_in, int *nset, int *nval, real *dt, real **t)
730 #define MAXLINELEN 16384
731 char line0[MAXLINELEN];
733 int t_nalloc, *val_nalloc, a, narg, n, sin, set, nchar;
735 gmx_bool bEndOfSet, bTimeInRange, bFirstLine = TRUE;
744 fp = gmx_fio_fopen(fn, "r");
745 for (sin = 0; sin < nsets_in; sin++)
753 narg = bHaveT ? 2 : 1;
757 while (!bEndOfSet && fgets(line0, MAXLINELEN, fp))
760 /* Remove whitespace */
761 while (line[0] == ' ' || line[0] == '\t')
765 bEndOfSet = (line[0] == '&');
766 if (line[0] != '#' && line[0] != '@' && line[0] != '\n' && !bEndOfSet)
768 if (bFirstLine && bHaveT)
770 /* Check the first line that should contain data */
771 a = sscanf(line, "%lf%lf", &dbl, &dbl);
774 gmx_fatal(FARGS, "Expected a number in %s on line:\n%s", fn, line0);
778 fprintf(stderr, "Found only 1 number on line, "
779 "assuming no time is present.\n");
790 while ((a < narg || (nsets_in == 1 && n == 0)) &&
791 sscanf(line, "%lf%n", &dbl, &nchar) == 1 && bTimeInRange)
793 /* Use set=-1 as the time "set" */
796 if (!bHaveT || (a > 0))
816 if (set == -1 && ((bTB && dbl < tb) || (bTE && dbl > te)))
818 bTimeInRange = FALSE;
833 srenew(val_nalloc, *nset);
844 t_nalloc = over_alloc_small(n);
845 srenew(*t, t_nalloc);
849 /* else we should check the time of the next sets with set 0 */
853 if (n >= val_nalloc[set])
855 val_nalloc[set] = over_alloc_small(n);
856 srenew(val[set], val_nalloc[set]);
858 val[set][n] = (real)dbl;
864 if (line0[strlen(line0)-1] != '\n')
866 fprintf(stderr, "File %s does not end with a newline, ignoring the last line\n", fn);
868 else if (bTimeInRange)
872 fprintf(stderr, "Ignoring invalid line in %s:\n%s", fn, line0);
878 fprintf(stderr, "Invalid line in %s:\n%s"
879 "Using zeros for the last %d sets\n",
897 for (a = 0; a < n; a++)
904 *dt = (real)((*t)[n-1]-(*t)[0])/(n-1.0);
915 fprintf(stderr, "Set %d is shorter (%d) than the previous set (%d)\n",
918 fprintf(stderr, "Will use only the first %d points of every set\n",