# include "data.h"

int line (int argc, char **argv) {
  
  int N, Npts, Ngraph;
  Graphdata graphmode;
  Vector *vecx, *vecy, *dxm, *dxp, *dym, *dyp;

  /* 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 */
  dxm = dxp = dym = dyp = NULL;
  if (N = get_argument (argc, argv, "-dx")) {
    remove_argument (N, &argc, argv);
    if ((dxm = 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 ((dxp = 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 ((dym = 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 ((dyp = 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 ((dym != NULL) && (dyp == NULL)) dyp = dym;
  if ((dyp != NULL) && (dym == NULL)) dym = dyp;
  if ((dyp != NULL) || (dym != NULL)) graphmode.etype |= 0x01;
  if ((dxm != NULL) && (dxp == NULL)) dxp = dxm;
  if ((dxp != NULL) && (dxm == NULL)) dxm = dxp;
  if ((dxp != NULL) || (dxm != NULL)) graphmode.etype |= 0x02;
  
  /* find vectors */
  if ((vecx = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((vecy = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if (vecy[0].Nelements != vecy[0].Nelements) {
    fprintf (stderr, "vectors %s and %s not the same length\n", argv[1], argv[2]);
    return (FALSE);
  }
  
  Npts = vecx[0].Nelements;
  if (!PrepPlotting (Npts, &graphmode)) return (FALSE);
  
  PlotVector (Npts, vecx[0].elements);
  PlotVector (Npts, vecy[0].elements);
  if (graphmode.etype & 0x01) {
    PlotVector (Npts, dym[0].elements);
    PlotVector (Npts, dyp[0].elements);
  }
  if (graphmode.etype & 0x02) {
    PlotVector (Npts, dxm[0].elements);
    PlotVector (Npts, dxp[0].elements);
  }
  
  return (TRUE);

}


