Index: /trunk/pedestal/.cvsignore
===================================================================
--- /trunk/pedestal/.cvsignore	(revision 11975)
+++ /trunk/pedestal/.cvsignore	(revision 11975)
@@ -0,0 +1,14 @@
+Makefile
+Makefile.in
+aclocal.m4
+autom4te.cache
+compile
+config.guess
+config.log
+config.status
+config.sub
+configure
+depcomp
+install-sh
+ltmain.sh
+missing
Index: /trunk/pedestal/Makefile.am
===================================================================
--- /trunk/pedestal/Makefile.am	(revision 11975)
+++ /trunk/pedestal/Makefile.am	(revision 11975)
@@ -0,0 +1,3 @@
+SUBDIRS = src
+
+CLEANFILES = *~ core core.*
Index: /trunk/pedestal/autogen.sh
===================================================================
--- /trunk/pedestal/autogen.sh	(revision 11975)
+++ /trunk/pedestal/autogen.sh	(revision 11975)
@@ -0,0 +1,103 @@
+#!/bin/sh
+# Run this to generate all the initial makefiles, etc.
+
+srcdir=`dirname $0`
+test -z "$srcdir" && srcdir=.
+
+ORIGDIR=`pwd`
+cd $srcdir
+
+PROJECT=ppImage
+TEST_TYPE=-f
+# change this to be a unique filename in the top level dir
+FILE=autogen.sh
+
+DIE=0
+
+LIBTOOLIZE=libtoolize
+ACLOCAL="aclocal $ACLOCAL_FLAGS"
+AUTOHEADER=autoheader
+AUTOMAKE=automake
+AUTOCONF=autoconf
+
+($LIBTOOLIZE --version) < /dev/null > /dev/null 2>&1 || {
+        echo
+        echo "You must have $LIBTOOlIZE installed to compile $PROJECT."
+        echo "Download the appropriate package for your distribution,"
+        echo "or get the source tarball at http://ftp.gnu.org/gnu/libtool/"
+        DIE=1
+}
+
+($ACLOCAL --version) < /dev/null > /dev/null 2>&1 || {
+        echo
+        echo "You must have $ACLOCAL installed to compile $PROJECT."
+        echo "Download the appropriate package for your distribution,"
+        echo "or get the source tarball at http://ftp.gnu.org/gnu/automake/"
+        DIE=1
+}
+
+($AUTOHEADER --version) < /dev/null > /dev/null 2>&1 || {
+        echo
+        echo "You must have $AUTOHEADER installed to compile $PROJECT."
+        echo "Download the appropriate package for your distribution,"
+        echo "or get the source tarball at http://ftp.gnu.org/gnu/autoconf/"
+        DIE=1
+}
+
+($AUTOMAKE --version) < /dev/null > /dev/null 2>&1 || {
+        echo
+        echo "You must have $AUTOMAKE installed to compile $PROJECT."
+        echo "Download the appropriate package for your distribution,"
+        echo "or get the source tarball at http://ftp.gnu.org/gnu/automake/"
+        DIE=1
+}
+
+($AUTOCONF --version) < /dev/null > /dev/null 2>&1 || {
+        echo
+        echo "You must have $AUTOCONF installed to compile $PROJECT."
+        echo "Download the appropriate package for your distribution,"
+        echo "or get the source tarball at http://ftp.gnu.org/gnu/autoconf/"
+        DIE=1
+}
+
+if test "$DIE" -eq 1; then
+        exit 1
+fi
+
+test $TEST_TYPE $FILE || {
+        echo "You must run this script in the top-level $PROJECT directory"
+        exit 1
+}
+
+if test -z "$*"; then
+        echo "I am going to run ./configure with no arguments - if you wish "
+        echo "to pass any to it, please specify them on the $0 command line."
+fi
+
+$LIBTOOLIZE --copy --force || echo "$LIBTOOlIZE failed"
+$ACLOCAL || echo "$ACLOCAL failed"
+$AUTOHEADER || echo "$AUTOHEADER failed"
+$AUTOMAKE --add-missing --force-missing --copy || echo "$AUTOMAKE failed"
+$AUTOCONF || echo "$AUTOCONF failed"
+
+cd $ORIGDIR
+
+run_configure=true
+for arg in $*; do
+    case $arg in
+        --no-configure)
+            run_configure=false
+            ;;
+        *)
+            ;;
+    esac
+done
+
+if $run_configure; then
+    $srcdir/configure --enable-maintainer-mode "$@"
+    echo
+    echo "Now type 'make' to compile $PROJECT."
+else
+    echo
+    echo "Now run 'configure' and 'make' to compile $PROJECT."
+fi
Index: /trunk/pedestal/configure.ac
===================================================================
--- /trunk/pedestal/configure.ac	(revision 11975)
+++ /trunk/pedestal/configure.ac	(revision 11975)
@@ -0,0 +1,28 @@
+AC_PREREQ(2.59)
+
+AC_INIT([pedestal], [1.0.0], [ipp-support@ifa.hawaii.edu])
+AC_CONFIG_SRCDIR([src])
+
+AM_INIT_AUTOMAKE([1.6 foreign dist-bzip2])
+AM_CONFIG_HEADER([src/config.h])
+AM_MAINTAINER_MODE
+
+IPP_STDCFLAGS
+
+AC_LANG(C)
+AC_GNU_SOURCE
+AC_PROG_CC
+AC_PROG_INSTALL
+dnl AC_PROG_LIBTOOL
+AC_SYS_LARGEFILE
+
+PKG_CHECK_MODULES([PSLIB], [pslib >= 1.0.0])
+
+IPP_STDOPTS
+CFLAGS="${CFLAGS=} -Wall -Werror -std=c99"
+
+AC_CONFIG_FILES([
+  Makefile
+  src/Makefile
+])
+AC_OUTPUT
Index: /trunk/pedestal/src/.cvsignore
===================================================================
--- /trunk/pedestal/src/.cvsignore	(revision 11975)
+++ /trunk/pedestal/src/.cvsignore	(revision 11975)
@@ -0,0 +1,7 @@
+.deps
+Makefile
+Makefile.in
+config.h
+config.h.in
+pedestal
+stamp-h1
Index: /trunk/pedestal/src/Makefile.am
===================================================================
--- /trunk/pedestal/src/Makefile.am	(revision 11975)
+++ /trunk/pedestal/src/Makefile.am	(revision 11975)
@@ -0,0 +1,14 @@
+bin_PROGRAMS = pedestal
+
+pedestal_CPPFLAGS = $(PSLIB_CFLAGS) $(pedestal_CFLAGS)
+pedestal_LDFLAGS = $(PSLIB_LIBS)
+pedestal_SOURCES = \
+	pedestal.c
+
+CLEANFILES = *~
+
+clean-local:
+	-rm -f TAGS
+# Tags for emacs
+tags:
+	etags `find . -name \*.[ch] -print`
Index: /trunk/pedestal/src/pedestal.c
===================================================================
--- /trunk/pedestal/src/pedestal.c	(revision 11975)
+++ /trunk/pedestal/src/pedestal.c	(revision 11975)
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <assert.h>
+#include <pslib.h>
+
+static bool subtract(psFits *inFile, psFits *outFile, const char *extname, const psMetadata *header)
+{
+    assert(inFile);
+    assert(outFile);
+    assert(extname);
+    assert(header);
+
+    int naxis = psMetadataLookupS32(NULL, header, "NAXIS"); // NAXIS from the header
+    if (naxis == 0) {
+        // No image attached
+        return psFitsWriteBlank(outFile, header, extname);
+    }
+
+    if (naxis != 3) {
+        psWarning("Extension %s has NAXIS == %d --- unable to subtract pedestal.\n", extname, naxis);
+        return false;
+    }
+
+    int naxis3 = psMetadataLookupS32(NULL, header, "NAXIS3"); // NAXIS3 from the header
+    if (naxis3 != 2) {
+        psWarning("Extension %s has NAXIS3 == %d --- unable to subtract pedestal.\n", extname, naxis3);
+        return false;
+    }
+
+    psFitsMoveExtName(inFile, extname);
+    psArray *cube = psFitsReadImageCube(inFile, psRegionSet(0, 0, 0, 0)); // Image cube
+    assert(cube->n == 2);               // We checked NAXIS3 earlier
+
+    psImage *image = cube->data[0];     // The image data
+    psImage *pedestal = cube->data[1];  // The pedestal data
+
+    image = (psImage*)psBinaryOp(image, image, "-", pedestal);
+
+    bool status = psFitsWriteImage(outFile, header, image, 0, extname); // Status of write
+    psFree(cube);
+
+    return status;
+}
+
+int main(int argc, char *argv[])
+{
+    if (argc != 3) {
+        printf("Subtract pedestal from FITS images.\n\n"
+               "Usage: %s IN.fits OUT.fits\n\n", argv[0]);
+        exit(argc == 1 ? PS_EXIT_SUCCESS : PS_EXIT_UNKNOWN_ERROR);
+    }
+
+    const char *inName = argv[1];       // Input file name
+    psFits *inFile = psFitsOpen(inName, "r"); // Input file
+    if (!inFile) {
+        psErrorStackPrint(stderr, "Unable to open input file %s", inName);
+        exit(PS_EXIT_DATA_ERROR);
+    }
+
+    const char *outName = argv[2];      // Output file name
+    psFits *outFile = psFitsOpen(outName, "w"); // Output file
+    if (!outFile) {
+        psErrorStackPrint(stderr, "Unable to open output file %s", outName);
+        exit(PS_EXIT_DATA_ERROR);
+    }
+
+    psMetadata *headers = psFitsReadHeaderSet(NULL, inFile); // Headers in the input
+
+    psMetadata *phu = psMetadataLookupMetadata(NULL, headers, "PHU"); // The PHU
+    subtract(inFile, outFile, "PHU", phu);
+    psMetadataRemoveKey(headers, "PHU");
+
+    psMetadataIterator *iter = psMetadataIteratorAlloc(headers, PS_LIST_HEAD, NULL); // Iterator for headers
+    psMetadataItem *item;               // Item from iteration
+    while ((item = psMetadataGetAndIncrement(iter))) {
+        assert(item->type == PS_DATA_METADATA);
+        subtract(inFile, outFile, item->name, item->data.V);
+    }
+    psFree(iter);
+    psFree(headers);
+
+    psFitsClose(inFile);
+    psFitsClose(outFile);
+
+    return PS_EXIT_SUCCESS;
+}
