Index: /trunk/psphot/src/Makefile.am
===================================================================
--- /trunk/psphot/src/Makefile.am	(revision 6726)
+++ /trunk/psphot/src/Makefile.am	(revision 6727)
@@ -39,11 +39,7 @@
 	psphotGrowthCurve.c	\
 	psphotFakeSources.c	\
+	psphotModelTest.c	\
+	psphotFitSet.c		\
 	psphotCleanup.c	   	
-
-junk =	psphotDefinePixels.c	\
-	pmSourceContour.c	\
-	pmModelFitSet.c		\
-	pmSourceFitSet.c     	\
-	psphotModelTest.c	
 
 noinst_HEADERS =		\
@@ -55,7 +51,2 @@
 tags:
 	etags `find . -name \*.[ch] -print`
-
-#	psphotFullFit.c		
-#	psphotApplyPSF.c	
-#	psphotFitGalaxies.c
-#	psphotLoadPixels.c	
Index: /trunk/psphot/src/psphot.c
===================================================================
--- /trunk/psphot/src/psphot.c	(revision 6726)
+++ /trunk/psphot/src/psphot.c	(revision 6727)
@@ -27,2 +27,19 @@
     exit (0);
 }
+
+/** I/O test code
+
+    psFits *fits = psFitsOpen (argv[1], "r");
+    psMetadata *header = psFitsReadHeader (NULL, fits);
+    psFitsClose (fits);
+
+    psMetadata *new = psMetadataCopy (NULL, header);
+    psMetadataConfigWrite (new, "test.cnf");
+
+    fits = psFitsOpen ("test.fits", "w");
+    psFitsWriteHeaderNotImage (fits, new);
+    psFitsClose (fits);
+
+    exit (0);
+
+**/
Index: /trunk/psphot/src/psphot.h
===================================================================
--- /trunk/psphot/src/psphot.h	(revision 6726)
+++ /trunk/psphot/src/psphot.h	(revision 6727)
@@ -46,5 +46,5 @@
 bool            psphotImageLoop (pmConfig *config);
 
-bool            psphotModelTest (pmReadout *readout, psMetadata *arguments, psMetadata *recipe);
+bool            psphotModelTest (pmReadout *readout, psMetadata *recipe);
 bool            psphotReadout (pmConfig *config, pmFPAview *view);
 void            psphotCleanup (void);
@@ -85,5 +85,5 @@
 // output functions
 bool 		psphotDumpMoments (psMetadata *config, psArray *sources);
-bool            psphotUpdateHeader (psMetadata *header, psMetadata *config);
+psMetadata     *psphotDefineHeader (psMetadata *config);
 
 // PSF / DBL / EXT evaluation functions
