Index: /trunk/psphot/Makefile
===================================================================
--- /trunk/psphot/Makefile	(revision 5621)
+++ /trunk/psphot/Makefile	(revision 5622)
@@ -56,24 +56,23 @@
 $(SRC)/pmModelGroup.$(ARCH).o: $(MODELS)
 
-FITSOURCE = \
-$(SRC)/fitsource.$(ARCH).o \
-$(SRC)/onesource.$(ARCH).o \
-$(SRC)/fs_args.$(ARCH).o \
-$(SRC)/psphot-utils.$(ARCH).o \
-$(SRC)/psPolynomials.$(ARCH).o \
-$(SRC)/psUtils.$(ARCH).o \
-$(SRC)/setup.$(ARCH).o \
-$(SRC)/LocalSky.$(ARCH).o
+MODELTEST = \
+$(SRC)/modelTestFitSource.$(ARCH).o \
+$(SRC)/modelTestArguments.$(ARCH).o \
+$(SRC)/psModulesUtils.$(ARCH).o	    \
+$(SRC)/psImageData.$(ARCH).o        \
+$(SRC)/psphotSetup.$(ARCH).o	    \
+$(SRC)/psphotDefinePixels.$(ARCH).o \
+$(SRC)/psphotModelTest.$(ARCH).o
 
 psphot: $(BIN)/psphot.$(ARCH)
-# $(BIN)/psphot.$(ARCH) : $(PSPHOT) $(PSMODULES)
 $(BIN)/psphot.$(ARCH) : $(PSPHOT)
 $(PSPHOT) $(PSMODULES): $(SRC)/psphot.h
 
-fitsource: $(BIN)/fitsource.$(ARCH)
-$(BIN)/fitsource.$(ARCH) : $(FITSOURCE)
-$(FITSOURCE): $(SRC)/psphot.h
+modeltest.install: psphotModelTest.install
+modeltest: $(BIN)/psphotModelTest.$(ARCH)
+$(BIN)/psphotModelTest.$(ARCH) : $(MODELTEST)
+$(MODELTEST): $(SRC)/psphot.h
 
-INSTALL = psphot
+INSTALL = psphot psphotModelTest
 
 # dependancy rules for binary code #########################
