# include "data.h"
# define TEN(X) (pow(10.0, (double)(X)))

int tvgrid (int argc, char **argv) {
  
  int ncolor, ndig1, ndig2, NX, NY, connect;
  int tDEC, tRA;
  double ra, dec, ra0, dec0, ra1, dec1;
  double x0, y0, x1, y1;
  double dDEC, fDEC, dRA, fRA;
  char buffer[1024], format[16];
  Coords coords;
  int Ximage, Nimage, N;
  Buffer *buf;

  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);
  
  if (argc != 3) {
    fprintf (stderr, "USAGE: load (overlay) (buffer)\n");
    fprintf (stderr, " (overlay) may be: red, green, blue, yellow\n");
    return (FALSE);
  }
  if (strcmp (argv[1], "0")   && strcmp (argv[1], "1")     && strcmp (argv[1], "2")   && strcmp (argv[1], "3") &&
      strcmp (argv[1], "red") && strcmp (argv[1], "green") && strcmp (argv[1], "blue") && strcmp (argv[1], "yellow")) {
    fprintf (stderr, "valid overlays may be: 0, red, 1, green, 2, blue, 3, yellow\n");
    return (FALSE);
  }

  ncolor = atof (argv[1]);
  if (!(strcmp (argv[1], "red"))) 
    ncolor = 0;
  if (!(strcmp (argv[1], "green"))) 
    ncolor = 1;
  if (!(strcmp (argv[1], "blue"))) 
    ncolor = 2;
  if (!(strcmp (argv[1], "yellow"))) 
    ncolor = 3;

  if ((buf = SelectBuffer (argv[2], OLDBUFFER, TRUE)) == NULL) return (FALSE);
  GetCoords (&coords, &buf[0].header);

  XY_to_RD (&ra0, &dec0, 0.0, 0.0, &coords);
  XY_to_RD (&ra1, &dec1, (double)buf[0].header.Naxis[0], (double)buf[0].header.Naxis[1], &coords);
  fprintf (stderr, "%f %f  %f %f\n", ra0, dec0, ra1, dec1);
  
  /* tell KII to expect overlay data */
  write (Ximage, "LOAD", 4); 
  sprintf (buffer, "OVERLAY: %7d", ncolor);
  write (Ximage, buffer, 16);
  
  dDEC = fabs(dec1 - dec0);
  tDEC = log10(dDEC) - log10(5.0);
  fDEC = log10(dDEC) - tDEC;
  if ((fDEC > log10(0.5)) && (fDEC < log10(1.0))) {
    dDEC = TEN (tDEC + log10(0.1));
  }
  if ((fDEC > log10(1.0)) && (fDEC < log10(2.0))) {
    dDEC = TEN (tDEC + log10(0.2));
  }
  if ((fDEC > log10(2.0)) && (fDEC < log10(5.0))) {
    dDEC = TEN (tDEC + log10(0.5));
  }
  if ((fDEC > log10(5.0)) && (fDEC < log10(10.0))) {
    dDEC = TEN (tDEC + log10(1.0));
  }
  if ((fDEC > log10(10.0)) && (fDEC < log10(20.0))) {
    dDEC = TEN (tDEC + log10(2.0));
  }
  if ((fDEC > log10(20.0)) && (fDEC < log10(50.0))) {
    dDEC = TEN (tDEC + log10(5.0));
  }
  ndig2 = ((log10(dDEC) < 0) ? fabs(log10(dDEC)) : 0);
  ndig1 = 3 + log10(MAX(dec0, dec1)) + ndig2;
  sprintf (format, "%%%d.%df", ndig1, ndig2);
  fprintf (stderr, "format: %s..\n", format);

  NX = buf[0].header.Naxis[0];
  NY = buf[0].header.Naxis[1];

    /* find starting position
    RD_to_XY (&x0, &y0, MIN(ra0, ra1), dec + 0.1*dDEC, &coords);
    sprintf (label, format, dec);
    sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "TEXT", x0, y0, (double)(strlen(label) + 1), 0.0);
    write (Ximage, buffer, 128);
    write (Ximage, label, strlen(label) + 1); */

  dRA = MAX (fabs(ra1 - ra0) / 100.0, 0.1);
  connect = FALSE;
  for (dec = dDEC * ((int)(MIN(dec0,dec1)/dDEC) + 1); dec < MAX(dec0,dec1); dec += dDEC) {
    for (ra = 0; ra < 361; ra += dRA) {
      RD_to_XY (&x1, &y1, ra, dec, &coords);
      if ((x1 >= 0) && (x1 < NX) && (y1 >= 0) && (y1 < NY)) {
	if (connect) {
	  sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "LINE", x0, y0, (x1 - x0), (y1 - y0));
	  write (Ximage, buffer, 128);
	}
	x0 = x1;
	y0 = y1;
	connect = TRUE;
      }
      else {
	connect = FALSE;
      }
    }
  }

  dRA = fabs(ra1 - ra0);
  tRA = log10(dRA) - log10(5.0);
  fRA = log10(dRA) - tRA;
  if ((fRA > log10(0.5)) && (fRA < log10(1.0))) {
    dRA = TEN (tRA + log10(0.1));
  }
  if ((fRA > log10(1.0)) && (fRA < log10(2.0))) {
    dRA = TEN (tRA + log10(0.2));
  }
  if ((fRA > log10(2.0)) && (fRA < log10(5.0))) {
    dRA = TEN (tRA + log10(0.5));
  }
  if ((fRA > log10(5.0)) && (fRA < log10(10.0))) {
    dRA = TEN (tRA + log10(1.0));
  }
  if ((fRA > log10(10.0)) && (fRA < log10(20.0))) {
    dRA = TEN (tRA + log10(2.0));
  }
  if ((fRA > log10(20.0)) && (fRA < log10(50.0))) {
    dRA = TEN (tRA + log10(5.0));
  }
  ndig2 = ((log10(dRA) < 0) ? fabs(log10(dRA)) : 0);
  ndig1 = 3 + log10(MAX(ra0, ra1)) + ndig2;
  sprintf (format, "%%%d.%df", ndig1, ndig2);
  fprintf (stderr, "format: %s..\n", format);

  dDEC = MAX (fabs(dec1 - dec0) / 100.0, 0.1);
  connect = FALSE;
  for (ra = dRA * ((int)(MIN(ra0,ra1)/dRA) + 1); ra < MAX(ra0,ra1); ra += dRA) {
    for (dec = -90; dec < 90; dec += dDEC) {
      RD_to_XY (&x1, &y1, ra, dec, &coords);
      if ((x1 >= 0) && (x1 < NX) && (y1 >= 0) && (y1 < NY)) {
	if (connect) {
	  sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "LINE", x0, y0, (x1 - x0), (y1 - y0));
	  write (Ximage, buffer, 128);
	}
	x0 = x1;
	y0 = y1;
	connect = TRUE;
      }
      else {
	connect = FALSE;
      }
    }
  }

  /*
  dDEC = fabs(dec1 - dec0) / 100;
  for (ra = dRA * ((int)(MIN(ra0, ra1)/dRA) + 1); ra < MAX(ra0, ra1); ra += dRA) {
    RD_to_XY (&x0, &y0, ra + 0.1*dRA, MIN(dec0, dec1), &coords);
    sprintf (label, format, ra);
    sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "TEXT", x0, y0, (double)(strlen(label) + 1), 0.0);
    write (Ximage, buffer, 128);
    write (Ximage, label, strlen(label) + 1);
    RD_to_XY (&x0, &y0, ra, MIN(dec0, dec1), &coords);
    for (dec = MIN(dec0, dec1) + dDEC; dec < MAX(dec0,dec1); dec += dDEC) {
      RD_to_XY (&x1, &y1, ra, dec, &coords);
      sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "LINE", x0, y0, (x1 - x0), (y1 - y0));
      write (Ximage, buffer, 128);
      x0 = x1;
      y0 = y1;
    }
  }
  */

  sprintf (buffer, "DONE");
  write (Ximage, buffer, 128);
  return (TRUE);
}


  

/*
  write (Ximage, "LOAD", 4); 
  sprintf (buffer, "OVERLAY: %7d", n);
  write (Ximage, buffer, 16);
  
  for (n = 0; scan_line (f, buffer) != EOF; n++) {
    c = strchr (buffer, '#');
    if (c != (char *) NULL) 
      *c = 0;  
    while ((c = strchr (buffer, '(')) != (char *) NULL) 
      *c = ' ';
    while ((c = strchr (buffer, ')')) != (char *) NULL) 
      *c = ' ';
    while ((c = strchr (buffer, ',')) != (char *) NULL) 
      *c = ' ';
    N = sscanf (buffer, "%s %lf %lf %lf %lf", type, &x, &y, &dx, &dy);
    if (N > 3) {
      if (N == 4)
	dy = dx;
      sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", type, x, y, dx, dy);
      write (Ximage, buffer, 128);
    }
  }
  fprintf (stderr, "loaded %d objects\n", n);
  sprintf (buffer, "DONE");
  write (Ximage, buffer, 128);
  if (f != stdin) {
    fclose (f);
  }
*/
