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

# ifndef PSASTRO_H
# define PSASTRO_H

# include "psastroErrorCodes.h"
# define PSASTRO_RECIPE "PSASTRO" // Name of the recipe to use

# define psMemCopy(A)(psMemIncrRefCounter((A)))
# define DEG_RAD 57.295779513082322
# define RAD_DEG  0.017453292519943
# define SIGN(X)  (((X) == 0) ? 0 : ((fabs((double)(X))) / (X)))

// this structure represents a fit to the logN / logS curve for a set of stars
// logN = offset + slope * logS
typedef struct {
    double mMin;			// minimum magnitude bin with data
    double mMax;			// maximum magnitude bin with data
    double offset;			// fitted line offset
    double slope;			// fitted line slope
    double mPeak;			// mag of peak bin 
    int nPeak;				// # of stars in peak bin
    int sPeak;				// sum of stars to peak bin
} pmLumFunc;

bool              psastroDataSave (pmConfig *config);
bool              psastroDefineFiles (pmConfig *config, pmFPAfile *input);
bool              psastroDefineFile (pmConfig *config, pmFPA *input, char *filerule, char *argname, pmFPAfileType fileType, pmDetrendType detrendType);
bool              psastroAnalysis (pmConfig *config);

bool              psastroConvertFPA (pmFPA *fpa, psMetadata *recipe);
bool              psastroConvertReadout (pmReadout *readout, psMetadata *recipe);
psArray          *pmSourceToAstromObj (psArray *sources);
bool              psastroAstromGuess (int *nStars, pmConfig *config);

psPlaneDistort   *psPlaneDistortIdentity ();
bool              psPlaneDistortIsIdentity (psPlaneDistort *distort);

psArray          *psastroLoadRefstars (pmConfig *config);
bool              psastroChipAstrom (pmConfig *config);
bool              psastroOneChip (pmFPA *fpa, pmChip *chip, psArray *refset, psArray *rawset, psMetadata *recipe, psMetadata *updates);
bool              psastroOneChipGrid (pmFPA *fpa, pmChip *chip, psArray *refset, psArray *rawset, psMetadata *recipe, psMetadata *updates);
bool              psastroOneChipFit (pmFPA *fpa, pmChip *chip, psArray *refset, psArray *rawset, psMetadata *recipe, psMetadata *updates);
bool              psastroChooseRefstars (pmConfig *config, psArray *refs);
bool              psastroRefstarSubset (pmReadout *readout);
pmLumFunc        *psastroLuminosityFunction (psArray *stars);
psArray          *psastroRemoveClumps (psArray *input, int scale);

// utility functions:
bool              psastroUpdateChipToFPA (pmFPA *fpa, pmChip *chip, psArray *rawstars, psArray *refstars);
bool              psastroWriteStars (char *filename, psArray *sources);
bool              psastroWriteTransform (psPlaneTransform *map);

// mosaic fitting functions
bool 		  psastroMosaicGradients (pmFPA *fpa, psMetadata *recipe);
bool 		  psastroMosaicCommonScale (pmFPA *fpa, psMetadata *recipe);
bool 		  psastroMosaicAstrom (pmConfig *config);
bool 		  psastroMosaicChipAstrom (pmFPA *fpa, psMetadata *recipe, int iteration);
bool 		  psastroMosaicSetMatch (pmFPA *fpa, psMetadata *recipe, int iteration);
bool 		  psastroMosaicSetAstrom (pmFPA *fpa);
bool 		  psastroMosaicHeaders (pmConfig *config);
bool 		  psastroMosaicRescaleChips (pmFPA *fpa);
bool 		  psastroMosaicOneChip (pmChip *chip, pmReadout *readout, psMetadata *recipe, psMetadata *updates, int iteration);

// Return version strings.
psString 	  psastroVersion(void);
psString 	  psastroVersionLong(void);

// demo plots
bool 		  psastroPlotRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip, psMetadata *recipe);
bool 		  psastroPlotRefstars (psArray *refstars, psMetadata *recipe);
bool 		  psastroPlotOneChipFit (psArray *rawstars, psArray *refstars, psArray *match, psMetadata *recipe);

bool 		  psastroDumpRawstars (psArray *rawstars, pmFPA *fpa, pmChip *chip);
bool 		  psastroDumpRefstars (psArray *refstars, char *filename);
bool 		  psastroDumpMatches (pmFPA *fpa, char *filename);
bool 		  psastroDumpStars (psArray *stars, char *filename);
bool              psastroDumpMatchedStars (char *filename, psArray *rawstars, psArray *refstars, psArray *match);
bool              psastroDumpGradients (psArray *gradients, char *filename);

bool		  psastroMosaicSetAstrom_tmp (pmFPA *fpa);
int 		  psastroSortByMag (const void *a, const void *b);

bool              psastroFixChips (pmConfig *config, psMetadata *recipe);
bool              psastroFixChipsTest (pmConfig *config, psMetadata *recipe);
bool              psastroUseModel (pmConfig *config, psMetadata *recipe);
bool              psastroDumpCorners (char *filename, pmFPA *fpa);


psArray          *psastroReadGetstarCatalog (psFits *fits);
psArray          *psastroReadGetstar_PS1_DEV_0 (psFits *fits);

bool              psastroAstromGuessSetChip (pmFPA *fpa, pmChip *chip, const pmFPAview *view, double pixelScale, bool bilevelAstrometry);
bool              psastroAstromGuessSetFPA (pmFPA *fpa, bool *bilevelAstrometry);
bool              psastroMetadataStats (pmConfig *config);

# endif /* PSASTRO_H */
