ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/total.c
(Generate patch)

Comparing ray/src/cal/total.c (file contents):
Revision 1.6 by greg, Fri Apr 18 16:32:27 2008 UTC vs.
Revision 1.16 by greg, Thu Mar 24 22:26:50 2022 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   *      5/18/88
8   */
9  
10 #include  <stdio.h>
10   #include  <stdlib.h>
11   #include  <ctype.h>
12   #include  <math.h>
13   #include  "platform.h"
14 + #include  "rtio.h"
15  
16 < #define  MAXCOL         2048            /* maximum number of columns */
16 > #define  MAXCOL         8192            /* maximum number of columns */
17  
18   #define  ADD            0               /* add numbers */
19   #define  MULT           1               /* multiply numbers */
# Line 22 | Line 22 | static const char      RCSid[] = "$Id$";
22  
23   double  init_val[] = {0., 1., -1e12, 1e12};     /* initial values */
24  
25 + long  incnt = 0;                        /* limit number of input records? */
26 + long  outcnt = 0;                       /* limit number of output records? */
27 +
28   int  func = ADD;                        /* default function */
29   double  power = 0.0;                    /* power for sum */
30   int  mean = 0;                          /* compute mean */
# Line 30 | Line 33 | int  nbicols = 0;                      /* number of binary input columns
33   int  bocols = 0;                        /* produce binary output columns */
34   int  tabc = '\t';                       /* default separator */
35   int  subtotal = 0;                      /* produce subtotals? */
36 + int  nrecsout = 0;                      /* number of records produced */
37  
38   static int execute(char *fname);
39  
# Line 55 | Line 59 | char  *argv[]
59                                          func = MULT;
60                                          break;
61                                  case 's':
58                                        func = ADD;
62                                          power = atof(argv[a]+2);
63                                          break;
64                                  case 'u':
# Line 72 | Line 75 | char  *argv[]
75                                          break;
76                                  case 'i':
77                                          switch (argv[a][2]) {
78 +                                        case 'n':
79 +                                                incnt = atol(argv[++a]);
80 +                                                break;
81                                          case 'a':
82                                                  nbicols = 0;
83                                                  break;
# Line 105 | Line 111 | char  *argv[]
111                                          break;
112                                  case 'o':
113                                          switch (argv[a][2]) {
114 +                                        case 'n':
115 +                                                outcnt = atol(argv[++a]);
116 +                                                break;
117                                          case 'a':
118                                                  bocols = 0;
119                                                  break;
# Line 127 | Line 136 | char  *argv[]
136                                  }
137                  else
138                          break;
139 +        if ((power != 0.0) & (func != ADD)) {
140 +                fprintf(stderr, "%s: -sE option requires summation\n", argv[0]);
141 +                exit(1);
142 +        }
143          if (mean) {
144                  if (func == MAX) {
145                          fprintf(stderr, "%s: average maximum?!\n", argv[0]);
# Line 143 | Line 156 | char  *argv[]
156          if (a >= argc)
157                  status = execute(NULL) == -1 ? 1 : status;
158          else
159 <                for ( ; a < argc; a++)
159 >                for ( ; a < argc && (outcnt <= 0 || nrecsout < outcnt); a++)
160                          status = execute(argv[a]) == -1 ? 2 : status;
161          exit(status);
162   }
# Line 160 | Line 173 | getrecord(                     /* read next input record */
173          int   nf;
174                                                  /* reading binary input? */
175          if (nbicols > 0)
176 <                return(fread(field, sizeof(double), nbicols, fp));
176 >                return(getbinary(field, sizeof(double), nbicols, fp));
177          if (nbicols < 0) {
178                  float   *fbuf = (float *)buf;
179                  int     i;
180 <                nf = fread(fbuf, sizeof(float), -nbicols, fp);
180 >                nf = getbinary(fbuf, sizeof(float), -nbicols, fp);
181                  for (i = nf; i-- > 0; )
182                          field[i] = fbuf[i];
183                  return(nf);
# Line 201 | Line 214 | putrecord(                     /* write out results record */
214   {
215                                                  /* binary output? */
216          if (bocols > 0) {
217 <                fwrite(field, sizeof(double), n, fp);
217 >                putbinary(field, sizeof(double), n, fp);
218                  return;
219          }
220          if (bocols < 0) {
221                  float   fv;
222                  while (n-- > 0) {
223                          fv = *field++;
224 <                        fwrite(&fv, sizeof(float), 1, fp);
224 >                        putbinary(&fv, sizeof(float), 1, fp);
225                  }
226                  return;
227          }
228                                                  /* ASCII output */
229 <        while (n-- > 0)
230 <                fprintf(fp, "%.9g%c", *field++, tabc);
229 >        while (n-- > 0) {
230 >                fprintf(fp, "%.9g", *field++);
231 >                if (n) fputc(tabc, fp);
232 >        }
233          fputc('\n', fp);
234 +        if (!subtotal)
235 +                fflush(fp);                     /* flush unless -r */
236   }
237  
238  
# Line 226 | Line 243 | char  *fname
243   {
244          double  inpval[MAXCOL];
245          double  tally[MAXCOL];
246 +        short   rsign[MAXCOL];
247          double  result[MAXCOL];
248 <        register int  n;
248 >        int  n;
249          int  nread, ncol;
250          long  nlin, ltotal;
251          FILE  *fp;
# Line 240 | Line 258 | char  *fname
258          }
259          if (nbicols)
260                  SET_FILE_BINARY(fp);
261 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
262 +        flockfile(fp);
263 + #endif
264 +
265          ltotal = 0;
266          while (!feof(fp)) {
267                  if (ltotal == 0) {                      /* initialize */
268                          if (func == MULT)       /* special case */
269 <                                for (n = 0; n < MAXCOL; n++)
269 >                                for (n = 0; n < MAXCOL; n++) {
270                                          tally[n] = 0.0;
271 +                                        rsign[n] = 1;
272 +                                }
273                          else
274                                  for (n = 0; n < MAXCOL; n++)
275                                          tally[n] = init_val[func];
276                  }
277                  ncol = 0;
278                  for (nlin = 0; (count <= 0 || nlin < count) &&
279 +                                (incnt <= 0 || nlin < incnt) &&
280                                  (nread = getrecord(inpval, fp)) > 0;
281                                  nlin++) {
282                                                          /* compute */
# Line 260 | Line 285 | char  *fname
285                                  case ADD:
286                                          if (inpval[n] == 0.0)
287                                                  break;
288 <                                        if (power != 0.0)
264 <                                                tally[n] += pow(fabs(inpval[n]),power);
265 <                                        else
288 >                                        if (power == 0.0)
289                                                  tally[n] += inpval[n];
290 +                                        else if (power == 1.0)
291 +                                                tally[n] += fabs(inpval[n]);
292 +                                        else if (power == -1.0)
293 +                                                tally[n] += 1.0/fabs(inpval[n]);
294 +                                        else
295 +                                                tally[n] += pow(fabs(inpval[n]), power);
296                                          break;
297                                  case MULT:
298                                          if (inpval[n] == 0.0)
299 <                                                break;
300 <                                        tally[n] += log(fabs(inpval[n]));
299 >                                                rsign[n] = 0;
300 >                                        else if (inpval[n] < 0.0) {
301 >                                                rsign[n] = -rsign[n];
302 >                                                inpval[n] = -inpval[n];
303 >                                        }
304 >                                        if (rsign[n])
305 >                                                tally[n] += log(inpval[n]);
306                                          break;
307                                  case MAX:
308                                          if (inpval[n] > tally[n])
# Line 294 | Line 328 | char  *fname
328                                          result[n] = pow(result[n], 1.0/power);
329                          }
330                          if (func == MULT)
331 <                                result[n] = exp(result[n]);
331 >                                result[n] = rsign[n] * exp(result[n]);
332                  }
333                  putrecord(result, ncol, stdout);
334 +                ++nrecsout;
335 +                if (outcnt > 0 && nrecsout >= outcnt)
336 +                        break;
337                  if (!subtotal)
338                          ltotal = 0;
339 +                if (incnt > 0 && nlin >= incnt)
340 +                        break;
341          }
342                                                          /* close input */
343 <        return(fclose(fp));
343 >        return(fclose(fp) == EOF ? 1 : 0);
344   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines