Index: /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraAnalysis.c
===================================================================
--- /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraAnalysis.c	(revision 42185)
+++ /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraAnalysis.c	(revision 42186)
@@ -20,9 +20,9 @@
 
     // loop over the input images
-    pmFPAfile *input = psMetadataLookupPtr (NULL, config->files, "FPCAMERA.INPUT");
+    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "FPCAMERA.INPUT");
     if (!input) ESCAPE(FPCAMERA_ERR_CONFIG, "Can't find or interpret output file rule FPCAMERA.INPUT!");
 
     // astrometry reference (smf)
-    pmFPAfile *astrom = psMetadataLookupPtr (NULL, config->files, "FPCAMERA.INPUT.ASTROM");
+    pmFPAfile *astrom = psMetadataLookupPtr (&status, config->files, "FPCAMERA.INPUT.ASTROM");
     if (!astrom) ESCAPE(FPCAMERA_ERR_CONFIG, "Can't find or interpret output file rule FPCAMERA.INPUT.ASTROM!");
 
@@ -33,5 +33,6 @@
     pmFPAfileActivate (config->files, true, "FPCAMERA.INPUT.VARIANCE");
     pmFPAfileActivate (config->files, true, "FPCAMERA.RESID");
-    // Note FPCAMERA.RESID is tied to FPCAMERA.INPUT so they must be active in the same block.
+    pmFPAfileActivate (config->files, true, "PSPHOT.PSF.LOAD"); // if this file is defined, we need to activate it now
+    // Note: the output file FPCAMERA.RESID is tied to FPCAMERA.INPUT so they must be active in the same block.
 
     view = pmFPAviewAlloc (0);
@@ -57,5 +58,22 @@
 
 		fpcameraChooseRefstars (input, astrom, view);
+
+		// Provide a simple (wrong) PSF as a default if psf model is not supplied with -psf or -psflist.
+		// The user-supplied psf model will replace this one on a chip-by-chip basis
+		// pmPSF *psf = pmPSFBuildSimple ("PS_MODEL_PS1_V1", 5.0, 5.0, 0.0, 0.5);
+		pmPSF *psf = pmPSFBuildSimple ("PS_MODEL_GAUSS", 5.0, 5.0, 0.0);
+		psf->fieldNx = readout->image->numCols;
+		psf->fieldNy = readout->image->numRows;
+		psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot PSF model", psf);
+		psFree (psf);
+
+		if (!psphotForcedReadout (config, view, "FPCAMERA.INPUT")) ESCAPE(FPCAMERA_ERR_DATA, "failure in psphotForcedReadout");
 	    }
+            // drop all versions of the internal files
+            status = true;
+            status &= pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL");
+            status &= pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL.STDEV");
+            status &= pmFPAfileDropInternal (config->files, "PSPHOT.BACKGND");
+            if (!status) ESCAPE(FPCAMERA_ERR_PROG, "trouble dropping internal files");
 	}
         if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE(FPCAMERA_ERR_IO, "failure in IOChecks(AFTER) at Chip");
@@ -72,2 +90,11 @@
 // chip->process is set (unset) based on command-line -chip selections (in fpcameraArguments)
 // chip->file_exists is set (in pmFPAFlags.c:pmChipSetFileStatus) by pmFPAfileDefineFromArgs (in pmFPAAddSource...)
+
+/*
+        // XXX set sxx, etc from FWHM in recipe
+        pmPSF *psf = pmPSFBuildSimple (modelNames->data[0], 1.0, 1.0, 0.0, 1.0);
+        psf->fieldNx = readout->image->numCols;
+        psf->fieldNy = readout->image->numRows;
+        psFree (modelNames);
+
+*/
Index: /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraArguments.c
===================================================================
--- /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraArguments.c	(revision 42185)
+++ /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraArguments.c	(revision 42186)
@@ -11,4 +11,5 @@
     fpcameraErrorRegister();		// register our error codes/messages
     pmModelClassInit();			// model inits are needed in pmSourceIO
+    psphotInit();
 
     if (argc == 1) ESCAPE(FPCAMERA_ERR_ARGUMENTS, "No arguments supplied");
@@ -72,4 +73,8 @@
     if (!status) ESCAPE(FPCAMERA_ERR_ARGUMENTS, "Missing -astrom-file (input) : provide an smf or similar");
 
+    // specify the input images to process
+    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT.PSF", "-psf",     "-psflist");
+    // user-supplied psf model is optional
+    
     if (argc != 2) ESCAPE(FPCAMERA_ERR_ARGUMENTS, "Incorrect number of arguments supplied");
 
Index: /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraChooseRefstars.c
===================================================================
--- /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraChooseRefstars.c	(revision 42185)
+++ /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraChooseRefstars.c	(revision 42186)
@@ -105,4 +105,5 @@
     pmDetections *detections = pmDetectionsAlloc();
     detections->allSources   = psArrayAllocEmpty (100);
+    detections->newSources   = psArrayAllocEmpty (100);
 
     bool status = psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detections);
Index: /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraCleanup.c
===================================================================
--- /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraCleanup.c	(revision 42185)
+++ /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraCleanup.c	(revision 42186)
@@ -3,4 +3,6 @@
 /* \brief this loop saves the photometry/astrometry data files */
 void fpcameraCleanup (pmConfig *config, psMetadata *stats) {
+
+    psMemCheckCorruption (stderr, true);
 
     psFree (stats);
@@ -10,9 +12,14 @@
 
     psTimerStop ();
-    psMemCheckCorruption (stderr, true);
+    psphotVisualClose();
+
     pmModelClassCleanup ();
     psTimeFinalize ();
     pmConceptsDone ();
     pmConfigDone ();
+
+    pmSourceFitSetDone ();
+    psLibFinalize();
+
     fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "fpcamera");
     return;
Index: /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraParseCamera.c
===================================================================
--- /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraParseCamera.c	(revision 42185)
+++ /branches/eam_branches/ipp-20220316/fpcamera/src/fpcameraParseCamera.c	(revision 42186)
@@ -19,4 +19,7 @@
     if (!status) ESCAPE (PS_ERR_UNKNOWN, "failed to load find definition");
 
+    pmFPAfile *psphotInput = pmFPAfileDefineFromFile (config, input, 1, 1, "PSPHOT.INPUT");
+    if (!psphotInput) ESCAPE(FPCAMERA_ERR_CONFIG, "Failed to define FPA for PSPHOT.INPUT");
+
     if (!psphotSetMaskBits (config)) ESCAPE (PS_ERR_UNKNOWN, "failed to set mask bit values");
 
@@ -24,4 +27,10 @@
     pmFPAfileDefineFromArgs (&status, config, "FPCAMERA.INPUT.ASTROM", "INPUT.ASTROM");
     if (!status) ESCAPE(FPCAMERA_ERR_CONFIG, "Failed to build FPA from FPCAMERA.INPUT.ASTROM");
+
+    // if optional PSF model is supplied, associate with input file
+    if (psMetadataLookupPtr(NULL, config->arguments, "INPUT.PSF")) {
+        pmFPAfileBindFromArgs(&status, input, config, "PSPHOT.PSF.LOAD", "INPUT.PSF");
+        if (!status) ESCAPE(FPCAMERA_ERR_CONFIG, "Failed to find/build PSPHOT.PSF.LOAD");
+    }
 
     // select the current recipe
@@ -56,2 +65,3 @@
     return true;
 }
+