Index: unk/psphot/src/psphotApplyPSF.c
===================================================================
--- /trunk/psphot/src/psphotApplyPSF.c	(revision 6726)
+++ 	(revision )
@@ -1,61 +1,0 @@
-# include "psphot.h"
-
-// fit psf model to all objects 
-bool psphotApplyPSF (pmReadout *readout, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky) 
-{ 
-    float x;
-    float y;
-    int   Nfit  = 0;
-    int   Nsub  = 0;
-    int   Niter = 0;
-
-    psTimerStart ("psphot");
-
-    psphotInitLimitsPSF (config);
-    psphotInitRadiusPSF (config, sky, psf->type);
-
-    for (int i = 0; i < sources->n; i++) {
-
-	pmSource *source = sources->data[i];
-
-	// skip non-astronomical objects (very likely defects)
-	if (source->mode  & PM_SOURCE_MODE_BLEND) continue; 
-	if (source->type == PM_SOURCE_TYPE_DEFECT) continue; 
-	if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
-
-	// use the source moments, etc to guess basic model parameters
-	pmModel *modelEXT = pmSourceModelGuess (source, psf->type); 
-
-	// set PSF parameters for this model
-	pmModel *model = pmModelFromPSF (modelEXT, psf);
-	psFree (modelEXT);
-
-	source->modelPSF = model;
-
-	// sets the model radius (via source->model) and adjusts pixels as needed
-	psphotCheckRadiusPSF (readout, source, model);
-
-	x = model->params->data.F32[2];
-	y = model->params->data.F32[3];
-
-	// fit PSF model (set/unset the pixel mask)
-	psImageKeepCircle (source->mask, x, y, model->radius, "OR", PSPHOT_MASK_MARKED);
-	pmSourceFitModel (source, model, true);
-	psImageKeepCircle (source->mask, x, y, model->radius, "AND", ~PSPHOT_MASK_MARKED);
-
-	Niter += model[0].nIter;
-	Nfit ++;
-
-	// check if model fit is acceptable
-	if (psphotEvalPSF (source, source->modelPSF)) {
-	    pmModelSub (source->pixels, source->mask, source->modelPSF, false, false);
-	    source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
-	    source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
-	    Nsub ++;
-	}
-    }
-
-    psLogMsg ("psphot", 3, "fit PSF models: %f sec for %d objects (%d total iterations)\n", psTimerMark ("psphot"), Nfit, Niter);
-    psLogMsg ("psphot", 4, "subtracted %d PSF models\n", Nsub);
-    return (true);
-}
Index: /trunk/psphot/src/psphotCleanup.c
===================================================================
--- /trunk/psphot/src/psphotCleanup.c	(revision 6726)
+++ /trunk/psphot/src/psphotCleanup.c	(revision 6727)
@@ -9,5 +9,6 @@
     psTimeFinalize ();
     pmConceptsDone ();
-    fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "psphot");
+    fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "psphot");
+    // fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "psphot");
     return;
 }
