Index: /trunk/psphot/src/psphot.c
===================================================================
--- /trunk/psphot/src/psphot.c	(revision 10184)
+++ /trunk/psphot/src/psphot.c	(revision 10185)
@@ -28,4 +28,7 @@
     }
 
+    // XXX for test checking of the config system
+    // psphotDumpConfig (config);
+
     // call psphot for each readout
     if (!psphotImageLoop (config)) {
Index: /trunk/psphot/src/psphot.h
===================================================================
--- /trunk/psphot/src/psphot.h	(revision 10184)
+++ /trunk/psphot/src/psphot.h	(revision 10185)
@@ -44,5 +44,5 @@
 bool            psphotReplaceUnfit (psArray *sources);
 bool            psphotApResid (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
-bool            psphotMagnitudes (psArray *sources, psMetadata *recipe, pmPSF *psf, const pmConfig *config);
+bool            psphotMagnitudes (psArray *sources, psMetadata *recipe, pmPSF *psf, pmReadout *background);
 bool            psphotSkyReplace (pmConfig *config, pmFPAview *view);
 bool            psphotReadoutCleanup (pmConfig *config, pmReadout *readout, psMetadata *recipe, pmPSF *psf, psArray *sources);
@@ -69,4 +69,6 @@
 bool            psphotWeightBias (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
 int             psphotSaveImage (psMetadata *header, psImage *image, char *filename);
+bool psphotDumpConfig (pmConfig *config);
+pmReadout *psphotSelectBackground (pmConfig *config, pmFPAview *view);
 
 // PSF / DBL / EXT evaluation functions
Index: /trunk/psphot/src/psphotMagnitudes.c
===================================================================
--- /trunk/psphot/src/psphotMagnitudes.c	(revision 10184)
+++ /trunk/psphot/src/psphotMagnitudes.c	(revision 10185)
@@ -4,5 +4,5 @@
 		      psMetadata *recipe,
 		      pmPSF *psf,
-		      const pmConfig *config)
+		      pmReadout *background)
 {
     bool status = false;
@@ -13,25 +13,18 @@
     pmSourceMagnitudesInit (recipe);
 
+    // XXX require (assert) that we have a background model, or 
+    // allow it to be missing, setting local sky to 0.0?
+
     // Get enough information to return sky level
-    assert (config != NULL);
-    const psImage *background = NULL;
-    int DX = 0, DY = 0, dx = 0, dy = 0;	// unpacked with PSPHOT.BACKMDL
-    {
-	const pmFPAfile *backgroundFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL");
-	if (backgroundFile != NULL) {
-	    assert (backgroundFile->readout != NULL);
-	    background = backgroundFile->readout->image;
-
-	    assert(backgroundFile->readout->analysis != NULL);
-	    DX = psMetadataLookupS32(&status, backgroundFile->readout->analysis, "XBIN");
-	    assert (status == true);
-	    DY = psMetadataLookupS32(&status, backgroundFile->readout->analysis, "YBIN");
-	    assert (status == true);
-	    dx = psMetadataLookupS32(&status, backgroundFile->readout->analysis, "x0");
-	    assert (status == true);
-	    dy = psMetadataLookupS32(&status, backgroundFile->readout->analysis, "y0");
-	    assert (status == true);
-	}
-    }
+    assert(background != NULL);
+    assert(background->analysis != NULL);
+    int DX = psMetadataLookupS32(&status, background->analysis, "XBIN");
+    assert (status == true);
+    int DY = psMetadataLookupS32(&status, background->analysis, "YBIN");
+    assert (status == true);
+    int dx = psMetadataLookupS32(&status, background->analysis, "x0");
+    assert (status == true);
+    int dy = psMetadataLookupS32(&status, background->analysis, "y0");
+    assert (status == true);
 
     bool IGNORE_GROWTH = psMetadataLookupBool (&status, recipe, "IGNORE_GROWTH");
@@ -47,16 +40,9 @@
 	if (status) Nap ++;
 
-	source->sky = 0;
-#if 0
-	source->sky += source->moments->Sky;
-#endif
-
-        if (background != NULL) {
-	    source->sky += psImageUnbinPixel(source->peak->x, source->peak->y, background, DX, DY, dx, dy);
-	    if (isnan(source->sky) && false) {
-		psError(PSPHOT_ERR_SKY, false, "Setting pmSource.sky");
-		psErrorStackPrint(NULL, " ");
-		psErrorClear();
-	    }
+	source->sky = psImageUnbinPixel(source->peak->x, source->peak->y, background->image, DX, DY, dx, dy);
+	if (isnan(source->sky) && false) {
+	  psError(PSPHOT_ERR_SKY, false, "Setting pmSource.sky");
+	  psErrorStackPrint(NULL, " ");
+	  psErrorClear();
 	}
     }	
Index: /trunk/psphot/src/psphotOutput.c
===================================================================
--- /trunk/psphot/src/psphotOutput.c	(revision 10184)
+++ /trunk/psphot/src/psphotOutput.c	(revision 10185)
@@ -1,3 +1,28 @@
 # include "psphot.h"
+
+pmReadout *psphotSelectBackground (pmConfig *config, pmFPAview *view) {
+
+    bool status;
+    pmReadout *background;    
+
+    pmFPAfile *file = psMetadataLookupPtr (&status, config->files, "PSPHOT.BACKMDL");
+    if (!file) return NULL;
+    if (file->mode == PM_FPA_MODE_INTERNAL) {
+	background = file->readout;
+    } else {
+	background = pmFPAviewThisReadout (view, file->fpa);
+    }
+    return background;
+}
+
+bool psphotDumpConfig (pmConfig *config) {
+
+  psMetadataConfigWrite (config->site, "site.md");
+  psMetadataConfigWrite (config->camera, "camera.md");
+  psMetadataConfigWrite (config->recipes, "recipes.md");
+  psMetadataConfigWrite (config->arguments, "arguments.md");
+  psMetadataConfigWrite (config->files, "files.md");
+  return true;
+}
 
 int psphotSaveImage (psMetadata *header, psImage *image, char *filename) {
Index: /trunk/psphot/src/psphotReadout.c
===================================================================
--- /trunk/psphot/src/psphotReadout.c	(revision 10184)
+++ /trunk/psphot/src/psphotReadout.c	(revision 10185)
@@ -32,4 +32,6 @@
     // generate a background model (median, smoothed image)
     psphotImageMedian (config, view);
+
+    pmReadout *background = psphotSelectBackground (config, view);
 
     // find the peaks in the image
@@ -111,5 +113,5 @@
 
     // calculate source magnitudes
-    psphotMagnitudes(sources, recipe, psf, config);
+    psphotMagnitudes(sources, recipe, psf, background);
 
     // replace background in residual image
