Back to index

radiance  4R0+20100331
Defines | Functions | Variables
total.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include "platform.h"

Go to the source code of this file.

Defines

#define MAXCOL   2048 /* maximum number of columns */
#define ADD   0 /* add numbers */
#define MULT   1 /* multiply numbers */
#define MAX   2 /* maximum */
#define MIN   3 /* minimum */

Functions

static int execute (char *fname)
int main (int argc, char *argv[])
static int getrecord (double field[MAXCOL], FILE *fp)
static void putrecord (const double *field, int n, FILE *fp)

Variables

static const char RCSid [] = "$Id: total.c,v 1.7 2010/01/29 17:21:43 greg Exp $"
double init_val [] = {0., 1., -1e12, 1e12}
int func = ADD
double power = 0.0
int mean = 0
int count = 0
int nbicols = 0
int bocols = 0
int tabc = '\t'
int subtotal = 0

Define Documentation

#define ADD   0 /* add numbers */

Definition at line 18 of file total.c.

#define MAX   2 /* maximum */

Definition at line 20 of file total.c.

#define MAXCOL   2048 /* maximum number of columns */

Definition at line 16 of file total.c.

#define MIN   3 /* minimum */

Definition at line 21 of file total.c.

#define MULT   1 /* multiply numbers */

Definition at line 19 of file total.c.


Function Documentation

static int execute ( char *  fname) [static]

Definition at line 223 of file total.c.

