Index: trunk/ppMerge/src/ppMergeLoop.c
===================================================================
--- trunk/ppMerge/src/ppMergeLoop.c	(revision 5860)
+++ trunk/ppMerge/src/ppMergeLoop.c	(revision 5862)
@@ -3,7 +3,7 @@
 bool ppMergeLoop (ppData *data, ppOptions *options, ppConfig *config) {
 
-    ppDetrend detrend;
-
-    ppFPA *sampleFPA = data->input->data[0];
+    char name[128];
+    ppFPA *sample = data->input->data[0];
+    pmFPA *sampleFPA = sample->fpa;
 
     psArray *cellList = psArrayAlloc (data->input->n);
@@ -20,6 +20,7 @@
     // iterate over all chips, using sampleFPA as reference
     for (int i = 0; i < sampleFPA->chips->n; i++) {
+        pmChip *maskChip = data->mask->fpa->chips->data[i]; // Chip of interest in input image
+        pmChip *outputChip = data->output->fpa->chips->data[i]; // Chip of interest in input image
         pmChip *sampleChip = sampleFPA->chips->data[i]; // Chip of interest in input image
-        pmChip *maskChip = data->mask->fpa->chips->data[i]; // Chip of interest in input image
         if (! sampleChip->valid) { continue; }
 
@@ -35,6 +36,7 @@
 	// iterate over all cells, using sampleChip as reference
         for (int j = 0; j < sampleChip->cells->n; j++) {
+	    pmCell *maskCell = maskChip->cells->data[j];
+	    pmCell *outputCell = outputChip->cells->data[j];
 	    pmCell *sampleCell = sampleChip->cells->data[j];
-	    pmCell *maskCell = maskChip->cells->data[j];
             if (! sampleCell->valid) { continue; }
 
@@ -50,5 +52,6 @@
 	    // build an array of the cells of interest
 	    for (int k = 0; k < cellList->n; k++) {
-		pmFPA *thisFPA = data->input->data[k];
+		ppFPA *thisFrame = data->input->data[k];
+		pmFPA *thisFPA = thisFrame->fpa;
 		pmChip *thisChip = thisFPA->chips->data[i];
 		cellList->data[k] = thisChip->cells->data[j];
@@ -56,6 +59,27 @@
 
 	    // run the merge function
-	    ppMergeCell (cellList, maskCell, options, config);
+	    ppMergeCell (outputCell, maskCell, cellList, options, config);
+
+	    sprintf (name, "test.%d.%d.fits", i, j);
+	    psFits *fits = psFitsOpen (name, "w");
+	    pmReadout *readout = outputCell->readouts->data[0];
+	    psFitsWriteImage (fits, NULL, readout->image, 0);
+	    psFitsClose (fits);
+
+# if (0)
+	    if (sampleCell->hdu) {
+		outputCell->hdu = sampleCell->hdu;
+		outputCell->hdu->images = psArrayAlloc (1);
+		outputCell->hdu->images->data[0] = outputCell->readouts->data[0];
+	    }
+# endif
 	}
+# if (0)	
+	if (sampleChip->hdu) {
+	    outputChip->hdu = sampleChip->hdu;
+	    outputChip->hdu->images = psArrayAlloc (1);
+	    // outputChip->hdu->images->data[0] = outputChip->readouts->data[0];
+	}
+# endif
     }
     return true;
