Index: trunk/psModules/src/camera/pmFPARead.c
===================================================================
--- trunk/psModules/src/camera/pmFPARead.c	(revision 24058)
+++ trunk/psModules/src/camera/pmFPARead.c	(revision 24792)
@@ -141,5 +141,5 @@
 // Determine number of readouts in the FITS file
 // In the process, reads the header and concepts
-static bool cellNumReadouts(pmCell *cell,    // Cell of interest
+static int cellNumReadouts(pmCell *cell,    // Cell of interest
                             psFits *fits,    // FITS file
                             pmConfig *config // Configuration
@@ -153,13 +153,13 @@
     if (!hdu || hdu->blankPHU) {
         psError(PS_ERR_IO, true, "Unable to find HDU");
-        return false;
+        return 0;
     }
     if (!pmCellReadHeader(cell, fits, config)) {
         psError(PS_ERR_IO, false, "Unable to read header for cell!\n");
-        return false;
+        return 0;
     }
     if (!pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS, true, NULL)) {
         psError(PS_ERR_IO, false, "Failed to read concepts for cell.\n");
-        return false;
+        return 0;
     }
 
@@ -169,15 +169,16 @@
     if (!mdok) {
         psError(PS_ERR_IO, true, "Unable to find NAXIS in header for extension %s\n", hdu->extname);
-        return false;
-    }
+        return 0;
+    }
+
     if (naxis == 0) {
         // No pixels to read
         psError(PS_ERR_IO, true, "No pixels in extension %s.", hdu->extname);
-        return false;
+        return 0;
     }
     if (naxis < 2 || naxis > 3) {
         psError(PS_ERR_IO, true, "NAXIS in header of extension %s (= %d) is not valid.\n",
                 hdu->extname, naxis);
-        return false;
+        return 0;
     }
     int naxis3;                     // Number of image planes
@@ -186,5 +187,5 @@
         if (!mdok) {
             psError(PS_ERR_IO, true, "Unable to find NAXIS3 in header for extension %s\n", hdu->extname);
-            return false;
+            return 0;
         }
     } else {
@@ -294,5 +295,6 @@
 static bool readoutMore(pmReadout *readout, // Readout of interest
                         psFits *fits,    // FITS file
-                        int z,          // Plane number
+                        int z,          // Plane number to read
+                        int *zMax,	// Max plane number in this cell
                         int numScans,   // Number of scans to read at a time
                         fpaReadType type, // Type of image
@@ -308,11 +310,10 @@
     // N readouts, but numScans set to 0.  only the first should report that it requires data,
     // even if all readouts lack the image pointer.
-    if (!image) {
+    if (numScans == 0) {
+      if (!image) {
         return true;
-    }
-
-    // If we have already read an image, this result implies we are done (no more to read)
-    if (numScans == 0) {
-        return false;
+      } else {
+	return false;
+      }
     }
 
@@ -322,6 +323,6 @@
         return false;
     }
-    int naxis3 = cellNumReadouts(cell, fits, config); // Number of planes
-    if (z >= naxis3) {
+    *zMax = cellNumReadouts(cell, fits, config); // Number of planes
+    if (z >= *zMax) {
         // No more to read
         return false;
@@ -484,4 +485,5 @@
                              psFits *fits, // FITS file
                              int z,     // Desired image plane
+			     int *zMax,	// Max plane number in this cell
                              int numScans, // Number of scans (row or col depends on CELL.READDIR); 0 for all
                              int overlap, // Number of scans (row/col) to overlap between scans
@@ -509,4 +511,6 @@
 
     int naxis3 = cellNumReadouts(cell, fits, config); // Number of image planes
+    if (zMax) *zMax = naxis3;
+
     if (z >= naxis3) {
         psError(PS_ERR_IO, false, "Desired image plane (%d) exceeds available number (%d).",
@@ -1021,13 +1025,13 @@
 
 
-bool pmReadoutMore(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config)
+bool pmReadoutMore(pmReadout *readout, psFits *fits, int z, int *zMax, int numScans, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(readout, false);
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_IMAGE, config);
-}
-
-bool pmReadoutReadChunk(pmReadout *readout, psFits *fits, int z, int numScans, int overlap, pmConfig *config)
+    return readoutMore(readout, fits, z, zMax, numScans, FPA_READ_TYPE_IMAGE, config);
+}
+
+bool pmReadoutReadChunk(pmReadout *readout, psFits *fits, int z, int *zMax, int numScans, int overlap, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(readout, false);
@@ -1036,5 +1040,5 @@
     PS_ASSERT_INT_NONNEGATIVE(numScans, false);
 
-    return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_IMAGE, config);
+    return readoutReadChunk(readout, fits, z, zMax, numScans, overlap, FPA_READ_TYPE_IMAGE, config);
 }
 
@@ -1044,5 +1048,5 @@
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_IMAGE, config);
+    return readoutReadChunk(readout, fits, z, NULL, 0, 0, FPA_READ_TYPE_IMAGE, config);
 }
 
@@ -1084,13 +1088,13 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool pmReadoutMoreMask(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config)
+bool pmReadoutMoreMask(pmReadout *readout, psFits *fits, int z, int *zMax, int numScans, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(readout, false);
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_MASK, config);
-}
-
-bool pmReadoutReadChunkMask(pmReadout *readout, psFits *fits, int z, int numScans, int overlap,
+    return readoutMore(readout, fits, z, zMax, numScans, FPA_READ_TYPE_MASK, config);
+}
+
+bool pmReadoutReadChunkMask(pmReadout *readout, psFits *fits, int z, int *zMax, int numScans, int overlap,
                             pmConfig *config)
 {
@@ -1100,5 +1104,5 @@
     PS_ASSERT_INT_NONNEGATIVE(numScans, false);
 
-    return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_MASK, config);
+    return readoutReadChunk(readout, fits, z, zMax, numScans, overlap, FPA_READ_TYPE_MASK, config);
 }
 
@@ -1108,5 +1112,5 @@
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_MASK, config);
+    return readoutReadChunk(readout, fits, z, NULL, 0, 0, FPA_READ_TYPE_MASK, config);
 }
 
@@ -1139,13 +1143,13 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool pmReadoutMoreVariance(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config)
+bool pmReadoutMoreVariance(pmReadout *readout, psFits *fits, int z, int *zMax, int numScans, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(readout, false);
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_VARIANCE, config);
-}
-
-bool pmReadoutReadChunkVariance(pmReadout *readout, psFits *fits, int z, int numScans, int overlap,
+    return readoutMore(readout, fits, z, zMax, numScans, FPA_READ_TYPE_VARIANCE, config);
+}
+
+bool pmReadoutReadChunkVariance(pmReadout *readout, psFits *fits, int z, int *zMax, int numScans, int overlap,
                               pmConfig *config)
 {
@@ -1155,5 +1159,5 @@
     PS_ASSERT_INT_NONNEGATIVE(numScans, false);
 
-    return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_VARIANCE, config);
+    return readoutReadChunk(readout, fits, z, zMax, numScans, overlap, FPA_READ_TYPE_VARIANCE, config);
 }
 
@@ -1163,5 +1167,5 @@
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_VARIANCE, config);
+    return readoutReadChunk(readout, fits, z, NULL, 0, 0, FPA_READ_TYPE_VARIANCE, config);
 }
 
