Index: trunk/psphot/src/Makefile.am
===================================================================
--- trunk/psphot/src/Makefile.am	(revision 33587)
+++ trunk/psphot/src/Makefile.am	(revision 33690)
@@ -206,5 +206,6 @@
         psphotPetrosianStats.c         \
         psphotPetrosianVisual.c        \
-	psphotEfficiency.c
+	psphotEfficiency.c	       \
+	psphotSetNFrames.c
 
 # re-instate these
Index: trunk/psphot/src/psphot.h
===================================================================
--- trunk/psphot/src/psphot.h	(revision 33587)
+++ trunk/psphot/src/psphot.h	(revision 33690)
@@ -481,3 +481,6 @@
 bool psphotStackObjectsSelectForAnalysis (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objects);
 
+bool psphotSetNFrames (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool psphotSetNFramesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
 #endif
Index: trunk/psphot/src/psphotArguments.c
===================================================================
--- trunk/psphot/src/psphotArguments.c	(revision 33587)
+++ trunk/psphot/src/psphotArguments.c	(revision 33690)
@@ -217,4 +217,5 @@
     pmConfigFileSetsMD (config->arguments, &argc, argv, "PSPHOT.PSF", "-psf",      "-psflist");
     pmConfigFileSetsMD (config->arguments, &argc, argv, "SRC",        "-src",      "-srclist");
+    pmConfigFileSetsMD (config->arguments, &argc, argv, "EXPNUM",     "-expnum",   "-expnumlist");
 
     if (argc == 1) {
Index: trunk/psphot/src/psphotKronIterate.c
===================================================================
--- trunk/psphot/src/psphotKronIterate.c	(revision 33587)
+++ trunk/psphot/src/psphotKronIterate.c	(revision 33690)
@@ -150,5 +150,5 @@
 	    if (!psphotKronIterate_Threaded(job)) {
 		psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
-		psFree(AnalysisRegion);
+		// psFree(AnalysisRegion);
 		return false;
 	    }
@@ -222,4 +222,8 @@
 	    float windowRadius = PS_MAX(RADIUS, maxWindow);
 
+#ifdef notdef
+            fprintf(stderr, "Redefining pixels for source: %d %4d %4d new radius: %f\n", 
+                                        i, source->peak->x, source->peak->y, windowRadius+2);
+#endif
 	    // re-allocate image, weight, mask arrays for each peak with box big enough to fit BIG_RADIUS
 	    pmSourceRedefinePixels (source, readout, source->peak->x, source->peak->y, windowRadius + 2);
Index: trunk/psphot/src/psphotParseCamera.c
===================================================================
--- trunk/psphot/src/psphotParseCamera.c	(revision 33587)
+++ trunk/psphot/src/psphotParseCamera.c	(revision 33690)
@@ -2,4 +2,6 @@
 
 // define the needed / desired I/O files
+
+
 bool psphotParseCamera (pmConfig *config) {
 
@@ -40,4 +42,11 @@
     }
 
+    pmFPAfileBindFromArgs (&status, input, config, "PSPHOT.EXPNUM", "EXPNUM");
+    if (!status) {
+        psError (PS_ERR_UNKNOWN, false, "failed to load find definition");
+        return NULL;
+    }
+
+
     // define the additional input/output files associated with psphot
     if (!psphotDefineFiles (config, input)) {
Index: trunk/psphot/src/psphotSetNFrames.c
===================================================================
--- trunk/psphot/src/psphotSetNFrames.c	(revision 33690)
+++ trunk/psphot/src/psphotSetNFrames.c	(revision 33690)
@@ -0,0 +1,68 @@
+# include "psphotInternal.h"
+
+# define ESCAPE(MESSAGE) {				\
+	psError(PSPHOT_ERR_DATA, false, MESSAGE);	\
+	psFree (view);					\
+	return false;					\
+    }
+
+
+// Set source->nFrames based on the values in the EXPNUM image at the coordinates of each source's peak
+
+bool psphotSetNFrames (pmConfig *config, const pmFPAview *view, const char *filerule)
+{
+    psLogMsg ("psphot", PS_LOG_INFO, "--- psphot Set N Frames ---");
+
+    int num = psphotFileruleCount(config, filerule);
+
+    // loop over the inputs
+    for (int i = 0; i < num; i++) {
+        if (!psphotSetNFramesReadout (config, view, filerule, i)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to set nFrames for %s entry %d", filerule, i);
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool psphotSetNFramesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index)
+{
+    bool status;
+    // find the currently selected readout
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
+    psAssert (file, "missing file?");
+
+    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+    psAssert (readout, "missing readout?");
+
+    psImage *expnum = psMetadataLookupPtr(&status, readout->analysis, "EXPNUM");
+    if (!status || !expnum) { 
+        psLogMsg ("psphot", PS_LOG_INFO, "No EXPNUM image for input %d", index);
+        return true;
+    }
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+    psAssert (detections->allSources, "missing sources?");
+
+    psArray *sources = detections->allSources;
+
+    int col0 = expnum->col0;
+    int row0 = expnum->row0;
+    int numCols = expnum->numCols;
+    int numRows = expnum->numRows;
+    
+    for (int i = 0; i < sources->n; i++) {
+        pmSource *source = sources->data[i];
+        int x = source->peak->x;
+        int y = source->peak->y;
+        if (x >= col0 && x < numCols && y >= row0 && y < numRows) {
+            source->nFrames = expnum->data.PS_TYPE_IMAGE_MASK_DATA[y][x];
+        } else {
+            source->nFrames = 0;
+        }
+    }
+
+    return true;
+}
Index: trunk/psphot/src/psphotStackArguments.c
===================================================================
--- trunk/psphot/src/psphotStackArguments.c	(revision 33587)
+++ trunk/psphot/src/psphotStackArguments.c	(revision 33690)
@@ -53,4 +53,8 @@
         psMetadataAddStr (options, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", argv[N]);
         psArgumentRemove (N, &argc, argv);
+    }
+    if ((N = psArgumentGet (argc, argv, "-ds9regions"))) {
+        psArgumentRemove (N, &argc, argv);
+        pmSubtractionRegions(true);
     }
 
Index: trunk/psphot/src/psphotStackImageLoop.c
===================================================================
--- trunk/psphot/src/psphotStackImageLoop.c	(revision 33587)
+++ trunk/psphot/src/psphotStackImageLoop.c	(revision 33690)
@@ -30,7 +30,16 @@
     }
 
+    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
+    bool useRaw = psMetadataLookupBool (NULL, recipe, "PSPHOT.STACK.USE.RAW");
+    if (useRaw && inputRaw == NULL) {
+        psLogMsg ("psphot", 1, "PSPHOT.STACK.USE.RAW set but no raw input.");
+        useRaw = false;
+    }
+
     pmFPAview *view = pmFPAviewAlloc (0);
 
-    // XXX for now, just load the full set of images up front
+
+    // XXX for now, just load the full set of images up front except for EXPNUM which we defer
+    pmFPAfileActivate (config->files, false, "PSPHOT.STACK.EXPNUM.RAW");
     if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) ESCAPE ("failed input for fpa in psphot.");
 
@@ -86,6 +95,9 @@
 	UpdateHeadersForChip(config, view);
 
-	// save output which is saved at the chip level
-	if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed output for Chip in psphot.");
+        // Defer output until we have performed psphotSetNFrames()
+        pmFPAfileActivate (config->files, false, NULL);
+
+	// Iterate up
+	if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed pmFPAfileIOChecks for Chip in psphot.");
     }
     psMemDump("doneloop");
@@ -93,5 +105,36 @@
     UpdateHeadersForFPA(config, view);
 
-    // save output which is saved at the fpa level
+    // Iterate up output which is saved at the fpa level
+    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed ouput pmFPAfileIOChecks FPA in psphot.");
+
+    // Load the appropriate EXPNUM image
+    pmFPAfileActivate (config->files, true, useRaw ? "PSPHOT.STACK.EXPNUM.RAW" : "PSPHOT.STACK.EXPNUM.CNV");
+
+    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) ESCAPE ("failed input for fpa EXPNUM in psphot.");
+
+    // for psphot, we force data to be read at the chip level
+    while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
+        psLogMsg ("psphot", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+        if (! chip->process || ! chip->file_exists) { continue; }
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) ESCAPE ("failed input for Chip EXPNUM in psphotStack.");
+
+        // there is now only a single chip (multiple readouts?). loop over it and process
+        while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
+            psLogMsg ("psphot", 5, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+	    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) ESCAPE ("failed input for Cell in psphotStack.");
+
+            // process each of the readouts
+            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+                psLogMsg ("psphot", 6, "Readout %d: %x %x\n", view->readout, cell->file_exists, cell->process);
+                if (! readout->data_exists) { continue; }
+
+                if (!psphotSetNFrames (config, view, useRaw ? inputRaw->name : inputCnv->name)) ESCAPE ("failed to setNFrames.");
+            }
+        }
+        // now activate all files to trigger final output
+        pmFPAfileActivate (config->files, true, NULL);
+
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed output for Chip in psphot.");
+    }
     if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed ouput for FPA in psphot.");
 
