Index: trunk/ppMerge/src/ppMergeCombine.c
===================================================================
--- trunk/ppMerge/src/ppMergeCombine.c	(revision 16229)
+++ trunk/ppMerge/src/ppMergeCombine.c	(revision 16842)
@@ -16,7 +16,8 @@
 #include "ppMergeVersion.h"
 
+#define TESTING
+
+
 #if 0
-static psMemId memId = 0;
-
 static FILE *dumpFile = NULL;
 
@@ -36,6 +37,8 @@
     exit(EXIT_FAILURE);
 }
-
-
+#endif
+
+#if 0
+static psMemId memId = 0;
 static void memDump(void)
 {
@@ -52,5 +55,7 @@
     psFree(leaks);
 }
-
+#endif
+
+#if 0
 static void memCheck(void)
 {
@@ -73,5 +78,5 @@
         psTrace("ppMerge", 0, "Memory in use: %zd\n", totalSize);
         psTrace("ppMerge", 0, "Largest block: %ld\n", largest);
-        psTrace("ppMerge", 0, "sbrk(): %zd\n", sbrk(0));
+        psTrace("ppMerge", 0, "sbrk(): %p\n", sbrk(0));
     }
     return;
@@ -119,18 +124,20 @@
     }
     while ((chip = pmFPAviewNextChip(view, fpa, 1))) {
-        if (chip->hdu) {
-            // Data will exist soon
-            pmFPAUpdateNames(data->out, chip, NULL);
-            chip->data_exists = true;
-        }
+        if (!chip->hdu) {
+            continue;
+        }
+        // Data will exist soon
+        pmFPAUpdateNames(data->out, chip, NULL);
+        chip->data_exists = true;
         pmChipWrite(chip, data->outFile, config->database, true, false); // Write header only
         pmCell *cell;                   // Cell of interest
         while ((cell = pmFPAviewNextCell(view, fpa, 1))) {
             cellNum++;
-            if (cell->hdu) {
-                // Data will exist soon
-                pmFPAUpdateNames(data->out, chip, cell);
-                chip->data_exists = cell->data_exists = true;
-            }
+            if (!cell->hdu) {
+                continue;
+            }
+            // Data will exist soon
+            pmFPAUpdateNames(data->out, chip, cell);
+            chip->data_exists = cell->data_exists = true;
             pmCellWrite(cell, data->outFile, config->database, true); // Write header only
             pmReadout *readout = pmReadoutAlloc(cell); // Output readout of interest
@@ -197,8 +204,8 @@
 
                         // If we're combining with weights, we want to generate weights.
-                        if (options->combine->weights) {
+                        if (options->combine->weights && !options->dark) {
 
                             // If it's a bias or dark, set the gain to zero: noise only contributed by read
-                            if ((!options->zero && !options->scale) || options->darktime) {
+                            if (!options->zero && !options->scale) {
                                 pmReadoutSetWeight(stack->data[i], false);
                             } else {
@@ -209,5 +216,6 @@
                         numRead++;
                     } else {
-			psTrace("ppMerge", 3, "Finished reading file %d, chip %d, cell %d, scan %d\n", i, view->chip, view->cell, numScan);
+                        psTrace("ppMerge", 3, "Finished reading file %d, chip %d, cell %d, scan %d\n",
+                                i, view->chip, view->cell, numScan);
                     }
 
@@ -220,4 +228,7 @@
                                                     options->shutterIter, options->shutterRej,
                                                     options->combine->maskVal);
+                    } else if (options->dark) {
+                        pmDarkCombine(cell, stack, options->darkOrdinates, options->combine->iter,
+                                      options->combine->rej, options->combine->maskVal);
                     } else {
                         pmReadoutCombine(readout, stack, cellZeros, cellScales, options->combine);
@@ -246,4 +257,5 @@
             psFree(stack);
 
+#if 0
             // Set the dark time for the output image, since we normalised
             if (options->darktime) {
@@ -253,4 +265,5 @@
                 expItem->data.F32 = 1.0;
             }
+#endif
 
             // Measure the fringes for this cell
@@ -334,9 +347,12 @@
             if (cell->hdu && !cell->hdu->blankPHU) {
                 psTrace("ppMerge", 5, "Writing out cell HDU.\n");
-                pmCellWrite(cell, data->outFile, config->database, false);
-                if (options->fringe) {
-                    pmCellWriteTable(data->outFile, cell, "FRINGE");
-                }
-
+                if (options->dark) {
+                    pmCellWriteDark(cell, data->outFile, config->database, false);
+                } else {
+                    pmCellWrite(cell, data->outFile, config->database, false);
+                    if (options->fringe) {
+                        pmCellWriteTable(data->outFile, cell, "FRINGE");
+                    }
+                }
                 pmCellFreeData(cell);
             }
@@ -354,7 +370,11 @@
         if (chip->hdu && !chip->hdu->blankPHU) {
             psTrace("ppMerge", 5, "Writing out chip HDU.\n");
-            pmChipWrite(chip, data->outFile, config->database, false, false);
-            if (options->fringe) {
-                pmChipWriteTable(data->outFile, chip, "FRINGE");
+            if (options->dark) {
+                pmChipWriteDark(chip, data->outFile, config->database, false, false);
+            } else {
+                pmChipWrite(chip, data->outFile, config->database, false, false);
+                if (options->fringe) {
+                    pmChipWriteTable(data->outFile, chip, "FRINGE");
+                }
             }
 
@@ -373,7 +393,11 @@
         // Write the pixels
         psTrace("ppMerge", 5, "Writing out FPA HDU.\n");
-        pmFPAWrite(data->out, data->outFile, config->database, false, false);
-        if (options->fringe) {
-            pmFPAWriteTable(data->outFile, fpa, "FRINGE");
+        if (options->dark) {
+            pmFPAWriteDark(data->out, data->outFile, config->database, false, false);
+        } else {
+            pmFPAWrite(data->out, data->outFile, config->database, false, false);
+            if (options->fringe) {
+                pmFPAWriteTable(data->outFile, fpa, "FRINGE");
+            }
         }
     }
