Index: trunk/psphot/src/psphotMagnitudes.c
===================================================================
--- trunk/psphot/src/psphotMagnitudes.c	(revision 23259)
+++ trunk/psphot/src/psphotMagnitudes.c	(revision 23287)
@@ -15,5 +15,5 @@
     int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
     if (!status) {
-	nThreads = 0;
+        nThreads = 0;
     }
 
@@ -36,5 +36,5 @@
 
     // the binning details are saved on the analysis metadata
-    psImageBinning *binning = psMetadataLookupPtr(&status, recipe, "PSPHOT.BACKGROUND.BINNING");
+    psImageBinning *binning = psMetadataLookupPtr(&status, backModel->analysis, "PSPHOT.BACKGROUND.BINNING");
 
     bool IGNORE_GROWTH = psMetadataLookupBool (&status, recipe, "IGNORE_GROWTH");
@@ -53,55 +53,55 @@
     for (int i = 0; i < cellGroups->n; i++) {
 
-	psArray *cells = cellGroups->data[i];
-
-	for (int j = 0; j < cells->n; j++) {
-
-	    // allocate a job -- if threads are not defined, this just runs the job
-	    psThreadJob *job = psThreadJobAlloc ("PSPHOT_MAGNITUDES");
-
-	    psArrayAdd(job->args, 1, cells->data[j]); // sources
-	    psArrayAdd(job->args, 1, psf);
-	    psArrayAdd(job->args, 1, binning);
-	    psArrayAdd(job->args, 1, backModel);
-	    psArrayAdd(job->args, 1, backStdev);
-
-	    PS_ARRAY_ADD_SCALAR(job->args, photMode, PS_TYPE_S32);
-	    PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
-	    PS_ARRAY_ADD_SCALAR(job->args, 0,        PS_TYPE_S32); // this is used as a return value for nAp
-
-	    if (!psThreadJobAddPending(job)) {
-		psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
-		psFree (job);
-		return false;
-	    }
-	    psFree(job);
+        psArray *cells = cellGroups->data[i];
+
+        for (int j = 0; j < cells->n; j++) {
+
+            // allocate a job -- if threads are not defined, this just runs the job
+            psThreadJob *job = psThreadJobAlloc ("PSPHOT_MAGNITUDES");
+
+            psArrayAdd(job->args, 1, cells->data[j]); // sources
+            psArrayAdd(job->args, 1, psf);
+            psArrayAdd(job->args, 1, binning);
+            psArrayAdd(job->args, 1, backModel);
+            psArrayAdd(job->args, 1, backStdev);
+
+            PS_ARRAY_ADD_SCALAR(job->args, photMode, PS_TYPE_S32);
+            PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
+            PS_ARRAY_ADD_SCALAR(job->args, 0,        PS_TYPE_S32); // this is used as a return value for nAp
+
+            if (!psThreadJobAddPending(job)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
+                psFree (job);
+                return false;
+            }
+            psFree(job);
 
 # if (0)
-		int nap = 0;
-		if (!psphotMagnitudes_Unthreaded (&nap, cells->data[j], psf, binning, backModel, backStdev, photMode, maskVal)) {
-		    psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
-		    return false;
-		}
-		Nap += nap;
+                int nap = 0;
+                if (!psphotMagnitudes_Unthreaded (&nap, cells->data[j], psf, binning, backModel, backStdev, photMode, maskVal)) {
+                    psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
+                    return false;
+                }
+                Nap += nap;
 # endif
-	}
-
-	// wait for the threads to finish and manage results
-	if (!psThreadPoolWait (false)) {
-	    psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
-	    return false;
-	}
-
-	// we have only supplied one type of job, so we can assume the types here
-	psThreadJob *job = NULL;
-	while ((job = psThreadJobGetDone()) != NULL) {
-	    if (job->args->n < 1) {
-		fprintf (stderr, "error with job\n");
-	    } else {
-		psScalar *scalar = job->args->data[7];
-		Nap += scalar->data.S32;
-	    }
-	    psFree(job);
-	}
+        }
+
+        // wait for the threads to finish and manage results
+        if (!psThreadPoolWait (false)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
+            return false;
+        }
+
+        // we have only supplied one type of job, so we can assume the types here
+        psThreadJob *job = NULL;
+        while ((job = psThreadJobGetDone()) != NULL) {
+            if (job->args->n < 1) {
+                fprintf (stderr, "error with job\n");
+            } else {
+                psScalar *scalar = job->args->data[7];
+                Nap += scalar->data.S32;
+            }
+            psFree(job);
+        }
     }
 
@@ -130,23 +130,23 @@
         if (status && isfinite(source->apMag)) Nap ++;
 
