Index: /branches/czw_branch/20120906/ippScripts/scripts/skycell_jpeg.pl
===================================================================
--- /branches/czw_branch/20120906/ippScripts/scripts/skycell_jpeg.pl	(revision 34627)
+++ /branches/czw_branch/20120906/ippScripts/scripts/skycell_jpeg.pl	(revision 34628)
@@ -269,10 +269,10 @@
     my %tangents = ();
 
-    my %products = ('image' => "PPSTACK.OUTPUT",
-		    'mask'  => "PPSTACK.OUTPUT.MASK",
-		    'variance' => "PPSTACK.OUTPUT.VARIANCE",
-		    'exp'   => "PPSTACK.OUTPUT.EXP",
-		    'num'   => "PPSTACK.OUTPUT.EXPNUM",
-#		    'bkg'   => "PPSTACK.OUTPUT.BKG"
+    my %products = ('image' => "PPSTACK.UNCONV",
+		    'mask'  => "PPSTACK.UNCONV.MASK",
+		    'variance' => "PPSTACK.UNCONV.VARIANCE",
+		    'exp'   => "PPSTACK.UNCONV.EXP",
+		    'num'   => "PPSTACK.UNCONV.EXPNUM",
+		    'bkg'   => "PPSTACK.OUTPUT.BKGMODEL"
 	);
     
@@ -292,7 +292,5 @@
 	unless (exists($tangents{$projection_cell})) {
 	    # Make a temp file and fill, but be sure to save 
-	    
 	    foreach my $key (keys %products) {
-		
 		($tempFile, $tempName) = tempfile("/tmp/skycell.$projection_cell.$key.XXXX",
 						  UNLINK => !$save_temps);
@@ -321,4 +319,13 @@
 	    $command = "$ppSkycell -images $tangents{$projection_cell}{$key}{NAME}";
 	    $command .= " ${outroot}.${projection_cell}.${key} ";
+	    if ($key eq 'bkg') {
+		$command .= " -Di BIN1 1 -Di BIN2 1 ";
+	    }
+	    elsif ($key eq 'image') {
+		$command .= " -masks $tangents{$projection_cell}{mask}{NAME} ";
+	    }
+	    elsif ($key eq 'mask') { 
+		next; # This should be made with the images.
+	    }
 	    print "$command\n";
 	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
Index: /branches/czw_branch/20120906/ippconfig/recipes/filerules-mef.mdc
===================================================================
--- /branches/czw_branch/20120906/ippconfig/recipes/filerules-mef.mdc	(revision 34627)
+++ /branches/czw_branch/20120906/ippconfig/recipes/filerules-mef.mdc	(revision 34628)
@@ -342,4 +342,6 @@
 PPSKYCELL.BIN1    	OUTPUT {OUTPUT}.{FILE.INDEX}.b1.fits     IMAGE     NONE       FPA        TRUE      NONE
 PPSKYCELL.BIN2    	OUTPUT {OUTPUT}.{FILE.INDEX}.b2.fits     IMAGE     NONE       FPA        TRUE      NONE
+PPSKYCELL.BIN1.MASK    	     OUTPUT {OUTPUT}.{FILE.INDEX}.b1.mk.fits     MASK     COMP_MASK       FPA        TRUE      NONE
+PPSKYCELL.BIN2.MASK    	     OUTPUT {OUTPUT}.{FILE.INDEX}.b2.mk.fits     MASK     COMP_MASK       FPA       TRUE      NONE
 
 LOG.IMFILE              OUTPUT {OUTPUT}.{CHIP.NAME}.log          TEXT      NONE       CHIP       TRUE      NONE
Index: /branches/czw_branch/20120906/ippconfig/recipes/filerules-simple.mdc
===================================================================
--- /branches/czw_branch/20120906/ippconfig/recipes/filerules-simple.mdc	(revision 34627)
+++ /branches/czw_branch/20120906/ippconfig/recipes/filerules-simple.mdc	(revision 34628)
@@ -321,4 +321,6 @@
 PPSKYCELL.BIN1    	     OUTPUT {OUTPUT}.{FILE.INDEX}.b1.fits     IMAGE     NONE       FPA        TRUE      NONE
 PPSKYCELL.BIN2    	     OUTPUT {OUTPUT}.{FILE.INDEX}.b2.fits     IMAGE     NONE       FPA        TRUE      NONE
+PPSKYCELL.BIN1.MASK    	     OUTPUT {OUTPUT}.b1.mk.fits     MASK     COMP_MASK       FPA        TRUE      NONE
+PPSKYCELL.BIN2.MASK    	     OUTPUT {OUTPUT}.b2.mk.fits     MASK     COMP_MASK       FPA       TRUE      NONE
 
 LOG.IMFILE                   OUTPUT {OUTPUT}.imfile.log           TEXT            NONE       FPA        TRUE      NONE
Index: /branches/czw_branch/20120906/ippconfig/recipes/filerules-split.mdc
===================================================================
--- /branches/czw_branch/20120906/ippconfig/recipes/filerules-split.mdc	(revision 34627)
+++ /branches/czw_branch/20120906/ippconfig/recipes/filerules-split.mdc	(revision 34628)
@@ -355,4 +355,6 @@
 PPSKYCELL.BIN1    	     OUTPUT {OUTPUT}.b1.fits     IMAGE     COMP_IMG       FPA        TRUE      NONE
 PPSKYCELL.BIN2    	     OUTPUT {OUTPUT}.b2.fits     IMAGE     COMP_IMG       FPA       TRUE      NONE
+PPSKYCELL.BIN1.MASK    	     OUTPUT {OUTPUT}.b1.mk.fits     MASK     COMP_MASK       FPA        TRUE      NONE
+PPSKYCELL.BIN2.MASK    	     OUTPUT {OUTPUT}.b2.mk.fits     MASK     COMP_MASK       FPA       TRUE      NONE
 
 LOG.IMFILE                   OUTPUT {OUTPUT}.{CHIP.NAME}.log          TEXT            NONE       CHIP       TRUE      NONE
Index: /branches/czw_branch/20120906/ippconfig/recipes/ppBackground.mdc
===================================================================
--- /branches/czw_branch/20120906/ippconfig/recipes/ppBackground.mdc	(revision 34627)
+++ /branches/czw_branch/20120906/ippconfig/recipes/ppBackground.mdc	(revision 34628)
@@ -1,4 +1,18 @@
 # Recipe options for ppBackground
 
+BINNING_RECIPE	STR	PSPHOT
+BINNING_XNAME	STR	BACKGROUND.XBIN
+BINNING_YNAME	STR	BACKGROUND.YBIN
+
+DEFAULT    METADATA
+END
+
+
+STACK	    METADATA
+  BINNING_RECIPE	STR	PSWARP
+  BINNING_XNAME	STR	BKG.XGRID	
+  BINNING_YNAME	STR	BKG.YGRID
+END
+	    
 
 BACKGROUND	 METADATA
Index: /branches/czw_branch/20120906/ppBackground/src/ppBackground.h
===================================================================
--- /branches/czw_branch/20120906/ppBackground/src/ppBackground.h	(revision 34627)
+++ /branches/czw_branch/20120906/ppBackground/src/ppBackground.h	(revision 34628)
@@ -37,4 +37,13 @@
     );
 
+/// Determine the binning from the recipe if available.
+psImageBinning *ppBackgroundBinningByRecipe(const psImage *image, // Image for which to generate a bg model
+					    const pmConfig *config, // Configuration
+					    psString recipe_name,
+					    psString Xbin_name,
+					    psString Ybin_name
+					    );
+
+
 /// Restore the background to an image
 bool ppBackgroundRestore(
Index: /branches/czw_branch/20120906/ppBackground/src/ppBackgroundRestore.c
===================================================================
--- /branches/czw_branch/20120906/ppBackground/src/ppBackgroundRestore.c	(revision 34627)
+++ /branches/czw_branch/20120906/ppBackground/src/ppBackgroundRestore.c	(revision 34628)
@@ -5,4 +5,32 @@
 
 #include "ppBackground.h"
+
+psImageBinning *ppBackgroundBinningByRecipe(const psImage *image, // Image for which to generate a bg model
+					    const pmConfig *config, // Configuration
+					    psString recipe_name,
+					    psString Xbin_name,
+					    psString Ybin_name
+					    )
+{
+  bool status = true;
+
+  psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, recipe_name);
+  assert (recipe);
+
+  // I have the fine image size, I know the binning factor, determine the ruff image size
+  psImageBinning *binning = psImageBinningAlloc();
+  binning->nXfine = image->numCols;
+  binning->nYfine = image->numRows;
+  binning->nXbin  = psMetadataLookupS32(&status, recipe, Xbin_name);
+  binning->nYbin  = psMetadataLookupS32(&status, recipe, Ybin_name);
+
+  psImageBinningSetRuffSize(binning, PS_IMAGE_BINNING_CENTER);
+  psImageBinningSetSkip(binning, image);
+
+  return binning;
+}
+
+  
+
 
 bool ppBackgroundRestore(pmChip *chip, const pmChip *background, const pmChip *pattern,
@@ -27,5 +55,18 @@
     if (background) {
         pmReadout *bgRO = pmFPAviewThisReadout(view, background->parent); // Readout with background
-        psImageBinning *binning = psphotBackgroundBinning(image, config);
+
+	psImageBinning *binning;
+	psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPBACKGROUND_RECIPE); // Recipe
+	if (!recipe) {
+	  binning = psphotBackgroundBinning(image, config);
+	}
+	else {
+	  binning = ppBackgroundBinningByRecipe(image,config,
+						psMetadataLookupStr(NULL,recipe,"BINNING_RECIPE"),
+						psMetadataLookupStr(NULL,recipe,"BINNING_XNAME"),
+						psMetadataLookupStr(NULL,recipe,"BINNING_YNAME"));
+	}
+						
+	fprintf(stderr,"%d %d %d %d\n",binning->nXfine,binning->nYfine,binning->nXbin,binning->nYbin);
         if (!binning) {
             psError(psErrorCodeLast(), false, "Unable to find background binning");
@@ -49,5 +90,9 @@
         for (int y = 0; y < numRows; y++) {
             for (int x = 0; x < numCols; x++) {
+	      if ((y % 250 == 0)&&(x % 250 == 0)) {
+		printf("%d %d %g\n",x,y,bgImage->data.F32[y][x]);
+	      }
                 image->data.F32[y][x] += bgImage->data.F32[y][x];
+		
             }
         }
