Index: branches/eam_branches/20091201/psphot/src/psphot.h
===================================================================
--- branches/eam_branches/20091201/psphot/src/psphot.h	(revision 26793)
+++ branches/eam_branches/20091201/psphot/src/psphot.h	(revision 26808)
@@ -48,5 +48,5 @@
 // XXX test functions
 psArray        *psphotFakeSources (void);
-bool 	        psphotMaskCosmicRayFootprintCheck (psArray *sources);
+bool            psphotMaskCosmicRayFootprintCheck (psArray *sources);
 
 // psphotReadout functions
@@ -63,79 +63,79 @@
 bool            psphotSubtractBackgroundReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
 
-bool		psphotFindDetections (pmConfig *config, const pmFPAview *view, bool firstPass);
-bool 		psphotFindDetectionsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool firstPass);
-
-bool		psphotSourceStats (pmConfig *config, const pmFPAview *view, bool setWindow);
-bool 		psphotSourceStatsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool setWindow);
-
-bool 		psphotDeblendSatstars (pmConfig *config, const pmFPAview *view);
-bool 		psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool 		psphotBasicDeblend (pmConfig *config, const pmFPAview *view);
-bool 		psphotBasicDeblendReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool 		psphotRoughClass (pmConfig *config, const pmFPAview *view);
-bool 		psphotRoughClassReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-bool 		psphotRoughClassRegion (int nRegion, psRegion *region, psArray *sources, psMetadata *analysis, psMetadata *recipe, const bool havePSF);
-
-bool 		psphotImageQuality (pmConfig *config, const pmFPAview *view);
-bool 		psphotImageQualityReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool 		psphotChoosePSF (pmConfig *config, const pmFPAview *view);
-bool 		psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool 		psphotGuessModels (pmConfig *config, const pmFPAview *view);
-bool 		psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool 		psphotMergeSources (pmConfig *config, const pmFPAview *view);
-bool 		psphotMergeSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool 		psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, bool final);
-bool 		psphotFitSourcesLinearReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, bool final);
-
-bool 		psphotSourceSize (pmConfig *config, const pmFPAview *view, bool getPSFsize);
-bool 		psphotSourceSizeReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool getPSFsize);
-
-bool 		psphotBlendFit (pmConfig *config, const pmFPAview *view);
-bool 		psphotBlendFitReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotFindDetections (pmConfig *config, const pmFPAview *view, bool firstPass);
+bool            psphotFindDetectionsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool firstPass);
+
+bool            psphotSourceStats (pmConfig *config, const pmFPAview *view, bool setWindow);
+bool            psphotSourceStatsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool setWindow);
+
+bool            psphotDeblendSatstars (pmConfig *config, const pmFPAview *view);
+bool            psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+
+bool            psphotBasicDeblend (pmConfig *config, const pmFPAview *view);
+bool            psphotBasicDeblendReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+
+bool            psphotRoughClass (pmConfig *config, const pmFPAview *view);
+bool            psphotRoughClassReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotRoughClassRegion (int nRegion, psRegion *region, psArray *sources, psMetadata *analysis, psMetadata *recipe, const bool havePSF);
+
+bool            psphotImageQuality (pmConfig *config, const pmFPAview *view);
+bool            psphotImageQualityReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
+bool            psphotChoosePSF (pmConfig *config, const pmFPAview *view);
+bool            psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
+bool            psphotGuessModels (pmConfig *config, const pmFPAview *view);
+bool            psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+
+bool            psphotMergeSources (pmConfig *config, const pmFPAview *view);
+bool            psphotMergeSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+
+bool            psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, bool final);
+bool            psphotFitSourcesLinearReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, bool final);
+
+bool            psphotSourceSize (pmConfig *config, const pmFPAview *view, bool getPSFsize);
+bool            psphotSourceSizeReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool getPSFsize);
+
+bool            psphotBlendFit (pmConfig *config, const pmFPAview *view);
+bool            psphotBlendFitReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
 bool            psphotBlendFit_Threaded (psThreadJob *job);
 