Index: unk/psphot/src/psphotDefinePixels.c
===================================================================
--- /trunk/psphot/src/psphotDefinePixels.c	(revision 6726)
+++ 	(revision )
@@ -1,63 +1,0 @@
-# include "psphot.h"
-
-// x,y are defined in the parent image coords of readout->image
-bool psphotDefinePixels(pmSource *mySource, 
-			const pmReadout *readout,
-			psF32 x, 
-			psF32 y,
-			psF32 Radius)
-{
-    psRegion srcRegion;
-
-    // Grab a subimage of the original image of size (2 * outerRadius).
-    srcRegion = psRegionForSquare (x, y, Radius);
-    srcRegion = psRegionForImage (readout->image, srcRegion);
-
-    mySource->pixels = psImageSubset(readout->image, srcRegion);
-    mySource->weight = psImageSubset(readout->weight, srcRegion);
-    mySource->mask   = psImageSubset(readout->mask,  srcRegion);
-    mySource->region = srcRegion;
-
-    return true;
-}
-
-bool psphotRedefinePixels(pmSource *mySource, 
-			  const pmReadout *readout,
-			  psF32 x, 
-			  psF32 y,
-			  psF32 Radius)
-{
-    bool extend;
-    psRegion newRegion;
-
-    if (Radius == 0) return false;
-
-    // check to see if new region is completely contained within old region
-    newRegion = psRegionForSquare (x, y, Radius);
-    newRegion = psRegionForImage (readout->image, newRegion);
-
-    extend = false;
-    extend |= (int)(newRegion.x0) < (int)(mySource->region.x0);
-    extend |= (int)(newRegion.x1) > (int)(mySource->region.x1);
-    extend |= (int)(newRegion.y0) < (int)(mySource->region.y0);
-    extend |= (int)(newRegion.y1) > (int)(mySource->region.y1);
-
-    extend |= (mySource->pixels == NULL);
-    extend |= (mySource->weight == NULL);
-    extend |= (mySource->mask == NULL);
-
-    // extend = true;
-    if (extend) {
-	// Grab a new subimage
-	psFree (mySource->pixels);
-	psFree (mySource->weight);
-	psFree (mySource->mask);
-
-	mySource->pixels = psImageSubset(readout->image,  newRegion);
-	mySource->weight = psImageSubset(readout->weight, newRegion);
-	mySource->mask   = psImageSubset(readout->mask,   newRegion);
-	mySource->region = newRegion;
-    }
-
-    return extend;
-}
Index: unk/psphot/src/psphotFitGalaxies.c
===================================================================
--- /trunk/psphot/src/psphotFitGalaxies.c	(revision 6726)
+++ 	(revision )
@@ -1,66 +1,0 @@
-# include "psphot.h"
-
-bool psphotFitExtended (pmReadout *readout, psMetadata *config, psArray *sources, psStats *skyStats)
-{ 
-    bool  status;
-    float x;
-    float y;
-    int   Nfit  = 0;
-    int   Nsub  = 0;
-    int   Niter = 0;
-
-    psTimerStart ("psphot");
-
-    float EXT_MIN_SN  	   = psMetadataLookupF32 (&status, config, "EXT_MIN_SN");
-    float EXT_MOMENTS_RAD  = psMetadataLookupF32 (&status, config, "EXT_MOMENTS_RADIUS");
-    char       *modelName  = psMetadataLookupStr (&status, config, "EXT_MODEL");
-    pmModelType modelType  = pmModelSetType (modelName);
-
-    psphotInitRadiusEXT (config, skyStats, modelType);
-
-    for (int i = 0; i < sources->n; i++) {
-	pmSource *source = sources->data[i];
-
-	// only fit suspected extended sources
-	if (source->type != PM_SOURCE_TYPE_EXTENDED) continue;
-	if (source->mode  & PM_SOURCE_MODE_BLEND) continue; 
-
-	// skip possible extended sources below fit threshold
-	if (source->moments->SN < EXT_MIN_SN) continue;
-
-	// recalculate the source moments using the larger extended-source moments radius
-	status = pmSourceMoments (source, EXT_MOMENTS_RAD);
-	if (!status) {
-	  source->mode |= PM_SOURCE_MODE_FAIL;  // better choice?
-	  continue;
-	}
-
-	// use the source moments, etc to guess basic model parameters
-	pmModel  *model  = pmSourceModelGuess (source, modelType); 
-	source->modelEXT = model;
-	x = model->params->data.F32[2];
-	y = model->params->data.F32[3];
-
-	// sets the model radius (via source->model) and adjusts pixels as needed
-	psphotCheckRadiusEXT (readout, source, model);
-
-	// fit EXT (not PSF) model (set/unset the pixel mask)
-	psImageKeepCircle (source->mask, x, y, model->radius, "OR", PM_SOURCE_MASK_MARKED);
-	pmSourceFitModel (source, model, false);
-	psImageKeepCircle (source->mask, x, y, model->radius, "AND", ~PM_SOURCE_MASK_MARKED);
-
-	Niter += model[0].nIter;
-	Nfit++;
-
-	// check if model fit is acceptable
-	if (pmModelFitStatus (model)) {
-	    pmModelSub (source->pixels, source->mask, model, false, false);
-	    source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
-	    source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
-	    Nsub ++;
-	}
-    }
-    psLogMsg ("psphot", 3, "fit EXT models: %f sec for %d objects (%d total iterations)\n", psTimerMark ("psphot"), Nfit, Niter);
-    psLogMsg ("psphot", 4, "subtracted %d EXT objects\n", Nsub);
-    return (true);
-}
Index: unk/psphot/src/psphotFullFit.c
===================================================================
--- /trunk/psphot/src/psphotFullFit.c	(revision 6726)
+++ 	(revision )
@@ -1,137 +1,0 @@
-# include "psphot.h"
-
-bool psphotFullFit (pmReadout *readout, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky) 
-{ 
-    bool  status;
-    float x, y;
-    int   Nfit = 0;
-    int   Nsub = 0;
-    int   Niter = 0;
-
-    psTimerStart ("psphot");
-
-    // source analysis is done in S/N order (brightest first)
-    sources = psArraySort (sources, psphotSortBySN);
-    
-    // extended source model parameters
-    float EXT_MIN_SN  	   = psMetadataLookupF32 (&status, config, "EXT_MIN_SN");
-    float EXT_MOMENTS_RAD  = psMetadataLookupF32 (&status, config, "EXT_MOMENTS_RADIUS");
-
-    // extended source model descriptions
-    char         *modelNameEXT = psMetadataLookupStr (&status, config, "EXT_MODEL");
-    pmModelType   modelTypeEXT = pmModelSetType (modelNameEXT);
-
-    psphotInitLimitsPSF (config);
-    psphotInitRadiusPSF (config, sky, psf->type);
-    psphotInitRadiusEXT (config, sky, modelTypeEXT);
-
-    for (int i = 0; i < sources->n; i++) {
-
-	pmSource *source = sources->data[i];
-
-	// skip non-astronomical objects (very likely defects)
-	// XXX EAM : should we try these anyway?
-	if (source->mode &  PM_SOURCE_MODE_BLEND) continue;
-	if (source->type == PM_SOURCE_TYPE_DEFECT) continue; 
-	if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
-
-	// save the PSF model from the Ensemble fit
-	pmModel *LIN  = pmModelCopy (source->modelPSF);
-
-	// attempt to fit the PSF model
-	pmModel *modelPSF = source->modelPSF;
-
-	// replace object in image
-	pmModelAdd (source->pixels, source->mask, modelPSF, false, false);
-	source->mode &= ~PM_SOURCE_MODE_SUBTRACTED;
-
-	psphotCheckRadiusPSF (readout, source, modelPSF);
-
-	x = modelPSF->params->data.F32[2];
-	y = modelPSF->params->data.F32[3];
-
-	// fit PSF model (set/unset the pixel mask)
-	psImageKeepCircle (source->mask, x, y, modelPSF->radius, "OR", PM_SOURCE_MASK_MARKED);
-	pmSourceFitModel (source, modelPSF, true);
-	psImageKeepCircle (source->mask, x, y, modelPSF->radius, "AND", ~PM_SOURCE_MASK_MARKED);
-
-	// track model evaluations
-	Niter += modelPSF[0].nIter;
-	Nfit ++;
-
-	// does the PSF model succeed?
-	if (psphotEvalPSF (source, source->modelPSF)) {
-	    pmModelSub (source->pixels, source->mask, source->modelPSF, false, false);
-	    source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
-	    source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
-	    psFree (LIN);
-	    Nsub ++;
-	    continue;
-	} 
-
-	// use the moments to get the extended source angle
-	// displace on either side of the main peak by 1sigma or so
-	// cut the peak in half?  use the main peak?
-	// also try fit set on (unsaturated?) objects with a blend within 2-3 sigma
-	// allow all possible nearby blends to be fit?
-	// EllipseMoments moments;
-	// moments.x2 = source->moments->Sx;
-	// moments.y2 = source->moments->Sy;
-	// moments.xy = source->moments->Sxy;
-	// EllipseAxes axes = EllipseMomentsToAxes (moments);
-
-	// skip the source if we don't think it is extended
-	if (source->type != PM_SOURCE_TYPE_EXTENDED) goto subLINEAR;
-	if (source->moments->SN < EXT_MIN_SN) goto subLINEAR;
-
-	// add the double-PSF fit mode
-	// how do we compare the results?  chisq? 
-	
-	// recalculate the source moments using the larger extended-source moments radius
-	if (!pmSourceMoments (source, EXT_MOMENTS_RAD)) goto subLINEAR;
-
-	// use the source moments, etc to guess basic model parameters
-	source->modelEXT = pmSourceModelGuess (source, modelTypeEXT); 
-	pmModel *modelEXT = source->modelEXT;
-
-	psphotCheckRadiusEXT (readout, source, modelEXT);
-
-	x = modelEXT->params->data.F32[2];
-	y = modelEXT->params->data.F32[3];
-
-	// fit EXT (not PSF) model (set/unset the pixel mask)
-	psImageKeepCircle (source->mask, x, y, modelEXT->radius, "OR", PM_SOURCE_MASK_MARKED);
-	pmSourceFitModel (source, modelEXT, false);
-	psImageKeepCircle (source->mask, x, y, modelEXT->radius, "AND", ~PM_SOURCE_MASK_MARKED);
-
-	// track model evaluations
-	Niter += modelEXT[0].nIter;
-	Nfit++;
-
-	// does the EXT model succeed?
-	if (psphotEvalEXT (source, source->modelEXT)) {
-	    pmModelSub (source->pixels, source->mask, source->modelEXT, false, false);
-	    source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
-	    source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
-	    psFree (LIN);
-	    Nsub ++;
-	    continue;
-	}
-
-	// re-subtract PSF for object, leave local sky
-    subLINEAR:
-	psFree (source->modelPSF);
-	source->modelPSF = LIN;
-	pmModelSub (source->pixels, source->mask, source->modelPSF, false, false);
-	source->mode |= PM_SOURCE_MODE_SUBTRACTED;
-	source->mode |= PM_SOURCE_MODE_TEMPSUB;
-	Nsub ++;
-    }
-
-    psLogMsg ("psphot", 3, "fit PSF models: %f sec for %d objects (%d total iterations)\n", psTimerMark ("psphot"), Nfit, Niter);
-    psLogMsg ("psphot", 4, "subtracted %d PSF objects\n", Nsub);
-    return (true);
-}
-
-
-// XXX EAM : can we distinguish the outcomes enough to re-add poor fits, etc?
Index: /trunk/psphot/src/psphotImageLoop.c
===================================================================
--- /trunk/psphot/src/psphotImageLoop.c	(revision 6726)
+++ /trunk/psphot/src/psphotImageLoop.c	(revision 6727)
@@ -28,5 +28,4 @@
 
 	while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
