# include "dvo.h"
# define D_NSTARS 1000
# define BYTES_STAR 66
# define BLOCK 1000

int cmpload (int argc, char **argv) {
  
  int i, nstar, N, Nin, Nout, Nextra, n, Type, type;
  int doneread, done, Nskip, Nbytes, nbytes, Ninstar;
  char *c, *c2;
  double *X, *Y, *M;
  double dtmp;
  FILE *f;
  char *buffer, *buffer2, *line;
  Header header;
  int Ximage, Nimage;
  
  Nimage = -1;
  if (N = get_argument (argc, argv, "-n")) {
    remove_argument (N, &argc, argv);
    Nimage = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetImage (&Ximage, &Nimage)) return (FALSE);

  Type = 0;
  if (N = get_argument (argc, argv, "-t")) {
    remove_argument (N, &argc, argv);
    Type = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: load (overlay) <filename>\n");
    return (FALSE);
  }

  if (!SelectOverlay (argv[1], &n)) 
    return (FALSE);

  if (!fits_read_header (argv[2], &header)) {
    fprintf (stderr, "ERROR: can't read header for %s\n", argv[2]);
    return (FALSE);
  }

  /* find expected number of stars */
  fits_scan (&header, "NSTARS", "%d", 1, &nstar);
  if (nstar == 0) {
    fprintf (stderr, "ERROR: can't get NSTARS from header\n");
    fits_free_header (&header);
    return (FALSE);
  }

  f = fopen (argv[2], "r");
  if (f == NULL) {
    fprintf (stderr, "ERROR: can't read data from %s\n", argv[2]);
    fits_free_header (&header);
    return (FALSE);
  }
  fseek (f, header.size, SEEK_SET); 

  ALLOCATE (X, double, nstar);
  ALLOCATE (Y, double, nstar);
  ALLOCATE (M, double, nstar);

  /* load in stars by blocks of 1000 */
  Nin = 0;
  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR) + 1);
  buffer[BLOCK*BYTES_STAR] = 0;
  Nextra = 0;
  doneread = FALSE;
  while (!doneread) {
    Nbytes = BYTES_STAR * BLOCK - Nextra;
    nbytes = fread (&buffer[Nextra], 1, Nbytes, f);
    if (nbytes == 0) {
      doneread = TRUE;
      continue;
    }
    nbytes += Nextra;
    /* check line-by-line integrity */
    c = buffer;
    done = FALSE;
    while ((c < buffer + nbytes) && (!done)) { 
      for (c2 = c; *c2 == '\n'; c2++);
      if (c2 > c) { /* extra return chars */
	memmove (c, c2, (int)(buffer + nbytes - c2));
	Nskip = c2 - c;
	nbytes -= Nskip;
	bzero (buffer + nbytes, Nskip);
      }
      c2 = strchr (c, '\n');
      if (c2 == (char *) NULL) {
	done = TRUE;	
	continue;
      }
      c2++;
      if ((c2 - c) != BYTES_STAR) { /* bad line, delete it */
	memmove (c, c2, (int)(buffer + nbytes - c2));
	Nskip = c2 - c;
	nbytes -= Nskip;
	bzero (buffer + nbytes, Nskip);
      } else {
	c = c2;
      }
    }
    Ninstar = nbytes / BYTES_STAR;
    Nextra = nbytes % BYTES_STAR;
    for (i = 0; i < Ninstar; i++, Nin++) {
      dparse (&X[Nin],  1, &buffer[i*BYTES_STAR]);
      dparse (&Y[Nin],  2, &buffer[i*BYTES_STAR]);
      dparse (&M[Nin],  3, &buffer[i*BYTES_STAR]);
      if (Type) {
	dparse (&dtmp, 5, &buffer[i*BYTES_STAR]);
	type = dtmp;
	if (type != Type) {
	  Nin --;
	}
      }
    }
  }
  fclose (f);

  ALLOCATE (line, char, 129);
  write (Ximage, "LOAD", 4); /* force Ximage to look for the incoming image */
  sprintf (line, "OVER %9d ", n);
  write (Ximage, line, 16);
  
  ALLOCATE (buffer2, char, 66000);
  bzero (buffer2, 65536);
  for (Nout = i = 0; i < Nin; i++, Nout++) {
    sprintf (&buffer2[Nout*128], "%15s %20.10f %20.10f %20.10f %20.10f ", "BOX", X[i], Y[i], 5.0, 5.0);
    if (Nout == 512) {
      sprintf (line, "NLINES  %7d ", Nout);
      write (Ximage, line, 16);
      write (Ximage, buffer2, Nout*128);
      bzero (buffer2, 65536);
      Nout = -1;
    }
  }

  if (Nout) {
    sprintf (line, "NLINES  %7d ", Nout);
    write (Ximage, line, 16);
    write (Ximage, buffer2, Nout*128);
  }
  sprintf (line, "DONE ");
  write (Ximage, line, 16);

  fits_free_header (&header);
  free (X);
  free (Y);
  free (M);
  free (buffer);
  /*  */

  free (buffer2);
  free (line);

  fprintf (stderr, "loaded %d objects\n", Nin);
  return (TRUE);
}