-bool 		psphotReplaceAllSources (pmConfig *config, const pmFPAview *view);
-bool 		psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool 		psphotAddNoise (pmConfig *config, const pmFPAview *view);
-bool 		psphotSubNoise (pmConfig *config, const pmFPAview *view);
-bool 		psphotAddOrSubNoise (pmConfig *config, const pmFPAview *view, bool add);
-bool 		psphotAddOrSubNoiseReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool add);
-
-bool 		psphotExtendedSourceAnalysis (pmConfig *config, const pmFPAview *view);
-bool 		psphotExtendedSourceAnalysisReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool 		psphotExtendedSourceFits (pmConfig *config, const pmFPAview *view);
-bool 		psphotExtendedSourceFitsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool 		psphotApResid (pmConfig *config, const pmFPAview *view);
-bool 		psphotApResidReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool 		psphotMagnitudes (pmConfig *config, const pmFPAview *view);
-bool 		psphotMagnitudesReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view);
+bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
+bool            psphotAddNoise (pmConfig *config, const pmFPAview *view);
+bool            psphotSubNoise (pmConfig *config, const pmFPAview *view);
+bool            psphotAddOrSubNoise (pmConfig *config, const pmFPAview *view, bool add);
+bool            psphotAddOrSubNoiseReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool add);
+
+bool            psphotExtendedSourceAnalysis (pmConfig *config, const pmFPAview *view);
+bool            psphotExtendedSourceAnalysisReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
+bool            psphotExtendedSourceFits (pmConfig *config, const pmFPAview *view);
+bool            psphotExtendedSourceFitsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
+bool            psphotApResid (pmConfig *config, const pmFPAview *view);
+bool            psphotApResidReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
+bool            psphotMagnitudes (pmConfig *config, const pmFPAview *view);
+bool            psphotMagnitudesReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
 bool            psphotMagnitudes_Threaded (psThreadJob *job);
 
-bool 		psphotEfficiency (pmConfig *config, const pmFPAview *view);
-bool 		psphotEfficiencyReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotEfficiency (pmConfig *config, const pmFPAview *view);
+bool            psphotEfficiencyReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
 
 bool            psphotPSFWeights(pmConfig *config, pmReadout *readout, const pmFPAview *view, psArray *sources);
 bool            psphotPSFWeights_Threaded (psThreadJob *job);
 
-bool 		psphotSkyReplace (pmConfig *config, const pmFPAview *view);
-bool 		psphotSkyReplaceReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool 		psphotSourceFreePixels (pmConfig *config, const pmFPAview *view);
-bool 		psphotSourceFreePixelsReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index);
+bool            psphotSkyReplace (pmConfig *config, const pmFPAview *view);
+bool            psphotSkyReplaceReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+
+bool            psphotSourceFreePixels (pmConfig *config, const pmFPAview *view);
+bool            psphotSourceFreePixelsReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index);
 
 // in psphotSourceStats.c:
 bool            psphotSourceStats_Threaded (psThreadJob *job);
 bool            psphotSourceStatsUpdate (psArray *sources, pmConfig *config, pmReadout *readout);
-bool 		psphotSetMomentsWindow (psMetadata *recipe, psMetadata *analysis, psArray *sources);
+bool            psphotSetMomentsWindow (psMetadata *recipe, psMetadata *analysis, psArray *sources);
 
 // in psphotChoosePSF.c:
@@ -144,5 +144,5 @@
 
 // in psphotGuessModel.c
-bool		psphotGuessModel_Threaded (psThreadJob *job);
+bool            psphotGuessModel_Threaded (psThreadJob *job);
 
 // in psphotMergeSources.c:
@@ -156,5 +156,5 @@
 
 // generate the detection structure for the supplied array of sources
-bool 	        psphotSetSourceParams (pmConfig *config, psArray *sources, pmPSF *psf);
+bool            psphotSetSourceParams (pmConfig *config, psArray *sources, pmPSF *psf);
 
 // in psphotModelBackground.c:
@@ -165,5 +165,5 @@
 // in psphotReplaceUnfit.c:
 bool            psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe);
-bool 		psphotReplaceUnfitSources (psArray *sources, psImageMaskType maskVal);
+bool            psphotReplaceUnfitSources (psArray *sources, psImageMaskType maskVal);
 
 // thread-related:
@@ -227,5 +227,5 @@
 
 bool            psphotLoadPSF (pmConfig *config, const pmFPAview *view);
-bool            psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+bool            psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *outFilename, const char *inFilename, int index);
 
 bool            psphotSetHeaderNstars (psMetadata *recipe, psArray *sources);
