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.10 by greg, Tue Mar 25 05:36:22 2014 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         8192            /* maximum number of columns */
17  
# 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          }
# Line 218 | Line 231 | putrecord(                     /* write out results record */
231                  if (n) fputc(tabc, fp);
232          }
233          fputc('\n', fp);
234 +        if (!subtotal)
235 +                fflush(fp);                     /* flush unless -r */
236   }
237  
238  
# Line 261 | Line 276 | char  *fname
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 269 | Line 285 | char  *fname
285                                  case ADD:
286                                          if (inpval[n] == 0.0)
287                                                  break;
288 <                                        if (power != 0.0)
273 <                                                tally[n] += pow(fabs(inpval[n]),power);
274 <                                        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)
# Line 311 | Line 331 | char  *fname
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