Index: trunk/psphot/src/psphotImageMedian.c
===================================================================
--- trunk/psphot/src/psphotImageMedian.c	(revision 6851)
+++ trunk/psphot/src/psphotImageMedian.c	(revision 6862)
@@ -33,8 +33,6 @@
 
     // scaling factor
-    int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND_XBIN");
-    if (!status) {DX = 64;}
-    int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND_YBIN");
-    if (!status) {DY = 64;}
+    int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
+    int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
 
     // overhang : we will balance this evenly
@@ -69,6 +67,8 @@
 
 	    // XXX the value of the upper and lower cuts probably should be studied...
-	    modelData[iy][ix] = psImageClippedStats (subset, submask, 0xff, 0.25, 0.75) + SKY_BIAS;
+	    psStats *stats = psImageClippedStats (subset, submask, 0xff, 0.25, 0.75);
+	    modelData[iy][ix] = stats->robustMedian + SKY_BIAS;
 
+	    psFree (stats);
 	    psFree (subset);
 	    psFree (submask);
@@ -82,6 +82,14 @@
     psLogMsg ("psphot", 3, "build median image: %f sec\n", psTimerMark ("psphot"));
 
-    // XXX temporarily until we get the pmFPAfile output finished
-    psphotSaveImage (NULL, model->image, "model.fits");
+    // measure background stats and save for later output
+    psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV | PS_STAT_MIN | PS_STAT_MAX);
+    stats = psImageStats (stats, model->image, NULL, 0);
+    psMetadataAdd (recipe, PS_LIST_TAIL, "SKY_MEAN", PS_DATA_F32 | PS_META_REPLACE, "sky model mean",          stats->sampleMean);
+    psMetadataAdd (recipe, PS_LIST_TAIL, "SKY_STDV", PS_DATA_F32 | PS_META_REPLACE, "sky model stdev",         stats->sampleStdev);
+    psMetadataAdd (recipe, PS_LIST_TAIL, "SKY_MAX",  PS_DATA_F32 | PS_META_REPLACE, "sky model maximum value", stats->max);
+    psMetadataAdd (recipe, PS_LIST_TAIL, "SKY_MIN",  PS_DATA_F32 | PS_META_REPLACE, "sky model minimum value", stats->min);
+    psMetadataAdd (recipe, PS_LIST_TAIL, "SKY_NX",   PS_DATA_S32 | PS_META_REPLACE, "sky model size (x)",      nx);
+    psMetadataAdd (recipe, PS_LIST_TAIL, "SKY_NY",   PS_DATA_S32 | PS_META_REPLACE, "sky model size (y)",      ny);
+    psFree (stats);
 
     // select background pixels, from output background file, or create
@@ -89,10 +97,5 @@
     if (background == NULL) {
 	background = pmFPAfileCreateInternal (config->files, "PSPHOT.BACKGND", Nx, Ny, PS_TYPE_F32);
-    } else {
-	// replace the supplied image data with an image of the desired size
-	// XXX should not have to do this: it should be allocated correctly
-	// psImageRecycle (background->image, Nx, Ny, PS_TYPE_F32);
-    }
-
+    } 
     psF32 **backData = background->image->data.F32;
 
@@ -100,7 +103,4 @@
     psImageUnbin (background->image, model->image, DX, DY, dx, dy);
     psLogMsg ("psphot", 3, "build resampled image: %f sec\n", psTimerMark ("psphot"));
-
-    // XXX temporarily until we get the pmFPAfile output finished...
-    psphotSaveImage (NULL, background->image, "back.fits");
 
     // back-sub image pixels, from output background file (don't create if not requested)
