# include "data.h"

int histogram (int argc, char **argv) {
  
  int i, bin, Nbins;
  float *V, start, end, delta;
  Vector *xvec, *yvec;

  if ((argc != 6) && (argc != 5)) {
    gprint (GP_ERR, "USAGE: hist invec outvec start end [delta]\n");
    return (FALSE);
  }

  delta = 1;
  start = atof (argv[3]);
  end   = atof (argv[4]);
  if (argc == 6) delta = atof (argv[5]);
 
  if ((start == end) || (delta == 0)) {
    gprint (GP_ERR, "error in value: %f to %f, %f\n", start, end, delta);
    return (FALSE);
  }
  delta = fabs (delta);
  if (end - start < 0) {
    delta = -1.0 * delta;
  }
  Nbins = (end - start) / delta;
  /* number here should match number in create.c */

  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((yvec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);
  yvec[0].Nelements = Nbins;
    
  REALLOCATE (yvec[0].elements, float, yvec[0].Nelements);
  bzero (yvec[0].elements, sizeof(float)*yvec[0].Nelements);

  V = xvec[0].elements;
  for (i = 0; i < xvec[0].Nelements; i++, V++) {
    bin = MIN (MAX (0, (*V - start) / delta), Nbins - 1);
    yvec[0].elements[bin] += 1.0;
  }      

  return (TRUE);

}
