Index: /trunk/psphot/src/psphotImageMedian.c
===================================================================
--- /trunk/psphot/src/psphotImageMedian.c	(revision 14941)
+++ /trunk/psphot/src/psphotImageMedian.c	(revision 14942)
@@ -80,5 +80,6 @@
                                  PS_STAT_FITTED_MEAN |
                                  PS_STAT_FITTED_MEAN_V2 |
-                                 PS_STAT_FITTED_MEAN_V3))) {
+                                 PS_STAT_FITTED_MEAN_V3 |
+                                 PS_STAT_FITTED_MEAN_V4))) {
         statsOptionLocation = PS_STAT_FITTED_MEAN;
     }
@@ -101,4 +102,6 @@
     } 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);
@@ -196,5 +199,7 @@
             // XXX don't bother trying if there are no valid pixels...
 
-            if (psImageBackground(stats, subset, submask, maskVal, rng)) {
+	    psVector *sample = NULL;
+
+            if (psImageBackground(stats, &sample, subset, submask, maskVal, rng)) {
                 if (stats->options & PS_STAT_ROBUST_QUARTILE) {
                     modelData[iy][ix] = stats->robustMedian;
@@ -203,8 +208,12 @@
                 }
                 modelStdevData[iy][ix] = psStatsGetValue(stats, statsOptionWidth);
+
+		// supply sample to plotting routing
+		psphotDiagnosticPlots (config, "IMAGE.BACKGROUND.CELL.HISTOGRAM", ix, iy, modelData[iy][ix], modelStdevData[iy][ix], sample);
+		psFree (sample);
             } else {
                 psStatsOptions currentOptions = stats->options;
                 stats->options = PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV;
-                if (!psImageBackground(stats, subset, submask, maskVal, rng)) {
+                if (!psImageBackground(stats, &sample, subset, submask, maskVal, rng)) {
                     psLogMsg ("psphot", PS_LOG_WARN, "Failed to estimate background using ROBUST_MEDIAN for "
                                "(%dx%d, (row0,col0) = (%d,%d)",
@@ -214,4 +223,8 @@
                     modelData[iy][ix] = psStatsGetValue (stats, PS_STAT_ROBUST_MEDIAN);
                     modelStdevData[iy][ix] = psStatsGetValue(stats, PS_STAT_ROBUST_STDEV);
+
+		    // supply sample to plotting routing
+		    psphotDiagnosticPlots (config, "IMAGE.BACKGROUND.CELL.HISTOGRAM", ix, iy, modelData[iy][ix], modelStdevData[iy][ix], sample);
+		    psFree (sample);
                 }
                 // drop errors caused by psImageBackground failures
@@ -224,4 +237,10 @@
             psFree (submask);
         }
+    }
+
+    if (psTraceGetLevel("psphot") > 5) {
+        char name[256];
+        sprintf (name, "backraw.%02d.fits", npass);
+        psphotSaveImage (NULL, model->image, name);
     }
 
