Index: trunk/psModules/src/camera/pmFPAMaskWeight.h
===================================================================
--- trunk/psModules/src/camera/pmFPAMaskWeight.h	(revision 18364)
+++ trunk/psModules/src/camera/pmFPAMaskWeight.h	(revision 18554)
@@ -5,6 +5,6 @@
  * @author Eugene Magnier, IfA
  *
- * @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-05-17 02:42:01 $
+ * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-07-15 20:25:00 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -32,5 +32,4 @@
     PM_MASK_MARK     = 0x80,            ///< The pixel is marked as temporarily ignored
 } pmMaskValue;
-#else
 #define PM_MASK_MARK 0x80            ///< The pixel is marked as temporarily ignored
 #define PM_MASK_SAT  0x04            ///< The pixel is saturated in the image of interest
@@ -43,6 +42,6 @@
 /// iterating using pmReadoutReadNext, in which case the HDU can't be generated.
 bool pmReadoutSetMask(pmReadout *readout, ///< Readout for which to set mask
-                      psMaskType sat,   ///< Mask value to give saturated pixels
-                      psMaskType bad    ///< Mask value to give bad (low) pixels
+                      psMaskType satMask, ///< Mask value to give saturated pixels
+                      psMaskType badMask  ///< Mask value to give bad (low) pixels
     );
 
Index: trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 18364)
+++ trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 18554)
@@ -10,4 +10,5 @@
 #include "pmErrorCodes.h"
 #include "pmConfig.h"
+#include "pmConfigMask.h"
 #include "pmDetrendDB.h"
 
@@ -489,4 +490,11 @@
         psFree(realName);
         return NULL;
+    }
+
+    if (file->type == PM_FPA_FILE_MASK) {
+	if (!pmConfigMaskReadHeader (config, phu)) {
+	    psError(PS_ERR_IO, false, "error in mask bits");
+	    return NULL;
+	}
     }
 
@@ -660,4 +668,5 @@
     file->fileLevel = input->fileLevel;
 
+
     // define the rule to identify these files in the file->names data
     psFree (file->filerule);
@@ -735,4 +744,11 @@
         psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]);
 
+	if ((i == 0) && (file->type == PM_FPA_FILE_MASK)) {
+	    if (!pmConfigMaskReadHeader (config, phu)) {
+		psError(PS_ERR_IO, false, "error in mask bits");
+		return NULL;
+	    }
+	}
+
         psFree(view);
         psFree(name);
@@ -806,4 +822,11 @@
 	psFree(formatName);
         return NULL;
+    }
+
+    if (file->type == PM_FPA_FILE_MASK) {
+	if (!pmConfigMaskReadHeader (config, phu)) {
+	    psError(PS_ERR_IO, false, "error in mask bits");
+	    return NULL;
+	}
     }
 
Index: trunk/psModules/src/camera/pmFPAfileFitsIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 18364)
+++ trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 18554)
@@ -7,4 +7,5 @@
 
 #include "pmConfig.h"
+#include "pmConfigMask.h"
 #include "pmDetrendDB.h"
 
@@ -496,4 +497,13 @@
     }
 
+    // whenever we write out a mask image, we should define the bits which represent mask concepts
+    if (file->type == PM_FPA_FILE_MASK) {
+	assert (phu->header);
+	if (!pmConfigMaskWriteHeader (config, phu->header)) {
+	    psError(PS_ERR_UNKNOWN, false, "failed to set the bitmask names in the PHU header for Image %s (%s)\n", file->filename, file->name);
+	    return false;
+	}
+    }
+
     switch (file->fileLevel) {
       case PM_FPA_LEVEL_FPA:
Index: trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileIO.c	(revision 18364)
+++ trunk/psModules/src/camera/pmFPAfileIO.c	(revision 18554)
@@ -9,4 +9,5 @@
 
 #include "pmConfig.h"
+#include "pmConfigMask.h"
 #include "pmDetrendDB.h"
 
@@ -428,4 +429,13 @@
             }
             pmConfigConformHeader(hdu->header, file->format);
+
+	    // whenever we write out a mask image, we should define the bits which represent mask concepts
+	    if (file->type == PM_FPA_FILE_MASK) {
+		assert (hdu->header);
+		if (!pmConfigMaskWriteHeader (config, hdu->header)) {
+		    psError(PS_ERR_UNKNOWN, false, "failed to set the bitmask names in the PHU header for Image %s (%s)\n", file->filename, file->name);
+		    return false;
+		}
+	    }
         }
 
@@ -792,4 +802,13 @@
           }
 
+	  // XXX if we have a mask file, then we need to read the mask bit names
+	  // defined for this file
+	  if (file->type == PM_FPA_FILE_MASK) {
+	    if (!pmConfigMaskReadHeader (config, phu)) {
+		psError(PS_ERR_IO, false, "error in mask bits");
+		return false;
+	    }
+	  }
+
           // determine the current format from the header
           // determine camera if not specified already
Index: trunk/psModules/src/camera/pmHDUUtils.c
===================================================================
--- trunk/psModules/src/camera/pmHDUUtils.c	(revision 18364)
+++ trunk/psModules/src/camera/pmHDUUtils.c	(revision 18554)
@@ -9,4 +9,25 @@
 #include "pmFPA.h"
 #include "pmHDUUtils.h"
+
+pmHDU *pmHDUGetFirst (const pmFPA *fpa) {
+
+    // XXX we probably should have an indicator in pmFPA about the depths.
+
+    if (!fpa) return NULL;
+    if (fpa->hdu) return fpa->hdu;
+
+    for (int i = 0; i < fpa->chips->n; i++) {
+	pmChip *chip = fpa->chips->data[i];
+	if (!chip) continue;
+	if (chip->hdu) return chip->hdu;
+	if (!chip->cells) continue;
+	for (int j = 0; j < chip->cells->n; j++) {
+	    pmCell *cell = chip->cells->data[j];
+	    if (!cell) continue;
+	    if (cell->hdu) return cell->hdu;
+	}
+    }
+    return NULL;
+}
 
 pmHDU *pmHDUFromFPA(const pmFPA *fpa)
Index: trunk/psModules/src/camera/pmHDUUtils.h
===================================================================
--- trunk/psModules/src/camera/pmHDUUtils.h	(revision 18364)
+++ trunk/psModules/src/camera/pmHDUUtils.h	(revision 18554)
@@ -4,6 +4,6 @@
  * @author Paul Price, IfA
  *
- * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-03-30 21:12:56 $
+ * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-07-15 20:25:00 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -14,4 +14,7 @@
 /// @addtogroup Camera Camera Layout
 /// @{
+
+/// Get the first HDU encountered in the hierarchy
+pmHDU *pmHDUGetFirst (const pmFPA *fpa);
 
 /// Get the lowest HDU in the hierarchy
