Index: trunk/psModules/src/camera/pmFPAWrite.c
===================================================================
--- trunk/psModules/src/camera/pmFPAWrite.c	(revision 7168)
+++ trunk/psModules/src/camera/pmFPAWrite.c	(revision 7249)
@@ -84,23 +84,28 @@
                  psFits *fits,          // FITS file to which to write
                  psDB *db,              // Database handle for "concepts" update
-                 bool pixels            // Write the pixels?
+                 bool pixels            // Write the pixels, or only the PHU header?
                 )
 {
     pmHDU *hdu = cell->hdu;             // The HDU
-    if (hdu && ((!pixels && hdu->phu) || pixels)) {
-        if (pixels && !hdu->images && !pmHDUGenerateForCell(cell)) {
-            psError(PS_ERR_IO, false, "Unable to generate HDU for cell.\n");
-            return false;
-        }
-        bool status = pmConceptsWriteCell(cell, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
-                                          PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
-        status &= pmHDUWrite(hdu, fits);
-        if (!status) {
-            psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
-            return false;
-        }
+    if (!hdu) {
+        return true;                    // We wrote every HDU that exists
     }
 
-    return true;
+    bool success = true;                // Success of writing
+    if ((!pixels && hdu->phu && !hdu->images) || // Data-less PHU
+            (pixels && (hdu->images || (!hdu->images && pmHDUGenerateForCell(cell) && hdu->images)))) { // Data
+        success &= pmConceptsWriteCell(cell, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
+                                       PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
+        success &= pmHDUWrite(hdu, fits);
+    }
+
+    if (!success) {
+        psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
+        return false;
+    }
+
+    // No lower levels to which to recurse
+
+    return success;
 }
 
@@ -109,28 +114,31 @@
                  psFits *fits,          // FITS file to which to write
                  psDB *db,              // Database handle for "concepts" update
-                 bool pixels            // Write the pixels?
+                 bool pixels,           // Write the pixels, or only the PHU header?
+                 bool recurse           // Recurse to lower levels?
                 )
 {
     pmHDU *hdu = chip->hdu;             // The HDU
-    if (hdu && ((!pixels && hdu->phu) || pixels)) {
-        if (pixels && !hdu->images && !pmHDUGenerateForChip(chip)) {
-            psError(PS_ERR_IO, false, "Unable to generate HDU for chip.\n");
-            return false;
-        }
-        bool status = pmConceptsWriteChip(chip, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
-                                          PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
-        status &= pmHDUWrite(hdu, fits);
-        if (!status) {
-            psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
-            return false;
-        }
+    if (!hdu) {
+        return true;                    // We wrote every HDU that exists
     }
 
     bool success = true;                // Success of writing
-    if (pixels) {
+    if ((!pixels && hdu->phu && !hdu->images) || // Data-less PHU
+            (pixels && (hdu->images || (!hdu->images && pmHDUGenerateForChip(chip) && hdu->images)))) { // Data
+        success &= pmConceptsWriteChip(chip, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
+                                       PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
+        success &= pmHDUWrite(hdu, fits);
+    }
+
+    if (!success) {
+        psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
+        return false;
+    }
+
+    if (recurse) {
         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);
+            success &= pmCellWrite(cell, fits, db, pixels);
         }
     }
@@ -144,28 +152,31 @@
                 psFits *fits,           // FITS file to which to write
                 psDB *db,               // Database handle for "concepts" update
-                bool pixels             // Write the pixels?
+                bool pixels,            // Write the pixels, or only the PHU header?
+                bool recurse            // Recurse to lower levels?
                )
 {
     pmHDU *hdu = fpa->hdu;              // The HDU
-    if (hdu && ((!pixels && hdu->phu) || pixels)) {
-        if (pixels && !hdu->images && !pmHDUGenerateForFPA(fpa)) {
-            psError(PS_ERR_IO, false, "Unable to generate HDU for FPA.\n");
-            return false;
-        }
-        bool status = pmConceptsWriteFPA(fpa, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
-                                         PM_CONCEPT_SOURCE_DEFAULTS, NULL);
-        status &= pmHDUWrite(hdu, fits);
-        if (!status) {
-            psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
-            return false;
-        }
+    if (!hdu) {
+        return true;                    // We wrote every HDU that exists
     }
 
     bool success = true;                // Success of writing
-    if (pixels) {
+    if ((!pixels && hdu->phu && !hdu->images) || // Data-less PHU
+            (pixels && (hdu->images || (!hdu->images && pmHDUGenerateForFPA(fpa) && hdu->images)))) { // Data
+        success &= pmConceptsWriteFPA(fpa, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
+                                      PM_CONCEPT_SOURCE_DEFAULTS, NULL);
+        success &= pmHDUWrite(hdu, fits);
+    }
+
+    if (!success) {
+        psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
+        return false;
+    }
+
+    if (recurse) {
         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);
+            success &= pmChipWrite(chip, fits, db, pixels, true);
         }
     }
