Index: trunk/ppImage/src/Makefile.am
===================================================================
--- trunk/ppImage/src/Makefile.am	(revision 34846)
+++ trunk/ppImage/src/Makefile.am	(revision 35081)
@@ -44,4 +44,5 @@
 	ppImageRebinReadout.c \
 	ppImageMosaic.c \
+	ppImageMosaicBackground.c \
 	ppImageMaskStats.c \
 	ppImagePhotom.c \
Index: trunk/ppImage/src/ppImage.h
===================================================================
--- trunk/ppImage/src/ppImage.h	(revision 34846)
+++ trunk/ppImage/src/ppImage.h	(revision 35081)
@@ -41,4 +41,5 @@
     bool doPatternCell;                 // Cell pattern correction
     bool doPatternContinuity;           // Cell continuity correction
+    bool doBackgroundContinuity;        // Do mosaic continuity correction
     bool doFringe;                      // Fringe subtraction
     bool doPhotom;                      // Source identification and photometry
@@ -50,6 +51,5 @@
     bool checkNoise;                    // measure cell-level variance
     bool applyParity;                   // Apply Cell parities
-
-  bool doMaskStats;                      // Calculate mask statistics
+    bool doMaskStats;                   // Calculate mask statistics
   
     bool doCrosstalkMeasure;            // measure crosstalk signal
@@ -183,4 +183,7 @@
 bool ppImageDetrendPatternApply(pmConfig *config, pmChip *chip, const pmFPAview *inputView, const ppImageOptions *options);
 
+// Do background continuity step
+bool ppImageMosaicBackground(pmConfig *config, const ppImageOptions *options);
+
 // Record which detrend file was used for the detrending
 bool ppImageDetrendRecord(
Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 34846)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 35081)
@@ -41,5 +41,5 @@
         ESCAPE("load failure for FPA");
     }
-
+    
     pmChip *chip;                       // Chip from FPA
     while ((chip = pmFPAviewNextChip(view, input->fpa, 1)) != NULL) {
@@ -233,5 +233,8 @@
             ESCAPE("Unable to bin chip (level 2).");
         }
-
+	if (options->doBackgroundContinuity) {
+	  pmFPAfile *out = psMetadataLookupPtr(NULL, config->files, "PPIMAGE.BACKMDL");
+	  pmFPAfileCopyView(out->fpa,out->src,view);
+	}
         // Close cells (XXX shouldn't pmFPAfileClose iterate down as needed?)
         view->cell = -1;
@@ -251,4 +254,9 @@
     }
 
+    // Do background model continuity updates
+    if (!ppImageMosaicBackground(config, options )) {
+      ESCAPE("failure in Background Mosaic");
+    }
+    
     // generate the full-scale FPA mosaic
     if (!ppImageMosaicFPA(config, options, "PPIMAGE.OUTPUT.FPA1", "PPIMAGE.BIN1")) {
Index: trunk/ppImage/src/ppImageMosaicBackground.c
===================================================================
--- trunk/ppImage/src/ppImageMosaicBackground.c	(revision 35081)
+++ trunk/ppImage/src/ppImageMosaicBackground.c	(revision 35081)
@@ -0,0 +1,77 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "ppImage.h"
+
+
+bool ppImageMosaicBackground(pmConfig *config, const ppImageOptions *options) {
+  assert(config);
+  assert(options);
+  
+  if (options->doBackgroundContinuity) {
+    bool status;
+    pmFPAfile *in = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT");
+    if (!status) {
+      psErrorStackPrint(stderr, "Can't find required I/O file!\n");
+      exit(EXIT_FAILURE);
+    }
+    pmFPAfile *out = psMetadataLookupPtr(&status, config->files, "PPIMAGE.BACKMDL");
+    if (!status) {
+      psErrorStackPrint(stderr, "Can't find required I/O file!\n");
+      exit(EXIT_FAILURE);
+    }
+    //pmReadout *model = pmFPAGenerateReadout(config, view, psphotGetFilerule("PSPHOT.BACKMDL"), inFPA, binning, index);
+    
+    pmFPAview *view = pmFPAviewAlloc(0);
+    //    pmFPAAddSourceFromView(out->fpa, view, out->format);
+    //    psFree(view);
+    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+      //	ESCAPE("load failure for Chip");
+    }
+
+    pmChip *chip;
+    
+    if (!pmPatternContinuityBackground(in,out,options->patternCellBG,options->patternCellMean,
+				       options->maskValue, options->darkMask, options->patternContinuityEdgeWidth)) {
+      // Free things?
+      psFree(view);
+      return(false);
+    }
+
+    // Write out output models
+    psFree(view);
+    view = pmFPAviewAlloc(0);
+    while ((chip = pmFPAviewNextChip(view, out->fpa, 1)) != NULL) {
+      pmCell *cell;
+      while ((cell = pmFPAviewNextCell(view, out->fpa, 1)) != NULL) {
+	pmReadout *readout;
+	while ((readout = pmFPAviewNextReadout(view, out->fpa, 1)) != NULL) {
+	  if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
+	    psError(PS_ERR_UNKNOWN, false, "I/O failure in ppImageMosaicBackground");
+	    psFree(view);
+	    return(false);
+	  }
+	}
+	if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	  psError(PS_ERR_UNKNOWN, false, "I/O failure in ppImageMosaicBackground");
+	  psFree(view);
+	  return(false);
+	}
+      }
+      if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psError(PS_ERR_UNKNOWN, false, "I/O failure in ppImageMosaicBackground");
+	psFree(view);
+	return(false);
+      }
+    }
+    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+      psError(PS_ERR_UNKNOWN, false, "I/O failure in ppImageMosaicBackground");
+      psFree(view);
+      return(false);
+    }
+    psFree(view);
+  }
+
+  return(true);
+}
Index: trunk/ppImage/src/ppImageOptions.c
===================================================================
--- trunk/ppImage/src/ppImageOptions.c	(revision 34846)
+++ trunk/ppImage/src/ppImageOptions.c	(revision 35081)
@@ -35,4 +35,5 @@
     options->doPatternCell   = false;   // Cell pattern correction
     options->doPatternContinuity = false; // Cell continuity correction
