Index: /branches/eam_branches/20091201/ppImage/src/ppImageDefineFile.c
===================================================================
--- /branches/eam_branches/20091201/ppImage/src/ppImageDefineFile.c	(revision 26875)
+++ /branches/eam_branches/20091201/ppImage/src/ppImageDefineFile.c	(revision 26876)
@@ -12,6 +12,6 @@
 
     if (!file) {
-        // look for the file on the RUN metadata
-        file = pmFPAfileDefineFromRun(&status, NULL, config, filerule);
+        // look for the file on the argument list
+        file = pmFPAfileDefineFromArgs(&status, config, filerule, argname);
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to load file definition");
@@ -20,6 +20,6 @@
     }
     if (!file) {
-        // look for the file on the argument list
-        file = pmFPAfileDefineFromArgs(&status, config, filerule, argname);
+        // look for the file on the RUN metadata
+        file = pmFPAfileDefineFromRun(&status, NULL, config, filerule);
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to load file definition");
Index: /branches/eam_branches/20091201/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- /branches/eam_branches/20091201/ppImage/src/ppImageDetrendReadout.c	(revision 26875)
+++ /branches/eam_branches/20091201/ppImage/src/ppImageDetrendReadout.c	(revision 26876)
@@ -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);