{
       double inpval[MAXCOL];
       double tally[MAXCOL];
       short  rsign[MAXCOL];
       double  result[MAXCOL];
       int  n;
       int  nread, ncol;
       long  nlin, ltotal;
       FILE  *fp;
                                                 /* open file */
       if (fname == NULL)
              fp = stdin;
       else if ((fp = fopen(fname, "r")) == NULL) {
              fprintf(stderr, "%s: cannot open\n", fname);
              return(-1);
       }
       if (nbicols)
              SET_FILE_BINARY(fp);
       ltotal = 0;
       while (!feof(fp)) {
              if (ltotal == 0) {                 /* initialize */
                     if (func == MULT)    /* special case */
                            for (n = 0; n < MAXCOL; n++) {
                                   tally[n] = 0.0;
                                   rsign[n] = 1;
                            }
                     else
                            for (n = 0; n < MAXCOL; n++)
                                   tally[n] = init_val[func];
              }
              ncol = 0;
              for (nlin = 0; (count <= 0 || nlin < count) &&
                            (nread = getrecord(inpval, fp)) > 0;
                            nlin++) {
                                                 /* compute */
                     for (n = 0; n < nread; n++)
                            switch (func) {
                            case ADD:
                                   if (inpval[n] == 0.0)
                                          break;
                                   if (power != 0.0)
                                          tally[n] += pow(fabs(inpval[n]),power);
                                   else
                                          tally[n] += inpval[n];
                                   break;
                            case MULT:
                                   if (inpval[n] == 0.0)
                                          rsign[n] = 0;
                                   else if (inpval[n] < 0.0) {
                                          rsign[n] = -rsign[n];
                                          inpval[n] = -inpval[n];
                                   }
                                   if (rsign[n])
                                          tally[n] += log(inpval[n]);
                                   break;
                            case MAX:
                                   if (inpval[n] > tally[n])
                                          tally[n] = inpval[n];
                                   break;
                            case MIN:
                                   if (inpval[n] < tally[n])
                                          tally[n] = inpval[n];
                                   break;
                            }
                     if (nread > ncol)
                            ncol = nread;
              }
              if (nlin == 0)
                     break;
                                          /* compute and print */
              ltotal += nlin;
              for (n = 0; n < ncol; n++) {
                     result[n] = tally[n];
                     if (mean) {
                            result[n] /= (double)ltotal;
                            if (func == ADD && power != 0.0 && result[n] != 0.0)
                                   result[n] = pow(result[n], 1.0/power);
                     }
                     if (func == MULT)
                            result[n] = rsign[n] * exp(result[n]);
              }
              putrecord(result, ncol, stdout);
              if (!subtotal)
                     ltotal = 0;
       }
                                                 /* close input */
       return(fclose(fp));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int getrecord ( double  field[MAXCOL],
FILE *  fp 
) [static]

Definition at line 153 of file total.c.

{
       char  buf[16*MAXCOL];
       char  *cp, *num;
       int   nf;
                                          /* reading binary input? */
       if (nbicols > 0)
              return(fread(field, sizeof(double), nbicols, fp));
       if (nbicols < 0) {
              float  *fbuf = (float *)buf;
              int    i;
              nf = fread(fbuf, sizeof(float), -nbicols, fp);
              for (i = nf; i-- > 0; )
                     field[i] = fbuf[i];
              return(nf);
       }
                                          /* reading ASCII input */
       cp = fgets(buf, sizeof(buf), fp);
       if (cp == NULL)
              return(0);
       for (nf = 0; nf < MAXCOL; nf++) {
              while (isspace(*cp))
                     cp++;
              if (!*cp)
                     break;
              num = cp;
              while (*cp && !(isspace(tabc)?isspace(*cp):*cp==tabc))
                     cp++;
              if (*cp)
                     *cp++ = '\0';
              if (!*num || isspace(*num))
                     field[nf] = init_val[func];
              else
                     field[nf] = atof(num);
       }
       return(nf);
}

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 37 of file total.c.

{
       int  status;
       int  a;

       for (a = 1; a < argc; a++)
              if (argv[a][0] == '-')
                     if (argv[a][1] >= '0' && argv[a][1] <= '9')
                            count = atoi(argv[a]+1);
                     else
                            switch (argv[a][1]) {
                            case 'm':
                                   mean = !mean;
                                   break;
                            case 'p':
                                   func = MULT;
                                   break;
                            case 's':
                                   func = ADD;
                                   power = atof(argv[a]+2);
                                   break;
                            case 'u':
                                   func = MAX;
                                   break;
                            case 'l':
                                   func = MIN;
                                   break;
                            case 't':
                                   tabc = argv[a][2];
                                   break;
                            case 'r':
                                   subtotal = !subtotal;
                                   break;
                            case 'i':
                                   switch (argv[a][2]) {
                                   case 'a':
                                          nbicols = 0;
                                          break;
                                   case 'd':
                                          if (isdigit(argv[a][3]))
                                                 nbicols = atoi(argv[a]+3);
                                          else
                                                 nbicols = 1;
                                          if (nbicols > MAXCOL) {
                                                 fprintf(stderr,
                                                 "%s: too many input columns\n",
                                                        argv[0]);
                                                 exit(1);
                                          }
                                          break;
                                   case 'f':
                                          if (isdigit(argv[a][3]))
                                                 nbicols = -atoi(argv[a]+3);
                                          else
                                                 nbicols = -1;
                                          if (-nbicols > MAXCOL) {
                                                 fprintf(stderr,
                                                 "%s: too many input columns\n",
                                                        argv[0]);
                                                 exit(1);
                                          }
                                          break;
                                   default:
                                          goto userr;
                                   }
                                   break;
                            case 'o':
                                   switch (argv[a][2]) {
                                   case 'a':
                                          bocols = 0;
                                          break;
                                   case 'd':
                                          bocols = 1;
                                          break;
                                   case 'f':
                                          bocols = -1;
                                          break;
                                   default:
                                          goto userr;
                                   }
                                   break;
                            default:;
                            userr:
                                   fprintf(stderr,
"Usage: %s [-m][-sE|p|u|l][-tC][-i{f|d}[N]][-o{f|d}][-count [-r]] [file..]\n",
                                                 argv[0]);
                                   exit(1);
                            }
              else
                     break;
       if (mean) {
              if (func == MAX) {
                     fprintf(stderr, "%s: average maximum?!\n", argv[0]);
                     exit(1);
              }
              if (func == MIN) {
                     fprintf(stderr, "%s: average minimum?!\n", argv[0]);
                     exit(1);
              }
       }
       if (bocols)
              SET_FILE_BINARY(stdout);
       status = 0;
       if (a >= argc)
              status = execute(NULL) == -1 ? 1 : status;
       else
              for ( ; a < argc; a++)
                     status = execute(argv[a]) == -1 ? 2 : status;
       exit(status);
}

Here is the call graph for this function:

static void putrecord ( const double *  field,
int  n,
FILE *  fp 
) [static]

Definition at line 196 of file total.c.

{
                                          /* binary output? */
       if (bocols > 0) {
              fwrite(field, sizeof(double), n, fp);
              return;
       }
       if (bocols < 0) {
              float  fv;
              while (n-- > 0) {
                     fv = *field++;
                     fwrite(&fv, sizeof(float), 1, fp);
              }
              return;
       }
                                          /* ASCII output */
       while (n-- > 0)
              fprintf(fp, "%.9g%c", *field++, tabc);
       fputc('\n', fp);
}

Here is the caller graph for this function:


Variable Documentation

int bocols = 0

Definition at line 30 of file total.c.

int count = 0

Definition at line 28 of file total.c.

int func = ADD

Definition at line 25 of file total.c.

double init_val[] = {0., 1., -1e12, 1e12}

Definition at line 23 of file total.c.

int mean = 0

Definition at line 27 of file total.c.

int nbicols = 0

Definition at line 29 of file total.c.

double power = 0.0

Definition at line 26 of file total.c.

const char RCSid[] = "$Id: total.c,v 1.7 2010/01/29 17:21:43 greg Exp $" [static]

Definition at line 2 of file total.c.

int subtotal = 0

Definition at line 32 of file total.c.

int tabc = '\t'

Definition at line 31 of file total.c.