Index: trunk/psphot/src/psphotImageLoop.c
===================================================================
--- trunk/psphot/src/psphotImageLoop.c	(revision 6522)
+++ trunk/psphot/src/psphotImageLoop.c	(revision 6715)
@@ -1,49 +1,99 @@
 # include "psphot.h"
 
-bool psphotImageLoop (psphotData *data, ppConfig *config) {
+// XXX where do we load optional mask and weight input images?
 
-    pmFPA *fpa = data->input->fpa;
+bool psphotImageLoop (pmConfig *config) {
 
-    psphotDataIO (data, config, -1, -1);
+    bool status;
+    pmChip *chip;
+    pmCell *cell;
+    pmReadout *readout;
 
-    for (int i = 0; i < fpa->chips->n; i++) {
-        pmChip *chip = fpa->chips->data[i]; // Chip of interest in input image
+    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSPHOT.INPUT");
+    if (!status) {
+	psErrorStackPrint(stderr, "Can't find input data!\n");
+	exit(EXIT_FAILURE);
+    }
 
-        psLogMsg ("psphot", 4, "Chip %d: %x %x\n", i, chip->exists, chip->process);
+    pmFPAview *view = pmFPAviewAlloc (0);
+
+    // files associated with the science image
+    pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+
+    while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
+        psLogMsg ("psphot", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
         if (! chip->process) { continue; }
+        if (! chip->file_exists) { continue; }
+	pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
 
-	psphotDataIO (data, config, i, -1);
-
-        for (int j = 0; j < chip->cells->n; j++) {
-            pmCell *cell = chip->cells->data[j]; // Cell of interest in input image
-
-            psLogMsg ("psphot", 4, "Cell %d: %x %x\n", j, cell->exists, cell->process);
+	while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
+	    continue;
+            psLogMsg ("psphot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
             if (! cell->process) { continue; }
-
-	    psphotDataIO (data, config, i, j);
-
-	    // XXX optional mask and weight input image should be loaded here?
-	    // this sets the weight map and basic mask applying CELL.BAD and CELL.SATURATION
-	    pmCellSetWeights(cell);
-
-	    // I have a valid mask, now mask in the analysis region of interest
-	    pmCellSetMask (cell, config->recipe); 
+	    if (! cell->file_exists) { continue; }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
 
 	    // process each of the readouts
-	    for (int k = 0; k < cell->readouts->n; k++) {
-		pmReadout *readout = cell->readouts->data[k]; // Readout of interest in input image
+	    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+		pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+		if (! readout->data_exists) { continue; }
 
 		// run a single-model test if desired
-		psphotModelTest (readout, config->arguments, config->recipe);
+		// XXX move this to psphotReadout??
+		// psphotModelTest (readout, recipe);
 
 		// run the actual photometry analysis
-		psphotReadout (readout, data, config);
+		psphotReadout (config, view);
 
-		// XXX what do we do if we have multiple readouts?
-		psphotOutput (readout, data, config);
+		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+
+		// write out the desired output dataset(s)
+		// psphotOutput (view, recipe);
 	    }
-        }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	}
+	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
     }
-    psphotOutputClose (data);
+    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+
+    psFree (view);
     return true;
 }
+
+// I/O files related to psphot:
+// PSPHOT.INPUT   : input image file(s)
+// PSPHOT.RESID   : residual image
+// PSPHOT.OUTPUT  : output object tables (object)
+
+// PSPHOT.BACKSUB : background subtracted image
+// PSPHOT.BACKGND : background model (full-scale image?)
+// PSPHOT.BACKMDL : background model (binned image?)
+// PSPHOT.PSF     : sample PSF images
+
+
+/**
+
+filename | pmFPAfile  | pmFPA  | pmFPAview
+chip00.f | PSPHOT.IN  | input  | view
+chip01.f | PSPHOT.IN  | input  | view
+chip02.f | PSPHOT.IN  | input  | view
+chip03.f | PSPHOT.IN  | input  | view
+
+out00.f  | PSPHOT.OUT | input  | view
+out01.f  | PSPHOT.OUT | input  | view
+out02.f  | PSPHOT.OUT | input  | view
+out03.f  | PSPHOT.OUT | input  | view
+
+obj00.f  | PSPHOT.OBJ | input  | view
+obj01.f  | PSPHOT.OBJ | input  | view
+obj02.f  | PSPHOT.OBJ | input  | view
+obj03.f  | PSPHOT.OBJ | input  | view
+
+bin00.f  | PSPHOT.BIN | binned | view
+bin01.f  | PSPHOT.BIN | binned | view
+bin02.f  | PSPHOT.BIN | binned | view
+bin03.f  | PSPHOT.BIN | binned | view
+
+mosaic.f | PSPHOT.MOS | mosaic | view
+
+**/
