Index: /branches/rel10_ifa/psModules/src/astrom/pmFPARead.c
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPARead.c	(revision 6851)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPARead.c	(revision 6852)
@@ -59,15 +59,100 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-
-#if 0
-bool pmReadoutRead(pmReadout *readout,  // Readout to read into
-                   psFits *fits,        // FITS file from which to read
-                   int numSections      // Total number of sections
-                   int section          // The section of interest
-                  )
-{
-    // This is very very different
-}
-#endif
+// Read the next readout; return true if we read pixels in.
+bool pmReadoutReadNext(pmReadout *readout, // Readout into which to read
+                       psFits *fits,    // FITS file from which to read
+                       int z,           // Readout number/plane; zero-offset indexing
+                       int numRows      // The number of rows to read
+                      )
+{
+    // Get the HDU and read the header
+    pmCell *cell = readout->parent;     // The parent cell
+    pmHDU *hdu = pmHDUFromCell(cell);   // The HDU
+    if (!hdu) {
+        return false;
+    }
+
+    if (!pmHDUReadHeader(hdu, fits)) {
+        psError(PS_ERR_IO, false, "Unable to read HDU header!\n");
+        return false;
+    }
+
+    // Check the third dimension
+    bool mdok = true;                   // Status of MD lookup
+    int naxis = psMetadataLookupS32(&mdok, hdu->header, "NAXIS"); // The number of axes
+    if (!mdok) {
+        psError(PS_ERR_IO, true, "Unable to find NAXIS in header for extension %s\n", hdu->extname);
+        return false;
+    }
+    if (naxis == 0) {
+        // No pixels to read, as for a PHU.
+        return false;
+    }
+    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;
+    }
+    int naxis3 = 1;                     // The number of image planes
+    if (naxis == 3) {
+        naxis3 = psMetadataLookupS32(&mdok, hdu->header, "NAXIS3");
+        if (!mdok) {
+            psError(PS_ERR_IO, true, "Unable to find NAXIS3 in header for extension %s\n", hdu->extname);
+            return false;
+        }
+    }
+    if (z >= naxis3) {
+        // Nothing to see here.  Move along.
+        return false;
+    }
+
+    // Get the size of the image plane
+    int naxis1 = psMetadataLookupS32(&mdok, hdu->header, "NAXIS1"); // The number of columns
+    if (!mdok) {
+        psError(PS_ERR_IO, true, "Unable to find NAXIS1 in header for extension %s\n", hdu->extname);
+        return false;
+    }
+    int naxis2 = psMetadataLookupS32(&mdok, hdu->header, "NAXIS2"); // The number of columns
+    if (!mdok) {
+        psError(PS_ERR_IO, true, "Unable to find NAXIS2 in header for extension %s\n", hdu->extname);
+        return false;
+    }
+
+    // Read the FITS image
+    int offset = readout->row0;         // The row number to read
+    if (readout->image) {
+        offset += readout->image->numRows;
+    }
+    if (offset >= naxis2) {
+        // We've read everything there is
+        return false;
+    }
+    int upper = offset + numRows;       // The upper limit for the pixel read
+    if (upper > naxis2) {
+        upper = naxis2;
+    }
+    psRegion region = psRegionSet(0, naxis1, offset, upper); // Region to be read
+    psImage *image = psFitsReadImage(fits, region, z); // The image
+    if (readout->image) {
+        psFree(readout->image);
+    }
+
+    // Stick the image into the HDU and the readout
+    if (!hdu->images) {
+        hdu->images = psArrayAlloc(naxis3);
+    }
+    if (hdu->images->n != naxis3) {
+        hdu->images = psArrayRealloc(hdu->images, naxis3);
+    }
+    if (hdu->images->data[z]) {
+        psFree(hdu->images->data[z]);
+    }
+    hdu->images->data[z] = image;
+    readout->image = psImageSubset(hdu->images->data[z], psRegionSet(0,0,0,0));
+    readout->row0 = region.y0;
+
+    return true;
+}
+
 
 // Read in the cell, and allocate the readouts
