# include "addstar.h"
# define D_NSTARS 1000
# define BYTES_STAR 31
# define BLOCK 1000
#include <sys/time.h>
#include <time.h>

Stars *gstars (file, NSTARS, image) 
char file[];
int *NSTARS;
Image *image;
{

  FILE *f;
  Header header;
  int i, j, nstar;
  int nbytes, Nbytes;
  Stars *stars;
  char *buffer, line[64], *c;
  struct tm timeptr;
  double tmp;

  if (!fits_read_header (file, &header)) {
    fprintf (stderr, "ERROR: can't find image file %s\n", file);
    clear_lockfile ();
    exit(0);
  }

  /* open file */
  f = fopen (file, "r");
  if (f == NULL) {
    fprintf (stderr, "ERROR: can't find data file %s\n", file);
    clear_lockfile ();
    exit(0);
  }
  fseek (f, header.size, SEEK_SET); 

  c = strrchr (file, 0x2f);
  if (c == (char *) NULL) {
    strcpy (image[0].name, file);
  } else { 
    strcpy (image[0].name, (c+1));
  }
  /* get astrometry information */
  strcpy (image[0].coords.ctype, "NONE");
  fits_scan (&header, "CTYPE1",   "%s",  1, image[0].coords.ctype);
  if (strcmp (image[0].coords.ctype, "RA---PLY")) {
    fprintf (stderr, "ERROR: wrong astrometric info in header\n");
    clear_lockfile ();
    exit (0);
  }
  fits_scan (&header, "CDELT1",   "%f", 1, &image[0].coords.cdelt1); 
  fits_scan (&header, "CDELT2",   "%f", 1, &image[0].coords.cdelt2);
  fits_scan (&header, "CRVAL1",   "%lf", 1, &image[0].coords.crval1);
  fits_scan (&header, "CRVAL2",   "%lf", 1, &image[0].coords.crval2);  
  fits_scan (&header, "CRPIX1",   "%f", 1, &image[0].coords.crpix1);
  fits_scan (&header, "CRPIX2",   "%f", 1, &image[0].coords.crpix2);
  fits_scan (&header, "PC001001", "%f", 1, &image[0].coords.pc1_1);
  fits_scan (&header, "PC001002", "%f", 1, &image[0].coords.pc1_2);
  fits_scan (&header, "PC002001", "%f", 1, &image[0].coords.pc2_1);
  fits_scan (&header, "PC002002", "%f", 1, &image[0].coords.pc2_2);
  fits_scan (&header, "CERROR",   "%lf", 1, &tmp);
  /* RA Terms */
  fits_scan (&header, "PCA1X2Y0", "%f", 1, &image[0].coords.polyterms[0][0]);
  fits_scan (&header, "PCA1X1Y1", "%f", 1, &image[0].coords.polyterms[1][0]);
  fits_scan (&header, "PCA1X0Y2", "%f", 1, &image[0].coords.polyterms[2][0]);
  fits_scan (&header, "PCA1X3Y0", "%f", 1, &image[0].coords.polyterms[3][0]);
  fits_scan (&header, "PCA1X2Y1", "%f", 1, &image[0].coords.polyterms[4][0]);
  fits_scan (&header, "PCA1X1Y2", "%f", 1, &image[0].coords.polyterms[5][0]);
  fits_scan (&header, "PCA1X0Y3", "%f", 1, &image[0].coords.polyterms[6][0]);
  /* Dec Terms */
  fits_scan (&header, "PCA2X2Y0", "%f", 1, &image[0].coords.polyterms[0][1]);
  fits_scan (&header, "PCA2X1Y1", "%f", 1, &image[0].coords.polyterms[1][1]);
  fits_scan (&header, "PCA2X0Y2", "%f", 1, &image[0].coords.polyterms[2][1]);
  fits_scan (&header, "PCA2X3Y0", "%f", 1, &image[0].coords.polyterms[3][1]);
  fits_scan (&header, "PCA2X2Y1", "%f", 1, &image[0].coords.polyterms[4][1]);
  fits_scan (&header, "PCA2X1Y2", "%f", 1, &image[0].coords.polyterms[5][1]);
  fits_scan (&header, "PCA2X0Y3", "%f", 1, &image[0].coords.polyterms[6][1]);
  image[0].coords.Npolyterms = 2; /* how many do we use? */
  /* CERROR in data file is in pixels, convert to 20*arcsec */
  image[0].cerror = tmp * 50.0 * image[0].coords.cdelt1 * 3600.0;
  while (image[0].coords.crval1 < 0) image[0].coords.crval1 += 360.0;
  while (image[0].coords.crval1 > 360.0) image[0].coords.crval1 -= 360.0;
 
  fits_scan (&header, "NAXIS1",   "%hd", 1, &image[0].NX); 
  fits_scan (&header, "NAXIS2",   "%hd", 1, &image[0].NY);
  fits_scan (&header, "PHOTCODE", "%hd", 1, &image[0].source);
  if (NEWPHOTCODE) {
    image[0].source = PHOTCODE;
  }
  image[0].NX -= XOVERSCAN;
  image[0].NY -= YOVERSCAN;

  tmp = 0;
  fits_scan (&header, "FLIMIT",   "%lf", 1, &tmp);
  image[0].detection_limit = tmp * 10.0;

  tmp = 0;
  fits_scan (&header, "FSATUR",   "%lf", 1, &tmp);
  image[0].saturation_limit = tmp * 10.0;

  if (!fits_scan (&header, "TZERO",   "%d",  1, &image[0].tzero)) {
    fits_scan (&header, "UT",   "%s",  1, line);
    /* remove : characters */
    for (c = strchr (line, 0x3a); c != (char *) NULL; c = strchr (c, 0x3a))
      *c = ' ';
    sscanf (line, "%d %d %d", &timeptr.tm_hour, &timeptr.tm_min, &timeptr.tm_sec);
    fits_scan (&header, "DATE-UT",   "%s",  1, line);
    for (c = strchr (line, 0x2f); c != (char *) NULL; c = strchr (c, 0x2f))
      *c = ' ';
    sscanf (line, "%d %d %d", &timeptr.tm_year, &timeptr.tm_mon, &timeptr.tm_mday);
    if (timeptr.tm_year < 90) { /* correct for year 2000 turnover */
      timeptr.tm_year += 100;
    }
    image[0].tzero = mktime (&timeptr);
  }

  tmp = 0;
  fits_scan (&header, "TRATE",   "%lf", 1, &tmp);
  image[0].trate = 10000 * tmp;

  tmp = 0;
  fits_scan (&header, "AIRMASS", "%lf", 1, &tmp);
  image[0].secz = 1000*tmp;

  /* secz is in units milli-airmass */
  image[0].Mcal =  ALPHA*(image[0].secz - 1000);
  image[0].Xm   = 30.0;
  image[0].code = 0;
  bzero (image[0].dummy, sizeof(image[0].dummy));

  /* find number of stars */
  fits_scan (&header, "NSTARS", "%d", 1, &image[0].nstar);
  if (image[0].nstar == 0) {
    fprintf (stderr, "ERROR: can't get NSTARS from header\n");
    clear_lockfile ();
    exit (0);
  }
  ALLOCATE (stars, Stars, image[0].nstar);
  Nbytes = image[0].nstar*BYTES_STAR;

  /* load in stars by blocks of 1000 */
  nstar = 0;
  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR));
  for (i = 0; i < (int)(Nbytes / (BLOCK*BYTES_STAR)); i++) {
    nbytes = Fread (buffer, 1, (BLOCK*BYTES_STAR), f, "char");
    if (nbytes != BLOCK*BYTES_STAR) {
      fprintf (stderr, "ERROR: failed to read in stars (1)\n");
      clear_lockfile ();
      exit (0);
    }
    for (j = 0; j < BLOCK; j++, nstar++) {
      dparse (&stars[nstar].X,  1, &buffer[j*BYTES_STAR]);
      dparse (&stars[nstar].Y,  2, &buffer[j*BYTES_STAR]);
      dparse (&stars[nstar].M,  3, &buffer[j*BYTES_STAR]);
      dparse (&stars[nstar].dM, 4, &buffer[j*BYTES_STAR]);
      XY_to_RD (&stars[nstar].R, &stars[nstar].D, stars[nstar].X, stars[nstar].Y, &image[0].coords);
      stars[nstar].found = -1;
    }
  }
  /* left over fraction of a block */
  nbytes = Fread (buffer, 1, (Nbytes % (BLOCK*BYTES_STAR)), f, "char");
  if (nbytes != (Nbytes % (BLOCK*BYTES_STAR))) {
    fprintf (stderr, "ERROR: failed to read in stars (2)\n");
    clear_lockfile ();
    exit (0);
  }
  for (j = 0; j < nbytes / BYTES_STAR; j++, nstar++) {
    dparse (&stars[nstar].X, 1, &buffer[j*BYTES_STAR]);
    dparse (&stars[nstar].Y, 2, &buffer[j*BYTES_STAR]);
    dparse (&stars[nstar].M, 3, &buffer[j*BYTES_STAR]);
    dparse (&stars[nstar].dM, 4, &buffer[j*BYTES_STAR]);
    XY_to_RD (&stars[nstar].R, &stars[nstar].D, stars[nstar].X, stars[nstar].Y, &image[0].coords);
    stars[nstar].found = -1;
  }

  if (image[0].nstar != nstar) {
    fprintf (stderr, "ERROR: failed to read in all stars (%d != %d)\n", image[0].nstar, nstar);
    clear_lockfile ();
    exit (0);
  }
  free (header.buffer);
  free (buffer);
 
  if (VERBOSE) fprintf (stderr, "read %d stars from target file\n", image[0].nstar);
  *NSTARS = image[0].nstar;

  return (stars);
}
