/** @file censorReadout.c
 *
 *  @brief reject detections that land within the streak mask regions
 *
 *  @ingroup censor
 *
 *  @author IfA
 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-07 02:03:34 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

# include "censor.h"

bool censorReadout (pmReadout *readout, pmConfig *config, const pmFPAview *view, bool badMaskFile) {

    bool status;

    // if there is no mask file for this readout drop all sources
    if (badMaskFile) {
      psMetadataRemoveKey (readout->analysis, "PSPHOT.DETECTIONS");
      return true;
    }

    // select the current recipe
    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, CENSOR_RECIPE);
    if (!recipe) {
        psError(CENSOR_ERR_CONFIG, true, "Can't find CENSOR recipe");
        return false;
    }

    psImageMaskType streakMaskValue = pmConfigMaskGet("STREAK", config); // Mask value for streak pixels

    psLogMsg ("censor", PS_LOG_INFO, "generating a bright-star mask");

    // select the reference mask fpa :: we use this to determine cell boundaries
    pmFPAfile *inMask = psMetadataLookupPtr (NULL, config->files, "CENSOR.INPUT.MASK");
    if (!inMask) {
        psError(CENSOR_ERR_CONFIG, true, "Can't find mask files");
        return false;
    }

    // XXX why not do this?
    pmReadout *readoutMask = pmFPAviewThisReadout (view, inMask->fpa);
    if (!readoutMask) {
        psError(CENSOR_ERR_CONFIG, true, "Can't find mask for this readout");
        return false;
    }

    // select the raw objects for this readout, flag is they fall in a mask
    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    if (detections == NULL) {
        psError(CENSOR_ERR_CONFIG, true, "Can't find detections for this readout");
        return false;
    }
    psArray *inSources = detections->allSources;
    psAssert (inSources, "missing sources?");

    // create a replacement output array:
    psArray *outSources = psArrayAllocEmpty(100);

    // only keep sources that do not match the specified mask bit
    for (int i = 0; i < inSources->n; i++) {
        pmSource *source = inSources->data[i];

        int xChip = source->peak->x;
        int yChip = source->peak->y;

        bool onChip = true;
        onChip &= (xChip >= 0);
        onChip &= (xChip < readoutMask->mask->numCols);
        onChip &= (yChip >= 0);
        onChip &= (yChip < readoutMask->mask->numRows);

        if (onChip) {
            psImageMaskType value = readoutMask->mask->data.PS_TYPE_IMAGE_MASK_DATA[yChip][xChip];
            if (value & streakMaskValue) {
                // skip sources on streak masks
                continue;
            }
        }
        psArrayAdd (outSources, 100, source);
    }

    psLogMsg ("censor", PS_LOG_INFO, "keeping %ld of %ld sources", outSources->n, inSources->n);

    psFree (detections->allSources);
    detections->allSources = outSources;

    return true;
}

