Index: trunk/pswarp/src/pswarpLoop.c
===================================================================
--- trunk/pswarp/src/pswarpLoop.c	(revision 14874)
+++ trunk/pswarp/src/pswarpLoop.c	(revision 14891)
@@ -3,4 +3,30 @@
 
 #define WCS_NONLIN_TOL 0.001            // Non-linear tolerance for header WCS
+
+// Lists of file rules for the detectors and skycells, and an independent list of everything else
+// Lists must end with a NULL
+static char *detectorFiles[] = { "PSWARP.INPUT", "PSWARP.MASK", "PSWARP.WEIGHT", NULL };
+static char *skycellFiles[] = { "PSWARP.OUTPUT", "PSWARP.OUTPUT.MASK", "PSWARP.OUTPUT.WEIGHT",
+                                "PSPHOT.OUTPUT", "PSPHOT.BACKMDL", "PSPHOT.BACKMDL.STDEV", "PSPHOT.BACKGND",
+                                "PSPHOT.BACKSUB", "PSPHOT.PSF.SAVE", "SOURCE.PLOT.MOMENTS",
+                                "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID", NULL };
+static char *independentFiles[] = { "PSWARP.ASTROM", // Read independently from the input pixels
+                                    "PSWARP.SKYCELL", // Don't care about the skycell once we have its WCS
+                                    "PSPHOT.INPUT", // This is just a pointer to PSWARP.OUTPUT
+                                    "PSWARP.OUTPUT.SOURCES", // Save these independently so we can do the PSF
+                                    NULL };
+
+// Activate a list of files
+static void fileActivation(pmConfig *config, // Configuration
+                           char **files, // Files to turn on/off
+                           bool state   // Activation state
+    )
+{
+    for (int i = 0; files[i] != NULL; i++) {
+        pmFPAfileActivate(config->files, state, files[i]);
+    }
+    return;
+}
+
 
 // Loop over the inputs, warp them to the output skycell and then write out the output.
@@ -57,17 +83,27 @@
     }
 
+    // Turn all skycell files on to generate them, and then turn them off for the loop over the input images
+    // the input, which is in a different format.
+    {
+        fileActivation(config, detectorFiles, false);
+        fileActivation(config, independentFiles, false);
+        fileActivation(config, skycellFiles, true);
+
+        pmFPAview *view = pmFPAviewAlloc(0);
+        pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
+        // They should all be defined at the FPA level, so that should be enough
+        psFree(view);
+
+        fileActivation(config, skycellFiles, false);
+    }
+
+
     // Read the input astrometry
     {
-        pmFPAfileActivate(config->files, false, "PSWARP.INPUT");
-        pmFPAfileActivate(config->files, false, "PSWARP.MASK");
-        pmFPAfileActivate(config->files, false, "PSWARP.WEIGHT");
         pmFPAfileActivate(config->files, true, "PSWARP.ASTROM");
-        pmFPAfileActivate(config->files, false, "PSWARP.OUTPUT");
-        pmFPAfileActivate(config->files, false, "PSWARP.OUTPUT.MASK");
-        pmFPAfileActivate(config->files, false, "PSWARP.OUTPUT.WEIGHT");
-        pmFPAfileActivate(config->files, false, "PSWARP.OUTPUT.SOURCES");
-        pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
+
         pmChip *chip;
         pmFPAview *view = pmFPAviewAlloc(0);
+        pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
         while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
             psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
@@ -83,14 +119,12 @@
         psFree(view);
 
-        pmFPAfileActivate(config->files, true, "PSWARP.INPUT");
-        pmFPAfileActivate(config->files, true, "PSWARP.MASK");
-        pmFPAfileActivate(config->files, true, "PSWARP.WEIGHT");
+        fileActivation(config, detectorFiles, true);
         pmFPAfileActivate(config->files, false, "PSWARP.ASTROM");
-        pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT");
-        pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.MASK");
-        pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.WEIGHT");
-        pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.SOURCES");
-    }
-
+    }
+
+    // Turn on the source output --- we need to get rid of these so that we can measure the PSF
+    pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.SOURCES");
+
+    // Don't care about the skycell anymore --- we've read it, and that's all we need to do.
     pmFPAfileActivate(config->files, false, "PSWARP.SKYCELL");
     view = pmFPAviewAlloc(0);
@@ -198,4 +232,11 @@
     }
 
+    pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
+
+    // Done with the detector side of things
+    fileActivation(config, detectorFiles, false);
+    fileActivation(config, independentFiles, false);
+    fileActivation(config, skycellFiles, true);
+
     // We need a new PSF model for the warped frame
     // It would be good to generate this analytically, but that's going to be tricky.
@@ -214,8 +255,14 @@
                          "Break point for psphot operations", "PSFMODEL");
 
+        // Get rid of the transformed sources so that we can measure the PSF
+        psMetadataRemoveKey(output->analysis, "PSPHOT.SOURCES");
+
+#if 0
         pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT");
         pmFPACopy(photFile->fpa, outFPA);
+#endif
 
         pmFPAview *view = pmFPAviewAlloc(0); // View into skycell
+        view->chip = view->cell = view->readout = 0;
         if (!psphotReadout(config, view)) {
             psError(psErrorCodeLast(), false, "Unable to determine PSF for warped image.\n");
@@ -225,5 +272,6 @@
         psFree(view);
 
-        pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");
+        pmFPAfileActivate(config->files, true, "PSPHOT.INPUT");
+        pmFPAfileActivate(config->files, true, "PSPHOT.PSF.SAVE");
     }
 
@@ -264,6 +312,15 @@
     psFree(headerName);
 
-    pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
-    psFree (view);
+    // Ensure everything is written out, at every level
+    view->chip = view->cell = view->readout = 0;
+    pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
+    view->readout = -1;
+    pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
+    view->cell = -1;
+    pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
+    view->chip = -1;
+    pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
+    psFree(view);
+    // Now done with the skycell side of things
 
     // Write out summary statistics
