Index: /branches/eam_branch_20081230/ppSim/src/ppSimPhotom.c
===================================================================
--- /branches/eam_branch_20081230/ppSim/src/ppSimPhotom.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppSim/src/ppSimPhotom.c	(revision 21090)
@@ -15,6 +15,5 @@
     psphotInit ();
 
- XXX : should this be a psImageMaskType?
-    int blankMask = 0;		// XXX not sure what this should be set to...
+    psImageMaskType blankMask = 1;		// XXX not sure what this should be set to...
     ppSimMosaicChip(config, blankMask, view, "PPSIM.FORCE.CHIP", "PPSIM.INPUT");
     ppSimMosaicChip(config, blankMask, view, "PPSIM.FAKE.CHIP", "PPSIM.OUTPUT");
Index: /branches/eam_branch_20081230/ppStack/src/ppStackArguments.c
===================================================================
--- /branches/eam_branch_20081230/ppStack/src/ppStackArguments.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppStack/src/ppStackArguments.c	(revision 21090)
@@ -79,6 +79,6 @@
         } \
     } \
-    psMaskType value = pmConfigMaskGet(name, config); \
-    psMetadataAddU8(recipe, PS_LIST_TAIL, RECIPENAME, PS_META_REPLACE, NULL, value); \
+    psImageMaskType value = pmConfigMaskGet(name, config); \
+    psMetadataAddImageMask(recipe, PS_LIST_TAIL, RECIPENAME, PS_META_REPLACE, NULL, value); \
 }
 
Index: /branches/eam_branch_20081230/ppStack/src/ppStackLoop.c
===================================================================
--- /branches/eam_branch_20081230/ppStack/src/ppStackLoop.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppStack/src/ppStackLoop.c	(revision 21090)
@@ -401,5 +401,5 @@
     int numGood = 0;                    // Number of good frames
     int numCols = 0, numRows = 0;       // Size of image
-    psVector *inputMask = psVectorAlloc(num, PS_TYPE_U8); // Mask for inputs
+    psVector *inputMask = psVectorAlloc(num, PS_TYPE_VECTOR_MASK); // Mask for inputs
     psVectorInit(inputMask, 0);
     psVector *matchChi2 = psVectorAlloc(num, PS_TYPE_F32); // chi^2 for stamps when matching
