Index: trunk/psModules/src/camera/pmFPAWrite.c
===================================================================
--- trunk/psModules/src/camera/pmFPAWrite.c	(revision 7469)
+++ trunk/psModules/src/camera/pmFPAWrite.c	(revision 7530)
@@ -128,30 +128,51 @@
 
     pmHDU *hdu = chip->hdu;             // The HDU
-    if (!hdu) {
-        return true;                    // We wrote every HDU that exists
-    }
-
-    bool success = true;                // Success of writing
-    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, true, NULL);
-        success &= pmHDUWrite(hdu, fits);
-    }
-
-    if (!success) {
-        psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
-        return false;
-    }
-
+
+    // XXXX this makes no sense to me at all:
+    // an image made up of only cells would not have
+    // any entries at this level, and could not be written out!!!
+
+    // if we have data at this level, try to write it out
+    if (hdu) {
+        // generate the HDU if needed
+        if (pixels && !hdu->images) {
+            if (!pmHDUGenerateForChip(chip))
+                psAbort ("pmFPAWrite", "error generating HDU");
+            if (!hdu->images)
+                psAbort ("pmChipWrite", "programming error: failure generating HDU");
+        }
+
+        // chip->hdu represents a blank data segment
+        bool blankSegment = !pixels && hdu->phu && !hdu->images;
+
+        // chip->hdu represents an image data segment
+        bool imageSegment = pixels && hdu->images;
+
+        if (blankSegment || imageSegment) {
+            pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS;
+            if (!pmConceptsWriteChip(chip, source, false, true, NULL)) {
+                psError(PS_ERR_IO, false, "Unable to write Concepts for Chip.\n");
+                return false;
+            }
+            if (!pmHDUWrite(hdu, fits)) {
+                psError(PS_ERR_IO, false, "Unable to write HDU for Chip.\n");
+                return false;
+            }
+        }
+    }
+
+    // XXX are we allowed to recurse if we have already written data at this level?
     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, pixels);
-        }
-    }
-
-    return success;
+            if (!pmCellWrite(cell, fits, db, pixels)) {
+                psError(PS_ERR_IO, false, "Unable to write Chip.\n");
+                return false;
+            }
+        }
+    }
+
+    return true;
 }
 
@@ -169,30 +190,62 @@
 
     pmHDU *hdu = fpa->hdu;              // The HDU
-    if (!hdu) {
-        return true;                    // We wrote every HDU that exists
-    }
-
-    bool success = true;                // Success of writing
-    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, true, NULL);
-        success &= pmHDUWrite(hdu, fits);
-    }
-
-    if (!success) {
-        psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
-        return false;
-    }
-
+
+    // XXXX this makes no sense to me at all:
+    // an image made up of only chips or cells would not have
+    // any entries at this level, and could not be written out!!!
+
+    // if we have data at this level, try to write it out
+    if (hdu) {
+
+        // XXXXXXXXX this was extremely unclear.  the conditions MUST be written more explicitly
+        // so someone else has a chance to understand it without massive reverse engineering!!!!
+
+        // generate the HDU if needed
+        if (pixels && !hdu->images) {
+            if (!pmHDUGenerateForFPA(fpa))
+                psAbort ("pmFPAWrite", "error generating HDU");
+            if (!hdu->images)
+                psAbort ("pmFPAWrite", "programming error: failure generating HDU");
+        }
+
+        // fpa->hdu represents a blank data segment
+        bool blankSegment = !pixels && hdu->phu && !hdu->images;
+
+        // fpa->hdu represents an image data segment
+        bool imageSegment = pixels && hdu->images;
+
+        // if neither of these conditions is true, we probably need to recurse down to the next level and try again
+        if (blankSegment || imageSegment) {
+            pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS;
+            if (!pmConceptsWriteFPA(fpa, source, true, NULL)) {
+                psError(PS_ERR_IO, false, "Unable to write Concepts for FPA.\n");
+                return false;
+            }
+            if (!pmHDUWrite(hdu, fits))  {
+                psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
+                return false;
+            }
+        }
+    }
+
+    // XXX what are the rules on when we allow recursion?
     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, pixels, true);
-        }
-    }
-
-    return success;
-}
-
+            if (!pmChipWrite(chip, fits, db, pixels, true)) {
+                psError(PS_ERR_IO, false, "Unable to write FPA.\n");
+                return false;
+            }
+        }
+    }
+
+    return true;
+
+    // summary:
+    // pmHDUWrite writes out the PHU header or the header + pixels
+    // we need to generate the HDU (why?)
+    // we call pmHDUWrite if:
+    // - the fpa->hdu represents a blank data segment (header w/ no pixels)
+    // - the fpa->hdu represents an image data segment (header w/ pixels)
+}
