Index: /branches/eam_branches/ipp-20130419/ppImage/src/Makefile.am
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/Makefile.am	(revision 35551)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/Makefile.am	(revision 35552)
@@ -65,5 +65,6 @@
 	ppImageMemory.c \
         ppImageAddNoise.c \
-        ppImageRandomGaussian.c
+        ppImageRandomGaussian.c \
+	ppImageAuxiliaryMask.c
 
 CLEANFILES = *~
Index: /branches/eam_branches/ipp-20130419/ppImage/src/ppImage.h
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/ppImage.h	(revision 35551)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/ppImage.h	(revision 35552)
@@ -30,4 +30,5 @@
     bool doMaskLow;                     // mask low pixels
     bool doMask;                        // Mask bad pixels
+    bool doAuxMask;                     // apply auxillary mask
     bool doNonLin;                      // Non-linearity correction
     bool doOverscan;                    // Overscan subtraction
@@ -126,4 +127,6 @@
   psU16 maskstat_magic;
   psU16 maskstat_advisory;
+
+  psString auxVideoMask;                // auxillary video mask file
   
 } ppImageOptions;
@@ -312,4 +315,5 @@
 void ppImageRandomGaussianFree(void);
 
+bool ppImageAuxiliaryMask(pmConfig *config, const pmFPAview *view, const ppImageOptions *options, psMetadata *stats);
 
 #endif
Index: /branches/eam_branches/ipp-20130419/ppImage/src/ppImageAuxiliaryMask.c
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/ppImageAuxiliaryMask.c	(revision 35552)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/ppImageAuxiliaryMask.c	(revision 35552)
@@ -0,0 +1,218 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "ppImage.h"
+
+psImage *readAuxiliaryMask(pmConfig *config, psString fileName)
+{
+    psString realName = pmConfigConvertFilename(fileName, config, false, false);
+    if (!realName) {
+        psError (psErrorCodeLast(), false, "unable to resolve %s", fileName);
+        return NULL;
+    }
+
+    psFits *fits = psFitsOpen(realName, "r");
+    if (!fits) {
+        psError (psErrorCodeLast(), false, "psFitsOpen failed for %s", realName);
+        psFree(realName);
+        return NULL;
+    }
+    psMetadata *header = psFitsReadHeader(NULL, fits);
+    if (!header) {
+        psFree(fits);
+        psError (psErrorCodeLast(), false, "psFitsReadHeader failed for %s", realName);
+        psFree(realName);
+        return NULL;
+    }
+    psRegion region = {0, 0, 0, 0};
+    psImage *image = psFitsReadImage(fits, region, 0);
+    psFree(fits);
+    psFree(header);
+    if (!image) {
+        psError (psErrorCodeLast(), false, "psFitsReadImage failed for %s", realName);
+        psFree(realName);
+        return NULL;
+    }
+    psFree(realName);
+
+    return image;
+}
+
+bool recordFileInHeader(pmChip *chip, psString tag, psString desc, psString filename)
+{
+    pmHDU *hdu = pmHDUGetHighest(chip->parent, chip, NULL);
+
+    // strip off the directories and nebulous bits
+    char *base = filename;
+    for (char *new = base; (new = strpbrk(base, "/:")); base = new + 1);
+
+    psMetadataAddStr(hdu->header, PS_LIST_TAIL, tag, PS_META_DUPLICATE_OK, desc, base);
+    return true;
+}
+
+// In this function, we augment the mask with the more conservative auxiliary mask
+bool ppImageAuxiliaryMask(pmConfig *config, const pmFPAview *view, const ppImageOptions *options, psMetadata *stats)
+{
+    psAssert(config, "Need configuration");
+    psAssert(view, "Need view to chip");
+    psAssert(options, "Need options");
+
+    if (!options->doAuxMask) {
+        psLogMsg ("ppImage", PS_LOG_INFO, "Auxiliary mask not enabled.");
+        return true;
+    }
+
+    bool status;
+    // Our target chip
+    pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.CHIP"); // File to correct
+    if (!status) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "PPIMAGE.CHIP file is not defined");
+        return false;
+    }
+
+    // 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;
+    }
+    
+    // Go find the readout
+    // code to find the readouts was adapted from ppImageSubtractBackground
+    // XXX: shouldn't most of these psWarnings be psAsserts?
+
+    // Since we are working on a chip-mosaicked image, there should only be a single cell and readout
+    pmChip *chip = pmFPAviewThisChip(view, input->fpa); // Chip of interest
+    if (!chip) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find chip");
+        return false;
+    }
+    if (chip->cells->n == 0) {
+        psWarning("Chip has no cells");
+        return true;
+    }
+    if (chip->cells->n > 1) {
+        psWarning("Chip has %ld cells; only the first will be processed", chip->cells->n);
+    }
+    pmCell *cell = chip->cells->data[0]; // Cell of interest
+    if (!cell || !cell->process || !cell->file_exists) {
+        // Nothing to process
+        return true;
+    }
+    if (cell->readouts->n == 0) {
+        psWarning("Cell has no readouts");
+        return true;
+    }
+    if (cell->readouts->n > 1) {
+        psWarning("Cell has %ld readouts; only the first will be processed", cell->readouts->n);
+    }
+    pmReadout *ro = cell->readouts->data[0]; // Readout of interest
+    if (!ro || !ro->data_exists) {
+        // Nothing to process
+        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 (options->hasVideo && options->auxVideoMask && strcmp(options->auxVideoMask, "NULL")) {
+        psImage *videoMask = readAuxiliaryMask(config, options->auxVideoMask);
+        if (!videoMask) {
+            psError(PS_ERR_UNKNOWN, false, "failed to read auxiliary video mask file");
+            return false;
+        }
+        psLogMsg ("ppImage", PS_LOG_INFO, "Auxiliary video mask file: %s", options->auxVideoMask);
+        pmConfigRunFilenameAddRead(config, "PPIMAGE.AUXVIDEOMASK", options->auxVideoMask);
+        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");
+            return false;
+        }
+        psFree(videoMask);
+    }
+
+    if ((mask->numRows != auxMask->numRows) || (mask->numCols != auxMask->numCols) ||
+        (mask->row0 != auxMask->row0) || (mask->col0 != auxMask->col0)) {
+        psError(PS_ERR_IO, false, "structure of auxiliary mask does not match this chip");
+        return false;
+    }
+
+    psImageMaskType maskDetector = pmConfigMaskGet("DETECTOR", config);
+
+    psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, "PPIMAGE");
+    psAssert(recipe, "Need recipe!");
+    psImageMaskType staticMaskVal = psMetadataLookupImageMask(&status, recipe, "MASKSTAT.STATIC");
+    psAssert(staticMaskVal, "Need staticMaskVal!");
+
+    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 ((mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & staticMaskVal) == 0) {
+                    // pixel was not previously included in the static mask so mask it
+                    ++numMasked;
+                    mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskDetector;
+                }
+                // I don't need to do this.
+                // The background subtraction code runs after us and will do it there
+                // image->data.F32[y][x] = 0.0;
+	    }
+        }
+    }
+
+    float maskedFrac = (float) numMasked / (numRows * numCols);
+    psLogMsg ("ppImage", PS_LOG_INFO, "Auxiliary masks masked %ld aditional pixels. Masked fraction: %f", numMasked, maskedFrac);
+    if (stats) {
+        psMetadataAddF32(stats,PS_LIST_TAIL, "MASKFRAC_MAGIC", PS_META_REPLACE,
+                   "Fraction of pixels masked by auxiliary masks", maskedFrac);
+    }
+
+    psFree(auxMask);
+
+    return true;
+}
Index: /branches/eam_branches/ipp-20130419/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/ppImageDetrendReadout.c	(revision 35551)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/ppImageDetrendReadout.c	(revision 35552)
@@ -50,5 +50,5 @@
 	char *Vptr = strchr(psMetadataLookupStr(NULL,input->parent->parent->hdu->header,"CELLMODE"),'V');
 	if (Vptr) {
-	  hasVideo = true;
+	  hasVideo = options->hasVideo = true;
 	  psLogMsg ("ppImage.detrend", PS_LOG_INFO, "VIDEO: %d %d %d\n",(int) options->hasVideo,(int) options->useVideoDark, (int) options->useVideoMask);
 	}
