#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

# include "psphot.h"

bool psphotReplaceUnfit (psArray *sources) { 

    pmSource *source;

    psTimerStart ("psphot");

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

	if (!(source->mode & PM_SOURCE_MODE_TEMPSUB)) continue;
	if (source->modelPSF == NULL) continue;

	psTrace (__func__, 3, "replacing object at %f,%f\n", 
		 source->modelPSF->params->data.F32[2], source->modelPSF->params->data.F32[3]);

	pmModelAdd (source->pixels, source->mask, source->modelPSF, false, false);
	source->mode &= ~PM_SOURCE_MODE_SUBTRACTED;
	source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
    }
    psLogMsg ("psphot.replace", 3, "replace unfitted models: %f sec (%ld objects)\n", psTimerMark ("psphot"), sources->n);
    return true;
}
