/*
 * streaksremove
 *
 * Convert satellite streak detctions into masks and remove the covered pixels from the
 * input images.
 * Optionally swap the inputs with the outputs so that subsequent references to the original
 * images access the destreaked versions.
 */

#include "streaksremove.h"

static pmConfig *parseArguments(int argc, char **argv);
static bool readAndCopyToOutput(streakFiles *sf, bool exciseAll);
static void exciseNonWarpedPixels(streakFiles *sfiles, psImageMaskType newMaskValue);
static bool warpedPixel(streakFiles *sfiles, int x, int y);
static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue);
static void writeImages(streakFiles *sf, bool exciseImageCube);
static void updateAstrometry(streakFiles *sfiles);
static void censorSources(streakFiles *sfiles, psImageMaskType maskStreak);
static long censorPixels(streakFiles *sfiles, psImage * pixels, bool checkNonWarpedPixels, psU16 maskStreak);

int
main(int argc, char *argv[])
{
    bool status;

    psLibInit(NULL);
    psTimerStart("STREAKSREMOVE");

    pmConfig *config = parseArguments(argc, argv);
    if (!config) {
        psError(PS_ERR_UNKNOWN, false, "failed to parse arguments\n");
        streaksExit("", PS_EXIT_CONFIG_ERROR);
    }

    // Values to set for masked pixels
    psU32 maskStreak = 0;           // for the image and weight (usually NAN, MAXINT for integer images)
    psU32 maskMask = 0;             // value looked up for MASK.STREAK

    psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS");

    // call Paul Sydney's code to parse the streaks file that DetectStreaks produced
    Streaks *streaks = readStreaksFile(streaksFileName);
    if (!streaks) {
        psError(PS_ERR_UNKNOWN, false, "failed to read streaks file: %s", streaksFileName);
        streaksExit("", PS_EXIT_PROG_ERROR);
    }

    // open all of the input and output files, save their descriptions in the streakFiles struct
    streakFiles *sfiles = openFiles(config, true, argv[0]);
    setupAstrometry(sfiles);

    // Optionally we can set pixels that are masked to NAN since they couldn't have been
    // examined for streaks. Usually this is done by the distribution system just prior
    // to release
    bool nanForRelease = psMetadataLookupBool(&status, config->arguments, "NAN_FOR_RELEASE");
    if (nanForRelease && (sfiles->inMask == NULL)) {
        psError(PS_ERR_UNKNOWN, true, "mask image must be supplied with -release.");
        streaksExit("", PS_EXIT_PROG_ERROR);
    }

    bool exciseAll = false;
    // --keepnonwarped is a test and debug mode
    bool keepNonWarpedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_WARPED");

    // we need to check for non warped pixels unless we've been asked not to or the stage is diff
    // (By definition pixels in diff images were included in a diff)
    bool checkNonWarpedPixels = ! (keepNonWarpedPixels || (sfiles->stage == IPP_STAGE_DIFF) );

    if (checkNonWarpedPixels ) {
        // From magic ICD:
        // In the raw and detrended images, the pixels which were not
        // included in any of the streak-processed warps must also be masked.
        // Note that the warp and difference skycells are only generated if more
        // than a small fraction of the pixels are lit by the input image.
        // if no skycells are provided sfiles->exciseAll is set to true

        psTimerStart("COMPUTE_WARPED_PIXELS");
        if (! computeWarpedPixels(sfiles) ) {
            // we have no choice to excise all pixels
            exciseAll = true;
        }
        psF64 cwp_t = psTimerClear("COMPUTE_WARPED_PIXELS");
        psLogMsg("streaksremove", PS_LOG_INFO, "time to compute warped pixels: %f\n", cwp_t);
    }

    if (sfiles->stage == IPP_STAGE_RAW) {
        // Except for raw stage, all of our (GPC1) files have one image extension.
        // Raw files have a phu and multiple extensions, one per chip
        // Since this is a raw file, copy it's PHU to output files
        copyPHU(sfiles, true);

        // advance to the first image extension
        if (!streakFilesNextExtension(sfiles)) {
            psErrorStackPrint(stderr, "failed to advance to first extension of input images");
            exit (PS_EXIT_PROG_ERROR);
        }
    }

    long totalPixels = 0;
    long totalStreakPixels = 0;

    // Iterate through each component of the input (except for raw images there is only one)
    do {
        bool exciseImageCube = false;

        psTimerStart("PROCESS_COMPONENT");

        // read the images and copy the data from the inputs to the outputs
        // This also sets up the astrometry and initializes the recovery image(s)

        if (!readAndCopyToOutput(sfiles, exciseAll)) {
            // false return value indicates that this extension is not an image and
            // the contents has already been copied to the output file.
            // we don't need to process this extesion for streaks.
            continue;
        }

        // now that we've read the input files, lookup the mask values
        if (maskStreak == 0) {
            strkGetMaskValues(sfiles, &maskStreak, &maskMask);
        }

        totalPixels += sfiles->inImage->numRows * sfiles->inImage->numCols;

        if (!exciseAll) {
            // Identify pixels to mask because of streaks

            psTimerStart("GET_STREAK_PIXELS");

            // call Paul Sydney's code to compute the set of pixels that are covered by the detected streaks
            StreakPixels *pixels = streak_on_component(streaks, sfiles->astrom, sfiles->inImage->numCols,
                                                        sfiles->inImage->numRows);
            psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS"));

            // if this extension contained an image, excise the streaked pixels.
            // otherwise it contained an image cube (video cell) which is handled in the if block
            if (sfiles->inImage->image) {
                if (checkNonWarpedPixels) {
                    psTimerStart("EXCISE_NON_WARPED");

                    // set non-warped pixels and variance to NAN, mask to maskStreak (since the pixel
                    // is excised as part of the destreaking process)
                    exciseNonWarpedPixels(sfiles, maskStreak);

                    psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED"));
                }

                psTimerStart("REMOVE_STREAKS");

                totalStreakPixels += censorPixels(sfiles, pixels, checkNonWarpedPixels, maskStreak);

                psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS"));

                if (nanForRelease) {
                    // set any pixels that were masked, to NAN (unless they are already NAN)
                    setMaskedToNAN(sfiles, maskMask, true);
                }

            } else {
                // this component contains an image cube
                // For now excise it completely
                exciseImageCube = true;
            }
            psFree(pixels);
        }

        if (sfiles->stage == IPP_STAGE_CHIP) {
            // as a convience to the user of the output, replace the bogus WCS transform in the
            // chip processed files with the data calcuated by psastro at the camera stage
            // (actually we use a linear approximation)
            updateAstrometry(sfiles);
        }

        censorSources(sfiles, maskStreak);

        // write the destreaked "temporary" images and the recovery images
        writeImages(sfiles, exciseImageCube);


        psLogMsg("streaksremove", PS_LOG_INFO, "time to process component %d: %f\n", sfiles->extnum, psTimerClear("PROCESS_COMPONENT"));
    } while (streakFilesNextExtension(sfiles));


    psFree(streaks);

    psLogMsg("streaksremove", PS_LOG_INFO, "pixels: %ld streak pixels: %ld %4.2f%%\n", totalPixels, totalStreakPixels, 100. * totalStreakPixels / totalPixels);

    // check the weight and mask files for extra extensions that might be in files
    // (covariance matrix for example)
    copyExtraExtensions(sfiles);

    // all done close the files. This is where the files are written so it can take a long time.

    psTimerStart("CLOSE_IMAGES");

    closeImages(sfiles);

    psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", psTimerClear("CLOSE_IMAGES"));


    if (!replicateOutputs(sfiles)) {
        psErrorStackPrint(stderr, "failed to replicate output files");
        deleteTemps(sfiles);
        exit(PS_EXIT_UNKNOWN_ERROR);
    }

    // NOTE: from here on we can't just quit if something goes wrong.
    // especially if we're working at the raw stage
    // turn off automatic deletion of output files by streaksExit
    setStreakFiles(NULL);

    if (psMetadataLookupBool(&status, config->arguments, "REPLACE")) {
        //     swap the instances for the input and output
        //     Note this is a nebulous database operation. No file I/O is performed
        if (!swapOutputsToInputs(sfiles)) {
            // XXX: Now what?
            // It is up to the program that reverts failed destreak runs to insure that
            // any input files that have been swapped are restored and that the de-streaked
            // versions are deleted

            psErrorStackPrint(stderr, "failed to swap files");

            // XXX: pick a specific error code for this failure
            exit(PS_EXIT_UNKNOWN_ERROR);
        }
    }
    // all done. Clean up to look for memory leaks.

    psFree(sfiles);
    psFree(config);
    pmConceptsDone();
    pmModelClassCleanup();
    streaksNebulousCleanup();
    pmConfigDone();
    psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", psTimerClear("STREAKSREMOVE"));
    psLibFinalize();

    fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "streaksremove");

    return 0;
}