Index: /trunk/psphot/src/modelTestArguments.c
===================================================================
--- /trunk/psphot/src/modelTestArguments.c	(revision 5622)
+++ /trunk/psphot/src/modelTestArguments.c	(revision 5622)
@@ -0,0 +1,73 @@
+# include "psphot.h"
+static int usage ();
+
+psMetadata *modelTestArguments (int *argc, char **argv) {
+
+  int N;
+  unsigned int Nfail;
+  int mode = PS_DATA_STRING | PS_META_REPLACE;
+  psF32 UseCoords_X, UseCoords_Y;
+
+  // basic pslib options
+  psLogSetFormat ("M");
+  psArgumentVerbosity (argc, argv);
+
+  // identify options in args list - these go on config 
+  bool UseCoords = false;
+  if ((N = psArgumentGet (*argc, argv, "-coords"))) {
+    UseCoords = true;
+    psArgumentRemove (N, argc, argv);
+    UseCoords_X = atof (argv[N]);
+    psArgumentRemove (N, argc, argv);
+    UseCoords_Y = atof (argv[N]);
+    psArgumentRemove (N, argc, argv);
+  }
+
+  char *model = NULL;
+  if ((N = psArgumentGet (*argc, argv, "-model"))) {
+    psArgumentRemove (N, argc, argv);
+    model = psStringCopy (argv[N]);
+    psArgumentRemove (N, argc, argv);
+  }
+
+  char *fitmode = NULL;
+  if ((N = psArgumentGet (*argc, argv, "-fitmode"))) {
+    psArgumentRemove (N, argc, argv);
+    fitmode = psStringCopy (argv[N]);
+    psArgumentRemove (N, argc, argv);
+  }
+
+  if (*argc != 3) usage ();
+
+  // load config information
+  psMetadata *config = psMetadataAlloc ();
+  psMetadataAdd (config, PS_LIST_HEAD, "PSF_MODEL", PS_DATA_METADATA_MULTI, "folder for psf model entries", NULL);
+  config = psMetadataConfigParse (config, &Nfail, argv[2], FALSE);
+
+  // identify input image
+  psMetadataAdd (config, PS_LIST_HEAD, "IMAGE", mode, "", argv[1]);
+
+  if (model != NULL) {
+    psMetadataAdd (config, PS_LIST_HEAD, "TEST_FIT_MODEL", mode, "", model);
+  }
+  if (fitmode != NULL) {
+    psMetadataAdd (config, PS_LIST_HEAD, "TEST_FIT_MODE", mode, "", fitmode);
+  }
+  if (UseCoords) {
+    mode = PS_DATA_F32 | PS_META_REPLACE;
+    psMetadataAdd (config, PS_LIST_HEAD, "TEST_FIT_X", mode, "", UseCoords_X);
+    psMetadataAdd (config, PS_LIST_HEAD, "TEST_FIT_Y", mode, "", UseCoords_Y);
+  }
+
+  return (config);
+}
+
+static int usage () {
+
+    fprintf (stderr, "USAGE: psphotModelTest (image.fits) (config)\n");
+    fprintf (stderr, "options: \n");
+    fprintf (stderr, "  -coords x y   : specify object center\n");
+    fprintf (stderr, "  -model (name) : specify model of interest\n");
+    fprintf (stderr, "  -fitmode (flt|psf) : specify how to fit model\n");
+    exit (2);
+}
Index: /trunk/psphot/src/modelTestFitSource.c
===================================================================
--- /trunk/psphot/src/modelTestFitSource.c	(revision 5622)
+++ /trunk/psphot/src/modelTestFitSource.c	(revision 5622)
@@ -0,0 +1,116 @@
+# include "psphot.h"
+
+int DumpImage (psImage *image, char *filename);
+
+bool modelTestFitSource (eamReadout *imdata, psMetadata *config) {
+
+    bool status;
+    int modelType;
+    float sky, obsMag, fitMag, value;
+    char name[64];
+
+    psMetadataItem *item  = NULL;
+
+    // find the model: supplied by user or first in the PSF_MODEL list
+    char *modelName  = psMetadataLookupPtr (&status, config, "TEST_FIT_MODEL");
+    if (modelName == NULL) {
+	// get the list pointers for the PSF_MODEL entries
+	psMetadataItem *mdi = psMetadataLookup (config, "PSF_MODEL");
+	if (mdi == NULL) psAbort ("psphotChoosePSF", "missing PSF_MODEL selection");
+
+	// take the first list element
+	psList *list = (psList *) mdi->data.list;
+	item = psListGet (list, PS_LIST_HEAD);
+	modelName = item->data.V;
+    }
+    modelType = pmModelSetType (modelName);
+    if (modelType < 0) psAbort ("fitsource", "unknown model %s", modelName);
+
+    // find the fitting parameters (try test values first)
+    float INNER = psMetadataLookupF32 (&status, config, "TEST_FIT_INNER_RADIUS");
+    if (!status) {
+	INNER = psMetadataLookupF32 (&status, config, "SKY_INNER_RADIUS");
+    }
+
+    float OUTER = psMetadataLookupF32 (&status, config, "TEST_FIT_OUTER_RADIUS");
+    if (!status) {
+	OUTER = psMetadataLookupF32 (&status, config, "SKY_OUTER_RADIUS");
+    }
+
+    float RADIUS = psMetadataLookupF32 (&status, config, "TEST_FIT_RADIUS");
+    if (!status) {
+	RADIUS = psMetadataLookupF32 (&status, config, "PSF_FIT_RADIUS");
+    }
+
+    float mRADIUS = psMetadataLookupF32 (&status, config, "TEST_MOMENTS_RADIUS");
+    if (!status) {
+	mRADIUS = psMetadataLookupF32 (&status, config, "PSF_MOMENTS_RADIUS");
+    }
+
+    // define the source of interest
+    float xObj     = psMetadataLookupF32 (&status, config, "TEST_FIT_X");
+    float yObj     = psMetadataLookupF32 (&status, config, "TEST_FIT_Y");
+
+    // construct the source structures
+    pmSource *source = pmSourceAlloc();
+    source->peak = pmPeakAlloc (xObj, yObj, 0, 0);
+    psphotDefinePixels (source, imdata, xObj, yObj, OUTER);
+    DumpImage (source->pixels, "object.fits");
+
+    // find the local sky
+    status = pmSourceLocalSky (source, PS_STAT_SAMPLE_MEDIAN, INNER);
+    if (!status) psAbort ("fitsource", "pmSourceLocalSky error");
+
+    // get the source moments
+    status = pmSourceMoments (source, mRADIUS);
+    if (!status) psAbort ("fitsource", "pmSourceLocalSky error");
+    source->peak->counts = source->moments->Peak;
+
+    // get the initial model parameter guess
+    pmModel *model = pmSourceModelGuess (source, modelType);
+    // if any parameters are defined, use those values
+    int nParams = pmModelParameterCount (modelType);
+    psF32 *params = model->params->data.F32;
+    for (int i = 0; i < nParams; i++) {
+	sprintf (name, "TEST_FIT_PAR%d", i);
+	value = psMetadataLookupF32 (&status, config, name);
+	if (status) {
+	    params[i] = value;
+	}
+    }
+
+    // what fitting mode to use?
+    char *fitModeWord = psMetadataLookupPtr (&status, config, "TEST_FIT_MODE");
+    if (!status) {
+	fitModeWord = psStringCopy ("FLT");
+    }
+    bool fitMode = !strcasecmp (fitModeWord, "PSF");
+
+    // define the pixels used for the fit
+    psImageKeepCircle (source->mask, xObj, yObj, RADIUS, "OR", PSPHOT_MASK_MARKED);
+    status = pmSourceFitModel (source, model, fitMode);
+
+    // measure the source mags
+    pmSourcePhotometry (&fitMag, &obsMag, model, source->pixels, source->mask);
+    fprintf (stderr, "ap: %f, fit: %f, apmifit: %f\n", obsMag, fitMag, obsMag - fitMag);
+
+    // subtract object, leave local sky
+    sky = model->params->data.F32[0];
+    model->params->data.F32[0] = 0;
+    pmSourceSubModel (source->pixels, source->mask, model, false);
+    model->params->data.F32[0] = sky;
+    
+    // write out 
+    DumpImage (source->pixels, "resid.fits");
+    DumpImage (source->mask, "mask.fits");
+    return true;
+}
+
+int DumpImage (psImage *image, char *filename) {
+
+    unlink (filename);
+    psFits *fits = psFitsAlloc (filename);
+    psFitsWriteImage (fits, NULL, image, 0);
+    psFree (fits);
+    return (TRUE);
+}
Index: /trunk/psphot/src/psphot.h
===================================================================
--- /trunk/psphot/src/psphot.h	(revision 5621)
+++ /trunk/psphot/src/psphot.h	(revision 5622)
@@ -54,2 +54,6 @@
 
 int  	     pmSourcesDophotType (pmSource *source);
+
+// psphotModelTest functions
+psMetadata *modelTestArguments (int *argc, char **argv);
+bool modelTestFitSource (eamReadout *imdata, psMetadata *config);
Index: /trunk/psphot/src/psphotModelTest.c
===================================================================
--- /trunk/psphot/src/psphotModelTest.c	(revision 5622)
+++ /trunk/psphot/src/psphotModelTest.c	(revision 5622)
@@ -0,0 +1,11 @@
+# include "psphot.h"
+
+int main (int argc, char **argv) {
+
+    psMetadata *config = modelTestArguments (&argc, argv);
+    eamReadout *imdata = psphotSetup (config);
+
+    modelTestFitSource (imdata, config);
+
+    exit (0);
+}
Index: /trunk/psphot/src/psphotSetup.c
===================================================================
--- /trunk/psphot/src/psphotSetup.c	(revision 5621)
+++ /trunk/psphot/src/psphotSetup.c	(revision 5622)
@@ -26,4 +26,5 @@
     // read header and image data from INPUT 
     char *input = psMetadataLookupPtr (&status, config, "IMAGE");
+    if (!status) psAbort ("psphot", "input image not specified");
     psFits *file = psFitsAlloc (input);
     header = psFitsReadHeader (header, file);
