/*++++++++++++++
.IDENTIFICATION pmm12.c
.LANGUAGE       C
.AUTHOR         Francois Ochsenbein [CDS]
.ENVIRONMENT    USNO-A2.0 Catalogue
.KEYWORDS       CDS Catalogue Server
.VERSION  1.0   01-Mar-1997
.VERSION  2.0   20-Oct-1998: USNO-A2.0
.COMMENTS       List one of the files containing the 488,000,000 stars.
---------------*/

#include <stdio.h>
#include <stdlib.h>	/* Malloc   */
#include <string.h>
#include <ctype.h>
#include <fcntl.h>	/* O_BINARY */
#include <math.h>

#ifndef O_BINARY	/* Required for MS-DOS	*/
#define O_BINARY 0
#endif
#include <stdio.h>

static char verbop = 0;	/* Verbose Option	*/

static char usage[] = "\
Usage: pmm12 input_zone\n\
";

static char help[] = "\
   input: a \"zoneXXXX.cat\" original file\n\
";

/*==================================================================
		Main Program
 *==================================================================*/
main (argc, argv) int argc; char **argv;
{
  long in[3], id, mags ;
  long b[1000], r[1000] ; 
  long bbb[1000], rrr[1000] ; 
  FILE *f ;
  int i ;

    if (argc < 2) {
	fprintf(stderr, "%s%s", usage, help) ;
	exit(1) ;
    }

    memset(bbb, 0, sizeof(bbb)) ;
    memset(rrr, 0, sizeof(rrr)) ;
    while (--argc > 0) {
	fprintf(stderr, "----Dealing with file: %s", *++argv) ;
        if (!(f = fopen(*argv, "rb"))) {
	    perror(*argv) ;
	    exit(1) ;
	}
	fflush(stderr) ;
	memset(b, 0, sizeof(b)) ;
	memset(r, 0, sizeof(r)) ;

        for (id=0; fread(in, sizeof(long), 3, f) > 0 ; ) {
	    id++ ;
	    /* printf("%08ld: RA=%09ld SD=%08ld MG=%11ld\n", id, 
    	    in[0], in[1], in[2] ) ;
	    */
	    mags = in[2] < 0 ? -in[2] : in[2] ;
	    r[mags%1000] += 1 ;
	    mags /= 1000 ;
	    b[mags%1000] += 1 ;
	}
	fputc('\n', stderr) ;
	close(f) ;
	fprintf(stderr, "==== %ld stars. Histos Bmag, Rmag:\n", id) ;
	for (i=0; i<1000; i++)  {
	    if ((b[i] == 0) && (r[i] == 0)) continue ;
	    bbb[i] += b[i] ; rrr[i] += r[i] ;
	    fprintf(stderr, "%8d %10d %10d\n", i, b[i], r[i]) ;
	}

    }

    fprintf(stderr, "==== Total Histo Bmag, Rmag:\n") ;
    for (i=0; i<1000; i++)  {
	if ((b[i] == 0) && (r[i] == 0)) continue ;
	fprintf(stderr, "%8d %10d %10d\n", i, bbb[i], rrr[i]) ;
    }

}
