Index: trunk/ppImage/src/ppImageDetrendCell.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendCell.c	(revision 6747)
+++ trunk/ppImage/src/ppImageDetrendCell.c	(revision 6817)
@@ -1,63 +1,62 @@
-#include <stdio.h>
-#include "pslib.h"
-#include "psmodules.h"
-#include "ppImageDetrend.h"
-#include "ppImageOptions.h"
+#include "ppImage.h"
 
-// mask, bias, dark, flat are defined per Cell
+bool ppImageDetrendReadout (ppImageOptions *options, pmConfig *config, pmFPAview *view) {
 
-pmReadout *ppImageDetrendSelectFirst(pmCell *cell, char *name, bool doThis)
-{
-    if (!doThis) {
-        return NULL;
+    bool status;
+    pmFPAfile *file;
+
+    // construct a view for the detrend images (which have only one readout)
+    pmFPAview *detview = pmFPAviewAlloc (0);
+    *detview = *view;
+    detview->readout = 0;
+
+    // find the currently selected readout
+    file = psMetadataLookupPtr (&status, config->files, "PPIMAGE.INPUT");
+    pmReadout *input = pmFPAviewThisReadout (view, file->fpa);
+
+    pmReadoutSetWeights (input);
+
+    // Mask bad pixels
+    if (options->doMask) {
+	file = psMetadataLookupPtr (&status, config->files, "PPIMAGE.MASK");
+	pmReadout *mask = pmFPAviewThisReadout (detview, file->fpa);
+	ppImageDetrendMask(input, mask);
     }
 
-    if (cell->readouts->n > 1) {
-        psLogMsg("phase2", PS_LOG_WARN, "%s contains multiple readouts: only the first will be used.", name);
+# if 0
+    // Non-linearity correction
+    if (options->doNonLin) {
+	ppImageDetrendNonLinear(detrend->input, input, options);
+    }
+# endif
+
+    // set up the readouts for dark and bias
+    pmReadout  *dark = NULL;
+    pmReadout  *bias = NULL;
+    if (options->doBias) {
+	file   = psMetadataLookupPtr (&status, config->files, "PPIMAGE.BIAS");
+	bias = pmFPAviewThisReadout (detview, file->fpa);
+    }
+    if (options->doDark) {
+	file   = psMetadataLookupPtr (&status, config->files, "PPIMAGE.DARK");
+	dark = pmFPAviewThisReadout (detview, file->fpa);
     }
 
-    pmReadout *readout = cell->readouts->data[0]; // Readout of interest in this cell
+    // Bias, dark and overscan subtraction are all merged.
+    ppImageDetrendBias(input, bias, dark, options);
 
-    return readout;
+    // Flat-field correction (no options used?)
+    if (options->doFlat) {
+	file = psMetadataLookupPtr (&status, config->files, "PPIMAGE.FLAT");
+	pmReadout *flat = pmFPAviewThisReadout (detview, file->fpa);
+	pmFlatField(input, flat);
+    }
+
+    psFree (detview);
+    return true;
 }
 
-bool ppImageDetrendCell(ppImageDetrend *detrend, ppImageOptions *options, pmConfig *config)
-{
-#if 0
-    pmCellSetWeights(detrend->input);
-#endif
+    // XXX in psphot, the recipe options are parsed at this depth
+    // XXX consider if we move ppImageOptions in here
+    // psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, "PSPHOT");
 
-    pmReadout *mask = ppImageDetrendSelectFirst(detrend->mask, "mask", options->doMask);
-    pmReadout *bias = ppImageDetrendSelectFirst(detrend->bias, "bias", options->doBias);
-    pmReadout *dark = ppImageDetrendSelectFirst(detrend->dark, "dark", options->doDark);
-    pmReadout *flat = ppImageDetrendSelectFirst(detrend->flat, "flat", options->doFlat);
-
-#if 0
-    printf("Flat type: %x\n", flat->image->type.type);
-    printf("Flat value: %f\n", flat->image->data.F32[flat->image->numRows/2][flat->image->numCols/2]);
-#endif
-
-    for (int k = 0; k < detrend->input->readouts->n; k++) {
-
-        pmReadout *input = detrend->input->readouts->data[k]; // Readout of interest in input image
-
-        // Mask bad pixels
-        if (options->doMask) {
-            ppImageDetrendMask(detrend->input, input, mask);
-        }
-
-        // Non-linearity correction
-        if (options->doNonLin) {
-            ppImageDetrendNonLinear(detrend->input, input, options);
-        }
-
-        // Bias, dark and overscan subtraction are all merged.
-        ppImageDetrendBias(input, bias, dark, options);
-
-        // Flat-field correction (no options used?)
-        if (options->doFlat) {
-            pmFlatField(input, flat);
-        }
-    }
-    return true;
-}
