Index: trunk/ppMerge/src/ppMergeCombine.c
===================================================================
--- trunk/ppMerge/src/ppMergeCombine.c	(revision 8847)
+++ trunk/ppMerge/src/ppMergeCombine.c	(revision 9325)
@@ -123,11 +123,14 @@
             }
 
-#if 1
-            // Read bit by bit
-
-            int numRead;  // Number of inputs read
-            int numScan = 0;
-            do {
-                numRead = 0;
+            // 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
+                exptimes->n = filenames->n;
+                psVectorInit(exptimes, 0);
+                psArray *images = psArrayAlloc(filenames->n); // Array of images
+                psArray *masks = psArrayAlloc(filenames->n); // Array of masks
+                images->n = masks->n = filenames->n;
                 for (int i = 0; i < filenames->n; i++) {
                     if (! filenames->data[i] || strlen(filenames->data[i]) == 0) {
@@ -136,68 +139,119 @@
                     psFits *fits = data->files->data[i]; // FITS file handle
                     if (!fits) {
-                        psError(PS_ERR_IO, false, "Unable to open input file %s --- ignored.\n",
-                                (char*)filenames->data[i]);
-                        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]);
+                        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];
+                    images->data[i] = psMemIncrRefCounter(readoutIn->image);
+                    masks->data[i] = psMemIncrRefCounter(readoutIn->mask);
+                    numRead++;
+                }
+                if (numRead > 1) {
+                    readout->image = pmShutterCorrectionMeasure(exptimes, images, masks,
+                                                                options->shutterSize, options->mean,
+                                                                options->stdev, options->combine->maskVal);
+                    psTrace(__func__, 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;
+                }
+                psFree(images);
+                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;
                         }
-                        numRead++;
-                    }
+                        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++;
+                        }
+                    }
+
+                    if (numRead > 0) {
+                        pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
+                        psTrace(__func__, 5, "Chip %d, cell %d, scan %d\n", view->chip, view->cell, numScan);
+                    }
+                    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(__func__, 5, "Chip %d, cell %d, scan %d\n", view->chip, view->cell, numScan);
+                    psTrace(__func__, 5, "Chip %d, cell %d\n", view->chip, view->cell);
                 }
-                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) {
-                    psError(PS_ERR_IO, false, "Unable to open input file %s --- ignored.\n",
-                            filenames->data[i]);
-                    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(__func__, 5, "Chip %d, cell %d\n", view->chip, view->cell);
-            }
-
-            memCheck();
+
+                memCheck();
 #endif
+            }
 
             psFree(readout);            // Drop reference
