Index: trunk/psLib/src/astronomy/psCoord2.c
===================================================================
--- trunk/psLib/src/astronomy/psCoord2.c	(revision 1312)
+++ trunk/psLib/src/astronomy/psCoord2.c	(revision 1325)
@@ -1,2 +1,3 @@
+// This file contains
 
 #include "psType.h"
@@ -53,4 +54,7 @@
 
     for (tmpChip = FPA->chips; tmpChip != NULL; tmpChip++) {
+        tmpCoord = psPlaneTransformApply(out, tmpChip->fromFPA, coord);
+
+
         //XXX     if (this is the correct chip)
         return(tmpChip);
@@ -58,4 +62,30 @@
 }
 
+#define PS_CHECK_VALID_IMAGE_COORDS(X, Y, IMAGE) \
+
+p_psCheckValidImageCoords(double x, double y, 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.
+ 
+XXX: We assume that if a cell has more than one readout, all readouts have
+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,
@@ -63,9 +93,18 @@
 {
     psCell *tmpCell = NULL;
+    psReadout *tmpReadout = NULL;
+    psPlane *tmpCoord = NULL;
 
     for (tmpCell = chip->cells; tmpCell != NULL; tmpCell++) {
-        //XXX        if (this is the correct cell)
-        return(tmpCcell);
+        if (tmpCell->readouts != NULL) {
+            tmpReadout = tmpCell->readouts;
+            tmpCoord = psPlaneTransformApply(out, tmpCell->fromChip, coord);
+            if (p_psCheckValidImageCoords(tmpCoord->x, tmpCoord->y,
+                                          tmpReadout->image)) {
+                return(tmpCell);
+            }
+        }
     }
+    return(NULL);
 }
 
@@ -74,5 +113,5 @@
                            const psCell *cell)
 {
-    return(psPlaneTransformApply(psPlane *out, cell->toChip, in));
+    return(psPlaneTransformApply(out, cell->toChip, in));
 }
 
@@ -81,5 +120,5 @@
                           const psChip *chip)
 {
-    return(psPlaneTransformApply(psPlane *out, chip->toFPA, in));
+    return(psPlaneTransformApply(out, chip->toFPA, in));
 }
 
@@ -88,5 +127,5 @@
                         const psFPA *fpa)
 {
-    return(psPlaneTransformApply(psPlane *out, fpa->toTangentPlane, in));
+    return(psPlaneTransformApply(out, fpa->toTangentPlane, in));
 }
 
@@ -101,91 +140,103 @@
                           const psCell *cell)
 {
-    return(psPlaneTransformApply(psPlane *out, cell->toFPA, in));
-}
-
+    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,
                            const psCell *cell)
 {
-    if (out == NULL) {
-        out = (psSphere *) psAlloc(sizeof(psSphere));
-    }
-
-    return(out);
-}
-
-psSphere *psCoordCelltoSkyQuick(psSphere *out,
-                                const psPlane *in,
-                                const psCell *cell)
-{
-    if (out == NULL) {
-        out = (psSphere *) psAlloc(sizeof(psSphere));
-    }
-
-    return(out);
-}
-
-psPlane *psCoordSkytoTP(psPlane *out,
-                        const psSphere *in,
-                        const psGrommit *grommit)
-{
-    if (out == NULL) {
-        out = (psPlane *) psAlloc(sizeof(psPlane));
-    }
-
-    return(out);
-}
-
-psPlane *psCoordTPtoFPA(psPlane *out,
-                        const psPlane *in,
-                        const psFPA *fpa)
-{
-    return(psPlaneTransformApply(psPlane *out, fpa->fromTangentPlane, in));
-}
-
-psPlane *psCoordFPAtoChip(psPlane *out,
-                          const psPlane *in,
-                          const psChip *chip)
-{
-
-    if (out == NULL) {
-        out = (psPlane *) psAlloc(sizeof(psPlane));
-    }
-
-    return(out);
-}
-
-psPlane *psCoordChiptoCell(psPlane *out,
-                           const psPlane *in,
-                           const psCell *cell)
-{
-    if (out == NULL) {
-        out = (psPlane *) psAlloc(sizeof(psPlane));
-    }
-
-    return(out);
-}
-
-psPlane *psCoordSkytoCell(psPlane *out,
-                          const psSphere *in,
-                          const psCell *cell)
-{
-    if (out == NULL) {
-        out = (psPlane *) psAlloc(sizeof(psPlane));
-    }
-
-    return(out);
-}
-
-psPlane *psCoordSkytoCellQuick(psPlane *out,
+    psPlane *tmp1 = NULL;
+    psPlane *tmp2 = NULL;
+    psFPA *parFPA = (cell->parent)->parent;
+    psGrommit *tmpGrommit = NULL;
+
+    tmp1 = psPlaneTransformApply(tmp1, cell->toFPA, in)
+           tmp2 = psPlaneTransformApply(tmp2, parFPA->toTangentPlane, tmp1)
+                  tmpGrommit = psGrommitAlloc(parFPA->exposure)
+                               tmp3 = psCoordTPtoSky(out, tmp2, psGrommit);
+
+    psFree(tmp1);
+    psFree(tmp2);
+    psFree(tmpGrommit);
+
+    return(psCoordTPtoSky(out, tmp2, psGrommit);
+       }
+
+       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.
+
+           free(tmp1)
+
+           return(out);
+       }
+
+       psPlane *psCoordSkytoTP(psPlane *out,
                                const psSphere *in,
-                               const psCell *cell)
-{
-    if (out == NULL) {
-        out = (psPlane *) psAlloc(sizeof(psPlane));
-    }
-
-    return(out);
-}
-
-
+                               const psGrommit *grommit)
+       {
+           if (out == NULL) {
+               out = (psPlane *) psAlloc(sizeof(psPlane));
+           }
+
+           return(out);
+       }
+
+       psPlane *psCoordTPtoFPA(psPlane *out,
+                               const psPlane *in,
+                               const psFPA *fpa)
+       {
+           return(psPlaneTransformApply(out, fpa->fromTangentPlane, in));
+       }
+
+       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)
+       {
+           out = psCoordSkytoTP(out, in, tmpGrommit);
+           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);
+       }
+
+
