Index: trunk/psphot/src/psphotImageLoop.c
===================================================================
--- trunk/psphot/src/psphotImageLoop.c	(revision 11310)
+++ trunk/psphot/src/psphotImageLoop.c	(revision 12587)
@@ -1,6 +1,4 @@
 # include "psphot.h"
 
-// XXX the errors in the pmFPAfileIOChecks could also be due to a programming or config error
-// XXX we need to either handle those errors or handle the error in pmFPAfileIOChecks and exit
 bool psphotImageLoop (pmConfig *config) {
 
@@ -10,4 +8,9 @@
     pmReadout *readout;
 
+    pmFPAfile *load = psMetadataLookupPtr (&status, config->files, "PSPHOT.LOAD");
+    if (!status) {
+	psError(PSPHOT_ERR_PROG, false, "Can't find input data!");
+	return false;
+    }
     pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSPHOT.INPUT");
     if (!status) {
@@ -25,7 +28,14 @@
     }
 
-    while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
+    // for psphot, we force data to be read at the chip level 
+    while ((chip = pmFPAviewNextChip (view, load->fpa, 1)) != NULL) {
         psLogMsg ("psphot", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
         if (! chip->process || ! chip->file_exists) { continue; }
+
+	// load just the input image data (image, mask, weight)
+	pmFPAfileActivate (config->files, false, NULL);
+	pmFPAfileActivate (config->files, true, "PSPHOT.LOAD");
+	pmFPAfileActivate (config->files, true, "PSPHOT.MASK");
+	pmFPAfileActivate (config->files, true, "PSPHOT.WEIGHT");
 	if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
             psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d in psphot\n", view->chip);
@@ -33,25 +43,34 @@
 	    return false;
 	}
+	pmFPAfileActivate (config->files, true, NULL);
 	
+	// mosaic the cells of a chip into a single contiguous chip.
+	// this probably needs to return a new fpa? 
+        if (!psphotMosaicChip(config, view, "PSPHOT.INPUT", "PSPHOT.LOAD")) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to mosaic chip.\n");
+            return false;
+        }
+
+	// try to load the data
+	if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
+            psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d in psphot\n", view->chip);
+	    psFree (view);
+	    return false;
+	}
+
+	// there is now only a single chip (multiple readouts?). loop over it and process
+	// XXX activate / de-activate files? no I/O here?
 	while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
             psLogMsg ("psphot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
-            if (! cell->process || ! cell->file_exists) { continue; }
-	    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
-		psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d, cell %d in psphot\n", view->chip, view->cell);
-		psFree (view);
-		return false;
-	    }
+            // if (! cell->process || ! cell->file_exists) { continue; }
+            if (! cell->process) { continue; }
 	    
 	    // process each of the readouts
 	    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
-		if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
-		    psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d, cell %d, readout %d in psphot\n", view->chip, view->cell, view->readout);
-		    psFree (view);
-		    return false;
-		}
-		
+		psLogMsg ("psphot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
 		if (! readout->data_exists) { continue; }
-		
+
 		// run the actual photometry analysis
+		// XXX calling psphotReadout here will operate on the mosaic'ed chips (for each readout)
 		if (!psphotReadout (config, view)) {
 		    psError(psErrorCodeLast(), false, "failure in psphotReadout for chip %d, cell %d, readout %d\n", view->chip, view->cell, view->readout);
@@ -59,17 +78,8 @@
 		    return false;
 		}
-
-		if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
-		    psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d, cell %d, readout %d in psphot\n", view->chip, view->cell, view->readout);
-		    psFree (view);
-		    return false;
-		}
-	    }
-	    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
-		psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d, cell %d in psphot\n", view->chip, view->cell);
-		psFree (view);
-		return false;
 	    }
 	}
+
+	// save output which is saved at the chip level
 	if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
             psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d in psphot\n", view->chip);
@@ -78,4 +88,5 @@
 	}
     }
+    // save output which is saved at the fpa level
     if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
 	psError(PSPHOT_ERR_DATA, false, "failed IO for fpa in psphot\n");
@@ -101,2 +112,5 @@
 // PSPHOT.PSF     : sample PSF images
 
+// PSPHOT.MASK
+// PSPHOT.WEIGHT
+// 