static long
censorPixels(streakFiles *sfiles, psImage *pixels, bool checkNonWarpedPixels, psU16 maskStreak)
{
    long streakPixels = 0;

    for (int y=0 ; y < sfiles->inImage->numRows; y++) {
        for (int x = 0; x < sfiles->inImage->numCols; x++) {
            if (psImageGet(pixels, x, y)) {
                ++streakPixels;
                if (!checkNonWarpedPixels || warpedPixel(sfiles, x, y)) {

                    excisePixel(sfiles, x, y, true, maskStreak);

                } else {
                    // This pixel was not included in any warp and has thus already excised
                    // by exciseNonWarpedPixels
                }
            }
        }
    }
    return streakPixels;
}

static void usage()
{
    fprintf(stderr, "USAGE: streaksremove -stage raw|chip|warp|diff -image IMAGE.fits -tmproot directory -streaks streaks_file\n\n");
    fprintf(stderr, "Optional arguments:\n");
    fprintf(stderr, "\t-class_id class_id: class_id (required for raw and chip stages)\n");
    fprintf(stderr, "\t-recovery: directory to save excised pixel images\n");
    fprintf(stderr, "\t-mask MASK.fits mask file to de-streak\n");
    fprintf(stderr, "\t-weight WEIGHT.fits: weight file to de-streak\n");
    fprintf(stderr, "\t-replace: replace the input images with the output\n");
    fprintf(stderr, "\t-keepnonwarped: do not exise pixels that were not part of difference processing\n");
    fprintf(stderr, "\t-transparent val: instead of setting excicsed pixel to NAN add val\n");
    fprintf(stderr, "\t-chip_mask MASK.fits: name of mask for chip stage (camera stage mask is passed tih -mask)\n");

    exit(2);


}
static pmConfig *parseArguments(int argc, char **argv)
{

    pmConfig *config = pmConfigRead(&argc, argv, NULL);
    if (!config) {
        streaksExit("failed to parse arguments", PS_EXIT_CONFIG_ERROR);
    }

    int argnum;
    ippStage stage = IPP_STAGE_NONE;
    if ((argnum = psArgumentGet(argc, argv, "-stage"))) {
        psArgumentRemove(argnum, &argc, argv);
        stage = parseStage(argv[argnum]);
        psMetadataAddS32(config->arguments, PS_LIST_TAIL, "STAGE", 0,
                "pipeline stage for streak removal", stage);
        psArgumentRemove(argnum, &argc, argv);
    } else {
        psError(PS_ERR_UNKNOWN, true, "-stage is required\n");
        usage();
    }

    if ((argnum = psArgumentGet(argc, argv, "-class_id"))) {
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "CLASS_ID", 0,
                "class_id", argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    } else if ((stage == IPP_STAGE_RAW) || (stage == IPP_STAGE_CHIP)) {
        psError(PS_ERR_UNKNOWN, true, "-class_id is required\n");
        usage();
    }

    if ((argnum = psArgumentGet(argc, argv, "-streaks"))) {
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "STREAKS", 0,
                "STREAKS", argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    } else {
        psError(PS_ERR_UNKNOWN, true, "-streaks is required\n");
        usage();
    }


    // If input image is in nebulous we require mask, weight, and tmproot to all be
    // nebulous paths
    bool nebulousImage = false;
    if ((argnum = psArgumentGet(argc, argv, "-image"))) {
        // for raw and chip level images we use psFits for I/O and ...
        nebulousImage = IN_NEBULOUS(argv[argnum+1]);
        if (CHIP_LEVEL_INPUT(stage)) {
            psArgumentRemove(argnum, &argc, argv);
            psMetadataAddStr(config->arguments, PS_LIST_TAIL, "INPUT", 0,
                    "name of input image", argv[argnum]);
            psArgumentRemove(argnum, &argc, argv);
        } else  {
            // ... for warped images we use pmFPAfiles
            if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "INPUT", "-image", NULL)) { ;
                psError(PS_ERR_UNKNOWN, false, "failed to process -image");
                usage();
            }
        }
    } else {
        psError(PS_ERR_UNKNOWN, true, "-image is required\n");
        usage();
    }

    bool gotReplace = false;
    if ((argnum = psArgumentGet(argc, argv, "-replace"))) {
        if (!nebulousImage) {
            psError(PS_ERR_UNKNOWN, true, "replace is only supported for nebulous files");
            usage();
        }
        gotReplace = true;
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "REPLACE", 0, "replace input files",
            true);
    }

    if ((argnum = psArgumentGet(argc, argv, "-release"))) {
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "NAN_FOR_RELEASE", 0, "set masked pixels to NAN",
            true);
    }

    if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) {
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_WARPED", 0,
            "skip excising of non warped pixels", true);
    }

    if ((argnum = psArgumentGet(argc, argv, "-transparent"))) {
        if (gotReplace) {
            psError(PS_ERR_UNKNOWN, true, "cannot make transparent streaks if -replace");
            usage();
        }
        psArgumentRemove(argnum, &argc, argv);
        double transparentStreaks = atof(argv[argnum]);
        psMetadataAddF64(config->arguments, PS_LIST_TAIL, "TRANSPARENT_STREAKS", 0,
            "value to adjust excised pixels", transparentStreaks);
        psArgumentRemove(argnum, &argc, argv);
    }

    // if skycells are not provided then we have to execise all pixels  unless -keepnonwarped
    pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist");

    if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "ASTROM", "-astrom", NULL)) { ;
        if (CHIP_LEVEL_INPUT(stage)) {
            psError(PS_ERR_UNKNOWN, true, "-astrom is required for raw and chip stages\n");
            usage();
        }
    }

    if ((argnum = psArgumentGet(argc, argv, "-mask"))) {
        psArgumentRemove(argnum, &argc, argv);
        bool nebulousMask = IN_NEBULOUS(argv[argnum]);
        // This isn't a strict requirement if stage == IPP_STAGE_CHIP
        if (nebulousMask != nebulousImage) {
            psError(PS_ERR_UNKNOWN, true, "mask image must be %snebulous path with %s image path\n",
                nebulousImage ? "" : "non ", nebulousImage ? "nebulous" : "regular");
            usage();
        }
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "INPUT.MASK", 0,
                "name of input mask image", argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    }

    if ((argnum = psArgumentGet(argc, argv, "-chip_mask"))) {
        if (stage != IPP_STAGE_CHIP) {
            psError(PS_ERR_UNKNOWN, true, "-chip_mask only allowed at chip stage");
            usage();
        }
        psArgumentRemove(argnum, &argc, argv);
        bool nebulousMask = IN_NEBULOUS(argv[argnum]);
        if (nebulousMask != nebulousImage) {
            psError(PS_ERR_UNKNOWN, true, "chip mask image must be %snebulous path with %s image path\n",
                nebulousImage ? "" : "non ", nebulousImage ? "nebulous" : "regular");
            usage();
        }
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "INPUT.CHMASK", 0,
                "name of input chip mask image", argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    }

    if ((argnum = psArgumentGet(argc, argv, "-weight"))) {
        psArgumentRemove(argnum, &argc, argv);
        bool nebulousWeight = IN_NEBULOUS(argv[argnum]);
        if (nebulousWeight != nebulousImage) {
            psError(PS_ERR_UNKNOWN, true, "weight image must have %snebulous path with %s image path\n",
                nebulousImage ? "" : "non ", nebulousImage ? "nebulous" : "regular");
            usage();
        }
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "INPUT.WEIGHT", 0,
                "name of input weight image", argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    }
    if ((argnum = psArgumentGet(argc, argv, "-sources"))) {
        psArgumentRemove(argnum, &argc, argv);
        bool nebulousSources = IN_NEBULOUS(argv[argnum]);
        if (nebulousSources != nebulousImage) {
            psError(PS_ERR_UNKNOWN, true, "sources file must have %snebulous path with %s image path\n",
                nebulousImage ? "" : "non ", nebulousImage ? "nebulous" : "regular");
            usage();
        }
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "SOURCES", 0,
                "name of input sources file", argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    }

    if ((argnum = psArgumentGet(argc, argv, "-tmproot"))) {
        psArgumentRemove(argnum, &argc, argv);
        bool nebulousTmp = IN_NEBULOUS(argv[argnum]);
        if (gotReplace && (nebulousTmp != nebulousImage)) {
            psError(PS_ERR_UNKNOWN, true, "tmproot must have %snebulous path with %s image path with -replace\n",
                nebulousImage ? "" : "non ", nebulousImage ? "nebulous" : "regular");
            usage();
        }
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "path for (temporary if replae) output files",
            argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    } else {
        psError(PS_ERR_UNKNOWN, true, "-tmproot is required\n");
        usage();
    }

    if ((argnum = psArgumentGet(argc, argv, "-recovery"))) {
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "RECOVERY", 0, "path for recovery files",
            argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    } else if ((stage == IPP_STAGE_RAW) && gotReplace) {
        psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw with -replace\n");
        usage();
    }

    if (argc != 1) {
        psString unexpectedArguments = NULL;
        for (int i=1; i<argc; i++) {
            psStringAppend(&unexpectedArguments, "%s ", argv[i]);
        }
        psError(PS_ERR_UNKNOWN, true, "unexpected arguments: %s", unexpectedArguments);
        psFree(unexpectedArguments);
        usage();
    }

    return config;
}

