Index: trunk/ppImage/src/Makefile.am
===================================================================
--- trunk/ppImage/src/Makefile.am	(revision 25875)
+++ trunk/ppImage/src/Makefile.am	(revision 25930)
@@ -41,4 +41,5 @@
 	ppImageDetrendFringe.c \
 	ppImageDetrendFree.c \
+	ppImageDetrendPattern.c \
 	ppImageRebinReadout.c \
 	ppImageMosaic.c \
Index: trunk/ppImage/src/ppImage.h
===================================================================
--- trunk/ppImage/src/ppImage.h	(revision 25875)
+++ trunk/ppImage/src/ppImage.h	(revision 25930)
@@ -159,4 +159,6 @@
 bool ppImageBurntoolMask(pmConfig *config, ppImageOptions *options, pmFPAview *view, pmReadout *mask);
 
+bool ppImageDetrendPatternApply(pmConfig *config, pmChip *chip, const pmFPAview *inputView, const ppImageOptions *options);
+
 // Record which detrend file was used for the detrending
 bool ppImageDetrendRecord(
Index: trunk/ppImage/src/ppImageDetrendFringe.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendFringe.c	(revision 25875)
+++ trunk/ppImage/src/ppImageDetrendFringe.c	(revision 25930)
@@ -84,4 +84,6 @@
     psArray *cells = chip->cells;       // Component cells
     psArray *fringes = psArrayAlloc(cells->n); // Fringes, to return
+    int video_cell_zero = 0;
+    
     for (int i = 0; i < cells->n; i++) {
         fringes->data[i] = NULL;
@@ -89,13 +91,42 @@
         pmCell *cell = cells->data[i];  // Cell of interest
 
+	psTrace("psModules.detrend",7,"Readouts: Cell %d chip: %ld\n",i,cell->readouts->n);
 	// XXX for now, skip the video cells (cell->readouts->n > 1)
-	if (cell->readouts->n > 1) {
-	    psWarning ("Skipping Video Cell (%d) for ppImageDetrendFringe.c:getFringes", i);
-	    continue;
-	}
-
+	// CZW: This mess creates a fake set of fringe stats by stealing the previous one.
+	// We let the fitting code know that this is all lies by scaling the weights by a crazy amount.
+
+	if ( (cell->readouts->n > 1) ) {
+	  psTrace("psModules.detrend",7,"Should be skipping scichip: %d\n",i);
+	  psWarning ("Skipping Video Cell (%d) for ppImageDetrendFringe.c:getFringes", i);
+
+	  if (i == 0) {
+	    video_cell_zero = 1;
+	  }
+	  else {
+	    pmFringeStats *prevFringe = fringes->data[i-1];
+	    pmFringeStats *fringe     = pmFringeStatsAlloc(prevFringe->regions);
+	    for (int j = 0; j < fringe->regions->nRequested; j++) {
+	      fringe->f->data.F32[j] = prevFringe->f->data.F32[j];
+	      fringe->df->data.F32[j] = prevFringe->df->data.F32[j] / 1e6;
+	    }
+	    fringes->data[i] = fringe;
+	  }
+	    
+	  continue;
+	}
+	
         fringes->data[i] = psMemIncrRefCounter(psMetadataLookupPtr(NULL, cell->analysis, source));
     }
 
+    if (video_cell_zero == 1) {
+      pmFringeStats *prevFringe = fringes->data[1];
+      pmFringeStats *fringe     = pmFringeStatsAlloc(prevFringe->regions);
+      for (int j = 0; j < fringe->regions->nRequested; j++) {
+	fringe->f->data.F32[j] = NAN;
+	fringe->df->data.F32[j] = 1.0;
+      }
+      fringes->data[0] = fringe;
+    }
+    
     return fringes;
 }
