IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 27, 2007, 10:55:23 AM (19 years ago)
Author:
Paul Price
Message:

Adding function pmSubtractionKernelsOptimumISIS, which derives an optimum set of kernels for ISIS (and GUNK). Its use necessitated a few API changes to the other parts of the image subtraction.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/imcombine/pmSubtractionMatch.c

    r14606 r14671  
    99#include "pmHDU.h"
    1010#include "pmFPA.h"
     11#include "pmSubtractionParams.h"
    1112#include "pmSubtractionKernels.h"
    1213#include "pmSubtractionStamps.h"
     
    4445
    4546
     47static bool getStamps(psArray **stamps, // Stamps to read
     48                      const psArray *stampsData, // Stamp data from a file
     49                      const pmReadout *reference, // Reference readout
     50                      const pmReadout *input, // Input readout, or NULL to generate fake stamps
     51                      const psImage *subMask, // Mask for subtraction, or NULL
     52                      psImage *weight,  // Weight map
     53                      const psRegion *region, // Region of interest, or NULL
     54                      float threshold,  // Threshold for stamp finding
     55                      float stampSpacing, // Spacing between stamps
     56                      float targetWidth,// Target width for fake stamps
     57                      int size,         // Kernel half-size
     58                      int footprint     // Convolution footprint for stamps
     59    )
     60{
     61
     62    psImage *inImage = NULL; // Input image
     63    if (input) {
     64        inImage = input->image;
     65    }
     66
     67    if (stampsData) {
     68        psVector *xStamp = NULL, *yStamp = NULL, *fluxStamp = NULL; // Stamp positions and fluxes
     69        if (input) {
     70            // We have x, y because the target is provided by the input image
     71            xStamp = stampsData->data[0];
     72            yStamp = stampsData->data[1];
     73        } else {
     74            // We have x, y and flux in order to generate a target
     75            xStamp = stampsData->data[0];
     76            yStamp = stampsData->data[1];
     77            fluxStamp = stampsData->data[2];
     78        }
     79
     80        psFree(*stamps);
     81        *stamps = pmSubtractionSetStamps(xStamp, yStamp, fluxStamp, subMask, region);
     82    } else {
     83        psTrace("psModules.imcombine", 3, "Finding stamps...\n");
     84        *stamps = pmSubtractionFindStamps(*stamps, reference->image, subMask, region,
     85                                          threshold, stampSpacing);
     86    }
     87    if (!stamps) {
     88        psError(PS_ERR_UNKNOWN, false, "Unable to find stamps.");
     89        return false;
     90    }
     91
     92    memCheck("  find stamps");
     93
     94    if (!input && !pmSubtractionGenerateStamps(*stamps, targetWidth, footprint, size)) {
     95        psError(PS_ERR_UNKNOWN, false, "Unable to generate target stamps.");
     96        return false;
     97    }
     98
     99    psTrace("psModules.imcombine", 3, "Extracting stamps...\n");
     100    if (!pmSubtractionExtractStamps(*stamps, reference->image, inImage, weight, footprint, size)) {
     101        psError(PS_ERR_UNKNOWN, false, "Unable to extract stamps.");
     102        return false;
     103    }
     104
     105    memCheck("   extract stamps");
     106
     107    return true;
     108}
     109
     110
     111
     112
     113
    46114bool pmSubtractionMatch(pmReadout *convolved, const pmReadout *reference, const pmReadout *input,
    47115                        int footprint, float regionSize, float stampSpacing, float threshold,
    48116                        const char *stampsName, float targetWidth, pmSubtractionKernelsType type,
    49                         int size, int order, const psVector *isisWidths, const psVector *isisOrders,
    50                         int inner, int ringsOrder, int binning, int iter, float rej, psMaskType maskBad,
     117                        int size, int spatialOrder, const psVector *isisWidths, const psVector *isisOrders,
     118                        int inner, int ringsOrder, int binning, bool optimum, psVector *optFWHMs,
     119                        int optOrder, float optThreshold, int iter, float rej, psMaskType maskBad,
    51120                        psMaskType maskBlank)
    52121{
     
    88157    // We'll check kernel type when we allocate the kernels
    89158    PS_ASSERT_INT_POSITIVE(size, false);
    90     PS_ASSERT_INT_NONNEGATIVE(order, false);
     159    PS_ASSERT_INT_NONNEGATIVE(spatialOrder, false);
    91160    if (isisWidths || isisOrders) {
    92161        PS_ASSERT_VECTOR_NON_NULL(isisWidths, false);
     
    99168    PS_ASSERT_INT_NONNEGATIVE(ringsOrder, false);
    100169    PS_ASSERT_INT_POSITIVE(binning, false);
     170    if (optimum) {
     171        PS_ASSERT_VECTOR_NON_NULL(optFWHMs, false);
     172        PS_ASSERT_INT_NONNEGATIVE(optOrder, false);
     173        PS_ASSERT_FLOAT_LARGER_THAN(optThreshold, 0.0, false);
     174        PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(optThreshold, 1.0, false);
     175    }
    101176    PS_ASSERT_INT_POSITIVE(iter, false);
    102177    PS_ASSERT_FLOAT_LARGER_THAN(rej, 0.0, false);
     
    136211    }
    137212
     213    // Read stamps from file
     214    psArray *stampsData = NULL;         // Stamps data read from file
     215    if (stampsName && strlen(stampsName) > 0) {
     216        psTrace("psModules.imcombine", 3, "Reading stamps from %s...\n", stampsName);
     217        psVector *xStamp = NULL, *yStamp = NULL; // Stamp positions and fluxes
     218        if (input) {
     219            // We have x, y because the target is provided by the input image
     220            stampsData = psVectorsReadFromFile(stampsName, "%f %f"); // Stamp positions
     221        } else {
     222            // We have x, y and flux in order to generate a target
     223            stampsData = psVectorsReadFromFile(stampsName, "%f %f %f"); // Stamp positions
     224        }
     225
     226        // Correct for IRAF/FITS (unit-offset) positions to C (zero-offset) positions
     227        xStamp = stampsData->data[0];
     228        yStamp = stampsData->data[1];
     229        psBinaryOp(xStamp, xStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
     230        psBinaryOp(yStamp, yStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
     231    }
     232
    138233    int numCols = reference->image->numCols, numRows = reference->image->numRows; // Image dimensions
    139234
     
    145240    memCheck("mask");
    146241
    147     // Kernel basis functions
    148     pmSubtractionKernels *kernels = pmSubtractionKernelsGenerate(type, size, order, isisWidths, isisOrders,
    149                                                                  inner, binning, ringsOrder);
     242
     243    psArray *stamps = NULL;             // Stamps for matching PSF
     244
     245    pmSubtractionKernels *kernels = NULL; // Kernel basis functions
     246    if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) {
     247        if (!getStamps(&stamps, stampsData, reference, input, subMask, weight, NULL,
     248                       threshold, stampSpacing, targetWidth, size, footprint)) {
     249            goto ERROR;
     250        }
     251        kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder, optFWHMs, optOrder,
     252                                                  stamps, footprint, optThreshold);
     253        // XXX This is not quite the best thing to do here--- we'll find the same set of stamps again later,
     254        // but since we may not be interested in the entire image on the first pass through, we have to blow
     255        // the whole lot away.  The alternative is to mark stamps that are outside the region of interest, and
     256        // ignore them when generating sums and rejecting.
     257        psFree(stamps);
     258        stamps = NULL;
     259
     260        if (!kernels) {
     261            psErrorClear();
     262            psWarning("Unable to derive optimum ISIS kernel --- switching to default.");
     263        }
     264    }
     265
     266    if (kernels == NULL) {
     267        // Not an ISIS/GUNK kernel, or the optimum kernel search failed
     268        kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders,
     269                                               inner, binning, ringsOrder);
     270    }
     271
    150272    psMetadataAddPtr(convolved->analysis, PS_LIST_TAIL, "SUBTRACTION.KERNEL", PS_DATA_UNKNOWN,
    151273                     "Subtraction kernels", kernels);
    152     psArray *stamps = NULL;             // Stamps for matching PSF
    153274    psVector *solution = NULL;          // Solution to match PSF
    154275
     
    186307                psTrace("psModules.imcombine", 2, "Iteration %d...\n", k);
    187308
    188                 if (stampsName && strlen(stampsName) > 0) {
    189                     psTrace("psModules.imcombine", 3, "Reading stamps from %s...\n", stampsName);
    190                     iter = 1;           // There is no iterating because we use all the stamps we have
    191                     psVector *xStamp = NULL, *yStamp = NULL, *fluxStamp = NULL; // Stamp positions and fluxes
    192                     if (input) {
    193                         // We have x, y because the target is provided by the input image
    194                         psArray *stampData = psVectorsReadFromFile(stampsName, "%f %f"); // Stamp positions
    195                         xStamp = stampData->data[0];
    196                         yStamp = stampData->data[1];
    197                     } else {
    198                         // We have x, y and flux in order to generate a target
    199                         psArray *stampData = psVectorsReadFromFile(stampsName, "%f %f %f"); // Stamp positions
    200                         xStamp = stampData->data[0];
    201                         yStamp = stampData->data[1];
    202                         fluxStamp = stampData->data[2];
    203                     }
    204 
    205                     // Correct for IRAF/FITS (unit-offset) positions to C (zero-offset) positions
    206                     psBinaryOp(xStamp, xStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
    207                     psBinaryOp(yStamp, yStamp, "-", psScalarAlloc(1.0, PS_TYPE_F32));
    208 
    209                     stamps = pmSubtractionSetStamps(xStamp, yStamp, fluxStamp, subMask, region);
    210                 } else {
    211                     psTrace("psModules.imcombine", 3, "Finding stamps...\n");
    212                     stamps = pmSubtractionFindStamps(stamps, reference->image, subMask, region,
    213                                                      threshold, stampSpacing);
    214                 }
    215                 if (!stamps) {
    216                     psError(PS_ERR_UNKNOWN, false, "Unable to find stamps.");
     309                if (!getStamps(&stamps, stampsData, reference, input, subMask, weight, region,
     310                               threshold, stampSpacing, targetWidth, size, footprint)) {
    217311                    goto ERROR;
    218312                }
    219 
    220                 memCheck("  find stamps");
    221 
    222                 if (!input && !pmSubtractionGenerateStamps(stamps, targetWidth, footprint, kernels)) {
    223                     psError(PS_ERR_UNKNOWN, false, "Unable to generate target stamps.");
    224                     goto ERROR;
    225                 }
    226 
    227                 psTrace("psModules.imcombine", 3, "Extracting stamps...\n");
    228                 if (!pmSubtractionExtractStamps(stamps, reference->image, inImage, weight,
    229                                                 footprint, kernels)) {
    230                     psError(PS_ERR_UNKNOWN, false, "Unable to extract stamps.");
    231                     goto ERROR;
    232                 }
    233 
    234                 memCheck("   extract stamps");
    235313
    236314                psTrace("psModules.imcombine", 3, "Calculating equation...\n");
     
    376454    psFree(subMask);
    377455    subMask = NULL;
     456    psFree(stampsData);
     457    stampsData = NULL;
    378458
    379459    if (!pmSubtractionBorder(convolved->image, convolved->weight, convolved->mask, kernels, maskBlank)) {
     
    395475    psFree(stamps);
    396476    psFree(solution);
     477    psFree(stampsData);
    397478    return false;
    398479}
Note: See TracChangeset for help on using the changeset viewer.