# include "astro.h"

int region (int argc, char **argv) {
  
  char string[256];
  double Ra, Dec, Radius;
  double dx, dy;
  int N, Ngraph, Xgraph;
  Graphdata graphmode;

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

  if ((N = get_argument (argc, argv, "-ew"))) {
    remove_argument (N, &argc, argv);
    graphmode.flipeast = TRUE;
  }

  if ((N = get_argument (argc, argv, "+ew"))) {
    remove_argument (N, &argc, argv);
    graphmode.flipeast = FALSE;
  }

  if ((N = get_argument (argc, argv, "-ns"))) {
    remove_argument (N, &argc, argv);
    graphmode.flipnorth = TRUE;
  }

  if ((N = get_argument (argc, argv, "+ns"))) {
    remove_argument (N, &argc, argv);
    graphmode.flipnorth = FALSE;
  }

  if ((argc != 4) && (argc != 5)) {
    gprint (GP_ERR, "USAGE: region Ra Dec Radius [projection] [orientation]\n");
    gprint (GP_ERR, " current: %f %f (%f x %f) (%s)\n", 
	     graphmode.coords.crval1, graphmode.coords.crval2, 
	     fabs(graphmode.xmax - graphmode.xmin), 
	     fabs(graphmode.ymax - graphmode.ymin), 
	     &graphmode.coords.ctype[5]);
    return (FALSE);
  }
  
  if (!str_to_radec (&Ra, &Dec, argv[1], argv[2])) return (FALSE);
  Radius = atof (argv[3]);
  strcpy (graphmode.coords.ctype, "RA---TAN");
  if (argc == 5) {
    if (!strcasecmp (argv[4], "SIN")) 
      strcpy (graphmode.coords.ctype, "RA---SIN");
    if (!strcasecmp (argv[4], "ZEA"))
      strcpy (graphmode.coords.ctype, "RA---ZEA");
    if (!strcasecmp (argv[4], "AIT")) 
      strcpy (graphmode.coords.ctype, "RA---AIT");
    if (!strcasecmp (argv[4], "GLS")) 
      strcpy (graphmode.coords.ctype, "RA---GLS");
    if (!strcasecmp (argv[4], "PAR")) 
      strcpy (graphmode.coords.ctype, "RA---PAR");
  }

  
  /* ask kapa for coordinate limits, so get the right aspect ratio */
  KiiSendCommand (Xgraph, 4, "LIMS"); 
  KiiScanMessage (Xgraph, "%lf %lf", &dx, &dy); 
  dx = fabs (dx);
  dy = fabs (dy); 

  /* define limits for Ra, Dec at center, grid in degrees */
  if (dy < dx) {
    graphmode.xmin = -(dx/dy)*Radius;
    graphmode.ymin = -Radius;
    graphmode.xmax = (dx/dy)*Radius;
    graphmode.ymax = Radius;
  } else {
    graphmode.xmin = -Radius;
    graphmode.ymin = -(dy/dx)*Radius;
    graphmode.xmax = Radius;
    graphmode.ymax = (dy/dx)*Radius;
  } 

  set_variable ("XMIN", graphmode.xmin);
  set_variable ("XMAX", graphmode.xmax);
  set_variable ("YMIN", graphmode.ymin);
  set_variable ("YMAX", graphmode.ymax);

  set_variable ("RMIN", Ra  + graphmode.xmin);
  set_variable ("RMAX", Ra  + graphmode.xmax);
  set_variable ("DMIN", Dec + graphmode.ymin);
  set_variable ("DMAX", Dec + graphmode.ymax);

  set_int_variable ("EAST_RIGHT", !graphmode.flipeast);
  set_int_variable ("NORTH_UP", !graphmode.flipnorth);

  graphmode.coords.pc1_1 = (graphmode.flipeast) ? -1 : 1;
  graphmode.coords.pc2_2 = (graphmode.flipnorth) ? -1 : 1;

  graphmode.coords.pc1_2 = graphmode.coords.pc2_1 = 0.0;
  graphmode.coords.crval1 = Ra;
  graphmode.coords.crval2 = Dec;
  graphmode.coords.crpix1 = 0.0;
  graphmode.coords.crpix2 = 0.0;
  graphmode.coords.cdelt1 = graphmode.coords.cdelt2 = 1.0;

  KapaClear (Xgraph, TRUE);
  KapaSetLimits (Xgraph, &graphmode);

  /* drop this? */
  sprintf (string, "%8.4f %8.4f (%f)", Ra, Dec, Radius);
  KapaSendLabel (Xgraph, string, 2);

  SetGraph (graphmode);
  return (TRUE);
}


