Index: /trunk/psphot/src/psphotImageMedian.c
===================================================================
--- /trunk/psphot/src/psphotImageMedian.c	(revision 7731)
+++ /trunk/psphot/src/psphotImageMedian.c	(revision 7732)
@@ -6,6 +6,13 @@
 {
     bool status;
+    pmFPA *inFPA;
+    pmFPAfile *file;
     psRegion region;
     int MAX_SAMPLE_PIXELS;
+
+    pmReadout *model = NULL;
+    pmReadout *readout = NULL;
+    pmReadout *background = NULL;
+    pmReadout *backSub = NULL;
 
     psTimerStart ("psphot");
@@ -31,5 +38,7 @@
 
     // find the currently selected readout
-    pmReadout  *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT");
+    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.INPUT");
+    inFPA = file->fpa;
+    readout = pmFPAviewThisReadout (view, inFPA);
 
     psImage *image = readout->image;
@@ -53,11 +62,18 @@
 
     // select model pixels (from output background model file, or create internal file)
-    pmReadout *model = pmFPAfileThisReadout (config->files, view, "PSPHOT.BACKMDL");
-    if (model == NULL) {
-        // select model pixels, from output background model file, or create
+    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.BACKMDL");
+    if (file == NULL) {
+	// we are not using PSPHOT.BACKMDL as an I/O file: define an internal version
         model = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKMDL", nx, ny, PS_TYPE_F32);
     } else {
-        // replace the supplied image data with an image of the desired size
-        psImageRecycle (model->image, nx, ny, PS_TYPE_F32);
+	// we are using PSPHOT.BACKMDL as an I/O file: select readout or create
+	model = pmFPAviewThisReadout (view, file->fpa);
+	if (model == NULL) {
+	    // readout does not yet exist: create from input
+	    pmFPAfileCopyStructureView (file->fpa, inFPA, DX, DY, view);
+	    model = pmFPAviewThisReadout (view, file->fpa);
+	    if (nx != model->image->numCols) psAbort ("psphot", "inconsistent size");
+	    if (ny != model->image->numRows) psAbort ("psphot", "inconsistent size");
+	}
     }
     psF32 **modelData = model->image->data.F32;
@@ -107,6 +123,16 @@
 
     // select background pixels, from output background file, or create
-    pmReadout *background = pmFPAfileThisReadout (config->files, view, "PSPHOT.BACKGND");
-    if (background == NULL) {
+    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.BACKGND");
+    if (file) {
+	// we are using PSPHOT.BACKMDL as an I/O file: select readout or create
+	background = pmFPAviewThisReadout (view, file->fpa);
+	if (background == NULL) {
+	    // readout does not yet exist: create from input
+	    pmFPAfileCopyStructureView (file->fpa, inFPA, 1, 1, view);
+	    background = pmFPAviewThisReadout (view, file->fpa);
+	    if (Nx != background->image->numCols) psAbort ("psphot", "inconsistent size");
+	    if (Ny != background->image->numRows) psAbort ("psphot", "inconsistent size");
+	}
+    } else {
         background = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", Nx, Ny, PS_TYPE_F32);
     }
@@ -118,9 +144,21 @@
 
     // back-sub image pixels, from output background file (don't create if not requested)
-    pmReadout *backSub = pmFPAfileThisReadout (config->files, view, "PSPHOT.BACKSUB");
+    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.BACKSUB");
+    if (file) {
+	// we are using PSPHOT.BACKMDL as an I/O file: select readout or create
+	backSub = pmFPAviewThisReadout (view, file->fpa);
+	if (backSub == NULL) {
+	    // readout does not yet exist: create from input
+	    pmFPAfileCopyStructureView (file->fpa, inFPA, 1, 1, view);
+	    backSub = pmFPAviewThisReadout (view, file->fpa);
+	}
+    }
 
-    // psphotSaveImage (NULL, image, "image.fits");
-    // psphotSaveImage (NULL, background->image, "back.fits");
-    // psphotSaveImage (NULL, mask, "mask.fits");
+# ifdef TESTSAVE
+    psphotSaveImage (NULL, image, "image.fits");
+    psphotSaveImage (NULL, background->image, "back.fits");
+    psphotSaveImage (NULL, mask, "mask.fits");
+    psphotSaveImage (NULL, model->image, "backmdl.fits");
+# endif
 
     // subtract the background model (save in backSub, if requested)
@@ -136,7 +174,7 @@
     }
 
-    // if (backSub) psphotSaveImage (NULL, backSub->image, "backsub.fits");
-    // psphotSaveImage (NULL, model->image, "backmdl.fits");
-    // psphotSaveImage (NULL, background->image, "backgnd.fits");
+# ifdef TESTSAVE
+    psphotSaveImage (NULL, image, "backsub.fits");
+# endif
 
     psLogMsg ("psphot", 3, "subtracted background model: %f sec\n", psTimerMark ("psphot"));