@@ -241,33 +241,33 @@
 
 // functions related to extended source analysis
-bool  	        psphotRadialProfile (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal);
-bool  		psphotRadialProfilesByAngles (pmSource *source, int Nsec, float Rmax);
-float 		psphotRadiusFromProfile (pmSource *source, psVector *radius, psVector *flux, float fluxMin, float fluxMax);
-bool  		psphotRadiiFromProfiles (pmSource *source, float fluxMin, float fluxMax);
-bool  		psphotEllipticalProfile (pmSource *source);
-bool  		psphotEllipticalContour (pmSource *source);
+bool            psphotRadialProfile (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal);
+bool            psphotRadialProfilesByAngles (pmSource *source, int Nsec, float Rmax);
+float           psphotRadiusFromProfile (pmSource *source, psVector *radius, psVector *flux, float fluxMin, float fluxMax);
+bool            psphotRadiiFromProfiles (pmSource *source, float fluxMin, float fluxMax);
+bool            psphotEllipticalProfile (pmSource *source);
+bool            psphotEllipticalContour (pmSource *source);
 
 // psphotVisual functions
 bool            psphotVisualShowImage (pmReadout *readout);
-bool 		psphotVisualShowBackground (pmConfig *config, const pmFPAview *view, pmReadout *readout);
-bool 		psphotVisualShowSignificance (psImage *image, float min, float max);
-bool 		psphotVisualShowPeaks (pmDetections *detections);
-bool 		psphotVisualShowFootprints (pmDetections *detections);
-bool 		psphotVisualShowMoments (psArray *sources);
-bool 		psphotVisualPlotMoments (psMetadata *recipe, psMetadata *analysis, psArray *sources);
-bool 		psphotVisualShowRoughClass (psArray *sources);
-bool 		psphotVisualShowPSFStars (psMetadata *recipe, pmPSF *psf, psArray *sources);
-bool 		psphotVisualShowSatStars (psMetadata *recipe, pmPSF *psf, psArray *sources);
-bool 		psphotVisualShowPSFModel (pmReadout *readout, pmPSF *psf);
-bool 		psphotVisualPlotRadialProfile (int myKapa, pmSource *source, psImageMaskType maskVal);
-bool 		psphotVisualPlotRadialProfiles (psMetadata *recipe, psArray *sources);
-bool 		psphotVisualShowFlags (psArray *sources);
-bool 		psphotVisualShowSourceSize (pmReadout *readout, psArray *sources);
-bool 		psphotVisualShowResidualImage (pmReadout *readout);
-bool 		psphotVisualPlotApResid (psArray *sources, float mean, float error);
-bool 		psphotVisualPlotChisq (psArray *sources);
-bool 		psphotVisualPlotSourceSize (psMetadata *recipe, psMetadata *analysis, psArray *sources);
-bool 		psphotVisualShowPetrosians (psArray *sources);
-bool 		psphotVisualEraseOverlays (int channel, char *overlay);
+bool            psphotVisualShowBackground (pmConfig *config, const pmFPAview *view, pmReadout *readout);
+bool            psphotVisualShowSignificance (psImage *image, float min, float max);
+bool            psphotVisualShowPeaks (pmDetections *detections);
+bool            psphotVisualShowFootprints (pmDetections *detections);
+bool            psphotVisualShowMoments (psArray *sources);
+bool            psphotVisualPlotMoments (psMetadata *recipe, psMetadata *analysis, psArray *sources);
+bool            psphotVisualShowRoughClass (psArray *sources);
+bool            psphotVisualShowPSFStars (psMetadata *recipe, pmPSF *psf, psArray *sources);
+bool            psphotVisualShowSatStars (psMetadata *recipe, pmPSF *psf, psArray *sources);
+bool            psphotVisualShowPSFModel (pmReadout *readout, pmPSF *psf);
+bool            psphotVisualPlotRadialProfile (int myKapa, pmSource *source, psImageMaskType maskVal);
+bool            psphotVisualPlotRadialProfiles (psMetadata *recipe, psArray *sources);
+bool            psphotVisualShowFlags (psArray *sources);
+bool            psphotVisualShowSourceSize (pmReadout *readout, psArray *sources);
+bool            psphotVisualShowResidualImage (pmReadout *readout);
+bool            psphotVisualPlotApResid (psArray *sources, float mean, float error);
+bool            psphotVisualPlotChisq (psArray *sources);
+bool            psphotVisualPlotSourceSize (psMetadata *recipe, psMetadata *analysis, psArray *sources);
+bool            psphotVisualShowPetrosians (psArray *sources);
+bool            psphotVisualEraseOverlays (int channel, char *overlay);
 
 bool psphotPetrosian (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal);
