Index: trunk/psModules/src/camera/pmFPAWrite.c
===================================================================
--- trunk/psModules/src/camera/pmFPAWrite.c	(revision 19351)
+++ trunk/psModules/src/camera/pmFPAWrite.c	(revision 19385)
@@ -36,56 +36,4 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-
-// Update the FPA.OBS, CHIP.NAME and CELL.NAME in the FITS header, if required
-bool pmFPAUpdateNames(pmFPA *fpa, pmChip *chip, pmCell *cell)
-{
-    pmHDU *hdu = pmHDUGetHighest(fpa, chip, cell); // Highest HDU, i.e., the PHU
-    if (!hdu) {
-        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find HDU.\n");
-        return false;
-    }
-    if (!hdu->header) {
-        hdu->header = psMetadataAlloc();
-    }
-    bool mdok;                          // Status of MD lookup
-    psMetadata *fileData = psMetadataLookupMetadata(&mdok, hdu->format, "FILE"); // File information
-    if (!mdok || !fileData) {
-        psError(PS_ERR_UNKNOWN, true, "Unable to find FILE information in camera format.\n");
-        return false;
-    }
-    if (fpa) {
-        const char *fpaObsHdr = psMetadataLookupStr(&mdok, fileData, "FPA.OBS");
-        if (mdok && fpaObsHdr && strlen(fpaObsHdr) > 0) {
-            const char *fpaObs = psMetadataLookupStr(NULL, fpa->concepts, "FPA.OBS");
-            psMetadataAddStr(hdu->header, PS_LIST_TAIL, fpaObsHdr, PS_META_REPLACE,
-                             "Observation identifier", fpaObs);
-        }
-    }
-
-    if (fpa && !fpa->hdu && (chip || cell)) {
-        const char *rule = psMetadataLookupStr(NULL, fileData, "CONTENT.RULE"); // How to define the CONTENT
-        if (!rule) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to find CONTENT.RULE in FILE in camera format.");
-            return false;
-        }
-
-        pmFPAview *view = pmFPAviewGenerate(fpa, chip, cell, NULL); // View for fpa, chip, cell
-        psString content = pmFPANameFromRule(rule, fpa, view); // Content of this file, specified by the rule
-        psFree(view);
-
-        const char *contentKey = psMetadataLookupStr(NULL, fileData, "CONTENT"); // The CONTENT header keyword
-        if (!contentKey) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to find CONTENT in FILE in the camera format.");
-            psFree(content);
-            return false;
-        }
-
-        psMetadataAddStr(hdu->header, PS_LIST_TAIL, contentKey, PS_META_REPLACE, "Content of file", content);
-        psFree(content);                // Drop reference
-    }
-
-    return true;
-}
-
 // Return the appropriate image array for the given type
 static psArray **appropriateImageArray(pmHDU *hdu, // HDU containing the image arrays
@@ -109,4 +57,5 @@
 static bool appropriateWriteFunc(pmHDU *hdu, // HDU to write
                                  psFits *fits, // FITS file to which to write
+                                 const pmConfig *config, // Configuration
                                  fpaWriteType type // Type to write
                                 )
@@ -114,9 +63,9 @@
     switch (type) {
     case FPA_WRITE_TYPE_IMAGE:
-        return pmHDUWrite(hdu, fits);
+        return pmHDUWrite(hdu, fits, config);
     case FPA_WRITE_TYPE_MASK:
-        return pmHDUWriteMask(hdu, fits);
+        return pmHDUWriteMask(hdu, fits, config);
     case FPA_WRITE_TYPE_WEIGHT:
-        return pmHDUWriteWeight(hdu, fits);
+        return pmHDUWriteWeight(hdu, fits, config);
     default:
         psAbort("Unknown write type: %x\n", type);
@@ -165,5 +114,5 @@
             return false;
         }
-        if (!appropriateWriteFunc(hdu, fits, type)) {
+        if (!appropriateWriteFunc(hdu, fits, config, type)) {
             psError(PS_ERR_IO, false, "Unable to write HDU for cell.\n");
             return false;
@@ -215,5 +164,5 @@
                 return false;
             }
-            if (!appropriateWriteFunc(hdu, fits, type)) {
+            if (!appropriateWriteFunc(hdu, fits, config, type)) {
                 psError(PS_ERR_IO, false, "Unable to write HDU for chip.\n");
                 return false;
@@ -279,5 +228,5 @@
                 return false;
             }
-            if (!appropriateWriteFunc(hdu, fits, type))  {
+            if (!appropriateWriteFunc(hdu, fits, config, type))  {
                 psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
                 return false;
@@ -305,4 +254,55 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+// Update the FPA.OBS, CHIP.NAME and CELL.NAME in the FITS header, if required
+bool pmFPAUpdateNames(pmFPA *fpa, pmChip *chip, pmCell *cell)
+{
+    pmHDU *hdu = pmHDUGetHighest(fpa, chip, cell); // Highest HDU, i.e., the PHU
+    if (!hdu) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find HDU.\n");
+        return false;
+    }
+    if (!hdu->header) {
+        hdu->header = psMetadataAlloc();
+    }
+    bool mdok;                          // Status of MD lookup
+    psMetadata *fileData = psMetadataLookupMetadata(&mdok, hdu->format, "FILE"); // File information
+    if (!mdok || !fileData) {
+        psError(PS_ERR_UNKNOWN, true, "Unable to find FILE information in camera format.\n");
+        return false;
+    }
+    if (fpa) {
+        const char *fpaObsHdr = psMetadataLookupStr(&mdok, fileData, "FPA.OBS");
+        if (mdok && fpaObsHdr && strlen(fpaObsHdr) > 0) {
+            const char *fpaObs = psMetadataLookupStr(NULL, fpa->concepts, "FPA.OBS");
+            psMetadataAddStr(hdu->header, PS_LIST_TAIL, fpaObsHdr, PS_META_REPLACE,
+                             "Observation identifier", fpaObs);
+        }
+    }
+
+    if (fpa && !fpa->hdu && (chip || cell)) {
+        const char *rule = psMetadataLookupStr(NULL, fileData, "CONTENT.RULE"); // How to define the CONTENT
+        if (!rule) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to find CONTENT.RULE in FILE in camera format.");
+            return false;
+        }
+
+        pmFPAview *view = pmFPAviewGenerate(fpa, chip, cell, NULL); // View for fpa, chip, cell
+        psString content = pmFPANameFromRule(rule, fpa, view); // Content of this file, specified by the rule
+        psFree(view);
+
+        const char *contentKey = psMetadataLookupStr(NULL, fileData, "CONTENT"); // The CONTENT header keyword
+        if (!contentKey) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to find CONTENT in FILE in the camera format.");
+            psFree(content);
+            return false;
+        }
+
+        psMetadataAddStr(hdu->header, PS_LIST_TAIL, contentKey, PS_META_REPLACE, "Content of file", content);
+        psFree(content);                // Drop reference
+    }
+
+    return true;
+}
+
 bool pmReadoutWriteNext(pmReadout *readout, psFits *fits, int z)
 {
@@ -347,5 +347,6 @@
         return false;
     }
-    psImage *image = readout->image; // The image from the HDU to write
+    psImage *image = readout->image;    // The image from the HDU to write
+    //    psImage *mask = readout->mask;        // Corresponding mask image
     if (readout->row0 == 0 && readout->col0 == 0 && z == 0) {
         // Then we can assume that nothing has been written to the FITS file for now
Index: trunk/psModules/src/camera/pmHDU.c
===================================================================
--- trunk/psModules/src/camera/pmHDU.c	(revision 19351)
+++ 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);
+}
Index: trunk/psModules/src/camera/pmHDU.h
===================================================================
--- trunk/psModules/src/camera/pmHDU.h	(revision 19351)
+++ trunk/psModules/src/camera/pmHDU.h	(revision 19385)
@@ -4,6 +4,6 @@
  * @author Paul Price, IfA
  *
- * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-01-02 20:33:41 $
+ * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-09-05 08:21:35 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -11,4 +11,7 @@
 #ifndef PM_HDU_H
 #define PM_HDU_H
+
+#include <pslib.h>
+#include "pmConfig.h"
 
 /// @addtogroup Camera Camera Layout
@@ -62,20 +65,23 @@
 bool pmHDUReadWeight(pmHDU *hdu,        ///< HDU to read
                      psFits *fits       ///< FITS file to read from
-                    );
+    );
 
 /// Write the HDU header and pixels
 bool pmHDUWrite(pmHDU *hdu,             ///< HDU to write
-                psFits *fits            ///< FITS file to write to
-               );
+                psFits *fits,           ///< FITS file to write to
+                const pmConfig *config  ///< Configuration
+    );
 
 /// Write the HDU header and mask
 bool pmHDUWriteMask(pmHDU *hdu,         ///< HDU to write
-                    psFits *fits        ///< FITS file to write to
-                   );
+                    psFits *fits,       ///< FITS file to write to
+                    const pmConfig *config  ///< Configuration
+    );
 
 /// Write the HDU header and weight map
 bool pmHDUWriteWeight(pmHDU *hdu,       ///< HDU to write
-                      psFits *fits      ///< FITS file to write to
-                     );
+                      psFits *fits,     ///< FITS file to write to
+                      const pmConfig *config  ///< Configuration
+    );
 
 /// @}
