Index: /branches/eam_branches/20090820/ppImage/src/ppImage.h
===================================================================
--- /branches/eam_branches/20090820/ppImage/src/ppImage.h	(revision 25159)
+++ /branches/eam_branches/20090820/ppImage/src/ppImage.h	(revision 25160)
@@ -130,5 +130,5 @@
 
 // apply the cell flips to the input data before analysis
-bool ppImageParityFlip (pmConfig *config, const ppImageOptions *options, const pmFPAview *view);
+bool ppImageParityFlip (pmConfig *config, const ppImageOptions *options, const pmFPAview *view, bool native);
 
 // Loop over the input
Index: /branches/eam_branches/20090820/ppImage/src/ppImageLoop.c
===================================================================
--- /branches/eam_branches/20090820/ppImage/src/ppImageLoop.c	(revision 25159)
+++ /branches/eam_branches/20090820/ppImage/src/ppImageLoop.c	(revision 25160)
@@ -99,6 +99,6 @@
                 }
 
-                // perform the detrend analysis
-                if (!ppImageParityFlip(config, options, view)) {
+                // flip the image to match the native detector orientation (to match bias, flat, etc)
+                if (!ppImageParityFlip(config, options, view, true)) {
                     ESCAPE("Unable to detrend readout");
                 }
@@ -132,4 +132,17 @@
                 ppImageDetrendRecord(cell, config, options, view);
             }
+
+            // process each of the readouts
+	    // XXX reset the view to the first readout?
+            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+                if (!readout->data_exists) {
+                    continue;
+                }
+                // flip the image to match the raw readout orientation
+                if (!ppImageParityFlip(config, options, view, false)) {
+                    ESCAPE("Unable to detrend readout");
+                }
+	    }
+
             // free detrend images potentially in use: MASK, BIAS, DARK, SHUTTER, FLAT
             if (!ppImageDetrendFree (config, view)) {
Index: /branches/eam_branches/20090820/ppImage/src/ppImageParityFlip.c
===================================================================
--- /branches/eam_branches/20090820/ppImage/src/ppImageParityFlip.c	(revision 25159)
+++ /branches/eam_branches/20090820/ppImage/src/ppImageParityFlip.c	(revision 25160)
@@ -10,7 +10,11 @@
 item->data.TYPE = VALUE; }
 
