Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 5857)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 5858)
@@ -1,39 +1,52 @@
 # include "ppImage.h"
 
-bool ppImageLoop (ppData *data, ppRecipe *options, ppConfig *config) {
+bool ppImageLoop (ppData *data, ppOptions *options, ppConfig *config) {
 
-    psArray *inputChips = data->input->chips; // Array of chips in input image
-    psArray *biasChips 	= data->bias->chips;  // Array of chips in bias image
-    psArray *darkChips 	= data->dark->chips;  // Array of chips in dark image
-    psArray *maskChips 	= data->mask->chips;  // Array of chips in mask image
-    psArray *flatChips 	= data->flat->chips;  // Array of chips in flat image
+    ppDetrend detrend;
 
-    for (int i = 0; i < inputChips->n; i++) {
-        pmChip *inputChip = inputChips->data[i]; // Chip of interest in input image
-        pmChip *biasChip  = biasChips->data[i];  // Chip of interest in bias image
-        pmChip *darkChip  = darkChips->data[i];	 // Chip of interest in dark image
-        pmChip *maskChip  = maskChips->data[i];	 // Chip of interest in mask image
-        pmChip *flatChip  = flatChips->data[i];  // Chip of interest in flat image
+    if (options->imageLoadDepth == PP_LOAD_FPA) {
+	ppImageLoadPixels (data->input, data->process, config->database, -1, -1);
+	ppImageLoadPixels (data->bias, 	data->process, config->database, -1, -1);
+	ppImageLoadPixels (data->dark, 	data->process, config->database, -1, -1);
+	ppImageLoadPixels (data->mask, 	data->process, config->database, -1, -1);
+	ppImageLoadPixels (data->flat, 	data->process, config->database, -1, -1);
+    }
+    for (int i = 0; i < data->input->fpa->chips->n; i++) {
+        pmChip *inputChip = data->input->fpa->chips->data[i]; // Chip of interest in input image
+        pmChip *biasChip  = data->bias->fpa->chips->data[i];  // Chip of interest in bias image
+        pmChip *darkChip  = data->dark->fpa->chips->data[i];  // Chip of interest in dark image
+        pmChip *maskChip  = data->mask->fpa->chips->data[i];  // Chip of interest in mask image
+        pmChip *flatChip  = data->flat->fpa->chips->data[i];  // Chip of interest in flat image
 
         if (! inputChip->valid) { continue; }
 
-        psArray *inputCells = inputChip->cells; // Array of cells in input image
-        psArray *biasCells  = biasChip->cells; 	// Array of cells in bias image
-        psArray *darkCells  = darkChip->cells; 	// Array of cells in dark image
-        psArray *maskCells  = maskChip->cells; 	// Array of cells in mask image
-        psArray *flatCells  = flatChip->cells; 	// Array of cells in flat image
+	if (options->imageLoadDepth == PP_LOAD_CHIP) {
+	    ppImageLoadPixels (data->input, data->process, config->database, i, -1);
+	    ppImageLoadPixels (data->bias,  data->process, config->database, i, -1);
+	    ppImageLoadPixels (data->dark,  data->process, config->database, i, -1);
+	    ppImageLoadPixels (data->mask,  data->process, config->database, i, -1);
+	    ppImageLoadPixels (data->flat,  data->process, config->database, i, -1);
+	}
+        for (int j = 0; j < inputChip->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
 
-        for (int j = 0; j < inputCells->n; j++) {
-            pmCell *inputCell = inputCells->data[j]; // Cell of interest in input image
-            pmCell *biasCell  = biasCells->data[j];  // Cell of interest in bias image
-            pmCell *darkCell  = darkCells->data[j];  // Cell of interest in dark imag
-            pmCell *maskCell  = maskCells->data[j];  // Cell of interest in mask image
-            pmCell *flatCell  = flatCells->data[j];  // Cell of interest in flat image
+            if (! detrend.input->valid) { continue; }
 
-            if (! inputCell->valid) { continue; }
-
-	    ppReadoutDetrend (inputCell, maskCell, biasCell, darkCell, flatCell);
+	    if (options->imageLoadDepth == PP_LOAD_CELL) {
+		ppImageLoadPixels (data->input, data->process, config->database, i, j);
+		ppImageLoadPixels (data->bias,  data->process, config->database, i, j);
+		ppImageLoadPixels (data->dark,  data->process, config->database, i, j);
+		ppImageLoadPixels (data->mask,  data->process, config->database, i, j);
+		ppImageLoadPixels (data->flat,  data->process, config->database, i, j);
+	    }
+	    ppDetrendCell (&detrend, options, config);
 	}
     }
     return true;
 }
+
+/* if we allow per-readout loads, we need to load detrend images here */
