Index: /trunk/ppImage/src/ppImage.h
===================================================================
--- /trunk/ppImage/src/ppImage.h	(revision 12806)
+++ /trunk/ppImage/src/ppImage.h	(revision 12807)
@@ -7,4 +7,7 @@
 
 #include <stdio.h>
+#include <string.h>
+#include <strings.h>  // for strcasecmp
+#include <unistd.h>   // for unlink
 #include "pslib.h"
 #include "psmodules.h"
Index: /trunk/ppImage/src/ppImageAstrom.c
===================================================================
--- /trunk/ppImage/src/ppImageAstrom.c	(revision 12806)
+++ /trunk/ppImage/src/ppImageAstrom.c	(revision 12807)
@@ -17,65 +17,17 @@
     // find or define a pmFPAfile PSPHOT.INPUT
     pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSASTRO.INPUT");
-    if (!status) {
-	// psphotReadout requires a pmFPAfile supplied with the name PSASTRO.INPUT
-	// create a pmFPAfile which points at PSPHOT.OUTPUT
-	// mode is 'REFERENCE' to prevent double frees of the fpa
-	pmFPAfile *output = psMetadataLookupPtr (&status, config->files, "PSPHOT.OUTPUT");
-	input = pmFPAfileDefineInput (config, output->fpa, "PSASTRO.INPUT");
-	input->mode = PM_FPA_MODE_REFERENCE;
-
-	pmFPAfileDefineOutput (config, input->fpa, "PSASTRO.OUTPUT");
-    }
+    PS_ASSERT (input, false);
 
     // convert the output sources created by psphot into astrometry objects
     if (!psastroConvertFPA (input->fpa, recipe)) {
-	psErrorStackPrint(stderr, "error loading input data\n");
-	exit (1);
+	psError (PSASTRO_ERR_UNKNOWN, false, "error reading input data\n");
+	return false;
     }
 
-    // interpret the available initial astrometric information
-    // apply the initial guess
-    if (!psastroAstromGuess (config)) {
-	psErrorStackPrint(stderr, "failed to determine initial astrometry guess\n");
-	exit (1);
+    if (!psastroAnalysis (config)) {
+	psError (PSASTRO_ERR_UNKNOWN, false, "failure in psastro analysis\n");
+	return false;
     }
 
-    // load the reference stars overlapping the data stars
-    psArray *refs = psastroLoadRefstars(config);
-    if (!refs) {
-	psErrorStackPrint(stderr, "failed to load reference data\n");
-	exit (1);
-    }
-
-    if (!psastroChooseRefstars (config, refs)) {
-	psErrorStackPrint(stderr, "failed to select reference data for chips\n");
-	exit (1);
-    }
-
-    // XXX does this check the recipe??
-    bool chipastro = psMetadataLookupBool (NULL, config->arguments, "PSASTRO.CHIP.MODE");
-    bool mosastro  = psMetadataLookupBool (NULL, config->arguments, "PSASTRO.MOSAIC.MODE");
-    if (!chipastro && !mosastro) {
-	psLogMsg ("psastro", 3, "no astrometry mode selected, assuming chip mode\n");
-	chipastro= true;
-    }
-
-    if (chipastro) {
-      if (!psastroChipAstrom (config, refs)) {
-	    psErrorStackPrint(stderr, "failed to perform single chip astrometry\n");
-	    exit (1);
-	}
-    } 
-    if (mosastro) {
-	if (!psastroMosaicAstrom (config, refs)) {
-	    psErrorStackPrint(stderr, "failed to perform mosaic camera astrometry\n");
-	    exit (1);
-	}
-    }
-
-    // XXX should this be left to the ppImageLoop?
-    psastroDataSave (config);
-
-    psFree (refs);
     return true;
 }