-bool ppImageParityFlip (pmConfig *config, const ppImageOptions *options, const pmFPAview *view) {
+// flip the image to have 'native' (detector) or 'raw' (readout) orientation 
+bool ppImageParityFlip (pmConfig *config, const ppImageOptions *options, const pmFPAview *view, bool native) {
 
     bool status;
+    int xParity, yParity;
+    int xParityRaw, yParityRaw;
+    int xParityTarget, yParityTarget;
 
     if (!options->applyParity) return true;
@@ -21,20 +25,47 @@
     pmCell *cell = readout->parent;
 
-    int xParity = psMetadataLookupS32(&status, cell->concepts, "CELL.XPARITY");
-    if (!status || (xParity != -1 && xParity != 1)) {
-        psWarning("CELL.XPARITY is not set for the input cell; assuming 1.\n");
-        FIX_CONCEPT(cell->concepts, "CELL.XPARITY", S32, 1);
-        xParity = 1;
-    }
-    int yParity = psMetadataLookupS32(&status, cell->concepts, "CELL.YPARITY");
-    if (!status || (yParity != -1 && yParity != 1)) {
-        psWarning("CELL.YPARITY is not set for the input cell; assuming 1.\n");
-        FIX_CONCEPT(cell->concepts, "CELL.YPARITY", S32, 1);
-        yParity = 1;
+    if (native) {
+	// find the current (raw) parity
+	xParity = psMetadataLookupS32(&status, cell->concepts, "CELL.XPARITY");
+	if (!status || (xParity != -1 && xParity != 1)) {
+	    psWarning("CELL.XPARITY is not set for the input cell; assuming 1.\n");
+	    FIX_CONCEPT(cell->concepts, "CELL.XPARITY", S32, 1);
+	    xParity = 1;
+	}
+	yParity = psMetadataLookupS32(&status, cell->concepts, "CELL.YPARITY");
+	if (!status || (yParity != -1 && yParity != 1)) {
+	    psWarning("CELL.YPARITY is not set for the input cell; assuming 1.\n");
+	    FIX_CONCEPT(cell->concepts, "CELL.YPARITY", S32, 1);
+	    yParity = 1;
+	}
+
+	// save the raw parity
+	psMetadataAddS32 (cell->concepts, PS_LIST_TAIL, "CELL.XPARITY.RAW", PS_META_REPLACE, "original parity", xParity);
+	psMetadataAddS32 (cell->concepts, PS_LIST_TAIL, "CELL.YPARITY.RAW", PS_META_REPLACE, "original parity", yParity);
+
+	xParityTarget = 1;
+	yParityTarget = 1;
+    } else {
+	// find the current (native) parity
+	xParity = psMetadataLookupS32(&status, cell->concepts, "CELL.XPARITY");
+	psAssert (status, "CELL.XPARITY not set : cannot call ppImageParityFlip (native = false) without first calling it with native = true");
+
+	yParity = psMetadataLookupS32(&status, cell->concepts, "CELL.YPARITY");
+	psAssert (status, "CELL.YPARITY not set : cannot call ppImageParityFlip (native = false) without first calling it with native = true");
+
+	// find the raw parity
+	xParityRaw = psMetadataLookupS32(&status, cell->concepts, "CELL.XPARITY.RAW");
+	psAssert (status, "CELL.XPARITY not set : cannot call ppImageParityFlip (native = false) without first calling it with native = true");
+
+	yParityRaw = psMetadataLookupS32(&status, cell->concepts, "CELL.YPARITY.RAW");
+	psAssert (status, "CELL.YPARITY not set : cannot call ppImageParityFlip (native = false) without first calling it with native = true");
+
+	xParityTarget = xParityRaw;
+	yParityTarget = yParityRaw;
     }
 
     // for this case, nothing to be done:
-    if ((xParity == 1) && (yParity == 1)) return true;
-    
+    if ((xParity == xParityTarget) && (yParity == yParityTarget)) return true;
+
     psImage *image = readout->image;
     psImage *var   = readout->variance;
@@ -55,5 +86,7 @@
 
     // the three cases (+1,-1), (-1,+1), (-1,-1) should be handled independently
-    if ((xParity == -1) && (yParity == +1)) {
+
+    // flip only in x-direction
+    if ((xParity != xParityTarget) && (yParity == yParityTarget)) {
 	for (int iy = 0; iy < Ny; iy++) {
 	    for (int ix = 0; ix < Nx; ix++) {
@@ -71,5 +104,6 @@
 	}
     }
-    if ((xParity == +1) && (yParity == -1)) {
+    // flip only in y-direction
+    if ((xParity == xParityTarget) && (yParity != yParityTarget)) {
 	for (int iy = 0; iy < Ny/2; iy++) {
 	    memcpy (imrow, image->data.F32[iy], Nx*sizeof(psF32));
@@ -90,5 +124,6 @@
 	}
     }
-    if ((xParity == -1) && (yParity == -1)) {
+    // flip in both directions
+    if ((xParity != xParityTarget) && (yParity == yParityTarget)) {
 	for (int iy = 0; iy < Ny/2; iy++) {
 	    for (int ix = 0; ix < Nx; ix++) {
@@ -118,6 +153,6 @@
     }
 
-    // FIX_CONCEPT(cell->concepts, "CELL.XPARITY", S32, 1);
-    // FIX_CONCEPT(cell->concepts, "CELL.YPARITY", S32, 1);
+    FIX_CONCEPT(cell->concepts, "CELL.XPARITY", S32, xParityTarget);
+    FIX_CONCEPT(cell->concepts, "CELL.YPARITY", S32, yParityTarget);
 
     psFree (imrow);
