/*++++++++++++++
.IDENTIFICATION usnob1-read.c
.LANGUAGE       C
.AUTHOR         Francois Ochsenbein [CDS]
.ENVIRONMENT    USNO-B1.0 Catalogue
.KEYWORDS       CDS Catalogue Server
.VERSION  1.0   07-Dec-2002: A test -- read home USNO-B1.0
.COMMENTS       Access to USNO-B1.0 catalogue
		This program reads the whole USNO-B1, and 
		keeps the TYCHO Indexes.
---------------*/

#include <usnob1.h>	/* Structure definitions */
#include <stdlib.h>	/* Structure definitions */
#include <stdio.h>	/* Structure definitions */
#include <time.h>	/* Structure definitions */

#define ITEMS(a)        (sizeof(a)/sizeof(a[0]))


typedef struct {
   int order;		/* The date of creation	*/
   int TYC1;
   short minizone;	/* Lowest USNOB zone	*/
   short maxizone;	/* Lowest USNOB zone	*/
   int maxnum;		/* The largest index	*/
   int used;
   short *usno;		/* List of USNO's	*/
} infoTYC1;

typedef int (*SORT_FCT)(/* const void *, const void * */) ;

int usnob_options = 0;
static infoTYC1 head[500];
static int nheads;
static int order = 0;

/*==================================================================
		Create a new TYC1
 *==================================================================*/

static int dif6(short *a, short *b) 
/*++++++++++++++++
.PURPOSE  Sort the table
.RETURNS  Difference
-----------------*/
{
    return(a[0] - b[0]);
}

int write_TYC1()
/*++++++++++++++++
.PURPOSE  Write the oldest infoTYC1
.RETURNS  The index in head
.REMARKS  May need to write one to file...
-----------------*/
{
  char buf[200];
  short *tab;
  int i,o,m;
  FILE *f;
    for (o=m=9999999, i=0; i<nheads; i++) {
	if (head[i].TYC1 == 0) continue;
	if (head[i].order < o) o=head[i].order, m=i;
    }
    if (m >= nheads) return(-1);
    sprintf(buf, "../TYC/%04d=%04d", head[m].TYC1, head[m].minizone);
    f = fopen(buf, "w");
    if (!f) perror(buf);

    qsort(tab=head[m].usno, head[m].used, 6, (SORT_FCT)dif6);

    /* File out the exact size */
    printf("----%s %5dstars.\n", buf, head[m].used);
    sprintf(buf, 
       "USNOB1.0-TYCHO2: TYC1=%04d, minizone=%04d, values=%d",
       head[m].TYC1, head[m].minizone, head[m].used);
    fprintf(f, "%s", buf);
    if (strlen(buf)&1) ; else fputc(' ', f);
    fputc('\n', f);
    fwrite(tab, 6, head[m].used, f);
    fclose(f);
    /* Free the allocated items */
    free(tab);
    memset(&head[m], 0, sizeof(infoTYC1));
    return(m);
}

int new_TYC1(int TYC1)
/*++++++++++++++++
.PURPOSE  Create a new TYC1
.RETURNS  The index in head
.REMARKS  May need to write one to file...
-----------------*/
{
  int m;
    if (nheads >= ITEMS(head)) 		/* Need free space */
	m = write_TYC1();
    else m = nheads++;
    head[m].order = ++order;
    head[m].TYC1  = TYC1;
    head[m].minizone = 1800;
    head[m].maxizone = -1;
    head[m].maxnum = 10000;
    head[m].usno = (short *)malloc(head[m].maxnum*6);
    memset(head[m].usno, 0, head[m].maxnum*sizeof(int));
    return(m);
}

int digest(USNOBtyc *rec)
/*++++++++++++++++
.PURPOSE  The routine which gets the records
.RETURNS  -1 = STOP
.REMARKS  Recursivity = simplicity !!
-----------------*/
{
  infoTYC1 *pm;
  short *s;
  int n,m;
    if ((rec->flags&USNOB_TYC) == 0) return(0);
    for (m=0; (m<nheads) && head[m].TYC1 != rec->TYC1; m++) ;
    if (m >= nheads) m = new_TYC1(rec->TYC1);
    pm = &head[m];
    if (pm->used >= pm->maxnum) {	/* Must realloc */
	n = pm->maxnum;
	pm->maxnum = (rec->TYC2+101)/100; 
	pm->maxnum *= 100;
	printf("....Realloc TYC1=%04d with %d\n", rec->TYC1, pm->maxnum);
	pm->usno = realloc(pm->usno, pm->maxnum*sizeof(int));
    }
    if (pm->used == 0) pm->minizone = rec->zone;
    if (rec->zone > pm->maxizone) 
	pm->maxizone = rec->zone;
    if ((pm->maxizone-pm->minizone)&(~0xff)) printf(
       "****Too large zone TYC1=%04d Uzone=(%04d-%04d)\n", 
        pm->minizone, pm->maxizone);
    
    s = pm->usno + 3*pm->used; pm->used += 1;
    s[0] = rec->TYC2;
    s[1] = (rec->TYC1-pm->minizone)<<8;
    s[1] |= rec->id>>13;
    s[2] = (rec->id<<3) | rec->TYC3;

    return(0);
}

/*==================================================================
		Main Program
 *==================================================================*/

main (argc, argv) int argc; char **argv;
{
    usnob_search((long *)0, (long *)0, digest);

    /* Close all files */
    while (write_TYC1() >= 0) ;
    usnob_close() ;
    exit(0);
}
