# include "data.h"

int point (int argc, char **argv) {
  
  int N, n, ISCEL, ISPIX;
  int Ximage, Nimage;
  double x, y, dx, dy, x1, y1;
  double dra, ddec, ra1, dec1, ra, dec;
  char *line;
  Coords coords;
  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);
  
  ISCEL = FALSE;
  if (N = get_argument (argc, argv, "-c")) {
    remove_argument (N, &argc, argv);
    ISCEL = TRUE;
  }
  
  ISPIX = FALSE;
  if (N = get_argument (argc, argv, "-p")) {
    remove_argument (N, &argc, argv);
    ISPIX = TRUE;
  }

  if (argc != 7) {
    fprintf (stderr, "USAGE: point (overlay) TYPE x y dx dy [-c]\n");
    return (FALSE);
  }
  
  if (!SelectOverlay (argv[1], &n)) 
    return (FALSE);

  if (ISCEL) {
    if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);
    GetCoords (&coords, &buf[0].header);
  }

  ALLOCATE (line, char, 1024);     /* space to read the file */

  ra = atof(argv[3]);
  dec = atof(argv[4]);
  dra = atof(argv[5]);
  ddec = atof(argv[6]);
  if (ISCEL) {
    RD_to_XY (&x, &y, ra, dec, &coords);
    if (!ISPIX) {
      ra1 = ra + dra;
      dec1 = dec + ddec;
      RD_to_XY (&x1, &y1, ra1, dec1, &coords);
      dx = x1 - x;
      dy = y1 - y;
    } else {
      dx = atof(argv[5]);
      dy = atof(argv[6]);
    }
  }
  else {
    x = ra;
    y = dec;
    dx = dra;
    dy = ddec;
  }

  write (Ximage, "LOAD", 4); /* force Ximage to look for the incoming image */
  sprintf (line, "OVERLAY %6d ", n);
  write (Ximage, line, 16);
  sprintf (line, "NLINES %7d ", 1);
  write (Ximage, line, 16);
  sprintf (line, "%15s %20.10f %20.10f %20.10f %20.10f ", argv[2], x, y, dx, dy);
  write (Ximage, line, 128);
  sprintf (line, "DONE ");
  write (Ximage, line, 16);

  free (line);

  return (TRUE);

}

