Index: /branches/eam_branch_20080324/psphot/src/psphotPSFConvModel.c
===================================================================
--- /branches/eam_branch_20080324/psphot/src/psphotPSFConvModel.c	(revision 17315)
+++ /branches/eam_branch_20080324/psphot/src/psphotPSFConvModel.c	(revision 17316)
@@ -4,18 +4,11 @@
 static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
 static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1;
-// static psF32 PM_SOURCE_FIT_MODEL_WEIGHT = 1.0;
-// static bool  PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = true;
 
 // input source has both modelPSF and modelEXT.  on successful exit, we set the
 // modelConv to contain the fitted parameters, and the modelFlux to contain the 
 // convolved model image.
-bool psphotPSFConvModel (pmSource *source, psMetadata *recipe, psMaskType maskVal) {
+pmModel *psphotPSFConvModel (pmReadout *readout, pmSource *source, pmModelType modelType, psMaskType maskVal, int psfSize) {
     
     bool status;
-
-    int psfSize = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
-    if (!status) {
-	psfSize = 2;
-    }
 
     // make sure we save a cached copy of the psf flux
@@ -23,6 +16,4 @@
 
     // convert the cached cached psf model for this source to a psKernel
-    // XXX for the moment, hard-wire the kernel to be 5x5 (2 pix radius)
-    // XXX for the moment, hard-wire the kernel to be 9x9 (4 pix radius)
     psKernel *psf = psphotKernelFromPSF (source, psfSize);
 
@@ -41,5 +32,6 @@
     }
 
-# if (0)
+# define USE_DELTA_PSF 0
+# if (USE_DELTA_PSF)
     // XXX sanity check: convolve with delta function should behave like unconvolved version
     for (int i = 0; i < psf->image->numRows; i++) {
@@ -54,7 +46,16 @@
     // XXX we could modify the parameter values or even the model 
     // here based on the observed seeing (some lookup table...)
-    pmModel *modelConv = pmModelCopy (source->modelEXT);
+
+    // XXX we previously required the unconvolved version to have been fit first
+    // pmModel *modelConv = pmModelCopy (source->modelEXT);
+
+    // use the source moments, etc to guess basic model parameters
+    pmModel *modelConv = pmSourceModelGuess (source, modelType);
+    PS_ASSERT (modelConv, NULL);
+
     psVector *params  = modelConv->params;
     psVector *dparams = modelConv->dparams;
+
+    psphotCheckRadiusEXT (readout, source, modelConv);
 
     // create the minimization constraints
@@ -91,5 +92,5 @@
     psTrace ("psphot", 4, "niter: %d, chisq: %f", myMin->iter, myMin->value);
 
-    // renormalize output model image
+    // renormalize output model image (generated by fitting process)
     float Io = params->data.F32[PM_PAR_I0];
     for (int iy = 0; iy < source->modelFlux->numRows; iy++) {
@@ -115,10 +116,7 @@
     onPic &= (params->data.F32[PM_PAR_YPOS] >= source->pixels->row0);
     onPic &= (params->data.F32[PM_PAR_YPOS] <  source->pixels->row0 + source->pixels->numRows);
-    if (!onPic) {
-        modelConv->flags |= PM_MODEL_STATUS_OFFIMAGE;
-    }
+    if (!onPic) modelConv->flags |= PM_MODEL_STATUS_OFFIMAGE;
 
-    source->mode |= PM_SOURCE_MODE_FITTED;
-    source->modelConv = modelConv;
+    source->mode |= PM_SOURCE_MODE_FITTED; // XXX is this needed?
 
     psFree(myMin);
@@ -126,6 +124,4 @@
     psFree(constraint);
 
-    bool retval = (onPic && fitStatus);
-    psTrace("psphot", 5, "---- %s(%d) end ----\n", __func__, retval);
-    return(retval);
+    return modelConv;
 }
