Adding DisEMBL dependency Tisean executable
[jabaws.git] / binaries / src / disembl / Tisean_3.0.1 / source_c / corr.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 <math.h>
24 #include <limits.h>
25 #include <string.h>
26 #include "routines/tsa.h"
27
28 #define WID_STR "Estimates the autocorrelations of a data set"
29
30 char *format,*outfile=NULL,stout=1,normalize=1;
31 unsigned int column=1;
32 unsigned int verbosity=0xff;
33 unsigned long tau=100,length=ULONG_MAX,exclude=0;
34 double *array;
35 double av,var;
36 char *infile=NULL;
37
38 void show_options(char *progname) 
39 {
40   what_i_do(progname,WID_STR);
41   fprintf(stderr," Usage: %s [Options]\n",progname);
42   fprintf(stderr," Options:\n");
43   fprintf(stderr,"Everything not being a valid option will be interpreted"
44           " as a possible"
45           " datafile.\nIf no datafile is given stdin is read. Just - also"
46           " means stdin\n");
47   fprintf(stderr,"\t-l length [default is whole file]\n");
48   fprintf(stderr,"\t-x # of lines to be ignored [default 0]\n");
49   fprintf(stderr,"\t-c column to read [default is 1]\n");
50   fprintf(stderr,"\t-D corrlength  [default is 100]\n");
51   fprintf(stderr,"\t-n don\'t normalize to the variance"
52           " of the data [not set]\n");
53   fprintf(stderr,"\t-o output_file  [default is 'datafile'.cor; no -o"
54   " means stdout]\n");
55   fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t"
56           "0='only panic messages'\n\t\t"
57           "1='+ input/output messages'\n");
58   fprintf(stderr,"\t-h show these options\n");
59   fprintf(stderr,"\n");
60   exit(0);
61 }
62
63 void scan_options(int argc,char **argv)
64 {
65   char *out;
66
67   if ((out=check_option(argv,argc,'l','u')) != NULL)
68     sscanf(out,"%lu",&length);
69   if ((out=check_option(argv,argc,'x','u')) != NULL)
70     sscanf(out,"%lu",&exclude);
71   if ((out=check_option(argv,argc,'c','u')) != NULL)
72     sscanf(out,"%u",&column);
73   if ((out=check_option(argv,argc,'D','u')) != NULL)
74     sscanf(out,"%lu",&tau);
75   if ((out=check_option(argv,argc,'n','n')) != NULL)
76     normalize=0;
77   if ((out=check_option(argv,argc,'V','u')) != NULL)
78     sscanf(out,"%u",&verbosity);
79   if ((out=check_option(argv,argc,'o','o')) != NULL) {
80     stout=0;
81     if (strlen(out) > 0)
82       outfile=out;
83   }
84 }
85
86 double corr(long i)
87 {
88   long j;
89   double c=0.0;
90   
91   for (j=0;j<(length-i);j++)
92     c += array[j]*array[j+i];
93
94   return c/(length-i);
95 }
96
97 int main(int argc,char** argv)
98 {
99   char stdi=0;
100   long i;
101   FILE *fout=NULL;
102   
103   if (scan_help(argc,argv))
104     show_options(argv[0]);
105   
106   scan_options(argc,argv);
107 #ifndef OMIT_WHAT_I_DO
108   if (verbosity&VER_INPUT)
109     what_i_do(argv[0],WID_STR);
110 #endif
111
112   infile=search_datafile(argc,argv,&column,verbosity);
113   if (infile == NULL)
114     stdi=1;
115   
116   if (outfile == NULL) {
117     if (!stdi) {
118       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
119       strcpy(outfile,infile);
120       strcat(outfile,".cor");
121     }
122     else {
123       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
124       strcpy(outfile,"stdin.cor");
125     }
126   }
127   if (!stout)
128     test_outfile(outfile);
129
130   array=(double*)get_series(infile,&length,exclude,column,verbosity);
131
132   if (tau >= length)
133     tau=length-1;
134
135   variance(array,length,&av,&var);
136
137   if (normalize) {
138     for (i=0;i<length;i++)
139       array[i] -= av;
140   }
141
142   if (!stout) {
143     fout=fopen(outfile,"w");
144     if (verbosity&VER_INPUT)
145       fprintf(stderr,"Opened %s for writing\n",outfile);
146     fprintf(fout,"# average=%e\n",av);
147     fprintf(fout,"# standard deviation=%e\n",var);
148   }
149   else {
150     if (verbosity&VER_INPUT)
151       fprintf(stderr,"Writing to stdout\n");
152     fprintf(stdout,"# average=%e\n",av);
153     fprintf(stdout,"# standard deviation=%e\n",var);
154   }
155   if (normalize)
156     var *= var;
157   else
158     var=1.0;
159
160   for (i=0;i<=tau;i++)
161     if (!stout) {
162       fprintf(fout,"%ld %e\n",i,corr(i)/var);
163       fflush(fout);
164     }
165     else {
166       fprintf(stdout,"%ld %e\n",i,corr(i)/var);
167       fflush(stdout);
168     }
169   if (!stout)
170     fclose(fout);
171
172   if (outfile != NULL)
173     free(outfile);
174   if (infile != NULL)
175     free(infile);
176   free(array);
177
178   return 0;
179 }