Index: /branches/eam_branches/psphot.20100506/src/Makefile.am
===================================================================
--- /branches/eam_branches/psphot.20100506/src/Makefile.am	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/Makefile.am	(revision 27883)
@@ -95,6 +95,9 @@
 	psphotFitSourcesLinearStack.c \
 	psphotSourceMatch.c           \
-	psphotStackMatchPSFs.c       \
-	psphotStackMatchPSFsUtils.c  \
+	psphotStackMatchPSFs.c        \
+	psphotStackMatchPSFsUtils.c   \
+	psphotStackMatchPSFsPrepare.c \
+	psphotStackOptions.c          \
+	psphotStackPSF.c	      \
 	psphotCleanup.c
 
Index: /branches/eam_branches/psphot.20100506/src/psphot.h
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphot.h	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphot.h	(revision 27883)
@@ -367,10 +367,21 @@
 int pmPhotObjSortByX (const void **a, const void **b);
 
+typedef enum {
+    PSPHOT_CNV_SRC_NONE,
+    PSPHOT_CNV_SRC_AUTO,
+    PSPHOT_CNV_SRC_CNV,
+    PSPHOT_CNV_SRC_RAW,
+} psphotStackConvolveSource;
+
 /// Options for stacking process
 typedef struct {
     // Setup
     
+    int numCols;                            // size of image (X)
+    int numRows;                            // size of image (Y)
+
     int num;                            // Number of inputs
     bool convolve;                      // Convolve images?
+    psphotStackConvolveSource convolveSource;
     // psArray *convImages, *convMasks, *convVariances; // Filenames for the temporary convolved images
 
@@ -420,4 +431,5 @@
 bool psphotStackMatchPSFs (pmConfig *config, const pmFPAview *view);
 bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index);
+bool psphotStackMatchPSFsPrepare (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index);
 
 // psphotStackMatchPSFsUtils
@@ -437,3 +449,8 @@
 bool readImage(psImage **target, const char *name, const pmConfig *config);
 
+pmPSF *psphotStackPSF(const pmConfig *config, int numCols, int numRows, const psArray *psfs, const psVector *inputMask);
+
+psphotStackOptions *psphotStackOptionsAlloc (int num);
+psphotStackConvolveSource psphotStackConvolveSourceFromString (const char *string);
+
 #endif
Index: /branches/eam_branches/psphot.20100506/src/psphotStackArguments.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackArguments.c	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackArguments.c	(revision 27883)
@@ -1,4 +1,5 @@
 # include "psphotStandAlone.h"
 
+static void dumpTemplate(void);
 static void usage(pmConfig *config, int exitCode);
 static void writeHelpInfo(FILE* ofile);