-	    continue;
             psLogMsg ("psphot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
             if (! cell->process) { continue; }
@@ -39,15 +38,8 @@
 		if (! readout->data_exists) { continue; }
 
-		// run a single-model test if desired
-		// XXX move this to psphotReadout??
-		// psphotModelTest (readout, recipe);
-
 		// run the actual photometry analysis
 		psphotReadout (config, view);
 
 		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
-
-		// write out the desired output dataset(s)
-		// psphotOutput (view, recipe);
 	    }
 	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
Index: unk/psphot/src/psphotLoadPixels.c
===================================================================
--- /trunk/psphot/src/psphotLoadPixels.c	(revision 6726)
+++ 	(revision )
@@ -1,38 +1,0 @@
-# include "psphot.h"
-
-bool ppImageLoadPixels (ppFile *input, psDB *db, int chipNum, int cellNum)
-{
-    // an input chip is valid for processing if:
-    // (((chipNum == i) || (chipNum == -1)) && chip.process)
-
-    // If we have not opened the file, skip it
-    if (input->fits == NULL) {
-        return false;
-    }
-
-    psTrace(__func__, 1, "Loading %d,%d for %s\n", chipNum, cellNum, input->filename);
-
-    // set input:valid flags according to process and chipNum/cellNum
-    for (int i = 0; i < input->fpa->chips->n; i++) {
-        pmChip *chip = input->fpa->chips->data[i]; // Chip in input image
-        chip->process = (! chip->exists && ((chipNum == i) || (chipNum == -1)));
-
-        for (int j = 0; j < chip->cells->n; j++) {
-            pmCell *cell = chip->cells->data[j]; // Cell in input image
-            cell->process &= chip->process;
-            cell->process = (! cell->exists && ((cellNum == i) || (cellNum == -1)));
-        }
-    }
-    
-    // Read in the input pixels
-    if (! pmFPARead(input->fpa, input->fits, input->phu, db)) {
-        psErrorStackPrint(stderr, "Unable to populate camera from input FITS file\n");
-        exit(EXIT_FAILURE);
-    }
-
-    return true;
-}
-
-// XXX this is not very efficient with fseeks : each pmFPARead is randomly accessing the file
-// XXX does this handle multi-file data?
-// XXX this does NOT preserve the state of the input valid flags
Index: /trunk/psphot/src/psphotModelTest.c
===================================================================
--- /trunk/psphot/src/psphotModelTest.c	(revision 6726)
+++ /trunk/psphot/src/psphotModelTest.c	(revision 6727)
@@ -1,7 +1,6 @@
 # include "psphot.h"
