Index: /trunk/psLib/src/astro/psCoord.c
===================================================================
--- /trunk/psLib/src/astro/psCoord.c	(revision 1474)
+++ /trunk/psLib/src/astro/psCoord.c	(revision 1475)
@@ -11,6 +11,6 @@
 *  @author George Gusciora, MHPCC
 *
-*  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2004-08-10 23:59:41 $
+*  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-08-11 20:07:44 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -39,8 +39,10 @@
     }
     out->x = transform->x->coeff[0][0] +
-             (transform->x->coeff[1][0] * coords->x) + (transform->x->coeff[0][1] * coords->y);
+             (transform->x->coeff[1][0] * coords->x) +
+             (transform->x->coeff[0][1] * coords->y);
 
     out->y = transform->y->coeff[0][0] +
-             (transform->y->coeff[1][0] * coords->x) + (transform->y->coeff[0][1] * coords->y);
+             (transform->y->coeff[1][0] * coords->x) +
+             (transform->y->coeff[0][1] * coords->y);
 
     return (out);
@@ -48,5 +50,5 @@
 
 // This transformation takes into account parameters beyond an objects
-// spatial coordinates: term3 and term4.
+// spatial coordinates: term3 and term4 (magnitude and color).
 psPlane* psPlaneDistortApply(psPlane* out,
                              const psPlaneDistort* transform,
Index: /trunk/psLib/src/astronomy/psAstrometry.c
===================================================================
--- /trunk/psLib/src/astronomy/psAstrometry.c	(revision 1474)
+++ /trunk/psLib/src/astronomy/psAstrometry.c	(revision 1475)
@@ -8,6 +8,6 @@
 *  @author George Gusciora, MHPCC
 *
-*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2004-08-10 23:59:41 $
+*  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-08-11 20:07:44 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,7 +19,4 @@
 #include "psAstrometry.h"
 #include "psMemory.h"
-
-static int checkValidChipCoords(double x, double y, psChip* tmpChip);
-static int checkValidImageCoords(double x, double y, psImage* tmpImage);
 
 psExposure* psExposureAlloc(double ra, double dec, double hourAngle,
@@ -44,14 +41,17 @@
 
     return exp;
-
 }
 
 /*
-    Several members of the psGrommit data structure have no direct counterpart
-    in the psExposure structure.  How are we to determine them?
+    XXX: Several members of the psGrommit data structure have no direct
+    counterpart in the psExposure structure.  How are we to determine them?
 */
 psGrommit* psGrommitAlloc(const psExposure* exp)
 {
-    double *slaGrommit = (double *)psAlloc(14 * sizeof(double));
+    // XXX: is this the correct action?
+    if (exp == NULL) {
+        return(NULL);
+    }
+
     psGrommit* grommit = (psGrommit* ) psAlloc(sizeof(psGrommit));
 
@@ -77,8 +77,10 @@
     *(double *)&grommit->siderealTime = 0.0;
 
-    psFree(slaGrommit);
     return (grommit);
 }
 
+/*
+ * XXX: The SDRS states this should be a private p_ps() procedure.
+ */
 void psGrommitFree(psGrommit* grommit)
 {
@@ -86,177 +88,260 @@
 }
 
-psCell* psCellinFPA(psCell* out, const psPlane* coord, const psFPA* FPA)
+psCell* psCellinFPA(psCell* outCell,
+                    const psPlane* fpaCoord,
+                    const psFPA* FPA)
 {
     psChip* tmpChip = NULL;
-    psCell* tmpCell = NULL;
-
-    tmpChip = psChipinFPA(tmpChip, coord, FPA);
-    tmpCell = psCellinChip(tmpCell, coord, tmpChip);
-    return (tmpCell);
-}
-
-/*
-    XXX: do this
-*/
-int checkValidChipCoords(double x, double y, psChip* tmpChip)
-{
-    return (0);
-}
-
-psChip* psChipinFPA(psChip* out, const psPlane* coord, const psFPA* FPA)
+    psPlane* chipCoord = NULL;
+
+    if (FPA == NULL) {
+        return(NULL);
+    }
+    if (fpaCoord == NULL) {
+        return(NULL);
+    }
+
+    // Determine which chip contains the fpaCoords.
+    tmpChip = psChipinFPA(tmpChip, fpaCoord, FPA);
+
+    // Convert to those chip coordinates.
+    chipCoord = psCoordFPAtoChip(chipCoord, fpaCoord, tmpChip);
+
+    // Determine which cell contains those chip coordinates.
+    outCell = psCellinChip(outCell, chipCoord, tmpChip);
+
+    psFree(tmpChip);
+    psFree(chipCoord);
+
+    return (outCell);
+}
+
+int p_psCheckValidImageCoords(double x,
+                              double y,
+                              psImage* tmpImage)
+{
+    if (tmpImage == NULL) {
+        return(0);
+    }
+
+    if ((x < 0.0) ||
+            (x > (double)tmpImage->numCols) ||
+            (y < 0.0) ||
+            (y > (double)tmpImage->numRows)) {
+        return (0);
+    }
+
+    return (1);
+}
+
+/*****************************************************************************
+    XXX: Return the answer in "out".
+ *****************************************************************************/
+psChip* psChipinFPA(psChip* out,
+                    const psPlane* fpaCoord,
+                    const psFPA* FPA)
 {
     psArray* chips = FPA->chips;
     int nChips = chips->n;
-    psPlane* tmpCoord = NULL;
-
+    psPlane* chipCoord = NULL;
+    psCell *tmpCell = NULL;
+
+    // Loop through every chip in this FPA.  Convert the original
+    // FPA coordinates to chip coordinates for that chip.  Then,
+    // determine if any cells in that chip contain those chip
+    // coordinates.
     for (int i = 0; i < nChips; i++) {
         psChip* tmpChip = chips->data[i];
 
-        tmpCoord = psPlaneTransformApply(tmpCoord, tmpChip->fromFPA, coord);
-        if (checkValidChipCoords(tmpCoord->x, tmpCoord->y, tmpChip)) {
-            psFree(tmpCoord);
-            // XXX: George, you didn't use the out parameter!
-            return (tmpChip);
+        chipCoord = psPlaneTransformApply(chipCoord, tmpChip->fromFPA, fpaCoord);
+        tmpCell = psCellinChip(tmpCell, chipCoord, tmpChip);
+        if (tmpCell != NULL) {
+            psFree(chipCoord);
+            return(tmpChip);
         }
-        psFree(tmpCoord);
-    }
-    psFree(tmpCoord);
+        psFree(chipCoord);
+    }
+
     return (NULL);
 }
 
