# include "dvoshell.h"
int RD_to_XYpic (double *x, double *y, double r, double d, Coords *coords, double Rmin, double Rmax, double Rmid, int *leftside);

// XXX EAM : this function is deprecated (now points at skycat)
int pcat (int argc, char **argv) {
  
  double Radius;
  int i, j, kapa, N, Nregions, ShowAll, NPTS, Npts, leftside;
  RegionFile *regions;
  char filename[128];
  struct stat filestat;
  Vector Xvec, Yvec;
  Graphdata graphmode;
  double X[4], Y[4], Rmin, Rmax, Rmid;
  char catdir[256];
  int VERBOSE;

  VarConfig ("CATDIR", "%s", catdir);

  VERBOSE = FALSE;
  if ((N = get_argument (argc, argv, "-v"))) {
    remove_argument (N, &argc, argv);
    VERBOSE = TRUE;
  }

  ShowAll = FALSE;
  if ((N = get_argument (argc, argv, "-all"))) {
    remove_argument (N, &argc, argv);
    ShowAll = TRUE;
  }
  if (argc != 1) {
    gprint (GP_ERR, "USAGE: pcat [-all]\n");
    return (FALSE);
  }
  if (!GetGraph (&graphmode, &kapa, NULL)) return (FALSE);
  
  Radius = MAX (fabs(graphmode.xmax), fabs(graphmode.ymax));
  regions = find_regions (graphmode.coords.crval1, graphmode.coords.crval2, Radius, &Nregions);

  Rmin = graphmode.coords.crval1 - 180.0;
  Rmax = graphmode.coords.crval1 + 180.0;
  Rmid = 0.5*(Rmin + Rmax);

  NPTS = 200;
  ALLOCATE (Xvec.elements, float, NPTS);
  ALLOCATE (Yvec.elements, float, NPTS);
  Npts = 0;
   
  for (i = 0; i < Nregions; i++) {
    sprintf (filename, "%s/%s", catdir, regions[i].name);
    if (ShowAll || (stat (filename, &filestat) != -1)) {
      if (VERBOSE) gprint (GP_ERR, "%3d %s\n", i, regions[i].name);

      leftside = -1;
      RD_to_XYpic (&X[0], &Y[0], regions[i].RA0, regions[i].DEC0, &graphmode.coords, Rmin, Rmax, Rmid, &leftside);
      RD_to_XYpic (&X[1], &Y[1], regions[i].RA0, regions[i].DEC1, &graphmode.coords, Rmin, Rmax, Rmid, &leftside);
      RD_to_XYpic (&X[2], &Y[2], regions[i].RA1, regions[i].DEC1, &graphmode.coords, Rmin, Rmax, Rmid, &leftside);
      RD_to_XYpic (&X[3], &Y[3], regions[i].RA1, regions[i].DEC0, &graphmode.coords, Rmin, Rmax, Rmid, &leftside);

      Xvec.elements[Npts] = X[0];
      Yvec.elements[Npts] = Y[0];
      for (j = 1; j < 4; j++) {
	Xvec.elements[Npts + j*2 - 0] = X[j];
	Yvec.elements[Npts + j*2 - 0] = Y[j];
	Xvec.elements[Npts + j*2 - 1] = X[j];
	Yvec.elements[Npts + j*2 - 1] = Y[j];
      }
      Xvec.elements[Npts+7] = Xvec.elements[Npts];
      Yvec.elements[Npts+7] = Yvec.elements[Npts];
      Npts += 8;
      if (Npts > 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);
      }
    }
  }

  gprint (GP_ERR, "plotting %d catalogs\n", Npts/8);
  Xvec.Nelements = Xvec.Nelements = Npts;
  if (Npts > 0) {
    graphmode.style = 2; /* points */
    graphmode.ptype = 100; /* connect pairs of points */
    graphmode.etype = 0;
    PlotVectorPair (kapa, Npts, Xvec.elements, Yvec.elements, &graphmode);
  }

  free (Xvec.elements);
  free (Yvec.elements);
  free (regions);

  return (TRUE);

}

int RD_to_XYpic (double *x, double *y, double r, double d, Coords *coords, double Rmin, double Rmax, double Rmid, int *leftside) {

  while (r < Rmin) { r += 360.0; }
  while (r > Rmax) { r -= 360.0; }

  if (*leftside == -1) {
    *leftside = (r < Rmid);
  } else {
    if (  *leftside && (r > Rmid + 90)) { r -= 360.0; }
    if (! *leftside && (r < Rmid - 90)) { r += 360.0; }
  }

  RD_to_XY (x, y, r, d, coords);

  return (TRUE);
}