@@ -112,5 +143,5 @@
     psArray *science = NULL;
     if (isResidual) {
-	science = getFringes(scienceChip, "FRINGE.RESIDUALS"); // Fringe residuals on science chip
+	science = getFringes(scienceChip,  "FRINGE.RESIDUALS"); // Fringe residuals on science chip
     } else {
 	science = getFringes(scienceChip, "FRINGE.MEASUREMENTS"); // Fringe measurements on science chip
@@ -124,12 +155,13 @@
     psArray *references = getFringes(refChip, "FRINGE.MEASUREMENTS"); // Fringe measurements on reference chip
     int numRefs = ((psArray*)references->data[0])->n; // Number of reference fringes
-    psArray *referencesCat = psArrayAlloc(numRefs); // Reference fringes
-    for (int i = 0; i < numRefs; i++) { // Iterate over fringes
-        psArray *refs = psArrayAlloc(references->n); // Array of fringes for each cell
-        for (int j = 0; j < references->n; j++) { // Iterate over cells
-            psArray *ref = references->data[j]; // Array of references for this cell
+    psArray *referencesCat = psArrayAlloc(numRefs);   // Reference fringes
+    for (int i = 0; i < numRefs; i++) {               // Iterate over fringes
+        psArray *refs = psArrayAlloc(references->n);  // Array of fringes for each cell
+        for (int j = 0; j < references->n; j++) {     // Iterate over cells
+            psArray *ref = references->data[j];       // Array of references for this cell
+	    
             refs->data[j] = psMemIncrRefCounter(ref->data[i]);
         }
-        referencesCat->data[i] = pmFringeStatsConcatenate(refs, NULL, NULL);
+	referencesCat->data[i] = pmFringeStatsConcatenate(refs, NULL, NULL);
         psFree(refs);
     }
Index: trunk/ppImage/src/ppImageDetrendPattern.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendPattern.c	(revision 25930)
+++ trunk/ppImage/src/ppImageDetrendPattern.c	(revision 25930)
@@ -0,0 +1,69 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "ppImage.h"
+
+#define ESCAPE(MESSAGE) { \
+  psError(PS_ERR_UNKNOWN, false, MESSAGE); \
+  psFree(view); \
+  return false; \
+}
+
+bool ppImageDetrendPatternApply(pmConfig *config, pmChip *chip, const pmFPAview *inputView, const ppImageOptions *options) {
+
+  
+  pmCell *cell = NULL;
+
+  assert (options->doPattern); // do not call if not needed
+  assert (inputView->chip != -1);
+  assert (inputView->cell == -1);
+  assert (inputView->readout == -1);
+  bool status;
+  pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT");
+  
+  pmFPAview *view = pmFPAviewAlloc(0); // View for local processing
+  *view = *inputView;
+
+  while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
+    if (!cell->process || !cell->file_exists) {
+      continue;
+    }
+    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+      ESCAPE("load failure for Cell");
+    }
+
+    if (!cell->data_exists) {
+      continue;
+    }
+    
+    if (cell->readouts->n > 1) {
+      psWarning ("Skipping Video Cell for ppImageDetrendPatternApply");
+      continue;
+    }
+    
+    // process each of the readouts
+    pmReadout *readout;         // Readout from cell
+    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+      if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	ESCAPE("load failure for Readout");
+      }
+      if (!readout->data_exists) {
+	continue;
+      }
+
+      // perfore pattern correction
+      if (!pmPatternRow(readout, options->patternOrder, options->patternIter, options->patternRej,
+			options->patternThresh, options->patternMean, options->patternStdev,
+			options->maskValue, options->darkMask)) {
+	psFree(view);
+	return(false);
+      }
+    }
+  }
+
+  psFree(view);
+  return(true);
+}
+  
+    
Index: trunk/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendReadout.c	(revision 25875)
+++ trunk/ppImage/src/ppImageDetrendReadout.c	(revision 25930)
@@ -128,13 +128,13 @@
     }
 
-    // Pattern noise correction
-    if (options->doPattern) {
-        if (!pmPatternRow(input, options->patternOrder, options->patternIter, options->patternRej,
-                          options->patternThresh, options->patternMean, options->patternStdev,
-                          options->maskValue, options->darkMask)) {
-            psFree(detview);
-            return false;
-        }
-    }
+/*     // Pattern noise correction */
+/*     if (options->doPattern) { */
+/*         if (!pmPatternRow(input, options->patternOrder, options->patternIter, options->patternRej, */
+/*                           options->patternThresh, options->patternMean, options->patternStdev, */
+/*                           options->maskValue, options->darkMask)) { */
+/*             psFree(detview); */
+/*             return false; */
+/*         } */
+/*     } */
 
     // Normalization by a single (known) constant
Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 25875)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 25930)
@@ -163,4 +163,11 @@
         }
 
+	// Apply the pattern noise correction
+	if (options->doPattern) {
+	  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)) {
Index: trunk/ppImage/src/ppImageOptions.c
===================================================================
--- trunk/ppImage/src/ppImageOptions.c	(revision 25875)
+++ trunk/ppImage/src/ppImageOptions.c	(revision 25930)
@@ -249,5 +249,6 @@
 
     options->burntoolTrails = psMetadataLookupS32(&status, recipe, "BURNTOOL.TRAILS");
-    fprintf(stderr,"TRAILS: %d %d %d\n",options->burntoolTrails,psMetadataLookupS32(&status,recipe,"BURNTOOL.TRAILS"),status);
+    psTrace("psModules.detrend", 7, "burntoolTrails: %d BURNTOOL.TRAILS: %d Status: %d\n",
+	    options->burntoolTrails,psMetadataLookupS32(&status,recipe,"BURNTOOL.TRAILS"),status);
     if (!status) {
       psWarning("BURNTOOL.TRAILS not found in recipe: setting to default value.\n");