static void
updateAstrometry(streakFiles *sf)
{
    // XXX: why do I check this here? Shouldn't it be just around the call to linearizeTransforms?
    if (sf->bilevelAstrometry) {

        if (!linearizeTransforms(sf->astrom)) {
            // fit failed, leave the astrometry unchanged
            return;
        }

        if (!pmAstromWriteWCS(sf->outImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
            psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
            streaksExit("", PS_EXIT_UNKNOWN_ERROR);
        }
        if (sf->outMask) {
            pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
        }
        if (sf->outWeight) {
            pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
        }
    }
}

// set the image for the output files to the contents of the corresponding input file.
static bool
readAndCopyToOutput(streakFiles *sf, bool exciseAll)
{
    bool    updateAstrometry = false;
    if (sf->inImage->pmfile) {
        // image data from pmFPAfile (diff or warp file)
        readImageFrom_pmFile(sf);
        sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa,
                sf->chip, true, sf->cell->concepts, sf->inImage->numCols, sf->inImage->numRows);
    } else {
        // image data directly from psFits
        readImage(sf->inImage, sf->extnum, sf->stage, false);
        if (SFILE_IS_IMAGE(sf->inImage)) {
            sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa, sf->chip, false,
                sf->inImage->header, sf->inImage->numCols, sf->inImage->numRows);
        }
    }
    if (!sf->astrom) {
        psError(PS_ERR_UNKNOWN, false, "failed to set up astrometry for extension %d", sf->extnum);
        streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    }
    // For the chip level files, copy the WCS from the astrometry file to the header
    // XXX: do we want to do this for raw images as well?
    if (sf->stage == IPP_STAGE_CHIP) {
        if (!sf->bilevelAstrometry) {
            updateAstrometry = true;
            if (!pmAstromWriteWCS(sf->inImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
                psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
                streaksExit("", PS_EXIT_UNKNOWN_ERROR);
            }
        }
    }
    sf->outImage->header =  psMemIncrRefCounter(sf->inImage->header);
    if (sf->recImage) {
        sf->recImage->header = psMetadataCopy(NULL, sf->inImage->header);
        addRecoveryKeyword(sf->recImage->header);
    }
    addDestreakKeyword(sf->outImage->header);

    if (!SFILE_IS_IMAGE(sf->inImage)) {
        // fprintf(stderr, "skipping extnum: %d not an image\n", sf->extnum);
        // XXX: This extension is probably a video table.
        copyTable(sf->outImage, sf->inImage, sf->extnum);
        return false;
    }

    if (sf->inImage->image) {
        setupImageRefs(sf->outImage, sf->recImage, sf->inImage, sf->extnum, exciseAll);
    }  else {
        // Image cubes are handeled specially
    }

    // set up the compression parameters
    copyFitsOptions(sf->outImage, sf->recImage, sf->inImage, sf->tiles);

    if (sf->inMask) {
        readImage(sf->inMask, sf->extnum, sf->stage, true);
        if (sf->outMask) {
            sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
            if (sf->recMask) {
                sf->recMask->header = psMetadataCopy(NULL, sf->outMask->header);
                addRecoveryKeyword(sf->recMask->header);
            }
            addDestreakKeyword(sf->outMask->header);
            if (updateAstrometry) {
                pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
            }
            setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
            if (sf->outChMask) {
                sf->outChMask->header = (psMetadata *) psMemIncrRefCounter(sf->outMask->header);
                sf->outChMask->image = (psImage *) psMemIncrRefCounter(sf->outMask->image);
                if (sf->recChMask) {
                    sf->recChMask->header = (psMetadata *) psMemIncrRefCounter(sf->recMask->header);
                    sf->recChMask->image = (psImage *) psMemIncrRefCounter(sf->recMask->image);
                }
            }

            copyFitsOptions(sf->outMask, sf->recMask, sf->inMask, sf->tiles);
            if (sf->outChMask) {
                copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask, sf->tiles);
            }
        }
    }

    if (sf->inWeight) {
        readImage(sf->inWeight, sf->extnum, sf->stage, false);
        sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
        if (sf->recWeight) {
            sf->recWeight->header = psMetadataCopy(NULL, sf->outWeight->header);
            addRecoveryKeyword(sf->recWeight->header);
        }
        addDestreakKeyword(sf->outWeight->header);
        if (updateAstrometry) {
            pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
        }
        setupImageRefs(sf->outWeight, sf->recWeight, sf->inWeight, sf->extnum, exciseAll);

        copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight, sf->tiles);
    }

    return true;
}

