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.4 by greg, Thu Jun 2 04:47:27 2005 UTC vs.
Revision 1.13 by greg, Thu Aug 18 00:52:47 2016 UTC

# Line 12 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include  <ctype.h>
13   #include  <math.h>
14   #include  "platform.h"
15 + #include  "rtio.h"
16  
17 < #define  MAXCOL         256             /* maximum number of columns */
17 > #define  MAXCOL         8192            /* maximum number of columns */
18  
19   #define  ADD            0               /* add numbers */
20   #define  MULT           1               /* multiply numbers */
# Line 22 | Line 23 | static const char      RCSid[] = "$Id$";
23  
24   double  init_val[] = {0., 1., -1e12, 1e12};     /* initial values */
25  
26 + long  incnt = 0;                        /* limit number of input records? */
27 + long  outcnt = 0;                       /* limit number of output records? */
28 +
29   int  func = ADD;                        /* default function */
30   double  power = 0.0;                    /* power for sum */
31   int  mean = 0;                          /* compute mean */
# Line 30 | Line 34 | int  nbicols = 0;                      /* number of binary input columns
34   int  bocols = 0;                        /* produce binary output columns */
35   int  tabc = '\t';                       /* default separator */
36   int  subtotal = 0;                      /* produce subtotals? */
37 + int  nrecsout = 0;                      /* number of records produced */
38  
39   static int execute(char *fname);
40  
# Line 72 | Line 77 | char  *argv[]
77                                          break;
78                                  case 'i':
79                                          switch (argv[a][2]) {
80 +                                        case 'n':
81 +                                                incnt = atol(argv[++a]);
82 +                                                break;
83                                          case 'a':
84                                                  nbicols = 0;
85                                                  break;
# Line 105 | Line 113 | char  *argv[]
113                                          break;
114                                  case 'o':
115                                          switch (argv[a][2]) {
116 +                                        case 'n':
117 +                                                outcnt = atol(argv[++a]);
118 +                                                break;
119                                          case 'a':
120                                                  bocols = 0;
121                                                  break;
# Line 143 | Line 154 | char  *argv[]
154          if (a >= argc)
155                  status = execute(NULL) == -1 ? 1 : status;
156          else
157 <                for ( ; a < argc; a++)
157 >                for ( ; a < argc && (outcnt <= 0 || nrecsout < outcnt); a++)
158                          status = execute(argv[a]) == -1 ? 2 : status;
159          exit(status);
160   }
# Line 160 | Line 171 | getrecord(                     /* read next input record */
171          int   nf;
172                                                  /* reading binary input? */
173          if (nbicols > 0)
174 <                return(fread(field, sizeof(double), nbicols, fp));
174 >                return(getbinary(field, sizeof(double), nbicols, fp));
175          if (nbicols < 0) {
176                  float   *fbuf = (float *)buf;
177                  int     i;
178 <                nf = fread(fbuf, sizeof(float), -nbicols, fp);
178 >                nf = getbinary(fbuf, sizeof(float), -nbicols, fp);
179                  for (i = nf; i-- > 0; )
180                          field[i] = fbuf[i];
181                  return(nf);
# Line 201 | Line 212 | putrecord(                     /* write out results record */
212   {
213                                                  /* binary output? */
214          if (bocols > 0) {
215 <                fwrite(field, sizeof(double), n, fp);
215 >                putbinary(field, sizeof(double), n, fp);
216                  return;
217          }
218          if (bocols < 0) {
219                  float   fv;
220                  while (n-- > 0) {
221                          fv = *field++;
222 <                        fwrite(&fv, sizeof(float), 1, fp);
222 >                        putbinary(&fv, sizeof(float), 1, fp);
223                  }
224                  return;
225          }
226                                                  /* ASCII output */
227 <        while (n-- > 0)
228 <                fprintf(fp, "%.9g%c", *field++, tabc);
227 >        while (n-- > 0) {
228 >                fprintf(fp, "%.9g", *field++);
229 >                if (n) fputc(tabc, fp);
230 >        }
231          fputc('\n', fp);
232   }
233  
# Line 226 | Line 239 | char  *fname
239   {
240          double  inpval[MAXCOL];
241          double  tally[MAXCOL];
242 +        short   rsign[MAXCOL];
243          double  result[MAXCOL];
244 <        register int  n;
244 >        int  n;
245          int  nread, ncol;
246          long  nlin, ltotal;
247          FILE  *fp;
# Line 240 | Line 254 | char  *fname
254          }
255          if (nbicols)
256                  SET_FILE_BINARY(fp);
257 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
258 +        flockfile(fp);
259 + #endif
260 +
261          ltotal = 0;
262          while (!feof(fp)) {
263                  if (ltotal == 0) {                      /* initialize */
264                          if (func == MULT)       /* special case */
265 <                                for (n = 0; n < MAXCOL; n++)
265 >                                for (n = 0; n < MAXCOL; n++) {
266                                          tally[n] = 0.0;
267 +                                        rsign[n] = 1;
268 +                                }
269                          else
270                                  for (n = 0; n < MAXCOL; n++)
271                                          tally[n] = init_val[func];
272                  }
273                  ncol = 0;
274                  for (nlin = 0; (count <= 0 || nlin < count) &&
275 +                                (incnt <= 0 || nlin < incnt) &&
276                                  (nread = getrecord(inpval, fp)) > 0;
277                                  nlin++) {
278                                                          /* compute */
# Line 267 | Line 288 | char  *fname
288                                          break;
289                                  case MULT:
290                                          if (inpval[n] == 0.0)
291 <                                                break;
292 <                                        tally[n] += log(fabs(inpval[n]));
291 >                                                rsign[n] = 0;
292 >                                        else if (inpval[n] < 0.0) {
293 >                                                rsign[n] = -rsign[n];
294 >                                                inpval[n] = -inpval[n];
295 >                                        }
296 >                                        if (rsign[n])
297 >                                                tally[n] += log(inpval[n]);
298                                          break;
299                                  case MAX:
300                                          if (inpval[n] > tally[n])
# Line 294 | Line 320 | char  *fname
320                                          result[n] = pow(result[n], 1.0/power);
321                          }
322                          if (func == MULT)
323 <                                result[n] = exp(tally[n]);
323 >                                result[n] = rsign[n] * exp(result[n]);
324                  }
325                  putrecord(result, ncol, stdout);
326 +                ++nrecsout;
327 +                if (outcnt > 0 && nrecsout >= outcnt)
328 +                        break;
329                  if (!subtotal)
330                          ltotal = 0;
331 +                if (incnt > 0 && nlin >= incnt)
332 +                        break;
333          }
334                                                          /* close input */
335          return(fclose(fp));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines