Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / av-d2.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger Last modified: Sep 3, 1999 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <limits.h>
26 #include "routines/tsa.h"
27
28 #define WID_STR "Smoothes the output of the d2 program"
29
30 #define MAXLENGTH 1000
31
32 unsigned int maxdim=UINT_MAX,mindim=1;
33 unsigned int verbosity=0xff;
34 int aver=1;
35 char rescaled=0;
36 char stout=1;
37 char *outfile=NULL;
38 char *infile=NULL;
39
40 void show_options(char *progname)
41 {
42   what_i_do(progname,WID_STR);
43   fprintf(stderr,"Usage: %s [options]\n",progname);
44   fprintf(stderr,"Options:\n");
45   fprintf(stderr,"Everything not being a valid option will be interpreted"
46           " as a possible datafile.\nStdin does NOT work.\n");
47   fprintf(stderr,"\t-m dimension to start with [Default: 1]\n");
48   fprintf(stderr,"\t-M dimension to end with [Default: whole file]\n");
49   fprintf(stderr,"\t-a n average over (2n+1) values [Default: 1]\n");
50   fprintf(stderr,"\t-E use rescaled data for the length scales\n\t\t"
51           "[Default: use units of data]\n");
52   fprintf(stderr,"\t-o name of output file [Default: stdout,\n\t\t"
53           "-o without value means 'datafile'.av]\n");
54   fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t"
55           "0='only panic messages'\n\t\t"
56           "1='+ input/output messages'\n");
57   fprintf(stderr,"\t-h show these options\n");
58   exit(0);
59 }
60
61 void scan_options(int n,char **in)
62 {
63   char *out;
64   
65   if ((out=check_option(in,n,'m','u')) != NULL)
66     sscanf(out,"%u",&mindim);
67   if ((out=check_option(in,n,'M','u')) != NULL)
68     sscanf(out,"%u",&maxdim);
69   if ((out=check_option(in,n,'a','u')) != NULL)
70     sscanf(out,"%u",&aver);
71   if ((out=check_option(in,n,'V','u')) != NULL)
72     sscanf(out,"%u",&verbosity);
73   if ((out=check_option(in,n,'E','n')) != NULL)
74     rescaled=1;
75   if ((out=check_option(in,n,'o','o')) != NULL) {
76     stout=0;
77     if (strlen(out) > 0)
78       outfile=out;
79   }
80 }
81
82 int main(int argc,char **argv)
83 {
84   char instr[1024];
85   char *form1="%lf%lf",*form2="%*lf%lf%lf";
86   char empty=0;
87   unsigned int howmany,size=1;
88   int j,k;
89   long dim;
90   double *eps,*y;
91   double avy,aveps,norm;
92   FILE *file,*fout=NULL;
93
94   if ((argc < 2) || scan_help(argc,argv))
95     show_options(argv[0]);
96   
97   scan_options(argc,argv);
98 #ifndef OMIT_WHAT_I_DO
99   if (verbosity&VER_INPUT)
100     what_i_do(argv[0],WID_STR);
101 #endif
102
103   infile=search_datafile(argc,argv,0L,verbosity);
104   if (infile == NULL) {
105     fprintf(stderr,"You have to give a datafile. Exiting!\n");
106     exit(127);
107   }
108   if (outfile == NULL) {
109     check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1));
110     sprintf(outfile,"%s.av",infile);
111   }
112   
113   check_alloc(eps=(double*)malloc(sizeof(double)*MAXLENGTH));
114   check_alloc(y=(double*)malloc(sizeof(double)*MAXLENGTH));
115   
116   file=fopen(infile,"r");
117   
118   if (!stout) {
119     test_outfile(outfile);
120     fout=fopen(outfile,"w");
121     if (verbosity&VER_INPUT)
122       fprintf(stderr,"Opened %s for writing\n",outfile);
123   }
124   else {
125     if (verbosity&VER_INPUT)
126       fprintf(stderr,"Writing to stdout\n");
127   }
128
129   if (mindim > maxdim)
130     mindim=maxdim;
131   norm=2.0*aver+1.0;
132
133   while (fgets(instr,1024,file) != NULL) {
134     if (strlen(instr) != 1) {
135       if (instr[0] == '#') {
136         if (strstr(instr,"m= ") != NULL) {
137           sscanf(instr,"%*s %ld",&dim);
138           if ((dim >= mindim) && (dim <= maxdim)) {
139             howmany=0;
140             empty=0;
141             do {
142               if (fgets(instr,1024,file) == NULL)
143                 exit(127);
144               if (strlen(instr) == 1)
145                 empty=1;
146               if (!empty && (instr[0] != '#')) {
147                 if (!rescaled)
148                   sscanf(instr,form1,&eps[howmany],&y[howmany]);
149                 else
150                   sscanf(instr,form2,&y[howmany],&eps[howmany]);
151                 howmany++;
152                 if (!(howmany%MAXLENGTH)) {
153                   check_alloc(realloc(eps,size*MAXLENGTH*sizeof(double)));
154                   check_alloc(realloc(y,size*MAXLENGTH*sizeof(double)));
155                   size++;
156                 }
157               }
158             } while (!empty);
159             for (k=aver;k<howmany-aver;k++) {
160               avy=aveps=0.0;
161               for (j= -aver;j<=aver;j++) {
162                 avy += y[k+j];
163                 aveps += eps[k+j];
164               }
165               if (!stout)
166                 fprintf(fout,"%e %e\n",aveps/norm,avy/norm);
167               else
168                 fprintf(stdout,"%e %e\n",aveps/norm,avy/norm);
169             }
170             if (!stout)
171               fprintf(fout,"\n");
172             else
173               fprintf(stdout,"\n");
174           }
175         }
176       }
177     }
178   }
179
180   if (outfile != NULL)
181     free(outfile);
182   if (infile != NULL)
183     free(infile);
184   free(eps);
185   free(y);
186
187   return 0;
188 }