+    options->doBackgroundContinuity = false; // Chip level background continuity correction
     options->doFringe        = false;   // Fringe subtraction
     options->doPhotom        = false;   // Source identification and photometry
@@ -429,4 +430,7 @@
     }
 
+    // Option to enable the background continuity
+    options->doBackgroundContinuity = psMetadataLookupBool(NULL, recipe, "BACKGROUND.CONTINUITY");
+
 
     // Remnance options
Index: trunk/ppImage/src/ppImageParseCamera.c
===================================================================
--- trunk/ppImage/src/ppImageParseCamera.c	(revision 34846)
+++ trunk/ppImage/src/ppImageParseCamera.c	(revision 35081)
@@ -303,4 +303,5 @@
     }
 
+
     // chipImage    -> psphotInput  (pmFPAfileDefineFromFile)       : fpa is constructed
     // psphotInput  -> psphotOutput (pmFPAfileDefineOutputFromFile) : fpa is equated
@@ -387,4 +388,25 @@
     // the input data is the same as the outImage data : force the free levels to match
     input->freeLevel = PS_MIN(outImage->freeLevel, input->freeLevel);
+
+    // define the continuity corrected background model files
+    if (options->doBackgroundContinuity) {
+      pmFPAfile *bkgMosaicModel = pmFPAfileDefineFromFPA(config,input->fpa, 1, 1,  "PPIMAGE.BACKMDL");
+      if (!bkgMosaicModel) {
+	psError(PS_ERR_IO, false, _("Unable to generate new file from PPIMAGE.BACKMDL"));
+	psFree(options);
+	return NULL;
+      }
+      if (bkgMosaicModel->type != PM_FPA_FILE_IMAGE) {
+	psError(PS_ERR_IO, true, "PPIMAGE.BACKMDL is not of type IMAGE");
+	psFree(options);
+	return NULL;
+
+      }
+      bkgMosaicModel->save = options->doBackgroundContinuity;
+      //      bkgMosaicModel->freeLevel = PS_MIN(bkgMosaicModel->freeLevel, PM_FPA_LEVEL_FPA);
+      bkgMosaicModel->freeLevel = PM_FPA_LEVEL_FPA;
+      bkgMosaicModel->dataLevel = bkgMosaicModel->dataLevel;
+      bkgMosaicModel->fileLevel = PS_MIN(bkgMosaicModel->fileLevel, bkgMosaicModel->dataLevel);
+    }
 
     // define the binned target files (which may just be carriers for some camera configurations)
@@ -497,5 +519,5 @@
         chipVariance->save = false;
     }
-
+    
     if (psTraceGetLevel("ppImage.config") > 0) {
         // Get a look inside all the files.
