Index: trunk/ppImage/src/Makefile.am
===================================================================
--- trunk/ppImage/src/Makefile.am	(revision 35528)
+++ trunk/ppImage/src/Makefile.am	(revision 35531)
@@ -65,5 +65,6 @@
 	ppImageMemory.c \
         ppImageAddNoise.c \
-        ppImageRandomGaussian.c
+        ppImageRandomGaussian.c \
+	ppImageAuxiliaryMask.c
 
 CLEANFILES = *~
Index: trunk/ppImage/src/ppImage.h
===================================================================
--- trunk/ppImage/src/ppImage.h	(revision 35528)
+++ trunk/ppImage/src/ppImage.h	(revision 35531)
@@ -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);
 
 #endif
Index: trunk/ppImage/src/ppImageAuxiliaryMask.c
===================================================================
--- trunk/ppImage/src/ppImageAuxiliaryMask.c	(revision 35528)
+++ trunk/ppImage/src/ppImageAuxiliaryMask.c	(revision 35531)
@@ -40,4 +40,16 @@
 }
 
+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)
@@ -51,5 +63,6 @@
     }
 
-    bool status;                        // Status of MD lookup
+    bool status;
+    // Our target chip
     pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.CHIP"); // File to correct
     if (!status) {
@@ -58,4 +71,6 @@
     }
 
+    // Find a suitable detRun with type AUXMASK
+
     psTime *time = psMetadataLookupPtr(NULL, input->fpa->concepts, "FPA.TIME");
     if (time->sec == 0 && time->nsec == 0) {
@@ -63,5 +78,4 @@
     }
 
-    // XXX careful about this: is this set correctly in the camera.config files?
     char *cameraName = psMetadataLookupStr(NULL, input->fpa->concepts, "FPA.CAMERA");
     pmDetrendSelectOptions *detrendOptions = pmDetrendSelectOptionsAlloc(cameraName, *time, PM_DETREND_TYPE_AUXMASK);
@@ -80,18 +94,6 @@
     }
     
-#ifdef notdef
-    psMetadata *ppImageRecipe = psMetadataLookupPtr(NULL, config->recipes, RECIPE_NAME);
-    psAssert(ppImageRecipe, "Need PPIMAGE recipe");
-    psMetadata *psphotRecipe = psMetadataLookupPtr(NULL, config->recipes, PSPHOT_RECIPE);
-    psAssert(psphotRecipe, "Need PSPHOT recipe");
-
-    // XXX Should this be options->maskValue or options->maskValue & ~options->satMask?
-    //     The latter will leave saturated pixels high
-    psImageMaskType maskVal = options->maskValue;
-
-    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
-    psMetadataAddImageMask(psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskVal);
-#endif
-
+    // Go find the readout
+    // code to find the readouts was adapted from ppImageSubtractBackground
     // XXX: shouldn't most of these psWarnings be psAsserts?
 
@@ -129,5 +131,5 @@
     psImage *mask = ro->mask;
 
-    // now read the mask file
+    // now read the auxiliary mask file
     psString class_id = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
 
@@ -139,9 +141,13 @@
 
     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) {
-        // readAuxiliaryMask prints enough diagnostic information
+        psError(PS_ERR_UNKNOWN, false, "failed to read auxiliary mask file");
         return false;
     }
@@ -151,7 +157,11 @@
         psImage *videoMask = readAuxiliaryMask(config, options->auxVideoMask);
         if (!videoMask) {
+            psError(PS_ERR_UNKNOWN, false, "failed to read auxiliary video mask file");
             return false;
         }
-        // auxMask *= videoMask
+        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");
Index: trunk/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendReadout.c	(revision 35528)
+++ trunk/ppImage/src/ppImageDetrendReadout.c	(revision 35531)
@@ -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: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 35528)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 35531)
@@ -188,4 +188,9 @@
             ESCAPE("Unable to mosaic chip");
         }
+
+        if (!ppImageAuxiliaryMask(config, view, options)) {
+            ESCAPE("Unable to apply auxiliary mask");
+        }
+
         timeDetrend += psTimerClear(TIMER_DETREND);
 
Index: trunk/ppImage/src/ppImageOptions.c
===================================================================
--- trunk/ppImage/src/ppImageOptions.c	(revision 35528)
+++ trunk/ppImage/src/ppImageOptions.c	(revision 35531)
@@ -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)
