Index: /branches/eam_branch_20080511/ppSim/src/Makefile.am
===================================================================
--- /branches/eam_branch_20080511/ppSim/src/Makefile.am	(revision 17629)
+++ /branches/eam_branch_20080511/ppSim/src/Makefile.am	(revision 17630)
@@ -24,4 +24,7 @@
 	ppSimUtils.c            \
 	ppSimLoop.c		\
+	ppSimLoadSpots.c	\
+	ppSimPhotom.c		\
+	ppSimMosaicChip.c	\
 	ppSimBadPixels.c
 
Index: /branches/eam_branch_20080511/ppSim/src/ppSim.h
===================================================================
--- /branches/eam_branch_20080511/ppSim/src/ppSim.h	(revision 17629)
+++ /branches/eam_branch_20080511/ppSim/src/ppSim.h	(revision 17630)
@@ -98,4 +98,6 @@
 bool ppSimMakeSky (pmReadout *readout, psImage *expCorr, ppSimType type, pmConfig *config);
 
+psArray *ppSimLoadSpots (pmFPA *fpa, pmConfig *config);
+
 bool ppSimLoadStars (psArray *stars, pmFPA *fpa, pmConfig *config);
 bool ppSimMakeStars(psArray *stars, pmFPA *fpa, pmConfig *config, const psRandom *rng);
@@ -127,4 +129,10 @@
 bool ppSimInsertGalaxies (pmReadout *readout, psImage *expCorr, psArray *galaxies, pmConfig *config);
 
+bool ppSimMosaicChip(pmConfig *config, const psMaskType blankMask, const pmFPAview *view,
+		     const char *outFile, const char *inFile);
+
+bool ppSimPhotom (pmConfig *config, pmFPAview *view);
+
+
 /// Add bad pixels to an image
 bool ppSimBadPixels(pmReadout *readout, ///< Readout for which to generate bad pixels
Index: /branches/eam_branch_20080511/ppSim/src/ppSimCreate.c
===================================================================
--- /branches/eam_branch_20080511/ppSim/src/ppSimCreate.c	(revision 17629)
+++ /branches/eam_branch_20080511/ppSim/src/ppSimCreate.c	(revision 17630)
@@ -40,5 +40,5 @@
     }
 
-    // define the output image file
+    // define the output image file -- this is the basis for the ppSimLoop
     pmFPAfile *file = pmFPAfileDefineOutput(config, fpa, OUTPUT_FILE);
     if (!file) {
@@ -53,4 +53,5 @@
         return NULL;
     }
+    // XXX we should not require the output image to be written
     file->save = true;
 
@@ -58,20 +59,49 @@
     config->formatName = psStringCopy (file->formatName);
 
-    // have we supplied a psf model?
-    if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
-	bool status = false;
+    // For photometry, we operate on the chip-mosaicked image.  we create a copy of the mosaicked
+    // image for psphot so we can write out a clean image
+    if (options->doPhotom) {
 
-	// tie the psf file to the chipMosaic 
-        pmFPAfileBindFromArgs(&status, file, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
-	    psFree(fpa);
-	    psFree(file);
-            return NULL;
+	// we need a chip image if we perform photometry (is it necessary to build it if we don't use it?)
+	pmFPAfile *chipImage = pmFPAfileDefineChipMosaic(config, output->fpa, "PPSIM.CHIP");
+	if (!chipImage) {
+	    psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.CHIP"));
+	    psFree(options);
+	    return NULL;
+	}
+	if (chipImage->type != PM_FPA_FILE_IMAGE) {
+	    psError(PS_ERR_IO, true, "PPSIM.CHIP is not of type IMAGE");
+	    psFree(options);
+	    return NULL;
+	}
+
+        // this file is just used as a carrier; output files (eg, PSPHOT.RESID) are defined by
+        // psphotDefineFiles
+        pmFPAfile *psphotInput = pmFPAfileDefineFromFile (config, chipImage, 1, 1, "PSPHOT.INPUT");
+        PS_ASSERT (psphotInput, false);
+
+        // define associated psphot input/output files
+        if (!psphotDefineFiles (config, psphotInput)) {
+            psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psphot");
+            return false;
         }
+    } else {
+	// have we supplied a psf model?  this happens in psphotDefineFiles if we request a photometry
+	// analysis.  however, even if we do not, a psf model may be used to generate the fake
+	// sources.
+	if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
+	    // tie the psf file to the chipMosaic 
+	    pmFPAfileBindFromArgs(&status, file, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
+	    if (!status) {
+		psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
+		psFree(fpa);
+		psFree(file);
+		return NULL;
+	    }
+	}
     }
 
+    // PPSIM.SOURCES carries the constructed, fake sources with their true parameters
     // XXX only invoke this code for OBJECT types of images
-    // PPSIM.SOURCES carries the constructed, fake sources with their true parameters
     pmFPAfile *simSources = pmFPAfileDefineOutput (config, file->fpa, "PPSIM.SOURCES");
     if (!simSources) {
Index: /branches/eam_branch_20080511/ppSim/src/ppSimLoop.c
===================================================================
--- /branches/eam_branch_20080511/ppSim/src/ppSimLoop.c	(revision 17629)
+++ /branches/eam_branch_20080511/ppSim/src/ppSimLoop.c	(revision 17630)
@@ -5,4 +5,5 @@
     PS_ASSERT_PTR_NON_NULL(config, PS_EXIT_PROG_ERROR);
 
+    // in this program, we are looping over the output image, rather than the input as in ppImage
     pmFPAfile *file = psMetadataLookupPtr(NULL, config->files, OUTPUT_FILE); // Output file
     assert(file);
@@ -15,4 +16,9 @@
 
     int binning = psMetadataLookupS32(NULL, config->arguments, "BINNING"); // Binning in x and y
+
+    // Load forced-photometry positions
+    if (type == PPSIM_TYPE_OBJECT) {
+      psArray *spots = ppSimLoadSpots (fpa, config);
+    }
 
     // Load catalogue stars
@@ -170,4 +176,9 @@
         }
 
+        ppSimMosaicChip(config, blankMask, view, "PPSIM.CHIP", "PPSIM.OUTPUT");
+
+        // we perform photometry on the readouts of this chip in the output
+	ppSimPhotom (config, view);
+
         if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
             psError(PS_ERR_IO, false, "Unable to write file.");
