Index: trunk/ppImage/src/ppImageAuxiliaryMask.c
===================================================================
--- trunk/ppImage/src/ppImageAuxiliaryMask.c	(revision 35722)
+++ trunk/ppImage/src/ppImageAuxiliaryMask.c	(revision 36169)
@@ -35,4 +35,8 @@
         return NULL;
     }
+    if (image->type.type != PS_TYPE_IMAGE_MASK) {
+        psWarning("auxiliary mask image %s has unexpected type %d\n", realName, image->type.type);
+        return false;
+    }
     psFree(realName);
 
@@ -72,27 +76,10 @@
     }
 
-    // Find a suitable detRun with type AUXMASK
-
-    psTime *time = psMetadataLookupPtr(NULL, input->fpa->concepts, "FPA.TIME");
-    if (time->sec == 0 && time->nsec == 0) {
-        psLogMsg ("psModules.camera", PS_LOG_WARN, "FPA.TIME has not been set.\n");
-    }
-
-    char *cameraName = psMetadataLookupStr(NULL, input->fpa->concepts, "FPA.CAMERA");
-    pmDetrendSelectOptions *detrendOptions = pmDetrendSelectOptionsAlloc(cameraName, *time, PM_DETREND_TYPE_AUXMASK);
-
-    pmDetrendSelectResults *results = pmDetrendSelect(detrendOptions, config);
-    psFree(detrendOptions);
-    if (!results) {
-        psError (psErrorCodeLast(), false, "no matching auxiliary mask found");
-        return false;
-    }
-
-    pmFPALevel fileLevel = pmFPALevelFromName(results->level);
-    if (fileLevel != PM_FPA_LEVEL_CHIP) {
-         psError (PM_ERR_CONFIG, false, "invalid file level %d for selected auxiliary mask", fileLevel);
-         return false;
-    }
-    
+    pmFPAfile *auxmask = psMetadataLookupPtr(&status, config->files, "PPIMAGE.AUXMASK");
+    if (!status) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "PPIMAGE.CHIP file is not defined");
+        return false;
+    }
+
     // Go find the readout
     // code to find the readouts was adapted from ppImageSubtractBackground
@@ -129,31 +116,38 @@
         return true;
     }
-    // not needed psImage *image = ro->image;
+
     psImage *mask = ro->mask;
 
-    // now read the auxiliary mask file
-    psString class_id = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
-
-    char *auxMaskFileName = pmDetrendFile(results->detID, class_id, config);
-    if (!auxMaskFileName) {
-         psError (PM_ERR_CONFIG, false, "unable to find auxiliary mask file name for %s %s", results->detID, class_id);
-         return false;
-    }
-    psLogMsg ("ppImage", PS_LOG_INFO, "Auxiliary mask file: %s", auxMaskFileName);
-
-    psFree(results);
-    // record that we read this file in the config dump file.
-    // Note: this value isn't used during updates though.
-    pmConfigRunFilenameAddRead(config, "PPIMAGE.AUXMASK", auxMaskFileName);
-    recordFileInHeader(chip, "DETREND.AUXMASK", "auxiliary mask", auxMaskFileName);
-
-    psImage *auxMask = readAuxiliaryMask(config, auxMaskFileName);
-    psFree(auxMaskFileName);
-    if (!auxMask) {
-        psError(PS_ERR_UNKNOWN, false, "failed to read auxiliary mask file");
-        return false;
-    }
-
-    // if the cell has video and the recipe value
+    if (mask->type.type != PS_TYPE_IMAGE_MASK) {
+        psWarning("mask image has unexpected type %d\n", mask->type.type);
+        return false;
+    }
+
+    pmChip *auxMaskChip = pmFPAviewThisChip(view, auxmask->fpa);
+    if (!auxMaskChip) {
+        psWarning("failed to find pmChip for auxiliary mask");
+        return false;
+    }
+    pmCell *auxMaskCell = auxMaskChip->cells->data[0];
+    if (!auxMaskCell) {
+        psWarning("failed to find pmCell for auxiliary mask");
+        return false;
+    }
+    pmReadout *auxMaskReadout = auxMaskCell->readouts->data[0];
+    if (!auxMaskReadout) {
+        psWarning("failed to find pmReadout for auxiliary mask");
+        return false;
+    }
+    psImage *auxMaskImage = auxMaskReadout->mask;
+    if (!auxMaskImage) {
+        psWarning("failed to find psImage for auxiliary mask");
+        return false;
+    }
+    if (auxMaskImage->type.type != PS_TYPE_IMAGE_MASK) {
+        psWarning("auxiliary mask image has unexpected type %d\n", auxMaskImage->type.type);
+        return false;
+    }
+
+    // if the cell has video and the recipe value is set or in the video mask
     if (options->hasVideo && options->auxVideoMask && strcmp(options->auxVideoMask, "NULL")) {
         psImage *videoMask = readAuxiliaryMask(config, options->auxVideoMask);
@@ -166,7 +160,7 @@
         recordFileInHeader(chip, "DETREND.AUXVIDEOMASK", "auxiliary video mask", options->auxVideoMask);
 
-        // compute auxMask *= videoMask
-        if (!psBinaryOp(auxMask, auxMask, "*", videoMask)) {
-            psError(PS_ERR_UNKNOWN, false, "mulitplication of auxiliary mask and auxiliary video mask failed");
+        // compute auxMask |= videoMask
+        if (!psBinaryOp(auxMaskImage, auxMaskImage, "|", videoMask)) {
+            psError(PS_ERR_UNKNOWN, false, "combination of auxiliary mask and auxiliary video mask failed");
             return false;
         }
@@ -174,6 +168,6 @@
     }
 
-    if ((mask->numRows != auxMask->numRows) || (mask->numCols != auxMask->numCols) ||
-        (mask->row0 != auxMask->row0) || (mask->col0 != auxMask->col0)) {
+    if ((mask->numRows != auxMaskImage->numRows) || (mask->numCols != auxMaskImage->numCols) ||
+        (mask->row0 != auxMaskImage->row0) || (mask->col0 != auxMaskImage->col0)) {
         psError(PS_ERR_IO, false, "structure of auxiliary mask does not match this chip");
         return false;
@@ -187,11 +181,12 @@
     psAssert(staticMaskVal, "Need staticMaskVal!");
 
+    // XXX: now that we are using an auxilary mask that matches the format of ps masks, we could just
+    // do psBinaryOp(mask, mask, "|", auxMaskImage) but run an explicit loop so that we can measure
+    // and log the number of additional pixels masked
     int numCols = mask->numCols, numRows = mask->numRows; // Size of image
     unsigned long numMasked = 0;
     for (int y = 0; y < numRows; y++) {
         for (int x = 0; x < numCols; x++) {
-            // auxMask is floating point type image with pixel value 0 meaning mask it
-            // XXX: support other types and get the mask value from the header
-            if (auxMask->data.F32[y][x] == 0) {
+            if (auxMaskImage->data.U16[y][x] != 0) {
                 if ((mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & staticMaskVal) == 0) {
                     // pixel was not previously included in the static mask so mask it
@@ -200,6 +195,6 @@
                 }
                 // I don't need to do this.
+                // image->data.F32[y][x] = 0.0;
                 // The background subtraction code runs after us and will do it there
-                // image->data.F32[y][x] = 0.0;
 	    }
         }
@@ -213,6 +208,4 @@
     }
 
-    psFree(auxMask);
-
     return true;
 }
