Index: trunk/psModules/src/camera/pmFPAWrite.c
===================================================================
--- trunk/psModules/src/camera/pmFPAWrite.c	(revision 9949)
+++ trunk/psModules/src/camera/pmFPAWrite.c	(revision 9983)
@@ -242,4 +242,114 @@
 
 
+bool pmCellWriteMask(pmCell *cell,          // Cell to write
+                     psFits *fits,          // FITS file to which to write
+                     psDB *db           // Database handle for "concepts" update
+                    )
+{
+    PS_ASSERT_PTR_NON_NULL(cell, false);
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+
+    pmHDU *hdu = cell->hdu;             // The HDU
+    if (!hdu) {
+        return true;                    // We wrote every HDU that exists
+    }
+
+    psTrace ("pmFPAWrite", 5, "writing mask to Cell\n");
+
+    // Generate the HDU if needed --- this is required after a pmFPACopy, or similar, which does not
+    // generate the HDU, but only copies the structure.
+    if (!hdu->blankPHU && !hdu->masks) {
+        if (!pmHDUGenerateForCell(cell) || !hdu->masks) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for cell --- likely programming error.\n");
+            return false;
+        }
+    }
+
+    pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | PM_CONCEPT_SOURCE_DEFAULTS;
+    if (!pmConceptsWriteCell(cell, source, false, NULL)) {
+        psError(PS_ERR_IO, false, "Unable to write concepts for cell.\n");
+        return false;
+    }
+    if (!pmHDUWriteMask(hdu, fits)) {
+        psError(PS_ERR_IO, false, "Unable to write HDU for cell.\n");
+        return false;
+    }
+
+    return true;
+}
+
+
+bool pmChipWriteMask(pmChip *chip, psFits *fits, psDB *db)
+{
+    PS_ASSERT_PTR_NON_NULL(chip, false);
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+
+    pmHDU *hdu = chip->hdu;             // The HDU
+    if (!hdu) {
+        return true;                    // We wrote every HDU that exists
+    }
+
+    psTrace ("pmFPAWrite", 5, "writing mask to Chip\n");
+
+    // Generate the HDU if needed --- this is required after a pmFPACopy, or similar, which does not
+    // generate the HDU, but only copies the structure.
+    if (!hdu->blankPHU && !hdu->masks) {
+        if (!pmHDUGenerateForChip(chip) || !hdu->masks) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for chip --- likely programming error.\n");
+            return false;
+        }
+    }
+
+    pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | 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 (!pmHDUWriteMask(hdu, fits)) {
+        psError(PS_ERR_IO, false, "Unable to write HDU for chip.\n");
+        return false;
+    }
+
+    return true;
+}
+
+
+
+bool pmFPAWriteMask(pmFPA *fpa, psFits *fits, psDB *db)
+{
+    PS_ASSERT_PTR_NON_NULL(fpa, false);
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+
+    pmHDU *hdu = fpa->hdu;              // The HDU
+    if (!hdu) {
+        return true;                    // We wrote every HDU that exists
+    }
+
+    psTrace ("pmFPAWrite", 5, "writing mask to FPA\n");
+
+    // Generate the HDU if needed --- this is required after a pmFPACopy, or similar, which does not
+    // generate the HDU, but only copies the structure.
+    if (!hdu->blankPHU && !hdu->masks) {
+        if (!pmHDUGenerateForFPA(fpa) || !hdu->masks) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for chip --- likely programming error.\n");
+            return false;
+        }
+    }
+
+    pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | 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 (!pmHDUWriteMask(hdu, fits))  {
+        psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
+        return false;
+    }
+
+    return true;
+}
+
+
+
 int pmCellWriteTable(psFits *fits, const pmCell *cell, const char *name)
 {
