IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 6, 2010, 8:02:38 PM (16 years ago)
Author:
eugene
Message:

working on psphotStack

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/psphot.20100506/src/psphotStackMatchPSFs.c

    r27850 r27876  
    11# include "psphotInternal.h"
    22
    3 bool psphotStackMatchPSFs (pmConfig *config, const pmFPAview *view, bool firstPass)
     3bool psphotStackMatchPSFs (pmConfig *config, const pmFPAview *view)
    44{
    55    bool status = true;
     6
     7    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, "PSPHOT");
     8    psAssert(recipe, "We've thrown an error on this before.");
    69
    710    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
    811    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
    912
    10     // loop over the available readouts
     13    // 'options' carries info needed to perform the stack matching
     14    psphotStackOptions *options = psphotStackOptionsAlloc(num);
     15
     16    options->convolve = psMetadataLookupBool (&status, recipe, "PSPHOT.STACK.MATCH.PSF");
     17    psAssert (status, "PSPHOT.STACK.MATCH.PSF not in recipe");
     18
     19    if (options->convolve) {
     20        char *convolveSource = psMetadataLookupStr (&status, recipe, "PSPHOT.STACK.MATCH.PSF.SOURCE");
     21        options->convolveSource = psphotStackConvolveSourceFromString (convolveSource);
     22        if (options->convolveSource == PSPHOT_CNV_SRC_NONE) {
     23            psError (PSPHOT_ERR_CONFIG, true, "stack convolution source not defined in recipe");
     24            return false;
     25        }
     26    }
     27
     28    // loop over the available readouts (ignore chisq image)
    1129    for (int i = 0; i < num; i++) {
    12         if (!psphotStackMatchPSFsReadout (config, view, i)) {
     30        if (!psphotStackMatchPSFsPrepare (config, view, options, i)) {
     31            psError (PSPHOT_ERR_CONFIG, false, "failed to set PSF matching options for entry %d", i);
     32            return false;
     33        }
     34    }
     35
     36    // Generate target PSF
     37    if (options->convolve) {
     38        options->psf = ppStackPSF(config, options->numCols, options->numRows, options->psfs, options->inputMask);
     39        if (!options->psf) {
     40            psError(psErrorCodeLast(), false, "Unable to determine output PSF.");
     41            return false;
     42        }
     43        psMetadataAddPtr(config->arguments, PS_LIST_TAIL, "PSF.TARGET", PS_DATA_UNKNOWN, "Target PSF for stack", options->psf);
     44        options->targetSeeing = pmPSFtoFWHM(options->psf, 0.5 * options->numCols, 0.5 * options->numRows); // FWHM for target
     45        psLogMsg("psphotStack", PS_LOG_INFO, "Target seeing FWHM: %f\n", options->targetSeeing);
     46
     47        // XXX is this needed to supply the psf to psphot??
     48        // pmChip *outChip = pmFPAfileThisChip(config->files, view, "PPSTACK.TARGET.PSF"); // Output chip
     49        // psMetadataAddPtr(outChip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN, "Target PSF", options->psf);
     50        // outChip->data_exists = true;
     51    }
     52
     53    // loop over the available readouts (ignore chisq image)
     54    for (int i = 0; i < num; i++) {
     55        if (!psphotStackMatchPSFsReadout (config, view, options, i)) {
    1356            psError (PSPHOT_ERR_CONFIG, false, "failed to find initial detections for PSPHOT.INPUT entry %d", i);
    1457            return false;
     
    1962
    2063// convolve the image to match desired PSF
    21 bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, int index) {
    22 
    23     bool status;
    24     int pass;
    25     float NSIGMA_PEAK = 25.0;
    26     int NMAX = 0;
     64bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index) {
    2765
    2866    // find the currently selected readout
    29     pmFPAfile *fileRaw = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", index); // File of interest
     67    pmFPAfile *fileRaw = pmFPAfileSelectSingle(config->files, "PSPHOT.STACK.INPUT.RAW", index); // File of interest
    3068    psAssert (fileRaw, "missing file?");
    3169
    32     pmFPAfile *fileCnv = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT.CONV", index); // File of interest
     70    pmFPAfile *fileCnv = pmFPAfileSelectSingle(config->files, "PSPHOT.STACK.INPUT.CNV", index); // File of interest
    3371    psAssert (fileCnv, "missing file?");
    3472
     
    3977    psAssert (readoutCnv, "missing readout?");
    4078
    41     // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
    42     psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
    43     psAssert (maskVal, "missing mask value?");
    44 
    45     /***** set up recipe options *****/
    46 
    47     psMetadata *stackRecipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
    48     psAssert(stackRecipe, "We've thrown an error on this before.");
    49 
    50     // Look up appropriate values from the ppSub recipe
    51     psMetadata *subRecipe = psMetadataLookupMetadata(NULL, config->recipes, "PPSUB"); // PPSUB recipe
    52     psAssert(subRecipe, "recipe missing");
    53 
    54     int size = psMetadataLookupS32(NULL, subRecipe, "KERNEL.SIZE"); // Kernel half-size
    55 
    56     float deconvLimit = psMetadataLookupF32(NULL, stackRecipe, "DECONV.LIMIT"); // Limit on deconvolution fraction
    57 
    58     psString maskValStr = psMetadataLookupStr(NULL, subRecipe, "MASK.VAL"); // Name of bits to mask going in
    59     psImageMaskType maskVal = pmConfigMaskGet(maskValStr, config); // Bits to mask going in to pmSubtractionMatch
    60     psString maskPoorStr = psMetadataLookupStr(NULL, stackRecipe, "MASK.POOR"); // Name of bits to mask for poor
    61     psImageMaskType maskPoor = pmConfigMaskGet(maskPoorStr, config); // Bits to mask for poor pixels
    62     psString maskBadStr = psMetadataLookupStr(NULL, stackRecipe, "MASK.BAD"); // Name of bits to mask for bad
    63     psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
    64 
    65     bool mdok;                          // Status of MD lookup
    66     float penalty = psMetadataLookupF32(NULL, subRecipe, "PENALTY"); // Penalty for wideness
    67     int threads = psMetadataLookupS32(NULL, config->arguments, "-threads"); // Number of threads
    68 
    69     if (!pmReadoutMaskNonfinite(readout, maskVal)) {
     79    // set NAN pixels to 'SAT'
     80    psImageMaskType maskVal = pmConfigMaskGet("SAT", config);
     81    if (!pmReadoutMaskNonfinite(readoutRaw, maskVal)) {
    7082        psError(psErrorCodeLast(), false, "Unable to mask non-finite pixels in readout.");
    7183        return false;
     
    7486    // Image Matching (PSFs or just flux)
    7587    if (options->convolve) {
    76       // Full match of PSFs
    77         pmReadout *conv = pmReadoutAlloc(NULL); // Conv readout, for holding results temporarily
    78 
    79         // Read previously produced kernel
    80         if (psMetadataLookupBool(NULL, config->arguments, "PPSTACK.DEBUG.STACK")) {
    81           loadKernel();
    82         } else {
    83           matchKernel();
    84         } // !DEBUG.STACK
    85 
    86         saveMatchData();
    87 
    88         saveChiSquare();
    89 
    90         renormKernel();
    91 
    92         // Reject image completely if the maximum deconvolution fraction exceeds the limit
    93         float deconv = psMetadataLookupF32(NULL, conv->analysis, PM_SUBTRACTION_ANALYSIS_DECONV_MAX); // Max deconvolution fraction
    94         if (deconv > deconvLimit) {
    95             psWarning("Maximum deconvolution fraction (%f) exceeds limit (%f) --- rejecting image %d\n", deconv, deconvLimit, index);
    96             psFree(conv);
    97             return NULL;
    98         }
    99 
    100         readout->analysis = psMetadataCopy(readout->analysis, conv->analysis);
    101 
    102         psFree(conv);
     88        matchKernel(config, readoutCnv, readoutRaw, options, index);
     89        saveMatchData(readoutCnv, options, index);
     90        // renormKernel(readoutCnv, options, index);
    10391    } else {
    104         // only match the flux
    105         float norm = powf(10.0, -0.4 * options->norm->data.F32[index]); // Normalisation
    106         psBinaryOp(readout->image, readout->image, "*", psScalarAlloc(norm, PS_TYPE_F32));
    107         psBinaryOp(readout->variance, readout->variance, "*", psScalarAlloc(PS_SQR(norm), PS_TYPE_F32));
     92        // only match the flux (NO! not for multi-filter, at least!)
     93        // XXX do not generate readoutCnv in this case?
     94        // float norm = powf(10.0, -0.4 * options->norm->data.F32[index]); // Normalisation
     95        // psBinaryOp(readoutRaw->image, readoutRaw->image, "*", psScalarAlloc(norm, PS_TYPE_F32));
     96        // psBinaryOp(readoutRaw->variance, readoutRaw->variance, "*", psScalarAlloc(PS_SQR(norm), PS_TYPE_F32));
    10897    }
    10998
    110     // Ensure the background value is zero
    111     psStats *bg = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV); // Statistics for background
    112     psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS); // Random number generator
    113     if (!psImageBackground(bg, NULL, readout->image, readout->mask, maskVal | maskBad, rng)) {
    114         psWarning("Can't measure background for image.");
    115         psErrorClear();
    116     } else {
    117         if (!psMetadataLookupBool(NULL, config->arguments, "PPSTACK.SKIP.BG.SUB")) {
    118             psLogMsg("ppStack", PS_LOG_INFO, "Correcting convolved image background by %lf (+/- %lf)",
    119                      psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), psStatsGetValue(bg, PS_STAT_ROBUST_STDEV));
    120             (void)psBinaryOp(readout->image, readout->image, "-",
    121                              psScalarAlloc(psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), PS_TYPE_F32));
    122         }
    123     }
     99    rescaleData(readoutCnv, config, options, index);
    124100
    125     if (!stackRenormaliseReadout(config, readout)) {
    126         psFree(rng);
    127         psFree(bg);
    128         return false;
    129     }
    130 
    131     // Measure the variance level for the weighting
    132     if (psMetadataLookupBool(NULL, stackRecipe, "WEIGHTS")) {
    133         if (!psImageBackground(bg, NULL, readout->variance, readout->mask, maskVal | maskBad, rng)) {
    134             psError(PPSTACK_ERR_DATA, false, "Can't measure mean variance for image.");
    135             psFree(rng);
    136             psFree(bg);
    137             return false;
    138         }
    139         options->weightings->data.F32[index] = 1.0 / (psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN) * psImageCovarianceFactor(readout->covariance));
    140     } else {
    141         options->weightings->data.F32[index] = 1.0;
    142     }
    143     psLogMsg("ppStack", PS_LOG_INFO, "Weighting for image %d is %f\n",
    144              index, options->weightings->data.F32[index]);
    145 
    146     psFree(rng);
    147     psFree(bg);
    148 
    149     dumpImage3();
     101    dumpImage(readoutCnv, readoutRaw, index, "convolved");
    150102
    151103    return true;
    152104}
     105
     106
     107# if (0)
     108// Read previously produced kernel
     109if (psMetadataLookupBool(NULL, config->arguments, "PPSTACK.DEBUG.STACK")) {
     110    loadKernel(config, readoutCnv, options, index);
     111} else {
     112    matchKernel(config, readoutCnv, readoutRaw, options, index);
     113}
     114# endif
Note: See TracChangeset for help on using the changeset viewer.