/* 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 (pmConfig *config, const pmFPAview *view, psMetadata *recipe, psMaskType maskVal, psMaskType mark);
bool            psphotReadout (pmConfig *config, const 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            psphotModelBackground (pmConfig *config, const pmFPAview *view, const char *filename, psMaskType maskVal);
bool            psphotSubtractBackground (pmConfig *config, const pmFPAview *view, const char *filename, psMaskType maskVal) ;
psArray        *psphotFindPeaks (pmReadout *readout, psMetadata *recipe,
                                 const bool returnFootprints, const int pass, psMaskType maskVal);
#include "pmFootprint.h"
psErrorCode     psphotCullPeaks(const psImage *img, const psImage *weight,
                                const psMetadata *recipe, psArray *footprints);
psArray        *psphotSourceStats (pmReadout *readout, psMetadata *recipe, psArray *allpeaks, psMaskType maskVal, psMaskType mark);
bool            psphotRoughClass (psArray *sources, psMetadata *recipe, const bool findPsfClump, psMaskType maskSat);
bool            psphotBasicDeblend (psArray *sources, psMetadata *recipe);
pmPSF          *psphotChoosePSF (pmReadout *readout, psArray *sources, psMetadata *recipe, psMaskType maskVal, psMaskType mark);
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, psMaskType maskVal);
bool            psphotGuessModels (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf, psMaskType maskVal);
bool            psphotBlendFit (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf, psMaskType maskVal);
bool            psphotReplaceUnfit (psArray *sources, psMaskType maskVal);
bool            psphotReplaceAll (psArray *sources, psMaskType maskVal);
bool            psphotApResid (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf, psMaskType maskVal, psMaskType mark);
bool            psphotMagErrorScale (float *errorScale, float *errorFloor, psVector *dMag, psVector *dap, psVector *mask, int nGroup);
bool            psphotApResidTrend (pmReadout *readout, pmPSF *psf, int Npsf, int scale, float *errorScale, float *errorFloor, psVector *mask, psVector *xPos, psVector *yPos, psVector *apResid, psVector *dMag);
bool            psphotMagnitudes (psArray *sources, psMetadata *recipe, pmPSF *psf, pmReadout *background, psMaskType maskVal, psMaskType mark);
bool            psphotSkyReplace (pmConfig *config, const pmFPAview *view);

// basic support functions
void            psphotModelClassInit (void);
bool            psphotGrowthCurve (pmReadout *readout, pmPSF *psf, bool ignore, psMaskType maskVal);
bool            psphotMaskReadout (pmReadout *readout, psMetadata *recipe, psMaskType maskVal);
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, const 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, const 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, psMaskType maskVal);
bool            psphotFitBlob (pmReadout *readout, pmSource *source, psArray *sources, pmPSF *psf, psMaskType maskVal);
bool            psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf, psMaskType maskVal);
pmModel        *psphotFitEXT (pmReadout *readout, pmSource *source, psMaskType maskVal);
psArray        *psphotFitDBL (pmReadout *readout, pmSource *source, psMaskType maskVal);

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

// 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, const pmFPAview *view, psArray *sources);
pmPSF          *psphotLoadPSF (pmConfig *config, const pmFPAview *view, psMetadata *recipe);
bool            psphotSetHeaderNstars (psMetadata *recipe, psArray *sources);
bool            psphotAddNoise (pmReadout *readout, psArray *sources, psMetadata *recipe, bool add, psMaskType maskVal);
bool            psphotRadialPlot (int *kapa, const char *filename, pmSource *source);
bool            psphotSourcePlots (pmReadout *readout, psArray *sources, psMetadata *recipe, psMaskType maskVal);
bool            psphotMosaicSubimage (psImage *outImage, pmSource *source, int Xo, int Yo, int DX, int DY);

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

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

bool            psphotExtendedSources (pmReadout *readout, psArray *sources, psMetadata *recipe, psMaskType maskVal);
bool            psphotPSFConvModel (pmSource *source, psMetadata *recipe, psMaskType maskVal);
psKernel       *psphotKernelFromPSF (pmSource *source, int nPix);

bool            psphotRadialProfile (pmSource *source, psMetadata *recipe, psMaskType maskVal);
bool 		psphotPetrosian (pmSource *source, psMetadata *recipe, psMaskType maskVal);
bool 		psphotIsophotal (pmSource *source, psMetadata *recipe, psMaskType maskVal);
bool 		psphotAnnuli (pmSource *source, psMetadata *recipe, psMaskType maskVal);
bool 		psphotKron (pmSource *source, psMetadata *recipe, psMaskType maskVal);

// structures & functions to support psf-convolved model fitting

// pmPCMData : PSF Convolved Model data storage structure
typedef struct {
    psImage *model;
    psArray *dmodels;
    psImage *modelConv;
    psArray *dmodelsConv;
} pmPCMData;


// psf-convolved model fitting
bool psphotModelWithPSF_LMM (
    psMinimization *min,
    psImage *covar,
    psVector *params,
    psMinConstraint *constraint,
    pmSource *source,
    const psKernel *psf,
    psMinimizeLMChi2Func func);

psF32 psphotModelWithPSF_SetABX(
    psImage  *alpha,
    psVector *beta,
    const psVector *params,
    const psVector *paramMask,
    pmPCMData *pcm,
    const pmSource *source,
    const psKernel *psf,
    psMinimizeLMChi2Func func);

pmPCMData *pmPCMDataAlloc (
    const psVector *params,
    const psVector *paramMask,
    pmSource *source);

psImage *pmPCMDataSaveImage (pmPCMData *pcm);

int psphotKapaOpen ();
bool psphotKapaClose ();
bool psphotImageBackgroundCellHistogram (psVector *values, float mean, float sigma, int ix, int iy);
bool psphotDiagnosticPlots (pmConfig *config, char *name, ...);

bool psphotMakeFluxScale (psImage *image, psMetadata *recipe, pmPSF *psf);

bool psphotCheckStarDistribution (psArray *sources, psArray *stars, pmPSFOptions *options);
int psphotSupplementStars (psArray *stars, psArray *sources, psImageBinning *binning, int ix, int iy);

#endif
