Index: trunk/psphot/src/psphotStackUpdateReadout.c
===================================================================
--- trunk/psphot/src/psphotStackUpdateReadout.c	(revision 38383)
+++ trunk/psphot/src/psphotStackUpdateReadout.c	(revision 38396)
@@ -10,5 +10,5 @@
 bool psphotStackUpdateReadout (pmConfig *config, const pmFPAview *view) {
 
-    psArray *objects = NULL; // used below after 'pass1finish' label
+    psArray *objects = NULL;
 
     // measure the total elapsed time in psphotReadout.  dtime is the elapsed time used jointly
@@ -79,14 +79,4 @@
         return psphotReadoutCleanup (config, view, STACK_RAW);
     }
-
-#ifdef MEASURE_PSF
-    if (!psphotChoosePSF (config, view, STACK_RAW, true)) {
-        psLogMsg ("psphot", 3, "failure to construct a psf model");
-        return psphotReadoutCleanup (config, view, STACK_RAW);
-    }
-    if (!strcasecmp (breakPt, "PSFMODEL")) {
-        return psphotReadoutCleanup (config, view, STACK_RAW);
-    }
-#endif
 
     if (!psphotDeblendSatstars (config, view, STACK_RAW)) {
@@ -327,5 +317,5 @@
     for (int i = 0; i < sources->n; i++) {
         pmSource *source = sources->data[i];
-        source->imageID = index;     // XXX is this necessary?
+        source->imageID = index;    // XXX: This is usually set in psphotSourceStatsReadout
         pmModel *modelPSF = source->modelPSF;
         // free any previous radial aperture measurements
@@ -339,5 +329,5 @@
         if (!(source->mode & PM_SOURCE_MODE_PSFMODEL)) {
             // The cmf loaders unconditionallly create psf models even if the source did not have one.
-            fprintf(stderr, "source %d %5d should not have a psf model 0x%08X%08X\n", index, source->seq, source->mode2, source->mode);
+            // fprintf(stderr, "source %d %5d should not have a psf model 0x%08X%08X\n", index, source->seq, source->mode2, source->mode);
             psFree(source->modelPSF) ;
         }
@@ -361,39 +351,29 @@
         // for anything except psphotStack -updatemode.
         if (modelPSF && isfinite(source->psfFlux)) {
-            // The cmf reader is incorrectly setting the sky parameter for psf models
-            modelPSF->params->data.F32[PM_PAR_SKY] = 0.0;
+            // The cmf reader was incorrectly setting the sky parameter for psf models
+            modelPSF->params->data.F32[PM_PAR_SKY]  = 0.0;
             modelPSF->dparams->data.F32[PM_PAR_SKY] = 0.0;
+
             // Calculate flux for normalized model
-            float saveI0 = modelPSF->params->data.F32[PM_PAR_I0];
             modelPSF->params->data.F32[PM_PAR_I0] = 1.0;
             float normFlux = modelPSF->class->modelFlux(modelPSF->params);
             float psfFlux = source->psfFlux;
-#define APPLY_APCORR
-#ifdef APPLY_APCORR
+            // back out the ApTrend
             double apTrend = pmTrend2DEval (psf->ApTrend, (float)source->peak->x, (float)source->peak->y);
             double adjustment = pow(10.0, -0.4*apTrend);
-            if (isfinite(adjustment) && adjustment != 0.0) {
-                psfFlux = psfFlux / adjustment;
-            } else {
-                fprintf(stderr, "apTrend adjustment for source %5d invalid: %f\n", source->seq, adjustment);
-            }
-#endif
+            psAssert ( (isfinite(adjustment) && adjustment != 0.0), "Invalid adjustment value: %f", adjustment);
+
+            psfFlux = psfFlux / adjustment;
             float newI0 =  psfFlux / normFlux;
-            if (isfinite (newI0) ) {
-                // psf model looks to be good. 
-                modelPSF->params->data.F32[PM_PAR_I0] = newI0;
-#ifdef USE_SAVED_I0
-                // I am testing with a modified cmf that saves PM_PAR_I0
-                modelPSF->params->data.F32[PM_PAR_I0] = saveI0;
-#endif
-                // fprintf (stderr, "%5d %12f %12f %12f\n", source->seq, saveI0, newI0, saveI0 - newI0);
-                goodModel = true;
-            } else {
-                psAssert(isfinite (modelPSF->params->data.F32[PM_PAR_I0]), "got infinite I0 for psf model");
-            }
-        }
-
-        // It is rather expensive initializing all of the extended models. Lets only initialize
-        // the extended model that will be used if any.
+
+            psAssert(isfinite (newI0) , "got infinite I0 for psf model");
+
+            // new I0 for psf model looks good. 
+            modelPSF->params->data.F32[PM_PAR_I0] = newI0;
+            goodModel = true;
+        }
+
+        // It is rather expensive setting the I0 all of the extended models. Lets only initialize
+        // the extended model if it will be used for subtraction.
 
         bool isPSF = false;
@@ -403,9 +383,9 @@
             goodModel = false;
             // calculate flux from the previously measured model magnitude. It will be nan if the
-            // flux was negative unfortunately.
+            // flux was negative unfortunately so we lose those.
             if (isfinite(model->mag)) {
                 float modelFlux = pow(10., -0.4 * model->mag);
 
-                // XXX: We are assuming here that we only use PCM models. Get from recipe.
+                // XXX: We are assuming here that we only use PCM models. Should get that information from the recipe.
                 pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
                 if (!pcm) {
@@ -421,5 +401,5 @@
                     goodModel = true;
                     // Usually this it is set when doing the fit. Since we are instantiating the model we
-                    // set it explicitly. 
+                    // set it explicitly here. 
                     model->isPCM = true;
                 }
@@ -430,7 +410,8 @@
                 // The original flux was probably negative.
                 // This is somewhat rare. I saw it in about .2% of extended sources in my first test.
-                // Set source type to star to prevent source subtraction from crashing. 
-                // this may cause model psf to be used in places.
-                // XXX However we aren't going to cache it below.
+                // Set source type to star to prevent source subtraction from crashing by trying to use
+                // an incomplete extended model.
+                // XXX: this may cause model psf to be used in places.
+                // XXX However we aren't going to cache it below so...
                 source->type = PM_SOURCE_TYPE_STAR;
                 model = NULL;
@@ -443,4 +424,5 @@
             // Is this a reasonable guess?
             source->moments->nPixels = source->modelPSF->nPix;
+            source->tmpFlags |= PM_SOURCE_TMPF_CANDIDATE_PSFSTAR;
         }
 
@@ -449,11 +431,13 @@
         source->moments->SN = 0;
         if (isfinite(kronFlux) && isfinite(source->moments->KronFluxErr) && isfinite(source->psfMag)) {
-            // This is how we set the SN column in the CFF files
+            // This is how we set the SN column in the CFF files...
             source->moments->SN = kronFlux/source->moments->KronFluxErr;
         } else if (isfinite(source->psfFlux) && isfinite(source->psfFluxErr)) {
+            // kron no good try this. It's just used for sorting
             source->moments->SN = source->psfFlux/source->psfFluxErr;
         }
 
         if (goodModel && model) {
+            // Yipee! cache the model
             if (model->isPCM) {
                 pmPCMCacheModel (source, maskVal, psfSize, fitNsigmaConv);
@@ -465,10 +449,4 @@
 
     psFree(fitOptions);
-
-#ifdef MEASURE_PSF
-    // need to drop the psf model in order for it to be re-measured
-    // Hmm, will this invalidate the results from above where we used the loaded one?
-    psMetadataRemoveKey(readout->analysis, "PSPHOT.PSF");
-#endif
 
     return true;
