/* Some Statistical Analysis */
#include <stdio.h>

static int max[22], min[22];
static int rep[65];
static int bhisto[32768];

static int log2(int n)
/*++++++++++++++++
*.PURPOSE  Compute log(2)
*.RETURNS  Number between -32 and 32
*-----------------*/
{
  int r=0, m;
    if (n<0) { n = -n; m = -1; }
    else m = 1;
    while (n > 0) { r += m; n >>= 1; }
    return(r);
}

static void prstat(char *text, int *val)
/*++++++++++++++++
*.PURPOSE  Print out the statistics
*.RETURNS  ---
*-----------------*/
{
  int i, *p;
    printf("%-16s", text);
    for (p=val, i=-32; i<=32; i++, p++) {
	if (*p == 0) continue;
	printf("%3d=%-12d", i, *p);
    }
    putchar('\n');
}

main(int argc, char **argv)
{
  int rec4[11], b, k, val, n=0;
  short *ph; unsigned char *pb;
  FILE *file; 
  char *p, buf[64];
    ph = (short *)rec4;
    pb = (unsigned char *)rec4;
    for (b=0; b<=sizeof(min)/sizeof(min[0]); b++) {
	min[b] = 2000000000; 
        max[b] = -2000000000;
    }
    while (--argc > 0) {
	p = *++argv;
	fprintf(stderr, "....Opening file: %s", p);
	file = fopen(p, "r");
	if (!file) { fprintf(stderr, "****"); perror(p); continue; }
	while (fread(rec4, sizeof(rec4), 1, file)>0) {
	    for (b=18; b<=20; b++) {
	        val = ph[b];
		if (val == 30000) continue;
	        if (val<min[b]) min[b]=val; if (val>max[b]) max[b]=val;
		if (val > 0) bhisto[val] += 1;
		else bhisto[0] += 1;
	    }
	    /*
	    bhisto[val] += 1;
	    b = 32+log2(val); rep[b] += 1;
	    */
	    n++;
	}
	fprintf(stderr, "%10d\n", n);
	fclose(file);
    }
    for (b=18; b<=20; b++) printf("#%02d: (%d,%d)\n", b, min[b], max[b]);
    for (b=k=0; b<32768; b++) {
	if (bhisto[b] == 0) continue;
	if (bhisto[b]) printf("%5d=%-10d", b, bhisto[b]);
	if ((k%5) == 4) putchar('\n');
	k++;
    }
    /*
    sprintf(buf, "(%d,%d)", min, max);
    prstat(buf, rep);
    for (b=0; b<256; b++) if (bhisto[b]) printf("%3d=%-12d", b, bhisto[b]);
    putchar('\n');
    */
}
