ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/total.c
Revision: 1.1
Committed: Sat Feb 22 02:07:20 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
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
32 main(argc, argv)
33 int argc;
34 char *argv[];
35 {
36 int status;
37 int a;
38
39 for (a = 1; a < argc; a++)
40 if (argv[a][0] == '-')
41 if (argv[a][1] >= '0' && argv[a][1] <= '9')
42 count = atoi(argv[a]+1);
43 else
44 switch (argv[a][1]) {
45 case 'm':
46 mean = !mean;
47 break;
48 case 'p':
49 func = MULT;
50 break;
51 case 's':
52 func = ADD;
53 power = atof(argv[a]+2);
54 break;
55 case 'u':
56 func = MAX;
57 break;
58 case 'l':
59 func = MIN;
60 break;
61 case 't':
62 tabc = argv[a][2];
63 break;
64 case 'r':
65 subtotal = !subtotal;
66 break;
67 default:
68 fprintf(stderr,
69 "Usage: %s [-m][-sE|p|u|l][-tC][-count [-r]] [file..]\n",
70 argv[0]);
71 exit(1);
72 }
73 else
74 break;
75 if (mean) {
76 if (func == MAX) {
77 fprintf(stderr, "%s: average maximum?!\n", argv[0]);
78 exit(1);
79 }
80 if (func == MIN) {
81 fprintf(stderr, "%s: average minimum?!\n", argv[0]);
82 exit(1);
83 }
84 }
85 status = 0;
86 if (a >= argc)
87 status = execute(NULL) == -1 ? 1 : status;
88 else
89 for ( ; a < argc; a++)
90 status = execute(argv[a]) == -1 ? 2 : status;
91 exit(status);
92 }
93
94
95 execute(fname) /* compute result */
96 char *fname;
97 {
98 double result[MAXCOL];
99 char buf[16*MAXCOL];
100 register char *cp, *num;
101 register int n;
102 double d;
103 int ncol;
104 long nlin, ltotal;
105 FILE *fp;
106 /* open file */
107 if (fname == NULL)
108 fp = stdin;
109 else if ((fp = fopen(fname, "r")) == NULL) {
110 fprintf(stderr, "%s: cannot open\n", fname);
111 return(-1);
112 }
113 ltotal = 0;
114 while (!feof(fp)) {
115 if (ltotal == 0) /* initialize */
116 if (func == MULT) /* special case */
117 for (n = 0; n < MAXCOL; n++)
118 result[n] = 0.0;
119 else
120 for (n = 0; n < MAXCOL; n++)
121 result[n] = init_val[func];
122 ncol = 0;
123 for (nlin = 0; (count <= 0 || nlin < count) &&
124 (cp = fgets(buf, sizeof(buf), fp)) != NULL;
125 nlin++) {
126 /* compute */
127 for (n = 0; n < MAXCOL; n++) {
128 while (isspace(*cp))
129 cp++;
130 if (!*cp)
131 break;
132 num = cp;
133 while (*cp && !(isspace(tabc)?isspace(*cp):*cp==tabc))
134 cp++;
135 if (*cp)
136 *cp++ = '\0';
137 if (!*num || isspace(*num))
138 d = init_val[func];
139 else
140 d = atof(num);
141 switch (func) {
142 case ADD:
143 if (d == 0.0)
144 break;
145 if (power != 0.0)
146 result[n] += pow(fabs(d),power);
147 else
148 result[n] += d;
149 break;
150 case MULT:
151 if (d == 0.0)
152 break;
153 result[n] += log(fabs(d));
154 break;
155 case MAX:
156 if (d > result[n])
157 result[n] = d;
158 break;
159 case MIN:
160 if (d < result[n])
161 result[n] = d;
162 break;
163 }
164 }
165 if (n > ncol)
166 ncol = n;
167 }
168 if (nlin == 0)
169 break;
170 /* compute and print */
171 ltotal += nlin;
172 for (n = 0; n < ncol; n++) {
173 d = result[n];
174 if (mean) {
175 d /= (double)ltotal;
176 if (func == ADD && power != 0.0 && d != 0.0)
177 d = pow(d, 1.0/power);
178 }
179 if (func == MULT)
180 d = exp(d);
181 printf("%.9g%c", d, tabc);
182 }
183 putchar('\n');
184 if (!subtotal)
185 ltotal = 0;
186 }
187 /* close file */
188 return(fclose(fp));
189 }