Index: /trunk/psphot/src/psphot.h
===================================================================
--- /trunk/psphot/src/psphot.h	(revision 6752)
+++ /trunk/psphot/src/psphot.h	(revision 6753)
@@ -4,36 +4,4 @@
 # include <pslib.h>
 # include <psmodules.h>
-
-# if (0)
-# include <psLine.h>
-# include <psSparse.h>
-# include <psEllipse.h>
-# include <psAdditionals.h>
-# include <pmConfig.h>
-# include <pmHDU.h>
-# include <pmHDUUtils.h>
-# include <pmFPA.h>
-# include <pmFPAview.h>
-# include <pmFPAfile.h>
-# include <pmFPARead.h>
-# include <pmFPAConstruct.h>
-# include <pmMaskBadPixels.h>
-# include <pmConcepts.h>
-
-# include <pmPeaks.h>
-# include <pmMoments.h>
-# include <pmModel.h>
-# include <pmSource.h>
-# include <pmSourceIO.h>
-# include <pmSourceSky.h>
-# include <pmSourceFitModel.h>
-# include <pmSourceFitSet.h>
-# include <pmSourceContour.h>
-# include <pmGrowthCurve.h>
-# include <pmPSF.h>
-# include <pmPSFtry.h>
-# include <pmModelGroup.h>
-# include <pmSourcePhotometry.h>
-# endif
 
 # define PSPHOT_RECIPE "PSPHOT"
Index: /trunk/psphot/src/psphotApResid.c
===================================================================
--- /trunk/psphot/src/psphotApResid.c	(revision 6752)
+++ /trunk/psphot/src/psphotApResid.c	(revision 6753)
@@ -17,5 +17,5 @@
     // measure the aperture loss as a function of radius for PSF
     float REF_RADIUS = psMetadataLookupF32 (&status, config, "PSF_REF_RADIUS");
-    psf->growth = pmGrowthCurveAlloc (3.0, REF_RADIUS, 0.1);
+    psf->growth = pmGrowthCurveAlloc (3.0, 50.0, REF_RADIUS);
     psphotGrowthCurve (readout, psf);
     
@@ -43,16 +43,17 @@
 	if (source->moments->SN < 2*FIT_SN_LIM) continue;
 
-	// get uncorrected magnitudes in scaled apertures
-	model = pmSourceMagnitudes (source, NULL, 0);
+	// get growth-corrected, apTrend-uncorrected magnitudes in scaled apertures
+	model = pmSourceMagnitudes (source, psf, true, false);
 	if (model == NULL) continue;
 
+	apResid->data.F64[Npsf] = source->apMag - source->psfMag;
+
+	xPos->data.F64[Npsf]    = model->params->data.F32[2];
+	yPos->data.F64[Npsf]    = model->params->data.F32[3];
+
+	flux->data.F64[Npsf]    = pow(10.0, -0.4*source->psfMag);
+	r2rflux->data.F64[Npsf] = PS_SQR(model->radiusTMP) / flux->data.F64[Npsf];
+
 	mask->data.U8[Npsf] = 0;
-	xPos->data.F64[Npsf] = model->params->data.F32[2];
-	yPos->data.F64[Npsf] = model->params->data.F32[3];
-
-	flux->data.F64[Npsf] = pow(10.0, -0.4*source->psfMag);
-	r2rflux->data.F64[Npsf] = PS_SQR(model->radius) / flux->data.F64[Npsf];
-	
-	apResid->data.F64[Npsf] = source->apMag + pmGrowthCurveCorrect (psf->growth, model->radius) - source->psfMag;
 
 	// XXX sanity clip?
@@ -83,4 +84,10 @@
     stats->max = 3.0;
 
