Index: /trunk/psModules/src/camera/pmFPARead.c
===================================================================
--- /trunk/psModules/src/camera/pmFPARead.c	(revision 13767)
+++ /trunk/psModules/src/camera/pmFPARead.c	(revision 13768)
@@ -120,8 +120,12 @@
 }
 
-// Read a component of a readout
+// Read a component of a readout.  We read in only the rows from min to max for plane z, for
+// the full region requested.  if we request a range outside the region, we will pad to fill
+// out the edges of the region with 'bad' pixels.  The output image always has max-min rows.
+// The region represents the maximum bounds of the full image
+
 psImage *readoutReadComponent(psImage *image, // Image into which to read
                               psFits *fits, // FITS file from which to read
-                              const psRegion *region, // Region to read
+                              const psRegion *fullImage, // full image region, read a subset
                               int readdir, // Read direction (1=rows, 2=cols)
                               int min,  // Minimum row/col number to read
@@ -132,37 +136,36 @@
 {
     assert(fits);
-    assert(region);
-
-    bool resize = false;                // Do we need to resize the image once read?
-    psRegion toRead = psRegionSet(region->x0, region->x1, region->y0, region->y1); // Region to read
-    psRegion fullSize = toRead;         // Full sized region
+    assert(fullImage);
+    assert((readdir == 1) || (readdir == 2));
+
+    int nRead = 0;
+    int nScans = max - min;
+    assert (nScans > 0);
+
+    psRegion toRead = *fullImage;  // full image region
+    
+    int dX = 0;
+    int dY = 0;
+    int nX = 0;
+    int nY = 0;
+
     if (readdir == 1) {
-        if (toRead.y0 <= min) {
-            toRead.y0 = min;
-        } else {
-            fullSize.y0 = min;
-            resize = true;
-        }
-        if (toRead.y1 >= max) {
-            toRead.y1 = max;
-        } else {
-            fullSize.y1 = max;
-            resize = false;
-        }
-    } else if (readdir == 2) {
-        if (toRead.x0 <= min) {
-            toRead.x0 = min;
-        } else {
-            fullSize.x0 = min;
-            resize = true;
-        }
-        if (toRead.x1 >= max) {
-            toRead.x1 = max;
-        } else {
-            fullSize.x1 = max;
-            resize = false;
-        }
+        toRead.y0 = PS_MAX (toRead.y0, min);
+        toRead.y1 = PS_MIN (toRead.y1, max);
+	nRead = toRead.y1 - toRead.y0;
+	if (min < fullImage->y0) {
+	    dY = toRead.y0;
+	}
+	nX = toRead.x1 - toRead.x0;
+	nY = nScans;
     } else {
-        psAbort("Read direction can only be 1 (rows) or 2 (cols).\n");
+        toRead.x0 = PS_MAX (toRead.x0, min);
+        toRead.x1 = PS_MIN (toRead.x1, max);
+	nRead = toRead.x1 - toRead.x0;
+	if (min < fullImage->x0) {
+	    dX = toRead.x0;
+	}
+	nX = nScans;
+	nY = toRead.y1 - toRead.y0;
     }
 
@@ -179,11 +182,11 @@
     }
 
-    if (resize) {
-        // For some reason, the region of interest is smaller than the number of pixels we want.
-        psTrace("psModules.camera", 5, "Resizing image to %.0fx%.0f\n",
-                fullSize.x1 - fullSize.x0, fullSize.y1 - fullSize.y0);
-        psImage *temp = psImageAlloc(fullSize.x1 - fullSize.x0, fullSize.y1 - fullSize.y0, image->type.type);
+    // XXX this modification is not carried back up stream: it affects readout->row0,col0
+    if (nRead < nScans) {
+        // The region of interest is smaller than the number of pixels we want.
+        psTrace("psModules.camera", 5, "Resizing image to %d,%d\n", nX, nY);
+        psImage *temp = psImageAlloc(nX, nY, image->type.type);
         psImageInit(temp, bad);
-        psImageOverlaySection(temp, image, toRead.x0, toRead.y0, "=");
+        psImageOverlaySection(temp, image, dX, dY, "=");
         psFree(image);
         image = temp;
@@ -385,5 +388,6 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool pmReadoutReadNext(pmReadout *readout, psFits *fits, int z, int numScans)
+// XXX need to pass back the error conditions as well as the 'keep going' state
+bool pmReadoutReadNext(bool *status, pmReadout *readout, psFits *fits, int z, int numScans)
 {
     PS_ASSERT_PTR_NON_NULL(readout, false);
@@ -392,4 +396,8 @@
     PS_ASSERT_INT_NONNEGATIVE(numScans, false);
 
+    assert (numScans || !readout->image); // cannot have readout->image and !numScans
+
+    *status = false;
+
     // Get the HDU and read the header
     pmCell *cell = readout->parent;     // The parent cell
@@ -397,4 +405,6 @@
     pmHDU *hdu = pmHDUFromCell(cell);   // The HDU
     if (!hdu || hdu->blankPHU) {
+	// XXX is this an error condition?
+	*status = true;
         return false;
     }
@@ -443,4 +453,5 @@
     if (naxis == 0) {
         // No pixels to read, as for a PHU.
+	*status = true;
         return false;
     }
@@ -460,4 +471,5 @@
     if (z >= naxis3) {
         // Nothing to see here.  Move along.
+	*status = true;
         return false;
     }
@@ -486,35 +498,30 @@
     int maxSize;                        // Number of cols,rows in image
     if (readdir == 1) {
-        maxSize = PS_MIN(naxis2, trimsec->y1);
+        maxSize = PS_MIN(naxis2, trimsec->y1 - trimsec->y0);
     } else {
-        maxSize = PS_MIN(naxis1, trimsec->x1);
-    }
-
-    // Read the FITS image
-    int offset = 0;                     // The offset from the start of the image to where we are now
-    if (readout->image) {
-        if (readdir == 1) {
-            // Reading rows
-            if (numScans == 0) {
-                numScans = naxis2 - readout->row0;
-            }
-            readout->row0 += readout->image->numRows;
-            offset = readout->row0;
-        } else {
-            // Reading columns
-            if (numScans == 0) {
-                numScans = naxis1 - readout->col0;
-            }
-            readout->col0 += readout->image->numCols;
-            offset = readout->col0;
-        }
+        maxSize = PS_MIN(naxis1, trimsec->x1 - trimsec->x0);
+    }
+
+    int offset;				// start of the segment
+    int upper;				// end of the segment
+    int lastScan;			// last possible scan
+
+    // Calculate the segment offset and upper limit, adjust readout->row0,col0
+    if (readdir == 1) {
+	// Reading rows
+	offset = (readout->image) ? readout->row0 + numScans : 0; // extend to next section or start at beginning?
+	offset = (numScans == 0) ? trimsec->x0 : offset; // full array ? read full trimsec : read section
+	readout->row0 = offset;
+	readout->col0 = trimsec->x0;
+	lastScan = trimsec->y1;
     } else {
-        if (numScans == 0) {
-            if (readdir == 1) {
-                numScans = naxis2;
-            } else
-                numScans = naxis1;
-        }
-    }
+	// Reading cols
+	offset = (readout->image) ? readout->col0 + numScans : 0;
+	offset = (numScans == 0) ? trimsec->y0 : offset; // full array ? read full trimsec : read section
+	readout->col0 = offset;
+	readout->row0 = trimsec->y0;
+	lastScan = trimsec->x1;
+    }
+    upper = offset + numScans;
 
     // Blow away existing data.
@@ -527,17 +534,16 @@
     }
 
-    if (offset >= maxSize) {
+    if (offset >= lastScan) {
         // We've read everything there is
         psTrace("psModules.camera", 7, "Read everything.\n");
-        psFree(readout->image);
-        return false;
-    }
-    int upper = PS_MIN(offset + numScans, maxSize); // The upper limit for the pixel read
+	*status = true;
+        return false;
+    }
+
     psTrace("psModules.camera", 7, "offset=%d, upper = %d, image is %dx%d, trimsec [%.0f:%.0f,%.0f:%.0f]\n",
             offset, upper, naxis1, naxis2, trimsec->x0, trimsec->x1, trimsec->y0, trimsec->y1);
 
     // Get the new the trim section
-    readout->image = readoutReadComponent(readout->image, fits, trimsec, readdir,
-                                          offset, upper, z, bad); // The image
+    readout->image = readoutReadComponent(readout->image, fits, trimsec, readdir, offset, upper, z, bad); // The image
 
     // Get the new bias sections
@@ -551,4 +557,5 @@
     psFree(biassecsIter);
 
+    *status = true;
     return true;
 }
Index: /trunk/psModules/src/camera/pmFPARead.h
===================================================================
--- /trunk/psModules/src/camera/pmFPARead.h	(revision 13767)
+++ /trunk/psModules/src/camera/pmFPARead.h	(revision 13768)
@@ -4,6 +4,6 @@
  * @author Paul Price, IfA
  *
- * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-03-30 21:12:56 $
+ * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-06-12 22:22:33 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -25,5 +25,6 @@
 /// Use pmReadoutWriteNext to write the data that's read by this function.  This function is intended for
 /// reading in many readouts into memory at once (e.g., for stacking) where the input is not written out.
-bool pmReadoutReadNext(pmReadout *readout, // Readout into which to read
+bool pmReadoutReadNext(bool *status,	// non-error exit condition?
+		       pmReadout *readout, // Readout into which to read
                        psFits *fits,    // FITS file from which to read
                        int z,           // Readout number/plane; zero-offset indexing
Index: /trunk/psModules/src/imcombine/pmReadoutCombine.c
===================================================================
--- /trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 13767)
+++ /trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 13768)
@@ -142,8 +142,8 @@
 
         // Range of pixels on output image
-        minInputRows = PS_MAX (xMin, PS_MIN(minInputRows, readout->row0));
-        maxInputRows = PS_MIN (xMax, PS_MAX(maxInputRows, readout->row0 + readout->image->numRows));
-        minInputCols = PS_MAX (yMin, PS_MIN(minInputCols, readout->col0));
-        maxInputCols = PS_MIN (yMax, PS_MAX(maxInputCols, readout->col0 + readout->image->numCols));
+        minInputCols = PS_MAX (xMin, PS_MIN(minInputCols, readout->col0));
+        maxInputCols = PS_MIN (xMax, PS_MAX(maxInputCols, readout->col0 + readout->image->numCols));
+        minInputRows = PS_MAX (yMin, PS_MIN(minInputRows, readout->row0));
+        maxInputRows = PS_MIN (yMax, PS_MAX(maxInputRows, readout->row0 + readout->image->numRows));
         psTrace("psModules.imcombine", 7, "Readout %ld: offset %d,%d; size %dx%d\n", i,
                 readout->col0, readout->row0, readout->image->numCols, readout->image->numRows);
