# include "data.h"

int zplot (int argc, char **argv) {
  
  int i, N, Npts, Ngraph;
  float *in, *out;
  double min, range;
  Graphdata graphmode;
  Vector *xvec, *yvec, *zvec, Zvec;

  Ngraph = -1;
  if ((N = get_argument (argc, argv, "-n"))) {
    remove_argument (N, &argc, argv);
    Ngraph = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);

  if (argc != 6) {
    gprint (GP_ERR, "USAGE: zplot <x> <y> <z> min max\n");
    return (FALSE);
  }

  min = atof(argv[4]);
  range = atof(argv[5]) - min;

  /* find vectors */
  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((yvec = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((zvec = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if (xvec[0].Nelements != yvec[0].Nelements) {
    gprint (GP_ERR, "vectors %s and %s not the same length\n", argv[1], argv[2]);
    return (FALSE);
  }
  if (xvec[0].Nelements != zvec[0].Nelements) {
    gprint (GP_ERR, "vectors %s and %s not the same length\n", argv[1], argv[3]);
    return (FALSE);
  }
  Zvec.Nelements = zvec[0].Nelements;
  ALLOCATE (Zvec.elements, float, Zvec.Nelements);
 
  in = zvec[0].elements;
  out = Zvec.elements;
  for (i = 0; i < Zvec.Nelements; i++, in++, out++) {
    *out = MIN (1.0, MAX (0.01, (*in - min) / range));
  }

  /* point size determined by Zvec */
  graphmode.style = 2; /* plot points */
  graphmode.size = -1; /* point size determined by Zvec */
  graphmode.etype = 0; /* no errorbars */
  Npts = xvec[0].Nelements;
  PrepPlotting (Npts, &graphmode);

  PlotVector (Npts, xvec[0].elements);
  PlotVector (Npts, yvec[0].elements);
  PlotVector (Npts, Zvec.elements);

  free (Zvec.elements);

  return (TRUE);

}


