Index: /branches/eam_branches/psphot.stack.20100120/src/Makefile.am
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/Makefile.am	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/Makefile.am	(revision 26691)
@@ -25,7 +25,6 @@
 libpsphot_la_LDFLAGS = $(PSPHOT_LIBS) $(PSMODULE_LIBS) $(PSLIB_LIBS)
 
-bin_PROGRAMS = psphot psphotForced psphotMakePSF psphotTest psphotMomentsStudy 
-# bin_PROGRAMS = psphotPetrosianStudy 
-# bin_PROGRAMS = psphot
+bin_PROGRAMS = psphot 
+# bin_PROGRAMS = psphotPetrosianStudy psphotForced psphotMakePSF psphotTest psphotMomentsStudy 
 
 psphot_CFLAGS = $(PSPHOT_CFLAGS) $(PSMODULE_CFLAGS) $(PSLIB_CFLAGS)
@@ -113,7 +112,4 @@
 	psphotDefineFiles.c	       \
 	psphotReadout.c		       \
-	psphotReadoutFindPSF.c	       \
-	psphotReadoutKnownSources.c    \
-	psphotReadoutMinimal.c	       \
 	psphotModelBackground.c	       \
 	psphotSubtractBackground.c     \
@@ -146,5 +142,4 @@
 	psphotKernelFromPSF.c	       \
 	psphotPSFConvModel.c	       \
-	psphotModelTest.c	       \
 	psphotFitSet.c		       \
 	psphotSourceFreePixels.c       \
@@ -175,4 +170,10 @@
 	psphotEfficiency.c
 
+# XXX need to fix these for the new apis
+#	psphotReadoutFindPSF.c	       
+#	psphotReadoutKnownSources.c    
+#	psphotReadoutMinimal.c	       
+#	psphotModelTest.c	       
+
 # re-instate these
 #	psphotIsophotal.c	       \
Index: /branches/eam_branches/psphot.stack.20100120/src/psphot.h
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphot.h	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphot.h	(revision 26691)
@@ -37,5 +37,7 @@
 bool            psphotReadoutMinimal(pmConfig *config, const pmFPAview *view);
 
-bool            psphotReadoutCleanup (pmConfig *config, pmReadout *readout, psMetadata *recipe, pmDetections *detections, pmPSF *psf, psArray *sources);
+bool            psphotReadoutCleanup (pmConfig *config, const pmFPAview *view);
+bool            psphotReadoutCleanupReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
 bool            psphotDefineFiles (pmConfig *config, pmFPAfile *input);
 void            psphotFilesActivate(pmConfig *config, bool state);
@@ -52,5 +54,5 @@
 bool            psphotAddPhotcodeReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
 
-bool            psphotSetMaskAndVariance (pmConfig *config, const pmFPAview *view, psMetadata *recipe);
+bool            psphotSetMaskAndVariance (pmConfig *config, const pmFPAview *view);
 bool            psphotSetMaskAndVarianceReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
 
@@ -71,5 +73,5 @@
 
 bool 		psphotBasicDeblend (pmConfig *config, const pmFPAview *view);
-bool 		psphotBasicDeblend (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+bool 		psphotBasicDeblendReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
 
 bool 		psphotRoughClass (pmConfig *config, const pmFPAview *view);
@@ -90,5 +92,5 @@
 
 bool 		psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, bool final);
-bool 		psphotFitSourcesLinearReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
+bool 		psphotFitSourcesLinearReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, bool final);
 
 bool 		psphotSourceSize (pmConfig *config, const pmFPAview *view, bool getPSFsize);
@@ -114,5 +116,5 @@
 
 bool 		psphotApResid (pmConfig *config, const pmFPAview *view);
-bool 		psphotApResid (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool 		psphotApResidReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
 
 bool 		psphotMagnitudes (pmConfig *config, const pmFPAview *view);
@@ -162,8 +164,4 @@
 bool            psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe);
 bool 		psphotReplaceUnfitSources (psArray *sources, psImageMaskType maskVal);
-bool            psphotReplaceUnfitSources (psArray *sources, psImageMaskType maskVal);
-
-bool            psphotReplaceAllSources (psArray *sources, psMetadata *recipe);
-bool            psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe);
 
 // thread-related:
@@ -186,5 +184,4 @@
 void            psphotModelClassInit (void);
 bool            psphotGrowthCurve (pmReadout *readout, pmPSF *psf, bool ignore, psImageMaskType maskVal);
-void            psphotSourceFreePixels (psArray *sources);
 
 // functions to set the correct source pixels
@@ -226,9 +223,8 @@
 bool            psphotFitSummary (void);
 
-pmPSF          *psphotLoadPSF (pmConfig *config, const pmFPAview *view, psMetadata *recipe);
+bool            psphotLoadPSF (pmConfig *config, const pmFPAview *view);
+bool            psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+
 bool            psphotSetHeaderNstars (psMetadata *recipe, psArray *sources);
-bool            psphotAddNoise (pmReadout *readout, const psArray *sources, const psMetadata *recipe);
-bool            psphotSubNoise (pmReadout *readout, const psArray *sources, const psMetadata *recipe);
-bool            psphotAddOrSubNoise (pmReadout *readout, const psArray *sources, const psMetadata *recipe, bool add);
 bool            psphotRadialPlot (int *kapa, const char *filename, pmSource *source);
 bool            psphotSourcePlots (pmReadout *readout, psArray *sources, psMetadata *recipe);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotAddNoise.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotAddNoise.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotAddNoise.c	(revision 26691)
@@ -18,5 +18,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotApResid.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotApResid.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotApResid.c	(revision 26691)
@@ -13,10 +13,10 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
-	if (!psphottApResidReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
+	if (!psphotApResidReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed on saturated star deblend analysis for PSPHOT.INPUT entry %d", i);
 	    return false;
@@ -26,5 +26,5 @@
 }
 
-bool psphotApResid (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe)
+bool psphotApResidReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe)
 {
     int Nfail = 0;
@@ -39,5 +39,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotBasicDeblend.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotBasicDeblend.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotBasicDeblend.c	(revision 26691)
@@ -6,5 +6,5 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -19,5 +19,5 @@
 }
 
-bool psphotBasicDeblend (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
+bool psphotBasicDeblendReadout (pmConfig *config, const pmFPAview *view, const char *filename, int fileIndex) {
 
     int N;
@@ -31,5 +31,5 @@
 
     // find the currently selected readout
-    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, fileIndex); // File of interest
     psAssert (file, "missing file?");
 
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotBlendFit.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotBlendFit.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotBlendFit.c	(revision 26691)
@@ -10,5 +10,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotChoosePSF.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotChoosePSF.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotChoosePSF.c	(revision 26691)
@@ -10,5 +10,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -28,10 +28,4 @@
     bool status;
 
-    // do not generate a PSF if we already were supplied one
-    if (psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF")) {
-	psLogMsg ("psphot", PS_LOG_DETAIL, "psf model supplied for input file %d", i);
-        return true;
-    }
-
     psTimerStart ("psphot.choose.psf");
 
@@ -42,4 +36,10 @@
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     psAssert (readout, "missing readout?");
+
+    // do not generate a PSF if we already were supplied one
+    if (psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF")) {
+	psLogMsg ("psphot", PS_LOG_DETAIL, "psf model supplied for input file %d", index);
+        return true;
+    }
 
     pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
@@ -350,5 +350,5 @@
 
     // save PSF on readout->analysis
-    if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_PTR, "psphot psf model", psf)) {
+    if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot psf model", psf)) {
 	psError (PSPHOT_ERR_UNKNOWN, false, "problem saving sources on readout");
         return false;
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotDeblendSatstars.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotDeblendSatstars.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotDeblendSatstars.c	(revision 26691)
@@ -6,5 +6,5 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -19,8 +19,9 @@
 }
 
-bool psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
+bool psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int fileIndex) {
 
     int N;
     pmSource *source;
+    bool status;
 
     psTimerStart ("psphot.deblend.sat");
@@ -30,5 +31,5 @@
 
     // find the currently selected readout
-    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, fileIndex); // File of interest
     psAssert (file, "missing file?");
 
@@ -51,5 +52,4 @@
     psAssert (recipe, "missing recipe?");
 
-    bool status;
     pmCell *cell = readout->parent;
     float SATURATION = 0.75*psMetadataLookupF32 (&status, cell->concepts, "CELL.SATURATION");
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotEfficiency.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotEfficiency.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotEfficiency.c	(revision 26691)
@@ -6,4 +6,6 @@
 //#define TESTING
 
+
+# if 0
 
 // Calculate the limiting magnitude for an image
@@ -148,4 +150,5 @@
 }
 
+# endif
 
 bool psphotEfficiency (pmConfig *config, const pmFPAview *view)
