Index: trunk/ppMerge/src/ppMergeLoop.c
===================================================================
--- trunk/ppMerge/src/ppMergeLoop.c	(revision 24908)
+++ trunk/ppMerge/src/ppMergeLoop.c	(revision 25769)
@@ -104,6 +104,6 @@
             goto ERROR;
         }
-	// the zeros vector is passed to pmReadoutCombine for each set of inputs per cell
-	zeros = psVectorAlloc(zeroSet->numRows, PS_TYPE_F32);
+        // the zeros vector is passed to pmReadoutCombine for each set of inputs per cell
+        zeros = psVectorAlloc(zeroSet->numRows, PS_TYPE_F32);
         // Flow through
       case PPMERGE_TYPE_FLAT:
@@ -150,4 +150,21 @@
         goto ERROR;
     }
+
+
+    // Average concepts across inputs
+    {
+        psList *inFPAs = psListAlloc(NULL); ///< List of FPAs
+        for (int i = 0; i < numFiles; i++) {
+            pmFPAfile *input = inputs->data[i]; ///< Input file
+            psListAdd(inFPAs, PS_LIST_TAIL, input->fpa);
+        }
+        if (!pmConceptsAverageFPAs(output->fpa, inFPAs)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to average FPA concepts.");
+            psFree(inFPAs);
+            goto ERROR;
+        }
+        psFree(inFPAs);
+    }
+
     pmChip *outChip;                    ///< Chip of interest
     while ((outChip = pmFPAviewNextChip(view, outFPA, 1))) {
@@ -158,13 +175,29 @@
             goto ERROR;
         }
+
+        // Average concepts across inputs
+        {
+            psList *inChips = psListAlloc(NULL);
+            for (int i=0; i < numFiles; i++) {
+                pmChip *chip = pmFPAviewThisChip(view, ((pmFPAfile *)inputs->data[i])->fpa);
+                psListAdd(inChips, PS_LIST_TAIL, chip);
+            }
+            if (!pmConceptsAverageChips(outChip, inChips, true)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to average Chip concepts.");
+                psFree(inChips);
+                goto ERROR;
+            }
+            psFree(inChips);
+        }
+
         pmCell *outCell;                ///< Cell of interest
 
-	// XXX TEST : force a single loop
+        // XXX TEST : force a single loop
         // outCell = pmFPAviewNextCell(view, outFPA, 1); {
 
         while ((outCell = pmFPAviewNextCell(view, outFPA, 1))) {
-	    if (!outCell->process || !outCell->file_exists) {
-		continue;
-	    }
+            if (!outCell->process || !outCell->file_exists) {
+                continue;
+            }
             if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
                 goto ERROR;
@@ -206,15 +239,15 @@
             }
 
-	    if (zeroSet) {
-	      for (int i = 0; i < zeroSet->numRows; i++) {
-		zeros->data.F32[i] = zeroSet->data.F32[i][cellNum];
-	      }
-	    }
-
-	    int rows = psMetadataLookupS32(NULL, config->arguments, "ROWS"); // Number of rows to read per chunk
-	    if (!rows && nThreads) {
-	      psError(PS_ERR_UNKNOWN, false, "Invalid combination of threads > 0 and ROWS == 0 (ie, multiple threads working on the full array...)");
-	      goto ERROR;
-	    }
+            if (zeroSet) {
+              for (int i = 0; i < zeroSet->numRows; i++) {
+                zeros->data.F32[i] = zeroSet->data.F32[i][cellNum];
+              }
+            }
+
+            int rows = psMetadataLookupS32(NULL, config->arguments, "ROWS"); // Number of rows to read per chunk
+            if (!rows && nThreads) {
+              psError(PS_ERR_UNKNOWN, false, "Invalid combination of threads > 0 and ROWS == 0 (ie, multiple threads working on the full array...)");
+              goto ERROR;
+            }
 
             ppMergeFileGroup *fileGroup = NULL;
@@ -227,6 +260,6 @@
                     pmFPAfile *input = pmFPAfileSelectSingle(config->files, "PPMERGE.INPUT", j);
                     pmCell *inCell = pmFPAviewThisCell(view, input->fpa); ///< Input cell
-		    pmReadout *readout = pmReadoutAlloc(inCell);
-		    readout->process = true; // until proven otherwise, attempt to process this readout
+                    pmReadout *readout = pmReadoutAlloc(inCell);
+                    readout->process = true; // until proven otherwise, attempt to process this readout
                     readouts->data[j] = readout;
                 }
@@ -361,5 +394,5 @@
 
             psFree(fileGroups);
-	    psFree(zeros);
+            psFree(zeros);
 
             // XXX eventually need to keep both the shutter and the pattern, as we do with dark
