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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
46 #ifdef HAVE_SYS_TIME_H
61 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
63 gmx_ctime_r(const time_t *clock,char *buf, int n);
66 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
70 xvg_format = output_env_get_xvg_format(oenv);
72 return (xvg_format == exvgXMGRACE || xvg_format == exvgXMGR);
75 static char *xvgrstr(const char *gmx,const output_env_t oenv,
78 /* Supported greek letter names and corresponding xmgrace/xmgr symbols */
79 const char *sym[]={ "beta", "chi", "delta", "eta", "lambda", "mu", "omega", "phi", "psi", "rho", "theta", NULL };
80 const char symc[]={ 'b', 'c', 'd', 'h', 'l', 'm', 'w', 'f', 'y', 'r', 'q', '\0' };
86 xvgf = output_env_get_xvg_format(oenv);
87 bXVGR = (xvgf == exvgXMGRACE || xvgf == exvgXMGR);
91 while (gmx[g] != '\0')
93 /* Check with the largest string we have ("lambda"), add one for \0 */
94 if (b + 6 + 1 >= buflen)
96 gmx_fatal(FARGS,"Output buffer length in xvgstr (%d) too small to process xvg input string '%s'",buflen,gmx);
115 else if (gmx[g] == 'S')
129 else if (gmx[g] == 'N')
131 /* End sub/superscript */
146 else if (gmx[g] == '4')
148 /* Backward compatibility for xmgr normal font "\4" */
152 sprintf(buf+b,"%s","\\f{}");
155 sprintf(buf+b,"%s","\\4");
164 else if (gmx[g] == '8')
166 /* Backward compatibility for xmgr symbol font "\8" */
170 sprintf(buf+b,"%s","\\x");
173 sprintf(buf+b,"%s","\\8");
184 /* Check for special symbol */
186 while (sym[i] != NULL &&
187 gmx_strncasecmp(sym[i],gmx+g,strlen(sym[i])) != 0)
201 sprintf(buf+b,"%s%c%s","\\x",c,"\\f{}");
204 sprintf(buf+b,"%s%c%s","\\8",c,"\\4");
207 strncat(buf+b,gmx+g,strlen(sym[i]));
209 if (gmx[g+strlen(sym[i])] != ' ')
221 /* Unknown escape sequence, this should not happen.
222 * We do not generate a fatal error, since that might
223 * stop otherwise functioning code from working.
224 * Copy the backslash to the output and continue processing.
241 void xvgr_header(FILE *fp,const char *title,const char *xaxis,
242 const char *yaxis,int exvg_graph_type,
243 const output_env_t oenv)
245 char pukestr[100],buf[STRLEN];
248 if (output_env_get_print_xvgr_codes(oenv))
251 gmx_ctime_r(&t,buf,STRLEN);
252 fprintf(fp,"# This file was created %s",buf);
253 fprintf(fp,"# by the following command:\n# %s\n#\n",command_line());
254 fprintf(fp,"# %s is part of G R O M A C S:\n#\n",ShortProgram());
256 fprintf(fp,"# %s\n#\n",pukestr);
257 fprintf(fp,"@ title \"%s\"\n",xvgrstr(title,oenv,buf,STRLEN));
258 fprintf(fp,"@ xaxis label \"%s\"\n",
259 xvgrstr(xaxis,oenv,buf,STRLEN));
260 fprintf(fp,"@ yaxis label \"%s\"\n",
261 xvgrstr(yaxis,oenv,buf,STRLEN));
262 switch (exvg_graph_type)
265 if (output_env_get_xvg_format(oenv) == exvgXMGR)
267 fprintf(fp,"@TYPE nxy\n");
271 fprintf(fp,"@TYPE xy\n");
275 fprintf(fp,"@TYPE xydy\n");
278 fprintf(fp,"@TYPE xydydy\n");
284 FILE *xvgropen_type(const char *fn,const char *title,const char *xaxis,
285 const char *yaxis,int exvg_graph_type,
286 const output_env_t oenv)
291 fp = gmx_fio_fopen(fn,"w");
293 xvgr_header(fp,title,xaxis,yaxis,exvg_graph_type,oenv);
298 FILE *xvgropen(const char *fn,const char *title,const char *xaxis,
299 const char *yaxis,const output_env_t oenv)
301 return xvgropen_type(fn,title,xaxis,yaxis,exvggtXNY,oenv);
310 void xvgr_subtitle(FILE *out,const char *subtitle,const output_env_t oenv)
314 if (output_env_get_print_xvgr_codes(oenv))
316 fprintf(out,"@ subtitle \"%s\"\n",xvgrstr(subtitle,oenv,buf,STRLEN));
320 void xvgr_view(FILE *out,real xmin,real ymin,real xmax,real ymax,
321 const output_env_t oenv)
323 if (output_env_get_print_xvgr_codes(oenv))
325 fprintf(out,"@ view %g, %g, %g, %g\n",xmin,ymin,xmax,ymax);
329 void xvgr_world(FILE *out,real xmin,real ymin,real xmax,real ymax,
330 const output_env_t oenv)
332 if (output_env_get_print_xvgr_codes(oenv))
334 fprintf(out,"@ world xmin %g\n"
337 "@ world ymax %g\n",xmin,ymin,xmax,ymax);
341 void xvgr_legend(FILE *out,int nsets,const char** setname,
342 const output_env_t oenv)
347 if (output_env_get_print_xvgr_codes(oenv))
349 xvgr_view(out,0.15,0.15,0.75,0.85,oenv);
350 fprintf(out,"@ legend on\n");
351 fprintf(out,"@ legend box on\n");
352 fprintf(out,"@ legend loctype view\n");
353 fprintf(out,"@ legend %g, %g\n",0.78,0.8);
354 fprintf(out,"@ legend length %d\n",2);
355 for(i=0; (i<nsets); i++)
358 if (output_env_get_xvg_format(oenv) == exvgXMGR)
360 fprintf(out,"@ legend string %d \"%s\"\n",
361 i,xvgrstr(setname[i],oenv,buf,STRLEN));
365 fprintf(out,"@ s%d legend \"%s\"\n",
366 i,xvgrstr(setname[i],oenv,buf,STRLEN));
373 void xvgr_new_dataset(FILE *out, int nr_first, int nsets,
374 const char **setname,
375 const output_env_t oenv)
380 if (output_env_get_print_xvgr_codes(oenv))
383 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)
506 /* fprintf(stderr,"ptr='%s'\n",ptr); */
508 for(i=0; (ptr[i] != '\0'); i++) {
509 is[cur] = isspace(ptr[i]);
510 if ((i > 0) && (is[cur] && !is[prev]))
517 static char *read_xvgr_string(const char *line)
519 const char *ptr0,*ptr1;
522 ptr0 = strchr(line,'"');
525 ptr1 = strchr(ptr0,'"');
529 str[ptr1-ptr0] = '\0';
544 int read_xvg_legend(const char *fn,double ***y,int *ny,
545 char **subtitle,char ***legend)
548 char *ptr,*ptr0,*ptr1;
551 int k,line=0,nny,nx,maxx,rval,legend_nalloc,set,nchar;
560 fp = gmx_fio_fopen(fn,"r");
563 if (subtitle != NULL) {
567 if (legend != NULL) {
571 while ((ptr = fgets3(fp,&tmpbuf,&len,10*STRLEN)) != NULL && ptr[0]!='&') {
575 if (legend != NULL) {
579 if (strncmp(ptr,"subtitle",8) == 0) {
581 if (subtitle != NULL)
583 *subtitle = read_xvgr_string(ptr);
585 } else if (strncmp(ptr,"legend string",13) == 0) {
587 sscanf(ptr,"%d%n",&set,&nchar);
589 } else if (ptr[0] == 's') {
591 sscanf(ptr,"%d%n",&set,&nchar);
594 if (strncmp(ptr,"legend",6) == 0) {
601 if (set >= legend_nalloc) {
602 legend_nalloc = set + 1;
603 srenew(*legend,legend_nalloc);
604 (*legend)[set] = read_xvgr_string(ptr);
608 } else if (ptr[0] != '#') {
610 (*ny) = nny = wordcount(ptr);
611 /* fprintf(stderr,"There are %d columns in your file\n",nny);*/
618 /* Allocate column space */
621 for(k=0; (k<nny); k++)
624 /* Initiate format string */
628 /* fprintf(stderr,"ptr='%s'\n",ptr);*/
629 for(k=0; (k<nny); k++) {
632 rval = sscanf(ptr,fmt,&lf);
633 /* fprintf(stderr,"rval = %d\n",rval);*/
634 if ((rval == EOF) || (rval == 0))
637 srenew(fmt,3*(nny+1)+1);
638 srenew(base,3*nny+1);
642 fprintf(stderr,"Only %d columns on line %d in file %s\n",
655 if (legend_nalloc > 0) {
656 if (*ny - 1 > legend_nalloc) {
657 srenew(*legend,*ny-1);
658 for(set=legend_nalloc; set<*ny-1; set++) {
659 (*legend)[set] = NULL;
667 int read_xvg(const char *fn,double ***y,int *ny)
669 return read_xvg_legend(fn,y,ny,NULL,NULL);
672 void write_xvg(const char *fn,const char *title,int nx,int ny,real **y,
673 const char **leg,const output_env_t oenv)
678 fp=xvgropen(fn,title,"X","Y",oenv);
680 xvgr_legend(fp,ny-1,leg,oenv);
681 for(i=0; (i<nx); i++) {
682 for(j=0; (j<ny); j++) {
683 fprintf(fp," %12.5e",y[j][i]);
690 real **read_xvg_time(const char *fn,
691 gmx_bool bHaveT,gmx_bool bTB,real tb,gmx_bool bTE,real te,
692 int nsets_in,int *nset,int *nval,real *dt,real **t)
695 #define MAXLINELEN 16384
696 char line0[MAXLINELEN];
698 int t_nalloc,*val_nalloc,a,narg,n,sin,set,nchar;
700 gmx_bool bEndOfSet,bTimeInRange,bFirstLine=TRUE;
709 fp = gmx_fio_fopen(fn,"r");
710 for(sin=0; sin<nsets_in; sin++) {
714 narg = bHaveT ? 2 : 1;
717 while (!bEndOfSet && fgets(line0,MAXLINELEN,fp)) {
719 /* Remove whitespace */
720 while (line[0]==' ' || line[0]=='\t')
722 bEndOfSet = (line[0] == '&');
723 if (line[0]!='#' && line[0]!='@' && line[0]!='\n' && !bEndOfSet) {
724 if (bFirstLine && bHaveT) {
725 /* Check the first line that should contain data */
726 a = sscanf(line,"%lf%lf",&dbl,&dbl);
728 gmx_fatal(FARGS,"Expected a number in %s on line:\n%s",fn,line0);
730 fprintf(stderr,"Found only 1 number on line, "
731 "assuming no time is present.\n");
740 while ((a<narg || (nsets_in==1 && n==0)) &&
741 sscanf(line,"%lf%n",&dbl,&nchar)==1 && bTimeInRange) {
742 /* Use set=-1 as the time "set" */
744 if (!bHaveT || (a>0))
754 if (set==-1 && ((bTB && dbl<tb) || (bTE && dbl>te)))
755 bTimeInRange = FALSE;
764 srenew(val_nalloc,*nset);
772 t_nalloc = over_alloc_small(n);
777 /* else we should check the time of the next sets with set 0 */
779 if (n >= val_nalloc[set]) {
780 val_nalloc[set] = over_alloc_small(n);
781 srenew(val[set],val_nalloc[set]);
783 val[set][n] = (real)dbl;
789 if (line0[strlen(line0)-1] != '\n') {
790 fprintf(stderr,"File %s does not end with a newline, ignoring the last line\n",fn);
791 } else if (bTimeInRange) {
793 fprintf(stderr,"Ignoring invalid line in %s:\n%s",fn,line0);
796 fprintf(stderr,"Invalid line in %s:\n%s"
797 "Using zeros for the last %d sets\n",
814 *dt = (real)((*t)[n-1]-(*t)[0])/(n-1.0);
819 fprintf(stderr,"Set %d is shorter (%d) than the previous set (%d)\n",
822 fprintf(stderr,"Will use only the first %d points of every set\n",