# include "psphotInternal.h"

bool psphotDetectReadout(pmConfig *config, const pmFPAview *view) {

    // measure the total elapsed time in psphotReadout.  XXX the current threading plan
    // for psphot envisions threading within psphotReadout, not multiple threads calling
    // the same psphotReadout.  In the current plan, this dtime is the elapsed time used
    // jointly by the multiple threads, not the total time used by all threads.
    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;
    }

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

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

    // load the psf model, if suppled.  FWHM_X,FWHM_Y,etc are saved in the recipe
    pmPSF *psf = psphotLoadPSF (config, view, recipe);
    psAssert (psf, "psf should be loaded");

    // grab the sources of interest from the storage location (pmFPAfile PSPHOT.INPUT.CMF)
    psArray *sources = psphotLoadPSFSources (config, view);
    if (!sources) {
      psError(PS_ERR_UNKNOWN, false, "No sources supplied to measure PSF");
      return false;
    }

    // include externally-supplied sources (supplied as PSPHOT.INPUT.CMF)
    psphotSetSourceParams (config, sources, psf);

    // calculate source magnitudes
    psphotPSFWeights(config, readout, view, sources);

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

// deep in pmSourcePixelWeight, we need the following items for each location of interest:
// pmModel *model (should be a realized version of the PSF, can have unity normalization)
// psImage *mask  (local subimage of mask for source)
// psImageMaskType maskVal (from recipe & mask header)

{ 
    pmModel *model = pmModelFromPSFforXY (psf, x, y, 1.0);
}

