Index: trunk/ppMerge/src/ppMergeCheckInputs.c
===================================================================
--- trunk/ppMerge/src/ppMergeCheckInputs.c	(revision 7002)
+++ trunk/ppMerge/src/ppMergeCheckInputs.c	(revision 7061)
@@ -5,9 +5,23 @@
 
 #include "ppMergeCheckInputs.h"
+#include "ppMergeData.h"
 
 // Check input files to make sure everything's consistent
-bool ppCheckInputs(const pmConfig *config // Configuration
+ppMergeData *ppMergeCheckInputs(ppMergeOptions *options, // Options
+                                const pmConfig *config // Configuration
     )
 {
+    ppMergeData *data = ppMergeDataAlloc(); // The data, to return
+
+    // Output file
+    psString outName = psMetadataLookupStr(NULL, config->arguments, "OUTPUT"); // The output file name
+    assert(outName);                    // It should be there!
+    data->outFile = psFitsOpen(outName, "w"); // Output FITS file
+    if (!data->outFile) {
+        // There's no point in continuing if we can't open the output
+        psErrorStackPrint(stderr, "Can't open output image: %s\n", outName);
+        exit(EXIT_FAILURE);
+    }
+
     psArray *filenames = psMetadataLookupPtr(NULL, config->arguments, "INPUT"); // The input file names
     assert(filenames);
@@ -27,18 +41,64 @@
         }
         psMetadata *header = psFitsReadHeader(NULL, inFile); // The FITS (primary) header
-        psMetadata *format = pmConfigCameraFormatFromHeader(config, header); // The camera format
-        psFree(header);
-        if (!format) {
-            psErrorPrint(PS_ERR_IO, false, "Unable to identify camera format for input file %s --- "
-                         "ignored.\n", name);
+        psFitsClose(inFile);
+
+        // The formats must be identical.  The chief reason for this is so that we know what output format to
+        // use.  I guess one could specify a different output format on the command line, but how do we
+        // generate a PHU for that?  Perhaps we could revisit this restriction in the future (construct an
+        // FPAview from the specified camera format configuration, and use pmFPAAddSourceFromView), but for
+        // now it's less hassle just to limit the output format to be the input format.
+        if (!data->format) {
+            data->format = pmConfigCameraFormatFromHeader(config, header);
+            psFree(header);
+            if (!options->format) {
+                psLogMsg(__func__, PS_LOG_WARN, "Unable to identify camera format for input file %s --- "
+                             "ignored.\n", name);
+                // Kick it out
+                psFree(header);
+                data->in->data[i] = NULL;
+                continue;
+            }
+        } else if (!pmConfigValidateCameraFormat(options->format, header)) {
+            psLogMsg(__func__, PS_LOG_WARN, "Input file %s doesn't match camera format --- ignored.\n", name);
             // Kick it out
-            psFree(filenames->data[i]);
-            filenames->data[i] = NULL;
+            psFree(header);
+            data->in->data[i] = NULL;
             continue;
         }
-        psFitsClose(inFile);
+
+        data->in->data[i] = pmFPAConstruct(config->camera);
+        pmFPAview *view = pmFPAAddSourceFromHeader(data->in->data[i], header, data->format);
+        psFree(view);
+
+        // Use the first valid input as the basis for the output --- including the header
+        if (!data->out) {
+            data->out = pmFPAConstruct(config->camera);
+            pmFPAview *view = pmFPAAddSourceFromHeader(data->out, header, data->format);
+            psFree(view);
+        }
+
         numGood++;
     }
 
+    // Count the cells
+    int numCells = 0;           // Number of cells in the output FPA
+    psArray *chips = data->out->chips; // Array of chips in output
+    for (int i = 0; i < chips->n; i++) {
+        pmChip *chip = chips->data[i];  // Chip of interest
+        if (!chip) {
+            continue;
+        }
+        psArray *cells = chip->cells;   // Array of cells
+        for (int j = 0; j < cells->n; j++) {
+            pmCell *cell = cells->data[j];
+                if (cell) {
+                    numCells++;
+                }
+        }
+    }
+    data->numCells = numCells;
+
     return (numGood > 1);
 }
+
+
