Index: /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/coordops.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/coordops.c	(revision 12068)
+++ /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/coordops.c	(revision 12069)
@@ -18,18 +18,9 @@
   *dec = 0;
   stht = ctht = 1;
-  type = &coords[0].ctype[4];
-  
-  /* PLY is equiv to LIN with higher order terms
-     ZPL is equiv to ZEA with higher order terms
-     DIS is equiv to TAN with higher order terms
-     WRP is equiv to PLY, with implied mosaic */
-
-  Polynomial = !strcmp(type, "-PLY") || !strcmp(type, "-DIS") || !strcmp(type, "-WRP") || !strcmp(type, "-ZPL");
-  Cartesian  = !strcmp(type, "-LIN") || !strcmp(type, "-PLY") || !strcmp(type, "-WRP") || !strcmp(&coords[0].ctype[0], "GENE");
-  PseudoCyl  = !strcmp(type, "-AIT") || !strcmp(type, "-GLS") || !strcmp(type, "-PAR");
-  Zenith1    = !strcmp(type, "-DIS") || !strcmp(type, "-TAN") || !strcmp(type, "-STG");
-  Zenith2    = !strcmp(type, "-SIN") || !strcmp(&coords[0].ctype[0], "MM");
-  Zenithal   = !strcmp(type, "-ZEA") || !strcmp(type, "-ZPL") || Zenith1 || Zenith2;
-  if (!Zenithal && !Cartesian && !PseudoCyl) return (FALSE);
+  
+  proj = GetProjection (coords[0].ctype);
+  mode = GetProjectionMode (proj);
+  if (proj == PROJ_NONE) return (FALSE);
+  if (proj == PROJ_MODE_NONE) return (FALSE);
 
   /** convert pixel coordinates to cartesian system **/
@@ -50,11 +41,11 @@
   }
 
