86911e6264cd9f3e994b46b9b0c8385e4bc3e7bd
[jabaws.git] / binaries / src / disembl / Tisean_3.0.1 / source_c / rescale.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: Nov 23, 2000 */
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 "Rescales the data"
29
30 unsigned long length=ULONG_MAX,exclude=0;
31 unsigned int dim=1;
32 unsigned int verbosity=0xff;
33 char *column=NULL;
34 char *outfile=NULL,stdo=1,set_av=0,set_var=0,dimset=0;
35 char *infile=NULL;
36 double **series;
37 double xmin=0.0,xmax=1.0;
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 data to use [default: whole file]\n");
49   fprintf(stderr,"\t-x # of lines to ignore [default: 0]\n");
50   fprintf(stderr,"\t-m # of components to be read [default: %u]\n",dim);
51   fprintf(stderr,"\t-c columns to read [default: 1,...,# of components]\n");
52   fprintf(stderr,"\t-z minimum of the new series [default: 0.0]\n");
53   fprintf(stderr,"\t-Z maximum of the new series [default: 1.0]\n");
54   fprintf(stderr,"\t-a create a series with average value equals 0\n");
55   fprintf(stderr,"\t-v create a series with variance 1\n");
56   fprintf(stderr,"\t-o output file name [default: 'datafile'.res']\n");
57   fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
58           "0='only panic messages'\n\t\t"
59           "1='+ input/output messages'\n");
60   fprintf(stderr,"\t-h show these options\n");
61   exit(0);
62 }
63
64 void scan_options(int n,char **in)
65 {
66   char *out;
67
68   if ((out=check_option(in,n,'l','u')) != NULL)
69     sscanf(out,"%lu",&length);
70   if ((out=check_option(in,n,'x','u')) != NULL)
71     sscanf(out,"%lu",&exclude);
72   if ((out=check_option(in,n,'m','u')) != NULL) {
73     sscanf(out,"%u",&dim);
74     dimset=1;
75   }
76   if ((out=check_option(in,n,'c','s')) != NULL)
77     column=out;
78   if ((out=check_option(in,n,'V','u')) != NULL)
79     sscanf(out,"%u",&verbosity);
80   if ((out=check_option(in,n,'z','f')) != NULL)
81     sscanf(out,"%lf",&xmin);
82   if ((out=check_option(in,n,'Z','f')) != NULL)
83     sscanf(out,"%lf",&xmax);
84   if ((out=check_option(in,n,'a','n')) != NULL)
85     set_av=1;
86   if ((out=check_option(in,n,'v','n')) != NULL)
87     set_var=1;
88   if ((out=check_option(in,n,'o','o')) != NULL) {
89     stdo=0;
90     if (strlen(out) > 0)
91       outfile=out;
92   }
93 }
94
95 int main(int argc,char **argv)
96 {
97   char stdi=0;
98   FILE *file;
99   double min,max;
100   double av,varianz;
101   long i,n;
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,NULL,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,".res");
121     }
122     else {
123       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
124       strcpy(outfile,"stdin.res");
125     }
126   }
127   if (!stdo)
128     test_outfile(outfile);
129
130   if (xmin >= xmax) {
131     fprintf(stderr,"Choosing the minimum larger or equal the maximum\n"
132             "makes no sense. Exiting!\n");
133     exit(RESCALE__WRONG_INTERVAL);
134   }
135
136   if (column == NULL)
137     series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
138                                       verbosity);
139   else
140     series=(double**)get_multi_series(infile,&length,exclude,&dim,column,
141                                       dimset,verbosity);
142
143   for (n=0;n<dim;n++) {
144     variance(series[n],length,&av,&varianz);
145     
146     if (set_av)
147       for (i=0;i<length;i++)
148         series[n][i] -= av;
149
150     if (set_var)
151       for (i=0;i<length;i++)
152         series[n][i] /= varianz;
153   
154     if (!set_var && !set_av) {
155       rescale_data(series[n],length,&min,&max);
156       for (i=0;i<length;i++)
157         series[n][i]=series[n][i]*(xmax-xmin)+xmin;
158     }
159   }
160
161   if (stdo) {
162     if (verbosity&VER_INPUT)
163       fprintf(stderr,"Writing to stdout\n");
164     for (i=0;i<length;i++) {
165       fprintf(stdout,"%e",series[0][i]);
166       for (n=1;n<dim;n++)
167         fprintf(stdout," %e",series[n][i]);
168       fprintf(stdout,"\n");
169     }
170   }
171   else {
172     file=fopen(outfile,"w");
173     if (verbosity&VER_INPUT)
174       fprintf(stderr,"Opened %s for writing\n",outfile);
175     for (i=0;i<length;i++) {
176       fprintf(file,"%e",series[0][i]);
177       for (n=1;n<dim;n++)
178         fprintf(file," %e",series[n][i]);
179       fprintf(file,"\n");
180     }
181     fclose(file);
182   }
183
184   return 0;
185 }