static void
writeImages(streakFiles *sf, bool exciseImageCube)
{
    psString extname = NULL;
    if (sf->nHDU > 1) {
        bool mdok;
        extname = psMetadataLookupStr(&mdok, sf->inImage->header, "EXTNAME");
    }
    if (sf->inImage->numZPlanes == 0)  {
        // note exciseing complete images is handled in readAndCopyToOutput
        writeImage(sf->outImage, extname, sf->extnum);
        writeImage(sf->recImage, extname, sf->extnum);
    } else {
        // we have an image cube
        double initValue;
        if (exciseImageCube) {
            // copy the entire input image to the recovery image
            writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
            initValue = NAN;
        } else {
            // otherwise write it to the output
            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
            initValue = 0;
        }

        // borrow one of the images from the imagecube and set it to init value
        psImage *image = psArrayGet (sf->inImage->imagecube, 0);
        psMemIncrRefCounter(image);
        psImageInit(image, initValue);
        if (exciseImageCube) {
            sf->outImage->image = image;
            writeImage(sf->outImage, extname, sf->extnum);
        } else {
            // write zero valued image to reccovery
            if (sf->recImage) {
                sf->recImage->image = image;
                writeImage(sf->recImage, extname, sf->extnum);
            }
        }
        // XXX: check this
        // Assumption: there are no mask and weight images for video cells
        return;
    }
    if (sf->outMask) {
        writeImage(sf->outMask, extname, sf->extnum);
        writeImage(sf->recMask, extname, sf->extnum);
        if (sf->stage == IPP_STAGE_CHIP) {
            writeImage(sf->outChMask, extname, sf->extnum);
            writeImage(sf->recChMask, extname, sf->extnum);
        }
    }
    if (sf->outWeight) {
        writeImage(sf->outWeight, extname, sf->extnum);
        writeImage(sf->recWeight, extname, sf->extnum);
    }
}

