Index: trunk/ppMerge/src/ppMergeCombine.c
===================================================================
--- trunk/ppMerge/src/ppMergeCombine.c	(revision 7067)
+++ trunk/ppMerge/src/ppMergeCombine.c	(revision 7260)
@@ -7,4 +7,5 @@
 #include "ppMergeData.h"
 #include "ppMergeCombine.h"
+
 
 // Combine the inputs
@@ -22,9 +23,9 @@
     // Sanity checks
     assert(!options->scale || scales);
-    assert(!scales || (scales->type.type == PS_TYPE_F64 &&
+    assert(!scales || (scales->type.type == PS_TYPE_F32 &&
                        scales->numCols == data->numCells &&
                        scales->numRows == filenames->n));
     assert(!options->zero || zeros);
-    assert(!zeros || (zeros->type.type == PS_TYPE_F64 &&
+    assert(!zeros || (zeros->type.type == PS_TYPE_F32 &&
                       zeros->numCols == data->numCells &&
                       zeros->numRows == filenames->n));
@@ -32,5 +33,5 @@
     // Iterate over the FPA
     int cellNum = -1;                   // Cell number
-    pmFPAWrite(data->out, data->outFile, config->database, false);
+    pmFPAWrite(data->out, data->outFile, config->database, false, false); // Write header only
     psArray *chips = data->out->chips;  // Array of output chips
     for (int i = 0; i < chips->n; i++) {
@@ -39,5 +40,5 @@
             continue;
         }
-        pmChipWrite(chip, data->outFile, config->database, false);
+        pmChipWrite(chip, data->outFile, config->database, false, false); // Write header only
         psArray *cells = chip->cells;   // Array of output cells
         for (int j = 0; j < cells->n; j++) {
@@ -47,7 +48,8 @@
             }
             cellNum++;
-            pmCellWrite(cell, data->outFile, config->database, false);
+            pmCellWrite(cell, data->outFile, config->database, false); // Write header only
             pmReadout *readout = pmReadoutAlloc(cell); // Output readout of interest
             psArray *stack = psArrayAlloc(filenames->n); // Stack of readouts to combine
+            stack->n = filenames->n;
             psVector *cellScales = NULL; // Scales for this cell
             if (scales) {
@@ -59,7 +61,7 @@
             }
 
-            bool stillReadingRows = false;  // Still reading rows from any of the files?
-
+            int numRead;  // Number of inputs read
             do {
+                numRead = 0;
                 for (int k = 0; k < filenames->n; k++) {
                     if (! filenames->data[k] || strlen(filenames->data[k]) == 0) {
@@ -82,15 +84,17 @@
 
                     // Only reading and writing the first readout in each cell (plane 0)
-                    stillReadingRows |= pmReadoutReadNext(stack->data[k], fits, 0, options->rows);
+                    if (pmReadoutReadNext(stack->data[k], fits, 0, options->rows)) {
+                        numRead++;
+                    }
                     psFitsClose(fits);
                 }
 
-                pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
-                pmReadoutWriteNext(readout, data->outFile, 0);
+                if (numRead > 0) {
+                    pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
+                    psTrace(__func__, 5, "Chip %d, cell %d, readout size %dx%d\n", i, j,
+                            readout->image->numCols, readout->image->numRows);
+                }
 
-            } while (stillReadingRows);
-
-            // Write the pixels
-            pmCellWrite(cell, data->outFile, config->database, true);
+            } while (numRead > 0);
 
             // Blow away the cell data
@@ -101,9 +105,12 @@
                 pmCellFreeData(cellIn);
             }
-            pmCellFreeData(cell);
+
+            // Write the pixels
+            if (cell->hdu) {
+                psTrace(__func__, 5, "Writing out cell HDU.\n");
+                pmCellWrite(cell, data->outFile, config->database, true);
+                pmCellFreeData(cell);
+            }
         }
-
-        // Write the pixels
-        pmChipWrite(chip, data->outFile, config->database, true);
 
         // Blow away the chip data
@@ -113,10 +120,18 @@
             pmChipFreeData(chipIn);
         }
-        pmChipFreeData(chip);
 
+        // Write the pixels
+        if (chip->hdu) {
+            psTrace(__func__, 5, "Writing out chip HDU.\n");
+            pmChipWrite(chip, data->outFile, config->database, true, false);
+            pmChipFreeData(chip);
+        }
     }
 
-    // Write the pixels
-    pmFPAWrite(data->out, data->outFile, config->database, true);
+    if (data->out->hdu) {
+        // Write the pixels
+        psTrace(__func__, 5, "Writing out FPA HDU.\n");
+        pmFPAWrite(data->out, data->outFile, config->database, true, false);
+    }
 
     // Blow away the FPA data
