ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/total.c
Revision: 1.3
Committed: Mon Jul 21 22:30:17 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 1.2: +3 -2 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: total.c,v 1.2 2003/06/08 12:03:09 schorsch Exp $";
3 #endif
4 /*
5 * total.c - program to reduce columns of data.
6 *
7 * 5/18/88
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <math.h>
14
15 #define MAXCOL 256 /* maximum number of columns */
16
17 #define ADD 0 /* add numbers */
18 #define MULT 1 /* multiply numbers */
19 #define MAX 2 /* maximum */
20 #define MIN 3 /* minimum */
21
22 double init_val[] = {0., 1., -1e12, 1e12}; /* initial values */
23
24 int func = ADD; /* default function */
25 double power = 0.0; /* power for sum */
26 int mean = 0; /* compute mean */
27 int count = 0; /* number to sum */
28 int tabc = '\t'; /* default separator */
29 int subtotal = 0; /* produce subtotals? */
30
31 static int execute(char *fname);
32
33 int
34 main(
35 int argc,
36 char *argv[]
37 )
38 {
39 int status;
40 int a;
41
42 for (a = 1; a < argc; a++)
43 if (argv[a][0] == '-')
44 if (argv[a][1] >= '0' && argv[a][1] <= '9')
45 count = atoi(argv[a]+1);
46 else
47 switch (argv[a][1]) {
48 case 'm':
49 mean = !mean;
50 break;
51 case 'p':
52 func = MULT;
53 break;
54 case 's':
55 func = ADD;
56 power = atof(argv[a]+2);
57 break;
58 case 'u':
59 func = MAX;
60 break;
61 case 'l':
62 func = MIN;
63 break;
64 case 't':
65 tabc = argv[a][2];
66 break;
67 case 'r':
68 subtotal = !subtotal;
69 break;
70 default:
71 fprintf(stderr,
72 "Usage: %s [-m][-sE|p|u|l][-tC][-count [-r]] [file..]\n",
73 argv[0]);
74 exit(1);
75 }
76 else
77 break;
78 if (mean) {
79 if (func == MAX) {
80 fprintf(stderr, "%s: average maximum?!\n", argv[0]);
81 exit(1);
82 }
83 if (func == MIN) {
84 fprintf(stderr, "%s: average minimum?!\n", argv[0]);
85 exit(1);
86 }
87 }
88 status = 0;
89 if (a >= argc)
90 status = execute(NULL) == -1 ? 1 : status;
91 else
92 for ( ; a < argc; a++)
93 status = execute(argv[a]) == -1 ? 2 : status;
94 exit(status);
95 }
96
97
98 static int
99 execute( /* compute result */
100 char *fname
101 )
102 {
103 double result[MAXCOL];
104 char buf[16*MAXCOL];
105 register char *cp, *num;
106 register int n;
107 double d;
108 int ncol;
109 long nlin, ltotal;
110 FILE *fp;
111 /* open file */
112 if (fname == NULL)
113 fp = stdin;
114 else if ((fp = fopen(fname, "r")) == NULL) {
115 fprintf(stderr, "%s: cannot open\n", fname);
116 return(-1);
117 }
118 ltotal = 0;
119 while (!feof(fp)) {
120 if (ltotal == 0) { /* initialize */
121 if (func == MULT) /* special case */
122 for (n = 0; n < MAXCOL; n++)
123 result[n] = 0.0;
124 else
125 for (n = 0; n < MAXCOL; n++)
126 result[n] = init_val[func];
127 }
128 ncol = 0;
129 for (nlin = 0; (count <= 0 || nlin < count) &&
130 (cp = fgets(buf, sizeof(buf), fp)) != NULL;
131 nlin++) {
132 /* compute */
133 for (n = 0; n < MAXCOL; n++) {
134 while (isspace(*cp))
135 cp++;
136 if (!*cp)
137 break;
138 num = cp;
139 while (*cp && !(isspace(tabc)?isspace(*cp):*cp==tabc))
140 cp++;
141 if (*cp)
142 *cp++ = '\0';
143 if (!*num || isspace(*num))
144 d = init_val[func];
145 else
146 d = atof(num);
147 switch (func) {
148 case ADD:
149 if (d == 0.0)
150 break;
151 if (power != 0.0)
152 result[n] += pow(fabs(d),power);
153 else
154 result[n] += d;
155 break;
156 case MULT:
157 if (d == 0.0)
158 break;
159 result[n] += log(fabs(d));
160 break;
161 case MAX:
162 if (d > result[n])
163 result[n] = d;
164 break;
165 case MIN:
166 if (d < result[n])
167 result[n] = d;
168 break;
169 }
170 }
171 if (n > ncol)
172 ncol = n;
173 }
174 if (nlin == 0)
175 break;
176 /* compute and print */
177 ltotal += nlin;
178 for (n = 0; n < ncol; n++) {
179 d = result[n];
180 if (mean) {
181 d /= (double)ltotal;
182 if (func == ADD && power != 0.0 && d != 0.0)
183 d = pow(d, 1.0/power);
184 }
185 if (func == MULT)
186 d = exp(d);
187 printf("%.9g%c", d, tabc);
188 }
189 putchar('\n');
190 if (!subtotal)
191 ltotal = 0;
192 }
193 /* close file */
194 return(fclose(fp));
195 }