Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / low121.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, 2001 */
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 "Simple lowpass filter in the time domain"
29
30
31 unsigned long length=ULONG_MAX,exclude=0;
32 unsigned int column=1,iterations=1;
33 unsigned int verbosity=0x1;
34 char *outfile=NULL,stdo=1;
35 char *infile=NULL;
36
37 double *series,*new;
38
39 void show_options(char *progname)
40 {
41   what_i_do(progname,WID_STR);
42   fprintf(stderr,"Usage: %s [options]\n",progname);
43   fprintf(stderr,"Options:\n");
44   fprintf(stderr,"Everything not being a valid option will be interpreted"
45           " as a possible"
46           " datafile.\nIf no datafile is given stdin is read. Just - also"
47           " means stdin\n");
48   fprintf(stderr,"\t-l # of points to use [Default: whole file]\n");
49   fprintf(stderr,"\t-x # of lines to be ignored [Default: 0]\n");
50   fprintf(stderr,"\t-c column to read [Default: 1]\n");
51   fprintf(stderr,"\t-i # of iterations [Default: 1]\n");
52   fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t"
53           "0='only panic messages'\n\t\t"
54           "1='+ input/output messages'\n\t\t"
55           "2='+ print each iteration to a separate file\n");
56   fprintf(stderr,"\t-o output file name(s) [Default: 'datafile'.low.n,\n\t\t"
57           "where n is the number of the iteration.\n\t\t"
58           "without -o the last iteration is written to stdout.]\n");
59   fprintf(stderr,"\t-h show these options\n");
60   exit(0);
61 }
62
63 void scan_options(int n,char **in)
64 {
65   char *out;
66
67   if ((out=check_option(in,n,'l','u')) != NULL)
68     sscanf(out,"%lu",&length);
69   if ((out=check_option(in,n,'x','u')) != NULL)
70     sscanf(out,"%lu",&exclude);
71   if ((out=check_option(in,n,'c','u')) != NULL)
72     sscanf(out,"%u",&column);
73   if ((out=check_option(in,n,'i','u')) != NULL)
74     sscanf(out,"%u",&iterations);
75   if ((out=check_option(in,n,'V','d')) != NULL)
76     sscanf(out,"%d",&verbosity);
77   if ((out=check_option(in,n,'o','o')) != NULL) {
78     stdo=0;
79     if (strlen(out) > 0)
80       outfile=out;
81   }
82 }
83
84 int main(int argc,char **argv)
85 {
86   char stdi=0;
87   char *ofname;
88   unsigned long i;
89   unsigned int iter;
90   FILE *file;
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)+5,(size_t)1));
108       check_alloc(ofname=(char*)calloc(strlen(infile)+9,(size_t)1));
109       sprintf(outfile,"%s.low",infile);
110     }
111     else {
112       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
113       check_alloc(ofname=(char*)calloc((size_t)14,(size_t)1));
114       sprintf(outfile,"stdin.low");
115     }
116   }
117   else
118     check_alloc(ofname=(char*)calloc(strlen(outfile)+10,(size_t)1));
119   
120   series=(double*)get_series(infile,&length,exclude,column,verbosity);
121   check_alloc(new=(double*)malloc(sizeof(double)*length));
122   
123   if (verbosity&VER_USR1) {
124     for (iter=1;iter<=iterations;iter++) {
125       new[0]=(2.0*series[0]+2.0*series[1])/4.0;
126       new[length-1]=(2.0*series[length-1]+2.0*series[length-2])/4.0;
127       for (i=1;i<length-1;i++)
128         new[i]=(series[i-1]+2.0*series[i]+series[i+1])/4.0;
129         sprintf(ofname,"%s.%d",outfile,iter);
130         test_outfile(ofname);
131         file=fopen(ofname,"w");
132         if (verbosity&VER_INPUT)
133           fprintf(stderr,"Opened %s for writing\n",ofname);
134         if (stdo && (iter == iterations)) {
135           if (verbosity&VER_INPUT)
136             fprintf(stderr,"Writing to stdout\n");
137         }
138         for (i=0;i<length;i++) {
139           if (stdo && (iter == iterations))
140             fprintf(stdout,"%e\n",series[i]=new[i]);
141           fprintf(file,"%e\n",series[i]=new[i]);
142         }
143         fclose(file);
144     }
145   }
146   else {
147     for (iter=1;iter<=iterations;iter++) {
148       new[0]=(2.0*series[0]+2.0*series[1])/4.0;
149       new[length-1]=(2.0*series[length-1]+2.0*series[length-2])/4.0;
150       for (i=1;i<length-1;i++)
151         new[i]=(series[i-1]+2.0*series[i]+series[i+1])/4.0;
152       for (i=0;i<length;i++)
153         series[i]=new[i];
154     }
155     if (!stdo) {
156       sprintf(ofname,"%s.%d",outfile,iterations);
157       file=fopen(ofname,"w");
158       if (verbosity&VER_INPUT)
159         fprintf(stderr,"Opened %s for writing\n",ofname);
160       for (i=0;i<length;i++)
161         fprintf(file,"%e\n",series[i]);
162       fclose(file);
163     }
164     else {
165       if (verbosity&VER_INPUT)
166         fprintf(stderr,"Writing to stdout\n");
167       for (i=0;i<length;i++)
168         fprintf(stdout,"%e\n",series[i]);
169     }
170   }
171
172   return 0;
173 }