Index: /trunk/ppStack/src/ppStackReadout.c
===================================================================
--- /trunk/ppStack/src/ppStackReadout.c	(revision 23576)
+++ /trunk/ppStack/src/ppStackReadout.c	(revision 23577)
@@ -150,5 +150,5 @@
 
     if (!pmStackCombine(outRO, stack, maskVal | maskBad, maskBad, kernelSize, iter,
-                        combineRej, combineSys, combineDiscard, true, useVariance, safe)) {
+                        combineRej, combineSys, combineDiscard, true, useVariance, safe, false)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to combine input readouts with rejection.");
         psFree(stack);
@@ -196,5 +196,10 @@
 
     bool mdok;                          // Status of MD lookup
+    int iter = psMetadataLookupS32(NULL, recipe, "ITER"); // Rejection iterations
+    float combineRej = psMetadataLookupF32(NULL, recipe, "COMBINE.REJ"); // Combination threshold
+    float combineSys = psMetadataLookupF32(NULL, recipe, "COMBINE.SYS"); // Combination systematic error
+    float combineDiscard = psMetadataLookupF32(NULL, recipe, "COMBINE.DISCARD"); // Olympic discard fraction
     bool useVariance = psMetadataLookupBool(&mdok, recipe, "VARIANCE"); // Use variance for rejection?
+    bool safe = psMetadataLookupBool(&mdok, recipe, "SAFE"); // Be safe when combining small numbers of pixels
 
     psString maskValStr = psMetadataLookupStr(NULL, recipe, "MASK.VAL"); // Name of bits to mask going in
@@ -206,5 +211,14 @@
     psArray *stack = psArrayAlloc(num); // Array for stacking
 
-    bool entire = (rejected ? false : true); // Combine entire image?
+    bool entire = true;                 // Combine entire image?
+    if (rejected) {
+        // We have rejection from a previous combination: combine without flagging pixels to inspect
+        entire = false;
+        safe = false;
+        iter = 0;
+        combineRej = NAN;
+        combineSys = NAN;
+    }
+
     for (int i = 0; i < num; i++) {
         pmReadout *ro = readouts->data[i];
@@ -240,6 +254,7 @@
     }
 
-    if (!pmStackCombine(outRO, stack, maskVal | maskBad, maskBad, 0, 0, NAN, NAN, NAN,
-                        entire, useVariance, false)) {
+    if (!pmStackCombine(outRO, stack, maskVal | maskBad, maskBad, 0,
+                        iter, combineRej, combineSys, combineDiscard,
+                        entire, useVariance, safe, !rejected)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to combine input readouts.");
         psFree(stack);
Index: /trunk/psModules/src/imcombine/pmStack.c
===================================================================
--- /trunk/psModules/src/imcombine/pmStack.c	(revision 23576)
+++ /trunk/psModules/src/imcombine/pmStack.c	(revision 23577)
@@ -46,4 +46,5 @@
     psVector *weights;                  // Pixel weightings
     psVector *sources;                  // Pixel sources (which image did they come from?)
+    psVector *limits;                   // Rejection limits
     psVector *sort;                     // Buffer for sorting (to get a robust estimator of the standard dev)
 } combineBuffer;
@@ -56,4 +57,5 @@
     psFree(buffer->weights);
     psFree(buffer->sources);
+    psFree(buffer->limits);
     psFree(buffer->sort);
     return;
@@ -71,4 +73,5 @@
     buffer->weights = psVectorAlloc(numImages, PS_TYPE_F32);
     buffer->sources = psVectorAlloc(numImages, PS_TYPE_U16);
+    buffer->limits = psVectorAlloc(numImages, PS_TYPE_F32);
     buffer->sort = psVectorAlloc(numImages, PS_TYPE_F32);
     return buffer;
@@ -319,4 +322,5 @@
                           bool useVariance, // Use variance for rejection when combining?
                           bool safe,    // Combine safely?
+                          bool rejectInspect, // Reject values marked for inspection from combination?
                           combineBuffer *buffer // Buffer for combination; to avoid multiple allocations
                          )
@@ -336,4 +340,5 @@
     psVector *pixelWeights = buffer->weights; // Image weights for the pixel of interest
     psVector *pixelSources = buffer->sources; // Sources for the pixel of interest
+    psVector *pixelLimits = buffer->limits; // Limits for the pixel of interest
     psVector *sort = buffer->sort;      // Sort buffer
 
@@ -375,4 +380,5 @@
     pixelWeights->n = num;
     pixelSources->n = num;
+    pixelLimits->n = num;
 
 #ifdef TESTING
@@ -389,5 +395,5 @@
     // Default option is that the pixel is bad
     float imageValue = NAN, varianceValue = NAN; // Value for combined image and variance map
