# include "psphotInternal.h"

bool psphotEvalEXT (pmSource *source, pmModel *model)
{ 
    int keep;

    // do we actually have a valid EXT model?
    if (model == NULL) {
	source->mode &= ~PM_SOURCE_MODE_FITTED;
	return false;
    }

    // was the model actually fitted?
    if (!(model->flags & PM_MODEL_STATUS_FITTED)) {
	source->mode &= ~PM_SOURCE_MODE_FITTED; 
	return false;
    }
    // did the model fit fail for one or another reason?
    if (model->flags & (PM_MODEL_STATUS_BADARGS | 
			PM_MODEL_STATUS_NONCONVERGE | 
			PM_MODEL_STATUS_OFFIMAGE)) {
	source->mode |= PM_SOURCE_MODE_FAIL;
	psLogMsg ("psphot", 5, "EXT fail fit\n");
	return false;
    }

    // unless we prove otherwise, this object is extended
    source->type = PM_SOURCE_TYPE_EXTENDED;

    // the following source->mode information pertains to modelEXT:
    source->mode |= PM_SOURCE_MODE_EXTMODEL;

    // if the object has a fitted peak below 0, the fit did not converge cleanly
    if (model->params->data.F32[1] <= 0) {
	source->mode |= PM_SOURCE_MODE_FAIL;
	return false;
    } 

    keep = model->modelFitStatus(model);
    if (keep) return true;

    // poor-quality fit; only keep if nothing else works...
    psLogMsg ("psphot", 5, "EXT poor fit\n");
    source->mode |= PM_SOURCE_MODE_POOR;
    return false;
}	
