Index: trunk/ppMerge/src/ppMergeCombine.c
===================================================================
--- trunk/ppMerge/src/ppMergeCombine.c	(revision 9954)
+++ trunk/ppMerge/src/ppMergeCombine.c	(revision 9996)
@@ -78,31 +78,4 @@
 #endif
 
-#if 0
-// Write fringe extensions
-static void writeFringes(psMetadata *fringes, // Table of extensions with fringe statistics to write
-                         psFits *fits   // FITS file to which to write
-                         )
-{
-    assert(fringes);
-
-    // Write the fringe extensions
-    psMetadataIterator *fringesIter = psMetadataIteratorAlloc(fringes, PS_LIST_HEAD, false); // Iterator
-    psMetadataItem *fringeItem;         // Item from iteration
-    while ((fringeItem = psMetadataGetAndIncrement(fringesIter))) {
-        const char *extname = fringeItem->name; // Extension name
-        psArray *fringe = fringeItem->data.V; // Fringe data (array of fringe components)
-        if (!pmFringesWriteFits(fits, NULL, fringe, extname)) {
-            psWarning("Unable to write fringe data to extension %s\n", extname);
-        }
-    }
-
-    // Clear the list
-    while (psListLength(fringes->list) > 0) {
-        psMetadataRemoveIndex(fringes, PS_LIST_TAIL);
-    }
-
-    return;
-}
-#endif
 
 // Combine the inputs
@@ -133,4 +106,8 @@
     pmFPAWrite(data->out, data->outFile, config->database, true, false); // Write header only
     pmChip *chip;                       // Chip of interest
+    psRandom *rng = NULL;               // Random number generator; required for building a mask
+    if (options->mask) {
+        rng = psRandomAlloc(PS_RANDOM_TAUS, 0);
+    }
     while ((chip = pmFPAviewNextChip(view, fpa, 1))) {
         pmChipWrite(chip, data->outFile, config->database, true, false); // Write header only
@@ -150,13 +127,10 @@
             }
 