+    // no correction
+    if (!strcasecmp (ApTrendOption, "NONE")) { 
+	// remove ApTrend fit from pmPSFtry
+	psf->ApTrend->coeff[0][0][0][0] = 0;
+    }
+
     // constant only
     if (!strcasecmp (ApTrendOption, "CONSTANT")) {
@@ -202,5 +209,5 @@
     psVector *resid = (psVector *) psBinaryOp (NULL, (void *) apResid, "-", (void *) fit);
 
-# if (0)
+# if (1)
     FILE *fout = fopen ("resid.dat", "w");
     for (int i = 0; i < resid->n; i++) {
@@ -241,5 +248,5 @@
 
     psLogMsg ("psphot.apresid", 3, "measure full-frame aperture residual: %f sec\n", psTimerMark ("psphot"));
-    psLogMsg ("psphot.apresid", 4, "aperture residual: %f +/- %f : %f bias\n", psf->ApResid, psf->dApResid, psf->skyBias);
+    psLogMsg ("psphot.apresid", 4, "aperture residual: %f +/- %f : %f bias, %f skysat\n", psf->ApResid, psf->dApResid, psf->skyBias, psf->skySat);
     psLogMsg ("psphot.apresid", 4, "apresid trends: %f %f %f %f %f\n", 
 	      1e3*psf->ApTrend->coeff[1][0][0][0],
Index: /trunk/psphot/src/psphotBlendFit.c
===================================================================
--- /trunk/psphot/src/psphotBlendFit.c	(revision 6752)
+++ /trunk/psphot/src/psphotBlendFit.c	(revision 6753)
@@ -31,4 +31,7 @@
 	if (source->type == PM_SOURCE_TYPE_DEFECT) continue; 
 	if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
+
+	// skip DBL second sources (ie, added by psphotFitBlob)
+	if (source->mode &  PM_SOURCE_MODE_PAIR) continue;
 
 	// limit selection to some SN limit
Index: /trunk/psphot/src/psphotEnsemblePSF.c
===================================================================
--- /trunk/psphot/src/psphotEnsemblePSF.c	(revision 6752)
+++ /trunk/psphot/src/psphotEnsemblePSF.c	(revision 6753)
@@ -118,5 +118,5 @@
 	// fill in the model pixel values
 	psImageInit (flux, 0.0);
-	psImageKeepCircle (mask, x, y, model->radius, "OR", PM_SOURCE_MASK_MARKED);
+	psImageKeepCircle (mask, x, y, model->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
 	pmModelAdd (flux, mask, model, false, false);
 
@@ -209,7 +209,7 @@
 	y = model->params->data.F32[3];
 
-	psImageKeepCircle (Fi->mask, x, y, model->radius, "OR", PM_SOURCE_MASK_MARKED);
+	psImageKeepCircle (Fi->mask, x, y, model->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
 	pmSourceChisq (model, Fi->pixels, Fi->mask, Fi->weight);
-	psImageKeepCircle (Fi->mask, x, y, model->radius, "AND", ~PM_SOURCE_MASK_MARKED);
+	psImageKeepCircle (Fi->mask, x, y, model->radiusTMP, "AND", ~PM_SOURCE_MASK_MARKED);
     }
 
Index: /trunk/psphot/src/psphotGrowthCurve.c
===================================================================
--- /trunk/psphot/src/psphotGrowthCurve.c	(revision 6752)
+++ /trunk/psphot/src/psphotGrowthCurve.c	(revision 6753)
@@ -52,5 +52,4 @@
 
 	psImageKeepCircle (mask, xc, yc, radius, "AND", ~PM_SOURCE_MASK_MARKED);
-
 	psf->growth->apMag->data.F32[i] = apMag;
     }
Index: /trunk/psphot/src/psphotImageMedian.c
===================================================================
--- /trunk/psphot/src/psphotImageMedian.c	(revision 6752)
+++ /trunk/psphot/src/psphotImageMedian.c	(revision 6753)
@@ -211,4 +211,6 @@
     psLogMsg ("psphot", 3, "build resampled image: %f sec\n", psTimerMark ("psphot"));
 
+    psphotSaveImage (NULL, background, "back.fits");
+
     // back-sub image pixels, from output background file (don't create if not requested)
     psImage *backSub = pmFPAfileReadoutImage (config->files, view, "PSPHOT.BACKSUB", 0, 0, PS_TYPE_F32);
Index: /trunk/psphot/src/psphotMagnitudes.c
===================================================================
--- /trunk/psphot/src/psphotMagnitudes.c	(revision 6752)
+++ /trunk/psphot/src/psphotMagnitudes.c	(revision 6753)
@@ -3,12 +3,9 @@
 bool psphotMagnitudes (psArray *sources, psMetadata *config, pmPSF *psf) {
 
-    bool status;
-
     psTimerStart ("psphot");
 
-    float RADIUS = psMetadataLookupF32 (&status, config, "AP_RADIUS");
     for (int i = 0; i < sources->n; i++) {
 	pmSource *source = (pmSource *) sources->data[i];
-	pmSourceMagnitudes (source, psf, RADIUS);
+	pmSourceMagnitudes (source, psf, true, true);
     }	
 
@@ -16,4 +13,2 @@
     return true;
 }
-
-// XXX should this function use RADIUS or should it 
Index: /trunk/psphot/src/psphotRadiusChecks.c
===================================================================
--- /trunk/psphot/src/psphotRadiusChecks.c	(revision 6752)
+++ /trunk/psphot/src/psphotRadiusChecks.c	(revision 6753)
@@ -1,3 +1,4 @@
 # include "psphot.h"
+# define RADIUS_TYPE float
 
 static float PSF_FIT_NSIGMA;
@@ -23,12 +24,12 @@
 
     // set the fit radius based on the object flux limit and the model
-    model->radius = modelRadiusPSF (model->params, PSF_FIT_NSIGMA*moments->dSky) + PSF_FIT_PADDING;
-    if (isnan(model->radius)) psAbort ("apply_psf_model", "error in radius");
+    model->radiusTMP = (RADIUS_TYPE) (modelRadiusPSF (model->params, PSF_FIT_NSIGMA*moments->dSky) + PSF_FIT_PADDING);
+    if (isnan(model->radiusTMP)) psAbort ("apply_psf_model", "error in radius");
 	
-    if (source->mode &  PM_SOURCE_MODE_SATSTAR) {
-	model->radius *= 2;
+    if (source->mode & PM_SOURCE_MODE_SATSTAR) {
+	model->radiusTMP *= 2;
     }
 
-    bool status = pmSourceRedefinePixels (source, readout, model->params->data.F32[2], model->params->data.F32[3], model->radius);
+    bool status = pmSourceRedefinePixels (source, readout, model->params->data.F32[2], model->params->data.F32[3], model->radiusTMP);
     return status;
 }
@@ -40,12 +41,12 @@
 
     // set the fit radius based on the object flux limit and the model
-    model->radius = modelRadiusPSF (model->params, PSF_FIT_NSIGMA*moments->dSky) + dR + PSF_FIT_PADDING;
-    if (isnan(model->radius)) psAbort ("apply_psf_model", "error in radius");
+    model->radiusTMP = (RADIUS_TYPE) (modelRadiusPSF (model->params, PSF_FIT_NSIGMA*moments->dSky) + dR + PSF_FIT_PADDING);
+    if (isnan(model->radiusTMP)) psAbort ("apply_psf_model", "error in radius");
 	
     if (source->mode &  PM_SOURCE_MODE_SATSTAR) {
-	model->radius *= 2;
+	model->radiusTMP *= 2;
     }
 
-    bool status = pmSourceRedefinePixels (source, readout, model->params->data.F32[2], model->params->data.F32[3], model->radius);
+    bool status = pmSourceRedefinePixels (source, readout, model->params->data.F32[2], model->params->data.F32[3], model->radiusTMP);
     return status;
 }
@@ -73,9 +74,9 @@
 
     // set the fit radius based on the object flux limit and the model
-    model->radius = modelRadiusEXT (model->params, EXT_FIT_NSIGMA*moments->dSky) + EXT_FIT_PADDING;
-    if (isnan(model->radius)) psAbort (__func__, "error in radius");
+    model->radiusTMP = (RADIUS_TYPE) (modelRadiusEXT (model->params, EXT_FIT_NSIGMA*moments->dSky) + EXT_FIT_PADDING);
+    if (isnan(model->radiusTMP)) psAbort (__func__, "error in radius");
 
     // redefine the pixels if needed
-    bool status = pmSourceRedefinePixels (source, readout, model->params->data.F32[2], model->params->data.F32[3], model->radius);
+    bool status = pmSourceRedefinePixels (source, readout, model->params->data.F32[2], model->params->data.F32[3], model->radiusTMP);
     return status;
 }
Index: /trunk/psphot/src/psphotReadout.c
===================================================================
--- /trunk/psphot/src/psphotReadout.c	(revision 6752)
+++ /trunk/psphot/src/psphotReadout.c	(revision 6753)
@@ -56,5 +56,5 @@
 
     // linear PSF fit to remaining peaks
-    psphotEnsemblePSF (readout, sources, recipe, psf, TRUE);
+    // psphotEnsemblePSF (readout, sources, recipe, psf, TRUE);
 
     // measure aperture photometry corrections
@@ -83,12 +83,12 @@
 }
 