static void
excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue)
{
    double exciseValue = sfiles->inImage->exciseValue;

    // we clip so that the streak calculation code doesn't have to
    if ((x < 0) || (x >= sfiles->inImage->numCols) || (y < 0) || (y >= sfiles->inImage->numRows)) {
        return;
    }

    float imageValue  = sfiles->inImage->image->data.F32[y][x];
    if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
        sfiles->recImage->image->data.F32[y][x] = imageValue;
    }

    if (sfiles->transparentStreaks == 0) {
        sfiles->outImage->image->data.F32[y][x] = exciseValue;
    } else {
        if (streak) {
            // as a visualization aid don't mask the pixel, just change the intensity
            sfiles->outImage->image->data.F32[y][x] = imageValue + sfiles->transparentStreaks;
        } else {
            sfiles->outImage->image->data.F32[y][x] = exciseValue;
        }
    }

    if (sfiles->outWeight) {
        if (sfiles->recWeight) {
            sfiles->recWeight->image->data.F32[y][x] = sfiles->inWeight->image->data.F32[y][x];
        }
        // Assume that weight images are always a floating point type
        sfiles->outWeight->image->data.F32[y][x] = NAN;
    }
    if (sfiles->outMask) {
        if (sfiles->recMask) {
            sfiles->recMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] =
                sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x];
        }
        sfiles->outMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] =
            sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] | newMaskValue;
    }
}

