Index: /branches/eam_branch_20080719/pswarp/src/Makefile.am
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/Makefile.am	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/Makefile.am	(revision 18753)
@@ -17,7 +17,6 @@
 	pswarpSetMaskBits.c		\
 	pswarpThreadLauncher.c	        \
-	pswarpTransformReadout_Opt.c	\
-	pswarpTransformReadout_Threaded.c   \
-	pswarpTransformReadout_Unthreaded.c \
+	pswarpTransformReadout.c	\
+	pswarpTransformSources.c \
 	pswarpTransformTile.c		\
 	pswarpVersion.c            
Index: /branches/eam_branch_20080719/pswarp/src/pswarp.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarp.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarp.c	(revision 18753)
@@ -17,8 +17,4 @@
     psLibInit(NULL);
 
-    // create the thread pool with 4 threads, supplying our thread launcher function
-    // XXX need to determine the number of threads from the config data
-    psThreadPoolInit (4, &pswarpThreadLauncher);
-    
     // model inits are needed in pmSourceIO
     // models defined in psphot/src/models are not available in psastro
Index: /branches/eam_branch_20080719/pswarp/src/pswarp.h
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarp.h	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarp.h	(revision 18753)
@@ -10,4 +10,6 @@
 #include <psmodules.h>
 #include <psphot.h>
+
+#define THREADED 1
 
 #include "pswarpErrorCodes.h"
@@ -33,5 +35,5 @@
     int nXpts, nYpts;                   // number of x,y samples in the grid
     int nXpix, nYpix;                   // x,y spacing in src image pixels of grid samples
-    int xMin,  yMin;                    // coordinate of first grid sample
+    double xMin,  yMin;			// coordinate of first grid sample
 } pswarpMapGrid;
 
@@ -52,7 +54,4 @@
 } pswarpTransformTileArgs;
 
-bool pswarpTransformReadout_Unthreaded(pmReadout *output, pmReadout *input, pmConfig *config);
-bool pswarpTransformReadout_Threaded(pmReadout *output, pmReadout *input, pmConfig *config);
-
 pswarpTransformTileArgs *pswarpTransformTileArgsAlloc();
 bool pswarpTransformTile (pswarpTransformTileArgs *args);
@@ -66,5 +65,5 @@
 void pswarpCleanup (pmConfig *config);
 bool pswarpTransformReadout (pmReadout *output, pmReadout *input, pmConfig *config);
-bool pswarpTransformReadout_Opt (pmReadout *output, pmReadout *input, pmConfig *config);
+bool pswarpTransformSources(pmReadout *output, pmReadout *input, pmConfig *config);
 
 bool pswarpMatchRange (int *minX, int *minY, int *maxX, int *maxY, pmReadout *dest, pmReadout *src);
@@ -75,4 +74,5 @@
 pswarpMapGrid *pswarpMapGridFromImage (pmReadout *dest, pmReadout *src, int nXpix, int nYpix);
 bool pswarpMapGridSetGrid (pswarpMapGrid *grid, int ix, int iy, int *gridX, int *gridY);
+bool pswarpMapGridCoordRange (pswarpMapGrid *grid, int gridX, int gridY, psPlane *min, psPlane *max);
 int pswarpMapGridNextGrid_X (pswarpMapGrid *grid, int gridX);
 int pswarpMapGridNextGrid_Y (pswarpMapGrid *grid, int gridY);
Index: /branches/eam_branch_20080719/pswarp/src/pswarpArguments.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpArguments.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpArguments.c	(revision 18753)
@@ -39,4 +39,16 @@
                          "Filename for statistics of output image", argv[N]);
         psArgumentRemove(N, &argc, argv);
+    }
+
+    // Number of threads
+    if ((N = psArgumentGet(argc, argv, "-threads"))) {
+        psArgumentRemove(N, &argc, argv);
+	int nThreads = atoi(argv[N]);
+        psMetadataAddS32(config->arguments, PS_LIST_TAIL, "NTHREADS", 0, "number of warp threads", nThreads);
+        psArgumentRemove(N, &argc, argv);
+
+	// create the thread pool with number of desired threads, supplying our thread launcher function
+	// XXX need to determine the number of threads from the config data
+	psThreadPoolInit (nThreads, &pswarpThreadLauncher);
     }
 
Index: /branches/eam_branch_20080719/pswarp/src/pswarpCleanup.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpCleanup.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpCleanup.c	(revision 18753)
@@ -9,4 +9,5 @@
     pmModelClassCleanup ();
     psTimeFinalize ();
