#ifndef PP_MOPS_H
#define PP_MOPS_H

#include <pslib.h>
#include <pmSourceMasks.h>

#define IN_EXTNAME "SkyChip.psf"        // Extension name for data in input
#define OBSERVATORY_CODE "F51"          // IAU Observatory Code
#define OUT_EXTNAME "MOPS_TRANSIENT_DETECTIONS" // Extension name for data in output
#define SOURCE_MASK (PM_SOURCE_MODE_FAIL | PM_SOURCE_MODE_BADPSF | PM_SOURCE_MODE_SATURATED | \
                     PM_SOURCE_MODE_CR_LIMIT | PM_SOURCE_MODE_SKY_FAILURE) // Flags to exclude

// Configuration data
typedef struct {
    psArray *input;                     // Input filenames
    psString exp_name;                  // Exposure name
    psS64 exp_id;                       // Exposure identifier
    psS64 chip_id;                      // Chip stage identifier
    psS64 cam_id;                       // Camera stage identifier
    psS64 fake_id;                      // Fake stage identifier
    psS64 warp_id;                      // Warp stage identifier
    psS64 diff_id;                      // Diff stage identifier
    bool positive;                      // Sense of subtraction, T=positive, F=negative
    float zp, zpErr;                    // Magnitude zero point and error
    float rmsAstrom;                    // Astrometric solution RMS
    psString output;                    // Output filename
} ppMopsArguments;

/// Parse arguments
ppMopsArguments *ppMopsArgumentsParse(int argc, char *argv[]);

typedef struct {
    psString raBoresight, decBoresight; // RA,Dec of telescope boresight
    psString filter;                    // Filter for exposure
    float airmass;                      // Airmass of exposure
    float exptime;                      // Exposure time
    double posangle;                    // Position angle
    double alt, az;                     // Telescope altitude and azimuth
    double mjd;                         // Modified Julian Date
    float seeing;                       // Seeing of exposure
    long num;                           // Number of detections
    psVector *x, *y;                    // Image coordinates
    psVector *ra, *dec;                 // Sky coordinates
    psVector *raErr, *decErr;           // Error in sky coordinates
    psVector *mag, *magErr;             // Magnitude and associated error
    psVector *extended;                 // Measure of extendedness
    psVector *angle, *angleErr;         // Angle of trail and associated error
    psVector *length, *lengthErr;       // Length of trail and associated error
    psVector *flags;                    // psphot flags
    psVector *diffSkyfileId;            // Identifier for source image
    psVector *naxis1, *naxis2;          // Size of image
    psVector *mask;                     // Mask for detections
} ppMopsDetections;

ppMopsDetections *ppMopsDetectionsAlloc(long num);

/// Copy a detection
bool ppMopsDetectionsCopySingle(ppMopsDetections *target, const ppMopsDetections *source, long index);

/// Purge the detections list of masked detections
bool ppMopsDetectionsPurge(ppMopsDetections *detections);


/// Read detections
psArray *ppMopsRead(const ppMopsArguments *args);

/// Merge detections
ppMopsDetections *ppMopsMerge(const psArray *detections);

/// Write detections
bool ppMopsWrite(const ppMopsDetections *detections, const ppMopsArguments *args);

#endif
