# include "addusno.h"

gcatalog (catname, catalog)
char *catname;
Catalog catalog[];
{
  
  char filename[128];
  int Nitems, nitems;
  int i, Nmeas, Nmiss, done;
  FILE *f;
  struct tm *local;
  struct timeval now;


  sprintf (filename, "%s/%s\0", CATDIR, catname);

  /* read catalog header */
  if (!fits_read_header (filename, &catalog[0].header)) {
    fprintf (stderr, "ERROR: can't open catalog file: %s\n", filename);
    exit (0);
  }

  f = fopen (filename, "r");
  if (f == (FILE *) NULL) {
    fprintf (stderr, "ERROR: can't open catalog file: %s\n", filename);
    exit (0);
  }
  fseek (f, catalog[0].header.size, SEEK_SET); 

  /** find number of stars, measurements **/
  catalog[0].Nmissing = catalog[0].Naverage = catalog[0].Nmeasure = 0;
  fits_scan (&catalog[0].header, "NSTARS", "%d", 1, &catalog[0].Naverage);
  fits_scan (&catalog[0].header, "NMEAS", "%d", 1, &catalog[0].Nmeasure);
  fits_scan (&catalog[0].header, "NMISS", "%d", 1, &catalog[0].Nmissing);
  ALLOCATE (catalog[0].average, Average, MAX (catalog[0].Naverage, 1));
  ALLOCATE (catalog[0].measure, Measure, MAX (catalog[0].Nmeasure, 1));
  ALLOCATE (catalog[0].missing, Missing, MAX (catalog[0].Nmissing, 1));

  done = FALSE;
  fits_scan (&catalog[0].header, "ADDUSNO", "%t", 1, &done);
  if (done && !FORCE_RUN) {
    if (VERBOSE) fprintf (stderr, "SUCCESS: file already up-to-date %s\n", filename);
    fclose (f);
    clear_lockfile ();
    exit (0);
  }

  if (catalog[0].Naverage == 0) {
    if (VERBOSE) fprintf (stderr, "no stars yet in catalog %s\n", filename);
    clear_lockfile ();
    fclose (f);
    exit (0);
  }

  /* read average values */
  Nitems = catalog[0].Naverage;
  nitems = Fread (catalog[0].average, sizeof(Average), Nitems, f, "average");
  if (nitems != Nitems) {
    fprintf (stderr, "ERROR: failed to read data from catalog file %s (1)\n", filename);
    fclose (f);
    exit (0);
  }
  
  /* read measurements */
  Nitems = catalog[0].Nmeasure;
  nitems = Fread (catalog[0].measure, sizeof(Measure), Nitems, f, "measure");
  if (nitems != Nitems) {
    fprintf (stderr, "ERROR: failed to read data from catalog file %s (2)\n", filename);
    fclose (f);
    exit (0);
  }
  
  /* read missing */
  Nitems = catalog[0].Nmissing;
  nitems = Fread (catalog[0].missing, sizeof(Missing), Nitems, f, "missing");
  if (nitems != Nitems) {
    fprintf (stderr, "ERROR: failed to read data from catalog file %s (3)\n", filename);
    fclose (f);
    exit (0);
  }
  
  if (VERBOSE) fprintf (stderr, "read %d stars from catalog file %s (%d measurements, %d missing)\n", 
	   catalog[0].Naverage, filename, catalog[0].Nmeasure, catalog[0].Nmissing);

  for (i = Nmeas = Nmiss = 0; i < catalog[0].Naverage; i++) {
    Nmeas += catalog[0].average[i].Nm; 
    Nmiss += catalog[0].average[i].Nn; 
  }
  if ((Nmeas != catalog[0].Nmeasure) || (Nmiss != catalog[0].Nmissing)) {
    fprintf (stderr, "ERROR: data in catalog %s is corrupt, sums don't check\n");
    fprintf (stderr, "ERROR: Nmeas: %d, %d\n", Nmeas, catalog[0].Nmeasure);
    fprintf (stderr, "ERROR: Nmiss: %d, %d\n", Nmiss, catalog[0].Nmissing);
    exit (0);
  }

  fclose (f);

}