@@ -157,10 +160,10 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
-	if (!psphotReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
+	if (!psphotEfficiencyReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed on saturated star deblend analysis for PSPHOT.INPUT entry %d", i);
 	    return false;
@@ -173,9 +176,12 @@
 bool psphotEfficiencyReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe)
 {
+# if 0
+    bool status = true;
+
     psTimerStart("psphot.fake");
 
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
@@ -512,4 +518,5 @@
     psLogMsg("psphot", PS_LOG_INFO, "Detection efficiency: %lf sec\n", psTimerClear("psphot.fake"));
 
+# endif
 
     return true;
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotExtendedSourceAnalysis.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotExtendedSourceAnalysis.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotExtendedSourceAnalysis.c	(revision 26691)
@@ -16,5 +16,5 @@
     }
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -41,5 +41,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotExtendedSourceFits.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotExtendedSourceFits.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotExtendedSourceFits.c	(revision 26691)
@@ -16,5 +16,5 @@
     }
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -42,5 +42,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotFindDetections.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotFindDetections.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotFindDetections.c	(revision 26691)
@@ -12,5 +12,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -54,5 +54,5 @@
 	
 	// save detections on the readout->analysis
-	if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_PTR, "psphot detectinos", detections)) {
+	if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detectinos", detections)) {
 	    psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
 	    return false;
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotFitSourcesLinear.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotFitSourcesLinear.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotFitSourcesLinear.c	(revision 26691)
@@ -17,5 +17,5 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -30,5 +30,5 @@
 }
 
-bool psphotFitSourcesLinearReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
+bool psphotFitSourcesLinearReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, bool final) {
 
     bool status;
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotGuessModels.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotGuessModels.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotGuessModels.c	(revision 26691)
@@ -12,5 +12,5 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -34,5 +34,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotImageQuality.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotImageQuality.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotImageQuality.c	(revision 26691)
@@ -13,5 +13,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -29,4 +29,6 @@
 bool psphotImageQualityReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe)
 {
+    bool status = true;
+
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotLoadPSF.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotLoadPSF.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotLoadPSF.c	(revision 26691)
@@ -2,9 +2,11 @@
 
 // load an externally supplied psf model
-pmPSF *psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
+bool psphotLoadPSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
+
+    bool status;
 
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     // find the currently selected chip
@@ -13,9 +15,9 @@
 
     // find the currently selected readout
-    pmReadout *readout = pmFPAfileThisReadout (view, file->fpa);
+    pmReadout *readout = pmFPAviewThisReadout (view, file->fpa);
     if (!readout) return false;
 
     // check if a PSF model is supplied by the user
-    pmPSF *psf = psMetadataLookupPtr (NULL, chip->analysis, "PSPHOT.PSF");
+    pmPSF *psf = psMetadataLookupPtr (&status, chip->analysis, "PSPHOT.PSF");
     if (psf == NULL) {
         psLogMsg ("psphot", 3, "no psf supplied for this chip");
@@ -30,5 +32,5 @@
 
     // save PSF on readout->analysis
-    if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_PTR, "psphot psf model", psf)) {
+    if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot psf model", psf)) {
 	psError (PSPHOT_ERR_UNKNOWN, false, "problem saving sources on readout");
         return false;
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotMagnitudes.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotMagnitudes.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotMagnitudes.c	(revision 26691)
@@ -9,5 +9,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -31,5 +31,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotMaskReadout.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotMaskReadout.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotMaskReadout.c	(revision 26691)
@@ -16,11 +16,8 @@
 
 	// Generate the mask and weight images, including the user-defined analysis region of interest
-	if (!psphotSetMaskAndVarianceReadout (config, readout, recipe)) {
+	if (!psphotSetMaskAndVarianceReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed to generate mask for PSPHOT.INPUT entry %d", i);
 	    return false;
 	}
-
-	// display the image, weight, mask (ch 1,2,3)
-	psphotVisualShowImage (readout);
     }
     return true;
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotMergeSources.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotMergeSources.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotMergeSources.c	(revision 26691)
@@ -10,5 +10,5 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -26,4 +26,6 @@
 bool psphotMergeSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
 
+    bool status;
+
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
@@ -80,5 +82,5 @@
 		source->modelPSF = NULL;
 	    }
-	    psphotMergeSources (sources, extSourcesCMF);
+	    // XXX psphotMergeSources (sources, extSourcesCMF);
 	}
     }
@@ -101,5 +103,5 @@
 		source->modelPSF = NULL;
 	    }
-	    psphotMergeSources (sources, extSourcesTXT);
+	    // XXX psphotMergeSources (sources, extSourcesTXT);
 	}
     }
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotReadout.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotReadout.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotReadout.c	(revision 26691)
@@ -127,5 +127,5 @@
     // merge the newly selected sources into the existing list
     // NOTE: merge OLD and NEW
