# include "astro.h"

int region (int argc, char **argv) {
  
  double Ra, Dec, Radius;
  float dx, dy;
  int N, kapa, NoClear, dXpix, dYpix;
  char *name;
  Graphdata graphmode;

  name = NULL;
  if ((N = get_argument (argc, argv, "-n"))) {
    remove_argument (N, &argc, argv);
    name = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetGraph (&graphmode, &kapa, name)) return (FALSE);
  FREE (name);

  NoClear = FALSE;
  if ((N = get_argument (argc, argv, "-no-clear"))) {
    remove_argument (N, &argc, argv);
    NoClear = TRUE;
  }

  if ((N = get_argument (argc, argv, "-image"))) {
    remove_argument (N, &argc, argv);
    KapaGetImageCoords (kapa, &graphmode.coords);
    KapaGetImageRange (kapa, &graphmode.xmin, &graphmode.xmax, &graphmode.ymax, &graphmode.ymin, &dXpix, &dYpix);

    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);

    // if (!NoClear) KapaClearSections (kapa);
    KapaSetLimits (kapa, &graphmode);

    SetGraph (&graphmode);
    return (TRUE);
    // Set Region based on image
  }

  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, "  [-image] [-ew] [+ew] [-ns] [+ns] [-no-clear]\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 (!ohana_str_to_radec (&Ra, &Dec, argv[1], argv[2])) return (FALSE);
  Radius = atof (argv[3]);
  strcpy (graphmode.coords.ctype, "DEC--TAN");
  if (argc == 5) {
    if (!strcasecmp (argv[4], "TAN")) 
      strcpy (graphmode.coords.ctype, "DEC--TAN");
    if (!strcasecmp (argv[4], "SIN")) 
      strcpy (graphmode.coords.ctype, "DEC--SIN");
    if (!strcasecmp (argv[4], "ARC")) 
      strcpy (graphmode.coords.ctype, "DEC--ARC");
    if (!strcasecmp (argv[4], "STG")) 
      strcpy (graphmode.coords.ctype, "DEC--STG");
    if (!strcasecmp (argv[4], "ZEA"))
      strcpy (graphmode.coords.ctype, "DEC--ZEA");
    if (!strcasecmp (argv[4], "AIT")) 
      strcpy (graphmode.coords.ctype, "DEC--AIT");
    if (!strcasecmp (argv[4], "GLS")) 
      strcpy (graphmode.coords.ctype, "DEC--GLS");
    if (!strcasecmp (argv[4], "PAR")) 
      strcpy (graphmode.coords.ctype, "DEC--PAR");
  }

  
  /* ask kapa for coordinate limits, to get the right aspect ratio */
  KapaGetLimits (kapa, &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;

  if (!NoClear) KapaClearSections (kapa);
  KapaSetLimits (kapa, &graphmode);

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

  // XXX is this the right thing to be doing?
  SetGraph (&graphmode);
  return (TRUE);
}


