# include "psphotInternal.h"

// construct an initial PSF model for each object 
bool psphotResidPSF (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf) {

  psTimerStart ("psphot");

  for (int i = 0; i < sources->n; i++) {
    pmSource *source = sources->data[i];

    // only use the psf stars to build the residual model
    if (!(source->mode & PM_SOURCE_MODE_PSFSTAR)) continue;

    if (source->mode & PM_SOURCE_MODE_SATSTAR) {
	Xo = source->moments->x;
	Yo = source->moments->y;
	Io = source->peak->flux;
    } else {
	Xo = source->peak->xf;
	Yo = source->peak->yf;
	Io = source->peak->flux;
    }

    // set PSF parameters for this model (apply 2D shape model)
    pmModel *modelPSF = pmModelFromPSFforXY (psf, Xo, Yo, Io);

    // XXX need to define the guess flux?
    // set the fit radius based on the object flux limit and the model
    psphotCheckRadiusPSF (readout, source, modelPSF);

    // set the source PSF model
    source->modelPSF = modelPSF;
  }
  psLogMsg ("psphot.models", 4, "built models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot"));
  return true;
}

// XXX do we always know which model is supposed to be used?  
