Fix core WST file
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / delay.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 (rewritten in C) Aug 22, 2004*/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <ctype.h>
26 #include "routines/tsa.h"
27
28 #define WID_STR "Produces delay vectors"
29
30
31 unsigned long length=ULONG_MAX;
32 unsigned long exclude=0;
33 unsigned int verbosity=0xff;
34 int delay=1;
35 unsigned int indim=1,embdim=2;
36 char *column=NULL,*format=NULL,*multidelay=NULL;
37 char *outfile=NULL;
38 char *infile=NULL;
39 char dimset=0,formatset=0,embset=0,mdelayset=0,delayset=0;
40 char stdo=1;
41
42 double **series;
43 unsigned int *formatlist,*delaylist;
44
45 void show_options(char *progname)
46 {
47   what_i_do(progname,WID_STR);
48   fprintf(stderr,"\nUsage: %s [options]\n",progname);
49   fprintf(stderr,"Options:\n");
50   fprintf(stderr,"Everything not being a valid option will be interpreted as a"
51           " possible datafile.\nIf no datafile is given stdin is read."
52           " Just - also means stdin\n");
53   fprintf(stderr,"\t-l # of data [default: whole file]\n");
54   fprintf(stderr,"\t-x # of rows to ignore [default: 0]\n");
55   fprintf(stderr,"\t-M num. of columns to read [default: %u]\n",indim);
56   fprintf(stderr,"\t-c columns to read [default: 1,...,M]\n");
57   fprintf(stderr,"\t-m dimension [default: 2]\n");
58   fprintf(stderr,"\t-F format of the delay vector (see man page)\n");
59   fprintf(stderr,"\t-d delay [default: 1]\n");
60   fprintf(stderr,"\t-D multi delay list (see man page)\n");
61   fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
62           "0='only panic messages'\n\t\t"
63           "1='+ input/output messages'\n");
64   fprintf(stderr,"\t-o output file [default: 'datafile'.del, "
65           "without -o: stdout]\n");
66   fprintf(stderr,"\t-h show these options\n");
67   exit(0);
68 }
69
70 void scan_options(int n,char **str)
71 {
72   char *out;
73
74   if ((out=check_option(str,n,'l','u')) != NULL)
75     sscanf(out,"%lu",&length);
76   if ((out=check_option(str,n,'x','u')) != NULL)
77     sscanf(out,"%lu",&exclude);
78   if ((out=check_option(str,n,'c','s')) != NULL)
79     column=out;
80   if ((out=check_option(str,n,'M','u')) != NULL) {
81     sscanf(out,"%u",&indim);
82     dimset=1;
83   }
84   if ((out=check_option(str,n,'F','s')) != NULL) {
85     format=out;
86     formatset=1;
87   }
88   if ((out=check_option(str,n,'m','u')) != NULL) {
89     sscanf(out,"%u",&embdim);
90     embset=1;
91   }
92   if ((out=check_option(str,n,'d','u')) != NULL) {
93     sscanf(out,"%u",&delay);
94     delayset=1;
95   }
96   if ((out=check_option(str,n,'D','s')) != NULL) {
97     multidelay=out;
98     mdelayset=1;
99   }
100   if ((out=check_option(str,n,'V','u')) != NULL)
101     sscanf(out,"%u",&verbosity);
102   if ((out=check_option(str,n,'o','o')) != NULL) {
103     stdo=0;
104     if (strlen(out) > 0)
105       outfile=out;
106   }
107 }
108
109 void create_format_list(void)
110 {
111   unsigned int i=0,num=0,sum=0;
112
113   while (format[i]) {
114     if (!(isdigit(format[i])) && !(format[i] == ',')) {
115       fprintf(stderr,"Wrong format of -F parameter. Exiting!\n");
116       exit(DELAY_WRONG_FORMAT_F);
117     }
118     i++;
119   }
120
121   i=0;
122   while (format[i]) {
123     if (format[i++] == ',')
124       num++;
125   }
126
127   check_alloc(formatlist=(unsigned int*)malloc(sizeof(int)*(num+1)));
128   for (i=0;i<=num;i++) {
129     sscanf(format,"%d",&formatlist[i]);
130     if (i<num) {
131       while ((*format) != ',')
132         format++;
133     }
134     format++;
135   }
136
137   if (dimset && ((num+1) != indim)) {
138     fprintf(stderr,"Number of dimensions in -F is not equal to -M. Exiting!\n");
139     exit(DELAY_DIM_NOT_EQUAL_F_M);
140   }
141
142   for (i=0;i<=num;i++)
143     sum += formatlist[i];
144   if (embset && (sum != embdim)) {
145     fprintf(stderr,"The dimensions given in -m and -F are not equal!"
146             " Exiting\n");
147     exit(DELAY_DIM_NOT_EQUAL_F_m);
148   }
149   if (!dimset)
150     indim=num+1;
151   if (!embset)
152     embdim=sum;
153 }
154
155 void create_delay_list(void)
156 {
157   unsigned int i=0,num=0;
158
159   while (multidelay[i]) {
160     if (!(isdigit(multidelay[i])) && !(multidelay[i] == ',')) {
161       fprintf(stderr,"Wrong format of -D parameter. Exiting!\n");
162       exit(DELAY_WRONG_FORMAT_D);
163     }
164     i++;
165   }
166
167   i=0;
168   while (multidelay[i]) {
169     if (multidelay[i++] == ',')
170       num++;
171   }
172
173   check_alloc(delaylist=(unsigned int*)malloc(sizeof(int)*(num+1)));
174   for (i=0;i<=num;i++) {
175     sscanf(multidelay,"%d",&delaylist[i]);
176     if (i<num) {
177       while ((*multidelay) != ',')
178         multidelay++;
179     }
180     multidelay++;
181   }
182
183   if ((num+1) != (embdim-indim)) {
184     fprintf(stderr,"Wrong number of delays. See man page. Exiting!\n");
185     exit(DELAY_WRONG_NUM_D);
186   }
187 }
188
189 int main(int argc,char **argv)
190 {
191   char stin=0;
192   unsigned long i;
193   int j,k;
194   unsigned int alldim,maxemb,emb,rundel,delsum,runmdel;
195   unsigned int *inddelay;
196   FILE *fout;
197
198   if (scan_help(argc,argv))
199     show_options(argv[0]);
200
201   scan_options(argc,argv);
202 #ifndef OMIT_WHAT_I_DO
203   if (verbosity&VER_INPUT)
204     what_i_do(argv[0],WID_STR);
205 #endif
206
207
208   infile=search_datafile(argc,argv,NULL,verbosity);
209   if (infile == NULL)
210     stin=1;
211
212   if (outfile == NULL) {
213     if (!stin) {
214       check_alloc(outfile=(char*)calloc(strlen(infile)+5,1));
215       strcpy(outfile,infile);
216       strcat(outfile,".del");
217     }
218     else {
219       check_alloc(outfile=(char*)calloc(10,1));
220       strcpy(outfile,"stdin.del");
221     }
222   }
223   if (!stdo)
224     test_outfile(outfile);
225
226   if (delayset && mdelayset) {
227     fprintf(stderr,"-d and -D can't be used simultaneously. Exiting!\n");
228     exit(DELAY_INCONS_d_D);
229   }
230
231   if (delay < 1) {
232     fprintf(stderr,"Delay has to be larger than 0. Exiting!\n");
233     exit(DELAY_SMALL_ZERO);
234   }
235
236   if (!formatset && (embdim%indim)) {
237     fprintf(stderr,"Inconsistent -m and -M. Please set -F\n");
238     exit(DELAY_INCONS_m_M);
239   }
240   if (formatset) {
241     create_format_list();
242   }
243   else {
244     check_alloc(formatlist=(unsigned int*)malloc(sizeof(int)*indim));
245     for (i=0;i<indim;i++) {
246       formatlist[i]=embdim/indim;
247     }
248   }
249
250   alldim=0;
251   for (i=0;i<indim;i++)
252     alldim += formatlist[i];
253
254   if (mdelayset) {
255     create_delay_list();
256   }
257
258   check_alloc(inddelay=(unsigned int*)malloc(sizeof(int)*alldim));
259
260   rundel=0;
261   if (!mdelayset) {
262     for (i=0;i<indim;i++) {
263       delsum=0;
264       inddelay[rundel++]=delsum;
265       for (j=1;j<formatlist[i];j++) {
266         delsum += delay;
267         inddelay[rundel++]=delsum;
268       }
269     }
270   }
271   else {
272     runmdel=0;
273     for (i=0;i<indim;i++) {
274       delsum=0;
275       inddelay[rundel++]=delsum;
276       for (j=1;j<formatlist[i];j++) {
277         delsum += delaylist[runmdel++];
278         inddelay[rundel++]=delsum;
279       }
280     }
281   }
282
283   maxemb=0;
284   for (i=0;i<alldim;i++)
285     maxemb=(maxemb<inddelay[i])?inddelay[i]:maxemb;
286
287   if (column == NULL) {
288     series=get_multi_series(infile,&length,exclude,&indim,"",dimset,verbosity);
289   }
290   else {
291     series=get_multi_series(infile,&length,exclude,&indim,column,dimset,
292                             verbosity);
293   }
294
295   if (stdo) {
296     if (verbosity)
297       fprintf(stderr,"Writing to stdout\n");
298     for (i=maxemb;i<length;i++) {
299       rundel=0;
300       for (j=0;j<indim;j++) {
301         emb=formatlist[j];
302         for (k=0;k<emb;k++)
303           fprintf(stdout,"%e ",series[j][i-inddelay[rundel++]]);
304       }
305       fprintf(stdout,"\n");
306     }
307   }
308   else {
309     fout=fopen(outfile,"w");
310     if (verbosity)
311       fprintf(stderr,"Opened %s for writing\n",outfile);
312     for (i=maxemb;i<length;i++) {
313       for (j=0;j<indim;j++) {
314         rundel=0;
315         emb=formatlist[j];
316         for (k=0;k<emb;k++)
317           fprintf(fout,"%e ",series[j][i-inddelay[rundel++]]);
318       }
319       fprintf(fout,"\n");
320     }
321     fclose(fout);
322   }
323
324   if (formatlist != NULL)
325     free(formatlist);
326   if (delaylist != NULL)
327     free(delaylist);
328   free(inddelay);
329
330   return 0;
331 }