Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / resample.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: Mar 11, 2002 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <math.h>
26 #include "routines/tsa.h"
27
28 #define WID_STR "Resample the data"
29
30 unsigned long length=ULONG_MAX,exclude=0;
31 unsigned int column=1,order=4;
32 unsigned int verbosity=0xff;
33 char *outfile=NULL,stdo=1;
34 char *infile=NULL;
35 double *series,sampletime=0.5;
36
37 void show_options(char *progname)
38 {
39   what_i_do(progname,WID_STR);
40   fprintf(stderr," Usage: %s [options]\n",progname);
41   fprintf(stderr," Options:\n");
42   fprintf(stderr,"Everything not being a valid option will be interpreted"
43           " as a possible"
44           " datafile.\nIf no datafile is given stdin is read. Just - also"
45           " means stdin\n");
46   fprintf(stderr,"\t-l length of file [default is whole file]\n");
47   fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
48   fprintf(stderr,"\t-c column to read [default is 1]\n");
49   fprintf(stderr,"\t-s new sampling time (in units of the old one)"
50           " [default is %f]\n",sampletime);
51   fprintf(stderr,"\t-p order of the interpolation [default is %d]\n",order);
52   fprintf(stderr,"\t-o output file name [default is 'datafile'.rs]\n");
53   fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t"
54           "0='only panic messages'\n\t\t"
55           "1='+ input/output messages'\n");
56   fprintf(stderr,"\t-h show these options\n\n");
57   exit(0);
58 }
59
60 void scan_options(int argc,char **argv)
61 {
62   char *out;
63
64   if ((out=check_option(argv,argc,'s','f')) != NULL)
65     sscanf(out,"%lf",&sampletime);
66   if ((out=check_option(argv,argc,'l','u')) != NULL)
67     sscanf(out,"%lu",&length);
68   if ((out=check_option(argv,argc,'x','u')) != NULL)
69     sscanf(out,"%lu",&exclude);
70   if ((out=check_option(argv,argc,'c','u')) != NULL)
71     sscanf(out,"%u",&column);
72   if ((out=check_option(argv,argc,'p','u')) != NULL)
73     sscanf(out,"%u",&order);
74   if ((out=check_option(argv,argc,'V','u')) != NULL)
75     sscanf(out,"%u",&verbosity);
76   if ((out=check_option(argv,argc,'o','o')) != NULL) {
77     stdo=0;
78     if (strlen(out) > 0)
79       outfile=out;
80   }
81 }
82
83 int main(int argc,char **argv)
84 {
85   char stdi=0;
86   long i,j,itime,itime_old;
87   int horder,horder2;
88   double **mat,*vec,**imat,*coef;
89   double time,htime,new_el;
90   FILE *file=NULL;
91
92   if (scan_help(argc,argv))
93     show_options(argv[0]);
94   
95   scan_options(argc,argv);
96 #ifndef OMIT_WHAT_I_DO
97   if (verbosity&VER_INPUT)
98     what_i_do(argv[0],WID_STR);
99 #endif
100
101   infile=search_datafile(argc,argv,&column,verbosity);
102   if (infile == NULL)
103     stdi=1;
104
105   if (outfile == NULL) {
106     if (!stdi) {
107       check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1));
108       strcpy(outfile,infile);
109       strcat(outfile,".rs");
110     }
111     else {
112       check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1));
113       strcpy(outfile,"stdin.rs");
114     }
115   }
116   if (!stdo)
117     test_outfile(outfile);
118
119   series=(double*)get_series(infile,&length,exclude,column,verbosity);
120   
121   horder=order+1;
122   horder2=(horder+1)/2-horder;
123
124   check_alloc(mat=(double**)malloc(sizeof(double*)*horder));
125   for (i=0;i<horder;i++)
126     check_alloc(mat[i]=(double*)malloc(sizeof(double)*horder));
127   check_alloc(vec=(double*)malloc(sizeof(double)*horder));
128   check_alloc(coef=(double*)malloc(sizeof(double)*horder));
129   
130   for (i=0;i<horder;i++)
131     for (j=0;j<horder;j++)
132       mat[i][j]=pow((double)(horder2+i),(double)j);
133
134   imat=invert_matrix(mat,(unsigned int)horder);
135
136   if (!stdo) {
137     file=fopen(outfile,"w");
138     if (verbosity&VER_INPUT)
139       fprintf(stderr,"Opened %s for writing\n",outfile);
140   }
141   else {
142     if (verbosity&VER_INPUT)
143       fprintf(stderr,"Writing to stdout\n");
144   }
145
146   time=(horder+1)/2.;
147   itime_old= -1;
148   while (time < (double)(length-horder/2)) {
149     itime=(int)time+horder2;
150     if (itime != itime_old) {
151       for (i=0;i<horder;i++)
152         vec[i]=series[i+itime];
153       for (i=0;i<horder;i++) {
154         coef[i]=0.0;
155         for (j=0;j<horder;j++)
156           coef[i] += imat[i][j]*vec[j];
157       }
158     }
159     itime_old=itime;
160     htime=time-itime+horder2;
161     new_el=coef[0];
162     for (i=1;i<horder;i++)
163       new_el += coef[i]*pow(htime,(double)i);
164     if (stdo)
165       fprintf(stdout,"%e\n",new_el);
166     else
167       fprintf(file,"%e\n",new_el);
168     time += sampletime;
169   }
170   if (!stdo)
171     fclose(file);
172
173   return 0;
174 }