Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / extrema.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: Dec 17, 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 "Determines the maxima (minima) of a possibly multivariate\
29  time series"
30
31
32 unsigned long length=ULONG_MAX,exclude=0;
33 char *column=NULL;
34 unsigned int verbosity=0xff;
35 unsigned int dim=1;
36 unsigned int which=1;
37 double mintime=0.0;
38 char dimset=0;
39 char maxima=1;
40 char stdo=1;
41 char *outfile=NULL;
42 char *infile=NULL;
43
44 void show_options(char *progname)
45 {
46   what_i_do(progname,WID_STR);
47   fprintf(stderr,"Usage: %s [options]\n",progname);
48   fprintf(stderr,"Options:\n");
49   fprintf(stderr,"Everything not being a valid option will be interpreted"
50           " as a possible"
51           " datafile.\nIf no datafile is given stdin is read. Just - also"
52           " means stdin\n");
53   fprintf(stderr,"\t-l # of points to use [Default: whole file]\n");
54   fprintf(stderr,"\t-x # of lines to be ignored [Default: 0]\n");
55   fprintf(stderr,"\t-m dimension (# of components) [Default: 1]\n");
56   fprintf(stderr,"\t-c columns to read [Default: 1,...,# of components]\n");
57   fprintf(stderr,"\t-w which component to maxi(mini)mize [Default: 1]\n");
58   fprintf(stderr,"\t-z determine minima instead of maxima [Default: maxima]\n");
59   fprintf(stderr,"\t-t minimal required time between two extrema "
60           "[Default: 0.0]\n");
61   fprintf(stderr,"\t-o output file name [Default: 'datafile'.ext,"
62           " without -o: stdout]\n");
63   fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t"
64           "0='only panic messages'\n\t\t"
65           "1='+ input/output messages'\n");
66   fprintf(stderr,"\t-h show these options\n");
67   exit(0);
68 }
69
70 void scan_options(int n,char **in)
71 {
72   char *out;
73
74   if ((out=check_option(in,n,'l','u')) != NULL)
75     sscanf(out,"%lu",&length);
76   if ((out=check_option(in,n,'x','u')) != NULL)
77     sscanf(out,"%lu",&exclude);
78   if ((out=check_option(in,n,'m','u')) != NULL) {
79     sscanf(out,"%u",&dim);
80     dimset=1;
81   }
82   if ((out=check_option(in,n,'c','s')) != NULL)
83     column=out;
84   if ((out=check_option(in,n,'w','u')) != NULL)
85     sscanf(out,"%u",&which);
86   if ((out=check_option(in,n,'z','n')) != NULL)
87     maxima=0;
88   if ((out=check_option(in,n,'t','f')) != NULL)
89     sscanf(out,"%lf",&mintime);
90   if ((out=check_option(in,n,'V','u')) != NULL)
91     sscanf(out,"%u",&verbosity);
92   if ((out=check_option(in,n,'o','o')) != NULL) {
93     stdo=0;
94     if (strlen(out) > 0)
95       outfile=out;
96   }
97 }
98
99 int main(int argc,char **argv)
100 {
101   char stdi=0;
102   unsigned long i,j;
103   double **series;
104   double x[3],a,b,c,lasttime,nexttime,time;
105   FILE *fout=NULL;
106
107   if (scan_help(argc,argv))
108     show_options(argv[0]);
109   
110   scan_options(argc,argv);
111 #ifndef OMIT_WHAT_I_DO
112   if (verbosity&VER_INPUT)
113     what_i_do(argv[0],WID_STR);
114 #endif
115
116   which--;
117   if (which > (dim-1)) {
118     fprintf(stderr,"The component to maxi(mini)mize has to be smaller or equal"
119             "to the number\nof components! Exiting\n");
120     exit(EXTREMA_STRANGE_COMPONENT);
121   }
122   infile=search_datafile(argc,argv,NULL,verbosity);
123   if (infile == NULL)
124     stdi=1;
125
126   if (outfile == NULL) {
127     if (!stdi) {
128       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
129       sprintf(outfile,"%s.ext",infile);
130     }
131     else {
132       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
133       sprintf(outfile,"stdin.ext");
134     }
135   }
136
137   if (column == NULL)
138     series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
139                                       verbosity);
140   else
141     series=(double**)get_multi_series(infile,&length,exclude,&dim,column,
142                                       dimset,verbosity);
143   
144   if (!stdo) {
145     test_outfile(outfile);    
146     fout=fopen(outfile,"w");
147     if (verbosity&VER_INPUT)
148       fprintf(stderr,"Opened %s for writing\n",outfile);
149   }
150   else {
151     if (verbosity&VER_INPUT)
152       fprintf(stderr,"Writing to stdout\n");
153   }
154   
155   lasttime=0.0;
156   x[0]=series[which][0];
157   x[1]=series[which][1];
158   for (i=2;i<length;i++) {
159     x[2]=series[which][i];
160     if (maxima) {
161       if ((x[1] >= x[0]) && (x[1] > x[2])) {
162         a=x[1];
163         b=(x[2]-x[0])/2.0;
164         c=(x[2]-2.0*x[1]+x[0])/2.0;
165         time= -b/2.0/c;
166         nexttime=(double)i-1.0+time;
167         if ((nexttime-lasttime) >= mintime) {
168           for (j=0;j<dim;j++) {
169             a=series[j][i-1];
170             b=(series[j][i]-series[j][i-2])/2.0;
171             c=(series[j][i]-2.0*series[j][i-1]+series[j][i-2])/2.0;
172             if (!stdo)
173               fprintf(fout,"%e ",a+b*time+c*sqr(time));
174             else
175               fprintf(stdout,"%e ",a+b*time+c*sqr(time));
176           }
177           if (!stdo)
178             fprintf(fout,"%e\n",nexttime-lasttime);
179           else
180             fprintf(stdout,"%e\n",nexttime-lasttime);
181           lasttime=nexttime;
182         }
183       }
184     }
185     else {
186       if ((x[1] <= x[0]) && (x[1] < x[2])) {
187         a=x[1];
188         b=(x[2]-x[0])/2.0;
189         c=(x[2]-2.0*x[1]+x[0])/2.0;
190         time= -b/2.0/c;
191         nexttime=(double)i-1.0+time;
192         if ((nexttime-lasttime) >= mintime) {
193           for (j=0;j<dim;j++) {
194             a=series[j][i-1];
195             b=(series[j][i]-series[j][i-2])/2.0;
196             c=(series[j][i]-2.0*series[j][i-1]+series[j][i-2])/2.0;
197             if (!stdo)
198               fprintf(fout,"%e ",a+b*time+c*sqr(time));
199             else
200               fprintf(stdout,"%e ",a+b*time+c*sqr(time));
201           }
202           if (!stdo)
203             fprintf(fout,"%e\n",nexttime-lasttime);
204           else
205             fprintf(stdout,"%e\n",nexttime-lasttime);
206           lasttime=nexttime;
207         }
208       }
209     }
210     x[0]=x[1];
211     x[1]=x[2];
212   }
213   if (!stdo)
214     fclose(fout);
215
216   if (infile != NULL)
217     free(infile);
218   if (outfile != NULL)
219     free(outfile);
220   for (i=0;i<dim;i++)
221     free(series[i]);
222   free(series);
223   
224   return 0;
225 }