Index: /trunk/psastro/configure.ac
===================================================================
--- /trunk/psastro/configure.ac	(revision 18006)
+++ /trunk/psastro/configure.ac	(revision 18007)
@@ -183,6 +183,7 @@
 
 dnl ------------- psLib, psModules ---------------
-PKG_CHECK_MODULES([PSLIB], [pslib >= 1.0.0])
+PKG_CHECK_MODULES([PSLIB],    [pslib >= 1.0.0])
 PKG_CHECK_MODULES([PSMODULE], [psmodules >= 1.0.0])
+PKG_CHECK_MODULES([PPSTATS],  [ppStats >= 1.0.0]) 
 
 dnl Set CFLAGS for build
Index: /trunk/psastro/src/Makefile.am
===================================================================
--- /trunk/psastro/src/Makefile.am	(revision 18006)
+++ /trunk/psastro/src/Makefile.am	(revision 18007)
@@ -5,6 +5,6 @@
 bin_PROGRAMS = psastro psastroModel gpcModel
 
-psastro_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSASTRO_CFLAGS)
-psastro_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS) $(PSASTRO_LIBS)
+psastro_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PPSTATS_CFLAGS) $(PSASTRO_CFLAGS)
+psastro_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS) $(PPSTATS_LIBS) $(PSASTRO_LIBS)
 psastro_LDADD = libpsastro.la
 
@@ -23,4 +23,5 @@
 	psastroDataLoad.c           \
 	psastroDataSave.c           \
+	psastroMetadataStats.c      \
 	psastroCleanup.c
 
@@ -38,6 +39,4 @@
 
 gpcModel_SOURCES = gpcModel.c
-
-## move DataSave to psastro_SOURCES?
 
 libpsastro_la_SOURCES = \
Index: /trunk/psastro/src/psastro.h
===================================================================
--- /trunk/psastro/src/psastro.h	(revision 18006)
+++ /trunk/psastro/src/psastro.h	(revision 18007)
@@ -95,4 +95,5 @@
 bool              psastroAstromGuessSetChip (pmFPA *fpa, pmChip *chip, const pmFPAview *view, double pixelScale, bool bilevelAstrometry);
 bool              psastroAstromGuessSetFPA (pmFPA *fpa, bool *bilevelAstrometry);
+bool              psastroMetadataStats (pmConfig *config);
 
 # endif /* PSASTRO_H */
Index: /trunk/psastro/src/psastroArguments.c
===================================================================
--- /trunk/psastro/src/psastroArguments.c	(revision 18006)
+++ /trunk/psastro/src/psastroArguments.c	(revision 18007)
@@ -55,4 +55,9 @@
     }
 
+    if ((N = psArgumentGet(argc, argv, "-stats"))) {
+        psArgumentRemove(N, &argc, argv);
+        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "STATS", PS_META_REPLACE, "Filename for summary statistics", argv[N]);
+        psArgumentRemove(N, &argc, argv);
+    }
 
     // apply mosastro mode?
Index: /trunk/psastro/src/psastroDataSave.c
===================================================================
--- /trunk/psastro/src/psastroDataSave.c	(revision 18006)
+++ /trunk/psastro/src/psastroDataSave.c	(revision 18007)
@@ -61,4 +61,7 @@
     if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE;
 
+    // Write out summary statistics
+    if (!psastroMetadataStats (config)) ESCAPE;
+
     // activate all files except PSASTRO.OUTPUT
     pmFPAfileActivate (config->files, true, NULL);
Index: /trunk/psastro/src/psastroInternal.h
===================================================================
--- /trunk/psastro/src/psastroInternal.h	(revision 18006)
+++ /trunk/psastro/src/psastroInternal.h	(revision 18007)
@@ -12,4 +12,5 @@
 # include <pslib.h>
 # include <psmodules.h>
+# include <ppStats.h>
 # include "psastro.h"
 
Index: /trunk/psastro/src/psastroMetadataStats.c
===================================================================
--- /trunk/psastro/src/psastroMetadataStats.c	(revision 18007)
+++ /trunk/psastro/src/psastroMetadataStats.c	(revision 18007)
@@ -0,0 +1,68 @@
+# include "psastroInternal.h"
+
+bool psastroMetadataStats (pmConfig *config) {
+
+    bool status;
+
+    char *filename = psMetadataLookupStr (&status, config->arguments, "STATS");
+    if (!filename) return true; // stats not requested
+
+    // Extract statistics from the last output fpa
+    pmFPAfile *output = psMetadataLookupPtr(&status, config->files, "PSASTRO.OUTPUT");
+
+    if (!output) {
+	psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find output file (PSASTRO.OUTPUT).");
+	return false;
+    }
+
+    // create output stats metadata
+    psMetadata *stats = psMetadataAlloc ();
+
+    // extract stats for the complete fpa 
+    pmFPAview *view = pmFPAviewAlloc(0);
+
+    if (!ppStatsMetadata(stats, output->fpa, view, 0, config)) {
+	psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to generate stats for image.");
+	psFree(view);
+	psFree(stats);
+	return false;
+    }
+
+    // if we did not request any specific stats, the structure is empty
+    if (stats && stats->list->n == 0) {
+	psWarning ("stats output specified, but no requested stats entries in headers");
+	psFree(view);
+	psFree(stats);
+	return true;
+    }
+
+    // convert the stats MDC to a string
+    char *statsMDC = psMetadataConfigFormat(stats);
+    if (!statsMDC || strlen(statsMDC) == 0) {
+	psError(PS_ERR_IO, false, "Unable to serialize stats metadata.\n");
+	return false;
+    } 
+
+    // convert to a real UNIX filename
+    psString resolved = pmConfigConvertFilename(filename, config, true); // Resolved filename
+    FILE *statsFile = fopen (resolved, "w");
+    if (!statsFile) {
+	psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
+	psFree(stats);
+	psFree(view);
+	psFree(statsMDC);
+	psFree(resolved);
+	return false;
+    }
+
+    // write the stats MDC to a file
+    // XXX why does this not call psMetadataConfigPrint?
+    fprintf(statsFile, "%s", statsMDC);
+    fclose(statsFile);
+
+    psFree(resolved);
+    psFree(statsMDC);
+    psFree(view);
+    psFree(stats);
+    return true;
+}
