Index: trunk/psModules/src/objects/pmModelUtils.c
===================================================================
--- trunk/psModules/src/objects/pmModelUtils.c	(revision 14530)
+++ trunk/psModules/src/objects/pmModelUtils.c	(revision 14652)
@@ -5,6 +5,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-08-16 18:33:37 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-08-24 00:11:02 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -20,38 +20,24 @@
 #include <string.h>
 #include <pslib.h>
+#include "pmHDU.h"
+#include "pmFPA.h"
 #include "pmResiduals.h"
+#include "pmGrowthCurve.h"
+#include "pmPSF.h"
 #include "pmModel.h"
+#include "pmErrorCodes.h"
+#include "pmModelUtils.h"
 
-// instantiate a model for the PSF at this location (normalized or not?)
-// NOTE: psf and (x,y) are defined wrt chip coordinates
-pmModel *pmModelFromPSFforXY (pmPSF *psf, float x, float y, float flux) {
-
-    assert (psf);
-
-    // allocate a new pmModel to hold the PSF version
-    pmModel *modelEXT = pmModelAlloc (psf->type);
-
-    modelEXT->params->data.F32[PM_PAR_SKY]  = 0.0;
-    modelEXT->params->data.F32[PM_PAR_I0]   = 1.0;
-    modelEXT->params->data.F32[PM_PAR_XPOS] = x;
-    modelEXT->params->data.F32[PM_PAR_YPOS] = y;
-
-    // find function used to set the model parameters
-    pmModelFromPSFFunc modelFromPSFFunc = pmModelFromPSFFunc_GetFunction (psf->type);
-
+/*****************************************************************************
+pmModelFromPSF (*modelEXT, *psf):  use the model position parameters to
+construct a realization of the PSF model at the object coordinates
+ *****************************************************************************/
+pmModel *pmModelFromPSF (pmModel *modelEXT, pmPSF *psf)
+{
     // allocate a new pmModel to hold the PSF version
     pmModel *modelPSF = pmModelAlloc (psf->type);
 
-    // adjust the normalization: 
-    pmModelFlux modelFluxFunc = pmModelFlux_GetFunction (model->type);
-    normFlux = modelFluxFunc (model->params);
-    assert (isfinite(normFlux));
-    assert (normFlux > 0);
-
-    // set the correct normalization
-    modelEXT->params->data.F32[PM_PAR_I0] = flux / normFlux;
-
     // set model parameters for this source based on PSF information
-    if (!modelFromPSFFunc (modelPSF, modelEXT, psf)) {
+    if (!modelEXT->modelFromPSF (modelPSF, modelEXT, psf)) {
         psError(PM_ERR_PSF, false, "Failed to set model params from PSF");
         psFree(modelPSF);
@@ -60,35 +46,44 @@
     // XXX note that model->residuals is just a reference
     modelPSF->residuals = psf->residuals;
-    psFree (modelEXT);
 
     return (modelPSF);
 }
 
-pmSource *pmSourceFromModel (pmModel *model, pmReadout *readout, pmSourceType type) {
+// instantiate a model for the PSF at this location with peak flux
+// NOTE: psf and (Xo,Yo) are defined wrt chip coordinates
+pmModel *pmModelFromPSFforXY (pmPSF *psf, float Xo, float Yo, float Io) {
 
-    pmSource *source = pmSourceAlloc ();
+    assert (psf);
 
-    // use the model centroid for the peak
-    switch (type) {
-      case PM_SOURCE_TYPE_STAR:
-	source->modelPSF = model;
-	break;
-      case PM_SOURCE_TYPE_EXTENDED:
-	source->modelEXT = model;
-	break;
-      default:
-	psAbort ("psphot", "error");
+    // allocate a new pmModel to hold the PSF version
+    pmModel *modelPSF = pmModelAlloc (psf->type);
+
+    // set model parameters for this source based on PSF information
+    if (!modelPSF->modelParamsFromPSF (modelPSF, psf, Xo, Yo, Io)) {
+        psError(PM_ERR_PSF, false, "Failed to set model params from PSF");
+        psFree(modelPSF);
+        return NULL;
     }
 
-    source->peak = pmPeakAlloc ();
+    // XXX note that model->residuals is just a reference
+    modelPSF->residuals = psf->residuals;
 
-    float x = model->params->data.F32[PM_PAR_XPOS];
-    float y = model->params->data.F32[PM_PAR_YPOS];
+    return (modelPSF);
+}
 
-    // XXX need to define the radius in some rational way
-    // XXX x,y are defined wrt readout->image parent, but the model
-    // parameters are defined wrt chip coordinates
-    pmSourceDefinePixels (source, readout, x, y, radius);
+// set this model to have the requested flux
+bool pmModelSetFlux (pmModel *model, float flux) {
 
-    return (source);
+    // set Io to be 1.0
+    model->params->data.F32[PM_PAR_I0] = 1.0;
+
+    // determine the normalized flux
+    float normFlux = model->modelFlux (model->params);
+    assert (isfinite(normFlux));
+    assert (normFlux > 0);
+
+    // set the desired normalization
+    model->params->data.F32[PM_PAR_I0] = flux / normFlux;
+
+    return true;
 }