Index: branches/eam_branches/20091201/psphot/src/psphotGuessModels.c
===================================================================
--- branches/eam_branches/20091201/psphot/src/psphotGuessModels.c	(revision 26793)
+++ branches/eam_branches/20091201/psphot/src/psphotGuessModels.c	(revision 26808)
@@ -3,5 +3,5 @@
 // XXX : the threading here is not great.  this may be due to blocks between elements, but
 // the selection of the objects in a cell is not optimal.  To fix:
-// 1) define the boundaries of the cells up front 
+// 1) define the boundaries of the cells up front
 // 2) loop over the sources once and associate them with their cell
 // 3) define the threaded function to work with sources for a given cell
@@ -17,8 +17,8 @@
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
-	if (!psphotGuessModelsReadout (config, view, "PSPHOT.INPUT", i)) {
+        if (!psphotGuessModelsReadout (config, view, "PSPHOT.INPUT", i)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed on to guess models for PSPHOT.INPUT entry %d", i);
-	    return false;
-	}
+            return false;
+        }
     }
     return true;
@@ -46,10 +46,10 @@
 
     if (!sources->n) {
-	psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping model guess");
-	return true;
+        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping model guess");
+        return true;
     }
 
     pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
-    psAssert (sources, "missing psf?");
+    psAssert (psf, "missing psf?");
 
     // select the appropriate recipe information
@@ -60,5 +60,5 @@
     int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
     if (!status) {
-	nThreads = 0;
+        nThreads = 0;
     }
 
@@ -85,43 +85,43 @@
     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_GUESS_MODEL");
-	    psArrayAdd(job->args, 1, readout);
-	    psArrayAdd(job->args, 1, cells->data[j]); // sources
-	    psArrayAdd(job->args, 1, psf);
-
-	    // XXX change these to use abstract mask type info
-	    PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
-	    PS_ARRAY_ADD_SCALAR(job->args, markVal,  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
-	// wait here for the threaded jobs to finish
-	// fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
-	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) {
-	    // we have no returned data from this operation
-	    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_GUESS_MODEL");
+            psArrayAdd(job->args, 1, readout);
+            psArrayAdd(job->args, 1, cells->data[j]); // sources
+            psArrayAdd(job->args, 1, psf);
+
+            // XXX change these to use abstract mask type info
+            PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
+            PS_ARRAY_ADD_SCALAR(job->args, markVal,  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
+        // wait here for the threaded jobs to finish
+        // fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
+        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) {
+            // we have no returned data from this operation
+            if (job->args->n < 1) {
+                fprintf (stderr, "error with job\n");
+            }
+            psFree(job);
+        }
     }
 
@@ -129,9 +129,9 @@
     int nMiss = 0;
     for (int i = 0; i < sources->n; i++) {
-	pmSource *source = sources->data[i];
-	if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
-	    continue;
-	}
-	nMiss ++;
+        pmSource *source = sources->data[i];
+        if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
+            continue;
+        }
+        nMiss ++;
     }
     psAssert (nMiss == 0, "failed to attempt to build models for %d objects\n", nMiss);
@@ -149,5 +149,5 @@
     psArray *sources   = job->args->data[1];
     pmPSF *psf         = job->args->data[2];
-    
+
     psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[3],PS_TYPE_IMAGE_MASK_DATA);
     psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
