ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/histo.c
Revision: 1.5
Committed: Fri Apr 18 16:32:27 2008 UTC (16 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R0, rad3R9, rad4R2P1, rad5R3
Changes since 1.4: +2 -2 lines
Log Message:
Changed maximum number of input columns to more generous 2K

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: histo.c,v 1.4 2003/12/14 16:33:37 greg Exp $";
3 #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 #include <string.h>
13 #include <math.h>
14 #include <ctype.h>
15
16 #define MAXCOL 2048 /* 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 int percentile = 0;
25
26 long outrange[MAXCOL][2];
27 long histo[MAXCOL][MAXDIV];
28 long ctotal[MAXCOL];
29 double minv, maxv;
30 int ndiv;
31
32 int ncols;
33
34
35 static void
36 readinp(void) /* gather statistics on input */
37 {
38 char buf[16*MAXCOL];
39 double d;
40 int i;
41 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 if (d <= minv)
54 outrange[c][0]++;
55 else if (d >= maxv)
56 outrange[c][1]++;
57 else
58 histo[c][(int)(ndiv*(d-minv)/(maxv-minv))]++;
59 }
60 if (c > ncols)
61 ncols = c;
62 }
63 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 }
69
70
71 static void
72 printcumul( /* print cumulative histogram results */
73 int pctl
74 )
75 {
76 long csum[MAXCOL];
77 register int i, c;
78
79 for (c = ncols; c--; )
80 csum[c] = outrange[c][0];
81
82 for (i = 0; i <= ndiv; i++) {
83 printf("%g", minv + (maxv-minv)*i/ndiv);
84 for (c = 0; c < ncols; c++) {
85 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 }
92 putchar('\n');
93 }
94 }
95
96
97 static void
98 printhisto( /* print histogram results */
99 int pctl
100 )
101 {
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 if (pctl)
108 printf("\t%f", 100.*histo[c][i]/ctotal[c]);
109 else
110 printf("\t%ld", histo[c][i]);
111 putchar('\n');
112 }
113 }
114
115
116 int
117 main(
118 int argc,
119 char *argv[]
120 )
121 {
122 progname = argv[0];
123 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 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 if ((minv >= maxv) | (ndiv <= 0))
146 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 printcumul(percentile);
155 else
156 printhisto(percentile);
157 exit(0);
158 userr:
159 fprintf(stderr, "Usage: %s [-c][-p] min max n\n", progname);
160 fprintf(stderr, " Or: %s [-c][-p] imin imax\n", progname);
161 exit(1);
162 }
163
164