#ifndef PPSTACK_LOOP_H
#define PPSTACK_LOOP_H

#include <pslib.h>
#include <psmodules.h>

#include "ppStackOptions.h"
#include "ppStackThread.h"
#include "ppStack.h"


// Loop over the inputs, doing the combination
bool ppStackLoop(
    pmConfig *config,                    // Configuration
    ppStackOptions *options             // Options for stacking
    );

// Setup
bool ppStackSetup(
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Preparation for stacking
//
// Merge input source lists, determine target PSF
bool ppStackPrepare(
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Convolve inputs to match target PSF
bool ppStackConvolve(
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Prepare for combination
bool ppStackCombinePrepare(
    const char *outName,                // Name of output file
    const char *expName,                // Name of exposure file
    ppStackFileList files,              // Files of interest
    ppStackThreadData *stack,           // Stack
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Initial combination
bool ppStackCombineInitial(
    ppStackThreadData *stack,           // Stack
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Reject pixels
bool ppStackReject(
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Final combination
bool ppStackCombineFinal(
    ppStackThreadData *stack,           // Stack
    psArray *covariances,               // Covariances
    ppStackOptions *options,            // Options
    pmConfig *config,                   // Configuration
    bool safe,                          // Allow safe combination?
    bool norm,                          // Normalise images?
    bool grow                           // Grow rejection masks?
    );

// Cleanup following combination
bool ppStackCleanup(
    ppStackThreadData *stack,           // Stack
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Photometry
bool ppStackPhotometry(
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

// Finish up
bool ppStackFinish(
    ppStackOptions *options,            // Options
    pmConfig *config                    // Configuration
    );

#endif