-    psImageMaskType maskValue = bad;         // Value for combined mask
+    psImageMaskType maskValue = bad;    // Value for combined mask
     switch (num) {
       case 0:
@@ -449,6 +455,12 @@
               if (PS_SQR(diff) > PS_SQR(rej) * sigma2) {
                   // Not consistent: mark both for inspection
-                  combineInspect(inputs, x, y, pixelSources->data.U16[0]);
-                  combineInspect(inputs, x, y, pixelSources->data.U16[1]);
+                  if (rejectInspect) {
+                      imageValue = NAN;
+                      varianceValue = NAN;
+                      maskValue = bad;
+                  } else {
+                      combineInspect(inputs, x, y, pixelSources->data.U16[0]);
+                      combineInspect(inputs, x, y, pixelSources->data.U16[1]);
+                  }
 #ifdef TESTING
                   if (x == TEST_X && y == TEST_Y) {
@@ -501,5 +513,5 @@
 #endif
 
-                      pixelVariances->data.F32[i] = rej2 * (pixelVariances->data.F32[i] + sysVar);
+                      pixelLimits->data.F32[i] = rej2 * (pixelVariances->data.F32[i] + sysVar);
                   }
               }
@@ -541,5 +553,7 @@
 #define MASK_PIXEL_FOR_INSPECTION() \
     pixelMasks->data.PS_TYPE_VECTOR_MASK_DATA[j] = 0xff; \
-    combineInspect(inputs, x, y, pixelSources->data.U16[j]); \
+    if (!rejectInspect) { \
+        combineInspect(inputs, x, y, pixelSources->data.U16[j]); \
+    } \
     numClipped++; \
     totalClipped++;
@@ -553,10 +567,10 @@
                       // Comparing squares --- cheaper than lots of sqrts
                       // pixelVariances includes the rejection limit, from above
-                      if (PS_SQR(diff) > pixelVariances->data.F32[j]) {
+                      if (PS_SQR(diff) > pixelLimits->data.F32[j]) {
                           MASK_PIXEL_FOR_INSPECTION();
 #ifdef TESTING
                           if (x == TEST_X && y == TEST_Y) {
                               fprintf(stderr, "Rejecting input %d based on variance: %f > %f\n",
-                                      j, diff, sqrtf(pixelVariances->data.F32[j]));
+                                      j, diff, sqrtf(pixelLimits->data.F32[j]));
                           }
 #endif
@@ -573,4 +587,35 @@
               }
           }
+
+          if (rejectInspect && totalClipped > 0) {
+              // Get rid of the masked values
+              // The alternative to this is to make combinationMeanVariance() accept a mask
+              int good = 0;            // Index of good value
+              for (int i = 0; i < num; i++) {
+                  if (pixelMasks->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
+                      continue;
+                  }
+                  if (i != good) {
+                      pixelData->data.F32[good] = pixelData->data.F32[i];
+                      pixelVariances->data.F32[good] = pixelVariances->data.F32[i];
+                      pixelWeights->data.F32[good] = pixelWeights->data.F32[i];
+                      pixelData->data.F32[good] = pixelData->data.F32[i];
+                  }
+                  good++;
+              }
+              pixelData->n = good;
+              pixelVariances->n = good;
+              pixelWeights->n = good;
+              if (combinationMeanVariance(&mean, &var, pixelData, pixelVariances, pixelWeights)) {
+                  imageValue = mean;
+                  varianceValue = var;
+                  maskValue = 0;
+              } else {
+                  imageValue = NAN;
+                  varianceValue = NAN;
+                  maskValue = bad;
+              }
+          }
+
           break;
       }
@@ -734,5 +779,5 @@
 bool pmStackCombine(pmReadout *combined, psArray *input, psImageMaskType maskVal, psImageMaskType bad,
                     int kernelSize, int numIter, float rej, float sys, float discard,
-                    bool entire, bool useVariance, bool safe)
+                    bool entire, bool useVariance, bool safe, bool rejectInspect)
 {
     PS_ASSERT_PTR_NON_NULL(combined, false);
@@ -838,5 +883,5 @@
                     combinePixels(combinedImage, combinedMask, combinedVariance, input, weights,
                                   addVariance, reject, x, y, maskVal, bad, numIter, rej, sys, discard,
-                                  useVariance, safe, buffer);
+                                  useVariance, safe, rejectInspect, buffer);
                 }
             }
@@ -853,5 +898,5 @@
                 combinePixels(combinedImage, combinedMask, combinedVariance, input, weights,
                               addVariance, reject, x, y, maskVal, bad, numIter, rej, sys, discard,
-                              useVariance, safe, buffer);
+                              useVariance, safe, rejectInspect, buffer);
             }
         }
@@ -881,5 +926,5 @@
                 combinePixels(combinedImage, combinedMask, combinedVariance, input, weights,
                               addVariance, NULL, x, y, maskVal, bad, numIter, rej, sys, discard,
-                              useVariance, safe, buffer);
+                              useVariance, safe, rejectInspect, buffer);
             }
         }
Index: /trunk/psModules/src/imcombine/pmStack.h
===================================================================
--- /trunk/psModules/src/imcombine/pmStack.h	(revision 23576)
+++ /trunk/psModules/src/imcombine/pmStack.h	(revision 23577)
@@ -52,5 +52,6 @@
                     bool entire,        ///< Combine entire image even if rejection lists provided?
                     bool useVariance,   ///< Use variance values for rejection?
-                    bool safe           ///< Play safe with small numbers of input pixels (mask if N <= 2)?
+                    bool safe,          ///< Play safe with small numbers of input pixels (mask if N <= 2)?
+                    bool rejectInspect  ///< Reject pixels instead of marking them for inspection?
     );
 
