Index: trunk/Ohana/src/opihi/cmd.astro/cgrid.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/cgrid.c	(revision 41515)
+++ trunk/Ohana/src/opihi/cmd.astro/cgrid.c	(revision 42942)
@@ -12,6 +12,6 @@
 # define ADD_COORDINATE(RA,DEC)						\
   status = RD_to_XY (&Xvec.elements.Flt[N], &Yvec.elements.Flt[N], (RA), (DEC), &graphmode.coords); \
-  if ((Xvec.elements.Flt[N] >= graphmode.xmin) && (Xvec.elements.Flt[N] <= graphmode.xmax) && \
-      (Yvec.elements.Flt[N] >= graphmode.ymin) && (Yvec.elements.Flt[N] <= graphmode.ymax) && status) { \
+  if ((Xvec.elements.Flt[N] >= Xmin) && (Xvec.elements.Flt[N] <= Xmax) && \
+      (Yvec.elements.Flt[N] >= Ymin) && (Yvec.elements.Flt[N] <= Ymax) && status) { \
     N++;								\
     CHECKELEMENTS;							\
@@ -154,4 +154,10 @@
   if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
+  // we need to rationalize graphmode.xmin,xmax (can be flipped)
+  int Xmin = MIN(graphmode.xmin,graphmode.xmax);
+  int Xmax = MAX(graphmode.xmin,graphmode.xmax);
+  int Ymin = MIN(graphmode.ymin,graphmode.ymax);
+  int Ymax = MAX(graphmode.ymin,graphmode.ymax);
+
   if (argc != 1) goto usage;
 
@@ -159,10 +165,11 @@
   NorthPole = SouthPole = FALSE;
   status = RD_to_XY (&x, &y, 0.0, 90.0, &graphmode.coords);
-  if ((x >= graphmode.xmin) && (x <= graphmode.xmax) && 
-      (y >= graphmode.ymin) && (y <= graphmode.ymax) && status)
+  // if the parity is flipped in either direction, xmin > xmax or ymin > ymax
+  if ((x >= Xmin) && (x <= Xmax) && 
+      (y >= Ymin) && (y <= Ymax) && status)
     NorthPole = TRUE;
   status = RD_to_XY (&x, &y, 0.0, -90.0, &graphmode.coords);
-  if ((x >= graphmode.xmin) && (x <= graphmode.xmax) && 
-      (y >= graphmode.ymin) && (y <= graphmode.ymax) && status)
+  if ((x >= Xmin) && (x <= Xmax) && 
+      (y >= Ymin) && (y <= Ymax) && status)
     SouthPole = TRUE;
 
@@ -173,11 +180,11 @@
   if (mode == PROJ_MODE_PSEUDOCYL) {
     // for PSEUDOCYL, the ra range is 360 if the corners are invalid 
-    InvalidCorner |= XY_to_RD (&r, &d, graphmode.xmin, graphmode.ymin, &graphmode.coords); 
-    InvalidCorner |= XY_to_RD (&r, &d, graphmode.xmax, graphmode.ymin, &graphmode.coords); 
-    InvalidCorner |= XY_to_RD (&r, &d, graphmode.xmax, graphmode.ymax, &graphmode.coords); 
-    InvalidCorner |= XY_to_RD (&r, &d, graphmode.xmin, graphmode.ymax, &graphmode.coords); 
+    InvalidCorner |= XY_to_RD (&r, &d, Xmin, Ymin, &graphmode.coords); 
+    InvalidCorner |= XY_to_RD (&r, &d, Xmax, Ymin, &graphmode.coords); 
+    InvalidCorner |= XY_to_RD (&r, &d, Xmax, Ymax, &graphmode.coords); 
+    InvalidCorner |= XY_to_RD (&r, &d, Xmin, Ymax, &graphmode.coords); 
   }    
 
-  range = MIN (fabs(graphmode.coords.cdelt1*(graphmode.xmax-graphmode.xmin)), fabs(graphmode.coords.cdelt2*(graphmode.ymax-graphmode.ymin)));
+  range = MIN (fabs(graphmode.coords.cdelt1*(Xmax-Xmin)), fabs(graphmode.coords.cdelt2*(Ymax-Ymin)));
   if (NorthPole || SouthPole || InvalidCorner) range = 360;
   dR = range * GRID_SPACING;
@@ -266,6 +273,6 @@
     char line[16], format[16];
     double xt, yt, frac;
-    // dx = +0.01 * (graphmode.xmax - graphmode.xmin);
-    // dy = -0.02 * (graphmode.ymax - graphmode.ymin);
+    // dx = +0.01 * (Xmax - Xmin);
+    // dy = -0.02 * (Ymax - Ymin);
 
     if (isnan(LabelRA)) LabelRA = graphmode.coords.crval1;
@@ -274,8 +281,8 @@
       status = RD_to_XY (&xt, &yt, r, LabelDEC, &graphmode.coords);
       if (!status) continue;
-      if (xt < graphmode.xmin) continue;
-      if (xt > graphmode.xmax) continue;
-      if (yt < graphmode.ymin) continue;
-      if (yt > graphmode.ymax) continue;
+      if (xt < Xmin) continue;
+      if (xt > Xmax) continue;
+      if (yt < Ymin) continue;
+      if (yt > Ymax) continue;
       frac = -1.0 * log10(minorRA);
       if (frac != (int)frac) {
@@ -295,8 +302,8 @@
       status = RD_to_XY (&xt, &yt, r, LabelDEC, &graphmode.coords);
       if (!status) continue;
-      if (xt < graphmode.xmin) continue;
-      if (xt > graphmode.xmax) continue;
-      if (yt < graphmode.ymin) continue;
-      if (yt > graphmode.ymax) continue;
+      if (xt < Xmin) continue;
+      if (xt > Xmax) continue;
+      if (yt < Ymin) continue;
+      if (yt > Ymax) continue;
       frac = -1.0 * log10(minorRA);
       if (frac != (int)frac) {
@@ -316,8 +323,8 @@
       status = RD_to_XY (&xt, &yt, LabelRA, d, &graphmode.coords);
       if (!status) continue;
-      if (xt < graphmode.xmin) continue;
-      if (xt > graphmode.xmax) continue;
-      if (yt < graphmode.ymin) continue;
-      if (yt > graphmode.ymax) continue;
+      if (xt < Xmin) continue;
+      if (xt > Xmax) continue;
+      if (yt < Ymin) continue;
+      if (yt > Ymax) continue;
       frac = -1.0 * log10(minorDEC);
       if (frac != (int)frac) {
@@ -332,8 +339,8 @@
       status = RD_to_XY (&xt, &yt, LabelRA, d, &graphmode.coords);
       if (!status) continue;
-      if (xt < graphmode.xmin) continue;
-      if (xt > graphmode.xmax) continue;
-      if (yt < graphmode.ymin) continue;
-      if (yt > graphmode.ymax) continue;
+      if (xt < Xmin) continue;
+      if (xt > Xmax) continue;
+      if (yt < Ymin) continue;
+      if (yt > Ymax) continue;
       frac = -1.0 * log10(minorDEC);
       if (frac != (int)frac) {
