#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ppImage.h"
#include "ppImageDetrendFringe.h"

bool ppImageDetrendReadout(pmConfig *config, ppImageOptions *options, pmFPAview *view)
{
    // construct a view for the detrend images (which have only one readout)
    pmFPAview *detview = pmFPAviewAlloc(0);
    *detview = *view;
    detview->readout = 0;

    // find the currently selected readout
    pmReadout *input = pmFPAfileThisReadout(config->files, view, "PPIMAGE.INPUT");

# if 0
    // Non-linearity correction
    if (options->doNonLin) {
        ppImageDetrendNonLinear(detrend->input, input, options);
    }
# endif

    // set up the readouts for dark and bias
    pmReadout  *dark = NULL;
    pmReadout  *bias = NULL;
    if (options->doBias) {
        bias = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.BIAS");
    }
    if (options->doDark) {
        dark = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.DARK");
    }

    // Bias, dark and overscan subtraction are all merged.
    if (options->doBias || options->doDark || options->doOverscan) {
        if (!pmBiasSubtract(input, options->overscan, bias, dark)) {
            psError(PS_ERR_UNKNOWN, false, "Unable to subtract bias.\n");
            return false;
        }
    }

    if (options->doMask || options->doShutter || options->doFlat || options->doPhotom) {
        // create the target mask and weight images
        pmReadoutGenerateMaskWeight(input, true);
    }

    // apply the externally supplied mask to the input->mask pixels
    if (options->doMask) {
        pmReadout *mask = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.MASK");
        pmMaskBadPixels(input, mask, options->maskValue);
    }

    // Shutter correction
    if (options->doShutter) {
        pmReadout *shutter = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.SHUTTER");
        if (!pmShutterCorrectionApply(input, shutter)) {
            return false;
        }
    }

    // Flat-field correction (no options used?)
    if (options->doFlat) {
        pmReadoutGenerateMaskWeight(input, true);
        pmReadout *flat = pmFPAfileThisReadout(config->files, detview, "PPIMAGE.FLAT");
        if (!pmFlatField(input, flat)) {
            return false;
        }
    }

    // Normalisation by a (known) constant
    bool mdok;                          // Status of MD lookup
    float norm = psMetadataLookupF32(&mdok, config->arguments, "NORMALISATION");
    if (mdok && isfinite(norm) && norm != 1.0) {
        pmHDU *hdu = pmHDUFromReadout(input); // HDU of interest
        psString comment = NULL;        // Comment to add
        psStringAppend(&comment, "Normalisation: %f", norm);
        psMetadataAddStr(hdu->header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, comment, "");
        psFree(comment);

        psBinaryOp(input->image, input->image, "*", psScalarAlloc(norm, PS_TYPE_F32));
    }

    if (options->doFringe) {
        pmCell *fringe = pmFPAfileThisCell(config->files, detview, "PPIMAGE.FRINGE");
        if (!ppImageDetrendFringeMeasure(input, fringe, options)) {
            return false;
        }
    }

    psFree(detview);
    return true;
}
