/** @file  pmFringeStats.h
 *
 *  @brief Measure Fringe statistics and apply correction
 *
 *  @author Eugene Magnier, IfA
 *
 *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2006-04-12 23:50:45 $
 *
 *  Copyright 2004 IfA, University of Hawaii
 */

# ifndef PM_FRINGE_STATS
# define PM_FRINGE_STATS

/** Structure to hold the fringe measurement regions.
 */
typedef struct
{
    psU32 nRequested;   // number of fringe points selected
    psU32 nAccepted;   // number of fringe points not masked
    psU32 dX;    // median box half-width
    psU32 dY;    // median box half-height
    psU32 nX;    // large-scale smoothing in x (col)
    psU32 nY;    // large-scale smoothing in y (row)
    psVector *x;    // fringe point coordinates (col)
    psVector *y;    // fringe point coordinates (row)
    psVector *mask;   // fringe point on/off mask
}
pmFringeRegions;

pmFringeRegions *pmFringeRegionsAlloc (
    int nPts,     // number of points to create
    int dX,     // half-width of fringe boxes
    int dY,     // half-height of fringe boxes
    int nX,     // smoothing scale in x
    int nY    // smoothing scale in y
);

/** Structure to hold the fringe measurements for a particular image
 */
typedef struct
{
    pmFringeRegions *regions;           // Fringe regions
    psVector *f;    // fringe point median
    psVector *df;   // fringe point stdev
}
pmFringeStats;

pmFringeStats *pmFringeStatsAlloc(pmFringeRegions *regions);


/** the pmFringeScale structure defines the relationship between two fringe measurements
 */
typedef struct
{
    int nFringeFrames;
    psVector *coeff;
    psVector *coeffErr;
}
pmFringeScale;

/** Measure the fringe stats for an image
 *
 *  Given an input image and a fringe stats structure, measure the statistics for each of the fringe points on
 *  the image.  If the fringe vectors are NULL, new vectors are created.
 *
 *  XXX should the structure carry the image dimensions for validation?
 *
 *  @return  bool: True or false for success or failure
 */
pmFringeStats *pmFringeStatsMeasure(
    pmFringeRegions *fringe,            ///< fringe regions
    pmReadout *readout,                 ///< measure fringes on this readout
    psMaskType maskVal                  ///< Mask value for statistics
);

/** Fringe correct the science image
 *
 *  Given a science image, a set of fringe stats and a matched set of fringe images, correct the science image
 *  for the fringe images.
 *
 *  @return  psImage: summed fringe image
 */
psImage *pmFringeCorrect(pmReadout *in, // place info about results here
                         pmFringeRegions *fringes, // The fringe regions used
                         psArray *fringeImages, // fringe images to use in correction
                         psArray *fringeStats, // fringe stats to use in correction
                         psMaskType maskVal // Value to mask
                        );

# endif
