# include "astro.h"

int coords (int argc, char **argv) {

  int mode, N;
  double X, Y, R, D;
  Coords coords;
  Buffer *buf;

  mode = 0;
  if (N = get_argument (argc, argv, "-p")) {
    remove_argument (N, &argc, argv);
    X = atof(argv[N]);
    remove_argument (N, &argc, argv);
    Y = atof(argv[N]);
    remove_argument (N, &argc, argv);
    mode = 1;
  }
  if (N = get_argument (argc, argv, "-c")) {
    if (mode == 1) {
      fprintf (stderr, "only one of -p or -c can be used\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    R = atof(argv[N]);
    remove_argument (N, &argc, argv);
    D = atof(argv[N]);
    remove_argument (N, &argc, argv);
    mode = 2;
  }
  if ((mode == 0) || (argc != 2)) {
    fprintf (stderr, "USAGE: coords [buffer] (-c R D) / (-p X Y)\n");
    return (FALSE);
  }    

  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
  GetCoords (&coords, &buf[0].header);
  
  if (mode == 1) {
    XY_to_RD (&R, &D, X, Y, &coords);
    fprintf (stderr, "%10.6f %10.6f\n", R, D);
    set_variable ("RA", R);
    set_variable ("DEC", D);
  }
    
  if (mode == 2) {
    RD_to_XY (&X, &Y, R, D, &coords);
    fprintf (stderr, "%7.2f %7.2f\n", X, Y);
    set_variable ("Xc", X);
    set_variable ("Yc", Y);
  }

  return (TRUE);

}
