/* 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
} pmLumFunc;

bool              psastroDataSave (pmConfig *config);
bool              psastroDefineFiles (pmConfig *config, pmFPAfile *input);
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              psastroChooseRefstars (pmConfig *config, psArray *refs);
bool              psastroRefstarSubset (pmReadout *readout);
pmLumFunc        *psastroLuminosityFunction (psArray *stars);

// 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);
bool 		  psastroPlotRefstars (psArray *refstars);
bool 		  psastroPlotOneChipFit (psArray *rawstars, psArray *refstars, psArray *match, pmAstromFitResults *results);

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		  psastroMosaicSetAstrom_tmp (pmFPA *fpa);
int 		  psastroSortByMag (const void **a, const void **b);

# endif /* PSASTRO_H */
