# include "addstar.h"
# include "ohana.h"

main (int argc, char **argv) {
  
  Catalog catalog;
  char filename[64], line[64];
  int Nitems, nitems;
  int i, Nmeas, Nmiss;
  FILE *f;
  struct tm *local;
  struct timeval now;

  if (argc != 2) {
    fprintf (stderr, "USAGE: testcat (catname)\n");
    exit (1);
  }

  /* read catalog header */
  if (!fits_read_header (argv[1], &catalog.header)) {
    fprintf (stderr, "can't find file %s\n", argv[1]);
    exit (1);
  } else {
    strcpy (filename, argv[1]);
  }

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

  /** find number of stars, measurements **/
  catalog.Nmissing = catalog.Naverage = catalog.Nmeasure = 0;
  fits_scan (&catalog.header, "NSTARS", "%d", 1, &catalog.Naverage);
  fits_scan (&catalog.header, "NMEAS", "%d", 1, &catalog.Nmeasure);
  fits_scan (&catalog.header, "NMISS", "%d", 1, &catalog.Nmissing);
  fprintf (stderr, "loading data for %d %d %d\n", catalog.Naverage, catalog.Nmeasure, catalog.Nmissing);
  ALLOCATE (catalog.average, Average, MAX (catalog.Naverage, 1));
  ALLOCATE (catalog.measure, Measure, MAX (catalog.Nmeasure, 1));
  ALLOCATE (catalog.missing, Missing, MAX (catalog.Nmissing, 1));
  if (catalog.Naverage == 0) {
    fprintf (stderr, "no stars yet in catalog %s\n", argv[1]);
    fclose (f);
    exit (0);
  }

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

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

  fclose (f);

  fprintf (stderr, "catalog %s looks ok\n", argv[1]);
  exit (0);

}

