Index: trunk/psModules/src/imcombine/pmStack.c
===================================================================
--- trunk/psModules/src/imcombine/pmStack.c	(revision 19336)
+++ trunk/psModules/src/imcombine/pmStack.c	(revision 19482)
@@ -8,6 +8,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.38 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-09-03 19:42:54 $
+ *  @version $Revision: 1.39 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-09-11 03:37:58 $
  *  Copyright 2004-2007 Institute for Astronomy, University of Hawaii
  *
@@ -309,8 +309,9 @@
               }
           }
-          if (useVariance && safe && numIter > 0) {
+          if (useVariance && numIter > 0) {
               // Use variance to check that the two are consistent
               float diff = pixelData->data.F32[0] - pixelData->data.F32[1];
-              float sigma2 = pixelVariances->data.F32[0] + pixelVariances->data.F32[1];
+              float sigma2 = pixelVariances->data.F32[0] * varFactors->data.F32[pixelSources->data.U16[0]] +
+                  pixelVariances->data.F32[1] * varFactors->data.F32[pixelSources->data.U16[1]];
               if (PS_SQR(diff) > PS_SQR(rej) * sigma2) {
                   // Not consistent: mark both for inspection
@@ -341,5 +342,5 @@
                   float rej2 = PS_SQR(rej); // Rejection level squared
                   for (int i = 0; i < num; i++) {
-                      pixelVariances->data.F32[i] *= rej2 * varFactors->data.F32[i];
+                      pixelVariances->data.F32[i] *= rej2 * varFactors->data.F32[pixelSources->data.U16[i]];
                   }
               }
@@ -348,19 +349,17 @@
           // The clipping that follows is solely to identify suspect pixels.
           // These suspect pixels will be inspected in more detail by other functions.
-          int numClipped = INT_MAX;       // Number of pixels clipped per iteration
-          int totalClipped = 0;           // Total number of pixels clipped
+          int numClipped = INT_MAX;     // Number of pixels clipped per iteration
+          int totalClipped = 0;         // Total number of pixels clipped
           for (int i = 0; i < numIter && numClipped > 0 && num - totalClipped > 2; i++) {
               numClipped = 0;
               float median, stdev;    // Median and stdev of the combination, for rejection
 
-              if (!combinationMedianStdev(&median, useVariance ? NULL : &stdev, pixelData, pixelMasks, sort)) {
+              if (!combinationMedianStdev(&median, useVariance ? NULL : &stdev,
+                                          pixelData, pixelMasks, sort)) {
                   psWarning("Bad median/stdev at %d,%d", x, y);
                   break;
               }
 
-              float limit = NAN;        // Rejection limit, when rejecting based on data properties
-              if (!useVariance) {
-                  limit = rej * stdev;
-              }
+              float limit = rej * stdev; // Rejection limit, when rejecting based on data properties
 
 // Mask a pixel for inspection
@@ -378,4 +377,5 @@
                   if (useVariance) {
                       // Comparing squares --- cheaper than lots of sqrts
+                      // pixelVariances includes the variance factor and the rejection limit, from above
                       if (PS_SQR(diff) > pixelVariances->data.F32[j]) {
                           MASK_PIXEL_FOR_INSPECTION();