-# include "psEllipse.h"
 static char DEFAULT_MODE[] = "EXT";
 
-bool psphotModelTest (pmReadout *readout, recipe) {
+bool psphotModelTest (pmReadout *readout, psMetadata *recipe) {
 
     bool status;
@@ -97,9 +96,9 @@
     fprintf (stderr, "sum: %f @ (%f, %f)\n", source->moments->Sum, source->moments->x, source->moments->y);
 
-    EllipseMoments moments;
+    psEllipseMoments moments;
     moments.x2 = source->moments->Sx;
     moments.y2 = source->moments->Sy;
     moments.xy = source->moments->Sxy;
-    EllipseAxes axes = EllipseMomentsToAxes (moments);
+    psEllipseAxes axes = psEllipseMomentsToAxes (moments);
 
     fprintf (stderr, "axes: %f @ (%f, %f)\n", axes.theta*180/M_PI, axes.major, axes.minor);
@@ -137,9 +136,9 @@
 
     // list model input shape
-    EllipseShape shape;
+    psEllipseShape shape;
     shape.sx  = 1.4 / model->params->data.F32[4];
     shape.sy  = 1.4 / model->params->data.F32[5];
     shape.sxy = model->params->data.F32[6];
-    axes = EllipseShapeToAxes (shape);
+    axes = psEllipseShapeToAxes (shape);
 
     fprintf (stderr, "guess: %f @ (%f, %f)\n", axes.theta*180/M_PI, axes.major, axes.minor);
@@ -152,5 +151,5 @@
 
     // define the pixels used for the fit
-    psImageKeepCircle (source->mask, xObj, yObj, RADIUS, "OR", PSPHOT_MASK_MARKED);
+    psImageKeepCircle (source->mask, xObj, yObj, RADIUS, "OR", PM_SOURCE_MASK_MARKED);
 
     char *fitset = psMetadataLookupStr (&status, recipe, "TEST_FIT_SET");
@@ -163,5 +162,6 @@
 
     // measure the source mags
-    pmSourcePhotometry (&fitMag, &obsMag, model, source->pixels, source->mask);
+    pmSourcePhotometryModel (&fitMag, model);
+    pmSourcePhotometryAper  (&obsMag, model, source->pixels, source->mask);
     fprintf (stderr, "ap: %f, fit: %f, apmifit: %f\n", obsMag, fitMag, obsMag - fitMag);
 
Index: /trunk/psphot/src/psphotOutput.c
===================================================================
--- /trunk/psphot/src/psphotOutput.c	(revision 6726)
+++ /trunk/psphot/src/psphotOutput.c	(revision 6727)
@@ -89,22 +89,22 @@
 }
 
-bool psphotUpdateHeader (psMetadata *header, psMetadata *config) {
+// these values are saved in an output header stub - they are added to either the
+// PHU header (CMP) or the MEF table header (CMF)
+psMetadata *psphotDefineHeader (psMetadata *recipe) {
+
+    psMetadata *header = psMetadataAlloc ();
 
     // write necessary information to output header
-    psMetadataItemTransfer (header, config, "NSTARS");
-    psMetadataItemTransfer (header, config, "ZERO_PT");
-    psMetadataItemTransfer (header, config, "APMIFIT");
-    psMetadataItemTransfer (header, config, "dAPMIFIT");
-    psMetadataItemTransfer (header, config, "SKYBIAS");
-    psMetadataItemTransfer (header, config, "PHOTCODE");
+    psMetadataItemTransfer (header, recipe, "ZERO_PT");
+    psMetadataItemTransfer (header, recipe, "PHOTCODE");
+
+    psMetadataItemTransfer (header, recipe, "APMIFIT");
+    psMetadataItemTransfer (header, recipe, "DAPMIFIT");
+    psMetadataItemTransfer (header, recipe, "NAPMIFIT");
+    psMetadataItemTransfer (header, recipe, "SKYBIAS");
+    psMetadataItemTransfer (header, recipe, "SKYSAT");
     
-    // write necessary information to output header
-    psMetadataItemTransfer (header, config, "NPSFSTAR");
-    psMetadataItemTransfer (header, config, "SKYBIAS");
-    psMetadataItemTransfer (header, config, "SKYSAT");
-    psMetadataItemTransfer (header, config, "APMIFIT");
-    psMetadataItemTransfer (header, config, "DAPMIFIT");
-    psMetadataItemTransfer (header, config, "NAPMIFIT");
-    psMetadataItemTransfer (header, config, "APLOSS");
+    psMetadataItemTransfer (header, recipe, "NPSFSTAR");
+    psMetadataItemTransfer (header, recipe, "APLOSS");
 
     // XXX these need to be defined from elsewhere
@@ -115,4 +115,4 @@
     psMetadataAdd (header, PS_LIST_TAIL, "FLIMIT",   PS_DATA_F32 | PS_META_REPLACE, "COMPLETENESS MAG",    0.0);
 
-    return true;
+    return header;
 }
Index: /trunk/psphot/src/psphotParseCamera.c
===================================================================
--- /trunk/psphot/src/psphotParseCamera.c	(revision 6726)
+++ /trunk/psphot/src/psphotParseCamera.c	(revision 6727)
@@ -62,6 +62,6 @@
 
     pmFPAfileDefine (config->files, format, input, "PSPHOT.OUTPUT");
+    pmFPAfileDefine (config->files, format, input, "PSPHOT.RESID");
 
-    // pmFPAfileDefine (config->files, format, input, "PSPHOT.RESID");
     // pmFPAfileDefine (config->files, format, input, "PSPHOT.PSF_INPUT");
     // pmFPAfileDefine (config->files, format, input, "PSPHOT.PSF_OUTPUT");
@@ -76,6 +76,12 @@
 
     // psphot is supplied with the output name
+    // this needs to be better: supply to all output (WRITE) files?
     char *output = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
+
     file = psMetadataLookupPtr (&status, config->files, "PSPHOT.OUTPUT");
+    if (!status) psAbort (__func__, "missing OUTPUT entry");
+    psMetadataAddStr (file->names, PS_LIST_TAIL, "OUTPUT", 0, "", output);
+
+    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.RESID");
     if (!status) psAbort (__func__, "missing OUTPUT entry");
     psMetadataAddStr (file->names, PS_LIST_TAIL, "OUTPUT", 0, "", output);
@@ -95,4 +101,5 @@
     psMetadataAddStr (recipe, PS_LIST_TAIL, "PHOTCODE", PS_META_NO_REPLACE, "default photcode", "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);
 
     // Chip selection: if we are using a single chip, select it for each FPA
Index: /trunk/psphot/src/psphotReadout.c
===================================================================
--- /trunk/psphot/src/psphotReadout.c	(revision 6726)
+++ /trunk/psphot/src/psphotReadout.c	(revision 6727)
@@ -1,6 +1,5 @@
 # include "psphot.h"
 
-// XXX 2006.02.07 : no leaks!
-// XXX change 'config' to 'recipe' eventually
+// XXX 2006.03.28 : no leaks!
 bool psphotReadout (pmConfig *config, pmFPAview *view) {
 
@@ -15,9 +14,4 @@
     pmReadout  *readout = pmFPAviewThisReadout (view, input->fpa);
 
-    sources = psphotFakeSources();
-    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES",   PS_DATA_ARRAY,   "psphot sources", sources);
-    psFree (sources);
-    return true;
-
     // XXX does this need to invoke I/O?
     pmReadoutSetWeights (readout);
@@ -25,4 +19,7 @@
     // I have a valid mask, now mask in the analysis region of interest
     psphotMaskReadout (readout, recipe); 
+
+    // run a single-model test if desired
+    psphotModelTest (readout, recipe);
 
     // generate a background model (median, smoothed image)
@@ -55,5 +52,5 @@
     psphotReplaceUnfit (sources);
 
-    // find remaining peaks after first source subtraction pass
+    // XXX find remaining peaks after first source subtraction pass
     // psphotFindPeaks ();
 
@@ -67,21 +64,18 @@
     psphotMagnitudes (sources, recipe, psf);
 
-    // update the header with stats results
-    // XXX need to do this conditionally?
-    // XXX psMetadata *header = pmReadoutGetHeader (readout);
-    // XXX psphotUpdateHeader (header, config);
+    // create an output header with stats results
+    psMetadata *header = psphotDefineHeader (recipe);
 
-    // XXX make this an option?
     // replace background in residual image
     psphotSkyReplace (config, view);
 
-    // need to do something with the sources, psf, and sky
-    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES",   PS_DATA_ARRAY,   "psphot sources", sources);
-    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF",       PS_DATA_UNKNOWN, "psphot psf", psf);
-    // XXX : replace? status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SKY.MEAN",  PS_DATA_F32,     "psphot sky mean", sky->sampleMean);
-    // XXX : replace? status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SKY.SIGMA", PS_DATA_F32,     "psphot sky stdev", sky->sampleStdev);
+    // save the results of the analysis 
+    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,    "psphot sources", sources);
+    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.HEADER",  PS_DATA_METADATA, "header stats", header);
+    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF",     PS_DATA_UNKNOWN,  "psphot psf", psf);
 
     // free up the local copies of the data
     psFree (psf);
+    psFree (header);
     psFree (sources);
     psFree (peaks);
@@ -89,31 +83,12 @@
 }
 
-
-// XXX Deprecate or allow these versions?
-# if (0)
-    // select analysis method
-    char *FITMODE = psMetadataLookupStr (&status, config, "FITMODE");
-    if (!strcasecmp(FITMODE, "ENSEMBLE")) {
-	psphotEnsemblePSF (readout, config, sources, psf);
-    }
-
-    if (!strcasecmp(FITMODE, "FULL")) {
-	psphotEnsemblePSF (readout, config, sources, psf);
-	psphotFullFit (readout, config, sources, psf);
-	psphotReplaceUnfit (sources);
-	psphotApResid (readout, sources, config, psf);
-    }
-
-    if (!strcasecmp(FITMODE, "BASIC")) {
-	psphotApplyPSF (readout, config, sources, psf);
-	psphotFitExtended (readout, config, sources);
-    }
-
-
-
-    psphotSaveImage (NULL, readout->image,  "pixels.fits");
-    psphotSaveImage (NULL, readout->weight, "weight.fits");
-    psphotSaveImage (NULL, readout->mask,   "mask.fits");
-    exit (1);
-
-# endif
+    # if (1)
+    fprintf (stderr, "making fake sources\n");
+    sources = psphotFakeSources ();
+    psMetadata *tmp = psMetadataAlloc ();
+    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,    "psphot sources", sources);
+    status = psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.HEADER",  PS_DATA_METADATA, "header stats", tmp);
+    psFree (sources);
+    psFree (tmp);
+    return true;
+    # endif
Index: /trunk/psphot/src/psphotSkyReplace.c
===================================================================
--- /trunk/psphot/src/psphotSkyReplace.c	(revision 6726)
+++ /trunk/psphot/src/psphotSkyReplace.c	(revision 6727)
@@ -1,4 +1,5 @@
 # include "psphot.h"
 
+// XXX make this an option?
 // in order to  successfully replace the sky, we must define a corresponding file...
 bool psphotSkyReplace (pmConfig *config, pmFPAview *view) {
