Index: trunk/pois/src/poisCalculateDeviations.c
===================================================================
--- trunk/pois/src/poisCalculateDeviations.c	(revision 3798)
+++ trunk/pois/src/poisCalculateDeviations.c	(revision 5717)
@@ -46,18 +46,17 @@
 	int y = stamp->y;		// Stamp y coord
 	if (stamp->status == POIS_STAMP_USED) {
-	    psImage *refStamp = psImageSubset(refImage, x - xSize, y - ySize, x + xSize, y + ySize);
-	    psImage *inStamp = psImageSubset(inImage, x - xSize, y - ySize, x + xSize, y + ySize);
-	    psImage *maskStamp = psImageSubset(mask, x - xSize, y - ySize, x + xSize, y + ySize);
+	    psRegion stampRegion = {x - xSize, x + xSize, y - ySize, y + ySize};
+	    psImage *refStamp = psImageSubset(refImage, stampRegion);
+	    psImage *inStamp = psImageSubset(inImage, stampRegion);
+	    psImage *maskStamp = psImageSubset(mask, stampRegion);
 	    psImage *convRefStamp = poisConvolveImage(refStamp, maskStamp, solution, kernelParams, config);
 	    // Calculate chi^2
 	    (void)psBinaryOp(subStamp, inStamp, "-", convRefStamp);
+	    (void)psBinaryOp(subStamp, subStamp, "*", subStamp);
 	    (void)psBinaryOp(subStamp, subStamp, "/", inStamp);
-	    (void)psBinaryOp(subStamp, subStamp, "*", subStamp);
-	    psImage *subStampTrim = psImageSubset(subStamp, config->xKernel, config->yKernel,
-						  config->xKernel + 2 * footprint,
-						  config->yKernel + 2 * footprint);
-	    psImage *maskStampTrim = psImageSubset(maskStamp, config->xKernel, config->yKernel,
-						   config->xKernel + 2 * footprint,
-						   config->yKernel + 2 * footprint);
+	    psRegion stampTrim = { config->xKernel, config->xKernel + 2 * footprint, config->yKernel,
+				   config->yKernel + 2 * footprint };
+	    psImage *subStampTrim = psImageSubset(subStamp, stampTrim);
+	    psImage *maskStampTrim = psImageSubset(maskStamp, stampTrim);
 	    // Copy image to workaround bug 305
 	    psImage *tempImage = psImageCopy(NULL, subStampTrim, PS_TYPE_F32);
@@ -69,5 +68,5 @@
 	    psFree(tempMask);
 	    
-	    deviations->data.F32[s] = stats->sampleMean * (float)footprint * (float)footprint * 4.0;
+	    deviations->data.F32[s] = sqrtf(stats->sampleMean / 2.0);
 	    psTrace("pois.calculateDeviations", 5, "Deviation of stamp %d (%d,%d) is %f\n", s, x, y,
 		    deviations->data.F32[s]);
@@ -77,5 +76,5 @@
 	    snprintf(stampName, MAXCHAR, "stamp%d.fits", s);
 	    psFits *stampFile = psFitsAlloc(stampName);
-	    if (!psFitsWriteImage(stampFile, NULL, subStampTrim, 0, NULL)) {
+	    if (!psFitsWriteImage(stampFile, NULL, subStampTrim, 0)) {
 		psErrorStackPrint(stderr, "Unable to write stamp: %s\n", stampName);
 	    }
@@ -83,4 +82,5 @@
 #endif
 
+#if 0
 	    psFree(convRefStamp);
 	    psFree(maskStampTrim);
@@ -89,4 +89,5 @@
 	    psFree(refStamp);
 	    psFree(inStamp);
+#endif
 	}
     }