static void
exciseNonWarpedPixels(streakFiles *sfiles, psImageMaskType newMaskValue)
{
    int cell_x0 = sfiles->astrom->cell_x0;
    int cell_y0 = sfiles->astrom->cell_y0;
    int xParity = sfiles->astrom->xParity;
    int yParity = sfiles->astrom->yParity;
    int numCols = sfiles->inImage->numCols; // for raw images this was calculated from the width of datasec
    int numRows = sfiles->inImage->numRows; // for raw images this was calculated from the height of datasec

//    printf("%2d x0: %4d y0: %4d xpar: %d ypar: %d\n", sfiles->extnum, cell_x0, cell_y0, xParity, yParity);

    // XXX: in the case of raw images we are making an implicit assumption here
    // that the DATASEC begins at (x, y) = (0, 0) in the array for this image
    // While this is true for GPC1 it isn't necessarily true
    for (int yCell = 0; yCell < numRows; yCell++) {
        int yChip;
        if (yParity == 1) {
            yChip = cell_y0 + yCell;
        } else {
            yChip = cell_y0 - yCell;
        }

        psU8 *pixels = sfiles->warpedPixels->data.U8[yChip];

        if (xParity == 1) {
            pixels += cell_x0;
            for (int xCell = 0; xCell < numCols; xCell++, pixels++) {
                if (! *pixels ) {
                    excisePixel(sfiles, xCell, yCell, false, newMaskValue);
                }
            }
        } else {
            // negative parity
            // point to the right most pixel in chip coordinates (lowest in cell coords)
            pixels += cell_x0 - numCols;
            for (int xCell = numCols - 1; xCell >= 0 ; xCell--, pixels++) {
                if (!*pixels) {
                    excisePixel(sfiles, xCell, yCell, false, newMaskValue);
                }
            }
        }
    }
}

