Index: branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreArguments.c
===================================================================
--- branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreArguments.c	(revision 26870)
+++ branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreArguments.c	(revision 26871)
@@ -8,4 +8,5 @@
 pmConfig *ppbgrestoreArguments(int argc, char *argv[])
 {
+    int argnum;
     if (argc == 1) {
         usage();
@@ -32,4 +33,10 @@
         usage();
     }
+
+    if ((argnum = psArgumentGet(argc, argv, "-savebg"))) {
+        psArgumentRemove(argnum, &argc, argv);
+        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "SAVE_BACKGROUND", 0, "save background innmage", true);
+    }
+
     if (argc != 2) usage ();
 
Index: branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreCleanup.c
===================================================================
--- branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreCleanup.c	(revision 26870)
+++ branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreCleanup.c	(revision 26871)
@@ -22,5 +22,5 @@
     // fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "ppImage");
 
-    fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "ppbgrestore");
+//    fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "ppbgrestore");
 
 }
Index: branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreLoop.c
===================================================================
--- branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreLoop.c	(revision 26870)
+++ branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreLoop.c	(revision 26871)
@@ -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,20 @@
             return false;
         }
-        pmReadout *background = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", image->numCols, image->numRows, PS_TYPE_F32);
+        pmReadout *backgroundRO;
+#define USE_CELL
+#ifdef USE_CELL
+        pmCell *backgroundCell = pmFPAviewThisCell(&roView, background->fpa);
+        backgroundRO = pmReadoutAlloc(backgroundCell);
+#else
+        backgroundRO = pmFPAviewThisReadout(&roView, background->fpa);
+//        backgroundRO = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", image->numCols, image->numRows, PS_TYPE_F32);
+#endif
+        backgroundRO->image = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
+	backgroundRO->data_exists = true;
+	backgroundRO->parent->data_exists = true;
+	backgroundRO->parent->parent->data_exists = true;
 
         // 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,9 +125,10 @@
         }
         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;
+        long naninputs = 0;
         long finitecount = 0;
         for (int y = 0; y < numRows; y++) {
@@ -130,12 +151,17 @@
                 } else if (isfinite(imageval)) {
                     // XXX: TODO:following ppImage we should not modify pixels that are masked.
-                    image->data.F32[y][x] += bgvalue;
-                }
+                    image->data.F32[y][x] = imageval + bgvalue;
+                } else {
+		    naninputs++;
+		}
             }
+        }
+        if (naninputs) {
+            printf("%ld infinite pixels found in input image\n", naninputs);
         }
         if (nancount) {
             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
Index: branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreParseCamera.c
===================================================================
--- branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreParseCamera.c	(revision 26870)
+++ branches/eam_branches/20091201/ppbgrestore/src/ppbgrestoreParseCamera.c	(revision 26871)
@@ -23,5 +23,12 @@
     pmFPAfile *outImage = pmFPAfileDefineOutput(config, input->fpa, "PPBGRESTORE.OUTPUT");
     if (!outImage) {
-        psError(PS_ERR_IO, false, _("Unable to generate output file from PPIMAGE.OUTPUT"));
+        psError(PS_ERR_IO, false, _("Unable to generate output file from PPBGRESTORE.OUTPUT"));
+        psFree(options);
+        return NULL;
+    }
+    // pmFPAfile *outBG = pmFPAfileDefineOutput(config, NULL, "PPBGRESTORE.BACKGROUND");
+    pmFPAfile *outBG = pmFPAfileDefineFromFPA(config, input->fpa, 1, 1, "PPBGRESTORE.BACKGROUND");
+    if (!outBG) {
+        psError(PS_ERR_IO, false, _("Unable to generate bacgkround file from PPBGRESTORE.BACKGROUND"));
         psFree(options);
         return NULL;
