Index: trunk/ppMerge/src/ppMergeLoop.c
===================================================================
--- trunk/ppMerge/src/ppMergeLoop.c	(revision 42764)
+++ trunk/ppMerge/src/ppMergeLoop.c	(revision 42785)
@@ -39,12 +39,7 @@
     bool haveVariances = psMetadataLookupBool(&mdok, arguments, "INPUTS.VARIANCES"); // Do we have variances?
 
-    psArray *inputs = ppMergeFileDataLevel(config, "PPMERGE.INPUT", PM_FPA_LEVEL_READOUT); // Input images
-    psArray *masks = NULL, *variances = NULL; // Input masks and variances
-    if (haveMasks) {
-        masks = ppMergeFileDataLevel(config, "PPMERGE.INPUT.MASK", PM_FPA_LEVEL_READOUT);
-    }
-    if (haveVariances) {
-        variances = ppMergeFileDataLevel(config, "PPMERGE.INPUT.VARIANCE", PM_FPA_LEVEL_READOUT);
-    }
+    psArray *inputs    = ppMergeFileDataLevel(config, "PPMERGE.INPUT"); // Input images
+    psArray *masks     = haveMasks ? ppMergeFileDataLevel(config, "PPMERGE.INPUT.MASK") : NULL; // Input masks 
+    psArray *variances = haveVariances ? ppMergeFileDataLevel(config, "PPMERGE.INPUT.VARIANCE") : NULL; // Input variances
 
     int nThreads = psMetadataLookupS32 (&mdok, arguments, "NTHREADS");
@@ -135,5 +130,4 @@
     psString darkNorm = psMetadataLookupStr(&mdok, arguments, "DARK.NORM"); ///< Dark normalisation
 
-
     if (!ppMergeFileActivate(config, PPMERGE_FILES_ALL, true)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to activate files.");
@@ -152,5 +146,4 @@
     }
 
-
     // Average concepts across inputs
     {
@@ -180,5 +173,5 @@
         {
             psList *inChips = psListAlloc(NULL);
-            for (int i=0; i < numFiles; i++) {
+            for (int i = 0; i < numFiles; i++) {
                 pmChip *chip = pmFPAviewThisChip(view, ((pmFPAfile *)inputs->data[i])->fpa);
                 psListAdd(inChips, PS_LIST_TAIL, chip);
@@ -193,7 +186,4 @@
 
         pmCell *outCell;                ///< Cell of interest
-
-        // XXX TEST : force a single loop
-        // outCell = pmFPAviewNextCell(view, outFPA, 1); {
 
         while ((outCell = pmFPAviewNextCell(view, outFPA, 1))) {
@@ -291,4 +281,5 @@
                 psAssert (fileGroups->n > 0, "no valid file groups defined");
                 fileGroup = fileGroups->data[0];
+
                 if (!pmDarkCombinePrepare(outCell, fileGroup->readouts, darkOrdinates, darkNorm)) {
                     goto ERROR;
@@ -305,4 +296,6 @@
                 psAbort("Should never get here.");
             }
+	    fprintf (stderr, "after pmDarkCombinePrepare / pmReadoutCombinePrepare:\n");
+	    psMemStats(true, NULL, NULL);
 
             // Read input data by chunks
@@ -357,6 +350,8 @@
                       // call: pmDarkCombine(outCell, fileGroup->readouts, iter, rej, maskVal);
                       if (!psThreadJobAddPending(job)) {
-			//                          goto ERROR;
-			// continue; // I don't care.
+			// CZW commented this out with a note that this was needed to allow ppMerge to work even if some inputs are invalid.
+			// EAM thinks pmDarkCombine will only return FALSE if there is a programming or setup error (see pmDark.c:pmDarkCombine)
+			// if so, we should raise an error here.
+			// goto ERROR;
                       }
                       break;
@@ -400,5 +395,4 @@
 
             psFree(fileGroups);
-	    //            psFree(zeros);
 
             // XXX eventually need to keep both the shutter and the pattern, as we do with dark
@@ -541,4 +535,12 @@
             }
 
+            // free the data used by the input files (we need to do the work ourselves)
+            for (int i = 0; i < numFiles; i++) {
+                if (!ppMergeFileFreeInput(config, view, i)) {
+                    psError(PS_ERR_UNKNOWN, false, "Unable to open file %d", i);
+                    goto ERROR;
+                }
+            }
+
             psFree(outRO);
             cellNum++;
@@ -547,4 +549,6 @@
                 goto ERROR;
             }
+
+	    // pmFPAfileIOList (config);
         }
 
@@ -564,4 +568,5 @@
     psFree(variances);
     psFree(stats);
+    psFree(zeros);
     return true;
 
@@ -573,4 +578,5 @@
     psFree(variances);
     psFree(stats);
+    psFree(zeros);
     return false;
 }