-	if (backModel) {
-	    psAssert (binning, "if backModel is defined, so should binning be");
-	    source->sky = psImageUnbinPixel(source->peak->x, source->peak->y, backModel->image, binning);
-	    if (isnan(source->sky) && false) {
-		psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.sky");
-	    }
-	} else {
-	    source->sky = NAN;
-	}
-
-	if (backStdev) {
-	    psAssert (binning, "if backStdev is defined, so should binning be");
-	    source->skyErr = psImageUnbinPixel(source->peak->x, source->peak->y, backStdev->image, binning);
-	    if (isnan(source->skyErr) && false) {
-		psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.skyErr");
-	    }
-	} else {
-	    source->skyErr = NAN;
-	}
+        if (backModel) {
+            psAssert (binning, "if backModel is defined, so should binning be");
+            source->sky = psImageUnbinPixel(source->peak->x, source->peak->y, backModel->image, binning);
+            if (isnan(source->sky) && false) {
+                psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.sky");
+            }
+        } else {
+            source->sky = NAN;
+        }
+
+        if (backStdev) {
+            psAssert (binning, "if backStdev is defined, so should binning be");
+            source->skyErr = psImageUnbinPixel(source->peak->x, source->peak->y, backStdev->image, binning);
+            if (isnan(source->skyErr) && false) {
+                psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.skyErr");
+            }
+        } else {
+            source->skyErr = NAN;
+        }
     }
 
@@ -169,23 +169,23 @@
         if (status && isfinite(source->apMag)) Nap ++;
 
-	if (backModel) {
-	    psAssert (binning, "if backModel is defined, so should binning be");
-	    source->sky = psImageUnbinPixel(source->peak->x, source->peak->y, backModel->image, binning);
-	    if (isnan(source->sky) && false) {
-		psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.sky");
-	    }
-	} else {
-	    source->sky = NAN;
-	}
-
-	if (backStdev) {
-	    psAssert (binning, "if backStdev is defined, so should binning be");
-	    source->skyErr = psImageUnbinPixel(source->peak->x, source->peak->y, backStdev->image, binning);
-	    if (isnan(source->skyErr) && false) {
-		psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.skyErr");
-	    }
-	} else {
-	    source->skyErr = NAN;
-	}
+        if (backModel) {
+            psAssert (binning, "if backModel is defined, so should binning be");
+            source->sky = psImageUnbinPixel(source->peak->x, source->peak->y, backModel->image, binning);
+            if (isnan(source->sky) && false) {
+                psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.sky");
+            }
+        } else {
+            source->sky = NAN;
+        }
+
+        if (backStdev) {
+            psAssert (binning, "if backStdev is defined, so should binning be");
+            source->skyErr = psImageUnbinPixel(source->peak->x, source->peak->y, backStdev->image, binning);
+            if (isnan(source->skyErr) && false) {
+                psLogMsg ("psphot.magnitudes", PS_LOG_DETAIL, "error setting pmSource.skyErr");
+            }
+        } else {
+            source->skyErr = NAN;
+        }
     }
 
@@ -210,5 +210,5 @@
     int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
     if (!status) {
-	nThreads = 0;
+        nThreads = 0;
     }
     nThreads = 0; // XXX until testing is complete, do not thread this function
@@ -233,37 +233,37 @@
     for (int i = 0; i < cellGroups->n; i++) {
 
-	psArray *cells = cellGroups->data[i];
-
-	for (int j = 0; j < cells->n; j++) {
-
-	    // allocate a job -- if threads are not defined, this just runs the job
-	    psThreadJob *job = psThreadJobAlloc ("PSPHOT_PSF_WEIGHTS");
-
-	    psArrayAdd(job->args, 1, cells->data[j]); // sources
-	    PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
-
-	    if (!psThreadJobAddPending(job)) {
-		psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
-		psFree (job);
-		return false;
-	    }
-	    psFree(job);
-
-	}
-
-	// wait for the threads to finish and manage results
-	if (!psThreadPoolWait (false)) {
-	    psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
-	    return false;
-	}
-
-	// we have only supplied one type of job, so we can assume the types here
-	psThreadJob *job = NULL;
-	while ((job = psThreadJobGetDone()) != NULL) {
-	    if (job->args->n < 1) {
-		fprintf (stderr, "error with job\n");
-	    } 
-	    psFree(job);
-	}
+        psArray *cells = cellGroups->data[i];
+
+        for (int j = 0; j < cells->n; j++) {
+
+            // allocate a job -- if threads are not defined, this just runs the job
+            psThreadJob *job = psThreadJobAlloc ("PSPHOT_PSF_WEIGHTS");
+
+            psArrayAdd(job->args, 1, cells->data[j]); // sources
+            PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
+
+            if (!psThreadJobAddPending(job)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
+                psFree (job);
+                return false;
+            }
+            psFree(job);
+
+        }
+
+        // wait for the threads to finish and manage results
+        if (!psThreadPoolWait (false)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
+            return false;
+        }
+
+        // we have only supplied one type of job, so we can assume the types here
+        psThreadJob *job = NULL;
+        while ((job = psThreadJobGetDone()) != NULL) {
+            if (job->args->n < 1) {
+                fprintf (stderr, "error with job\n");
+            }
+            psFree(job);
+        }
     }
 
@@ -285,21 +285,21 @@
         pmSource *source = (pmSource *) sources->data[i];
 