Index: /trunk/ppImage/src/ppImageParseCamera.c
===================================================================
--- /trunk/ppImage/src/ppImageParseCamera.c	(revision 12806)
+++ /trunk/ppImage/src/ppImageParseCamera.c	(revision 12807)
@@ -5,5 +5,4 @@
 # include "ppImage.h"
 
-// XXX clean up error checks: return NULL, not psAbort
 ppImageOptions *ppImageParseCamera (pmConfig *config) {
 
@@ -17,19 +16,8 @@
     }
 
-#if 0
-    // the input image defines the camera, and all recipes and options the follow
-    pmFPAfile *inputMask = pmFPAfileDefineFromArgs (&status, config, "PPIMAGE.INPUT.MASK", "INPUT.MASK");
-    if (!status || !inputMask) {
-        psError(PS_ERR_IO, false, "Failed to build FPA from PPIMAGE.INPUT.MASK");
-        return NULL;
-    }
-
-    // the input image defines the camera, and all recipes and options the follow
-    pmFPAfile *inputWeight = pmFPAfileDefineFromArgs (&status, config, "PPIMAGE.INPUT.WEIGHT", "INPUT.WEIGHT");
-    if (!status || !inputWeight) {
-        psError(PS_ERR_IO, false, "Failed to build FPA from PPIMAGE.INPUT.WEIGHT");
-        return NULL;
-    }
-#endif
+    // if MASK or WEIGHT was supplied on command line, bind files to 'input'
+    // the mask and weight will be mosaicked with the image
+    pmFPAfileBindFromArgs (NULL, input, config, "PPIMAGE.INPUT.MASK",   "MASK");
+    pmFPAfileBindFromArgs (NULL, input, config, "PPIMAGE.INPUT.WEIGHT", "WEIGHT");
 
     // add recipe options supplied on command line
@@ -189,4 +177,38 @@
         psFree(options);
         return NULL;
+    }
+
+    // For photometry, we operate on the chip-mosaicked image
+    if (options->doPhotom) {
+	pmFPAfile *psphotInput = pmFPAfileDefineInput (config, byChip->fpa, "PSPHOT.INPUT");
+	PS_ASSERT (psphotInput, false);
+	psphotInput->mode = PM_FPA_MODE_REFERENCE;
+
+	// define associated psphot input/output files
+	if (!psphotDefineFiles (config, psphotInput)) {
+	    psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psphot");
+	    return false;
+	}
+    }
+
+    // For photometry, we operate on the chip-mosaicked image
+    if (options->doAstromChip || options->doAstromMosaic) {
+	if (!options->doPhotom) {
+	    psError (PSASTRO_ERR_CONFIG, false, "photometry mode is not selected; it is required for astrometry");
+	    return false;
+	}
+
+	pmFPAfile *psphotOutput = psMetadataLookupPtr (&status, config->files, "PSPHOT.OUTPUT");
+	PS_ASSERT (psphotOutput, false);
+
+	pmFPAfile *psastroInput = pmFPAfileDefineInput (config, psphotOutput->fpa, "PSASTRO.INPUT");
+	PS_ASSERT (psastroInput, false);
+	psastroInput->mode = PM_FPA_MODE_REFERENCE;
+
+	// define associated psphot input/output files
+	if (!psastroDefineFiles (config, psastroInput)) {
+	    psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psastro");
+	    return false;
+	}
     }
 
@@ -265,17 +287,17 @@
     }
 