-  /**** Locally Cartesian Projections ****/
-  if (Cartesian) {
+  /** Locally Cartesian Projections **/
+  if (mode == PROJ_MODE_CARTESIAN) {
     *ra  = L + coords[0].crval1;
     *dec = M + coords[0].crval2;
 
     /* mosaic astrometry : WRP is chip astrometry; apply mosaic (DIS) term */
-    if (!strcmp(type, "-WRP")) {
+    if (proj == PROJ_WRP) {
       if (mosaic == NULL) return (FALSE);
       XY_to_RD (ra, dec, L + coords[0].crval1, M + coords[0].crval2, mosaic);
@@ -63,6 +54,6 @@
   }
   
-  /**** Zenithal Projections ****/
-  if (Zenithal) {
+  /** Zenithal Projections **/
+  if (mode == PROJ_MODE_ZENITHAL) {
     R = hypot (L,M);
     if ((L == 0) && (M == 0)) {
@@ -74,29 +65,34 @@
     }
 
-    /* this is wrong : STG is not TAN - need to put in correct relationships.  but is a close approx */
-    if (Zenith1) {
-      if (R == 0) {
-	stht = 1.0;
-	ctht = 0.0;
-      } else {
-	T = DEG_RAD / R;
-	stht =   T / sqrt ( 1.0 + T*T);
-	ctht = 1.0 / sqrt ( 1.0 + T*T);
-      }
-    }
-    if (Zenith2) {
-      ctht = RAD_DEG * R;
-      stht = sqrt (1 - ctht*ctht);
-    }
-    if (!strcmp(type, "-ZEA") || !strcmp(type, "-ZPL")) {
-      if (R > 2*DEG_RAD) {
-	*ra = L;
-	*dec = M;
-	return (FALSE);
-      }
-      stht = 1 - 0.5*SQ(R*RAD_DEG);
-      ctht = sqrt (1 - stht*stht);
-    }
-
+    switch (proj) {
+      case PROJ_TAN:
+	if (R == 0) {
+	  stht = 1.0;
+	  ctht = 0.0;
+	} else {
+	  T = DEG_RAD / R;
+	  stht =   T / sqrt ( 1.0 + T*T);
+	  ctht = 1.0 / sqrt ( 1.0 + T*T);
+	}
+	break;
+      case PROJ_STG:
+	stht = (4 - RAD_DEG*R) / (4 + RAD_DEC*R);
+	ctht = sqrt (1 - stht*stht);
+	break;
+      case PROJ_SIN:
+	ctht = RAD_DEG * R;
+	stht = sqrt (1 - ctht*ctht);
+	break;
+      case PROJ_ZEA:
+      case PROJ_ZPL:
+	if (R > 2*DEG_RAD) {
+	  *ra = L;
+	  *dec = M;
+	  return (FALSE);
+	}
+	stht = 1 - 0.5*SQ(R*RAD_DEG);
+	ctht = sqrt (1 - stht*stht);
+	break;
+    }
     sdp  = sin(RAD_DEG*coords[0].crval2);
     cdp  = cos(RAD_DEG*coords[0].crval2);
@@ -119,6 +115,7 @@
   
   /**** Other Conventional Projections ****/
-  if (PseudoCyl) {
-    if (!strcmp(type, "-AIT")) {
+  if (mode == PROJ_MODE_PSEUDOCLY) {
+    switch (proj) {
+      case PROJ_AIT:
       Z2 = (1.0 - SQ(RAD_DEG*0.25*L) - SQ(RAD_DEG*0.5*M));
       if (Z2 < 0) return (FALSE);
@@ -126,21 +123,19 @@
       alpha = 2.0 * DEG_RAD * atan2 (RAD_DEG*0.5*Z*L, 2.0*Z2 - 1.0);
       delta = DEG_RAD * asin (RAD_DEG*M*Z);
-      *ra  = alpha + coords[0].crval1;
-      *dec = delta + coords[0].crval2;
-    }
-    if (!strcmp(type, "-GLS")) {
-      /* L,M in degrees, alpha,delta in degrees */
-      alpha = L / cos (RAD_DEG * M);
-      delta = M;
-      *ra  = alpha + coords[0].crval1;
-      *dec = delta + coords[0].crval2;
-    }
-    if (!strcmp(type, "-PAR")) {
-      /* L,M in degrees, alpha,delta in degrees */
-      alpha = L / (1.0 - SQ(2.0*M/180));
-      delta = 3 * DEG_RAD * asin (M/180.0);
-      *ra  = alpha + coords[0].crval1;
-      *dec = delta + coords[0].crval2;
-    }
+      break;
+      
+      case PROJ_GLS:
+	/* L,M in degrees, alpha,delta in degrees */
+	alpha = L / cos (RAD_DEG * M);
+	delta = M;
+	break;
+      case PROJ_PAR:
+	/* L,M in degrees, alpha,delta in degrees */
+	alpha = L / (1.0 - SQ(2.0*M/180));
+	delta = 3 * DEG_RAD * asin (M/180.0);
+	break;
+    }
+    *ra  = alpha + coords[0].crval1;
+    *dec = delta + coords[0].crval2;
 
     /* rationalize ra range 0 - 360.0 */
@@ -170,15 +165,12 @@
   L = M = 0;
 
-  Polynomial = !strcmp(type, "-PLY") || !strcmp(type, "-DIS") || !strcmp(type, "-WRP") || !strcmp(type, "-ZPL");
-  Cartesian  = !strcmp(type, "-LIN") || !strcmp(type, "-PLY") || !strcmp(type, "-WRP") || !strcmp(&coords[0].ctype[0], "GENE");
-  PseudoCyl  = !strcmp(type, "-AIT") || !strcmp(type, "-GLS") || !strcmp(type, "-PAR");
-  Zenith1    = !strcmp(type, "-DIS") || !strcmp(type, "-TAN") || !strcmp(type, "-STG");
-  Zenith2    = !strcmp(type, "-SIN") || !strcmp(&coords[0].ctype[0], "MM");
-  Zenithal   = !strcmp(type, "-ZEA") || !strcmp(type, "-ZPL") || Zenith1 || Zenith2;
-  if (!Zenithal && !Cartesian && !PseudoCyl) return (FALSE);
+  proj = GetProjection (coords[0].ctype);
+  mode = GetProjectionMode (proj);
+  if (proj == PROJ_NONE) return (FALSE);
+  if (proj == PROJ_MODE_NONE) return (FALSE);
 
   /**** Locally Cartesian Projections ****/
-  if (Cartesian) {
-    if (!strcmp(type, "-WRP")) {
+  if (mode == PROJ_MODE_CARTESIAN) {
+    if (proj == PROJ_WRP) {
       if (mosaic == NULL) return (FALSE);
       RD_to_XY (&Lo, &Mo, ra, dec, mosaic);
@@ -192,5 +184,5 @@
   
   /**** Zenithal Projections ****/
-  if (Zenithal)  {
+  if (mode == PROJ_MODE_ZENITHAL) {
     sdp  = sin(RAD_DEG*coords[0].crval2);
     cdp  = cos(RAD_DEG*coords[0].crval2);
@@ -205,46 +197,52 @@
     if (stht < 0) status = FALSE;
 
-    if (Zenith1) {
-      L =  DEG_RAD * sphi / stht;
-      M = -DEG_RAD * cphi / stht;
-    }
-    if (Zenith2) {
-      L =  DEG_RAD * sphi;
-      M = -DEG_RAD * cphi;
-    }
-    if (!strcmp(type, "-ZEA") || !strcmp(type, "-ZPL")) {
-      Rc = DEG_RAD * M_SQRT2 / sqrt (1 + stht);
-      L =  Rc * sphi;
-      M = -Rc * cphi;
-      status = TRUE;
+    switch (proj) {
+      case PROJ_TAN:
+      case PROJ_DIS:
+	L =  DEG_RAD * sphi / stht;
+	M = -DEG_RAD * cphi / stht;
+	break;
+      case PROJ_SIN:
+	L =  DEG_RAD * sphi;
+	M = -DEG_RAD * cphi;
+	break;
+      case PROJ_ZEA:
+      case PROJ_ZPL:
+	Rc = DEG_RAD * M_SQRT2 / sqrt (1 + stht);
+	L =  Rc * sphi;
+	M = -Rc * cphi;
+	status = TRUE;
+	break;
     }
   }
 
   /**** Other Standard Projections ****/
-  if (PseudoCyl) {
-    if (!strcmp(type, "-AIT")) {
-      phi = RAD_DEG*(ra - coords[0].crval1);
-      theta = RAD_DEG*(dec - coords[0].crval2);
-      P = 1.0 + cos (theta) * cos (0.5*phi);
-      if (P != 0.0) {
-	A =  DEG_RAD * sqrt (2.0 / P);
-	L =  2.0 * A * cos (theta) * sin (0.5*phi);
-	M =  A * sin (theta);
-      } else { 
-	L =  0.0;
-	M =  0.0;
-      }	
-    }
-    if (!strcmp(type, "-GLS")) {
-      phi = ra - coords[0].crval1;
-      theta = dec - coords[0].crval2;
-      L = phi * cos(RAD_DEG * theta);
-      M = theta;
-    }
-    if (!strcmp(type, "-PAR")) {
-      phi = ra - coords[0].crval1;
-      theta = dec - coords[0].crval2;
-      L = phi * (2.0*cos(2*RAD_DEG*theta/3.0) - 1);
-      M = 180.0 * sin (RAD_DEG*theta/3.0);
+  if (mode == PROJ_MODE_PSEUDOCYL) {
+    switch (proj) {
+      case PROJ_AIT:
+	phi = RAD_DEG*(ra - coords[0].crval1);
+	theta = RAD_DEG*(dec - coords[0].crval2);
+	P = 1.0 + cos (theta) * cos (0.5*phi);
+	if (P != 0.0) {
+	  A =  DEG_RAD * sqrt (2.0 / P);
+	  L =  2.0 * A * cos (theta) * sin (0.5*phi);
+	  M =  A * sin (theta);
+	} else { 
+	  L =  0.0;
+	  M =  0.0;
+	}	
+	break;
+      case PROJ_GLS:
+	phi = ra - coords[0].crval1;
+	theta = dec - coords[0].crval2;
+	L = phi * cos(RAD_DEG * theta);
+	M = theta;
+	break;
+      case PROJ_PAR:
+	phi = ra - coords[0].crval1;
+	theta = dec - coords[0].crval2;
+	L = phi * (2.0*cos(2*RAD_DEG*theta/3.0) - 1);
+	M = 180.0 * sin (RAD_DEG*theta/3.0);
+	break;
     }
   }
@@ -270,5 +268,4 @@
       dL = (L - Lo);
       dM = (M - Mo);
-      // fprintf (stderr, "%d: %f,%f : %f,%f : %f,%f : %f,%f\n", i, L, M, X, Y, Lo, Mo, dL, dM);
 
       X += determ * (coords[0].pc2_2*dL - coords[0].pc1_2*dM);
@@ -282,5 +279,4 @@
 
   return (status);
-  
 }
 
@@ -599,2 +595,68 @@
 
 */
+
+  /* PLY is equiv to LIN with higher order terms
+     ZPL is equiv to ZEA with higher order terms
+     DIS is equiv to TAN with higher order terms
+     WRP is equiv to PLY, with implied mosaic */
+
+int GetProjection (char *ctype) {
+  if (!strcmp(&ctype[4], "-ZEA")) return PROJ_ZEA;
+  if (!strcmp(&ctype[4], "-ZPL")) return PROJ_ZPL;
+  if (!strcmp(&ctype[4], "-ARC")) return PROJ_ARC;
+  if (!strcmp(&ctype[4], "-STG")) return PROJ_STG;
+  if (!strcmp(&ctype[4], "-SIN")) return PROJ_SIN;
+  if (!strcmp(&ctype[0], "MM"))   return PROJ_SIN; // note ctype[0]
+  if (!strcmp(&ctype[4], "-TAN")) return PROJ_TAN;
+  if (!strcmp(&ctype[4], "-DIS")) return PROJ_DIS;
+  if (!strcmp(&ctype[4], "-LIN")) return PROJ_LIN;
+  if (!strcmp(&ctype[0], "GENE")) return PROJ_LIN; // note ctype[0]
+  if (!strcmp(&ctype[4], "-PLY")) return PROJ_PLY;
+  if (!strcmp(&ctype[4], "-WRP")) return PROJ_WRP;
+  if (!strcmp(&ctype[4], "-AIT")) return PROJ_AIT;
+  if (!strcmp(&ctype[4], "-GLS")) return PROJ_GLS;
+  if (!strcmp(&ctype[4], "-PAR")) return PROJ_PAR;
+  return PROJ_NONE;
+}
+  
+int SetProjection (char *ctype, int proj) {
+  switch (proj) {
+    case PROJ_ZEA: strcpy(&ctype[4], "-ZEA") return TRUE;
+    case PROJ_ZPL: strcpy(&ctype[4], "-ZPL") return TRUE;
+    case PROJ_ARC: strcpy(&ctype[4], "-ARC") return TRUE;
+    case PROJ_STG: strcpy(&ctype[4], "-STG") return TRUE;
+    case PROJ_SIN: strcpy(&ctype[4], "-SIN") return TRUE;
+    case PROJ_TAN: strcpy(&ctype[4], "-TAN") return TRUE;
+    case PROJ_DIS: strcpy(&ctype[4], "-DIS") return TRUE;
+    case PROJ_LIN: strcpy(&ctype[4], "-LIN") return TRUE;
+    case PROJ_PLY: strcpy(&ctype[4], "-PLY") return TRUE;
+    case PROJ_WRP: strcpy(&ctype[4], "-WRP") return TRUE;
+    case PROJ_AIT: strcpy(&ctype[4], "-AIT") return TRUE;
+    case PROJ_GLS: strcpy(&ctype[4], "-GLS") return TRUE;
+    case PROJ_PAR: strcpy(&ctype[4], "-PAR") return TRUE;
+  }
+  return FALSE;
+}  
+
+int GetProjectionMode (int proj) {
+  switch (proj) {
+    case PROJ_ZEA:
+    case PROJ_ZPL:
+    case PROJ_ARC:
+    case PROJ_STG:
+    case PROJ_SIN:
+    case PROJ_TAN:
+    case PROJ_DIS:
+      return PROJ_MODE_ZENITHAL;
+    case PROJ_LIN: 
+    case PROJ_PLY: 
+    case PROJ_WRP: 
+      return PROJ_MODE_CARTESIAN;
+    case PROJ_AIT:
+    case PROJ_GLS:
+    case PROJ_PAR:
+      return PROJ_MODE_PSEUDOCLY;
+  }
+  return PROJ_MODE_NONE;
+}
+