Index: /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFs.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFs.c	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFs.c	(revision 27883)
@@ -36,5 +36,5 @@
     // Generate target PSF
     if (options->convolve) {
-        options->psf = ppStackPSF(config, options->numCols, options->numRows, options->psfs, options->inputMask);
+        options->psf = psphotStackPSF(config, options->numCols, options->numRows, options->psfs, options->inputMask);
         if (!options->psf) {
             psError(psErrorCodeLast(), false, "Unable to determine output PSF.");
Index: /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFsPrepare.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFsPrepare.c	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFsPrepare.c	(revision 27883)
@@ -18,13 +18,13 @@
 
     switch (options->convolveSource) {
-      case AUTO:
+      case PSPHOT_CNV_SRC_AUTO:
 	fileSrc = fileCnv ? fileCnv : fileRaw;
 	break;
 
-      case RAW:
+      case PSPHOT_CNV_SRC_RAW:
 	fileSrc = fileRaw;
 	break;
 
-      case CNV:
+      case PSPHOT_CNV_SRC_CNV:
 	fileSrc = fileCnv;
 	break;
@@ -34,5 +34,5 @@
     }
     if (!fileSrc) {
-	psError(PSPHOT_ERR_CONFIG, "desired convolution source is missing (cnv : %llx, raw : %llx)", (long long) fileCnv, (long long) fileRaw);
+	psError(PSPHOT_ERR_CONFIG, true, "desired convolution source is missing (cnv : %llx, raw : %llx)", (long long) fileCnv, (long long) fileRaw);
 	return false;
     }
@@ -44,5 +44,5 @@
 	if (!psf) {
 	    // XXX if we were not supplied a PSF, we should be able to generate one by calling psphot
-	    psError(PPSTACK_ERR_PROG, false, "Unable to find PSF.");
+	    psError(PSPHOT_ERR_PROG, false, "Unable to find PSF.");
 	    return false;
 	}
@@ -78,12 +78,12 @@
 	pmFPAfile *outputImage = pmFPAfileDefineOutput(config, NULL, "PSPHOT.STACK.OUTPUT.IMAGE");
 	pmReadout *readout = pmFPAviewThisReadout(view, outputImage->fpa); // Readout with sources
-	detections = psMetadataLookupPtr(NULL, readout->analysis, "PSPHOT.DETECTIONS"); // Sources
+	pmDetections *detections = psMetadataLookupPtr(NULL, readout->analysis, "PSPHOT.DETECTIONS"); // Sources
 	if (!detections || !detections->allSources) {
-	    psWarning("No detections found for image %d --- rejecting.", i);
-	    options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PPSTACK_MASK_CAL;
-	    continue;
+	    psWarning("No detections found for image %d --- rejecting.", index);
+	    options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[index] = 0x01;
+	    return true;  // XXX not an error: we continue processing other images
 	}
 	psAssert (detections->allSources, "missing sources?");
-	options->sourceLists->data[i] = psMemIncrRefCounter(detections->allSources);
+	options->sourceLists->data[index] = psMemIncrRefCounter(detections->allSources);
     }
 
@@ -108,5 +108,5 @@
 	if (numFWHM == 0) {
 	    options->inputSeeing->data.F32[index] = NAN;
-	    options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[index] = PPSTACK_MASK_PSF;
+	    options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[index] = 0x02;
 	    psLogMsg("ppStack", PS_LOG_INFO, "Unable to measure PSF FWHM for image %d --- rejected.", index);
 	} else {
Index: /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFsUtils.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFsUtils.c	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFsUtils.c	(revision 27883)
@@ -180,4 +180,5 @@
 // It implicitly assumes the output root name is the same between invocations.
 
+# if (0)
 bool loadKernel (pmConfig *config, pmReadout *readoutCnv, psphotStackOptions *options, int index) {
 
@@ -228,4 +229,5 @@
     return true;
 }
+# endif
 
 bool dumpImage(pmReadout *readoutOut, pmReadout *readoutRef, int index, char *rootname) {
Index: /branches/eam_branches/psphot.20100506/src/psphotStackOptions.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackOptions.c	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackOptions.c	(revision 27883)
@@ -1,10 +1,3 @@
 # include "psphotInternal.h"
-
-typedef enum {
-    PSPHOT_CNV_SRC_NONE,
-    PSPHOT_CNV_SRC_AUTO,
-    PSPHOT_CNV_SRC_CNV,
-    PSPHOT_CNV_SRC_RAW,
-} psphotStackConvolveSource;
 
 static void psphotStackOptionsFree (psphotStackOptions *options) {
@@ -35,4 +28,5 @@
     options->psf = NULL;
     options->convolve = false;
+    options->convolveSource = PSPHOT_CNV_SRC_NONE;
 
     options->psfs = psArrayAlloc(num);
Index: /branches/eam_branches/psphot.20100506/src/psphotStackPSF.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackPSF.c	(revision 27883)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackPSF.c	(revision 27883)
@@ -0,0 +1,47 @@
+# include "psphotInternal.h"
+
+pmPSF *psphotStackPSF(const pmConfig *config, int numCols, int numRows, const psArray *psfs, const psVector *inputMask)
+{
+    bool mdok = false;
+
+#ifndef TESTING
+    // Get the recipe values
+    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, "PPSTACK"); // ppStack recipe
+    psAssert(recipe, "We've thrown an error on this before.");
+
+    int psfInstances = psMetadataLookupS32(NULL, recipe, "PSF.INSTANCES"); // Number of instances for PSF
+    float psfRadius = psMetadataLookupF32(NULL, recipe, "PSF.RADIUS"); // Radius for PSF
+    const char *psfModel = psMetadataLookupStr(NULL, recipe, "PSF.MODEL"); // Model for PSF
+    int psfOrder = psMetadataLookupS32(NULL, recipe, "PSF.ORDER"); // Spatial order for PSF
+
+    psString maskValStr = psMetadataLookupStr(&mdok, recipe, "MASK.VAL"); // Name of bits to mask going in
+    if (!mdok || !maskValStr) {
+        psError(PSPHOT_ERR_CONFIG, false, "Unable to find MASK.VAL in recipe");
+        return false;
+    }
+    psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask
+
+    for (int i = 0; i < psfs->n; i++) {
+        if (inputMask->data.U8[i]) {
+            psFree(psfs->data[i]);
+            psfs->data[i] = NULL;
+        }
+    }
+
+    // Solve for the target PSF
+    pmPSF *psf = pmPSFEnvelope(numCols, numRows, psfs, psfInstances, psfRadius, psfModel, psfOrder, psfOrder, maskVal);
+    if (!psf) {
+        psError(PSPHOT_ERR_PSF, false, "Unable to determine output PSF.");
+        return NULL;
+    }
+#else
+    // Dummy PSF
+    pmPSF *psf = pmPSFBuildSimple("PS_MODEL_PS1_V1", 4.0, 4.0, 0.0, 1.0);
+    if (!psf) {
+        psError(PSPHOT_ERR_PSF, false, "Unable to build dummy PSF.");
+        return NULL;
+    }
+#endif
+
+    return psf;
+}
Index: /branches/eam_branches/psphot.20100506/src/psphotStackParseCamera.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackParseCamera.c	(revision 27882)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackParseCamera.c	(revision 27883)
@@ -113,5 +113,5 @@
 	    }
 	    outputImage->save = true;
-	    outsources->fileID = i;		// this is used to generate output names
+	    outputImage->fileID = i;		// this is used to generate output names
 
 	    pmFPAfile *outputMask = pmFPAfileDefineOutput(config, outputImage->fpa, "PSPHOT.STACK.OUTPUT.MASK");