-    # if (1)
-    fprintf (stderr, "making fake sources\n");
-    sources = psphotFakeSources ();
-    psMetadata *tmp = psMetadataAlloc ();
-    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,    "psphot sources", sources);
-    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.HEADER",  PS_DATA_METADATA, "header stats", tmp);
-    psFree (sources);
-    psFree (tmp);
-    return true;
-    # endif
+# if (0)
+fprintf (stderr, "making fake sources\n");
+sources = psphotFakeSources ();
+psMetadata *tmp = psMetadataAlloc ();
+status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,    "psphot sources", sources);
+status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.HEADER",  PS_DATA_METADATA, "header stats", tmp);
+psFree (sources);
+psFree (tmp);
+return true;
+# endif
Index: /trunk/psphot/src/psphotSourceFits.c
===================================================================
--- /trunk/psphot/src/psphotSourceFits.c	(revision 6752)
+++ /trunk/psphot/src/psphotSourceFits.c	(revision 6753)
@@ -17,7 +17,7 @@
 
     // fit PSF model (set/unset the pixel mask)
-    psImageKeepCircle (source->mask, x, y, PSF->radius, "OR", PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
     pmSourceFitModel (source, PSF, true);
-    psImageKeepCircle (source->mask, x, y, PSF->radius, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "AND", ~PM_SOURCE_MASK_MARKED);
 
     // does the PSF model succeed?
@@ -189,7 +189,7 @@
 
     // fit EXT (not PSF) model (set/unset the pixel mask)
-    psImageKeepCircle (source->mask, x, y, PSF->radius, "OR", PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
     pmSourceFitSet (source, modelSet, true);
-    psImageKeepCircle (source->mask, x, y, PSF->radius, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "AND", ~PM_SOURCE_MASK_MARKED);
 
     return (modelSet);
@@ -209,7 +209,7 @@
 
     // fit EXT (not PSF) model (set/unset the pixel mask)
-    psImageKeepCircle (source->mask, x, y, EXT->radius, "OR", PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, EXT->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
     pmSourceFitModel (source, EXT, false);
-    psImageKeepCircle (source->mask, x, y, EXT->radius, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, EXT->radiusTMP, "AND", ~PM_SOURCE_MASK_MARKED);
 
     return (EXT);
@@ -272,7 +272,7 @@
 
     // fit PSF model (set/unset the pixel mask)
-    psImageKeepCircle (source->mask, x, y, PSF->radius, "OR", PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
     pmSourceFitSet (source, modelSet, true);
-    psImageKeepCircle (source->mask, x, y, PSF->radius, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "AND", ~PM_SOURCE_MASK_MARKED);
 
     // evaluate the blend objects, subtract if good, free otherwise
