# include "data.h"

int plot (int argc, char **argv) {
  
  int N, Npts, Ngraph;
  Graphdata graphmode;
  Vector *xvec, *yvec, *dxmvec, *dxpvec, *dymvec, *dypvec;

  /* choose the appropriate graphing window */
  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);

  /* evaluate various plotting options */
  if (N = get_argument (argc, argv, "-lt")) {
    remove_argument (N, &argc, argv);
    graphmode.ltype = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "-lw")) {
    remove_argument (N, &argc, argv);
    graphmode.lweight = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "-pt")) {
    remove_argument (N, &argc, argv);
    graphmode.ptype = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "+eb")) {
    remove_argument (N, &argc, argv);
    graphmode.ebar = TRUE;
  }
  if (N = get_argument (argc, argv, "-eb")) {
    remove_argument (N, &argc, argv);
    graphmode.ebar = FALSE;
  }
  if (N = get_argument (argc, argv, "-sz")) {
    remove_argument (N, &argc, argv);
    graphmode.size = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "-c")) {
    remove_argument (N, &argc, argv);
    graphmode.color = GetColor (argv[N]);
    if (graphmode.color == -1) return (FALSE);
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "-x")) {
    remove_argument (N, &argc, argv);
    graphmode.style = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* decide on error bars */
  dxmvec = dxpvec = dymvec = dypvec = NULL;
  if (N = get_argument (argc, argv, "-dx")) {
    remove_argument (N, &argc, argv);
    if ((dxmvec = SelectVector (argv[N], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "+dx")) {
    remove_argument (N, &argc, argv);
    if ((dxpvec = SelectVector (argv[N], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "-dy")) {
    remove_argument (N, &argc, argv);
    if ((dymvec = SelectVector (argv[N], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "+dy")) {
    remove_argument (N, &argc, argv);
    if ((dypvec = SelectVector (argv[N], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
    remove_argument (N, &argc, argv);
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: plot <x> <y>\n");
    return (FALSE);
  }

  /* set plotting options (these are sticky) */
  SetGraph (graphmode);

  /* set errorbar mode (these are NOT sticky) */
  graphmode.etype = 0;
  if ((dymvec != NULL) && (dypvec == NULL)) dypvec = dymvec;
  if ((dypvec != NULL) && (dymvec == NULL)) dymvec = dypvec;
  if (dypvec != NULL) graphmode.etype |= 0x01;
  if ((dxmvec != NULL) && (dxpvec == NULL)) dxpvec = dxmvec;
  if ((dxpvec != NULL) && (dxmvec == NULL)) dxmvec = dxpvec;
  if (dxpvec != NULL) graphmode.etype |= 0x02;
  
  /* find vectors */
  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((yvec = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if (xvec[0].Nelements != yvec[0].Nelements) {
    fprintf (stderr, "vectors %s and %s not the same length\n", argv[1], argv[2]);
    return (FALSE);
  }
  Npts = xvec[0].Nelements;
  if (Npts == 0) return (TRUE);

  if (!PrepPlotting (Npts, &graphmode)) return (FALSE);
  
  PlotVector (Npts, xvec[0].elements);
  PlotVector (Npts, yvec[0].elements);
  if (graphmode.etype & 0x01) {
    PlotVector (Npts, dymvec[0].elements);
    PlotVector (Npts, dypvec[0].elements);
  }
  if (graphmode.etype & 0x02) {
    PlotVector (Npts, dxmvec[0].elements);
    PlotVector (Npts, dxpvec[0].elements);
  }
  
  return (TRUE);

}


