Index: /trunk/psphot/src/psphotImageMedian.c
===================================================================
--- /trunk/psphot/src/psphotImageMedian.c	(revision 12435)
+++ /trunk/psphot/src/psphotImageMedian.c	(revision 12436)
@@ -62,4 +62,16 @@
         stats->options |= PS_STAT_USE_BINSIZE;
     }
+
+    psMetadataConfigPrint (stderr, recipe);
+
+    // optionally set the sigma clipping
+    stats->clipSigma = psMetadataLookupF32 (&status, recipe, "SKY_CLIP_SIGMA");
+    if (!status) {
+        stats->clipSigma = 1.0;
+    }
+
+    // stats is not initialized by psStats???  use this to save the input options
+    psStats *statsDefaults = psStatsAlloc (statsOption);
+    *statsDefaults = *stats;
 
     // find the currently selected readout
@@ -142,4 +154,9 @@
             int sx = ix*DX - xOffset;
             int sy = iy*DY - yOffset;
+
+	    // XXX test override
+	    // sx = 1955;
+	    // sy = 4;
+
             region = psRegionSet (sx, sx + 2*DX, sy, sy + 2*DY);
             region = psRegionForImage (image, region);
@@ -152,8 +169,12 @@
             psImage *submask = psImageSubset (mask, region);
 
+	    // reset the default values
+	    *stats = *statsDefaults;
+
             // Use the selected background statistic for the first pass
 	    // If it fails, fall back on the "ROBUST_MEDIAN" version
 	    // If both fail, set the pixel to NAN and (below) interpolate
 	    // XXX psImageBackground will probably be renamed psImageStats
+	    // XXX don't bother trying if there are no valid pixels...
             if (psImageBackground(stats, subset, submask, 0xff, rng)) {
 		if (stats->options & PS_STAT_ROBUST_QUARTILE) {
@@ -164,5 +185,5 @@
 	    } else {
 		psStatsOptions currentOptions = stats->options;
-		stats->options |= PS_STAT_ROBUST_MEDIAN;
+		stats->options = PS_STAT_ROBUST_MEDIAN;
 		if (!psImageBackground(stats, subset, submask, 0xff, rng)) {
 		    psLogMsg ("psphot", PS_LOG_WARN, "Failed to estimate background using ROBUST_MEDIAN for "
@@ -299,8 +320,10 @@
 # ifdef TESTSAVE
     psphotSaveImage (NULL, image, "backsub.fits");
+    exit (1);
 # endif
 
     psLogMsg ("psphot", PS_LOG_INFO, "subtracted background model: %f sec\n", psTimerMark ("psphot"));
     psFree(stats);
+    psFree(statsDefaults);
     psFree(rng);
 
