Index: trunk/ppbgrestore/src/ppbgrestoreLoop.c
===================================================================
--- trunk/ppbgrestore/src/ppbgrestoreLoop.c	(revision 25974)
+++ trunk/ppbgrestore/src/ppbgrestoreLoop.c	(revision 26662)
@@ -30,4 +30,11 @@
         exit(PS_EXIT_PROG_ERROR);
     }
+    pmFPAfile *background= psMetadataLookupPtr(&status, config->files, "PPBGRESTORE.BACKGROUND");
+    if (!status) {
+        psErrorStackPrint(stderr, "Can't find output data!\n");
+        ppbgrestoreCleanup(config, options);
+        exit(PS_EXIT_PROG_ERROR);
+    }
+    bool save_background = psMetadataLookupBool(&status, config->arguments, "SAVE_BACKGROUND");
 
     pmFPAview *view = pmFPAviewAlloc(0);// View for level of interest
@@ -90,4 +97,5 @@
         }
         output->save = true;
+        background->save = save_background;
 
         psImageBinning *binning = psphotBackgroundBinning(image, config);
@@ -96,8 +104,10 @@
             return false;
         }
-        pmReadout *background = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", image->numCols, image->numRows, PS_TYPE_F32);
+        pmReadout *backgroundRO = pmFPAviewThisReadout(&roView, background->fpa);
+//        pmReadout *backgroundRO = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", image->numCols, image->numRows, PS_TYPE_F32);
+        backgroundRO->image = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
 
         // linear interpolation to full-scale
-        if (!psImageUnbin (background->image, modelRO->image, binning)) {
+        if (!psImageUnbin (backgroundRO->image, modelRO->image, binning)) {
             psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
             psFree(binning);
@@ -105,7 +115,7 @@
         }
         psFree(binning);
-        psF32 **backData = background->image->data.F32;
+        psF32 **backData = backgroundRO->image->data.F32;
 
-        // Do the background subtraction
+        // Undo the background subtraction
         int numCols = image->numCols, numRows = image->numRows; // Size of image
         long nancount = 0;
@@ -137,5 +147,5 @@
             psWarning("%ld infinite pixels found in background %ld pixels were finite\n", nancount, finitecount);
         }
-        pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
+//        pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
 
         // Close chip
