Index: trunk/psModules/src/camera/pmHDU.c
===================================================================
--- trunk/psModules/src/camera/pmHDU.c	(revision 15973)
+++ trunk/psModules/src/camera/pmHDU.c	(revision 19385)
@@ -9,4 +9,5 @@
 
 #include "pmConfig.h"
+#include "pmConfigMask.h"
 #include "pmHDU.h"
 #include "pmFPA.h"
@@ -164,5 +165,7 @@
 // Write an HDU to a FITS file
 static bool hduWrite(pmHDU *hdu,        // HDU to write
-                     psArray *images,   // Images to write
+                     const psArray *images, // Images to write
+                     const psArray *masks, // Masks to use when writing
+                     psMaskType maskVal,// Value to mask
                      psFits *fits       // FITS file to which to write
                     )
@@ -205,5 +208,5 @@
     if (images) {
         psTrace("psModules.camera", 9, "Writing pixels for %s\n", hdu->extname);
-        if (!psFitsWriteImageCube(fits, hdu->header, images, extname)) {
+        if (!psFitsWriteImageCubeWithMask(fits, hdu->header, images, masks, maskVal, extname)) {
             psError(PS_ERR_IO, false, "Unable to write image to extension %s\n", hdu->extname);
             return false;
@@ -214,25 +217,28 @@
 
 // XXX: Add a region specifier?
-bool pmHDUWrite(pmHDU *hdu, psFits *fits)
-{
-    PS_ASSERT_PTR_NON_NULL(hdu, false);
-    PS_ASSERT_PTR_NON_NULL(fits, false);
-
-    return hduWrite(hdu, hdu->images, fits);
-}
-
-bool pmHDUWriteMask(pmHDU *hdu, psFits *fits)
-{
-    PS_ASSERT_PTR_NON_NULL(hdu, false);
-    PS_ASSERT_PTR_NON_NULL(fits, false);
-
-    return hduWrite(hdu, hdu->masks, fits);
-}
-
-bool pmHDUWriteWeight(pmHDU *hdu, psFits *fits)
-{
-    PS_ASSERT_PTR_NON_NULL(hdu, false);
-    PS_ASSERT_PTR_NON_NULL(fits, false);
-
-    return hduWrite(hdu, hdu->weights, fits);
-}
+bool pmHDUWrite(pmHDU *hdu, psFits *fits, const pmConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(hdu, false);
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+
+    psMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config); // Value to mask
+    return hduWrite(hdu, hdu->images, hdu->masks, maskVal, fits);
+}
+
+bool pmHDUWriteMask(pmHDU *hdu, psFits *fits, const pmConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(hdu, false);
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+
+    // We don't supply a mask because we're writing the mask!
+    return hduWrite(hdu, hdu->masks, NULL, 0, fits);
+}
+
+bool pmHDUWriteWeight(pmHDU *hdu, psFits *fits, const pmConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(hdu, false);
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+
+    psMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config); // Value to mask
+    return hduWrite(hdu, hdu->weights, hdu->masks, maskVal, fits);
+}
