# include "astro.h"

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

  char buffer[20], key;
  int i, N, Npix, Nborder, Nspot;
  double X, Y, Z, max;
  int Ximage, Nimage;
  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);

  Nborder = 3;
  if (N = get_argument (argc, argv, "-border")) {
    remove_argument (N, &argc, argv);
    Nborder  = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }
  Nborder = MAX (Nborder, 1);
  
  max = 60000;
  if (N = get_argument (argc, argv, "-sat")) {
    remove_argument (N, &argc, argv);
    max  = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }
  
  if ((argc != 2) && (argc != 3)) {
    fprintf (stderr, "USAGE: gauss Npix [Nspots] [-border N] [-sat cnts]\n");
    return (FALSE);
  }
  
  if (Ximage < 1) {
    fprintf (stderr, "no active TV\n");
    return (FALSE);
  }

  Nspot = 0;
  Npix = atof (argv[1]);
  if (argc == 3) {
    Nspot = atof (argv[2]);
  }

  if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);

  fcntl (Ximage, F_SETFL, !O_NONBLOCK);  
  write (Ximage, "CURS", 4); /* ask Ximage to look for the keystrokes */
  
  for (i = 0; (i < Nspot) || (Nspot == 0); i++) {
    bzero (buffer, 20);
    read (Ximage, buffer, 16);
    buffer[16] = 0;
    sscanf (buffer, "%c %lf %lf", &key, &X, &Y);
    if ((key == 'q') || (key == 'Q')) {
      break;
    }
    Z = get_aperture_stats (&buf[0].matrix, (int)(X+0.5), (int)(Y+0.5), Npix, Nborder, max);
    
  }
  write (Ximage, "NCUR", 4); /* tell Ximage "end of keystrokes" */
  return (TRUE);
}

