Index: trunk/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendReadout.c	(revision 26629)
+++ trunk/ppImage/src/ppImageDetrendReadout.c	(revision 26644)
@@ -14,4 +14,22 @@
     // 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;
+	} 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.
@@ -90,23 +108,4 @@
         }
 
-        // Check that the gain is set
-        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;
-            } else {
-                psError(PS_ERR_BAD_PARAMETER_VALUE, false, "CELL.GAIN is not set for readout (%d,%d,%d)",
-                        view->chip, view->cell, view->readout);
-                return false;
-            }
-        }
-
         pmReadoutGenerateVariance(input, noiseImage, true);
         psFree (noiseImage);