@@ -156,69 +156,69 @@
 
     for (int i = 0; i < sources->n; i++) {
-	pmSource *source = sources->data[i];
-
-	// this is used to mark sources for which the model is measured. We check later that
-	// all are used.
-	source->tmpFlags |= PM_SOURCE_TMPF_MODEL_GUESS;
-
-	// skip non-astronomical objects (very likely defects)
-	if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
-	if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
-	if (!source->peak) continue;
-
-	nSrc ++;
-	
-	// the guess central intensity comes from the peak:
-	float Io = source->peak->flux;
-
-	// We have two options to get a guess for the object position: the position from the
-	// peak and the position from the moments.  Use the peak position if (a) there are no
-	// moments and (b) the sources is not saturated
-
-	bool useMoments = false;
-	useMoments = (source->mode & PM_SOURCE_MODE_SATSTAR);  // we only want to try if SATSTAR is set, but..
-	useMoments = (useMoments && source->moments);	       // can't if there are no moments
-	useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
-	useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
-
-	float Xo, Yo;
-	if (useMoments) {
-	    Xo = source->moments->Mx;
-	    Yo = source->moments->My;
-	} else {
-	    Xo = source->peak->xf;
-	    Yo = source->peak->yf;
-	}
-
-	// set PSF parameters for this model (apply 2D shape model to coordinates Xo, Yo)
-	pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
-
-	if (modelPSF == NULL) {
-	    psWarning ("Failed to determine PSF model at (%f,%f); trying image center", Xo, Yo);
-
-	    float Xc = 0.5*readout->image->numCols;
-	    float Yc = 0.5*readout->image->numRows;
-	    pmModel *modelPSF = pmModelFromPSFforXY(psf, Xc, Yc, Io);
-	    if (modelPSF == NULL) {
-		psError(PSPHOT_ERR_PSF, false, "Failed to determine PSF model at center of image");
-		return false;
-	    }
-
-	    // Now set the object position at the expected location:
-	    modelPSF->params->data.F32[PM_PAR_XPOS] = Xo;
-	    modelPSF->params->data.F32[PM_PAR_YPOS] = Yo;
-	    source->mode |= PM_SOURCE_MODE_BADPSF;
-	}
-
-	// set the fit radius based on the object flux limit and the model
-	// this function affects the mask pixels
-	psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
-
-	// set the source PSF model
-	psAssert (source->modelPSF == NULL, "failed to free one of the models?");
-	source->modelPSF = modelPSF;
-	source->modelPSF->residuals = psf->residuals;
-
-	pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
+        pmSource *source = sources->data[i];
+
+        // this is used to mark sources for which the model is measured. We check later that
+        // all are used.
+        source->tmpFlags |= PM_SOURCE_TMPF_MODEL_GUESS;
+
+        // skip non-astronomical objects (very likely defects)
+        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
+        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
+        if (!source->peak) continue;
+
+        nSrc ++;
+
+        // the guess central intensity comes from the peak:
+        float Io = source->peak->flux;
+
+        // We have two options to get a guess for the object position: the position from the
+        // peak and the position from the moments.  Use the peak position if (a) there are no
+        // moments and (b) the sources is not saturated
+
+        bool useMoments = false;
+        useMoments = (source->mode & PM_SOURCE_MODE_SATSTAR);  // we only want to try if SATSTAR is set, but..
+        useMoments = (useMoments && source->moments);          // can't if there are no moments
+        useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
+        useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
+
+        float Xo, Yo;
+        if (useMoments) {
+            Xo = source->moments->Mx;
+            Yo = source->moments->My;
+        } else {
+            Xo = source->peak->xf;
+            Yo = source->peak->yf;
+        }
+
+        // set PSF parameters for this model (apply 2D shape model to coordinates Xo, Yo)
+        pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
+
+        if (modelPSF == NULL) {
+            psWarning ("Failed to determine PSF model at (%f,%f); trying image center", Xo, Yo);
+
+            float Xc = 0.5*readout->image->numCols;
+            float Yc = 0.5*readout->image->numRows;
+            pmModel *modelPSF = pmModelFromPSFforXY(psf, Xc, Yc, Io);
+            if (modelPSF == NULL) {
+                psError(PSPHOT_ERR_PSF, false, "Failed to determine PSF model at center of image");
+                return false;
+            }
+
+            // Now set the object position at the expected location:
+            modelPSF->params->data.F32[PM_PAR_XPOS] = Xo;
+            modelPSF->params->data.F32[PM_PAR_YPOS] = Yo;
+            source->mode |= PM_SOURCE_MODE_BADPSF;
+        }
+
+        // set the fit radius based on the object flux limit and the model
+        // this function affects the mask pixels
+        psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
+
+        // set the source PSF model
+        psAssert (source->modelPSF == NULL, "failed to free one of the models?");
+        source->modelPSF = modelPSF;
+        source->modelPSF->residuals = psf->residuals;
+
+        pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
     }
 