static bool
warpedPixel(streakFiles *sfiles, int x, int y)
{
    PixelPos chipCoord;

    if (!CHIP_LEVEL_INPUT(sfiles->stage)) {
        // if we're here on a skycell image by definition this pixel was warped
        return true;
    }

    // we clip so that the streak calculation code doesn't have to
    // clipping here insures that we don't touch the overscan regions
    if ((x < 0) || (x >= sfiles->inImage->numCols) ||
        (y < 0) || (y >= sfiles->inImage->numRows)) {
        return false;
    }

    cellToChipInt(&chipCoord.x, &chipCoord.y, sfiles->astrom, x, y);

    if (chipCoord.x < 0 || chipCoord.x >= sfiles->warpedPixels->numCols) {
        return false;
    }
    if (chipCoord.y < 0 || chipCoord.y >= sfiles->warpedPixels->numRows) {
        return false;
    }

    return psImageGet(sfiles->warpedPixels, chipCoord.x, chipCoord.y) ? true : false;
}

// read a sources file (.cmf) and remove any rows whose coordinate is convered by a
// streak mask
static void
censorSources(streakFiles *sfiles, psImageMaskType maskStreak)
{
    if ((!sfiles->inSources) || (!sfiles->outMask)) {
        return;
    }
    psImage *maskImage = sfiles->outMask->image;
    if (!maskImage) {
        psError(PS_ERR_IO, false, "maskImage is null!");
        streaksExit("", PS_EXIT_PROG_ERROR);
    }

    sFile *in = sfiles->inSources;
    sFile *out = sfiles->outSources;


    // Primary header, should be "something.hdr"
    {
        psMetadata *header = psFitsReadHeader(NULL, in->fits);
        if (!header) {
            psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }

        bool status;
        psString extname = psMetadataLookupStr(&status, header, "EXTNAME");
        if (!extname) {
            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }
        addDestreakKeyword(header);

        if (!psFitsWriteBlank(out->fits, header, extname)) {
            psError(PS_ERR_IO, false, "failed to write blank in header of %s", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }
        psFree(header);
    }

    // Extension with PSF fits, should be "something.psf"
    {
        if (!psFitsMoveExtNum(in->fits, 1, true)) {
            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }

        psMetadata *header = psFitsReadHeader(NULL, in->fits);
        if (!header) {
            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }
        psString extname = psMetadataLookupStr(NULL, header, "EXTNAME");
        if (!extname) {
            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }

        psArray *inTable = psFitsReadTable(in->fits);
        if (!inTable->n) {
            psErrorStackPrint(stderr, "table in %s is empty", in->resolved_name);
            streaksExit("", PS_EXIT_DATA_ERROR);
        }

        psArray *outTable = psArrayAllocEmpty(inTable->n);
        int j = 0;
        int numCensored = 0;
        for (int i = 0 ; i < inTable->n; i++) {
            psMetadata *row = inTable->data[i];

            psF32 x = psMetadataLookupF32(NULL, row, "X_PSF");
            psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF");

            psImageMaskType mask;
            if ((x >= maskImage->numCols) || (y >= maskImage->numRows) || (x <  0) || (y < 0)) {
                mask = maskStreak;
            } else {
                mask = maskImage->data.PS_TYPE_IMAGE_MASK_DATA[(int)y][(int)x];
            }

            // Key the source if the center pixel is not masked with maskStreak
            if (!(mask & maskStreak) ) {
                psArraySet(outTable, j++, row);
            } else {
                numCensored++;
            }
        }

        // get rid of unused elements (don't know if this is necessary)
        psArrayRealloc(outTable, j);

        addDestreakKeyword(header);
        if (psArrayLength(outTable) > 0) {
            printf("Censored %d sources\n", numCensored);
            if (! psFitsWriteTable(out->fits, header, outTable, extname)) {
                psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
                streaksExit("", PS_EXIT_DATA_ERROR);
            }
        } else {
            printf("Censored ALL %d sources\n", numCensored);
            if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) {
                psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name);
                streaksExit("", PS_EXIT_DATA_ERROR);
            }
        }
        psFree(header);
        psFree(outTable);
        psFree(inTable);
    }

    // XXX Will need to update to handle extension with extended sources, etc.

    if (!psFitsClose(out->fits)) {
        psErrorStackPrint(stderr, "failed to close table %s", out->resolved_name);
        streaksExit("", PS_EXIT_DATA_ERROR);
    }
}