-            // Building a shutter correction
-            if (options->shutter) {
-                // Read in the pixels
-                int numRead = 0;  // Number of inputs read
-                psVector *exptimes = psVectorAlloc(filenames->n, PS_TYPE_F32);  // Reference measurements
-                psVectorInit(exptimes, 0);
-                psArray *images = psArrayAlloc(filenames->n); // Array of images
-                psArray *weights = psArrayAlloc(filenames->n); // Array of weights
-                psArray *masks = psArrayAlloc(filenames->n); // Array of masks
+            // Read bit by bit
+            int numRead;  // Number of inputs read
+            int numScan = 0;
+
+            do {
+                numRead = 0;
                 for (int i = 0; i < filenames->n; i++) {
                     if (! filenames->data[i] || strlen(filenames->data[i]) == 0) {
@@ -167,126 +141,46 @@
                         continue;
                     }
-                    pmFPA *fpaIn = data->in->data[i]; // Input FPA
-                    pmChip *chipIn = fpaIn->chips->data[view->chip]; // Input chip
-                    pmCell *cellIn = chipIn->cells->data[view->cell]; // Input cell
-                    if (!cellIn->file_exists) {
-                        // || !cellIn->data_exists) {
-                        continue;
-                    }
-                    if (!pmCellRead(cellIn, fits, config->database) || cellIn->readouts->n == 0) {
-                        continue;
-                    }
-                    if (cellIn->readouts->n > 1) {
-                        psWarning("Multiple readouts in file %d, chip %d, cell %d --- "
-                                  "only the first will be used.\n", i, view->chip, view->cell);
-                    }
-                    float exptime = psMetadataLookupF32(NULL, cellIn->concepts, "CELL.EXPOSURE");
-                    if (!isfinite(exptime)) {
-                        psWarning("File %d, chip %d, cell %d has bad exposure time.\n",
-                                  i, view->chip, view->cell);
-                    }
-                    exptimes->data.F32[i] = exptime;
-                    pmReadout *readoutIn = cellIn->readouts->data[0];
-                    pmReadoutSetWeight(readoutIn);
-                    images->data[i] = psMemIncrRefCounter(readoutIn->image);
-                    weights->data[i] = psMemIncrRefCounter(readoutIn->weight);
-                    masks->data[i] = psMemIncrRefCounter(readoutIn->mask);
-                    numRead++;
+
+                    if (!stack->data[i]) {
+                        pmFPA *fpaIn = data->in->data[i]; // Input FPA
+                        pmChip *chipIn = fpaIn->chips->data[view->chip]; // Input chip
+                        pmCell *cellIn = chipIn->cells->data[view->cell]; // Input cell
+                        stack->data[i] = pmReadoutAlloc(cellIn); // Input readout
+                    }
+
+                    // Only reading and writing the first readout in each cell (plane 0)
+                    if (pmReadoutReadNext(stack->data[i], fits, 0, options->rows)) {
+                        // For anything except BIAS and DARK, we want a mask
+                        if (options->scale || options->zero || options->shutter) {
+                            pmReadoutSetMask(stack->data[i]);
+                        }
+                        if (options->shutter) {
+                            pmReadoutSetWeight(stack->data[i]);
+                        }
+
+                        numRead++;
+                    } else {
+                        psTrace("ppMerge", 3, "Unable to read from file %d for chip %d, "
+                                "cell %d, scan %d\n", i, view->chip, view->cell, numScan);
+                    }
+
                 }
-                if (numRead > 1) {
-                    readout->image = pmShutterCorrectionMeasure(exptimes, images, weights, masks,
-                                                                options->shutterSize, options->mean,
-                                                                options->stdev, options->shutterIter,
-                                                                options->shutterRej,
-                                                                options->combine->maskVal);
-                    psTrace("ppMerge", 5, "Chip %d, cell %d\n", view->chip, view->cell);
-                } else {
-                    psError(PS_ERR_UNKNOWN, true, "Not enough images to combine: %d\n", numRead);
-                    psFree(images);
-                    psFree(exptimes);
-                    return false;
+
+                psTrace("ppMerge", 5, "Chip %d, cell %d, scan %d\n", view->chip, view->cell, numScan);
+                if (numRead > 0) {
+
+                    if (options->shutter) {
+                        readout->image = pmShutterCorrectionMeasure(stack, options->shutterSize,
+                                                                    options->mean, options->stdev,
+                                                                    options->shutterIter,
+                                                                    options->shutterRej,
+                                                                    options->combine->maskVal);
+                    } else {
+                        pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
+                    }
                 }
-                psFree(images);
-                psFree(masks);
-                psFree(weights);
-                psFree(exptimes);
-            } else {
-#if 1
-                // Read bit by bit
-
-                int numRead;  // Number of inputs read
-                int numScan = 0;
-                do {
-                    numRead = 0;
-                    for (int i = 0; i < filenames->n; i++) {
-                        if (! filenames->data[i] || strlen(filenames->data[i]) == 0) {
-                            continue;
-                        }
-                        psFits *fits = data->files->data[i]; // FITS file handle
-                        if (!fits) {
-                            continue;
-                        }
-
-                        if (!stack->data[i]) {
-                            pmFPA *fpaIn = data->in->data[i]; // Input FPA
-                            pmChip *chipIn = fpaIn->chips->data[view->chip]; // Input chip
-                            pmCell *cellIn = chipIn->cells->data[view->cell]; // Input cell
-                            stack->data[i] = pmReadoutAlloc(cellIn); // Input readout
-                        }
-
-                        // Only reading and writing the first readout in each cell (plane 0)
-                        if (pmReadoutReadNext(stack->data[i], fits, 0, options->rows)) {
-                            // For anything except BIAS and DARK, we want a mask
-                            if (options->scale || options->zero) {
-                                pmReadoutSetMask(stack->data[i]);
-                            }
-                            numRead++;
-                        } else {
-                            psTrace("ppMerge", 3, "Unable to read from file %d for chip %d, "
-                                    "cell %d, scan %d\n", i, view->chip, view->cell, numScan);
-                        }
-                    }
-
-                    psTrace("ppMerge", 5, "Chip %d, cell %d, scan %d\n", view->chip, view->cell, numScan);
-                    if (numRead > 0) {
-                        pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
-                    }
-                    numScan++;
-
-                } while (numRead > 0);
-
-#else
-            // Read whole cells at a time
-
-                printf("Chip %d, cell %d\n", view->chip, view->cell);
-                int numRead = 0;  // Number of inputs read
-                for (int i = 0; i < filenames->n; i++) {
-                    if (! filenames->data[i] || strlen(filenames->data[i]) == 0) {
-                        continue;
-                    }
-                    psFits *fits = data->files->data[i]; // FITS file handle
-                    if (!fits) {
-                        continue;
-                    }
-                    pmFPA *fpaIn = data->in->data[i]; // Input FPA
-                    pmChip *chipIn = fpaIn->chips->data[view->chip]; // Input chip
-                    pmCell *cellIn = chipIn->cells->data[view->cell]; // Input cell
-                    if (!cellIn->file_exists || !cellIn->data_exists) {
-                        continue;
-                    }
-                    if (!pmCellRead(cellIn, fits, config->database) || cellIn->readouts->n == 0) {
-                        continue;
-                    }
-                    stack->data[i] = cellIn->readouts->data[0];
-                    numRead++;
-                }
-                if (numRead > 0) {
-                    pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
-                    psTrace("ppMerge", 5, "Chip %d, cell %d\n", view->chip, view->cell);
-                }
-
-                memCheck();
-#endif
-            }
+                numScan++;
+
+            } while (numRead > 0);
 
             psFree(stack);
@@ -392,4 +286,5 @@
 
     psFree(view);
+    psFree(rng);
 
     return true;