@@ -424,13 +457,13 @@
             // Put the new readout into the cell after the existing readouts.
             if (type == PPMERGE_TYPE_FRINGE && outRO) {
-		if (fringeSmooth) {
-		    if (outRO->mask) {
-			psImage *smoothed = psImageSmoothMask (NULL, outRO->image, outRO->mask, maskVal, fringeSmoothSigma, 3, 0.2);
-			psFree (outRO->image);
-			outRO->image = smoothed;
-		    } else {
-			psImageSmooth (outRO->image, fringeSmoothSigma, 3);
-		    }
-		}
+                if (fringeSmooth) {
+                    if (outRO->mask) {
+                        psImage *smoothed = psImageSmoothMask (NULL, outRO->image, outRO->mask, maskVal, fringeSmoothSigma, 3, 0.2);
+                        psFree (outRO->image);
+                        outRO->image = smoothed;
+                    } else {
+                        psImageSmooth (outRO->image, fringeSmoothSigma, 3);
+                    }
+                }
 
                 pmFringeRegions *regions = pmFringeRegionsAlloc(fringeNum, fringeSize, fringeSize, fringeSmoothX, fringeSmoothY);
@@ -447,8 +480,8 @@
 
                 // XXX replaced this : pmFringesFormat(outCell, NULL, fringes);
-		if (!psMetadataAdd(outCell->analysis, PS_LIST_TAIL, "FRINGE.MEASUREMENTS", PS_DATA_ARRAY, "Fringes", fringes)) {
-		    psError(PS_ERR_UNKNOWN, false, "Unable to add fringe to analysis metadata\n");
-		    goto ERROR;
-		}
+                if (!psMetadataAdd(outCell->analysis, PS_LIST_TAIL, "FRINGE.MEASUREMENTS", PS_DATA_ARRAY, "Fringes", fringes)) {
+                    psError(PS_ERR_UNKNOWN, false, "Unable to add fringe to analysis metadata\n");
+                    goto ERROR;
+                }
                 psFree(fringes);        // Drop reference
             }
@@ -472,14 +505,14 @@
                 psImageMaskType cteMaskValue = pmConfigMaskGet(cteMaskName, config);
 
-		if (0) {
-		  psFits *fits = NULL;
-		  fits = psFitsOpen ("combine.fits", "w");
-		  psFitsWriteImage (fits, NULL, outRO->image, 0, NULL);
-		  psFitsClose (fits);
-
-		  fits = psFitsOpen ("inmask.fits", "w");
-		  psFitsWriteImage (fits, NULL, outRO->mask, 0, NULL);
-		  psFitsClose (fits);
-		}
+                if (0) {
+                  psFits *fits = NULL;
+                  fits = psFitsOpen ("combine.fits", "w");
+                  psFitsWriteImage (fits, NULL, outRO->image, 0, NULL);
+                  psFitsClose (fits);
+
+                  fits = psFitsOpen ("inmask.fits", "w");
+                  psFitsWriteImage (fits, NULL, outRO->mask, 0, NULL);
+                  psFitsClose (fits);
+                }
 
                 psF32 **outputImage = outRO->image->data.F32;
@@ -493,10 +526,10 @@
                 }
 
-		if (0) {
-		  psFits *fits = NULL;
-		  fits = psFitsOpen ("otmask.fits", "w");
-		  psFitsWriteImage (fits, NULL, outRO->mask, 0, NULL);
-		  psFitsClose (fits);
-		}
+                if (0) {
+                  psFits *fits = NULL;
+                  fits = psFitsOpen ("otmask.fits", "w");
+                  psFitsWriteImage (fits, NULL, outRO->mask, 0, NULL);
+                  psFitsClose (fits);
+                }
 
             }
@@ -510,35 +543,8 @@
         }
 
-        if (outChip->data_exists) {
-            psList *inChips = psListAlloc(NULL);
-            for (int i=0; i < numFiles; i++) {
-                pmChip *chip = pmFPAviewThisChip(view, ((pmFPAfile *)inputs->data[i])->fpa);
-                psListAdd(inChips, PS_LIST_TAIL, chip);
-            }
-            if (!pmConceptsAverageChips(outChip, inChips, true)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to average Chip concepts.");
-                psFree(inChips);
-                goto ERROR;
-            }
-            psFree(inChips);
-        }
         if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
             goto ERROR;
         }
     }
-
-    // Get list of FPAs for concepts averaging
-    psList *inFPAs = psListAlloc(NULL); ///< List of FPAs
-    for (int i = 0; i < numFiles; i++) {
-        pmFPAfile *input = inputs->data[i]; ///< Input file
-        psListAdd(inFPAs, PS_LIST_TAIL, input->fpa);
-    }
-    if (!pmConceptsAverageFPAs(output->fpa, inFPAs)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to average FPA concepts.");
-        psFree(inFPAs);
-        goto ERROR;
-    }
-    psFree(inFPAs);
-
 
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
