Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 7771)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 8348)
@@ -1,3 +1,4 @@
-# include "ppImage.h"
+#include <ppStats.h>
+#include "ppImage.h"
 
 bool ppImageLoop (pmConfig *config, ppImageOptions *options) {
@@ -8,8 +9,22 @@
     pmReadout *readout;
 
+    bool mdok;                      // Status of MD lookup
+    psMetadata *extraOpts = psMetadataLookupMD(&mdok, config->arguments, "PPIMAGE.OPTIONS"); // Extra options
+    const char *statsName = psMetadataLookupStr(&mdok, extraOpts, "STATS"); // Filename for statistics
+    psMetadata *stats = NULL;           // Container for statistics
+    FILE *statsFile = NULL;             // File stream for statistics
+    if (mdok && statsName && strlen(statsName) > 0) {
+        statsFile = fopen(statsName, "w");
+        if (!statsFile) {
+            psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", statsName);
+        } else {
+            stats = psMetadataAlloc();
+        }
+    }
+
     pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PPIMAGE.INPUT");
     if (!status) {
-	psErrorStackPrint(stderr, "Can't find input data!\n");
-	exit(EXIT_FAILURE);
+        psErrorStackPrint(stderr, "Can't find input data!\n");
+        exit(EXIT_FAILURE);
     }
 
@@ -23,32 +38,52 @@
         if (!chip->process || !chip->file_exists) { continue; }
 
-	if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) return false;
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) return false;
 
-	while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
+        while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
             psLogMsg ("ppImageLoop", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
             if (!cell->process || !cell->file_exists) { continue; }
-	    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) return false;
+            if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) return false;
 
-	    // process each of the readouts
-	    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
-		if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) return false;
-		if (!readout->data_exists) { continue; }
+            // process each of the readouts
+            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+                if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) return false;
+                if (!readout->data_exists) { continue; }
 
-		// perform the detrend analysis
-		if (!ppImageDetrendReadout (config, options, view)) return false;
-		
-		if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
-	    }
-	    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
-	}
+                // perform the detrend analysis
+                if (!ppImageDetrendReadout (config, options, view)) return false;
 
-	ppImageMosaicChip (config, view, "PPIMAGE.OUTPUT.CHIP", "PPIMAGE.OUTPUT");
-	ppImageRebinChip (config, view, "PPIMAGE.BIN1", "PPIMAGE.OUTPUT.CHIP");
-	ppImageRebinChip (config, view, "PPIMAGE.BIN2", "PPIMAGE.OUTPUT.CHIP");
+                if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
+            }
 
-	// we perform photometry on the readouts of this chip in the output
-	if (options->doPhotom) ppImagePhotom (config, view);
+            // Perform statistics on the detrended cell
+            if (stats) {
+                ppStats(stats, input->fpa, view, config);
+            }
 
-	if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
+            if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
+        }
+
+        ppImageMosaicChip (config, view, "PPIMAGE.OUTPUT.CHIP", "PPIMAGE.OUTPUT");
+        ppImageRebinChip (config, view, "PPIMAGE.BIN1", "PPIMAGE.OUTPUT.CHIP");
+        ppImageRebinChip (config, view, "PPIMAGE.BIN2", "PPIMAGE.OUTPUT.CHIP");
+
+        // we perform photometry on the readouts of this chip in the output
+        if (options->doPhotom) ppImagePhotom (config, view);
+
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
+    }
+
+    // Write out summary statistics
+    if (stats) {
+        const char *statsMDC = psMetadataConfigFormat(stats);
+        if (!statsMDC || strlen(statsMDC) == 0) {
+            psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
+        } else {
+            fprintf(statsFile, "%s", statsMDC);
+        }
+        psFree(statsMDC);
+        fclose(statsFile);
+
+        psFree(stats);
     }
 
