/** @file ppSubReadoutRenorm.c
 *
 *  @brief
 *
 *  @ingroup ppSub
 *
 *  @author IfA
 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-06 03:20:02 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

#include "ppSub.h"

bool ppSubReadoutRenormPixels (pmConfig *config, psMetadata *recipe, pmReadout *readout) {

    bool mdok = false;

    if (!psMetadataLookupBool(&mdok, recipe, "RENORM")) return true;

    // Statistics for renormalisation
    psStatsOptions renormMean = psStatsOptionFromString(psMetadataLookupStr(&mdok, recipe, "RENORM.MEAN"));
    psStatsOptions renormStdev = psStatsOptionFromString(psMetadataLookupStr(&mdok, recipe, "RENORM.STDEV"));
    if (renormMean == PS_STAT_NONE || renormStdev == PS_STAT_NONE) {
        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
                "Unable to parse renormalisation statistics from recipe.");
        return false;
    }
    psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
    if (!pmReadoutVarianceRenormPixels(readout, maskValue, renormMean, renormStdev, NULL)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to renormalise variances.");
        return false;
    }
    return true;
}

/**
 * old-style variance renormalization
 */
bool ppSubReadoutRenormPhot (pmConfig *config, psMetadata *recipe, pmReadout *readout) {

    bool mdok = false;

    if (!psMetadataLookupBool(&mdok, recipe, "RENORM")) return true;

    // Statistics for renormalisation
    psStatsOptions renormMean = psStatsOptionFromString(psMetadataLookupStr(&mdok, recipe, "RENORM.MEAN"));
    psStatsOptions renormStdev = psStatsOptionFromString(psMetadataLookupStr(&mdok, recipe, "RENORM.STDEV"));
    if (renormMean == PS_STAT_NONE || renormStdev == PS_STAT_NONE) {
        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
                "Unable to parse renormalisation statistics from recipe.");
        return false;
    }

    // other renorm parameters
    int renormNum = psMetadataLookupS32(&mdok, recipe, "RENORM.NUM"); // Number of samples for renormalisation
    float renormWidth = psMetadataLookupS32(&mdok, recipe, "RENORM.WIDTH"); // Width of Gaussian phot

    psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
    if (!pmReadoutVarianceRenormPhot(readout, maskValue, renormNum, renormWidth, renormMean, renormStdev, NULL)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to renormalise variances.");
        return false;
    }
    return true;
}
