Index: /trunk/psModules/src/objects/pmModelUtils.c
===================================================================
--- /trunk/psModules/src/objects/pmModelUtils.c	(revision 14530)
+++ /trunk/psModules/src/objects/pmModelUtils.c	(revision 14530)
@@ -0,0 +1,94 @@
+/** @file  pmModelUtils.c
+ *
+ *  Functions to manipulate object models
+ *
+ *  @author EAM, IfA
+ *
+ *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-08-16 18:33:37 $
+ *
+ *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include <pslib.h>
+#include "pmResiduals.h"
+#include "pmModel.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);
+
+    // 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)) {
+        psError(PM_ERR_PSF, false, "Failed to set model params from PSF");
+        psFree(modelPSF);
+        return NULL;
+    }
+    // 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) {
+
+    pmSource *source = pmSourceAlloc ();
+
+    // 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");
+    }
+
+    source->peak = pmPeakAlloc ();
+
+    float x = model->params->data.F32[PM_PAR_XPOS];
+    float y = model->params->data.F32[PM_PAR_YPOS];
+
+    // 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);
+
+    return (source);
+}