Index: trunk/psphot/src/psphotStackParseCamera.c
===================================================================
--- trunk/psphot/src/psphotStackParseCamera.c	(revision 33587)
+++ trunk/psphot/src/psphotStackParseCamera.c	(revision 33690)
@@ -59,4 +59,11 @@
 		}
 	    }
+	    psString expnum = psMetadataLookupStr(&status, input, "RAW:EXPNUM"); // Name of expnum image
+	    if (expnum && strlen(expnum) > 0) {
+		if (!defineFile(config, rawInputFile, "PSPHOT.STACK.EXPNUM.RAW", expnum, PM_FPA_FILE_EXPNUM)) {
+		    psError(PS_ERR_UNKNOWN, false, "Unable to define file from expnum %d (%s)", i, expnum);
+		    return false;
+		}
+	    }
 	    nRaw ++;
 	}
@@ -81,4 +88,11 @@
 		if (!defineFile(config, cnvInputFile, "PSPHOT.STACK.VARIANCE.CNV", variance, PM_FPA_FILE_VARIANCE)) {
 		    psError(PS_ERR_UNKNOWN, false, "Unable to define file from variance %d (%s)", i, variance);
+		    return false;
+		}
+	    }
+	    psString expnum = psMetadataLookupStr(&status, input, "CNV:EXPNUM"); // Name of EXPNUM image
+	    if (expnum && strlen(expnum) > 0) {
+		if (!defineFile(config, cnvInputFile, "PSPHOT.STACK.EXPNUM.CNV", expnum, PM_FPA_FILE_EXPNUM)) {
+		    psError(PS_ERR_UNKNOWN, false, "Unable to define file from expnum %d (%s)", i, expnum);
 		    return false;
 		}
