# include "astro.h"

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

  char key[20];
  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)) {
    gprint (GP_ERR, "USAGE: gauss Npix [Nspots] [-border N] [-sat cnts]\n");
    return (FALSE);
  }
  
  if (Ximage < 1) {
    gprint (GP_ERR, "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);

  KiiCursorOn (Ximage);

  for (i = 0; (i < Nspot) || (Nspot == 0); i++) {
    KiiCursorRead (Ximage, &X, &Y, key);
    if (!strcasecmp (key, "Q")) break;
    Z = get_aperture_stats (&buf[0].matrix, (int)(X+0.5), (int)(Y+0.5), Npix, Nborder, max);
  }
  KiiCursorOff (Ximage);
  return (TRUE);
}

