Index: trunk/ppImage/src/ppImagePhotom.c
===================================================================
--- trunk/ppImage/src/ppImagePhotom.c	(revision 7592)
+++ trunk/ppImage/src/ppImagePhotom.c	(revision 7621)
@@ -24,58 +24,82 @@
     pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSPHOT.INPUT");
     if (!status) {
-
 	// psphotReadout requires a pmFPAfile supplied with the name PSPHOT.INPUT
-	// create a pmFPAfile which points at PPIMAGE.OUTPUT
+	// create a pmFPAfile which points to PPIMAGE.OUTPUT.CHIP (guaranteed to be mosaiced by chip)
 	// mode is 'REFERENCE' to prevent double frees of the fpa
-	pmFPAfile *output = psMetadataLookupPtr (&status, config->files, "PPIMAGE.OUTPUT");
+	pmFPAfile *output = psMetadataLookupPtr (&status, config->files, "PPIMAGE.OUTPUT.CHIP");
 	input = pmFPAfileDefineInput (config, output->fpa, "PSPHOT.INPUT");
 	input->mode = PM_FPA_MODE_REFERENCE;
 
 	pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.OUTPUT");
-
-	// supply the output name (from cmd-line) to all output (WRITE) files
-	// XXX does this cause trouble with existing files?
-	char *outname = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
-	pmFPAfileAddFileNames (config->files, "OUTPUT", outname, PM_FPA_MODE_WRITE);
     }
 
-    // XXX add the option output files here
+    // optionally save the residual image 
+    if (psMetadataLookupBool(NULL, recipe, "SAVE.RESID")) {
+	pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.RESID");
+    }
 
-    // int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
-    // int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
+    int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
+    int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
 
-    // we only was to operate on PSPHOT pmFPAfiles here:
-    pmFPAfileActivate (config->files, false, NULL);
-    pmFPAfileActivate (config->files, true, "PSPHOT.INPUT");
-    pmFPAfileActivate (config->files, true, "PSPHOT.RESID");
-    pmFPAfileActivate (config->files, true, "PSPHOT.OUTPUT");
+    // these calls construct a new fpa for the I/O handle 
 
-    pmFPAfileActivate (config->files, true, "PSPHOT.BACKSUB");
-    pmFPAfileActivate (config->files, true, "PSPHOT.BACKGND");
-    pmFPAfileActivate (config->files, true, "PSPHOT.BACKMDL");
+    // optionally save the background model (small FITS image)
+    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKMDL")) {
+	pmFPAfileDefineFromFPA (config, input->fpa, DX, DY, "PSPHOT.BACKMDL");
+    }
+    // optionally save the full background image
+    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKGND")) {
+	pmFPAfileDefineFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKGND");
+    }
+    // optionally save the background-subtracted image
+    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKSUB")) {
+	pmFPAfileDefineFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKSUB");
+    }
+    // optionally save the PSF Model
+    if (psMetadataLookupBool(NULL, recipe, "SAVE.PSF")) {
+	pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.PSF.SAVE");
+    }
+    // optionally load the PSF Model
+    if (psMetadataLookupBool(NULL, recipe, "LOAD.PSF")) {
+	pmFPAfileDefineInput (config, input->fpa, "PSPHOT.PSF.LOAD");
+    }
 
+    // XXX not tested (or defined?)
+    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.PSF_SAMPLE");
 
+    // view->cell = -1;
     // iterate over the cells in the current chip
-    // view->cell = -1;
-
     while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
 	psLogMsg ("ppImagePhot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
 	if (! cell->process || ! cell->file_exists) { continue; }
-	pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
 
 	// process each of the readouts
 	while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
-	    pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
 	    if (! readout->data_exists) { continue; }
 
 	    // run the actual photometry analysis
 	    psphotReadout (config, view);
+	}
+    }
+    return true;
+}
 
-	    pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
-	}
-	pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
-    }
+    // we only want to operate on PSPHOT pmFPAfiles here:
+    // XXX is this necessary?  why invoke I/O during this function?
+    // the psphot constructed files, if chosen, will be saved at the chip level
+    // in the next call outside of this function.
+    // XXX there could be an issue if those files are set for 'cell' or 'readout'
+    // pmFPAfileActivate (config->files, false, NULL);
+    // pmFPAfileActivate (config->files, true, "PSPHOT.INPUT");
+    // pmFPAfileActivate (config->files, true, "PSPHOT.RESID");
+    // pmFPAfileActivate (config->files, true, "PSPHOT.OUTPUT");
+    // pmFPAfileActivate (config->files, true, "PSPHOT.BACKSUB");
+    // pmFPAfileActivate (config->files, true, "PSPHOT.BACKGND");
+    // pmFPAfileActivate (config->files, true, "PSPHOT.BACKMDL");
+
+
 
     // de-activate the PSPHOT image files, activate the PPIMAGE ones
+    # if 0
     pmFPAfileActivate (config->files, false, NULL);
     pmFPAfileActivate (config->files, true, "PPIMAGE.INPUT");
@@ -85,10 +109,8 @@
     pmFPAfileActivate (config->files, true, "PPIMAGE.FLAT");
     pmFPAfileActivate (config->files, true, "PPIMAGE.OUTPUT");
-
     pmFPAfileActivate (config->files, true, "PPIMAGE.BIN1");
     pmFPAfileActivate (config->files, true, "PPIMAGE.JPEG1");
     pmFPAfileActivate (config->files, true, "PPIMAGE.BIN2");
     pmFPAfileActivate (config->files, true, "PPIMAGE.JPEG2");
+    # endif
 
-    return true;
-}
