ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/histo.c
Revision: 1.4
Committed: Sun Dec 14 16:33:37 2003 UTC (20 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad3R6, rad3R6P1, rad3R8
Changes since 1.3: +46 -17 lines
Log Message:
Added -p option to histo to report percentiles and changed output slightly

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.4 static const char RCSid[] = "$Id: histo.c,v 1.3 2003/07/27 22:12:01 schorsch Exp $";
3 greg 1.1 #endif
4     /*
5     * Compute a histogram from input data
6     *
7     * 9/5/96 Greg Ward
8     */
9    
10     #include <stdio.h>
11     #include <stdlib.h>
12 schorsch 1.2 #include <string.h>
13 greg 1.1 #include <math.h>
14     #include <ctype.h>
15    
16     #define MAXCOL 64 /* maximum number of input columns */
17     #define MAXDIV 1024
18    
19     #define isint(x) (floor((x)+1e-6) != floor((x)-1e-6))
20    
21     char *progname;
22    
23     int cumulative = 0;
24 greg 1.4 int percentile = 0;
25 greg 1.1
26 greg 1.4 long outrange[MAXCOL][2];
27 greg 1.1 long histo[MAXCOL][MAXDIV];
28 greg 1.4 long ctotal[MAXCOL];
29 greg 1.1 double minv, maxv;
30     int ndiv;
31    
32     int ncols;
33    
34    
35 schorsch 1.2 static void
36     readinp(void) /* gather statistics on input */
37 greg 1.1 {
38     char buf[16*MAXCOL];
39     double d;
40 greg 1.4 int i;
41 greg 1.1 register int c;
42     register char *cp;
43    
44     while ((cp = fgets(buf, sizeof(buf), stdin)) != NULL) {
45     for (c = 0; c < MAXCOL; c++) {
46     while (isspace(*cp))
47     cp++;
48     if (!*cp)
49     break;
50     d = atof(cp);
51     while (*cp && !isspace(*cp))
52     cp++;
53 greg 1.4 if (d <= minv)
54     outrange[c][0]++;
55     else if (d >= maxv)
56     outrange[c][1]++;
57     else
58 greg 1.1 histo[c][(int)(ndiv*(d-minv)/(maxv-minv))]++;
59     }
60     if (c > ncols)
61     ncols = c;
62     }
63 greg 1.4 for (c = 0; c < ncols; c++) {
64     ctotal[c] += outrange[c][0] + outrange[c][1];
65     for (i = 0; i < ndiv; i++)
66     ctotal[c] += histo[c][i];
67     }
68 greg 1.1 }
69    
70    
71 schorsch 1.2 static void
72 greg 1.4 printcumul( /* print cumulative histogram results */
73     int pctl
74     )
75 greg 1.1 {
76 greg 1.4 long csum[MAXCOL];
77 greg 1.1 register int i, c;
78    
79     for (c = ncols; c--; )
80 greg 1.4 csum[c] = outrange[c][0];
81 greg 1.1
82 greg 1.4 for (i = 0; i <= ndiv; i++) {
83     printf("%g", minv + (maxv-minv)*i/ndiv);
84 greg 1.1 for (c = 0; c < ncols; c++) {
85 greg 1.4 if (pctl)
86     printf("\t%f", 100.*csum[c]/ctotal[c]);
87     else
88     printf("\t%ld", csum[c]);
89     if (i < ndiv)
90     csum[c] += histo[c][i];
91 greg 1.1 }
92     putchar('\n');
93     }
94     }
95    
96    
97 schorsch 1.2 static void
98 greg 1.4 printhisto( /* print histogram results */
99     int pctl
100     )
101 greg 1.1 {
102     register int i, c;
103    
104     for (i = 0; i < ndiv; i++) {
105     printf("%g", minv + (maxv-minv)*(i+.5)/ndiv);
106     for (c = 0; c < ncols; c++)
107 greg 1.4 if (pctl)
108     printf("\t%f", 100.*histo[c][i]/ctotal[c]);
109     else
110     printf("\t%ld", histo[c][i]);
111 greg 1.1 putchar('\n');
112     }
113     }
114 schorsch 1.2
115    
116     int
117     main(
118     int argc,
119     char *argv[]
120     )
121     {
122     progname = argv[0];
123 greg 1.4 while (argc > 1 && argv[1][0] == '-') {
124     if (argv[1][1] == 'c')
125     cumulative++;
126     else if (argv[1][1] == 'p')
127     percentile++;
128     else
129     break;
130 schorsch 1.2 argc--; argv++;
131     }
132     if (argc < 3)
133     goto userr;
134     minv = atof(argv[1]);
135     maxv = atof(argv[2]);
136     if (argc == 4)
137     ndiv = atoi(argv[3]);
138     else {
139     if (argc > 4 || !isint(minv) || !isint(maxv))
140     goto userr;
141     maxv += 0.5;
142     minv -= 0.5;
143     ndiv = maxv - minv + 0.5;
144     }
145 schorsch 1.3 if ((minv >= maxv) | (ndiv <= 0))
146 schorsch 1.2 goto userr;
147     if (ndiv > MAXDIV) {
148     fprintf(stderr, "%s: maximum number of divisions: %d\n",
149     progname, MAXDIV);
150     goto userr;
151     }
152     readinp();
153     if (cumulative)
154 greg 1.4 printcumul(percentile);
155 schorsch 1.2 else
156 greg 1.4 printhisto(percentile);
157 schorsch 1.2 exit(0);
158     userr:
159 greg 1.4 fprintf(stderr, "Usage: %s [-c][-p] min max n\n", progname);
160     fprintf(stderr, " Or: %s [-c][-p] imin imax\n", progname);
161 schorsch 1.2 exit(1);
162     }
163    
164