Index: trunk/pswarp/src/pswarpMapGrid.c
===================================================================
--- trunk/pswarp/src/pswarpMapGrid.c	(revision 12505)
+++ trunk/pswarp/src/pswarpMapGrid.c	(revision 12523)
@@ -1,6 +1,8 @@
 # include "pswarp.h"
 
-// construct a grid with superpixel spacing of nXpix, nYpix
-// XXX for the moment, ignore readout->cell->chip offsets
+// pswarpMapGridFromImage builds a set (a grid) of locally-linear maps which convert the source
+// coordinates (src) to destination coordinates (dest).  we construct a grid with superpixel
+// spacing of nXpix, nYpix.  The transformation for each grid cell is valid for the superpixel.
+// The grid over-fills the source image so ever source image pixel is guaranteed to have a map.
 pswarpMapGrid *pswarpMapGridFromImage (pmReadout *dest, pmReadout *src, int nXpix, int nYpix) {
 
@@ -8,18 +10,22 @@
     int j, nj;
 
-    // XXX I was trying to match the grids too closely
-    // split the difference of the remainder
-    // int xMin = 0.5*(src->image->numCols % nXpix);
-    // int yMin = 0.5*(src->image->numRows % nYpix);
-    int xMin = 0;
-    int yMin = 0;
-
-    int nXpts = src->image->numCols / nXpix + 1;
-    if (src->image->numCols % nXpix) nXpts ++;
-    int nYpts = src->image->numRows / nYpix + 1;
-    if (src->image->numRows % nYpix) nYpts ++;
-
+    // start counting from the center of the superpixels
+    int xMin = 0.5*nXpix;
+    int yMin = 0.5*nYpix;
+
+    // the map is defined for coordinates in the image parent frame.
+    int Nx = src->image->numCols + src->image->col0;
+    int Ny = src->image->numRows + src->image->row0;
+
+    // allocate an extra superpixel to carry the remainder
+    int nXpts = Nx / nXpix;
+    int nYpts = Ny / nYpix;
+    if (Nx % nXpix) nXpts ++;
+    if (Ny % nYpix) nYpts ++;
+
+    // create the grid of maps
     pswarpMapGrid *grid = pswarpMapGridAlloc (nXpts, nYpts);
 
+    // measure the map for the center of each superpixel
     for (ni = 0, i = xMin; ni < nXpts; i += nXpix, ni++) {
 	for (nj = 0, j = yMin; nj < nYpts; j += nYpix, nj++) {
@@ -35,24 +41,30 @@
 }
 
+// set the grid coordinate (gridX,gridY) for the given source image coordinate (ix,iy)
 bool pswarpMapGridSetGrid (pswarpMapGrid *grid, int ix, int iy, int *gridX, int *gridY) {
 
-    *gridX = (ix - grid->xMin + 0.5*grid->nXpix) / grid->nXpix;
-    *gridY = (iy - grid->yMin + 0.5*grid->nYpix) / grid->nYpix;
+    *gridX = 0.5 + (ix - grid->xMin) / (double) grid->nXpix;
+    *gridY = 0.5 + (iy - grid->yMin) / (double) grid->nYpix;
     return true;
 }
 
-bool pswarpMapGridNextGrid_X (pswarpMapGrid *grid, int gridX, int *nextX) {
-
-    *nextX = gridX*grid->nXpix + grid->xMin + 0.5*grid->nXpix;
-    return true;
-}
-
-bool pswarpMapGridNextGrid_Y (pswarpMapGrid *grid, int gridY, int *nextY) {
-
-    *nextY = gridY*grid->nYpix + grid->yMin + 0.5*grid->nYpix;
-    return true;
-}
-
-// measure the max error accumulated in appling one grid point to its neighbors
+// given the specified grid coordinate (gridX), return the x-coordinate for the source image
+// corresponding to the next grid cell
+int pswarpMapGridNextGrid_X (pswarpMapGrid *grid, int gridX) {
+
+    int nextX = (gridX + 0.5)*grid->nXpix + grid->xMin;
+    return nextX;
+}
+
+// given the specified grid coordinate (gridY), return the y-coordinate for the source image
+// corresponding to the next grid cell
+int pswarpMapGridNextGrid_Y (pswarpMapGrid *grid, int gridY) {
+
+    int nextY = (gridY + 0.5)*grid->nYpix + grid->yMin;
+    return nextY;
+}
+
+// measure the max error accumulated in applying one grid point to its neighbors
+// XXX double-check this
 double pswarpMapGridMaxError (pswarpMapGrid *grid) {
 
@@ -76,4 +88,5 @@
 }
 
+// given the source coordinate (inX,inY), return the destination coordinate (outX,outY)
 bool pswarpMapApply (double *outX, double *outY, pswarpMap *map, double inX, double inY) {
 
@@ -84,5 +97,7 @@
 }
 
-// determine the map for the given pixel from src to dest. pixel is in src coords
+// determine the (linear) map for the given pixel (ix,iy) from source image (src) to the destination image (dest)
+// pixel is in src coords. input and output pixel coordinates are in the parent frame of the image (Note that the
+// astrometric transformations are supplied for the parent image coordinate frame.
 bool pswarpMapSetLocalModel (pswarpMap *map, pmReadout *dest, pmReadout *src, int ix, int iy) {
 
