Index: trunk/psphot/src/psphotImageLoop.c
===================================================================
--- trunk/psphot/src/psphotImageLoop.c	(revision 32348)
+++ trunk/psphot/src/psphotImageLoop.c	(revision 33691)
@@ -75,8 +75,11 @@
         pmFPAfileActivate (config->files, false, "PSPHOT.MASK");
         pmFPAfileActivate (config->files, false, "PSPHOT.VARIANCE");
+        // do not load the exposure number file yet file until we are done with the psphot analysis
+        pmFPAfileActivate (config->files, false, "PSPHOT.EXPNUM");
         if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) ESCAPE ("failed input for Chip in psphot.");
 
         // re-activate files so they will be closed and freed below
-        pmFPAfileActivate (config->files, true, NULL);
+//        XXX: Defer this
+//        pmFPAfileActivate (config->files, true, NULL);
 
         // there is now only a single chip (multiple readouts?). loop over it and process
@@ -150,8 +153,26 @@
             }
         }
-        // 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 and closing of files until we've (possibly) done the NFrames analysis below
+        pmFPAfileActivate (config->files, false, NULL);
+
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed pmFPAfileIOChecks for Chip in psphot.");
     }
-    // save output which is saved at the fpa level
+    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed pmFPAfileIOChecks for FPA in psphot.");
+
+    // activate the EXPNUM image, cause it to be read in (if it exists), use it to set N Frames for the sources
+    // and finally iterate up to trigger writing of the output files
+    pmFPAfileActivate (config->files, true, "PSPHOT.EXPNUM");
+    while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) ESCAPE ("failed attempting to load EXPNUM input for Chip in psphot.");
+        while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
+            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+                if (! readout->data_exists) { continue; }
+                if (!psphotSetNFrames (config, view, "PSPHOT.INPUT")) ESCAPE ("failed to setNFrames.");
+            }
+        }
+        // now activate all files to trigger the output
+        pmFPAfileActivate (config->files, true, NULL);
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed to iterate up Chip in psphot.");
+    }
     if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed ouput for FPA in psphot.");
 
