# include "psphotInternal.h"

// 2006.02.02 : no leaks
bool psphotRoughClass (psArray *sources, psMetadata *recipe, const bool findPsfClump) {

    static pmPSFClump   psfClump;
    static bool havePsfClump = false;

    psTimerStart ("psphot");

    if (findPsfClump) {
	psfClump = pmSourcePSFClump (sources, recipe);
    } else if (!havePsfClump) {
	psError(PSPHOT_ERR_PROG, false, "You must find the PSF clump before reusing it");
    } else {
	;
    }
    
    havePsfClump = false;		// we don't know if it's valid
    if (psfClump.X < 0) {
	psError(PSPHOT_ERR_PROG, false, "programming error calling pmSourcePSFClump");
	return false;
    }
    if (!psfClump.X || !psfClump.Y) {
	psError(PSPHOT_ERR_DATA, true, "Failed to find a valid PSF clump");
	return false;
    }
    psLogMsg ("psphot", 3, "psf clump  X,  Y: %f, %f\n", psfClump.X, psfClump.Y);
    psLogMsg ("psphot", 3, "psf clump DX, DY: %f, %f\n", psfClump.dX, psfClump.dY);

    // group into STAR, COSMIC, EXTENDED, SATURATED, etc.
    if (!pmSourceRoughClass (sources, recipe, psfClump)) {
	psError(PSPHOT_ERR_PROG, false, "programming error calling pmSourceRoughClass");
	return false;
    }

    // optional printout of source moments only
    psphotDumpMoments (recipe, sources);

    psLogMsg ("psphot.roughclass", PS_LOG_INFO, "rough classification: %f sec\n", psTimerMark ("psphot"));

    havePsfClump = true;			// we have set psfClump
    
    return true;
}

