Index: branches/simmosaic_branches/ppImage/src/ppImageLoop.c
===================================================================
--- branches/simmosaic_branches/ppImage/src/ppImageLoop.c	(revision 24860)
+++ branches/simmosaic_branches/ppImage/src/ppImageLoop.c	(revision 27839)
@@ -8,4 +8,5 @@
   psError(PS_ERR_UNKNOWN, false, MESSAGE); \
   psFree(view); \
+  pmFPAfileFreeSetStrict(false); \
   return false; \
 }
@@ -51,17 +52,17 @@
         }
 
-	// crosstalk measurement needs to be done on the entire chip at once, and before
-	// signal levels are modified by the detrending.  If crosstalk measurement is
-	// requested, the read-level for the images is set to CHIP.
-	if (!ppImageMeasureCrosstalk(config, options, view)) {
-	  ESCAPE("Unable to perform crosstalk correction");
-	}
-
-	// crosstalk correction needs to be done on the entire chip at once, and before
-	// signal levels are modified by the detrending.  If crosstalk correction is
-	// requested, the read-level for the images is set to CHIP.
-	if (!ppImageCorrectCrosstalk(config, options, view)) {
-	  ESCAPE("Unable to perform crosstalk correction");
-	}
+        // crosstalk measurement needs to be done on the entire chip at once, and before
+        // signal levels are modified by the detrending.  If crosstalk measurement is
+        // requested, the read-level for the images is set to CHIP.
+        if (!ppImageMeasureCrosstalk(config, options, view)) {
+          ESCAPE("Unable to perform crosstalk correction");
+        }
+
+        // crosstalk correction needs to be done on the entire chip at once, and before
+        // signal levels are modified by the detrending.  If crosstalk correction is
+        // requested, the read-level for the images is set to CHIP.
+        if (!ppImageCorrectCrosstalk(config, options, view)) {
+          ESCAPE("Unable to perform crosstalk correction");
+        }
 
         psTimerStart(TIMER_DETREND);
@@ -99,11 +100,8 @@
                 }
 
-                // perform the detrend analysis
-                if (!ppImageParityFlip(config, options, view)) {
+                // flip the image to match the native detector orientation (to match bias, flat, etc)
+                if (!ppImageParityFlip(config, options, view, true)) {
                     ESCAPE("Unable to detrend readout");
                 }
-
-		// XXX TEST:
-		// psphotSaveImage (NULL, readout->image, "test.image.fits");
 
                 // XXX set the options->*Mask values here (after the mask images have been loaded
@@ -132,4 +130,17 @@
                 ppImageDetrendRecord(cell, config, options, view);
             }
+
+            // process each of the readouts
+            // XXX reset the view to the first readout?
+            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+                if (!readout->data_exists) {
+                    continue;
+                }
+                // flip the image to match the raw readout orientation
+                if (!ppImageParityFlip(config, options, view, false)) {
+                    ESCAPE("Unable to detrend readout");
+                }
+            }
+
             // free detrend images potentially in use: MASK, BIAS, DARK, SHUTTER, FLAT
             if (!ppImageDetrendFree (config, view)) {
@@ -153,4 +164,11 @@
         }
 
+        // Apply the pattern correction
+        if (options->doPatternRow || options->doPatternCell) {
+          if (!ppImageDetrendPatternApply(config,chip,view,options)) {
+            ESCAPE("Unable to apply pattern corrections");
+          }
+        }
+
         // measure various pixel-based statistics for this image
         if (!ppImagePixelStats(config, stats, options, view)) {
@@ -161,5 +179,4 @@
         }
         timeDetrend += psTimerClear(TIMER_DETREND);
-
 
         // we perform photometry on the readouts of this chip in the output
@@ -250,5 +267,7 @@
     psString dump_file = psMetadataLookupStr(&status, config->arguments, "DUMP_CONFIG");
     if (dump_file) {
-        pmConfigDump(config, dump_file);
+        if (!pmConfigDump(config, dump_file)) {
+            ESCAPE("Unable to dump configuration.");
+        }
     }
 
