Index: /trunk/pswarp/configure.ac
===================================================================
--- /trunk/pswarp/configure.ac	(revision 13103)
+++ /trunk/pswarp/configure.ac	(revision 13104)
@@ -25,164 +25,8 @@
 fi
 
-dnl ------------------ kapa,libkapa options -------------------------
-dnl -- libkapa implies the requirement for libpng, libjpeg as well --
 
-dnl save LIBS/CFLAGS/LDFLAGS
-TMP_LIBS=${LIBS}
-TMP_CFLAGS=${CFLAGS}
-TMP_LDFLAGS=${LDFLAGS}
-TMP_CPPFLAGS=${CPPFLAGS}
-
-dnl test for command-line options: use ohana-config if not supplied
-KAPA_CFLAGS_CONFIG="true"
-KAPA_LIBS_CONFIG="true"
-AC_ARG_WITH(kapa,
-[AS_HELP_STRING(--with-kapa=DIR,Specify location of libkapa)],
-[KAPA_CFLAGS="-I$withval/include" KAPA_LIBS="-L$withval/lib" 
- KAPA_CFLAGS_CONFIG="false"       KAPA_LIBS_CONFIG="false"])
-AC_ARG_WITH(kapa-include,
-[AS_HELP_STRING(--with-kapa-include=DIR,Specify libkapa include directory.)],
-[KAPA_CFLAGS="-I$withval" KAPA_CFLAGS_CONFIG="false"])
-AC_ARG_WITH(kapa-lib,
-[AS_HELP_STRING(--with-kapa-lib=DIR,Specify libkapa library directory.)],
-[KAPA_LIBS="-L$withval" KAPA_LIBS_CONFIG="false"])
-
-echo "KAPA_CFLAGS_CONFIG: $KAPA_CFLAGS_CONFIG"
-echo "KAPA_LIBS_CONFIG: $KAPA_LIBS_CONFIG"
-echo "KAPA_CFLAGS: $KAPA_CFLAGS"
-echo "KAPA_LIBS: $KAPA_LIBS"
-
-dnl HAVE_KAPA is set to false if any of the tests fail
-HAVE_KAPA="true"
-AC_MSG_NOTICE([checking for libkapa])
-if test "$KAPA_CFLAGS_CONFIG" = "true" -o "$KAPA_LIBS_CONFIG" = "true"; then
-  AC_MSG_NOTICE([kapa info supplied by ohana-config])
-  KAPA_CONFIG=`which ohana-config`
-  AC_CHECK_FILE($KAPA_CONFIG,[],
-    [HAVE_KAPA="false"; AC_MSG_WARN([libkapa is not found: output plots disabled.  Obtain libkapa at http://kiawe.ifa.hawaii.edu/Elixir/Ohana or use --with-kapa to specify location])])
-  
-  echo "HAVE_KAPA: $HAVE_KAPA"
-  echo "KAPA_CFLAGS_CONFIG: $KAPA_CFLAGS_CONFIG"
-
-  if test "$HAVE_KAPA" = "true" -a "$KAPA_CFLAGS_CONFIG" = "true" ; then
-   AC_MSG_NOTICE([libkapa cflags info supplied by ohana-config])
-   AC_MSG_CHECKING([libkapa cflags])
-   KAPA_CFLAGS="`${KAPA_CONFIG} --cflags`"
-   AC_MSG_RESULT([${KAPA_CFLAGS}])
-  fi
-
-  if test "$HAVE_KAPA" = "true" -a "$KAPA_LIBS_CONFIG" = "true" ; then
-   AC_MSG_NOTICE([libkapa ldflags info supplied by ohana-config])
-   AC_MSG_CHECKING([libkapa ldflags])
-   KAPA_LIBS="`${KAPA_CONFIG} --libs` -lX11"
-   AC_MSG_RESULT([${KAPA_LIBS}])
-  fi
-fi
-
-if test "$HAVE_KAPA" = "true" ; then
- AC_MSG_NOTICE([libkapa supplied])
- PSWARP_CFLAGS="${PSWARP_CFLAGS} ${KAPA_CFLAGS}"
- PSWARP_LIBS="${PSWARP_LIBS} ${KAPA_LIBS}"
-else
- AC_MSG_NOTICE([libkapa ignored])
-fi
-
-dnl restore the CFLAGS/LDFLAGS
-LIBS=${TMP_LIBS}
-CFLAGS=${TMP_CFLAGS}
-LDFLAGS=${TMP_LDFLAGS}
-CPPFLAGS=${TMP_CPPFLAGS}
-
-dnl ------------------ libjpeg options ---------------------
-
-dnl save LIBS/CFLAGS/LDFLAGS
-TMP_LIBS=${LIBS}
-TMP_CFLAGS=${CFLAGS}
-TMP_LDFLAGS=${LDFLAGS}
-TMP_CPPFLAGS=${CPPFLAGS}
-
-AC_ARG_WITH(jpeg,
-[AS_HELP_STRING(--with-jpeg=DIR,Specify location of libjpeg.)],
-[JPEG_CFLAGS="-I$withval/include"
- JPEG_LDFLAGS="-L$withval/lib"])
-AC_ARG_WITH(jpeg-include,
-[AS_HELP_STRING(--with-jpeg-include=DIR,Specify libjpeg include directory.)],
-[JPEG_CFLAGS="-I$withval"])
-AC_ARG_WITH(jpeg-lib,
-[AS_HELP_STRING(--with-jpeg-lib=DIR,Specify libjpeg library directory.)],
-[JPEG_LDFLAGS="-L$withval"])
-
-CFLAGS="${CFLAGS} ${JPEG_CFLAGS}"
-CPPFLAGS=${CFLAGS}
-LDFLAGS="${LDFLAGS} ${JPEG_LDFLAGS}"
-
-AC_CHECK_HEADERS([jpeglib.h],
-  [PSWARP_CFLAGS="$PSWARP_CFLAGS $JPEG_CFLAGS" AC_SUBST(JPEG_CFLAGS)],
-  [HAVE_KAPA=false; AC_MSG_WARN([libjpeg headers not found: output plots disabled.  Obtain libjpeg from http://www.ijg.org/ or use --with-jpeg to specify location.])]
-)
-
-AC_CHECK_LIB(jpeg,jpeg_CreateCompress,
-  [PSWARP_LIBS="$PSWARP_LIBS $JPEG_LDFLAGS -ljpeg"],
-  [HAVE_KAPA=false; AC_MSG_WARN([libjpeg library not found: output plots disabled.  Obtain libjpeg from http://www.ijg.org/ or use --with-jpeg to specify location.])]
-)
-
-dnl restore the CFLAGS/LDFLAGS
-LIBS=${TMP_LIBS}
-CFLAGS=${TMP_CFLAGS}
-LDFLAGS=${TMP_LDFLAGS}
-CPPFLAGS=${TMP_CPPFLAGS}
-
-dnl ------------------ libpng options ---------------------
-
-dnl save LIBS/CFLAGS/LDFLAGS
-TMP_LIBS=${LIBS}
-TMP_CFLAGS=${CFLAGS}
-TMP_LDFLAGS=${LDFLAGS}
-TMP_CPPFLAGS=${CPPFLAGS}
-
-AC_ARG_WITH(png,
-[AS_HELP_STRING(--with-png=DIR,Specify location of libpng.)],
-[PNG_CFLAGS="-I$withval/include"
- PNG_LDFLAGS="-L$withval/lib"])
-AC_ARG_WITH(png-include,
-[AS_HELP_STRING(--with-png-include=DIR,Specify libpng include directory.)],
-[PNG_CFLAGS="-I$withval"])
-AC_ARG_WITH(png-lib,
-[AS_HELP_STRING(--with-png-lib=DIR,Specify libpng library directory.)],
-[PNG_LDFLAGS="-L$withval"])
-
-CFLAGS="${CFLAGS} ${PNG_CFLAGS}"
-CPPFLAGS=${CFLAGS}
-LDFLAGS="${LDFLAGS} ${PNG_LDFLAGS}"
-
-AC_CHECK_HEADERS([png.h],
-  [PSWARP_CFLAGS="$PSWARP_CFLAGS $PNG_CFLAGS" AC_SUBST(PNG_CFLAGS)],
-  [HAVE_KAPA=false; AC_MSG_WARN([libpng headers not found: output plots disabled.  Obtain libpng from http://www.ijg.org/ or use --with-png to specify location.])]
-)
-
-AC_CHECK_LIB(png,png_init_io,
-  [PSWARP_LIBS="$PSWARP_LIBS $PNG_LDFLAGS -lpng"],
-  [HAVE_KAPA=false; AC_MSG_WARN([libpng library not found: output plots disabled.  Obtain libpng from http://www.ijg.org/ or use --with-png to specify location.])]
-)
-
-dnl restore the CFLAGS/LDFLAGS
-LIBS=${TMP_LIBS}
-CFLAGS=${TMP_CFLAGS}
-LDFLAGS=${TMP_LDFLAGS}
-CPPFLAGS=${TMP_CPPFLAGS}
-
-dnl ------------------ use kapa or not? ---------------------
-
-if test "$HAVE_KAPA" == "true" ; then
-  AC_MSG_RESULT([including plotting functions])
-  AC_DEFINE([HAVE_KAPA],[1],[enable use of libkapa])
-else
-  AC_MSG_RESULT([skipping plotting functions])
-  AC_DEFINE([HAVE_KAPA],[0],[disable use of libkapa])
-fi
-
-dnl ------------- psLib, psModules ---------------
-PKG_CHECK_MODULES(PSLIB, pslib >= 1.0.0)
-PKG_CHECK_MODULES(PSMODULE, psmodules >= 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/pswarp/src/Makefile.am
===================================================================
--- /trunk/pswarp/src/Makefile.am	(revision 13103)
+++ /trunk/pswarp/src/Makefile.am	(revision 13104)
@@ -1,30 +1,23 @@
-
-lib_LTLIBRARIES = libpswarp.la
-libpswarp_la_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSWARP_CFLAGS)
-
 bin_PROGRAMS = pswarp
-pswarp_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSWARP_CFLAGS)
-pswarp_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS) $(PSWARP_LIBS)
-pswarp_LDADD = libpswarp.la
+pswarp_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PPSTATS_CFLAGS) $(PSWARP_CFLAGS)
+pswarp_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS) $(PPSTATS_LIBS) $(PSWARP_LIBS)
 
 pswarp_SOURCES = \
-	pswarp.c		
+	pswarp.c			\
+	pswarpArguments.c		\
+	pswarpCleanup.c			\
+	pswarpDataLoad.c		\
+	pswarpDataSave.c		\
+	pswarpDefine.c			\
+	pswarpDefineSkycell.c		\
+	pswarpErrorCodes.c		\
+	pswarpMapGrid.c			\
+	pswarpMatchRange.c		\
+	pswarpParseCamera.c		\
+	pswarpTransformReadout.c	\
+	pswarpTransformReadout_Opt.c	\
+	pswarpVersion.c            
 
-libpswarp_la_SOURCES = \
-pswarpArguments.c	   \
-pswarpCleanup.c            \
-pswarpDataLoad.c           \
-pswarpDataSave.c           \
-pswarpDefine.c             \
-pswarpDefineSkycell.c      \
-pswarpErrorCodes.c         \
-pswarpMapGrid.c            \
-pswarpMatchRange.c         \
-pswarpParseCamera.c   	   \
-pswarpTransformReadout.c   \
-pswarpTransformReadout_Opt.c \
-pswarpVersion.c            
-
-include_HEADERS = \
+noinst_HEADERS = \
 	pswarp.h \
 	pswarpErrorCodes.h
Index: /trunk/pswarp/src/pswarpArguments.c
===================================================================
--- /trunk/pswarp/src/pswarpArguments.c	(revision 13103)
+++ /trunk/pswarp/src/pswarpArguments.c	(revision 13104)
@@ -26,8 +26,17 @@
 
     // chip selection is used to limit chips to be processed
-    if ((N = psArgumentGet (argc, argv, "-chip"))) {
-        psArgumentRemove (N, &argc, argv);
-        psMetadataAddStr (config->arguments, PS_LIST_TAIL, "CHIP_SELECTIONS", PS_DATA_STRING, "", argv[N]);
-        psArgumentRemove (N, &argc, argv);
+    if ((N = psArgumentGet(argc, argv, "-chip"))) {
+        psArgumentRemove(N, &argc, argv);
+        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "CHIP_SELECTIONS", PS_DATA_STRING,
+                         "Only process these chips", argv[N]);
+        psArgumentRemove(N, &argc, argv);
+    }
+
+    // Statistics file
+    if ((N = psArgumentGet(argc, argv, "-stat"))) {
+        psArgumentRemove(N, &argc, argv);
+        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "STATS", PS_DATA_STRING,
+                         "Filename for statistics of output image", argv[N]);
+        psArgumentRemove(N, &argc, argv);
     }
 
Index: /trunk/pswarp/src/pswarpDataSave.c
===================================================================
--- /trunk/pswarp/src/pswarpDataSave.c	(revision 13103)
+++ /trunk/pswarp/src/pswarpDataSave.c	(revision 13104)
@@ -1,3 +1,4 @@
-# include "pswarp.h"
+#include <ppStats.h>
+#include "pswarp.h"
 
 // this loop loads the data from the input files and selects the
@@ -7,5 +8,5 @@
 
 // all of the different astrometry analysis modes use the same data load loop
-bool pswarpDataSave (pmConfig *config) {
+bool pswarpDataSave(pmConfig *config) {
 
     pmChip *chip;
@@ -18,4 +19,17 @@
         psError(PSWARP_ERR_CONFIG, true, "Can't find input data!\n");
         return false;
+    }
+
+    bool mdok;                          // Status of MD lookup
+    const char *statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); // Filename for statistics
+    psMetadata *stats = NULL;           // Container for statistics
+    FILE *statsFile = NULL;             // File stream for statistics
+    if (mdok && statsName && strlen(statsName) > 0) {
+        statsFile = fopen(statsName, "w");
+        if (!statsFile) {
+            psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", statsName);
+            return false;
+        }
+        stats = psMetadataAlloc();
     }
 
@@ -40,10 +54,40 @@
             pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
 
-            // process each of the readouts
-            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+            // Perform statistics on the cell
+            if (stats) {
+                bool mdok;              // Status of MD lookup
+                pmFPAfile *output = psMetadataLookupPtr(&mdok, config->files, "PSWARP.OUTPUT");
+                if (!mdok || !output) {
+                    psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find file PSWARP.OUTPUT.\n");
+                    psFree(view);
+                    psFree(stats);
+                    return false;
+                }
+                ppStats(stats, output->fpa, view, config);
+            }
+
+            pmHDU *hdu = pmHDUFromCell(cell); // HDU that owns the cell
+
+            // Process readouts
+            while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
                 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
-                if (! readout->data_exists) { continue; }
+                if (!readout->data_exists) {
+                    continue;
+                }
 
                 // pswarpConvertReadout (readout, config);
+
+                // Add MD5 information for readout
+                const char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
+                const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME");
+                psString headerName = NULL; // Header name for MD5
+                psStringAppend(&headerName, "MD5_%s_%s_%d", chipName, cellName, view->readout);
+                psVector *md5 = psImageMD5(readout->image); // md5 hash
+                psString md5string = psMD5toString(md5); // String
+                psFree(md5);
+                psMetadataAddStr(hdu->header, PS_LIST_TAIL, headerName, PS_META_REPLACE,
+                                 "Image MD5", md5string);
+                psFree(md5string);
+                psFree(headerName);
 
                 pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
@@ -59,4 +103,20 @@
     pmFPAfileActivate (config->files, true, NULL);
 
+    // Write out summary statistics
+    if (stats) {
+        const char *statsMDC = psMetadataConfigFormat(stats);
+        if (!statsMDC || strlen(statsMDC) == 0) {
+            psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
+            psFree(stats);
+            fclose(statsFile);
+            return false;
+        }
+        fprintf(statsFile, "%s", statsMDC);
+        psFree((void*)statsMDC);
+        fclose(statsFile);
+
+        psFree(stats);
+    }
+
     return true;
 }
