Index: trunk/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendReadout.c	(revision 42890)
+++ trunk/ppImage/src/ppImageDetrendReadout.c	(revision 42964)
@@ -24,8 +24,9 @@
     pmReadout *input = pmFPAfileThisReadout(config->files, view, "PPIMAGE.INPUT");
 
-    // Check that the gain is set (this is used by both pmReadoutGenerateMask and pmReadoutGenerateVariance)
+    // Check that the GAIN is set (this is used by both pmReadoutGenerateMask and pmReadoutGenerateVariance)
     { 
-      float gain = psMetadataLookupF32(NULL, input->parent->concepts, "CELL.GAIN"); // Gain for cell
-      if (!isfinite(gain)) {
+      bool mdok = true; // Status of MD lookup
+      float gain = psMetadataLookupF32(&mdok, input->parent->concepts, "CELL.GAIN"); // Gain for cell
+      if (!mdok || !isfinite(gain)) {
 	psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, RECIPE_NAME); // Recipe
 	psAssert(recipe, "Should be there!");
@@ -44,5 +45,24 @@
       }
     }
-    // psLogMsg ("ppImage", 6, "check gain: %f sec\n", psTimerMark ("detrend.readout"));
+
+    // Check that the SATURATION is set (this is used by pmReadoutSetMask)
+    // NOTE: here we use the value of SATURATION.OVERRIDE as a signal : NAN means do not override
+    { 
+      bool mdok = true; // Status of MD lookup
+      float saturation = psMetadataLookupF32(&mdok, input->parent->concepts, "CELL.SATURATION"); // Saturation for cell
+      if (!mdok || !isfinite(saturation)) {
+	psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, RECIPE_NAME); // Recipe
+	psAssert(recipe, "Should be there!");
+	float override = psMetadataLookupF32(NULL, recipe, "SATURATION.OVERRIDE"); // Override the bad saturation?
+	if (isfinite(override)) {
+	  psMetadataItem *item = psMetadataLookup(input->parent->concepts, "CELL.SATURATION"); // Saturation item
+	  psAssert(item, "Should be there!");
+	  item->data.F32 = override;
+	  psWarning("CELL.SATURATION is not set for readout (%d,%d,%d) --- setting to %f.", view->chip, view->cell, view->readout, override);
+	} else {
+	  psWarning("CELL.SATURATION is NAN for readout (%d,%d,%d), image will be masked.", view->chip, view->cell, view->readout);
+	}
+      }
+    }
 
     // Check to see if we're in a chip that contains video
