# include "astro.h"

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

  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)) {
    fprintf (stderr, "USAGE: region Ra Dec Radius [projection] [orientation]\n");
    fprintf (stderr, " 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 */
  status = write (Xgraph, "LIMS", 4); 
  read (Xgraph, buffer, 30); 
  sscanf (buffer, "%*s %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);

  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;

  write (Xgraph, "ERAS", 4);
    
  write (Xgraph, "SLIM", 4);
  sprintf (buffer, "%f %f %f %f ", graphmode.xmin, graphmode.xmax, graphmode.ymin, graphmode.ymax);
  sprintf (buffer2, "NBYTES: %6d ", strlen (buffer));
  write (Xgraph, buffer2, 16);
  write (Xgraph, buffer, strlen (buffer));

  sprintf (string, "%8.4f %8.4f (%f)", Ra, Dec, Radius);
  SendLabel (string, Xgraph, 2);

  SetGraph (graphmode);

  return (TRUE);

}


