Index: trunk/psModules/src/astrom/pmFPAWrite.c
===================================================================
--- trunk/psModules/src/astrom/pmFPAWrite.c	(revision 6872)
+++ trunk/psModules/src/astrom/pmFPAWrite.c	(revision 6896)
@@ -5,10 +5,73 @@
 #include "pmFPA.h"
 #include "pmHDU.h"
+#include "pmHDUUtils.h"
 #include "pmConcepts.h"
+
+
+bool pmReadoutWriteNext(pmReadout *readout, // Readout to write
+                        psFits *fits,   // FITS file to which to write
+                        int z           // Image plane to write
+                       )
+{
+    pmHDU *hdu = pmHDUFromReadout(readout); // The HDU to which to write
+    psMetadata *header = hdu->header;   // The FITS header
+    if (!header) {
+        psError(PS_ERR_IO, true, "No FITS header available in the HDU.\n");
+        return false;
+    }
+
+    // We have to rely to a great extent on the FITS header, because otherwise we simply don't know how many
+    // image planes there are (NAXIS3) or the size of the original image (NAXIS1, NAXIS2).
+    bool mdok = true;                   // Status of MD lookup
+    int naxis1 = psMetadataLookupS32(&mdok, header, "NAXIS1"); // Number of columns
+    if (!mdok || naxis1 <= 0) {
+        psError(PS_ERR_IO, true, "Can't find NAXIS1 in header.\n");
+        return false;
+    }
+    int naxis2 = psMetadataLookupS32(&mdok, header, "NAXIS2"); // Number of rows
+    if (!mdok || naxis2 <= 0) {
+        psError(PS_ERR_IO, true, "Can't find NAXIS2 in header.\n");
+        return false;
+    }
+    int naxis3 = psMetadataLookupS32(&mdok, header, "NAXIS3"); // Number of image planes
+    if (!mdok || naxis3 <= 0) {
+        naxis3 = 1;
+    }
+    if (z >= naxis3) {
+        psError(PS_ERR_IO, true, "Specified a plane number (%d) greater than NAXIS3 allows.\n", z);
+        return false;
+    }
+
+    psImage *image = hdu->images->data[z]; // The image from the HDU to write
+    if (readout->row0 == 0 && readout->col0 == 0 && z == 0) {
+        // Then we can assume that nothing has been written to the FITS file for now
+        if (naxis1 == image->numCols && naxis2 == image->numRows) {
+            // We can write the whole lot at once
+            return psFitsWriteImage(fits, header, image, z, hdu->extname);
+        }
+        // Create a dummy image so we can write something larger than we actually have
+        psImage *dummy = psImageAlloc(naxis1, naxis2, image->type.type); // Dummy image
+        psImageInit(dummy, 0);
+        psImageOverlaySection(dummy, image, 0, 0, "=");
+        bool result = psFitsWriteImage(fits, header, dummy, z, hdu->extname);
+        psFree(dummy);
+        return result;
+    }
+
+    // We can simply update an existing HDU
+    if (!psFitsMoveExtName(fits, hdu->extname)) {
+        psError(PS_ERR_IO, false, "Unable to move to extension %s\n", hdu->extname);
+        return false;
+    }
+    return psFitsUpdateImage(fits, image, readout->col0, readout->row0, z);
+}
+
+
 
 
 bool pmCellWrite(pmCell *cell,          // Cell to write
                  psFits *fits,          // FITS file to which to write
-                 psDB *db               // Database handle for "concepts" update
+                 psDB *db,              // Database handle for "concepts" update
+                 bool pixels            // Write the pixels?
                 )
 {
@@ -17,5 +80,5 @@
 
     pmHDU *hdu = cell->hdu;             // The HDU
-    if (hdu && !pmHDUWrite(hdu, fits)) {
+    if (hdu && ((!pixels && hdu->phu) || pixels) && !pmHDUWrite(hdu, fits)) {
         psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
         return false;
@@ -28,5 +91,6 @@
 bool pmChipWrite(pmChip *chip,          // Chip to write
                  psFits *fits,          // FITS file to which to write
-                 psDB *db               // Database handle for "concepts" update
+                 psDB *db,              // Database handle for "concepts" update
+                 bool pixels            // Write the pixels?
                 )
 {
@@ -35,5 +99,5 @@
 
     pmHDU *hdu = chip->hdu;             // The HDU
-    if (hdu && !pmHDUWrite(hdu, fits)) {
+    if (hdu && ((!pixels && hdu->phu) || pixels) && !pmHDUWrite(hdu, fits)) {
         psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
         return false;
@@ -41,8 +105,10 @@
 
     bool success = true;                // Success of writing
-    psArray *cells = chip->cells;       // Array of cells
-    for (int i = 0; i < cells->n; i++) {
-        pmCell *cell = cells->data[i];  // The cell of interest
-        success |= pmCellWrite(cell, fits, db);
+    if (pixels) {
+        psArray *cells = chip->cells;       // Array of cells
+        for (int i = 0; i < cells->n; i++) {
+            pmCell *cell = cells->data[i];  // The cell of interest
+            success |= pmCellWrite(cell, fits, db, true);
+        }
     }
 
@@ -55,5 +121,6 @@
 bool pmFPAWrite(pmFPA *fpa,             // FPA to write
                 psFits *fits,           // FITS file to which to write
-                psDB *db                // Database handle for "concepts" update
+                psDB *db,               // Database handle for "concepts" update
+                bool pixels             // Write the pixels?
                )
 {
@@ -62,5 +129,5 @@
 
     pmHDU *hdu = fpa->hdu;              // The HDU
-    if (hdu && !pmHDUWrite(hdu, fits)) {
+    if (hdu && ((!pixels && hdu->phu) || pixels) && !pmHDUWrite(hdu, fits)) {
         psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
         return false;
@@ -68,8 +135,10 @@
 
     bool success = true;                // Success of writing
-    psArray *chips = fpa->chips;        // Array of chips
-    for (int i = 0; i < chips->n; i++) {
-        pmChip *chip = chips->data[i];  // The chip of interest
-        success |= pmChipWrite(chip, fits, db);
+    if (pixels) {
+        psArray *chips = fpa->chips;        // Array of chips
+        for (int i = 0; i < chips->n; i++) {
+            pmChip *chip = chips->data[i];  // The chip of interest
+            success |= pmChipWrite(chip, fits, db, true);
+        }
     }
 
