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

# include "psphot.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;
    }

    // did the model fit fail for one or another reason?
    switch (model->status) {
      case PM_MODEL_SUCCESS:
	break;
      case PM_MODEL_UNTRIED:
	source->mode &= ~PM_SOURCE_MODE_FITTED; 
	return false;
      case PM_MODEL_BADARGS:
      case PM_MODEL_NONCONVERGE:
      case PM_MODEL_OFFIMAGE:
      default:
	psLogMsg ("psphot", 5, "EXT fail fit\n");
	source->mode |= PM_SOURCE_MODE_FAIL;
	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 = pmModelFitStatus (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;
}	