-#if 0
-    // Get a look inside all the files.
-    psMetadataIterator *filesIter = psMetadataIteratorAlloc(config->files, PS_LIST_HEAD, NULL); // Iterator
-    psMetadataItem *item;               // Item from iteration
-    fprintf(stderr, "Files:\n");
-    while ((item = psMetadataGetAndIncrement(filesIter))) {
-        pmFPAfile *file = item->data.V; // File of interest
-        fprintf(stderr, "%s: %p %p %p (%p) %p\n", file->name,
-                file->src, file->fpa,
-                file->camera, file->fpa->camera, file->format);
-    }
-    psFree(filesIter);
-#endif
+    if (psTraceGetLevel("ppImage.config") > 0) {
+	// Get a look inside all the files.
+	psMetadataIterator *filesIter = psMetadataIteratorAlloc(config->files, PS_LIST_HEAD, NULL); // Iterator
+	psMetadataItem *item;               // Item from iteration
+	fprintf(stderr, "Files:\n");
+	while ((item = psMetadataGetAndIncrement(filesIter))) {
+	    pmFPAfile *file = item->data.V; // File of interest
+	    fprintf(stderr, "%s: %p %p %p (%p) %p\n", file->name,
+		    file->src, file->fpa,
+		    file->camera, file->fpa->camera, file->format);
+	}
+	psFree(filesIter);
+    }
 
     return (options);
Index: /trunk/ppImage/src/ppImagePhotom.c
===================================================================
--- /trunk/ppImage/src/ppImagePhotom.c	(revision 12806)
+++ /trunk/ppImage/src/ppImagePhotom.c	(revision 12807)
@@ -5,7 +5,5 @@
 # include "ppImage.h"
 
