Index: trunk/psModules/src/camera/pmFPARead.c
===================================================================
--- trunk/psModules/src/camera/pmFPARead.c	(revision 7186)
+++ trunk/psModules/src/camera/pmFPARead.c	(revision 7204)
@@ -118,5 +118,8 @@
     }
 
+    psTrace(__func__, 5, "Reading section [%.0f:%.0f,%.0f:%.0f]\n",
+            toRead.x0, toRead.x1, toRead.y0, toRead.y1);
     psImage *image = psFitsReadImage(fits, toRead, z); // Desired pixels
+    psTrace(__func__, 7, "Image is %dx%d\n", image->numCols, image->numRows);
 
     // XXX: We only support F32 for now
@@ -129,4 +132,6 @@
     if (resize) {
         // For some reason, the region of interest is smaller than the number of pixels we want.
+        psTrace(__func__, 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);
         psImageInit(temp, bad);
@@ -232,4 +237,11 @@
     }
 
+    int maxSize;                        // Number of cols,rows in image
+    if (readdir == 1) {
+        maxSize = PS_MIN(naxis2, trimsec->y1);
+    } 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
@@ -245,20 +257,22 @@
         }
     }
-    if ((readdir == 1 && offset >= naxis2) || (readdir == 2 && offset > naxis1)) {
-        // We've read everything there is
-        return false;
-    }
-    int upper = offset + numScans;      // The upper limit for the pixel read
-    if (readdir == 1 && upper > naxis2) {
-        upper = naxis2;
-    } else if (readdir == 2 && upper > naxis1) {
-        upper = naxis1;
-    }
-
-    // Blow away existing data
+
+    // Blow away existing data.
+    // Do this before returning, so that we're not returning data from a previous read
     psFree(readout->image);
+    readout->image = NULL;
     while (readout->bias->n > 0) {
         psListRemove(readout->bias, PS_LIST_HEAD);
     }
+
+    if (offset >= maxSize) {
+        // We've read everything there is
+        psTrace(__func__, 7, "Read everything.\n");
+        psFree(readout->image);
+        return false;
+    }
+    int upper = PS_MIN(offset + numScans, maxSize); // The upper limit for the pixel read
+    psTrace(__func__, 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