Index: /branches/eam_branches/ipp-20130419/ppImage/src/ppImageLoop.c
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/ppImageLoop.c	(revision 35551)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/ppImageLoop.c	(revision 35552)
@@ -188,4 +188,9 @@
             ESCAPE("Unable to mosaic chip");
         }
+
+        if (!ppImageAuxiliaryMask(config, view, options, stats)) {
+            ESCAPE("Unable to apply auxiliary mask");
+        }
+
         timeDetrend += psTimerClear(TIMER_DETREND);
 
Index: /branches/eam_branches/ipp-20130419/ppImage/src/ppImageOptions.c
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/ppImageOptions.c	(revision 35551)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/ppImageOptions.c	(revision 35552)
@@ -10,4 +10,5 @@
     // psFree(options->nonLinearData);
     // psFree(options->nonLinearSource);
+    psFree(options->auxVideoMask);
 }
 
@@ -24,4 +25,5 @@
     options->doVarianceBuild = false;   // Build internal variance
     options->doMask          = false;   // Mask bad pixels
+    options->doAuxMask       = false;   // apply auxillary mask
     options->doNonLin        = false;   // Non-linearity correction
     options->doOverscan      = false;   // Overscan subtraction
@@ -116,4 +118,6 @@
     options->normClass       = NULL;    // per-class normalizations refer to this class
 
+    options->auxVideoMask    = NULL;    // auxillary video mask file name
+
     return options;
 }
@@ -228,4 +232,14 @@
     options->doMaskBurntool  = psMetadataLookupBool(NULL, recipe, "MASK.BURNTOOL");
     options->doVarianceBuild = psMetadataLookupBool(NULL, recipe, "VARIANCE.BUILD");
+    options->doAuxMask       = psMetadataLookupBool(NULL, recipe, "MASK.AUXMASK");
+    if (options->doAuxMask) {
+        // if we are applying an auxiliary mask we can optionally apply another
+        // mask to video cells only. 
+        psString auxVideoMask = psMetadataLookupStr(NULL, recipe, "AUX.VIDEO.MASK");
+        // save the value if defined and not the value "NULL"
+        if (auxVideoMask && strcmp(auxVideoMask, "NULL")) {
+            options->auxVideoMask = psStringCopy(auxVideoMask);
+        }
+    }
 
     // Mask recipe options (note that mask bit values are set in ppImageSetMaskBits.c)
Index: /branches/eam_branches/ipp-20130419/ppImage/src/ppImagePhotom.c
===================================================================
--- /branches/eam_branches/ipp-20130419/ppImage/src/ppImagePhotom.c	(revision 35551)
+++ /branches/eam_branches/ipp-20130419/ppImage/src/ppImagePhotom.c	(revision 35552)
@@ -46,5 +46,5 @@
                 }
                 psErrorClear();
-                psphotFilesActivate(config, false);
+		//                psphotFilesActivate(config, false);
             }
 
