# include "uniphot.h"

ZptTable *load_zpt_table (char *filename, int *nzpts) {

  int Nzpts, NZPTS;
  ZptTable *zpts;
  double zpt, mjd;

  FILE *f;

  f = fopen (filename, "r");
  if (!f) {
    fprintf (stderr, "ERROR: cannot open zpt table file %s\n", filename);
    exit (1);
  }

  Nzpts = 0;
  NZPTS = 100;
  ALLOCATE (zpts, ZptTable, NZPTS);

  // format is fixed: (time) (zpt)
  int status;
  while ((status = fscanf (f, "%lf %lf", &mjd, &zpt)) == 2) {
    zpts[Nzpts].zpt = zpt;
    zpts[Nzpts].time = ohana_mjd_to_sec (mjd);
    zpts[Nzpts].found = FALSE;

    Nzpts ++;
    CHECK_REALLOCATE (zpts, ZptTable, NZPTS, Nzpts, 100);
  }

  if (status != EOF) {
    fprintf (stderr, "unexpected formatting on line %d\n", Nzpts);
    exit (2);
  }

  fprintf (stderr, "loaded %d zero points\n", Nzpts);

  *nzpts = Nzpts;
  return zpts;
}