@@ -445,5 +445,5 @@
                           sourceLists->data[i], targetPSF, rng, config)) {
             psErrorStackPrint(stderr, "Unable to match image %d --- ignoring.", i);
-            inputMask->data.U8[i] = PPSTACK_MASK_MATCH;
+            inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PPSTACK_MASK_MATCH;
             psErrorClear();
             continue;
@@ -501,5 +501,5 @@
         psVector *values = psVectorAllocEmpty(num, PS_TYPE_F32); // Values to sort
         for (int i = 0; i < num; i++) {
-            if (inputMask->data.PS_TYPE_MASK_DATA[i] & PPSTACK_MASK_ALL) {
+            if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] & PPSTACK_MASK_ALL) {
                 continue;
             }
@@ -534,10 +534,10 @@
             numGood = 0;                    // Number of good images
             for (int i = 0; i < num; i++) {
-                if (inputMask->data.PS_TYPE_MASK_DATA[i] & PPSTACK_MASK_ALL) {
+	      if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] & PPSTACK_MASK_ALL) {
                     continue;
                 }
                 if (matchChi2->data.F32[i] > thresh) {
                     numRej++;
-                    inputMask->data.PS_TYPE_MASK_DATA[i] |= PPSTACK_MASK_CHI2;
+                    inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] |= PPSTACK_MASK_CHI2;
                     psLogMsg("ppStack", PS_LOG_INFO, "Rejecting image %d because of large matching chi^2: %f",
                              i, matchChi2->data.F32[i]);
@@ -612,5 +612,5 @@
 
         psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits to mask for bad
-        psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+        psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
         if (!pmReadoutStackDefineOutput(outRO, col0, row0, numCols, numRows, true, true, maskBad)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to prepare output.");
@@ -714,5 +714,5 @@
         inspect = psArrayAlloc(num);
         for (int i = 0; i < num; i++) {
-            if (inputMask->data.U8[i]) {
+            if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
                 continue;
             }
@@ -725,5 +725,5 @@
             psArray *results = job->results; // Results of job
             for (int i = 0; i < num; i++) {
-                if (inputMask->data.U8[i]) {
+                if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
                     continue;
                 }
@@ -751,5 +751,5 @@
         // Count images rejected out of hand
         for (int i = 0; i < num; i++) {
-            if (inputMask->data.U8[i]) {
+            if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
                 numRejected++;
             }
@@ -757,5 +757,5 @@
 
         for (int i = 0; i < num; i++) {
-            if (inputMask->data.U8[i]) {
+            if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
                 continue;
             }
@@ -802,5 +802,5 @@
         // Reject bad pixels
         for (int i = 0; i < num; i++) {
-            if (inputMask->data.U8[i]) {
+            if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
                 continue;
             }
@@ -845,5 +845,5 @@
                 psWarning("Rejection on image %d didn't work --- reject entire image.", i);
                 numRejected++;
-                inputMask->data.U8[i] = PPSTACK_MASK_REJECT;
+                inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PPSTACK_MASK_REJECT;
             } else {
                 float frac = reject->n / (float)(numCols * numRows); // Pixel fraction
@@ -856,5 +856,5 @@
                     // reject == NULL means reject image completely
                     reject = NULL;
-                    inputMask->data.U8[i] = PPSTACK_MASK_BAD;
+                    inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PPSTACK_MASK_BAD;
                     numRejected++;
                 }
@@ -968,5 +968,5 @@
     {
         psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits for bad
-        psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+        psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
         if (!pmReadoutMaskApply(outRO, maskBad)) {
             psWarning("Unable to apply mask");
@@ -979,5 +979,5 @@
     float totExposure = 0.0;            // Total exposure time
     for (int i = 0; i < num; i++) {
-        if (inputMask->data.U8[i]) {
+        if (inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
             continue;
         }
@@ -1044,5 +1044,5 @@
         pmCell *cell1 = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT.JPEG1");
         pmCell *cell2 = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT.JPEG2");
-        psMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
+        psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
 
         pmReadout *ro1 = pmReadoutAlloc(cell1), *ro2 = pmReadoutAlloc(cell2); // Binned readouts
@@ -1066,5 +1066,5 @@
         int renormNum = psMetadataLookupS32(&mdok, recipe, "RENORM.NUM"); // Number of samples
         float renormWidth = psMetadataLookupS32(&mdok, recipe, "RENORM.WIDTH"); // Width of Gaussian phot
-        psMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
+        psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
         if (!pmReadoutWeightRenormPhot(outRO, maskValue, renormNum, renormWidth,
                                        renormMean, renormStdev, NULL)) {
@@ -1112,5 +1112,5 @@
         psTrace("ppStack", 1, "Gathering statistics on stacked image....\n");
         psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits for bad
-        psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+        psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
 
         ppStatsFPA(stats, outRO->parent->parent->parent, view, maskBad, config);
Index: /branches/eam_branch_20081230/ppStack/src/ppStackMatch.c
===================================================================
--- /branches/eam_branch_20081230/ppStack/src/ppStackMatch.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppStack/src/ppStackMatch.c	(revision 21090)
@@ -104,5 +104,5 @@
 
 // Generate a background model of the readout we're matching
-psImage *stackBackgroundModel(pmReadout *ro, psMaskType maskVal, const psArray *sources, int size)
+psImage *stackBackgroundModel(pmReadout *ro, psImageMaskType maskVal, const psArray *sources, int size)
 {
     psImage *image = ro->image, *mask = ro->mask; // Image and mask of readout
@@ -169,9 +169,9 @@
 
     psString maskValStr = psMetadataLookupStr(NULL, recipe, "MASK.VAL"); // Name of bits to mask going in
-    psMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
+    psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
     psString maskPoorStr = psMetadataLookupStr(NULL, recipe, "MASK.POOR"); // Name of bits to mask for poor
-    psMaskType maskPoor = pmConfigMaskGet(maskPoorStr, config); // Bits to mask for poor pixels
+    psImageMaskType maskPoor = pmConfigMaskGet(maskPoorStr, config); // Bits to mask for poor pixels
     psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits to mask for bad
-    psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+    psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
 
     bool mdok;                          // Status of MD lookup
Index: /branches/eam_branch_20081230/ppStack/src/ppStackPhotometry.c
===================================================================
--- /branches/eam_branch_20081230/ppStack/src/ppStackPhotometry.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppStack/src/ppStackPhotometry.c	(revision 21090)
@@ -45,5 +45,5 @@
         return false;
     }
-    psMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask
+    psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask
 
     psImage *image = ro->image, *mask = ro->mask; // Image and mask from readout
@@ -85,5 +85,5 @@
             float dy2 = PS_SQR(y - v); // Distance from centroid
             for (int u = xMin; u <= xMax; u++) {
-                if (mask->data.PS_TYPE_MASK_DATA[v][u] & maskVal) {
+                if (mask->data.PS_TYPE_IMAGE_MASK_DATA[v][u] & maskVal) {
                     numBadPix++;
                     continue;
@@ -133,8 +133,8 @@
 
     // set maskValue and markValue in the psphot recipe
-    psMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
-    psMaskType markValue = pmConfigMaskGet("MARK.VALUE", config); // Bits to use for marking
-    psMetadataAddU8 (recipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "Bits to mask", maskValue);
-    psMetadataAddU8 (recipe, PS_LIST_TAIL, "MARK.PSPHOT", PS_META_REPLACE, "Bits to use for mark", markValue);
+    psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
+    psImageMaskType markValue = pmConfigMaskGet("MARK.VALUE", config); // Bits to use for marking
+    psMetadataAddImageMask (recipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "Bits to mask", maskValue);
+    psMetadataAddImageMask (recipe, PS_LIST_TAIL, "MARK.PSPHOT", PS_META_REPLACE, "Bits to use for mark", markValue);
 
     if (!psphotReadout(config, view)) {
Index: /branches/eam_branch_20081230/ppStack/src/ppStackReadout.c
===================================================================
--- /branches/eam_branch_20081230/ppStack/src/ppStackReadout.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppStack/src/ppStackReadout.c	(revision 21090)
@@ -114,7 +114,7 @@
 
     psString maskValStr = psMetadataLookupStr(NULL, recipe, "MASK.VAL"); // Name of bits to mask going in
-    psMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
+    psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
     psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits to mask for bad
-    psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+    psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
 
     int num = readouts->n;              // Number of inputs
@@ -141,5 +141,5 @@
         // Ensure there is a mask, or pmStackCombine will complain
         if (!ro->mask) {
-            ro->mask = psImageAlloc(ro->image->numCols, ro->image->numRows, PS_TYPE_MASK);
+            ro->mask = psImageAlloc(ro->image->numCols, ro->image->numRows, PS_TYPE_IMAGE_MASK);
             psImageInit(ro->mask, 0);
         }
@@ -208,7 +208,7 @@
 
     psString maskValStr = psMetadataLookupStr(NULL, recipe, "MASK.VAL"); // Name of bits to mask going in
-    psMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
+    psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
     psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits to mask for bad
-    psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+    psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
 
     int num = readouts->n;              // Number of inputs
Index: /branches/eam_branch_20081230/ppStack/src/ppStackSources.c
===================================================================
--- /branches/eam_branch_20081230/ppStack/src/ppStackSources.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppStack/src/ppStackSources.c	(revision 21090)
@@ -109,5 +109,5 @@
             psVectorInit(mag, NAN);
             for (int j = 0; j < match->num; j++) {
-                if (match->mask->data.PS_TYPE_MASK_DATA[j]) {
+                if (match->mask->data.PS_TYPE_VECTOR_MASK_DATA[j]) {
                     continue;
                 }
Index: /branches/eam_branch_20081230/ppSub/src/ppSubArguments.c
===================================================================
--- /branches/eam_branch_20081230/ppSub/src/ppSubArguments.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppSub/src/ppSubArguments.c	(revision 21090)
@@ -216,6 +216,6 @@
     psMetadataAddF32(arguments, PS_LIST_TAIL, "-rej", 0, "Rejection thresold (sigma)", NAN);
     psMetadataAddF32(arguments, PS_LIST_TAIL, "-sys", 0, "Relative systematic error in kernel", NAN);
-    psMetadataAddU8(arguments,  PS_LIST_TAIL, "-mask-bad", 0, "Mask value for bad pixels", 0);
-    psMetadataAddU8(arguments,  PS_LIST_TAIL, "-mask-poor", 0, "Mask value for poor pixels", 0);
+    psMetadataAddImageMask(arguments,  PS_LIST_TAIL, "-mask-bad", 0, "Mask value for bad pixels", 0);
+    psMetadataAddImageMask(arguments,  PS_LIST_TAIL, "-mask-poor", 0, "Mask value for poor pixels", 0);
     psMetadataAddF32(arguments,  PS_LIST_TAIL, "-poor-frac", 0, "Fraction of weight for poor pixels", NAN);
     psMetadataAddF32(arguments, PS_LIST_TAIL, "-badfrac", 0, "Maximum fraction of bad pixels to accept", 1.0);
Index: /branches/eam_branch_20081230/ppSub/src/ppSubBackground.c
===================================================================
--- /branches/eam_branch_20081230/ppSub/src/ppSubBackground.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppSub/src/ppSubBackground.c	(revision 21090)
@@ -30,8 +30,8 @@
 
     psString maskBadStr = psMetadataLookupStr(NULL, ppSubRecipe, "MASK.BAD"); // Name of bits to mask for bad
-    psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+    psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
 
     // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
-    psMetadataAddU8(psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskBad);
+    psMetadataAddImageMask(psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskBad);
 
     psImage *image = ro->image, *mask = ro->mask; // Image and mask of interest
@@ -59,5 +59,5 @@
     for (int y = 0; y < numRows; y++) {
         for (int x = 0; x < numCols; x++) {
-            if (mask && mask->data.PS_TYPE_MASK_DATA[y][x] & maskBad) {
+            if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskBad) {
                 image->data.F32[y][x] = 0.0;
             } else {
@@ -65,5 +65,5 @@
                 if (!isfinite(value)) {
                     image->data.F32[y][x] = NAN;
-                    mask->data.PS_TYPE_MASK_DATA[y][x] |= maskBad;
+                    mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskBad;
                 } else {
                     image->data.F32[y][x] -= value;
Index: /branches/eam_branch_20081230/ppSub/src/ppSubReadout.c
===================================================================
--- /branches/eam_branch_20081230/ppSub/src/ppSubReadout.c	(revision 21089)
+++ /branches/eam_branch_20081230/ppSub/src/ppSubReadout.c	(revision 21090)
@@ -103,9 +103,9 @@
     float penalty = psMetadataLookupF32(NULL, recipe, "PENALTY"); // Penalty for wideness
     psString maskValStr = psMetadataLookupStr(NULL, recipe, "MASK.IN"); // Name of bits to mask going in
-    psMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
+    psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
     psString maskPoorStr = psMetadataLookupStr(NULL, recipe, "MASK.POOR"); // Name of bits to mask for poor
-    psMaskType maskPoor = pmConfigMaskGet(maskPoorStr, config); // Bits to mask for poor pixels
+    psImageMaskType maskPoor = pmConfigMaskGet(maskPoorStr, config); // Bits to mask for poor pixels
     psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits to mask for bad
-    psMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
+    psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
     float badFrac = psMetadataLookupF32(NULL, recipe, "BADFRAC"); // Maximum bad fraction
     const char *stampsName = psMetadataLookupStr(&mdok, config->arguments, "STAMPS"); // Filename for stamps
@@ -146,5 +146,5 @@
             pmReadoutSetMask(inRO, pmConfigMaskGet("SAT", config), pmConfigMaskGet("BAD", config));
         } else {
-            inRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_MASK);
+            inRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_IMAGE_MASK);
             psImageInit(inRO->mask, 0);
         }
@@ -154,5 +154,5 @@
             pmReadoutSetMask(refRO, pmConfigMaskGet("SAT", config), pmConfigMaskGet("BAD", config));
         } else {
-            refRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_MASK);
+            refRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_IMAGE_MASK);
             psImageInit(refRO->mask, 0);
         }
@@ -363,10 +363,10 @@
         }
         if (minuend->mask) {
-            outRO->mask = psImageCopy(outRO->mask, minuend->mask, PS_TYPE_MASK);
+            outRO->mask = psImageCopy(outRO->mask, minuend->mask, PS_TYPE_IMAGE_MASK);
         }
         outRO->data_exists = outCell->data_exists = outCell->parent->data_exists = true;
 
         if (psMetadataLookupBool(&mdok, recipe, "RENORM")) {
-            psMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
+            psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
             if (!pmReadoutWeightRenormPhot(outRO, maskValue, renormNum, renormWidth,
                                            renormMean, renormStdev, NULL)) {
@@ -399,8 +399,8 @@
 
         // set maskValue and markValue in the psphot recipe
-        psMaskType maskValue = maskVal;
-        psMaskType markValue = pmConfigMaskGet("MARK.VALUE", config); // Bits to use for marking
-        psMetadataAddU8(psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "Bits to mask", maskValue);
-        psMetadataAddU8(psphotRecipe, PS_LIST_TAIL, "MARK.PSPHOT", PS_META_REPLACE, "Bits to use for marking",
+        psImageMaskType maskValue = maskVal;
+        psImageMaskType markValue = pmConfigMaskGet("MARK.VALUE", config); // Bits to use for marking
+        psMetadataAddImageMask(psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "Bits to mask", maskValue);
+        psMetadataAddImageMask(psphotRecipe, PS_LIST_TAIL, "MARK.PSPHOT", PS_META_REPLACE, "Bits to use for marking",
                         markValue);
 
@@ -477,6 +477,6 @@
     for (int y = 0; y < outRO->image->numRows; y++) {
         for (int x = 0; x < outRO->image->numCols; x++) {
-            if (isnan(outRO->image->data.F32[y][x]) && !(outRO->mask->data.U8[y][x] & maskVal)) {
-                printf("Unmasked NAN at %d %d --> %d\n", x, y, outRO->mask->data.U8[y][x]);
+            if (isnan(outRO->image->data.F32[y][x]) && !(outRO->mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal)) {
+                printf("Unmasked NAN at %d %d --> %d\n", x, y, outRO->mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x]);
             }
         }
@@ -568,5 +568,5 @@
 
         if (psMetadataLookupBool(&mdok, recipe, "RENORM")) {
-            psMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
+            psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
             if (!pmReadoutWeightRenormPhot(outRO, maskValue, renormNum, renormWidth,
                                            renormMean, renormStdev, NULL)) {
@@ -669,5 +669,5 @@
     // Renormalising for pixels, because that's what magic desires
     if (psMetadataLookupBool(&mdok, recipe, "RENORM")) {
-        psMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
+        psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
         if (!pmReadoutWeightRenormPixels(outRO, maskValue, renormMean, renormStdev, NULL)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to renormalise variances.");
Index: /branches/eam_branch_20081230/psphot/src/psphotMakeResiduals.c
===================================================================
--- /branches/eam_branch_20081230/psphot/src/psphotMakeResiduals.c	(revision 21089)
+++ /branches/eam_branch_20081230/psphot/src/psphotMakeResiduals.c	(revision 21090)
@@ -1,6 +1,4 @@
 # include "psphotInternal.h"
 
-XXX this function is probably broken: fmasks is confused between Image and Vector.  it is used by psVectorStats, so it must be Vector, but 
-  is populated by mflux, which seems to be Image..
 bool psphotMakeResiduals (psArray *sources, psMetadata *recipe, pmPSF *psf, psImageMaskType maskVal) {
 
@@ -71,8 +69,15 @@
     // - set output pixel, weight, and mask
 
-    XXX need to set these correctly based on the RECIPE values
-    const int badMask = 1;              // mask bits
-    const int poorMask = 2;             //       from psImageInterpolate
-    const int clippedMask = 4;          // mask bit set for clipped values
+    // these mask values do not correspond to the recipe values: they
+    // are not propagated to images: they just need to fit in an 8-bit
+    // value.  they are supplied to psImageInterpolate, which takes a
+    // psImageMaskType; the mask portion of the result from
+    // psImageInterpolate is supplied to fmasks, which is then used by
+    // psVectorStats
+
+    const psImageMaskType badMask     = 0x01;	// mask bits
+    const psImageMaskType poorMask    = 0x02;	// from psImageInterpolate
+    const psImageMaskType clippedMask = 0x04;	// mask bit set for clipped values
+    const psVectorMaskType fmaskVal = badMask | poorMask | clippedMask;
 
     // determine the maximum image size from the input sources
@@ -105,9 +110,9 @@
         psBinaryOp (weight, weight, "/", psScalarAlloc(Io*Io, PS_TYPE_F32));
 
-        // we will interpolate the image and weight - include the mask or not?
-        // XXX consider better values for the mask bits
-        psImageInterpolation *interp =
-            psImageInterpolationAlloc(mode, image, weight, NULL, 0xff, 0.0, 0.0, badMask, poorMask, 0.0, 0);
-        psArrayAdd (input,  100, interp);
+        // we interpolate the image and weight - include the mask or not?
+	// XXX why not the mask?
+        // psImageInterpolation *interp = psImageInterpolationAlloc(mode, image, weight, mask, maskVal, 0.0, 0.0, badMask, poorMask, 0.0, 0);
+        psImageInterpolation *interp = psImageInterpolationAlloc(mode, image, weight, NULL, 0xff, 0.0, 0.0, badMask, poorMask, 0.0, 0);
+        psArrayAdd (input, 100, interp);
 
         // save the X,Y position for future reference
@@ -166,14 +171,15 @@
                 bool offImage = false;
                 if (psImageInterpolate (&flux, &dflux, &mflux, ix, iy, interp) == PS_INTERPOLATE_STATUS_OFF) {
+                    // fprintf (stderr, "off image: %f %f : %f %f\n", ix, iy, flux, dflux);
                     // This pixel is off the image
                     offImage = true;
-                    fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = 1;
-                    // fprintf (stderr, "off image: %f %f : %f %f\n", ix, iy, flux, dflux);
-                }
-                fluxes->data.F32[i] = flux;
-                dfluxes->data.F32[i] = dflux;
-                fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = mflux; // XXX is mflux IMAGE or VECTOR type?
+                    fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = badMask;
+                } else {
+		  fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = mflux; // XXX is mflux IMAGE or VECTOR type?
+		}
+		fluxes->data.F32[i] = flux;
+		dfluxes->data.F32[i] = dflux;
                 if (isnan(flux)) {
-                    fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = 1;
+                    fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = badMask;
                 }
                 if (fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] == 0) {
@@ -182,5 +188,5 @@
             }
 
-            // skip pixels which are off the image...
+            // skip pixels with insufficient data
             bool validPixel = (SPATIAL_ORDER == 0) ? (nGoodPixel > 1) : (nGoodPixel > 3);
             if (!validPixel) {
@@ -188,5 +194,5 @@
                 resid->Rx->data.F32[oy][ox] = 0.0;
                 resid->Ry->data.F32[oy][ox] = 0.0;
-                resid->mask->data.PS_TYPE_IMAGE_MASK_DATA[oy][ox] = 1;
+                resid->mask->data.PS_TYPE_IMAGE_MASK_DATA[oy][ox] = badMask;
                 continue;
             }
@@ -194,5 +200,5 @@
             // measure the robust median to determine a baseline reference value
             *fluxClip = *fluxClipDef;
-            psVectorStats (fluxClip, fluxes, NULL, fmasks, 0xff);
+            psVectorStats (fluxClip, fluxes, NULL, fmasks, fmaskVal);
             psErrorClear();             // clear (ignore) any outstanding errors
 
@@ -204,5 +210,5 @@
                 float swing = fabs(delta) / sigma;
 
-                // make this a user option
+		// mask pixels which are out of range
                 if (swing > nSigma) {
                     fmasks->data.PS_TYPE_VECTOR_MASK_DATA[i] = clippedMask;
@@ -214,5 +220,5 @@
                 // measure the desired statistic on the unclipped pixels
                 *fluxStats = *fluxStatsDef;
-                psVectorStats (fluxStats, fluxes, NULL, fmasks, 0xff);
+                psVectorStats (fluxStats, fluxes, NULL, fmasks, fmaskVal);
                 psErrorClear();         // clear (ignore) any outstanding errors
 
Index: /branches/eam_branch_20081230/psphot/src/psphotModelWithPSF.c
===================================================================
--- /branches/eam_branch_20081230/psphot/src/psphotModelWithPSF.c	(revision 21089)
+++ /branches/eam_branch_20081230/psphot/src/psphotModelWithPSF.c	(revision 21090)
@@ -20,5 +20,5 @@
         paramMask = constraint->paramMask;
         if (paramMask != NULL) {
-	  PS_ASSERT_VECTOR_TYPE(paramMask, PS_TYPE_VECTOR_TYPE, false);
+	  PS_ASSERT_VECTOR_TYPE(paramMask, PS_TYPE_VECTOR_MASK, false);
             PS_ASSERT_VECTORS_SIZE_EQUAL(params, paramMask, false);
         }
Index: /branches/eam_branch_20081230/pswarp/src/pswarpLoop.c
===================================================================
--- /branches/eam_branch_20081230/pswarp/src/pswarpLoop.c	(revision 21089)
+++ /branches/eam_branch_20081230/pswarp/src/pswarpLoop.c	(revision 21090)
@@ -80,5 +80,5 @@
 
     // output mask bits
-    psMaskType maskValue = psMetadataLookupU8(&status, recipe, "MASK.OUTPUT");
+    psImageMaskType maskValue = psMetadataLookupImageMask(&status, recipe, "MASK.OUTPUT");
     psAssert (status, "MASK.OUTPUT was not defined");
 
Index: /branches/eam_branch_20081230/pswarp/src/pswarpPixelFraction.c
===================================================================
--- /branches/eam_branch_20081230/pswarp/src/pswarpPixelFraction.c	(revision 21089)
+++ /branches/eam_branch_20081230/pswarp/src/pswarpPixelFraction.c	(revision 21090)
@@ -20,5 +20,5 @@
     PS_ASSERT_IMAGE_NON_NULL(readout->mask, false);
     PS_ASSERT_IMAGES_SIZE_EQUAL(readout->mask, readout->image, false);
-    PS_ASSERT_IMAGE_TYPE(readout->mask, PS_TYPE_MASK, false);
+    PS_ASSERT_IMAGE_TYPE(readout->mask, PS_TYPE_IMAGE_MASK, false);
 
     if (stats) {
@@ -40,5 +40,5 @@
 
     // output mask bits
-    psMaskType maskValue = psMetadataLookupU8(&status, recipe, "MASK.OUTPUT"); 
+    psImageMaskType maskValue = psMetadataLookupImageMask(&status, recipe, "MASK.OUTPUT"); 
     psAssert (status, "MASK.OUTPUT was not defined");
 
@@ -55,5 +55,5 @@
     for (int y = 0; y < numRows; y++) {
         for (int x = 0; x < numCols; x++) {
-            if (mask->data.PS_TYPE_MASK_DATA[y][x] & maskValue) {
+            if (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskValue) {
                 numBad++;
             } else {
Index: /branches/eam_branch_20081230/pswarp/src/pswarpSetMaskBits.c
===================================================================
--- /branches/eam_branch_20081230/pswarp/src/pswarpSetMaskBits.c	(revision 21089)
+++ /branches/eam_branch_20081230/pswarp/src/pswarpSetMaskBits.c	(revision 21090)
@@ -10,8 +10,7 @@
 bool pswarpSetMaskBits (pmConfig *config) {
 
-    psMaskType maskIn = 0x00;			// mask for the input image
-    psMaskType markIn = 0x00;			// mark for the input image
-    psMaskType maskOut = 0x00;			// mask for the output image
-    psMaskType markOut = 0x00;			// mark for the output image
+    psImageMaskType maskIn = 0x00;			// mask for the input image
+    psImageMaskType markIn = 0x00;			// mark for the input image
+    psImageMaskType maskOut = 0x00;			// mask for the output image
 
     // this function sets the required single-image mask bits
@@ -22,5 +21,5 @@
 
     // mask for non-linear flat regions (default to DETECTOR if not defined)
-    psMaskType badMask = pmConfigMaskGet("BAD.WARP", config); 
+    psImageMaskType badMask = pmConfigMaskGet("BAD.WARP", config); 
     if (!badMask) {
 	badMask = 0x01;
@@ -30,5 +29,5 @@
 
     // mask for non-linear flat regions (default to DETECTOR if not defined)
-    psMaskType poorMask = pmConfigMaskGet("POOR.WARP", config); 
+    psImageMaskType poorMask = pmConfigMaskGet("POOR.WARP", config); 
     if (!poorMask) {
 	poorMask = 0x02;
@@ -38,18 +37,18 @@
 
     // search for an unset bit to use for MARK:
-    markOut = 0x80;
+    psImageMaskType markOut   = 0x00;
+    psImageMaskType markTrial = 0x01;
 
-    int nBits = sizeof(psMaskType) * 8;
+    int nBits = sizeof(psImageMaskType) * 8;
     for (int i = 0; !markOut && (i < nBits); i++) {
-	if (maskOut & markOut) {
-	    markOut >>= 1;
-	} else {
-	    markOut = markOut;
-	}
+        if (maskOut & markTrial) {
+            markTrial <<= 1;
+        } else {
+            markOut = markTrial;
+        }
     }
-
     if (!markOut) {
-	psError (PS_ERR_UNKNOWN, true, "Unable to define the MARK bit mask: all bits taken!");
-	return false;
+        psError (PS_ERR_UNKNOWN, true, "Unable to define the MARK bit mask: all bits taken!");
+        return false;
     }
 
@@ -63,8 +62,8 @@
     // set maskOut and markOut in the psphot recipe
     // NOTE: psphot works on the output images, not input images, so set the MARK and MASK correctly here
-    psMetadataAddU8 (warpRecipe, PS_LIST_TAIL, "MASK.INPUT",  PS_META_REPLACE, "user-defined mask", maskIn);
-    psMetadataAddU8 (warpRecipe, PS_LIST_TAIL, "MARK.INPUT",  PS_META_REPLACE, "user-defined mask", markIn);
-    psMetadataAddU8 (warpRecipe, PS_LIST_TAIL, "MASK.OUTPUT", PS_META_REPLACE, "user-defined mask", maskOut);
-    psMetadataAddU8 (warpRecipe, PS_LIST_TAIL, "MARK.OUTPUT", PS_META_REPLACE, "user-defined mask", markOut);
+    psMetadataAddImageMask (warpRecipe, PS_LIST_TAIL, "MASK.INPUT",  PS_META_REPLACE, "user-defined mask", maskIn);
+    psMetadataAddImageMask (warpRecipe, PS_LIST_TAIL, "MARK.INPUT",  PS_META_REPLACE, "user-defined mask", markIn);
+    psMetadataAddImageMask (warpRecipe, PS_LIST_TAIL, "MASK.OUTPUT", PS_META_REPLACE, "user-defined mask", maskOut);
+    psMetadataAddImageMask (warpRecipe, PS_LIST_TAIL, "MARK.OUTPUT", PS_META_REPLACE, "user-defined mask", markOut);
 
     // update the psphot recipe
@@ -77,6 +76,6 @@
     // set maskOut and markOut in the psphot recipe
     // NOTE: psphot works on the output images, not input images, so set the MARK and MASK correctly here
-    psMetadataAddU8 (psphotRecipe, PS_LIST_TAIL, "MARK.PSPHOT", PS_META_REPLACE, "user-defined mask", markOut);
-    psMetadataAddU8 (psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskOut);
+    psMetadataAddImageMask (psphotRecipe, PS_LIST_TAIL, "MARK.PSPHOT", PS_META_REPLACE, "user-defined mask", markOut);
+    psMetadataAddImageMask (psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskOut);
 
     return true;
Index: /branches/eam_branch_20081230/pswarp/src/pswarpTransformReadout.c
===================================================================
--- /branches/eam_branch_20081230/pswarp/src/pswarpTransformReadout.c	(revision 21089)
+++ /branches/eam_branch_20081230/pswarp/src/pswarpTransformReadout.c	(revision 21090)
@@ -22,7 +22,7 @@
 
     // output mask bits
-    psMaskType maskIn   = psMetadataLookupU8(&mdok, recipe, "MASK.INPUT");
-    psMaskType maskPoor = pmConfigMaskGet("POOR.WARP", config);
-    psMaskType maskBad  = pmConfigMaskGet("BAD.WARP", config);
+    psImageMaskType maskIn   = psMetadataLookupImageMask(&mdok, recipe, "MASK.INPUT");
+    psImageMaskType maskPoor = pmConfigMaskGet("POOR.WARP", config);
+    psImageMaskType maskBad  = pmConfigMaskGet("BAD.WARP", config);
     psAssert(mdok, "MASK.INPUT was not defined");
 
@@ -80,5 +80,5 @@
     }
     if ((input->mask || maskPoor || maskBad) && !output->mask) {
-        output->mask = psImageAlloc(output->image->numCols, output->image->numRows, PS_TYPE_MASK);
+        output->mask = psImageAlloc(output->image->numCols, output->image->numRows, PS_TYPE_IMAGE_MASK);
         psImageInit(output->mask, maskBad);
     }
Index: /branches/eam_branch_20081230/pswarp/src/pswarpTransformTile.c
===================================================================
--- /branches/eam_branch_20081230/pswarp/src/pswarpTransformTile.c	(revision 21089)
+++ /branches/eam_branch_20081230/pswarp/src/pswarpTransformTile.c	(revision 21090)
@@ -49,6 +49,6 @@
     psF32 **outImageData     = args->output->image->data.F32;
     psF32 **outVarData       = (args->output->weight) ? args->output->weight->data.F32 : NULL;
-    psMaskType **outMaskData = (args->output->mask)   ? args->output->mask->data.PS_TYPE_MASK_DATA : NULL;
-    psMaskType **inMaskData  = (args->input->mask)    ? args->input->mask->data.PS_TYPE_MASK_DATA : NULL;
+    psImageMaskType **outMaskData = (args->output->mask)   ? args->output->mask->data.PS_TYPE_IMAGE_MASK_DATA : NULL;
+    psImageMaskType **inMaskData  = (args->input->mask)    ? args->input->mask->data.PS_TYPE_IMAGE_MASK_DATA : NULL;
 
     pswarpMap *map = args->grid->maps[args->gridX][args->gridY]; // Map for this tile
@@ -67,5 +67,5 @@
 
             // Only transform those pixels requested
-            if (region && region->data.U8[y][x]) {
+            if (region && region->data.PS_TYPE_IMAGE_MASK_DATA[y][x]) {
                 continue;
             }
@@ -82,5 +82,5 @@
             // psImagePixelInterpolate determines the value at pixel coordinate (x,y) in child coordinates
             double imageValue, varValue; // Value of image and variance map
-            psMaskType maskValue = inMaskData ? inMaskData[(int)yIn][(int)xIn] : 0; // Value of mask
+            psImageMaskType maskValue = inMaskData ? inMaskData[(int)yIn][(int)xIn] : 0; // Value of mask
             if (!psImageInterpolate(&imageValue, &varValue, &maskValue, xIn, yIn, args->interp)) {
                 psError(PS_ERR_UNKNOWN, false, "Unable to interpolate image.");
