Index: trunk/psModules/src/imcombine/pmStack.c
===================================================================
--- trunk/psModules/src/imcombine/pmStack.c	(revision 16848)
+++ trunk/psModules/src/imcombine/pmStack.c	(revision 17005)
@@ -8,6 +8,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-03-06 21:38:39 $
+ *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-03-17 21:38:43 $
  *  Copyright 2004-2007 Institute for Astronomy, University of Hawaii
  *
@@ -80,5 +80,6 @@
 {
     psFree(data->readout);
-    psFree(data->pixels);
+    psFree(data->reject);
+    psFree(data->inspect);
     return;
 }
@@ -172,5 +173,5 @@
     } else {
         *median = num % 2 ? sortBuffer->data.F32[num / 2] :
-            (sortBuffer->data.F32[num / 2] + sortBuffer->data.F32[num / 2 + 1]) / 2.0;
+            (sortBuffer->data.F32[num / 2 - 1] + sortBuffer->data.F32[num / 2]) / 2.0;
         if (stdev) {
             if (num <= NUM_DIRECT_STDEV) {
@@ -180,5 +181,5 @@
                     sum += PS_SQR(sortBuffer->data.F32[i] - *median);
                 }
-                *stdev = sqrt(sum / (float)(num - 1));
+                *stdev = sqrt(sum / (double)(num - 1));
             } else {
                 // Standard deviation from the interquartile range
@@ -201,7 +202,8 @@
     pmStackData *data = inputs->data[source]; // Stack data of interest
     if (!data) {
+        psWarning("Can't find input data for source %d", source);
         return;
     }
-    data->pixels = psPixelsAdd(data->pixels, PIXEL_LIST_BUFFER, x, y);
+    data->inspect = psPixelsAdd(data->inspect, PIXEL_LIST_BUFFER, x, y);
     return;
 }
@@ -391,5 +393,5 @@
 // Ensure the input array of pmStackData is valid, and get some details out of it
 static bool validateInputData(bool *haveVariances, // Do we have variance maps in the sky images?
-                              bool *havePixels, // Do we have lists of pixels?
+                              bool *haveRejects, // Do we have lists of rejected pixels?
                               int *num,    // Number of inputs
                               int *numCols, int *numRows, // Size of (sky) images
@@ -420,5 +422,5 @@
         PS_ASSERT_IMAGE_TYPE(data->readout->weight, PS_TYPE_F32, false);
     }
-    *havePixels = (data->pixels != NULL);
+    *haveRejects = (data->reject != NULL);
 
     // Make sure the rest correspond with the first
@@ -433,6 +435,7 @@
             return false;
         }
-        if ((*havePixels && !data->pixels) || (data->pixels && !*havePixels)) {
-            psError(PS_ERR_UNEXPECTED_NULL, true, "The pixels are specified in some but not all inputs.");
+        if ((*haveRejects && !data->reject) || (data->reject && !*haveRejects)) {
+            psError(PS_ERR_UNEXPECTED_NULL, true,
+                    "The rejected pixels are specified in some but not all inputs.");
             return false;
         }
@@ -473,6 +476,6 @@
             continue;
         }