-// the top portion of this function is equivalent to psphotParseCamera,
-// but different in a few important ways.  
-// XXX move this section to ppImageParseCamera?
+// In this function, we perform the psphot analysis routine for the chip-mosaicked images
 bool ppImagePhotom (pmConfig *config, pmFPAview *view) {
 
@@ -16,120 +14,12 @@
     psphotModelGroupInit ();
 
-    // select recipe options supplied on command line
-    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
-
     // find or define a pmFPAfile PSPHOT.INPUT
     pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSPHOT.INPUT");
     if (!status) {
-	// psphotReadout requires a pmFPAfile supplied with the name PSPHOT.INPUT
-	// create a pmFPAfile which points to PPIMAGE.OUTPUT.CHIP (guaranteed to be mosaiced by chip)
-	// mode is 'REFERENCE' to prevent double frees of the fpa
-	pmFPAfile *output = psMetadataLookupPtr (&status, config->files, "PPIMAGE.OUTPUT.CHIP");
-	input = pmFPAfileDefineInput (config, output->fpa, "PSPHOT.INPUT");
-	input->mode = PM_FPA_MODE_REFERENCE;
-
-	// we set the freeLevel to be FPA so this pmFPAfile may be used as input to psastro
-	// XXX make this the resonsibility of a ppImageAstrom call?
-	// XXX test for psastro requests?
-	// XXX need to make this an INTERNAL file if output is not requested.
-	bool saveOutput = psMetadataLookupBool (NULL, recipe, "SAVE.OUTPUT");
-	if (saveOutput) {
-	  pmFPAfile *outfile = pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.OUTPUT");
-	  outfile->freeLevel = PM_FPA_LEVEL_FPA;
-	}
+	psError(PSPHOT_ERR_CONFIG, false, "PSPHOT.INPUT I/O file is not defined");
+	return false;
     }
 
-    // if we have requested PSPHOT.SRC (externally supplied sources), attempt to resolve it 
-    if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.SRC")) { 
-	pmFPAfileDefineFromArgs (&status, config, "PSPHOT.SRC", "PSPHOT.SRC");
-	if (!status) {
-	    psError(PSPHOT_ERR_CONFIG, false, "Failed to find/build PSPHOT.SRC");
-	    return status;
-	}
-    }
-
-    // NOTE the difference here from psphotParseCamera: the mask and weight
-    // images are supplied by ppImage and need not / should not be loaded here.
-
-    // optionally load the PSF Model and/or fixed stars
-    pmFPAfileBindFromArgs (NULL, input, config, "PSPHOT.PSF", "PSPHOT.PSF");
-
-    // set default recipe values here
-    // XXX place this in a psphot library function?
-    psMetadataAddStr (recipe, PS_LIST_TAIL, "FITMODE",         PS_META_NO_REPLACE, "default fitting mode", "NONE");
-    psMetadataAddStr (recipe, PS_LIST_TAIL, "BREAK_POINT",     PS_META_NO_REPLACE, "default break point",  "NONE");
-    psMetadataAddF32 (recipe, PS_LIST_TAIL, "ZERO_PT",         PS_META_NO_REPLACE, "default zero point",    25.00);
-    psMetadataAddS32 (recipe, PS_LIST_TAIL, "BACKGROUND.XBIN", PS_META_NO_REPLACE, "default binning",          64);
-    psMetadataAddS32 (recipe, PS_LIST_TAIL, "BACKGROUND.YBIN", PS_META_NO_REPLACE, "default binning",          64);
-
-    // determine PHOTCODE from fpa & view, overwrite in recipe
-    char *photcode = pmConceptsPhotcodeForView (config, input, view);
-    if (photcode) {
-	psMetadataAddStr (recipe, PS_LIST_TAIL, "PHOTCODE", PS_META_REPLACE, "photcode from FPA concepts", photcode);
-	psFree (photcode);
-    }
-
-    // optionally save the residual image 
-    if (psMetadataLookupBool(NULL, recipe, "SAVE.RESID")) {
-	if (!pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.RESID")) {
-	    psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKMDL");
-	    return false;
-	}
-    }
-
-    int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
-    int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
-
-    // these calls construct a new fpa for the I/O handle 
-
-    // optionally save the background model (small FITS image)
-    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKMDL")) {
-	if (!pmFPAfileDefineFromFPA (config, input->fpa, DX, DY, "PSPHOT.BACKMDL")) {
-	    psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKMDL");
-	    return false;
-	}
-    }
-    // optionally save the full background image
-    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKGND")) {
-	if (!pmFPAfileDefineFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKGND")) {
-	    psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKGND");
-	    return false;
-	}
-    }
-    // optionally save the background-subtracted image
-    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKSUB")) {
-	if (!pmFPAfileDefineFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKSUB")) {
-	    psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.");
-	    return false;
-	}
-    }
-    // optionally save the PSF Model
-    if (psMetadataLookupBool(NULL, recipe, "SAVE.PSF")) {
-	if (!pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.PSF.SAVE")) {
-	    psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.PSF.SAVE");
-	    return false;
-	}
-    }
-
-    // optionally save output plots
-    if (psMetadataLookupBool(NULL, recipe, "SAVE.PLOTS")) {
-	if (!pmFPAfileDefineOutput (config, input->fpa, "SOURCE.PLOT.MOMENTS")) {
-	    psTrace ("psphot", 3, "Cannot find a rule for SOURCE.PLOT.MOMENTS");
-	}
-	if (!pmFPAfileDefineOutput (config, input->fpa, "SOURCE.PLOT.PSFMODEL")) {
-	    psTrace ("psphot", 3, "Cannot find a rule for SOURCE.PLOT.PSFMODEL");
-	}
-    }
-
-    // XXX not tested (or defined?)
-    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.PSF_SAMPLE");
-
-    // XXX probably need to deactivate all files and activate the psphot ones explicitly
-
-    // In this section, we iterate over the cells and readout for this chip
-    // and run the psphot analysis routine here
-    // XXX worry about this not having unique PHOTCODES...
-    // view->cell = -1;
-    // iterate over the cells in the current chip
+    // iterate over the cells and readout for this chip
     while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
 	psLogMsg ("ppImagePhotom", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
@@ -146,6 +36,9 @@
 
     // the PSPHOT.INPUT file is a temporary file used to carry PPIMAGE.OUTPUT.CHIP to psphotReadout
+    // XXX not sure that this is needed...
     pmFPAfileActivate (config->files, false, "PSPHOT.INPUT");
 
     return true;
 }
+
+// XXX do we need to deactivate all files and activate the psphot ones explicitly?
