
/** @file  psStats.h
 *  \brief basic statistical operations
 *  @ingroup Stats
 *
 *  This file will hold the definition of the histogram and stats data
 *  structures.  It also contains prototypes for procedures which operate
 *  on those data structures.
 *
 *  @author George Gusciora, MHPCC
 *
 *  @version $Revision: 1.37 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2004-12-22 05:09:32 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 */
#if !defined(PS_STATS_H)
#define PS_STATS_H

#include "psVector.h"

/// @addtogroup Stats
/// @{

/******************************************************************************
    Statistical functions and data structures.
 *****************************************************************************/

/** enumeration of statistical calculation options
 *
 *  @see psStats, psVectorStats, psImageStats
 */
// XXX: Is PS_STAT_ROBUST_FOR_SAMPLE obsolete?
typedef enum {
    PS_STAT_SAMPLE_MEAN = 0x000001,
    PS_STAT_SAMPLE_MEDIAN = 0x000002,
    PS_STAT_SAMPLE_STDEV = 0x000004,
    PS_STAT_SAMPLE_QUARTILE = 0x000008,
    PS_STAT_ROBUST_MEAN = 0x000010,
    PS_STAT_ROBUST_MEDIAN = 0x000020,
    PS_STAT_ROBUST_MODE = 0x000040,
    PS_STAT_ROBUST_STDEV = 0x000080,
    PS_STAT_ROBUST_QUARTILE = 0x000100,
    PS_STAT_CLIPPED_MEAN = 0x000200,
    PS_STAT_CLIPPED_STDEV = 0x000400,
    PS_STAT_MAX =  0x000800,
    PS_STAT_MIN =  0x001000,
    PS_STAT_USE_RANGE =  0x002000,
    PS_STAT_USE_BINSIZE = 0x004000,
    PS_STAT_ROBUST_FOR_SAMPLE = 0x008000
} psStatsOptions;

/** This is the generic statistics structure.  It contails the data members
    for the various statistic values.  It also contains the options member to
    specifiy which statistics should be calculated. */
typedef struct
{
    psF64 sampleMean;          ///< formal mean of sample
    psF64 sampleMedian;        ///< formal median of sample
    psF64 sampleStdev;         ///< standard deviation of sample
    psF64 sampleUQ;            ///< upper quartile of sample
    psF64 sampleLQ;            ///< lower quartile of sample
    psF64 robustMean;          ///< robust mean of array
    psF64 robustMedian;        ///< robust median of array
    psF64 robustMode;          ///< Robust mode of array
    psF64 robustStdev;         ///< robust standard deviation of array
    psF64 robustUQ;            ///< robust upper quartile
    psF64 robustLQ;            ///< robust lower quartile
    psS32 robustN50;              ///<
    psS32 robustNfit;             ///<
    psF64 clippedMean;         ///< Nsigma clipped mean
    psF64 clippedStdev;        ///< standard deviation after clipping
    psS32 clippedNvalues;         ///< ???
    psF64 clipSigma;           ///< Nsigma used for clipping; user input
    psS32 clipIter;               ///< Number of clipping iterations; user input
    psF64 min;                 ///< minimum data value in array
    psF64 max;                 ///< maximum data value in array
    psF64 binsize;             ///<
    psStatsOptions options;     ///< bitmask of calculated values
}
psStats;

/** Performs statistical calculations on a vector.
 *
 *  @return psStats*    the statistical results as specified by stats->options
 */
psStats* psVectorStats(
    psStats* stats,    ///< stats structure defines stats to be calculated and how
    psVector* in,      ///< Vector to be analysed.
    psVector* errors,  ///< Errors.
    psVector* mask,    ///< Ignore elements where (maskVector & maskVal) != 0: must be INT or NULL
    psU32 maskVal      ///< Only mask elements with one of these bits set in maskVector
);

/** Allocator of the psStats structure.
 *
 *  @return psStats*    A new psStats struct with the options member set to the
 *                      value given.
 */
psStats* psStatsAlloc(
    psStatsOptions options             ///< Statistics to calculate
);

/******************************************************************************
    Histogram functions and data structures.
 *****************************************************************************/

/** The basic histogram structure which contains bounds and bins.
 *
 *  In this structure, the vector bounds specifies the boundaries of the 
 *  histogram bins, and must of type psF32, while nums specifies the number 
 *  of entries in the bin, and must of type psU32. The value of bounds.n must 
 *  therefore be 1 greater than than nums.n. The two values minNum and maxNum 
 *  are the number of data values which fell below the lower limit bound or 
 *  above the upper limit bound, respectively.
 */
typedef struct
{
    psVector* bounds;                  ///< Bounds for the bins (type F32)
    psVector* nums;                    ///< Number in each of the bins (INT)
    psS32 minNum;                        ///< Number below the minimum
    psS32 maxNum;                        ///< Number above the maximum
    psBool uniform;                      ///< Is it a uniform distribution?
}
psHistogram;

/** Allocator for psHistogram where the bounds of the bins are implicitly
 *  specified through simply specifying an upper and lower limit along with 
 *  the size of the bins. 
 *
 *  @return psHistogram*    Newly allocated psHistogram
 */
psHistogram* psHistogramAlloc(
    psF32 lower,                       ///< Lower limit for the bins
    psF32 upper,                       ///< Upper limit for the bins
    psS32 n                              ///< Number of bins
);

/** Allocator for psHistogram where the bounds of the bins are explicitly
 *  specified. 
 *
 *  @return psHistogram*    Newly allocated psHistogram
 */
psHistogram* psHistogramAllocGeneric(
    const psVector* restrict bounds    ///< Bounds for the bins
);

/** Calculate a histogram
 *
 *  The following function populates the histogram bins from the specified 
 *  vector (in). It alters and returns the histogram out structure. The input
 *  vector may be of types psU8, psU16, psF32, psF64.
 *
 *  @return psHistogram*   histogram result
 */
psHistogram* psVectorHistogram(
    psHistogram* out,                  ///< Histogram data
    const psVector* restrict in,       ///< Vector to analyse
    const psVector* restrict errors,   ///< Errors
    const psVector* restrict mask,     ///< Mask dat for input vector
    psU32 maskVal               ///< Mask value
);

/** Extracts the statistic value specified by stats->options.
 *
 *  @return psBool    If more than one statistic result is set in stats->options,
 *                  false is returned and the value parameter is not set, 
 *                  otherwise true is returned.
 */
psBool p_psGetStatValue(
    const psStats* stats,
    ///< the statistic struct to operate on

    psF64 *value
    ///< if return is true, this is set to the specified statistic value by stats->options
);

void p_psNormalizeVectorRangeU8(psVector* myData, psU8 low, psU8 high);
void p_psNormalizeVectorRangeU16(psVector* myData, psU16 low, psU16 high);
void p_psNormalizeVectorRangeU32(psVector* myData, psU32 low, psU32 high);
void p_psNormalizeVectorRangeU64(psVector* myData, psU64 low, psU64 high);
void p_psNormalizeVectorRangeS8(psVector* myData, psS8 low, psS8 high);
void p_psNormalizeVectorRangeS16(psVector* myData, psS16 low, psS16 high);
void p_psNormalizeVectorRangeS32(psVector* myData, psS32 low, psS32 high);
void p_psNormalizeVectorRangeS64(psVector* myData, psS64 low, psS64 high);
void p_psNormalizeVectorRangeF32(psVector* myData, psF32 low, psF32 high);
void p_psNormalizeVectorRangeF64(psVector* myData, psF64 low, psF64 high);

/// @}

#endif
