Index: trunk/psLib/src/astro/psCoord.c
===================================================================
--- trunk/psLib/src/astro/psCoord.c	(revision 3450)
+++ trunk/psLib/src/astro/psCoord.c	(revision 3470)
@@ -10,6 +10,6 @@
 *  @author GLG, MHPCC
 *
-*  @version $Revision: 1.58 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-03-18 21:34:43 $
+*  @version $Revision: 1.59 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-03-22 01:35:49 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -396,5 +396,5 @@
 void projectionFree(psProjection *p)
 {
-    psFree(p);
+    // There are no dynamically allocated items
 }
 
@@ -413,5 +413,5 @@
     p->type = type;
 
-    p_psMemSetDeallocator(p, (psFreeFcn) planeFree);
+    p_psMemSetDeallocator(p, (psFreeFcn) projectionFree);
     return(p);
 }
@@ -547,32 +547,42 @@
     PS_PTR_CHECK_NULL(position2, NULL);
 
+    // Check positions near 90 degree and issue warnings if necessary
     if (position1->d >= DEG_TO_RAD(90.0)) {
-        psLogMsg(__func__, PS_LOG_WARN, "WARNING: psDeproject(): position1->d is larger than 90 degrees.  Returning NULL.\n");
-        return(NULL);
+        psLogMsg(__func__, PS_LOG_WARN,
+                 "WARNING: psDeproject(): position1->d is larger than 90 degrees.  Returning NULL.");
+        return NULL;
     }
     if (position2->d >= DEG_TO_RAD(90.0)) {
-        psLogMsg(__func__, PS_LOG_WARN, "WARNING: psDeproject(): position2->d is larger than 90 degrees.  Returning NULL.\n");
-        return(NULL);
-    }
-
-    psPlane* lin;
-    psProjection proj;
-    psSphere* tmp;
-
+        psLogMsg(__func__, PS_LOG_WARN,
+                 "WARNING: psDeproject(): position2->d is larger than 90 degrees.  Returning NULL.");
+        return NULL;
+    }
+
+    // Allocate return structure
+    psSphere* tmp = psSphereAlloc();
+
+    // Mode is LINEAR - Use first position as projection center and project second point
+    // onto tangent plane, set point projected into psSphere structure x->r y->d
     if (mode == PS_LINEAR) {
-        proj.R = position1->r;
-        proj.D = position1->d;
-        proj.Xs = 1.0;
-        proj.Ys = 1.0;
-        proj.type = PS_PROJ_TAN;
-
-        lin = psProject(position1, &proj);
-        lin = psProject(position2, &proj);
-        tmp = psDeproject(lin, &proj);
+        psProjection* proj = psProjectionAlloc(position1->r,
+                                               position1->d,
+                                               1.0,
+                                               1.0,
+                                               PS_PROJ_TAN);
+
+        // Perform projection onto tangent plane
+        psPlane* lin = psProject(position2, proj);
+
+        // Set return values
+        tmp->r = lin->x;
+        tmp->d = lin->y;
+
+        // Free data structures allocated
+        psFree(proj);
         psFree(lin);
-        // XXX: Do we need to convert units in tmp?
-        return (tmp);
+
+        // Mode is SPHERICAL - Get difference between positiion 1 and position 2 and convert
+        // offset value from radians to desired units and return
     } else if (mode == PS_SPHERICAL) {
-        tmp = (psSphere* ) psAlloc(sizeof(psSphere));
         tmp->r = position2->r - position1->r;
         tmp->d = position2->d - position1->d;
@@ -585,4 +595,5 @@
         tmp->dErr = 0.0;
 
+        // Convert to desired units
         if (unit == PS_ARCSEC) {
             tmp->r = RAD_TO_SEC(tmp->r);
@@ -600,14 +611,18 @@
                     unit);
             psFree(tmp);
-            return(NULL);
+            return NULL;
         }
-
-        return (tmp);
-    }
-
-    psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-            PS_ERRORTEXT_psCoord_OFFSET_MODE_UNKNOWN,
-            mode);
-    return (NULL);
+        // Invalid mode
+    } else {
+
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psCoord_OFFSET_MODE_UNKNOWN,
+                mode);
+        psFree(tmp);
+        return NULL;
+    }
+
+    // Return value
+    return tmp;
 }
 
@@ -631,23 +646,37 @@
     PS_PTR_CHECK_NULL(offset, NULL);
 
-    psPlane lin;
     psSphere* tmp;
-    psProjection proj;
     psF64 tmpR = 0.0;
     psF64 tmpD = 0.0;
 
+    // If mode is linear then set position to projection center
+    // and offset to linear coordinate then deproject to obtain
+    // new sphere coordinate
     if (mode == PS_LINEAR) {
-        proj.R = position->r;
-        proj.D = position->d;
-        proj.Xs = 1.0;
-        proj.Ys = 1.0;
-        proj.type = PS_PROJ_TAN;
-
-        lin.x = offset->r;
-        lin.y = offset->d;
-
-        tmp = psDeproject(&lin, &proj);
-        return (tmp);
+
+        // Allocate plane coordinate and set coordinate
+        psPlane*  lin = psPlaneAlloc();
+        lin->x = offset->r;
+        lin->y = offset->d;
+
+        // Allocate and set projection structure
+        psProjection* proj = psProjectionAlloc(position->r,
+                                               position->d,
+                                               1.0,
+                                               1.0,
+                                               PS_PROJ_TAN);
+
+        // Project tangent plane coord to spherical coord
+        tmp = psDeproject(lin, proj);
+
+        // Free data structures used
+        psFree(proj);
+        psFree(lin);
+
+        // If mode is spherical then convert offset to radians, add the offset
+        // to the position and wrap to 0 to 2pi
     } else if (mode == PS_SPHERICAL) {
+
+        // Convert offset unit to radians
         if (unit == PS_ARCSEC) {
             tmpR = SEC_TO_RAD(offset->r);
@@ -666,8 +695,11 @@
                     PS_ERRORTEXT_psCoord_UNITS_UNKNOWN,
                     unit);
-            return(NULL);
+            return NULL;
         }
 
-        tmp = (psSphere* ) psAlloc(sizeof(psSphere));
+        // Allocate sphere structure to return
+        tmp = psSphereAlloc();
+
+        // Add offset and wrap to 0 to 2PI if necessary
         tmp->r = position->r + tmpR;
         tmp->r = fmod(tmp->r, 2.0*PS_PI);
@@ -677,12 +709,14 @@
         tmp->dErr = 0.0;
 
-        return (tmp);
-    }
-
-    psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-            PS_ERRORTEXT_psCoord_OFFSET_MODE_UNKNOWN,
-            mode);
-
-    return (NULL);
+        // Invalid mode report error
+    } else {
+
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psCoord_OFFSET_MODE_UNKNOWN,
+                mode);
+        return NULL;
+    }
+
+    return tmp;
 }
 