+    psThreadPoolFinalize ();
     pmConceptsDone ();
     pmConfigDone ();
Index: /branches/eam_branch_20080719/pswarp/src/pswarpLoop.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpLoop.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpLoop.c	(revision 18753)
@@ -241,5 +241,5 @@
                 }
 
-                pswarpTransformReadout_Opt(output, readout, config);
+                pswarpTransformReadout(output, readout, config);
 
                 pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
Index: /branches/eam_branch_20080719/pswarp/src/pswarpMapGrid.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpMapGrid.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpMapGrid.c	(revision 18753)
@@ -4,5 +4,5 @@
 // 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.
+// The grid over-fills the source image so every source image pixel is guaranteed to have a map.
 pswarpMapGrid *pswarpMapGridFromImage (pmReadout *dest, pmReadout *src, int nXpix, int nYpix) {
 
@@ -11,6 +11,6 @@
 
     // start counting from the center of the superpixels
-    int xMin = 0.5*nXpix;
-    int yMin = 0.5*nYpix;
+    double xMin = 0.5*nXpix;
+    double yMin = 0.5*nYpix;
 
     // the map is defined for coordinates in the image parent frame.
@@ -42,8 +42,22 @@
 
 // set the grid coordinate (gridX,gridY) for the given source image coordinate (ix,iy)
+// XXX return true if the result is on the src image, false otherwise (???)
 bool pswarpMapGridSetGrid (pswarpMapGrid *grid, int ix, int iy, int *gridX, int *gridY) {
 
     *gridX = 0.5 + (ix - grid->xMin) / (double) grid->nXpix;
     *gridY = 0.5 + (iy - grid->yMin) / (double) grid->nYpix;
+
+    return true;
+}
+
+// given the specified grid coordinate (gridX, gridY), return the min and max coordinates for the tile
+bool pswarpMapGridCoordRange (pswarpMapGrid *grid, int gridX, int gridY, psPlane *min, psPlane *max) {
+
+    min->x = (gridX - 0.5)*grid->nXpix + grid->xMin;
+    min->y = (gridY - 0.5)*grid->nYpix + grid->yMin;
+
+    max->x = min->x + grid->nXpix;
+    max->y = min->y + grid->nYpix;
+
     return true;
 }
Index: /branches/eam_branch_20080719/pswarp/src/pswarpThreadLauncher.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpThreadLauncher.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpThreadLauncher.c	(revision 18753)
@@ -1,3 +1,4 @@
 # include "pswarp.h"
+// XXX add in a thread exit api (set a local 'exit' variable)
 
 bool pswarpThreadTest (psArray *args) {
Index: /branches/eam_branch_20080719/pswarp/src/pswarpTransformReadout.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpTransformReadout.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpTransformReadout.c	(revision 18753)
@@ -1,78 +1,152 @@
 # include "pswarp.h"
 
-bool pswarpTransformReadout (pmReadout *output, pmReadout *input, pmConfig *config) {
-
+// NOTE: in this function, the coordinates are transformed from the OUTPUT to the INPUT
+bool pswarpTransformReadout(pmReadout *output, pmReadout *input, pmConfig *config)
+{
     // XXX this implementation currently ignores the use of the region
     psImage *region = NULL;
-    pmCell *cell = NULL;
 
-    // select the current recipe
-    // psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, PSWARP_RECIPE);
+    psTimerStart ("warp");
 
-    int outNx = output->image->numCols;
-    int outNy = output->image->numRows;
+    // Get warp parameters
+    bool mdok;
+    int nGridX = psMetadataLookupS32(NULL, config->arguments, "GRID.NX");
+    int nGridY = psMetadataLookupS32(NULL, config->arguments, "GRID.NY");
+    psImageInterpolateMode interpolationMode = psMetadataLookupS32(NULL, config->arguments, "INTERPOLATION.MODE");
 
-    psPlane *inPix = psPlaneAlloc();    // Coordinates on the input detector
-    psPlane *outPix = psPlaneAlloc();   // Coordinates on the output detector
+    // load the recipe
+    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSWARP_RECIPE);
+    psAssert (recipe, "missing recipe %s", PSWARP_RECIPE);
 
-    psPlane *FP = psPlaneAlloc();       // Coordinates on the focal plane
-    psPlane *TP = psPlaneAlloc();       // Coordinates on the tangent plane
-    psSphere *sky = psSphereAlloc();    // Coordinates on the sky
+    // output mask bits
+    psMaskType maskIn   = psMetadataLookupU8(&mdok, recipe, "MASK.INPUT"); 
+    psMaskType maskPoor = pmConfigMaskGet("POOR.WARP", config); 
+    psMaskType maskBad  = pmConfigMaskGet("BAD.WARP", config); 
+    psAssert (mdok, "MASK.INPUT was not defined");
 
-    cell = input->parent;
-    pmChip *chipInput = cell->parent;
-    pmFPA *fpaInput = chipInput->parent;
+    int nThreads = psMetadataLookupS32 (&mdok, config->arguments, "NTHREADS");
+    if (!mdok) nThreads = 0;
 
-    cell = output->parent;
-    pmChip *chipOutput = cell->parent;
-    pmFPA *fpaOutput = chipOutput->parent;
+    // Flux fraction for "poor"
+    float poorFrac = psMetadataLookupF32(NULL, config->arguments, "POOR.FRAC"); 
 
-    psF32 **outData = output->image->data.F32;
-    psImage *inImage = input->image;
+    // pswarpMapGridFromImage builds a set of locally-linear maps which convert the
+    // output coordinates to input coordinates
+    pswarpMapGrid *grid = pswarpMapGridFromImage (input, output, nGridX, nGridY);
 
-    psImageInterpolateOptions *interp = psImageInterpolateOptionsAlloc(PS_INTERPOLATE_BILINEAR, inImage,
-                                                                       NULL, NULL, 0, NAN, NAN, 0, 0, 0.0);
+    // XXX optionally modify the grid based on this result and force the maxError < XXX
+    double maxError = pswarpMapGridMaxError (grid);
+    psLogMsg ("pswarp", 3, "maximum error using this grid sampling: %f\n", maxError);
 
-    // Iterate over the output image pixels
-    for (int y = 0; y < outNy; y++) {
-        for (int x = 0; x < outNx; x++) {
-            // Only transform those pixels requested
-            if (region && region->data.U8[y][x]) continue;
+    // Interpolation options : move these from the arguments to explicit assignments
+    psImageInterpolateOptions *interp = psImageInterpolateOptionsAlloc(interpolationMode, input->image, input->weight, input->mask, maskIn, NAN, NAN, maskBad, maskPoor, poorFrac);
 
-            // XXX double check this 1/2 pixel offset
-            outPix->x = (double)x + 0.5;
-            outPix->y = (double)y + 0.5;
+    if (input->weight && !output->weight) {
+	output->weight = psImageAlloc(output->image->numCols, output->image->numRows, PS_TYPE_F32);
+	psImageInit(output->weight, NAN);
+    }
+    if ((input->mask || maskPoor || maskBad) && !output->mask) {
+	output->mask = psImageAlloc(output->image->numCols, output->image->numRows, PS_TYPE_MASK);
+	psImageInit(output->mask, maskBad);
+    }
 
-            psPlaneTransformApply(FP, chipOutput->toFPA, outPix);
-            psPlaneTransformApply (TP, fpaOutput->toTPA, FP);
-            psDeproject (sky, TP, fpaOutput->toSky);
+    // total number of good pixels across all tiles (summed below)
+    int goodPixels = 0;
 
-            psProject (TP, sky, fpaInput->toSky);
-            psPlaneTransformApply (FP, fpaInput->fromTPA, TP);
-            psPlaneTransformApply (inPix, chipInput->fromFPA, FP);
+    // create jobs and supply them to the threads
+    for (int gridY = 0; gridY < grid->nYpts; gridY++) {
+	for (int gridX = 0; gridX < grid->nXpts; gridX++) {
 
-            // XXX get interpolation method from the recipe
-            double value;
-            if (!psImageInterpolate(&value, NULL, NULL, inPix->x, inPix->y, interp)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to interpolate image.");
-                psFree(interp);
-                psFree(inPix);
-                psFree(outPix);
-                psFree(FP);
-                psFree(TP);
-                psFree(sky);
-                return false;
-            }
+	    pswarpTransformTileArgs *args = pswarpTransformTileArgsAlloc();
 
-            outData[y][x] = value;
-            // modify zero and scale?
+	    // these items are just views to the data; they are not freed with args
+	    args->input = input;
+	    args->output = output;
+	    args->grid = grid;
+	    args->interp = interp;
+	    args->region = region;
+
+	    args->gridX = gridX;
+	    args->gridY = gridY;
+	    args->goodPixels = 0;
+
+	    if (nThreads) {
+		// allocate a job
+		psThreadJob *job = psThreadJobAlloc ("PSWARP_TRANSFORM_TILE", 0);
+
+		// construct the arguments for this job
+		// job is pswarpTransformTile (gridX, gridY);
+		psArrayAdd (job->args, 1, args);
+		// fprintf (stderr, "adding job %d,%d, Nargs: %ld\n", gridX, gridY, job->args->n);
+		psThreadJobAddPending (job);
+	    } else {
+		pswarpTransformTile (args);
+		goodPixels += args->goodPixels;
+	    }
+	    psFree (args);
+	}
+    }
+
+    // wait for the threads to finish and manage results
+    if (nThreads) {
+	// wait here for the threaded jobs to finish
+	if (!psThreadPoolWait ()) {
+	    psError(PS_ERR_UNKNOWN, false, "Unable to interpolate image.");
+	    return false;
+	}
+	fprintf (stderr, "success for threaded jobs\n");
+
+	// each job records its own goodPixel values; sum them here
+	// we have only supplied one type of job, so we can assume the types here
+	psThreadJob *job = NULL;
+	while ((job = psThreadJobGetDone()) != NULL) {
+	    if (job->args->n < 1) {
+		fprintf (stderr, "error with job\n");
+	    } else {
+		pswarpTransformTileArgs *args = job->args->data[0];
+		// fprintf (stderr, "finished job %d,%d, Nargs: %ld\n", args->gridX, args->gridY, job->args->n);
+		goodPixels += args->goodPixels;
+	    }
+	    psFree (job);
+	}
+    }
+    psFree(interp);
+    psFree(grid);
+
+
+    // Store the variance factor and number of good pixels
+    if (goodPixels > 0) {
+	// Variance factor: large factor --> small scale
+        float varFactor = psImageInterpolateVarianceFactor(input->image->numCols / 2.0 + input->image->col0, input->image->numRows / 2.0 + input->image->row0, interp); 
+        psMetadataItem *vfItem = psMetadataLookup(output->analysis, PSWARP_ANALYSIS_VARFACTOR);
+        if (vfItem) {
+            psMetadataItem *goodpixItem = psMetadataLookup(output->analysis, PSWARP_ANALYSIS_GOODPIX);
+            psAssert(goodpixItem, "It should be where we left it!");
+            psAssert(vfItem->type == PS_TYPE_F32 && goodpixItem->type == PS_TYPE_S64,
+                     "Should be the type we said.");
+
+            vfItem->data.F32 += varFactor * goodPixels;
+            goodpixItem->data.S64 += goodPixels;
+        } else {
+            psMetadataAddF32(output->analysis, PS_LIST_TAIL, PSWARP_ANALYSIS_VARFACTOR, 0,
+                             "Variance factor weighted by the good pixels", varFactor * goodPixels);
+            psMetadataAddS64(output->analysis, PS_LIST_TAIL, PSWARP_ANALYSIS_GOODPIX, 0,
+                             "Number of good pixels", goodPixels);
         }
     }
-    psFree(interp);
-    psFree(inPix);
-    psFree(outPix);
-    psFree(FP);
-    psFree(TP);
-    psFree(sky);
+
+    if (goodPixels > 0) {
+	if (!pswarpTransformSources (output, input, config)) {
+	    psError(PS_ERR_UNKNOWN, false, "Unable to interpolate image.");
+	    return false;
+	}
+    }
+
+    // XXX should we not write anything out if there are no good pixels?
+    output->data_exists = true;
+    output->parent->data_exists = true;
+    output->parent->parent->data_exists = true;
+
+    psLogMsg ("pswarp", 3, "warping analysis: %f sec\n", psTimerMark ("warp"));
 
     return true;
Index: /branches/eam_branch_20080719/pswarp/src/pswarpTransformReadout_Threaded.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpTransformReadout_Threaded.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpTransformReadout_Threaded.c	(revision 18753)
@@ -1,5 +1,6 @@
 # include "pswarp.h"
 
-#define SOURCE_ARRAY_BUFFER 100         // Size to grow the array of sources at a time
+# define THREADED 1
+# define SOURCE_ARRAY_BUFFER 100         // Size to grow the array of sources at a time
 
 // NOTE: in this function, the coordinates are transformed from the OUTPUT to the INPUT
@@ -90,4 +91,5 @@
 	    args->goodPixels = 0;
 
+# if (THREADED)	    
 	    // allocate a job
 	    psThreadJob *job = psThreadJobAlloc ("PSWARP_TRANSFORM_TILE", 0);
@@ -97,7 +99,13 @@
 	    psArrayAdd (job->args, 1, args);
 	    psThreadJobAddPending (job);
+# else
+	    pswarpTransformTile (args);
+	    goodPixels += args->goodPixels;
+# endif
 	    psFree (args);
 	}
     }
+
+# if (THREADED)	    
     // wait here for the threaded jobs to finish
     if (!psThreadPoolWait ()) {
@@ -115,6 +123,8 @@
 	psFree (job);
     }
+# endif
     psFree(interp);
     psFree(grid);
+
 
     // Store the variance factor and number of good pixels
Index: /branches/eam_branch_20080719/pswarp/src/pswarpTransformSources.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpTransformSources.c	(revision 18753)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpTransformSources.c	(revision 18753)
@@ -0,0 +1,110 @@
+# include "pswarp.h"
+
+# define SOURCE_ARRAY_BUFFER 100         // Size to grow the array of sources at a time
+
+// NOTE: in this function, the coordinates are transformed from the OUTPUT to the INPUT
+bool pswarpTransformSources(pmReadout *output, pmReadout *input, pmConfig *config)
+{
+
+    // find the output pixel range
+    int minX, minY, maxX, maxY;
+    pswarpMatchRange (&minX, &minY, &maxX, &maxY, input, output);
+
+    // Get warp parameters
+    bool mdok;
+    int nGridX = psMetadataLookupS32(NULL, config->arguments, "GRID.NX");
+    int nGridY = psMetadataLookupS32(NULL, config->arguments, "GRID.NY");
+
+    // Transform sources
+    psArray *inSources = psMetadataLookupPtr(&mdok, input->analysis, "PSPHOT.SOURCES"); // Sources in source
+    if (!inSources) return true;
+
+    pswarpMapGrid *sourceGrid = pswarpMapGridFromImage(output, input, nGridX, nGridY); // Grid for sources
+
+    psArray *outSources = psMemIncrRefCounter(psMetadataLookupPtr(&mdok, output->analysis, "PSPHOT.SOURCES")); // Target sources
+    if (!outSources) {
+	outSources = psArrayAllocEmpty(SOURCE_ARRAY_BUFFER);
+	psMetadataAddPtr(output->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,
+			 "Warped sources", outSources);
+    }
+
+    // XXX it is probably not necessary to use the locally linear transformations we should
+    // be using the full astrometry: since there are 100 - 1000 fewer sources than pixels,
+    // this does not cost us so much time.
+    for (int i = 0; i < inSources->n; i++) {
+	pmSource *source = inSources->data[i]; // Source of interest
+	pmModel *model = source->modelPSF; // Model for this source
+	float xIn, yIn;             // Coordinates of source
+	xIn = model->params->data.F32[PM_PAR_XPOS] - input->image->col0;
+	yIn = model->params->data.F32[PM_PAR_YPOS] - input->image->row0;
+
+	int xGrid, yGrid;           // Grid coordinates for local map
+	if (!pswarpMapGridSetGrid(sourceGrid, xIn + 0.5, yIn + 0.5, &xGrid, &yGrid)) {
+	    psError(PS_ERR_UNKNOWN, false, "Unable to get grid coordinates for source at %f,%f\n",
+		    xIn, yIn);
+	    psFree(outSources);
+	    psFree(sourceGrid);
+	    return false;
+	}
+	if (xGrid < 0 || xGrid >= sourceGrid->nXpts || yGrid < 0 || yGrid >= sourceGrid->nYpts) {
+	    // It's not even on the grid
+	    // XXX how can this happen?
+	    continue;
+	}
+
+	pswarpMap *map = sourceGrid->maps[xGrid][yGrid]; // Locally linear transformation
+	double xOut, yOut;          // Output coordinates
+	if (!pswarpMapApply(&xOut, &yOut, map, xIn + 0.5, yIn + 0.5)) {
+	    psError(PS_ERR_UNKNOWN, false, "Unable to transform coordinates for source at %f,%f\n",
+		    xIn, yIn);
+	    psFree(outSources);
+	    psFree(sourceGrid);
+	    return false;
+	}
+	xOut += output->image->col0 - 0.5;
+	yOut += output->image->row0 - 0.5;
+	if (xOut < minX || xOut > maxX || yOut < minY || yOut > maxY) {
+	    // It's not in the output image
+	    continue;
+	}
+
+	// Generate the new source in the output frame
+	pmSource *new = pmSourceAlloc(); // New source
+	new->peak = pmPeakAlloc(xOut, yOut, source->peak->flux, PM_PEAK_LONE);
+	new->peak->flux = source->peak->flux;
+	new->type = PM_SOURCE_TYPE_STAR;
+	new->psfMag = source->psfMag;
+	new->errMag = source->errMag;
+	new->sky = source->sky;
+	new->skyErr = source->skyErr;
+	new->pixWeight = source->pixWeight;
+	new->modelPSF = pmModelAlloc(source->modelPSF->type);
+
+#if 0
+	// XXX Note that this will not set the correct axes
+	pmPSF_AxesToModel(new->modelPSF->params->data.F32,
+			  pmPSF_ModelToAxes(source->modelPSF->params->data.F32, 20.0));
+#endif
+
+	// Propagate the position erorrs
+	float dxIn, dyIn;           // Errors in input coordinates
+	dxIn = model->dparams->data.F32[PM_PAR_XPOS];
+	dyIn = model->dparams->data.F32[PM_PAR_YPOS];
+
+	float dxOut, dyOut;         // Errors in output coordinates
+	dxOut = sqrt(PS_SQR(map->Xx * dxIn) + PS_SQR(map->Xy * dyIn));
+	dyOut = sqrt(PS_SQR(map->Yx * dxIn) + PS_SQR(map->Yy * dyIn));
+
+	new->modelPSF->params->data.F32[PM_PAR_XPOS] = xOut;
+	new->modelPSF->params->data.F32[PM_PAR_YPOS] = yOut;
+	new->modelPSF->dparams->data.F32[PM_PAR_XPOS] = dxOut;
+	new->modelPSF->dparams->data.F32[PM_PAR_YPOS] = dyOut;
+
+	psArrayAdd(outSources, SOURCE_ARRAY_BUFFER, new);
+	psFree(new);                // Drop reference
+    }
+    psFree(sourceGrid);
+    psFree(outSources);             // Drop reference
+    return true;
+}
+
Index: /branches/eam_branch_20080719/pswarp/src/pswarpTransformTile.c
===================================================================
--- /branches/eam_branch_20080719/pswarp/src/pswarpTransformTile.c	(revision 18752)
+++ /branches/eam_branch_20080719/pswarp/src/pswarpTransformTile.c	(revision 18753)
@@ -37,13 +37,11 @@
     // int outNrow = args->output->image->numRows;
 
-    // XXX these are wrong (min or max is wrong)
-    int minX = pswarpMapGridNextGrid_Y (args->grid, args->gridY);
-    int minY = pswarpMapGridNextGrid_X (args->grid, args->gridX);
-    int maxX = pswarpMapGridNextGrid_Y (args->grid, args->gridY);
-    int maxY = pswarpMapGridNextGrid_X (args->grid, args->gridX);
+    // get the coordinate range for this grid tile
+    psPlane minPt, maxPt;
+    pswarpMapGridCoordRange (args->grid, args->gridX, args->gridY, &minPt, &maxPt);
 
-    psF32 **outImageData     = args->output->image->data.F32;
-    psF32 **outVarData       = args->output->weight->data.F32;
-    psMaskType **outMaskData = args->output->mask->data.U8;
+    psF32 **outImageData     = (args->output->image)  ? args->output->image->data.F32  : NULL;
+    psF32 **outVarData       = (args->output->weight) ? args->output->weight->data.F32 : NULL;
+    psMaskType **outMaskData = (args->output->mask)   ? args->output->mask->data.U8    : NULL;
 
     pswarpMap *map = args->grid->maps[args->gridX][args->gridY];
@@ -60,6 +58,6 @@
     // Iterate over the output image pixels (parent frame)
     long goodPixels = 0;                // Number of input pixels landing on the output image
-    for (int y = minY; y < maxY; y++) {
-        for (int x = minX; x < maxX; x++) {
+    for (int y = minPt.y; y < maxPt.y; y++) {
+        for (int x = minPt.x; x < maxPt.x; x++) {
 
             // Only transform those pixels requested