-    psphotMergeSources (config, view); FIX:
+    psphotMergeSources (config, view); 
 
     // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
@@ -186,5 +186,6 @@
     // merge the newly selected sources into the existing list
     // NOTE: merge OLD and NEW
-    psphotMergeSources (config, view); FIX: // (detections->newSources + detections->allSources -> detections->allSources)
+    // XXX check on free of sources...
+    psphotMergeSources (config, view); // (detections->newSources + detections->allSources -> detections->allSources)
 
     // NOTE: apply to ALL sources
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotReadoutCleanup.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotReadoutCleanup.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotReadoutCleanup.c	(revision 26691)
@@ -15,10 +15,14 @@
     }
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    // select the appropriate recipe information
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+    psAssert (recipe, "missing recipe?");
+
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
-	if (!psphotReadoutCleanupReadout (config, view, "PSPHOT.INPUT", i)) {
+	if (!psphotReadoutCleanupReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed on psphotReadoutCleanup for PSPHOT.INPUT entry %d", i);
 	    return false;
@@ -35,15 +39,11 @@
 // not a DATA error, then there was a serious problem.  Only in this case, or if the fail
 // on the stats measurement, do we return false
-bool psphotReadoutCleanupReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
+bool psphotReadoutCleanupReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
 
-    pmReadout *readout, psMetadata *recipe, pmDetections *detections, pmPSF *psf, psArray *sources;
-
-    // select the appropriate recipe information
-    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
-    psAssert (recipe, "missing recipe?");
+    bool status = true;
 
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
@@ -51,7 +51,7 @@
 
     // when psphotReadoutCleanup is called, these are not necessarily defined
-    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
+    pmPSF        *psf        = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
     pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
-    psArray *sources = detections->allSources;
+    psArray      *sources    = detections->allSources;
     // XXX where do we free these, in here (psMetadataRemove?)
 
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotReplaceUnfit.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotReplaceUnfit.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotReplaceUnfit.c	(revision 26691)
@@ -31,5 +31,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotRoughClass.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotRoughClass.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotRoughClass.c	(revision 26691)
@@ -16,5 +16,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotSkyReplace.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotSkyReplace.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotSkyReplace.c	(revision 26691)
@@ -5,10 +5,10 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
-	if (!psphotReadout (config, view, "PSPHOT.INPUT", i)) {
+	if (!psphotSkyReplaceReadout (config, view, "PSPHOT.INPUT", i)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed on saturated star deblend analysis for PSPHOT.INPUT entry %d", i);
 	    return false;
@@ -26,5 +26,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotSourceFreePixels.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotSourceFreePixels.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotSourceFreePixels.c	(revision 26691)
@@ -5,5 +5,5 @@
     bool status = true;
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -20,7 +20,9 @@
 bool psphotSourceFreePixelsReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index) {
 
+    bool status;
+
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotSourceSize.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotSourceSize.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotSourceSize.c	(revision 26691)
@@ -38,5 +38,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
@@ -61,5 +61,5 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
@@ -131,5 +131,6 @@
     psphotSourceSizeCR (readout, sources, &options);
 
-    psLogMsg ("psphot.size", PS_LOG_INFO, "measure source sizes for %ld sources: %f sec\n", sources->n - first, psTimerMark ("psphot.size"));
+    // XXX fix this (was source->n  - first)
+    psLogMsg ("psphot.size", PS_LOG_INFO, "measure source sizes for %ld sources: %f sec\n", sources->n, psTimerMark ("psphot.size"));
 
     psphotVisualPlotSourceSize (recipe, readout->analysis, sources);
@@ -633,4 +634,6 @@
 /**** ------ old versions of cosmic ray masking ----- ****/
 
+bool psphotMaskCosmicRayIsophot (pmSource *source, psImageMaskType maskVal, psImageMaskType crMask);
+
 // This attempt to mask the cosmic rays used the isophotal boundary
 bool psphotMaskCosmicRay_V1 (psImage *mask, pmSource *source, psImageMaskType maskVal, psImageMaskType crMask) {
Index: /branches/eam_branches/psphot.stack.20100120/src/psphotSourceStats.c
===================================================================
--- /branches/eam_branches/psphot.stack.20100120/src/psphotSourceStats.c	(revision 26690)
+++ /branches/eam_branches/psphot.stack.20100120/src/psphotSourceStats.c	(revision 26691)
@@ -11,5 +11,5 @@
     psAssert (recipe, "missing recipe?");
 
-    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