-	// we must have a valid model
-	pmModel *model = pmSourceGetModel (&isPSF, source);
-	if (model == NULL) {
-	  psTrace ("psphot", 3, "fail mag : no valid model");
-	  source->pixWeight = NAN;
-	  continue;
-	}
+        // we must have a valid model
+        pmModel *model = pmSourceGetModel (&isPSF, source);
+        if (model == NULL) {
+          psTrace ("psphot", 3, "fail mag : no valid model");
+          source->pixWeight = NAN;
+          continue;
+        }
 
         status = pmSourcePixelWeight (&source->pixWeight, model, source->maskObj, maskVal);
-	if (!status) {
-	  psTrace ("psphot", 3, "fail to measure pixel weight");
-	  source->pixWeight = NAN;
-	  continue;
-	}
-
-    }
-
-    return true;
-}
+        if (!status) {
+          psTrace ("psphot", 3, "fail to measure pixel weight");
+          source->pixWeight = NAN;
+          continue;
+        }
+
+    }
+
+    return true;
+}
Index: trunk/psphot/src/psphotModelBackground.c
===================================================================
--- trunk/psphot/src/psphotModelBackground.c	(revision 23259)
+++ trunk/psphot/src/psphotModelBackground.c	(revision 23287)
@@ -31,6 +31,6 @@
 static bool backgroundModel(psImage *model,  // Model image
                             psImage *modelStdev, // Model stdev image
-                            psImage *image, // Image for which to generate a background model
-                            psImage *mask, // Mask for image
+                            psMetadata *analysis, // Analysis metadata for outputs
+                            pmReadout *readout, // Readout for which to generate a background model
                             psImageBinning *binning, // Binning parameters
                             const pmConfig *config // Configuration
@@ -40,4 +40,6 @@
 
     bool status = true;
+
+    psImage *image = readout->image, *mask = readout->mask; // Image and mask for readout
 
     // select the appropriate recipe information
@@ -135,7 +137,6 @@
 
     // we save the binning structure for use in psphotMagnitudes
-    status = psMetadataAddPtr(recipe, PS_LIST_TAIL, "PSPHOT.BACKGROUND.BINNING", PS_DATA_UNKNOWN | PS_META_REPLACE, "Background binning", binning);
-    PS_ASSERT (status, false);
-
+    psMetadataAddPtr(analysis, PS_LIST_TAIL, "PSPHOT.BACKGROUND.BINNING",
+                     PS_DATA_UNKNOWN | PS_META_REPLACE, "Background binning", binning);
 
     psF32 **modelData = model->data.F32;
@@ -338,5 +339,5 @@
     psImage *modelStdev = psImageAlloc(binning->nXruff, binning->nYruff, PS_TYPE_F32); // Standard deviation
 
-    if (!backgroundModel(model, modelStdev, ro->image, ro->mask, binning, config)) {
+    if (!backgroundModel(model, modelStdev, ro->analysis, ro, binning, config)) {
         psFree(model);
         psFree(modelStdev);
@@ -360,13 +361,11 @@
     pmFPAfile *file = psMetadataLookupPtr (&status, config->files, filename);
     pmFPA *inFPA = file->fpa;
-    pmReadout *readout = pmFPAviewThisReadout (view, inFPA);
-    psImage *image = readout->image;
-    psImage *mask  = readout->mask;
-
-    psImageBinning *binning = backgroundBinning(image, config); // Image binning parameters
+    pmReadout *readout = pmFPAviewThisReadout(view, inFPA);
+
+    psImageBinning *binning = backgroundBinning(readout->image, config); // Image binning parameters
     pmReadout *model = pmFPAGenerateReadout(config, view, "PSPHOT.BACKMDL", inFPA, binning);
     pmReadout *modelStdev = pmFPAGenerateReadout(config, view, "PSPHOT.BACKMDL.STDEV", inFPA, binning);
 
-    if (!backgroundModel(model->image, modelStdev->image, image, mask, binning, config)) {
+    if (!backgroundModel(model->image, modelStdev->image, model->analysis, readout, binning, config)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to generate background model");
         return false;
Index: trunk/psphot/src/psphotSubtractBackground.c
===================================================================
--- trunk/psphot/src/psphotSubtractBackground.c	(revision 23259)
+++ trunk/psphot/src/psphotSubtractBackground.c	(revision 23287)
@@ -4,5 +4,5 @@
 // generate the median in NxN boxes, clipping heavily
 // linear interpolation to generate full-scale model
-bool psphotSubtractBackground (pmConfig *config, const pmFPAview *view, const char *filename) 
+bool psphotSubtractBackground (pmConfig *config, const pmFPAview *view, const char *filename)
 {
     bool status = true;
@@ -31,5 +31,5 @@
     assert (maskVal);
 
-    psImageBinning *binning = psMetadataLookupPtr(&status, recipe, "PSPHOT.BACKGROUND.BINNING");
+    psImageBinning *binning = psMetadataLookupPtr(&status, model->analysis, "PSPHOT.BACKGROUND.BINNING");
     assert (binning);
 
