Index: trunk/pswarp/src/pswarp.c
===================================================================
--- trunk/pswarp/src/pswarp.c	(revision 34800)
+++ trunk/pswarp/src/pswarp.c	(revision 35563)
@@ -12,5 +12,4 @@
 
 #include "pswarp.h"
-#include "pswarpFileNames.h"
 
 int main (int argc, char **argv)
@@ -22,12 +21,10 @@
     psphotInit();
 
-    const char *statsName = NULL;       // Filename for statistics
-    psMetadata *stats = NULL;           // Container for statistics
-    FILE *statsFile = NULL;             // File stream for statistics
-
     pmConfig *config = pswarpArguments(argc, argv);
     if (!config) {
-        goto DIE;
+        pswarpCleanup(config, NULL);
     }
+
+    pswarpStatsFile *statsFile = pswarpStatsFileOpen (config);
 
     pswarpVersionPrint();
@@ -35,111 +32,29 @@
     // load identify the data sources
     if (!pswarpParseCamera(config)) {
-        goto DIE;
+        pswarpCleanup(config, statsFile);
     }
 
     if (!pswarpOptions(config)) {
-        goto DIE;
+        pswarpCleanup(config, statsFile);
     }
 
-    // load the skycell layout information
-    if (!pswarpDefine(config)) {
-        goto DIE;
-    }
-    if (psMetadataLookupBool(NULL,config->arguments,"BACKGROUND.MODEL")) {
-      if (!pswarpDefineBackground(config)) {
-	goto DIE;
-      }
+    // load the input & output astrometry, find the output overlaps, generate the output pixels
+    if (!pswarpDefineLayout(config)) {
+        pswarpCleanup(config, statsFile);
     }
 
-    // Open the statistics file
-    bool mdok;                          ///< Status of MD lookup
-    statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); ///< Filename for statistics
-    if (mdok && statsName && strlen(statsName) > 0) {
-        psString resolved = pmConfigConvertFilename(statsName, config, true, true);
-        statsFile = fopen(resolved, "w");
-        if (!statsFile) {
-            psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
-            psFree(resolved);
-            goto DIE;
-        }
-        psFree(resolved);
-        stats = psMetadataAlloc();
-        psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", 0, "No problems", 0);
+    // load input pixels & and warp
+    if (!pswarpLoop(config, statsFile->md)) {
+        pswarpCleanup(config, statsFile);
     }
 
-    // load and warp
-    if (!pswarpLoop(config, stats)) {
-        goto DIE;
-    }
-    if (psMetadataLookupBool(NULL,config->arguments,"BACKGROUND.MODEL")) {
-      if (!pswarpLoopBackground(config, stats)) {
-	fprintf(stderr,"Dying!\n");
-        goto DIE;
-      }
+    // load input pixels & and warp
+    if (!pswarpLoopBackground(config, statsFile->md)) {
+	pswarpCleanup(config, statsFile);
     }
 
+    // output and free 
+    // NOTE: pswarpCleanup calls exit
     psLogMsg("pswarp", PS_LOG_INFO, "complete pswarp run: %f sec\n", psTimerMark("pswarp"));
-
-DIE:
-    {
-        psExit exitValue = pswarpExitCode(PS_EXIT_SUCCESS); // Exit code
-
-        // Ensure everything is written out, at every level
-        pswarpFileActivation(config, detectorFiles, true);
-        pswarpFileActivation(config, skycellFiles, true);
-        pswarpFileActivation(config, photFiles, true);
-        pswarpFileActivation(config, independentFiles, true);
-        if (!pswarpIOChecksAfter(config)) {
-            psError(psErrorCodeLast(), false, "Unable to write files.");
-            exitValue = pswarpExitCode(exitValue);
-            pmFPAfileFreeSetStrict(false);
-        }
-
-        // Write out summary statistics
-        if (stats && statsFile) {
-            psMetadataAddF32(stats, PS_LIST_TAIL, "DT_WARP", 0, "Time for warp completion",
-                             psTimerMark("pswarp"));
-
-            const char *statsMDC = psMetadataConfigFormat(stats);
-            if (!statsMDC) {
-                psError(psErrorCodeLast(), false, "Unable to get statistics file.");
-                exitValue = pswarpExitCode(exitValue);
-            }
-            psFree(stats);
-            if (fprintf(statsFile, "%s", statsMDC) != strlen(statsMDC)) {
-                psError(PSWARP_ERR_IO, true, "Unable to write statistics file.");
-                exitValue = pswarpExitCode(exitValue);
-            }
-            psFree(statsMDC);
-            if (fclose(statsFile) == EOF) {
-                psError(PSWARP_ERR_IO, true, "Unable to close statistics file.");
-                exitValue = pswarpExitCode(exitValue);
-            }
-            pmConfigRunFilenameAddWrite(config, "STATS", statsName);
-        }
-
-        // Dump configuration
-        psString dump_file = psMetadataLookupStr(&mdok, config->arguments, "DUMP_CONFIG");
-        if (dump_file) {
-            if (!pmConfigDump(config, dump_file)) {
-                psError(psErrorCodeLast(), false, "Unable to dump configuration");
-                exitValue = pswarpExitCode(exitValue);
-            }
-        }
-
-        psThreadPoolFinalize();
-        psMemCheckCorruption(stderr, true);
-
-        psFree(config);
-
-        psTimerStop();
-        pmVisualClose();
-        pmModelClassCleanup();
-        pmConceptsDone();
-        pmConfigDone();
-        psLibFinalize();
-
-        exitValue = pswarpExitCode(exitValue);
-        return exitValue;
-    }
+    pswarpCleanup(config, statsFile);
 }
