# include "psphotInternal.h"

// in this psphotReadout-variant, we are only trying to determine the PSF given an existing set
// of input source positions to use as initial PSF stars.
bool psphotReadoutFindPSF(pmConfig *config, const pmFPAview *view, psArray *inSources) {

    psTimerStart ("psphotReadout");

    // select the current recipe
    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
    if (!recipe) {
        psError(PSPHOT_ERR_CONFIG, false, "missing recipe %s", PSPHOT_RECIPE);
        return false;
    }

    // set the photcode for this image
    if (!psphotAddPhotcode(recipe, config, view, "PSPHOT.INPUT")) {
        psError(PSPHOT_ERR_CONFIG, false, "trouble defining the photcode");
        return false;
    }

    // find the currently selected readout
    pmReadout  *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT");
    PS_ASSERT_PTR_NON_NULL (readout, false);

    // Generate the mask and variance images, including the user-defined analysis region of interest
    psphotSetMaskAndVariance (config, readout, recipe);

    // display the image, weight, mask (ch 1,2,3)
    psphotVisualShowImage (readout);

    // Note that in this implementation, we do NOT model the background and we do not
    // attempt to detect the sources in the image

    // include externally-supplied sources (supplied as PSPHOT.INPUT.CMF)
    pmDetections *detections = psphotDetectionsFromSources (config, inSources);
    if (!detections || !detections->peaks) {
        psError(PSPHOT_ERR_ARGUMENTS, true, "Can't find PSF stars");
        return psphotReadoutCleanup(config, readout, recipe, detections, NULL, NULL);
    }

    // construct sources and measure basic stats (moments, local sky)
    psArray *sources = psphotSourceStats(config, readout, detections, true);
    if (!sources) return false;

    // peak flux is wrong : set based on previous image
    // use the peak measured in the moments analysis:
    for (int i = 0; i < sources->n; i++) {
      pmSource *source = sources->data[i];
      source->peak->flux = source->moments->Peak;
    }

    // classify sources based on moments, brightness (psf is not known)
    if (!psphotRoughClass (readout, sources, recipe, false)) {
        psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
        return psphotReadoutCleanup (config, readout, recipe, detections, NULL, sources);
    }

    pmPSF *psf = psphotChoosePSF(readout, sources, recipe);
    if (!psf) {
        psError(PSPHOT_ERR_PSF, false, "Failed to construct a psf model");
        psFree(sources);
        return psphotReadoutCleanup(config, readout, recipe, detections, NULL, NULL);
    }
    psphotVisualShowPSFModel(readout, psf);

    // drop the references to the image pixels held by each source
    psphotSourceFreePixels(sources);
    psFree(sources);

    // create the exported-metadata and free local data
    return psphotReadoutCleanup(config, readout, recipe, detections, psf, NULL);
}
