Index: branches/simmosaic_branches/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- branches/simmosaic_branches/ppImage/src/ppImageDetrendReadout.c	(revision 24860)
+++ branches/simmosaic_branches/ppImage/src/ppImageDetrendReadout.c	(revision 27839)
@@ -15,4 +15,25 @@
     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.
     if (options->doMaskBuild) {
@@ -26,4 +47,8 @@
         pmMaskBadPixels(input, mask, options->maskValue);
     }
+    if (options->doMaskBurntool) {
+      ppImageBurntoolMask(config,options,view,input);
+    }
+
 
 # if 0
@@ -64,27 +89,28 @@
     if (options->doVarianceBuild) {
         // create the target mask and variance images
-	psImage *noiseImage = NULL;
-	if (options->doNoiseMap) {
-	    // XXX convert the noiseMap image to a binned image
-	    pmReadout *noiseMap = NULL;
-	    noiseMap = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.NOISEMAP");
-	    noiseImage = psImageCopy (NULL, input->image, PS_TYPE_F32);
-	    psImageInit (noiseImage, 0.0);
-
-	    // XXX this works, but is not really quite right: the model shoud include the
-	    // offset information, we are not really getting exactly the right mapping from the
-	    // original file.
-	    psImageBinning *binning = psImageBinningAlloc();
-	    binning->nXruff = noiseMap->image->numCols;
-	    binning->nYruff = noiseMap->image->numRows;
-	    binning->nXfine = input->image->numCols;
-	    binning->nYfine = input->image->numRows;
-	    psImageBinningSetScale(binning, PS_IMAGE_BINNING_LEFT);
-
-	    psImageUnbin (noiseImage, noiseMap->image, binning);
-	    psFree (binning);
-	}
+        psImage *noiseImage = NULL;
+        if (options->doNoiseMap) {
+            // XXX convert the noiseMap image to a binned image
+            pmReadout *noiseMap = NULL;
+            noiseMap = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.NOISEMAP");
+            noiseImage = psImageCopy (NULL, input->image, PS_TYPE_F32);
+            psImageInit (noiseImage, 0.0);
+
+            // XXX this works, but is not really quite right: the model shoud include the
+            // offset information, we are not really getting exactly the right mapping from the
+            // original file.
+            psImageBinning *binning = psImageBinningAlloc();
+            binning->nXruff = noiseMap->image->numCols;
+            binning->nYruff = noiseMap->image->numRows;
+            binning->nXfine = input->image->numCols;
+            binning->nYfine = input->image->numRows;
+            psImageBinningSetScale(binning, PS_IMAGE_BINNING_LEFT);
+
+            psImageUnbin (noiseImage, noiseMap->image, binning);
+            psFree (binning);
+        }
+
         pmReadoutGenerateVariance(input, noiseImage, true);
-	psFree (noiseImage);
+        psFree (noiseImage);
     }
 
@@ -123,4 +149,14 @@
         }
     }
+
+/*     // 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