-/*
-    XXX: do this
-*/
-int checkValidImageCoords(double x, double y, psImage* tmpImage)
-{
-    if ((x < 0.0) || (x > (double)tmpImage->numCols) || (y < 0.0) || (y > (double)tmpImage->numRows)) {
-        return (0);
-    }
-    return (1);
-}
-
 /*****************************************************************************
 XXX: We assume that readouts have valid coordinates from the range
-(0,numRows or numCols) in each dimension, and that they are square with the
-x/y axis.
- 
+    (0,numRows or numCols) in each dimension, and that they are square with the
+     x/y axis.
 XXX: We assume that if a cell has more than one readout, all readouts have
-the same coordinates.
- 
+     the same coordinates.
 XXX: if we find no cell with has this coordinate, we return NULL.
- 
 XXX: must deallocate memory.
- *****************************************************************************/
-psCell* psCellinChip(psCell* out, const psPlane* coord, const psChip* chip)
-{
-    int i = 0;
-    psPlane* tmpCoord = NULL;
+XXX: must return the cell in the "outCell" parameter.
+XXX: verify the NULL parameter error handling.
+ *****************************************************************************/
+psCell* psCellinChip(psCell* outCell,
+                     const psPlane* chipCoord,
+                     const psChip* chip)
+{
+    psPlane* cellCoord = NULL;
     psArray* cells;
 
+    // We return NULL if either of the input parameters is NULL.
     if (chip == NULL) {
         return NULL;
     }
 
+    if (chipCoord == NULL) {
+        return NULL;
+    }
+
     cells = chip->cells;
-
     if (cells == NULL) {
         return NULL;
     }
 
-    for (i = 0; i < cells->n; i++) {
+    // We loop over each cell in the chip.  We transform the chipCoord into
+    // a cellCoord for that cell and determine if that cellCoord is valid.
+    // If so, then we return that cell.
+    for (int i = 0; i < cells->n; i++) {
         psCell* tmpCell = (psCell* ) cells->data[i];
         psArray* readouts = tmpCell->readouts;
 
+        // We only check a single readout for this cell since we assume that
+        // all readouts for a cell have identical parameters.
         if (readouts != NULL) {
-            for (int j = 0; j < readouts->n; j++) {
-                psReadout* tmpReadout = readouts->data[j];
-
-                tmpCoord = psPlaneTransformApply(tmpCoord, tmpCell->fromChip, coord);
-                if (checkValidImageCoords(tmpCoord->x, tmpCoord->y, tmpReadout->image)) {
-                    return (tmpCell);
-                }
+            psReadout* tmpReadout = readouts->data[0];
+
+            cellCoord = psPlaneTransformApply(cellCoord,
+                                              tmpCell->fromChip,
+                                              chipCoord);
+
+            if (p_psCheckValidImageCoords(cellCoord->x, cellCoord->y, tmpReadout->image)) {
+                psFree(cellCoord);
+                return (tmpCell);
             }
         }
     }
+
+    psFree(cellCoord);
     return (NULL);
 }
 
-psPlane* psCoordCelltoChip(psPlane* out, const psPlane* in, const psCell* cell)
-{
-    return (psPlaneTransformApply(out, cell->toChip, in));
-}
-
-psPlane* psCoordChipToFPA(psPlane* out, const psPlane* in, const psChip* chip)
-{
-    return (psPlaneTransformApply(out, chip->toFPA, in));
-}
-
-psPlane* psCoordFPAtoTP(psPlane* out, const psPlane* in, const psFPA* fpa)
-{
-    // XXX: This code doesn't work; fpa->toTangentPlane is of the wrong type.
-    // return(psPlaneTransformApply(out, fpa->toTangentPlane, in));
-    return (NULL);
+psPlane* psCoordCelltoChip(psPlane* outCoord,
+                           const psPlane* inCoord,
+                           const psCell* cell)
+{
+    return (psPlaneTransformApply(outCoord, cell->toChip, inCoord));
+}
+
+psPlane* psCoordChipToFPA(psPlane* outCoord,
+                          const psPlane* inCoord,
+                          const psChip* chip)
+{
+    return (psPlaneTransformApply(outCoord, chip->toFPA, inCoord));
+}
+
+/*****************************************************************************
+XXX: must determine the correct values for XXX_Mag and XXX_Col;
+ *****************************************************************************/
+psPlane* psCoordFPAtoTP(psPlane* outCoord,
+                        const psPlane* inCoord,
+                        const psFPA* fpa)
+{
+    float XXX_Mag = 0.0;
+    float XXX_Col = 0.0;
+
+    return(psPlaneDistortApply(outCoord, fpa->toTangentPlane, inCoord,
+                               XXX_Mag, XXX_Col));
 }
 
 // XXX: must wrap SLA_QAPQK here.
-psSphere* psCoordTPtoSky(psSphere* out, const psPlane* in, const psGrommit* grommit)
-{
+psSphere* psCoordTPtoSky(psSphere* outSphere,
+                         const psPlane* tpCoord,
+                         const psGrommit* grommit)
+{
+    if (outSphere == NULL) {
+        outSphere = (psSphere* ) psAlloc(sizeof(psSphere));
+    }
+
     /*
      * double RAP; double DAP;
      * 
-     * extern void sla_OAPQK(TYPE, OB1, OB2, AOPRMS, RAP, DAP); sla_OAPQK(TYPE, OB1, OB2, *grommit, &RAP,
-     * &DAP); */
-
-    return (out);
-}
-
-psPlane* psCoordCellToFPA(psPlane* out, const psPlane* in, const psCell* cell)
-{
-    return (psPlaneTransformApply(out, cell->toFPA, in));
-}
-
-// XXX: This implementation requires a new psGrommit be created for each
-// transformation, as well as a few psPlane structs.  Can this be implemented
-// better?
-psSphere* psCoordCelltoSky(psSphere* out,
-                           const psPlane* in,
+     * extern void sla_OAPQK(TYPE, OB1, OB2, AOPRMS, RAP, DAP); 
+     * sla_OAPQK(TYPE, OB1, OB2, *grommit, &RAP, &DAP);
+     */
+
+    return (outSphere);
+}
+
+psPlane* psCoordCellToFPA(psPlane* fpaCoord,
+                          const psPlane* cellCoord,
+                          const psCell* cell)
+{
+    return (psPlaneTransformApply(fpaCoord, cell->toFPA, cellCoord));
+}
+
+/*****************************************************************************
+XXX: This implementation requires a new psGrommit be created for each
+     transformation, as well as a few psPlane structs.  Can this be implemented
+     better?
+XXX: must determine the correct values for XXX_Mag and XXX_Col;
+ *****************************************************************************/
+psSphere* psCoordCelltoSky(psSphere* skyCoord,
+                           const psPlane* cellCoord,
                            const psCell* cell)
 {
-    psPlane* tmp1 = NULL;
-    psPlane* tmp2 = NULL;
+    psPlane* fpaCoord = NULL;
+    psPlane* tpCoord = NULL;
     psFPA* parFPA = (cell->parent)->parent;
     psGrommit* tmpGrommit = NULL;
-    float XXX1 = 0.0;
-    float XXX2 = 0.0;
-
-
-    tmp1 = psPlaneTransformApply(tmp1, cell->toFPA, in);
-    // XXX:
-    //
-    tmp2 = psPlaneDistortApply(tmp2, parFPA->toTangentPlane, tmp1, XXX1, XXX2);
+    float XXX_Mag = 0.0;
+    float XXX_Col = 0.0;
+
+    // Convert the input cell coordinates to FPA coordinates.
+    fpaCoord = psPlaneTransformApply(fpaCoord, cell->toFPA, cellCoord);
+
+    // Convert the FPA coordinates to tangent plane Coordinates.
+    tpCoord = psPlaneDistortApply(tpCoord, parFPA->toTangentPlane, fpaCoord, XXX_Mag, XXX_Col);
+
+    // Generate a grommit for this FPA.
     tmpGrommit = psGrommitAlloc(parFPA->exposure);
+
+    // Convert the tangent plane Coordinates to sky coordinates.
+    skyCoord = psCoordTPtoSky(skyCoord, tpCoord, tmpGrommit);
+
+    psFree(fpaCoord);
+    psFree(tpCoord);
+    psFree(tmpGrommit);
+
+    return(skyCoord);
+}
+
+/*****************************************************************************
+XXX: not done
+ *****************************************************************************/
+psSphere* psCoordCelltoSkyQuick(psSphere* outSphere,
+                                const psPlane* cellCoord,
+                                const psCell* cell)
+{
+    psPlane* tmp1 = NULL;
+
+    if (outSphere == NULL) {
+        outSphere = (psSphere* ) psAlloc(sizeof(psSphere));
+    }
+
+    tmp1 = psPlaneTransformApply(tmp1, cell->toSky, cellCoord);
+
+    // XXX: Do something to convert the linear coords in tmp1 to spherical
+    // coords in outSphere.
+
     psFree(tmp1);
-    psFree(tmp2);
-    psFree(tmpGrommit);
-
-    return(psCoordTPtoSky(out, tmp2, tmpGrommit));
-}
-
-psSphere* psCoordCelltoSkyQuick(psSphere* out, const psPlane* in, const psCell* cell)
-{
-    psPlane* tmp1 = NULL;
-
-    tmp1 = psPlaneTransformApply(tmp1, cell->toSky, in);
-
-    // XXX: Do something to convert the linear coords in tmp1 to spherical
-    // coords in out.
-
-    psFree(tmp1);
-
-    return (out);
-}
-
-// XXX: must wrap SLA_AOPQK here.
-psPlane* psCoordSkytoTP(psPlane* out, const psSphere* in, const psGrommit* grommit)
+
+    return (outSphere);
+}
+
+/*****************************************************************************
+XXX: must wrap SLA_AOPQK here.
+ *****************************************************************************/
+psPlane* psCoordSkytoTP(psPlane* tpCoord,
+                        const psSphere* in,
+                        const psGrommit* grommit)
 {
     /*
@@ -268,12 +353,12 @@
         double ROB;
      
-        if (out == NULL) {
-            out = (psPlane* ) psAlloc(sizeof(psPlane));
+        if (tpCoord == NULL) {
+            tpCoord = (psPlane* ) psAlloc(sizeof(psPlane));
         }
      
         sla_AOPQK(psSphere->r, psSphere->d, *grommit, &AOB, &ZOB, &HOB, &DOB, &ROB);
-        out->x = XXX;
-        out->y = XXX;
-        return (out);
+        tpCoord->x = XXX;
+        tpCoord->y = XXX;
+        return (tpCoord);
     */
     return(NULL);
@@ -281,46 +366,82 @@
 
 
-/*
-    XXX: What are the XXX1 and XXX2 args supposed to be?
-*/
-psPlane* psCoordTPtoFPA(psPlane* out, const psPlane* in, const psFPA* fpa)
-{
-    float XXX1 = 0.0;
-    float XXX2 = 0.0;
-
-    return (psPlaneDistortApply(out, fpa->fromTangentPlane, in, XXX1, XXX2));
-}
-
-psPlane* psCoordFPAtoChip(psPlane* out, const psPlane* in, const psChip* chip)
-{
-    return (psPlaneTransformApply(out, chip->fromFPA, in));
-}
-
-psPlane* psCoordChiptoCell(psPlane* out, const psPlane* in, const psCell* cell)
-{
-    return (psPlaneTransformApply(out, cell->fromChip, in));
-}
-
-psPlane* psCoordSkytoCell(psPlane* out, const psSphere* in, const psCell* cell)
+/*****************************************************************************
+XXX: must determine the correct values for XXX_Mag and XXX_Col;
+ *****************************************************************************/
+psPlane* psCoordTPtoFPA(psPlane* fpaCoord,
+                        const psPlane* tpCoord,
+                        const psFPA* fpa)
+{
+    float XXX_Mag = 0.0;
+    float XXX_Col = 0.0;
+
+    return (psPlaneDistortApply(fpaCoord, fpa->fromTangentPlane,
+                                tpCoord, XXX_Mag, XXX_Col));
+}
+
+/*****************************************************************************
+XXX: must first determine which chip contains this chipcoord.
+ *****************************************************************************/
+psPlane* psCoordFPAtoChip(psPlane* chipCoord,
+                          const psPlane* fpaCoord,
+                          const psChip* chip)
+{
+    return (psPlaneTransformApply(chipCoord, chip->fromFPA, fpaCoord));
+}
+
+/*****************************************************************************
+XXX: must first determine which cell contains this chipcoord.
+ *****************************************************************************/
+psPlane* psCoordChiptoCell(psPlane* cellCoord,
+                           const psPlane* chipCoord,
+                           const psCell* cell)
+{
+    return (psPlaneTransformApply(cellCoord, cell->fromChip, chipCoord));
+}
+
+/*****************************************************************************
+XXX: once this works, get rid of the individual pointers for the various
+     coords.
+XXX: How do we determine the grommit for the sky->tp coordinate conversion?
+XXX: must determine which cell contains this chipcoord.
+ *****************************************************************************/
+psPlane* psCoordSkytoCell(psPlane* cellCoord,
+                          const psSphere* skyCoord,
+                          const psCell* cell)
 {
     psGrommit* XXXGrommit = NULL;
-    psFPA *whichFPA = NULL;
-    psChip *whichChip = NULL;
-    psCell *whichCell = NULL;
-
-    out = psCoordSkytoTP(out, in, XXXGrommit);
-    out = psCoordTPtoFPA(out, out, whichFPA);
-    out = psCoordFPAtoChip(out, out, whichChip);
-    out = psCoordChiptoCell(out, out, whichCell);
-
-    return (out);
-}
-
-psPlane* psCoordSkytoCellQuick(psPlane* out, const psSphere* in, const psCell* cell)
-{
-    if (out == NULL) {
-        out = (psPlane* ) psAlloc(sizeof(psPlane));
-    }
-
-    return (out);
-}
+    psChip *whichChip = cell->parent;
+    psFPA *whichFPA = whichChip->parent;
+
+    // Convert the skyCoords to tangent plane coords.
+    psPlane *tpCoord = psCoordSkytoTP(tpCoord, skyCoord, XXXGrommit);
+
+    // Convert the tangent plane coords to FPA coords.
+    psPlane *fpaCoord = psCoordTPtoFPA(fpaCoord, tpCoord, whichFPA);
+
+    // Convert the FPA coords to chip coords.
+    psPlane *chipCoord = psCoordFPAtoChip(chipCoord, fpaCoord, whichChip);
+
+    // Convert the chip coords to cell coords.
+    cellCoord = psCoordChiptoCell(cellCoord, chipCoord, cell);
+
+    psFree(tpCoord);
+    psFree(fpaCoord);
+    psFree(chipCoord);
+
+    return (cellCoord);
+}
+
+/*****************************************************************************
+XXX: It is unclear how to perform this transformation.
+ *****************************************************************************/
+psPlane* psCoordSkytoCellQuick(psPlane* cellCoord,
+                               const psSphere* skyCoord,
+                               const psCell* cell)
+{
+    if (cellCoord == NULL) {
+        cellCoord = (psPlane* ) psAlloc(sizeof(psPlane));
+    }
+
+    return (cellCoord);
+}
Index: /trunk/psLib/src/astronomy/psAstrometry.h
===================================================================
--- /trunk/psLib/src/astronomy/psAstrometry.h	(revision 1474)
+++ /trunk/psLib/src/astronomy/psAstrometry.h	(revision 1475)
@@ -9,6 +9,6 @@
 *  @author George Gusciora, MHPCC
 *
-*  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2004-08-10 23:59:41 $
+*  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-08-11 20:07:44 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -238,4 +238,6 @@
                          );
 
+/// XXX: This is defined as a private p_ps() type of function in the SDRS.
+/// I changed this to a public function.
 void psGrommitFree(psGrommit *grommit);
 
