Index: trunk/psastro/src/psastroDataLoop.c
===================================================================
--- trunk/psastro/src/psastroDataLoop.c	(revision 6791)
+++ trunk/psastro/src/psastroDataLoop.c	(revision 6792)
@@ -1,64 +1,43 @@
 # include "psastro.h"
 
-bool psastroDataLoop (ppFile *file, ppConfig *config) {
+// load the data from the files in this loop.
+// we write out the result in a second loop
+// at the end of this function, the complete stellar data is loaded
+// into the correct fpa structure locations (readout.analysis:PSPHOT.SOURCES)
+bool psastroDataLoop (pmConfig *config) {
 
-    bool status;
-    ppImageLoadDepth imageLoadDepth;
-
-    // determine the load depth
-    const char *depth = psMetadataLookupStr(&status, config->recipe, "LOAD.DEPTH");
-    if (! status || ! depth || strlen(depth) == 0) {
-        psLogMsg("psastro", PS_LOG_ERROR, "LOAD.DEPTH not specified in recipe.");
-        exit(EXIT_FAILURE);
-    }
-    imageLoadDepth = PP_LOAD_NONE;
-    if (!strcasecmp(depth, "FPA")) imageLoadDepth = PP_LOAD_FPA;
-    if (!strcasecmp(depth, "CHIP")) imageLoadDepth = PP_LOAD_CHIP;
-    if (!strcasecmp(depth, "CELL")) imageLoadDepth = PP_LOAD_CELL;
-    if (imageLoadDepth == PP_LOAD_NONE) {
-        psLogMsg(__func__, PS_LOG_ERROR, "LOAD.DEPTH in recipe is not FPA, CHIP or CELL.");
-        exit(EXIT_FAILURE);
+    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSASTRO.INPUT");
+    if (!status) {
+	psErrorStackPrint(stderr, "Can't find input data!\n");
+	exit(EXIT_FAILURE);
     }
 
-    if (imageLoadDepth == PP_LOAD_FPA) {
-        psTrace(__func__, 1, "data pixels for FPA...\n");
-        psastroLoadSMF(file, config->database, -1, -1);
+    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 || !chip->file_exists) { continue; }
+	pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+
+	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; }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+
+	    // process each of the readouts
+	    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+		pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+		if (! readout->data_exists) { continue; }
+
+		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	    }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	}
+	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
     }
-
-    for (int i = 0; i < file->fpa->chips->n; i++) {
-        pmChip *chip = file->fpa->chips->data[i]; // Chip of interest in input image
-
-        psLogMsg ("psastro", 4, "Chip %d: %x %x\n", i, chip->exists, chip->process);
-        if (! chip->process) { continue; }
-
-	if (imageLoadDepth == PP_LOAD_CHIP) {
-            psTrace(__func__, 1, "Loading pixels for chip %d...\n", i);
-	    psastroLoadSMF(file, config->database, i, -1);
-        }
-
-        for (int j = 0; j < chip->cells->n; j++) {
-            pmCell *cell = chip->cells->data[j]; // Cell of interest in input image
-
-            psLogMsg ("psastro", 4, "Cell %d: %x %x\n", j, cell->exists, cell->process);
-            if (! cell->process) { continue; }
-
-            if (imageLoadDepth == PP_LOAD_CELL) {
-                psTrace(__func__, 1, "Loading pixels for chip %d, cell %d...\n", i, j);
-		psastroLoadSMF(file, config->database, i, j);
-            }
-	    
-	    // 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
-
-		// run a single-model test if desired
-		psphotModelTest (readout, config->arguments, config->recipe);
-		psphotReadout (readout, config->recipe);
-		psphotOutput (readout, config->arguments);
-	    }
-        }
-    }
+    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
     return true;
 }
-
-/* this is currently written only for single-chip analysis */
