Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / histogram.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 6, 2005*/
21 /*Changes:
22   12/06/05: shift output x value to center of interval
23 */
24 #include <math.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "routines/tsa.h"
30
31 #define WID_STR "Makes a histogram of the data"
32
33 unsigned long length=ULONG_MAX;
34 unsigned long base=50;
35 unsigned long exclude=0;
36 unsigned int column=1;
37 unsigned int verbosity=0xff;
38 double size;
39 char my_stdout=1,gotsize=0;
40 char *outfile=NULL;
41 char *infile=NULL;
42
43 double *series;
44 double average,var;
45 double min,max;
46 long *box;
47
48 void show_options(char *progname)
49 {
50   what_i_do(progname,WID_STR);
51   fprintf(stderr," Usage: %s [options]\n",progname);
52   fprintf(stderr," options:\n");
53   fprintf(stderr,"Everything not being a valid option will be interpreted as a"
54           " possible datafile.\nIf no datafile is given stdin is read. "
55           " Just - also means stdin\n");
56   fprintf(stderr,"\t-l length of file [default whole file]\n");
57   fprintf(stderr,"\t-x # of lines to ignore [default %ld]\n",exclude);
58   fprintf(stderr,"\t-c column to read [default %d]\n",column);
59   fprintf(stderr,"\t-b # of intervals [default %ld]\n",base);
60   fprintf(stderr,"\t-o output file [default 'datafile'.dat ;"
61           " If no -o is given: stdout]\n");
62   fprintf(stderr,"\t-V verbosity level [default 1]\n\t\t"
63           "0='only panic messages'\n\t\t"
64           "1='+ input/output messages'\n");
65   fprintf(stderr,"\t-h show these options\n");
66   exit(0);
67 }
68
69 void scan_options(int n,char **str)
70 {
71   char *out;
72
73   if ((out=check_option(str,n,'l','u')) != NULL)
74     sscanf(out,"%lu",&length);
75   if ((out=check_option(str,n,'x','u')) != NULL)
76     sscanf(out,"%lu",&exclude);
77   if ((out=check_option(str,n,'c','u')) != NULL)
78     sscanf(out,"%u",&column);
79   if ((out=check_option(str,n,'b','u')) != NULL)
80     sscanf(out,"%lu",&base);
81   if ((out=check_option(str,n,'V','u')) != NULL)
82     sscanf(out,"%u",&verbosity);
83   if ((out=check_option(str,n,'o','o')) != NULL) {
84     my_stdout=0;
85     if (strlen(out) > 0)
86       outfile=out;
87   }
88 }
89
90 int main(int argc,char **argv)
91 {
92   char stdi=0;
93   unsigned long i,j;
94   double x,norm,size=1.0,size2=1.0;
95   FILE *fout;
96
97   if (scan_help(argc,argv))
98     show_options(argv[0]);
99   
100   scan_options(argc,argv);
101 #ifndef OMIT_WHAT_I_DO
102   if (verbosity&VER_INPUT)
103     what_i_do(argv[0],WID_STR);
104 #endif
105
106   infile=search_datafile(argc,argv,&column,verbosity);
107   if (infile == NULL)
108     stdi=1;
109
110   if (outfile == NULL) {
111     if (!stdi) {
112       check_alloc(outfile=(char*)calloc(strlen(infile)+5,1));
113       strcpy(outfile,infile);
114       strcat(outfile,".his");
115     }
116     else {
117       check_alloc(outfile=(char*)calloc((size_t)10,1));
118       strcpy(outfile,"stdin.his");
119     }
120   }
121   if (!my_stdout)
122     test_outfile(outfile);
123
124   series=(double*)get_series(infile,&length,exclude,column,verbosity);
125   variance(series,length,&average,&var);
126   rescale_data(series,length,&min,&max);
127   
128   
129   if (base > 0) {
130     check_alloc(box=(long*)malloc(sizeof(long)*base));
131     for (i=0;i<base;i++)
132       box[i]=0;
133     size=1./base;
134     size2=size/2.0;
135     for (i=0;i<length;i++) {
136       if (series[i] > (1.0-size2))
137         series[i]=1.0-size2;
138       j=(long)(series[i]*base);
139       box[j]++;
140     }
141   }
142
143   norm=1.0/(double)length;
144   if (!my_stdout) {
145     fout=fopen(outfile,"w");
146     if (verbosity&VER_INPUT)
147       fprintf(stderr,"Opened %s for writing\n",outfile);
148     fprintf(fout,"#interval of data: [%e:%e]\n",min,max+min);
149     fprintf(fout,"#average= %e\n",average);
150     fprintf(fout,"#standard deviation= %e\n",var);
151     for (i=0;i<base;i++) {
152       x=(double)(i*size);
153       fprintf(fout,"%e %e\n",(x+size2)*max+min,(double)box[i]*norm);
154     }
155     fclose(fout);
156   }
157   else {
158     if (verbosity&VER_INPUT)
159       fprintf(stderr,"Writing to stdout\n");
160     fprintf(stdout,"#interval of data: [%e:%e]\n",min,max+min);
161     fprintf(stdout,"#average= %e\n",average);
162     fprintf(stdout,"#standard deviation= %e\n",var);
163     for (i=0;i<base;i++) {
164       x=(double)(i*size);
165       fprintf(stdout,"%e %e\n",(x+size2)*max+min,(double)box[i]*norm);
166       fflush(stdout);
167     }
168   }
169   return 0;
170 }