/** @file psastro.h
 *
 *  @brief This file defines the library functions available to external
 *  programs.
 *
 *  It must be included by programs which are compiled against
 *  psphot functions.
 *
 *  @ingroup psastro
 *
 *  @author IfA
 *  @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-09 21:25:34 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

# ifndef PSASTRO_H
# define PSASTRO_H

# include "psastroErrorCodes.h"

/// @addtogroup psastro
/// @{

# 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)))

#if 0
/**
 * 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;
#endif

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);
bool              psastroAstromGuessCheck (pmConfig *config);
bool              psastroMaskUpdates (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, pmLumFunc *rawFunc);
bool              psastroLuminosityFunctionPlot(psVector *lnMag, psVector *Mag, pmLumFunc *lumFunc, pmLumFunc *rawFunc);
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              psastroMosaicDistortion (pmFPA *fpa, psMetadata *recipe, int pass);
psPlaneTransform *psastroMosaicFitRotAndScale (pmFPA *fpa);
bool              psastroMosaicApplyRotAndScale (pmFPA *fpa, psPlaneTransform *TPtoFP);
bool              psastroMosaicDistortionFromGradients (pmFPA *fpa, psMetadata *recipe);
bool              psastroMosaicCorrectDistortion (pmFPA *fpa, psPlaneTransform *TPtoFP);
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          psastroSource(void);
psString          psastroVersionLong(void);
bool              psastroVersionHeader(psMetadata *header);
bool              psastroVersionHeaderFull(psMetadata *header);
void              psastroVersionPrint(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 *filenameU, char *filenameD, 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);

bool psastroZeroPoint (pmConfig *config);
bool psastroZeroPointReadout(pmReadout *readout, float zeropt, float exptime);
bool psastroZeroPointFromRecipe (float *zeropt, float *exptime, pmFPA *fpa, psMetadata *recipe);

///@}
# endif /* PSASTRO_H */