-        assert(data->pixels);
-        psPixels *pixels = data->pixels;// The pixels of interest
+        assert(data->reject);
+        psPixels *pixels = data->reject; // The rejected pixels
         for (int j = 0; j < pixels->n; j++) {
             int x = pixels->data[j].x, y = pixels->data[j].y; // Coordinates of interest
@@ -518,5 +521,6 @@
 
     data->readout = psMemIncrRefCounter(readout);
-    data->pixels = NULL;
+    data->reject = NULL;
+    data->inspect = NULL;
     data->weight = weight;
 
@@ -526,12 +530,12 @@
 /// Stack input images
 bool pmStackCombine(pmReadout *combined, psArray *input, psMaskType maskVal, psMaskType bad,
-                    int kernelSize, int numIter, float rej, bool useVariance, bool safe)
+                    int kernelSize, int numIter, float rej, bool entire, bool useVariance, bool safe)
 {
     PS_ASSERT_PTR_NON_NULL(combined, false);
     bool haveVariances;                 // Do we have the variance maps?
-    bool havePixels;                    // Do we have lists of pixels?
+    bool haveRejects;                   // Do we have lists of rejected pixels?
     int num;                            // Number of inputs
     int numCols, numRows;               // Size of (sky) images
-    if (!validateInputData(&haveVariances, &havePixels, &num, &numCols, &numRows, input)) {
+    if (!validateInputData(&haveVariances, &haveRejects, &num, &numCols, &numRows, input)) {
         return false;
     }
@@ -544,5 +548,5 @@
         PS_ASSERT_FLOAT_LARGER_THAN(rej, 0.0, false);
     }
-    if (havePixels) {
+    if (haveRejects) {
         // This is a subsequent combination, so expect that the image and mask already exist
         PS_ASSERT_IMAGE_NON_NULL(combined->image, false);
@@ -557,20 +561,16 @@
     }
 
-    // Pull out the image weightings
-    psVector *weights = psVectorAlloc(num, PS_TYPE_F32);
+    psVector *weights = psVectorAlloc(num, PS_TYPE_F32); // Relative weighting for each image
+    psArray *stack = psArrayAlloc(num); // Stack of readouts
     for (int i = 0; i < num; i++) {
         pmStackData *data = input->data[i]; // Stack data for this input
         if (!data) {
             weights->data.F32[i] = 0.0;
+            continue;
         }
         weights->data.F32[i] = data->weight;
-    }
-
-    // Get the sizes
-    psArray *stack = psArrayAlloc(input->n);
-    for (int i = 0; i < stack->n; i++) {
-        pmStackData *data = input->data[i]; // Stack data
         stack->data[i] = psMemIncrRefCounter(data->readout);
     }
+
     int minInputCols, maxInputCols, minInputRows, maxInputRows; // Smallest and largest values to combine
     int xSize, ySize;                   // Size of the output image
@@ -598,6 +598,5 @@
     combineBuffer *buffer = combineBufferAlloc(num);
 
-    if (havePixels) {
-        // Only combine select pixels
+    if (haveRejects) {
         psImage *combinedImage = combined->image; // Combined image
         psImage *combinedMask = combined->mask; // Combined mask
@@ -611,15 +610,28 @@
                 continue;
             }
-            pixels = psPixelsConcatenate(pixels, data->pixels);
-        }
-        for (int i = 0; i < pixels->n; i++) {
-            // Pixel coordinates are in the frame of the original image
-            int x = pixels->data[i].x, y = pixels->data[i].y; // Coordinates of interest
-            if (x < minInputCols || x >= maxInputCols || y < minInputRows || y >= maxInputRows) {
-                continue;
-            }
-            psVector *reject = pixelMapQuery(pixelMap, x, y); // Inspect these images closely
-            combinePixels(combinedImage, combinedMask, combinedVariance, input, weights, reject, x, y,
-                          maskVal, bad, numIter, rej, useVariance, safe, buffer);
+            pixels = psPixelsConcatenate(pixels, data->reject);
+        }
+
+        if (entire) {
+            // Combine entire image
+            for (int y = minInputRows; y < maxInputRows; y++) {
+                for (int x = minInputCols; x < maxInputCols; x++) {
+                    psVector *reject = pixelMapQuery(pixelMap, x, y); // Inspect these images closely
+                    combinePixels(combinedImage, combinedMask, combinedVariance, input, weights, reject, x, y,
+                                  maskVal, bad, numIter, rej, useVariance, safe, buffer);
+                }
+            }
+        } else {
+            // Only combine previously rejected pixels
+            for (int i = 0; i < pixels->n; i++) {
+                // Pixel coordinates are in the frame of the original image
+                int x = pixels->data[i].x, y = pixels->data[i].y; // Coordinates of interest
+                if (x < minInputCols || x >= maxInputCols || y < minInputRows || y >= maxInputRows) {
+                    continue;
+                }
+                psVector *reject = pixelMapQuery(pixelMap, x, y); // Inspect these images closely
+                combinePixels(combinedImage, combinedMask, combinedVariance, input, weights, reject, x, y,
+                              maskVal, bad, numIter, rej, useVariance, safe, buffer);
+            }
         }
         psFree(pixels);
@@ -644,15 +656,4 @@
         }
 
-        // Generate the pixel lists in which to place the rejected pixels
-        if (numIter != 0) {
-            for (int i = 0; i < num; i++) {
-                pmStackData *data = input->data[i]; // Stack data for this input
-                if (!data) {
-                    continue;
-                }
-                data->pixels = psPixelsAllocEmpty(PIXEL_LIST_BUFFER);
-            }
-        }
-
         for (int y = minInputRows; y < maxInputRows; y++) {
             for (int x = minInputCols; x < maxInputCols; x++) {
@@ -669,5 +670,5 @@
                     continue;
                 }
-                psTrace("psModules.imcombine", 5, "Image %d: %ld pixels to inspect.\n", i, data->pixels->n);
+                psTrace("psModules.imcombine", 5, "Image %d: %ld pixels to inspect.\n", i, data->inspect->n);
             }
         }
