# include "psphotInternal.h"

// load an externally supplied psf model
pmPSF *psphotLoadPSF (pmConfig *config, const pmFPAview *view, psMetadata *recipe) {

    // find the currently selected chip
    pmChip *chip = pmFPAfileThisChip (config->files, view, "PSPHOT.PSF.LOAD");
    if (!chip) return NULL;

    // find the currently selected readout
    pmReadout *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.PSF.LOAD");
    if (!readout) return NULL;

    // check if a PSF model is supplied by the user
    pmPSF *psf = psMetadataLookupPtr (NULL, chip->analysis, "PSPHOT.PSF");
    if (psf == NULL) {
        psLogMsg ("psphot", 3, "no psf supplied for this chip");
        return NULL;
    }

    if (!psphotPSFstats (readout, recipe, psf)) psAbort("cannot measure PSF shape terms");

    psLogMsg ("psphot", 3, "using externally supplied PSF model for this readout");

    // we return a psf which can be free (and which may be removed from the metadata)
    psMemIncrRefCounter (psf);
    return psf;
}
