Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 6775)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 6817)
@@ -1,141 +1,68 @@
-#include <stdio.h>
-#include "pslib.h"
-#include "psmodules.h"
-#include "ppImageDetrend.h"
-#include "ppImage.h"
-#include "ppMem.h"
+# include "ppImage.h"
 
-#define MEM_LEAKS 1
+bool ppImageLoop (ppImageOptions *options, pmConfig *config) {
 
-// Read the entire FPA
-void readFPA(ppFile *file,              // File to read
-             pmConfig *config           // Configuration, containing the DB handle
-    )
-{
-    if (file->fpa && file->fits) {
-        pmFPARead(file->fpa, file->fits, config->database);
+    bool status;
+    pmChip *chip;
+    pmCell *cell;
+    pmReadout *readout;
+
+    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PPIMAGE.INPUT");
+    if (!status) {
+	psErrorStackPrint(stderr, "Can't find input data!\n");
+	exit(EXIT_FAILURE);
     }
+
+    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; }
+
+		// perform the detrend analysis
+		ppImageDetrendReadout (options, config, view);
+
+		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	    }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	}
+
+	// ppImageChipMosaic (config, view);
+	// ppImagePSPhot (config, view);
+	// ppImagePSAstro (config, view);
+
+	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+
+	// XXX paul added these frees: make sure the same is happening in the pmFileIOChecks
+        // Now I can blow away the mosaic so I can then read more.
+        // psFree(inputChip);
+        // psFree(biasChip);
+        // psFree(darkChip);
+        // psFree(maskChip);
+        // psFree(flatChip);
+        // ppMemUsed();
+    }
+
+    // ppImageFPAMosaic (config, view);
+    // ppImageJpegFPA (config, view);
+
+    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+
+    psFree (view);
+    return true;
 }
 
-// Read a specified chip only
-void readChip(ppFile *file,             // File to read
-              pmConfig *config,         // Configuration
-              int chipNum               // Chip number to read
-    )
-{
-    pmFPA *fpa = file->fpa;             // The FPA
-    if (fpa && file->fits) {
-        pmChip *chip = fpa->chips->data[chipNum]; // The chip
-        pmChipRead(chip, file->fits, config->database);
-    }
-}
-
-
-void readCell(ppFile *file, pmConfig *config, int chipNum, int cellNum)
-{
-    pmFPA *fpa = file->fpa;             // The FPA
-    if (fpa && file->fits) {
-        pmChip *chip = fpa->chips->data[chipNum]; // The chip
-        if (chip) {
-            pmCell *cell = chip->cells->data[cellNum]; // The cell
-            pmCellRead(cell, file->fits, config->database);
-        }
-    }
-}
-
-
-bool ppImageLoop(ppImageData *data, ppImageOptions *options, pmConfig *config)
-{
-    ppImageDetrend detrend;
-
-    int processChip = psMetadataLookupS32(NULL, config->arguments, "-chip"); // Chip number to process or -1
-
-    // Load at FPA level if requested
-    if (processChip < 0 && options->imageLoadDepth == PP_LOAD_FPA) {
-        psTrace(__func__, 1, "Loading pixels for FPA...\n");
-        readFPA(data->input, config);
-        readFPA(data->bias,  config);
-        readFPA(data->dark,  config);
-        readFPA(data->mask,  config);
-        readFPA(data->flat,  config);
-    }
-
-    psArray *chips = data->input->fpa->chips; // Component chips
-    for (int i = 0; i < chips->n; i++) {
-
-        if (processChip >= 0 && i != processChip) {
-            continue;
-        }
-        if (options->imageLoadDepth == PP_LOAD_CHIP) {
-            psTrace(__func__, 1, "Loading pixels for chip %d...\n", i);
-            readChip(data->input, config, i);
-            readChip(data->bias,  config, i);
-            readChip(data->dark,  config, i);
-            readChip(data->mask,  config, i);
-            readChip(data->flat,  config, i);
-        }
-
-        pmChip *inputChip = chips->data[i];
-        pmChip *biasChip  = data->bias->fpa->chips->data[i];
-        pmChip *darkChip  = data->dark->fpa->chips->data[i];
-        pmChip *maskChip  = data->mask->fpa->chips->data[i];
-        pmChip *flatChip  = data->flat->fpa->chips->data[i];
-
-        psArray *cells = inputChip->cells;
-        for (int j = 0; j < cells->n; j++) {
-            detrend.input = inputChip->cells->data[j]; // Cell of interest in input image
-            detrend.bias  = biasChip->cells->data[j];  // Cell of interest in bias image
-            detrend.dark  = darkChip->cells->data[j];  // Cell of interest in dark imag
-            detrend.mask  = maskChip->cells->data[j];  // Cell of interest in mask image
-            detrend.flat  = flatChip->cells->data[j];  // Cell of interest in flat image
-
-            if (! detrend.input->process) { continue; }
-
-            if (options->imageLoadDepth == PP_LOAD_CELL) {
-                psTrace(__func__, 1, "Loading pixels for chip %d, cell %d...\n", i, j);
-                readCell(data->input, config, i, j);
-                readCell(data->bias,  config, i, j);
-                readCell(data->dark,  config, i, j);
-                readCell(data->mask,  config, i, j);
-                readCell(data->flat,  config, i, j);
-            }
-
-            ppImageDetrendCell(&detrend, options, config);
-
-            // Need to free detrend cells here so we have enough memory to do other stuff
-            psFree(detrend.bias);
-            psFree(detrend.dark);
-            psFree(detrend.mask);
-            psFree(detrend.flat);
-        }
-
-#if 0
-        int numMosaicked = pmChipMosaic(inputChip, 1, 1); // Number of cells mosaicked together
-        psLogMsg(__func__, PS_LOG_INFO, "%d cells mosaicked.\n", numMosaicked);
-
-        // XXX A kludge to get the write to behave w.r.t. the concepts --- we've changed the camera format, so
-        // the concepts don't know what on earth to do.
-        const psMetadata *camera = data->input->fpa->camera;
-        data->input->fpa->camera = NULL;
-#endif
-
-        // XXX Photometry goes here!
-
-        pmChipWrite(inputChip, data->output, config->database);
-
-#if 0
-        data->input->fpa->camera = camera;
-#endif
-
-        // Now I can blow away the mosaic so I can then read more.
-        psFree(inputChip);
-        psFree(biasChip);
-        psFree(darkChip);
-        psFree(maskChip);
-        psFree(flatChip);
-
-        ppMemUsed();
-    }
-
-    return true;
-}
