Index: trunk/ppImage/src/ppImageReplaceBackground.c
===================================================================
--- trunk/ppImage/src/ppImageReplaceBackground.c	(revision 18217)
+++ trunk/ppImage/src/ppImageReplaceBackground.c	(revision 18255)
@@ -4,4 +4,6 @@
 
 # include "ppImage.h"
+
+enum {MODE_NONE, MODE_MODEL, MODE_VALUE};
 
 // In this function, we perform the psphot analysis routine for the chip-mosaicked images
@@ -19,18 +21,34 @@
     }
 
-    // find the model data, if it exists yet (if not, we build it below)
-    pmFPAfile *modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL");
+    // select the appropriate recipe information
+    psMetadata *ppImageRecipe  = psMetadataLookupPtr (&status, config->recipes, "PPIMAGE");
 
-    // select the appropriate recipe information
-    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+    // select the appropriate recipe information (needed by psphotModelBackground)
+    psMetadata *psphotRecipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+
+    char *replaceSkyModeWord = psMetadataLookupStr (&status, ppImageRecipe, "REPLACE.MODE");
+    float replaceSkyValue = 0.0;
+
+    int replaceSkyMode = MODE_NONE;
+    if (!strcasecmp (replaceSkyModeWord, "MODEL")) {
+	replaceSkyMode = MODE_MODEL;
+    }
+    if (!strcasecmp (replaceSkyModeWord, "VALUE")) {
+	replaceSkyMode = MODE_VALUE;
+	replaceSkyValue = psMetadataLookupF32 (&status, ppImageRecipe, "REPLACE.VALUE");
+    }
+    psAssert (replaceSkyMode != MODE_NONE, "replace sky mode not defined");
 
     // define the mask value to be used in ppImage
     psMaskType maskVal  = options->satMask | options->badMask | options->flatMask | options->blankMask;
 
-    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
-    psMetadataAddU8 (recipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskVal);
+    pmFPAfile *modelFile = NULL;
+    if (replaceSkyMode == MODE_MODEL) {
+	// find the model data, if it exists yet (if not, we build it below)
+	modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL");
 
-    float meanValue = psMetadataLookupF32 (&status, recipe, "SKY_MEAN");
-    bool replaceSkyMean = psMetadataLookupBool (&status, recipe, "REPLACE.SKY_MEAN");
+	// user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
+	psMetadataAddU8 (psphotRecipe, PS_LIST_TAIL, "MASK.PSPHOT", PS_META_REPLACE, "user-defined mask", maskVal);
+    }
 
     // iterate over the cells and readout for this chip
@@ -45,35 +63,37 @@
 
 	    // replace masked pixels with values from model (unbinning not needed)
-
-	    // we are using this pmFPAfile as an I/O file: select readout or create
 	    pmReadout *modelReadout = NULL;
-	    if (modelFile) {
-		modelReadout = pmFPAviewThisReadout (view, modelFile->fpa);
+	    psImageBinning *binning = NULL;
+	    psImage *model = NULL;
+	    if (replaceSkyMode == MODE_MODEL) {
+		// we are using this pmFPAfile as an I/O file: select readout or create
+		if (modelFile) {
+		    modelReadout = pmFPAviewThisReadout (view, modelFile->fpa);
+		}
+		if (!modelReadout) {
+		    psphotModelBackground (config, view, "PPIMAGE.CHIP");
+		    modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL");
+		    assert (modelFile);
+		    if (modelFile->mode == PM_FPA_MODE_INTERNAL) {
+			modelReadout = modelFile->readout;
+		    } else {
+			modelReadout = pmFPAviewThisReadout (view, modelFile->fpa);
+		    }
+		    assert (modelReadout);
+		}
+		binning = psMetadataLookupPtr(&status, psphotRecipe, "PSPHOT.BACKGROUND.BINNING");
+		model = modelReadout->image;
 	    }
-	    if (!modelReadout) {
-		psphotModelBackground (config, view, "PPIMAGE.CHIP");
-		modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL");
-		assert (modelFile);
-		if (modelFile->mode == PM_FPA_MODE_INTERNAL) {
-		  modelReadout = modelFile->readout;
-		} else {
-		  modelReadout = pmFPAviewThisReadout (view, modelFile->fpa);
-		}
-		assert (modelReadout);
-	    }
-
-	    psImageBinning *binning = psMetadataLookupPtr(&status, recipe, "PSPHOT.BACKGROUND.BINNING");
 
 	    psImage *mask = readout->mask;
 	    psImage *image = readout->image;
-	    psImage *model = modelReadout->image;
 
 	    for (int iy = 0; iy < image->numRows; iy++) {
 		for (int ix = 0; ix < image->numCols; ix++) {
 		    if (!(mask->data.U8[iy][ix] && maskVal)) continue;
-		    if (replaceSkyMean) {
-			image->data.F32[iy][ix] = meanValue;
+		    if (replaceSkyMode == MODE_MODEL) {
+			image->data.F32[iy][ix] = psImageUnbinPixel_V2(ix, iy, model, binning);
 		    } else {
-			image->data.F32[iy][ix] = psImageUnbinPixel_V2(ix, iy, model, binning);
+			image->data.F32[iy][ix] = replaceSkyValue;
 		    }
 		}
@@ -81,6 +101,10 @@
 	}
     }
-    pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL");
-    pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL.STDEV");
+
+    if (replaceSkyMode == MODE_MODEL) {
+	pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL");
+	pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL.STDEV");
+    }
+
     return true;
 }