Index: branches/eam_branches/20091201/psphot/src/psphotLoadPSF.c
===================================================================
--- branches/eam_branches/20091201/psphot/src/psphotLoadPSF.c	(revision 26793)
+++ branches/eam_branches/20091201/psphot/src/psphotLoadPSF.c	(revision 26808)
@@ -11,11 +11,18 @@
 
 // load an externally supplied psf model
-bool psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
+bool psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *outFilename, const char *inFilename, int index) {
 
     bool status;
 
     // find the currently selected readout
-    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    if (file == NULL) {
+    pmFPAfile *inFile = pmFPAfileSelectSingle(config->files, inFilename, index); // File of interest
+    if (inFile == NULL) {
+        psLogMsg ("psphot", 3, "no psf supplied for this chip");
+        return true;
+    }
+
+    // find the currently selected readout
+    pmFPAfile *outFile = pmFPAfileSelectSingle(config->files, outFilename, index); // File of interest
+    if (outFile == NULL) {
         psLogMsg ("psphot", 3, "no psf supplied for this chip");
         return true;
@@ -23,9 +30,9 @@
 
     // find the currently selected chip
-    pmChip *chip = pmFPAviewThisChip (view, file->fpa);
+    pmChip *chip = pmFPAviewThisChip (view, inFile->fpa);
     if (!chip) return false;
 
     // find the currently selected readout
-    pmReadout *readout = pmFPAviewThisReadout (view, file->fpa);
+    pmReadout *readout = pmFPAviewThisReadout (view, outFile->fpa);
     if (!readout) return false;
 
@@ -64,5 +71,5 @@
 
         // Generate the mask and weight images, including the user-defined analysis region of interest
-        if (!psphotLoadPSFReadout (config, view, "PSPHOT.PSF.LOAD", i)) {
+        if (!psphotLoadPSFReadout (config, view, "PSPHOT.INPUT", "PSPHOT.PSF.LOAD", i)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed to load PSF model for PSPHOT.PSF.LOAD entry %d", i);
             return false;
Index: branches/eam_branches/20091201/psphot/src/psphotModelBackground.c
===================================================================
--- branches/eam_branches/20091201/psphot/src/psphotModelBackground.c	(revision 26793)
+++ branches/eam_branches/20091201/psphot/src/psphotModelBackground.c	(revision 26808)
@@ -38,9 +38,9 @@
 // readout->analysis
 static bool psphotModelBackgroundReadout(psImage *model,  // Model image
-				  psImage *modelStdev, // Model stdev 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
+                                  psImage *modelStdev, // Model stdev 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
     )
 {
@@ -218,7 +218,7 @@
                 stats->options = PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV;
                 if (!psImageBackground(stats, &sample, subset, submask, maskVal, rng)) {
-                    psLogMsg ("psphot", PS_LOG_WARN, "Failed to estimate background using ROBUST_MEDIAN for "
-                               "(%dx%d, (row0,col0) = (%d,%d)",
-                               subset->numRows, subset->numCols, subset->row0, subset->col0);
+                    // psLogMsg ("psphot", PS_LOG_WARN, "Failed to estimate background using ROBUST_MEDIAN for "
+                    //            "(%dx%d, (row0,col0) = (%d,%d)",
+                    //            subset->numRows, subset->numCols, subset->row0, subset->col0);
                     modelData[iy][ix] = modelStdevData[iy][ix] = NAN;
                 } else {
@@ -383,8 +383,8 @@
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
-	if (!psphotModelBackgroundReadoutFileIndex(config, view, "PSPHOT.INPUT", i)) {
+        if (!psphotModelBackgroundReadoutFileIndex(config, view, "PSPHOT.INPUT", i)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed to model background for PSPHOT.INPUT entry %d", i);
-	    return false;
-	}
+            return false;
+        }
     }
     return true;
Index: branches/eam_branches/20091201/psphot/src/psphotReadoutMinimal.c
===================================================================
--- branches/eam_branches/20091201/psphot/src/psphotReadoutMinimal.c	(revision 26793)
+++ branches/eam_branches/20091201/psphot/src/psphotReadoutMinimal.c	(revision 26808)
@@ -62,4 +62,7 @@
     psphotGuessModels (config, view);
 
+    // merge the newly selected sources into the existing list
+    psphotMergeSources (config, view);
+
     // linear PSF fit to source peaks
     psphotFitSourcesLinear (config, view, false);
