Index: trunk/ppBackground/src/ppBackgroundRestore.c
===================================================================
--- trunk/ppBackground/src/ppBackgroundModel.c	(revision 28276)
+++ trunk/ppBackground/src/ppBackgroundRestore.c	(revision 28280)
@@ -4,89 +4,68 @@
 #include <psphot.h>
 
+#include "ppBackground.h"
 
-bool ppBackgroundModel(
-        pmReadout *ro = cell->readouts->data[0]; // Readout of interest
-        if (!ro || !ro->data_exists) {
-            psError(PS_ERR_UNKNOWN, true, "readout has no data");
+bool ppBackgroundRestore(pmChip *chip, const pmChip *background, const pmChip *pattern,
+                         const pmFPAview *oldView, pmConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(chip, false);
+    PS_ASSERT_PTR_NON_NULL(oldView, false);
+
+    pmFPAview *view = pmFPAviewAlloc(0); // View to readout
+    *view = *oldView;
+    view->cell = 0;
+    view->readout = 0;
+
+    pmReadout *ro = pmFPAviewThisReadout(view, chip->parent);
+    if (!ro || !ro->data_exists) {
+        psError(PPBACKGROUND_ERR_CONFIG, true, "Readout has no data");
+        return false;
+    }
+    psImage *image = ro->image;                             // Image to correct
+    int numCols = image->numCols, numRows = image->numRows; // Size of image
+
+    if (background) {
+        pmReadout *bgRO = pmFPAviewThisReadout(view, background->parent); // Readout with background
+        psImageBinning *binning = psphotBackgroundBinning(bgRO->image, config);
+        if (!binning) {
+            psError(psErrorCodeLast(), false, "Unable to find background binning");
             return false;
         }
-        psImage *image = ro->image;
-//        psImage *mask = NULL;
-//        psImageMaskType maskVal = 0;
-
-        pmFPAview roView = *view;
-        roView.cell = 0;
-        roView.readout = 0;
-        pmReadout *modelRO = pmFPAviewThisReadout(&roView, backmdl->fpa);
-        if (!modelRO || !modelRO->data_exists) {
-            psError(PS_ERR_UNKNOWN, true, "model readout has no data");
+        if (binning->nXfine != numCols || binning->nYfine != numRows) {
+            psError(PPBACKGROUND_ERR_CONFIG, true,
+                    "Unbinned background model and input don't match (%dx%d vs %dx%d)",
+                    binning->nXfine, binning->nYfine, numCols, numRows);
             return false;
         }
-        output->save = true;
-        background->save = save_background;
 
-        psImageBinning *binning = psphotBackgroundBinning(image, config);
-        if (!binning) {
-            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find background binning");
-            return false;
-        }
-        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 (backgroundRO->image, modelRO->image, binning)) {
-            psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
+        psImage *bgImage = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Background
+        if (!psImageUnbin(bgImage, bgRO->image, binning)) {
+            psError(psErrorCodeLast(), false, "Unable to unbin background model");
             psFree(binning);
             return false;
         }
         psFree(binning);
-        psF32 **backData = backgroundRO->image->data.F32;
 
-        // 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++) {
             for (int x = 0; x < numCols; x++) {
-                float imageval =  image->data.F32[y][x] ;
-                float bgvalue = backData[y][x];
-                if (!isfinite(bgvalue)) {
-                    // if background is NAN leave the image unchanged
-                    // According to ppImageSubtractBackground pixels where the background is not finite should be NAN
-                    // experimentally
-                    // for breakpoint
-                    nancount++;
-                    if (isfinite(imageval)) {
-                         finitecount++;
-                         //   psWarning("unexpected finite image value %f found at %d %d background is infinite", imageval, x, y);
-                    }
-                    //      Doing this is causes a lot of new NAN pixels to appear.
-                    //      NANS are appearing in the model for some reason.
-                    //      image->data.F32[y][x] = NAN;
-                } else if (isfinite(imageval)) {
-                    // XXX: TODO:following ppImage we should not modify pixels that are masked.
-                    image->data.F32[y][x] = imageval + bgvalue;
-                } else {
-                    naninputs++;
-                }
+                image->data.F32[y][x] += bgImage->data.F32[y][x];
             }
         }
-        if (naninputs) {
-            printf("%ld infinite pixels found in input image\n", naninputs);
+        psFree(bgImage);
+    }
+
+    if (pattern) {
+        pmReadout *patternRO = pmFPAviewThisReadout(view, pattern->parent); // Readout with pattern
+        psImage *patternImage = patternRO->image; // Image with pattern
+        for (int y = 0; y < numRows; y++) {
+            for (int x = 0; x < numCols; x++) {
+                image->data.F32[y][x] += patternImage->data.F32[y][x];
+            }
         }
-        if (nancount) {
-            psWarning("%ld infinite pixels found in background %ld pixels were finite\n", nancount, finitecount);
-        }
-//        pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
+    }
+
+    return true;
+}
+
+
+
