Index: trunk/psModules/src/objects/pmModel.c
===================================================================
--- trunk/psModules/src/objects/pmModel.c	(revision 15562)
+++ trunk/psModules/src/objects/pmModel.c	(revision 15587)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-11-10 01:09:20 $
+ *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-11-11 21:49:18 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -218,17 +218,55 @@
     float yResidCenter = 0.0;
 
+    psImage *myRo = NULL;
+    psImage *myRx = NULL;
+    psImage *myRy = NULL;
     psImageInterpolateOptions *Ro = NULL;
     psImageInterpolateOptions *Rx = NULL;
     psImageInterpolateOptions *Ry = NULL;
     if (model->residuals && (mode & (PM_MODEL_OP_RES0 | PM_MODEL_OP_RES1))) {
-        Ro = psImageInterpolateOptionsAlloc(
-            PS_INTERPOLATE_BILINEAR,
-            model->residuals->Ro, NULL, mask, 0, 0.0, 0.0, 1, 0, 0.0);
-        Rx = psImageInterpolateOptionsAlloc(
-            PS_INTERPOLATE_BILINEAR,
-            model->residuals->Rx, NULL, NULL, 0, 0.0, 0.0, 1, 0, 0.0);
-        Ry = psImageInterpolateOptionsAlloc(
-            PS_INTERPOLATE_BILINEAR,
-            model->residuals->Ry, NULL, NULL, 0, 0.0, 0.0, 1, 0, 0.0);
+	// if the residual image and object image don't match, 
+	// supply an appropriately overlapped residual image
+	psImage *inRo = model->residuals->Ro;
+	psImage *inRx = model->residuals->Rx;
+	psImage *inRy = model->residuals->Ry;
+	if ((image->numCols != inRo->numCols) || 
+	    (image->numRows != inRo->numRows)) {
+	    myRo = psImageAlloc (image->numCols, image->numRows, PS_TYPE_F32);
+	    myRx = psImageAlloc (image->numCols, image->numRows, PS_TYPE_F32);
+	    myRy = psImageAlloc (image->numCols, image->numRows, PS_TYPE_F32);
+	    dx = (int)(inRx->numCols / 2) - (xPos - image->col0); 
+	    dy = (int)(inRx->numCols / 2) - (xPos - image->col0); 
+	    for (int iy = 0; iy < myRo->numRows; iy++) {
+		int jy = iy + dy;
+		if ((jy < 0) || (jy >= inRo->numRows)) {
+		    for (int ix = 0; ix < myRo->numCols; ix++) {
+			myRo->data.F32[iy][ix] = 0.0;
+			myRx->data.F32[iy][ix] = 0.0;
+			myRy->data.F32[iy][ix] = 0.0;
+		    }
+		    continue;
+		}
+		for (int ix = 0; ix < myRo->numCols; ix++) {
+		    int jx = ix + dx;
+		    if ((jx < 0) || (jx >= inRo->numCols)) {
+			myRo->data.F32[iy][ix] = 0.0;
+			myRx->data.F32[iy][ix] = 0.0;
+			myRy->data.F32[iy][ix] = 0.0;
+		    } else {
+			myRo->data.F32[iy][ix] = inRo->data.F32[jy][jx];
+			myRx->data.F32[iy][ix] = inRx->data.F32[jy][jx];
+			myRy->data.F32[iy][ix] = inRy->data.F32[jy][jx];
+		    }
+		}
+	    }		
+	} else {
+	    myRo = psMemIncrRefCounter (inRo);
+	    myRx = psMemIncrRefCounter (inRx);
+	    myRy = psMemIncrRefCounter (inRy);
+	}
+
+        Ro = psImageInterpolateOptionsAlloc(PS_INTERPOLATE_BILINEAR, myRo, NULL, mask, 0, 0.0, 0.0, 1, 0, 0.0);
+        Rx = psImageInterpolateOptionsAlloc(PS_INTERPOLATE_BILINEAR, myRx, NULL, NULL, 0, 0.0, 0.0, 1, 0, 0.0);
+        Ry = psImageInterpolateOptionsAlloc(PS_INTERPOLATE_BILINEAR, myRy, NULL, NULL, 0, 0.0, 0.0, 1, 0, 0.0);
 
         xBin = model->residuals->xBin;
@@ -303,4 +341,7 @@
     psFree(Rx);
     psFree(Ry);
+    psFree(myRo);
+    psFree(myRx);
+    psFree(myRy);
     psTrace("psModules.objects", 3, "---- %s(true) end ----\n", __func__);
     return(true);
