#ifndef PM_SUBTRACTION_KERNELS_H
#define PM_SUBTRACTION_KERNELS_H

#include <pslib.h>

/// Type of subtraction kernel
typedef enum {
    PM_SUBTRACTION_KERNEL_NONE,         ///< Nothing --- an error
    PM_SUBTRACTION_KERNEL_POIS,         ///< Pan-STARRS Optimal Image Subtraction --- delta functions
    PM_SUBTRACTION_KERNEL_ISIS,         ///< Traditional kernel --- gaussians modified by polynomials
    PM_SUBTRACTION_KERNEL_SPAM,         ///< Summed Pixels for Advanced Matching --- summed delta functions
    PM_SUBTRACTION_KERNEL_FRIES,        ///< Fibonacci Radius Increases Excellence of Subtraction
    PM_SUBTRACTION_KERNEL_GUNK,         ///< Grid United with Normal Kernel --- POIS and ISIS hybrid
    PM_SUBTRACTION_KERNEL_RINGS,        ///< Rings Instead of the Normal Gaussian Subtraction
} pmSubtractionKernelsType;

/// Kernels specification
typedef struct {
    pmSubtractionKernelsType type;      ///< Type of kernels --- allowing the use of multiple kernels
    psString description;               ///< Description of the kernel parameters
    long num;                           ///< Number of kernel components (not including the spatial ones)
    psVector *u, *v;                    ///< Offset (for POIS) or polynomial order (for ISIS)
    psVector *widths;                   ///< Gaussian FWHMs (ISIS) or ring radius (RINGS)
    psVector *uStop, *vStop;            ///< Width of kernel element (SPAM,FRIES only)
    int subIndex;                       ///< Index of kernel to be subtracted (to maintain flux conservation)
    psArray *preCalc;                   ///< Array of images containing pre-calculated kernel (for ISIS)
    int size;                           ///< The half-size of the kernel
    int inner;                          ///< The size of an inner region
    int spatialOrder;                   ///< The spatial order of the kernels
    int bgOrder;                        ///< The order for the background fitting
} pmSubtractionKernels;

/// Generate a delta-function grid for subtraction kernels (like the POIS kernel)
bool p_pmSubtractionKernelsAddGrid(pmSubtractionKernels *kernels, ///< The subtraction kernels to append to
                                   int start, ///< Index at which to start appending
                                   int size ///< Half-size of the grid
    );

/// General allocator for pmSubtractionKernels
///
/// Unlike the functions for the specific kernel type, this function does not set up the basis functions, but
/// merely allocates space for their storage.
pmSubtractionKernels *pmSubtractionKernelsAlloc(int numBasisFunctions, ///< Number of basis functions
                                                pmSubtractionKernelsType type, ///< Kernel type
                                                int size, ///< Half-size of kernel
                                                int spatialOrder ///< Order of spatial variations
    );

/// Generate POIS kernels
pmSubtractionKernels *pmSubtractionKernelsPOIS(int size, ///< Half-size of the kernel (in both dims)
                                               int spatialOrder ///< Order of spatial variations
    );

/// Generate ISIS kernels without the flux scaling built in
pmSubtractionKernels *p_pmSubtractionKernelsRawISIS(int size, ///< Half-size of the kernel
                                                    int spatialOrder, ///< Order of spatial variations
                                                    const psVector *fwhms, ///< Gaussian FWHMs
                                                    const psVector *orders ///< Polynomial order of gaussians
    );

/// Generate ISIS kernels
pmSubtractionKernels *pmSubtractionKernelsISIS(int size, ///< Half-size of the kernel
                                               int spatialOrder, ///< Order of spatial variations
                                               const psVector *fwhms, ///< Gaussian FWHMs
                                               const psVector *orders ///< Polynomial order of gaussians
                                               );

/// Generate SPAM kernels
pmSubtractionKernels *pmSubtractionKernelsSPAM(int size, ///< Half-size of the kernel
                                               int spatialOrder, ///< Order of spatial variations
                                               int inner, ///< Inner radius to preserve unbinned
                                               int binning ///< Kernel binning factor
    );

/// Generate FRIES kernels
pmSubtractionKernels *pmSubtractionKernelsFRIES(int size, ///< Half-size of the kernel
                                                int spatialOrder, ///< Order of spatial variations
                                                int inner ///< Inner radius to preserve unbinned
    );

/// Generate GUNK kernels
pmSubtractionKernels *pmSubtractionKernelsGUNK(int size, ///< Half-size of the kernel
                                               int spatialOrder, ///< Order of spatial variations
                                               const psVector *fwhms, ///< Gaussian FWHMs
                                               const psVector *orders, ///< Polynomial order of gaussians
                                               int inner ///< Inner radius containing grid of delta functions
    );

/// Generate RINGS kernels
pmSubtractionKernels *pmSubtractionKernelsRINGS(int size, ///< Half-size of the kernel
                                                int spatialOrder, ///< Order of spatial variations
                                                int inner, ///< Inner radius to preserve unbinned
                                                int ringsOrder ///< Polynomial order
    );


/// Generate a kernel of a specified type
pmSubtractionKernels *pmSubtractionKernelsGenerate(pmSubtractionKernelsType type, ///< Kernel type
                                                   int size, ///< Half-size of the kernel
                                                   int spatialOrder, ///< Order of spatial variations
                                                   const psVector *fwhms, ///< Gaussian FWHMs
                                                   const psVector *orders, ///< Polynomial order of gaussians
                                                   int inner, ///< Inner radius to preserve unbinned
                                                   int binning, ///< Kernel binning factor
                                                   int ringsOrder ///< Polynomial order for RINGS
    );

/// Return the appropriate type from a string
pmSubtractionKernelsType pmSubtractionKernelsTypeFromString(const char *string // String name for kernel type
    );


#endif
