# include "dvo.h"

int images (int argc, char **argv) {

  int i, j, Nimage, status, InPic, leftside, *plist, TimeSelect, ByName;
  time_t tzero, tend;
  int N, NPTS, n, npts;
  Vector Xvec, Yvec;
  double r, d, x[4], y[4], Rmin, Rmax, Rmid, trange;
  Image *image;
  Graphdata graphmode;
  char name[256];
  int Ngraph;

  Ngraph = 0;
  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);

  ByName = FALSE;
  if (N = get_argument (argc, argv, "-name")) {
    remove_argument (N, &argc, argv);
    strcpy (name, argv[N]);
    remove_argument (N, &argc, argv);
    ByName = TRUE;
  }

  TimeSelect = FALSE;
  if (N = get_argument (argc, argv, "-time")) {
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tzero)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!str_to_dtime (argv[N], &trange)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (trange < 0) {
      trange = fabs (trange);
      tzero -= trange;
    }
    TimeSelect = TRUE;
  }
  if (N = get_argument (argc, argv, "-trange")) {
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tzero)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tend)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    trange = tend - tzero;
    if (trange < 0) {
      trange = fabs (trange);
      tzero -= trange;
    }
    TimeSelect = TRUE;
  }
 
  if (argc != 1) {
    fprintf (stderr, "USAGE: image [-time start range]\n");
    return (FALSE);
  }
  
  image = LoadImages (&Nimage);
  if (Nimage == 0) {
    return (TRUE);
  }

  Rmin = graphmode.coords.crval1 - 180.0;
  Rmax = graphmode.coords.crval1 + 180.0;
  Rmid = 0.5*(Rmin + Rmax);
  
  npts = NPTS = 200;
  ALLOCATE (Xvec.elements, float, NPTS);
  ALLOCATE (Yvec.elements, float, NPTS);
  ALLOCATE (plist, int, NPTS);
  n = N = 0;
  for (i = 0; i < Nimage; i++) {
    if (ByName && strcmp (name, image[i].name)) continue;
    if (TimeSelect && ((image[i].tzero < tzero) || (image[i].tzero+image[i].trate*image[i].NY > tzero + trange))) continue;
    /* project this image to screen display coords */
    x[0] = 0;           y[0] = 0;
    x[1] = image[i].NX; y[1] = 0;
    x[2] = image[i].NX; y[2] = image[i].NY;
    x[3] = 0;           y[3] = image[i].NY;
    status = FALSE;

    leftside = FALSE;
    for (j = 0; j < 4; j++) {
      XY_to_RD (&r, &d, x[j], y[j], &image[i].coords);
      while (r < Rmin) { r += 360.0; }
      while (r > Rmax) { r -= 360.0; }
      if (j == 0) {
	leftside = (r < Rmid);
      } 
      if (j > 0) { 
	if ( leftside && (r > Rmid + 90)) { r -= 360.0; }
	if (!leftside && (r < Rmid - 90)) { r += 360.0; }
      }
      status |= fRD_to_XY (&Xvec.elements[N+2*j], &Yvec.elements[N+2*j], r, d, &graphmode.coords);
      if (j > 0) {
	Xvec.elements[N+2*j - 1] = Xvec.elements[N+2*j];
	Yvec.elements[N+2*j - 1] = Yvec.elements[N+2*j];
      }
    }
    Xvec.elements[N+7] = Xvec.elements[N];
    Yvec.elements[N+7] = Yvec.elements[N];
    InPic = FALSE;
    for (j = 0; j < 8; j+=2) {
      if ((Xvec.elements[N+j] >= graphmode.xmin) && 
	  (Xvec.elements[N+j] <= graphmode.xmax) && 
	  (Yvec.elements[N+j] >= graphmode.ymin) && 
	  (Yvec.elements[N+j] <= graphmode.ymax))
	InPic = TRUE;
    }
    if (InPic && status) {
      plist[n] = i;
      n++;
      if (n > npts - 1) {
	npts += 200;
	REALLOCATE (plist, int, npts);
      }
      N+=8;
      if (N > NPTS - 1) {  /* this is OK because NPTS is made always a multiple of 8 */
	NPTS += 200;
	REALLOCATE (Xvec.elements, float, NPTS);
	REALLOCATE (Yvec.elements, float, NPTS);
      }
    }
  }

  fprintf (stderr, "plotting %d images\n", N/8);
  Xvec.Nelements = Xvec.Nelements = N;
  if (N > 0) {
    graphmode.style = 2; /* points */
    graphmode.ptype = 100; /* connect pairs of points */
    graphmode.etype = 0;
    PrepPlotting (N, &graphmode);
    PlotVector (N, Xvec.elements);
    PlotVector (N, Yvec.elements);
  }

  free (Xvec.elements);
  free (Yvec.elements);
  free (image);
  return (TRUE);

}


