Index: /trunk/psphot/src/psphotMakeResiduals.c
===================================================================
--- /trunk/psphot/src/psphotMakeResiduals.c	(revision 13569)
+++ /trunk/psphot/src/psphotMakeResiduals.c	(revision 13570)
@@ -65,4 +65,10 @@
     // - re-measure the robust median & sigma
     // - set output pixel, weight, and mask
+
+    const int badMask = 1;		// mask bits
+    const int poorMask = 2;		//       from psImageInterpolate
+    const int clippedMask = 4;		// mask bit set for clipped values
+
+    bool offImage = false;		// pixel is off the image
 
     // determine the maximum image size from the input sources
@@ -97,5 +103,6 @@
 	// we will interpolate the image and weight - include the mask or not?
 	// XXX consider better values for the mask bits
-	psImageInterpolateOptions *interp = psImageInterpolateOptionsAlloc(mode, image, weight, NULL, 0xff, 0.0, 0.0, 1, 2, 0.0);
+	psImageInterpolateOptions *interp =
+	    psImageInterpolateOptionsAlloc(mode, image, weight, NULL, 0xff, 0.0, 0.0, badMask, poorMask, 0.0);
 	psArrayAdd (input,  100, interp);
 
@@ -149,5 +156,9 @@
 
 		mflux = 0;
-		psImageInterpolate (&flux, &dflux, &mflux, ix, iy, interp);
+		offImage = false;
+		if (psImageInterpolate (&flux, &dflux, &mflux, ix, iy, interp) == PS_INTERPOLATE_STATUS_OFF) {
+		    // This pixel is off the image
+		    offImage = true;
+		}
 		fluxes->data.F32[i] = flux;
 		dfluxes->data.F32[i] = dflux;
@@ -169,14 +180,15 @@
 		// make this a user option
 		if (swing > nSigma) {
-		    fmasks->data.U8[i] = 1;
+		    fmasks->data.U8[i] = clippedMask;
 		}
 	    }		    
 
-	    if (SPATIAL_ORDER == 0) {
+	    if (offImage || SPATIAL_ORDER == 0) {
 		// measure the desired statistic on the unclipped pixels
 		*fluxStats = *fluxStatsDef;
 		psVectorStats (fluxStats, fluxes, NULL, fmasks, 0xff);
 		
-		resid->Ro->data.F32[oy][ox] = psStatsGetValue (fluxStats, statOption);
+		resid->Ro->data.F32[oy][ox] = psStatsGetValue(fluxStats, statOption);
+		resid->Rx->data.F32[oy][ox] = resid->Ry->data.F32[oy][ox] = 0.0;
 		//resid->weight->data.F32[oy][ox] = fluxStats->sampleStdev;
 	    } else {
@@ -213,4 +225,5 @@
 		resid->Rx->data.F32[oy][ox] = B->data.F64[1];
 		resid->Ry->data.F32[oy][ox] = B->data.F64[2];
+		//resid->weight->data.F32[oy][ox] = XXX;
 	    }
 	}
