/* This file defines the library functions available to external programs.  It must be included
 * by programs which are compiled against psphot functions.
 */

#ifndef PSPHOT_H
#define PSPHOT_H

#include <psmodules.h>

#include "psphotErrorCodes.h"
#define PSPHOT_RECIPE "PSPHOT" // Name of the recipe to use

// top-level psphot functions
const char     *psphotCVSName(void);
psString        psphotVersion(void);
psString        psphotVersionLong(void);

bool            psphotModelTest (pmReadout *readout, psMetadata *recipe);
bool            psphotReadout (pmConfig *config, pmFPAview *view);
bool            psphotReadoutCleanup (pmConfig *config, pmReadout *readout, psMetadata *recipe, pmPSF *psf, psArray *sources);
bool            psphotDefineFiles (pmConfig *config, pmFPAfile *input);


// XXX test functions
psArray        *psphotFakeSources ();

// psphotReadout functions
bool            psphotImageMedian (pmConfig *config, pmFPAview *view);
psArray        *psphotFindPeaks (pmReadout *readout, psMetadata *recipe,
                                 const bool returnFootprints, const int pass);
#include "pmFootprint.h"
psErrorCode	psphotCullPeaks(const pmReadout *readout, const psMetadata *recipe, psArray *footprints);
psArray        *psphotSourceStats (pmReadout *readout, psMetadata *recipe, psArray *allpeaks);
bool            psphotRoughClass (psArray *sources, psMetadata *recipe);
bool            psphotBasicDeblend (psArray *sources, psMetadata *recipe);
pmPSF          *psphotChoosePSF (pmReadout *readout, psArray *sources, psMetadata *recipe);
bool            psphotPSFstats (pmReadout *readout, psMetadata *recipe, pmPSF *psf);
bool            psphotMomentsStats (pmReadout *readout, psMetadata *recipe, psArray *sources);
#if NOT_IN_LIBPSPHOT
bool            psphotEnsemblePSF (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf, bool final);
#endif
bool            psphotFitSourcesLinear (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf, bool final);
bool            psphotGuessModels (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
bool            psphotBlendFit (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
bool            psphotReplaceUnfit (psArray *sources);
bool            psphotReplaceAll (psArray *sources);
bool            psphotApResid (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
bool            psphotMagnitudes (psArray *sources, psMetadata *recipe, pmPSF *psf, pmReadout *background);
bool            psphotSkyReplace (pmConfig *config, pmFPAview *view);

// basic support functions
void            psphotModelGroupInit (void);
int             pmPeakSortBySN (const void **a, const void **b);
int             pmPeakSortByY (const void **a, const void **b);
int             pmSourceSortBySN (const void **a, const void **b);
int             pmSourceSortByY (const void **a, const void **b);
bool            psphotGrowthCurve (pmReadout *readout, pmPSF *psf, bool ignore);
bool            psphotMaskReadout (pmReadout *readout, psMetadata *recipe);
void            psphotSourceFreePixels (psArray *sources);

// functions to set the correct source pixels
bool            psphotInitRadiusPSF (const psMetadata *recipe, const pmModelType type);
bool            psphotCheckRadiusPSF (pmReadout *readout, pmSource *source, pmModel *model);
bool            psphotCheckRadiusPSFBlend (pmReadout *readout, pmSource *source, pmModel *model, float dR);
bool            psphotInitRadiusEXT (psMetadata *recipe, pmModelType type);
bool            psphotCheckRadiusEXT (pmReadout *readout, pmSource *source, pmModel *model);

// output functions
bool            psphotAddPhotcode (psMetadata *recipe, pmConfig *config, pmFPAview *view);
bool            psphotDumpMoments (psMetadata *recipe, psArray *sources);
psMetadata     *psphotDefineHeader (psMetadata *recipe);
int             psphotSaveImage (psMetadata *header, psImage *image, char *filename);
bool            psphotDumpConfig (pmConfig *config);
pmReadout      *psphotSelectBackground (pmConfig *config, pmFPAview *view, const bool stdev);

// PSF / DBL / EXT evaluation functions
bool            psphotEvalPSF (pmSource *source, pmModel *model);
bool            psphotEvalDBL (pmSource *source, pmModel *model);
bool            psphotEvalEXT (pmSource *source, pmModel *model);

//  functions to support the source fitting process
bool            psphotInitLimitsPSF (psMetadata *recipe, pmReadout *readout);
bool            psphotInitLimitsEXT (psMetadata *recipe);
bool            psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf);
bool            psphotFitBlob (pmReadout *readout, pmSource *source, psArray *sources, pmPSF *psf);
bool            psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf);
pmModel        *psphotFitEXT (pmReadout *readout, pmSource *source);
psArray        *psphotFitDBL (pmReadout *readout, pmSource *source);

// functions to support simultaneous multi-source fitting
bool            psphotFitSet (pmSource *oneSrc, pmModel *oneModel, char *fitset, pmSourceFitMode mode);

// plotting functions (available if libkapa is installed)
bool 		psphotPlotMoments (pmConfig *config, pmFPAview *view, psArray *sources);
bool 		psphotPlotPSFModel (pmConfig *config, pmFPAview *view, psArray *sources);
bool 		psphotFitInit ();
bool 		psphotFitSummary ();
bool 		psphotMergeSources (psArray *oldSources, psArray *newSources);
bool 		psphotLoadExtSources (pmConfig *config, pmFPAview *view, psArray *sources);
pmPSF 	       *psphotLoadPSF (pmConfig *config, pmFPAview *view, psMetadata *recipe);
bool 		psphotSetHeaderNstars (psMetadata *recipe, psArray *sources);
bool 		psphotAddNoise (pmReadout *readout, psArray *sources, psMetadata *recipe, bool add);
bool 		psphotRadialPlot (int *kapa, const char *filename, pmSource *source);
bool 		psphotSourcePlots (pmReadout *readout, psArray *sources, psMetadata *recipe);
bool 		psphotMosaicSubimage (psImage *outImage, pmSource *source, int Xo, int Yo, int DX, int DY);

bool 		psphotAddWithTest (pmSource *source, bool useState);
bool 		psphotSubWithTest (pmSource *source, bool useState);
bool 		psphotSetState (pmSource *source, bool curState);
bool 		psphotDeblendSatstars (psArray *sources, psMetadata *recipe);
bool            psphotSourceSize (pmReadout *readout, psArray *sources, psMetadata *recipe);

bool            psphotMakeResiduals (psArray *sources, psMetadata *recipe, pmPSF *psf);

#endif
