Index: /branches/eam_branches/ipp-20110213/psphot/src/psphotModelBackground.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psphot/src/psphotModelBackground.c	(revision 30867)
+++ /branches/eam_branches/ipp-20110213/psphot/src/psphotModelBackground.c	(revision 30868)
@@ -67,4 +67,9 @@
     psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
 
+    float dXsample = psMetadataLookupF32(&status, recipe, "BACKGROUND.XSAMPLE");
+    if (!status) dXsample = 1.0;
+    float dYsample = psMetadataLookupF32(&status, recipe, "BACKGROUND.YSAMPLE");
+    if (!status) dYsample = 1.0;
+
     // subtract this amount extra from the sky
     float SKY_BIAS = psMetadataLookupF32 (&status, recipe, "SKY_BIAS");
@@ -84,8 +89,5 @@
                                  PS_STAT_ROBUST_QUARTILE |
                                  PS_STAT_CLIPPED_MEAN |
-                                 PS_STAT_FITTED_MEAN |
-                                 PS_STAT_FITTED_MEAN_V2 |
-                                 PS_STAT_FITTED_MEAN_V3 |
-                                 PS_STAT_FITTED_MEAN_V4))) {
+                                 PS_STAT_FITTED_MEAN))) {
         statsOptionLocation = PS_STAT_FITTED_MEAN;
     }
@@ -95,19 +97,9 @@
         statsOptionWidth = PS_STAT_SAMPLE_STDEV;
     } else if (statsOptionLocation & (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_QUARTILE)) {
-#if 1
-        statsOptionWidth = PS_STAT_ROBUST_STDEV; // not set; => NaN
-#else
-        statsOptionWidth = PS_STAT_FITTED_STDEV;
-#endif
+        statsOptionWidth = PS_STAT_ROBUST_STDEV;
     } else if (statsOptionLocation & PS_STAT_FITTED_MEAN) {
         statsOptionWidth = PS_STAT_FITTED_STDEV;
     } else if (statsOptionLocation & PS_STAT_CLIPPED_MEAN) {
         statsOptionWidth = PS_STAT_CLIPPED_STDEV;
-    } else if (statsOptionLocation & PS_STAT_FITTED_MEAN_V2) {
-        statsOptionWidth = PS_STAT_FITTED_STDEV_V2;
-    } else if (statsOptionLocation & PS_STAT_FITTED_MEAN_V3) {
-        statsOptionWidth = PS_STAT_FITTED_STDEV_V3;
-    } else if (statsOptionLocation & PS_STAT_FITTED_MEAN_V4) {
-        statsOptionWidth = PS_STAT_FITTED_STDEV_V4;
     } else {
         psAbort("Unable to estimate variance of selected statsOptionLocations 0x%x", statsOptionLocation);
@@ -138,5 +130,5 @@
     stats->clipSigma = psMetadataLookupF32 (&status, recipe, "SKY_CLIP_SIGMA");
     if (!status) {
-        if ((stats->options & PS_STAT_FITTED_MEAN) || (stats->options & PS_STAT_FITTED_MEAN_V2)) {
+        if (stats->options & PS_STAT_FITTED_MEAN) {
             stats->clipSigma = 1.0;
         } else {
@@ -160,4 +152,8 @@
     int nFailures = 0;
     psImageBackgroundInit();
+
+    // we have Nx * Ny model points, but we can use a window which is larger (or smaller) than
+    // 1 superpixel.  If we have a window of size dXsample * dYsample, then the regions run from:
+    // (ix + 0.5) - dX to (ix + 0.5) + dX
 
     // measure clipped median for subimages
@@ -168,6 +164,6 @@
 
             // convert the ruff grid cell to the equivalent fine grid cell
-            // XXX we need to watch out for row0,col0
-            ruffRegion = psRegionSet (ix, ix + 1, iy, iy + 1);
+            ruffRegion = psRegionSet (ix + 0.5 - 0.5*dXsample, ix + 0.5 + 0.5*dXsample, 
+				      iy + 0.5 - 0.5*dYsample, iy + 0.5 + 0.5*dYsample);
             fineRegion = psImageBinningSetFineRegion (binning, ruffRegion);
             fineRegion = psRegionForImage (image, fineRegion);
@@ -205,8 +201,8 @@
 
             if (gotX && gotY) {
-                (void) psTraceSetLevel ("psLib.math.vectorFittedStats_v4", 6);
+                (void) psTraceSetLevel ("psLib.math.vectorFittedStats", 6);
                 (void) psTraceSetLevel ("psLib.math.vectorRobustStats", 6);
             } else {
-                (void) psTraceSetLevel ("psLib.math.vectorFittedStats_v4", 0);
+                (void) psTraceSetLevel ("psLib.math.vectorFittedStats", 0);
                 (void) psTraceSetLevel ("psLib.math.vectorRobustStats", 0);
             }
@@ -270,5 +266,5 @@
     for (int iy = 0; iy < model->numRows; iy++) {
         for (int ix = 0; ix < model->numCols; ix++) {
-            if (!isnan(modelData[iy][ix])) {
+            if (isfinite(modelData[iy][ix])) {
                 Value += modelData[iy][ix];
                 ValueStdev += modelStdevData[iy][ix];
@@ -286,9 +282,13 @@
                     if (jx   <   0) continue;
                     if (jx   >= model->numCols) continue;
+		    if (!isfinite(modelData[jy][jx])) continue;
                     value += modelData[jy][jx];
                     count += 1.0;
                 }
             }
-            if (count > 0) modelData[iy][ix] = value / count;
+            if (count > 0) {
+		psLogMsg ("psphot", PS_LOG_DETAIL, "patching background %d, %d: %f (%d pts)\n", ix, iy, (value / count), (int) count);
+		modelData[iy][ix] = value / count;
+	    }
         }
     }
