Index: branches/tap_branches/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- branches/tap_branches/ppImage/src/ppImageDetrendReadout.c	(revision 25900)
+++ branches/tap_branches/ppImage/src/ppImageDetrendReadout.c	(revision 27838)
@@ -14,4 +14,25 @@
     // find the currently selected readout
     pmReadout *input = pmFPAfileThisReadout(config->files, view, "PPIMAGE.INPUT");
+
+    // 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)) {
+	psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, RECIPE_NAME); // Recipe
+	psAssert(recipe, "Should be there!");
+	bool override = psMetadataLookupBool(NULL, recipe, "GAIN.OVERRIDE"); // Override the bad gain?
+	if (override) {
+	  psWarning("CELL.GAIN is not set for readout (%d,%d,%d) --- setting to unity.", view->chip, view->cell, view->readout);
+	  psMetadataItem *item = psMetadataLookup(input->parent->concepts, "CELL.GAIN"); // Gain item
+	  psAssert(item, "Should be there!");
+	  item->data.F32 = 1.0;
+
+	  // for unity gain, there is no modification for the readnoise, note that it has (effectively) been updated
+	  psMetadataRemoveKey(input->parent->concepts, "CELL.READNOISE.UPDATE");
+	} else {
+	  psWarning("CELL.GAIN is NAN for readout (%d,%d,%d), image will be masked.", view->chip, view->cell, view->readout);
+	}
+      }
+    }
 
     // Masking on the basis of pixel value needs to be done before anything else, so the values are pristine.
@@ -89,4 +110,5 @@
             psFree (binning);
         }
+
         pmReadoutGenerateVariance(input, noiseImage, true);
         psFree (noiseImage);
@@ -128,13 +150,13 @@
     }
 
-    // Pattern noise correction
-    if (options->doPattern) {
-        if (!pmPatternRow(input, options->patternOrder, options->patternIter, options->patternRej,
-                          options->patternThresh, options->patternMean, options->patternStdev,
-                          options->maskValue, options->darkMask)) {
-            psFree(detview);
-            return false;
-        }
-    }
+/*     // Pattern noise correction */
+/*     if (options->doPattern) { */
+/*         if (!pmPatternRow(input, options->patternOrder, options->patternIter, options->patternRej, */
+/*                           options->patternThresh, options->patternMean, options->patternStdev, */
+/*                           options->maskValue, options->darkMask)) { */
+/*             psFree(detview); */
+/*             return false; */
+/*         } */
+/*     } */
 
     // Normalization by a single (known) constant
