Index: trunk/psModules/src/camera/pmFPAMaskWeight.c
===================================================================
--- trunk/psModules/src/camera/pmFPAMaskWeight.c	(revision 20486)
+++ trunk/psModules/src/camera/pmFPAMaskWeight.c	(revision 20488)
@@ -414,9 +414,9 @@
     for (int i = 0; i < num; i++) {
         // Coordinates of interest
-        int xPix = psRandomUniform(rng) * xSize + xOffset;
-        int yPix = psRandomUniform(rng) * ySize + yOffset;
+        int xPix = psRandomUniform(rng) * xSize + xOffset + 0.5;
+        int yPix = psRandomUniform(rng) * ySize + yOffset + 0.5;
         psAssert(xPix - size >= 0 && xPix + size < numCols &&
-                 yPix - size >= 0 && yPix + size > numRows,
-                 "Bad pixel position");
+                 yPix - size >= 0 && yPix + size < numRows,
+                 "Bad pixel position: %d,%d", xPix, yPix);
 
         // Weighted aperture photometry
@@ -431,5 +431,5 @@
             float xSumWeight = 0.0; // Sum for weight measurement in x
             float xSumGauss = 0.0, xSumGauss2 = 0.0; // Sums of Gaussian kernels in x
-            float yGauss = peakFlux * gauss->data.F32[v]; // Value of Gaussian in y
+            float yGauss = gauss->data.F32[v]; // Value of Gaussian in y
             for (int u = 0, x = xPix - size; u < fullSize; u++, x++) {
                 if (mask && mask->data.PS_TYPE_MASK_DATA[y][x] & maskVal) {
@@ -440,5 +440,5 @@
                 float xGauss2 = gauss2->data.F32[u]; // Value of Gaussian^2 in x
                 xSumNoise += value * xGauss;
-                xSumSource += (value + xGauss * yGauss) * xGauss;
+                xSumSource += (value + peakFlux * xGauss * yGauss) * xGauss;
                 xSumWeight += weight->data.F32[y][x] * xGauss2;
                 xSumGauss += xGauss;
@@ -453,14 +453,17 @@
         }
 
-        float weightValue = sumWeight / sumGauss2; // Value of convolved weight
         photMask->data.PS_TYPE_MASK_DATA[i] = ((isfinite(sumNoise) && isfinite(sumSource) &&
-                                                isfinite(weightValue) && sumGauss > 0 && sumGauss2 > 0) ?
+                                                isfinite(sumWeight) && sumGauss > 0 && sumGauss2 > 0) ?
                                                0 : 0xFF);
 
-        noise->data.F32[i] = sumNoise / sumGauss;
-        source->data.F32[i] = sumSource / sumGauss;
-        guess->data.F32[i] = (sumSource > 0) ? sigFactor * PS_SQR(sumSource) / sumWeight * sumGauss2 : 0.0;
+        float smoothImageNoise = sumNoise / sumGauss; // Value of smoothed image pixel for noise
+        float smoothImageSource = sumSource / sumGauss; // Value of smoothed image pixel for source
+        float smoothWeight = sumWeight / sumGauss2; // Value of smoothed weight pixel
+
+        noise->data.F32[i] = smoothImageNoise;
+        source->data.F32[i] = smoothImageSource;
+        guess->data.F32[i] = (sumSource > 0) ? sigFactor * PS_SQR(smoothImageSource) / smoothWeight : 0.0;
         psTrace("psModules.camera", 10, "Flux %d (%d,%d): %f, %f, %f\n",
-                i, xPix, yPix, source->data.F32[i], noise->data.F32[i], guess->data.F32[i]);
+                i, xPix, yPix, smoothImageNoise, smoothImageSource, smoothWeight);
     }
     psFree(gauss);
@@ -491,5 +494,7 @@
             photMask->data.PS_TYPE_MASK_DATA[i] = 0xFF;
         }
-        ratio->data.F32[i] = measuredSig / guess->data.F32[i];
+        ratio->data.F32[i] = guess->data.F32[i] / measuredSig;
+        psTrace("psModules.camera", 9, "Ratio %d: %f, %f, %f\n",
+                i, guess->data.F32[i], measuredSig, ratio->data.F32[i]);
     }
     psFree(source);
