# include "data.h"

int peak (int argc, char **argv) {
  
  int i, imax;
  double start, end, xmax, ymax;
  float *X, *Y;
  Vector *vecx, *vecy;

  if (argc != 5) {
    fprintf (stderr, "USAGE: peak <x> <y> start end\n");
    return (FALSE);
  }

  if ((vecx = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((vecy = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);

  start = atof (argv[3]);
  end   = atof (argv[4]);

  X = vecx[0].elements;
  Y = vecy[0].elements;

  imax = -1;
  for (i = 0; i < vecx[0].Nelements-1; i++, X++, Y++) {
    if ((*X >= start) && (*X <= end)) {
      if ((imax == -1) || (*Y > ymax)) {
	xmax = *X;
	ymax = *Y;
	imax = i;
      }
    }
  }      

  set_variable ("peakval", ymax);
  set_variable ("peakpos", xmax);
  set_variable ("peaknum", imax);

  fprintf (stderr, "peak %f @ %f (%d)\n", ymax, xmax, imax);

  return (TRUE);
}

