IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 31, 2010, 5:00:42 PM (16 years ago)
Author:
eugene
Message:

updates relative to 20091201, fixes for all psphot variants

Location:
branches/eam_branches/psModules.stack.20100120
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/psModules.stack.20100120

  • branches/eam_branches/psModules.stack.20100120/src/imcombine/pmSubtractionStamps.c

    r26562 r26747  
    176176
    177177pmSubtractionStampList *pmSubtractionStampListAlloc(int numCols, int numRows, const psRegion *region,
    178                                                     int footprint, float spacing, float sysErr, float skyErr)
     178                                                    int footprint, float spacing, float normFrac,
     179                                                    float sysErr, float skyErr)
    179180{
    180181    pmSubtractionStampList *list = psAlloc(sizeof(pmSubtractionStampList)); // Stamp list to return
     
    217218    list->flux = NULL;
    218219    list->window = NULL;
     220    list->normFrac = normFrac;
     221    list->normWindow = 0;
    219222    list->footprint = footprint;
    220223    list->sysErr = sysErr;
     
    239242    out->window = psMemIncrRefCounter(in->window);
    240243    out->footprint = in->footprint;
     244    out->normWindow = in->normWindow;
    241245
    242246    for (int i = 0; i < num; i++) {
     
    308312    stamp->matrix = NULL;
    309313    stamp->vector = NULL;
     314    stamp->norm = NAN;
    310315
    311316    return stamp;
     
    316321                                                const psImage *image2, const psImage *subMask,
    317322                                                const psRegion *region, float thresh1, float thresh2,
    318                                                 int size, int footprint, float spacing, float sysErr, float skyErr,
    319                                                 pmSubtractionMode mode)
     323                                                int size, int footprint, float spacing, float normFrac,
     324                                                float sysErr, float skyErr, pmSubtractionMode mode)
    320325{
    321326    if (!image1 && !image2) {
     
    373378
    374379    if (!stamps) {
    375         stamps = pmSubtractionStampListAlloc(numCols, numRows, region, footprint, spacing, sysErr, skyErr);
     380        stamps = pmSubtractionStampListAlloc(numCols, numRows, region, footprint, spacing,
     381                                             normFrac, sysErr, skyErr);
    376382    }
    377383
     
    407413                // Take stamps off the top of the (sorted) list
    408414                for (int j = xList->n - 1; j >= 0 && !goodStamp; j--) {
    409                     int xCentre = xList->data.F32[j] + 0.5, yCentre = yList->data.F32[j] + 0.5;// Stamp centre
    410 
    411415                    // Chop off the top of the list
    412416                    xList->n = j;
     
    414418                    fluxList->n = j;
    415419
     420#if 0
    416421                    // Fish around a bit to see if we can find a pixel that isn't masked
     422                    // This is not a good idea if we're using the window feature
    417423                    psTrace("psModules.imcombine", 7, "Searching for stamp %d around %d,%d\n",
    418424                            i, xCentre, yCentre);
    419425
    420426                    // Search bounds
     427                    int xCentre = xList->data.F32[j] + 0.5, yCentre = yList->data.F32[j] + 0.5;// Stamp centre
    421428                    int search = footprint - size; // Search radius
    422429                    int xMin = PS_MAX(border, xCentre - search);
     
    427434                    goodStamp = stampSearch(&xStamp, &yStamp, &fluxStamp, image1, image2, thresh1, thresh2,
    428435                                            subMask, xMin, xMax, yMin, yMax, numCols, numRows, border);
    429 
    430                     // XXX reset for a test:
    431                     xStamp = xList->data.F32[j];
    432                     yStamp = yList->data.F32[j];
     436#else
     437                    // Only search the exact centre pixel
     438                    goodStamp = stampSearch(&xStamp, &yStamp, &fluxStamp, image1, image2, thresh1, thresh2,
     439                                            subMask, xList->data.F32[j], xList->data.F32[j],
     440                                            yList->data.F32[j], yList->data.F32[j], numCols, numRows, border);
     441#endif
    433442                    // fprintf (stderr, "find: %d %d ==> %5.1f %5.1f\n", xCentre, yCentre, xStamp, yStamp);
    434443                }
     
    486495                                               const psImage *image, const psImage *subMask,
    487496                                               const psRegion *region, int size, int footprint,
    488                                                float spacing, float sysErr, float skyErr, pmSubtractionMode mode)
     497                                               float spacing, float normFrac, float sysErr, float skyErr,
     498                                               pmSubtractionMode mode)
    489499
    490500{
     
    507517    pmSubtractionStampList *stamps = pmSubtractionStampListAlloc(subMask->numCols, subMask->numRows,
    508518                                                                 region, footprint, spacing,
    509                                                                  sysErr, skyErr); // Stamp list
     519                                                                 normFrac, sysErr, skyErr); // Stamp list
    510520    int numStamps = stamps->num;        // Number of stamps
    511521
     
    612622    PS_ASSERT_INT_NONNEGATIVE(kernelSize, false);
    613623
    614     int size = kernelSize + stamps->footprint; // Size of postage stamps
     624    int size = stamps->footprint; // Size of postage stamps
    615625
    616626    psFree (stamps->window);
     
    641651    // storage vector for flux data
    642652    psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
    643     psVector *flux = psVectorAllocEmpty(2*stamps->num, PS_TYPE_F32);
    644 
    645     double maxValue = 0.0;
     653    psVector *flux1 = psVectorAllocEmpty(2*stamps->num, PS_TYPE_F32);
     654    psVector *flux2 = psVectorAllocEmpty(2*stamps->num, PS_TYPE_F32);
    646655
    647656    // generate the window pixels
     657    double sum = 0.0;                   // Sum inside the window
     658    float maxValue = 0.0;               // Maximum value, for normalisation
    648659    for (int y = -size; y <= size; y++) {
    649660        for (int x = -size; x <= size; x++) {
    650661
    651             flux->n = 0;
     662            flux1->n = 0;
     663            flux2->n = 0;
    652664            for (int i = 0; i < stamps->num; i++) {
    653665                pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
     
    656668                if (!stamp->image2) continue;
    657669
    658                 psVectorAppend (flux, stamp->image1->kernel[y][x] / norm1->data.F32[i]);
    659                 psVectorAppend (flux, stamp->image2->kernel[y][x] / norm2->data.F32[i]);
     670                psVectorAppend(flux1, stamp->image1->kernel[y][x] / norm1->data.F32[i]);
     671                psVectorAppend(flux2, stamp->image2->kernel[y][x] / norm2->data.F32[i]);
    660672            }
    661673
    662674            psStatsInit (stats);
    663             if (!psVectorStats (stats, flux, NULL, NULL, 0)) {
     675            if (!psVectorStats (stats, flux1, NULL, NULL, 0)) {
    664676                psAbort ("failed to generate stats");
    665677            }
    666             stamps->window->kernel[y][x] = stats->robustMedian;
    667             if (maxValue < stats->robustMedian) {
    668                 maxValue = stats->robustMedian;
    669             }
    670         }
     678            float f1 = stats->robustMedian;
     679            psStatsInit (stats);
     680            if (!psVectorStats (stats, flux2, NULL, NULL, 0)) {
     681                psAbort ("failed to generate stats");
     682            }
     683            float f2 = stats->robustMedian;
     684
     685            stamps->window->kernel[y][x] = f1 + f2;
     686            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(size)) {
     687                sum += stamps->window->kernel[y][x];
     688            }
     689            maxValue = PS_MAX(maxValue, stamps->window->kernel[y][x]);
     690        }
     691    }
     692
     693    psTrace("psModules.imcombine", 3, "Window total: %f, threshold: %f\n",
     694            sum, (1.0 - stamps->normFrac) * sum);
     695    bool done = false;
     696    for (int radius = 1; radius <= size && !done; radius++) {
     697        double within = 0.0;
     698        for (int y = -radius; y <= radius; y++) {
     699            for (int x = -radius; x <= radius; x++) {
     700                if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(radius)) {
     701                    within += stamps->window->kernel[y][x];
     702                }
     703            }
     704        }
     705        psTrace("psModules.imcombine", 5, "Radius %d: %f\n", radius, within);
     706        if (within > (1.0 - stamps->normFrac) * sum) {
     707            stamps->normWindow = radius;
     708            done = true;
     709        }
     710    }
     711
     712    psTrace("psModules.imcombine", 3, "Normalisation window radius set to %d\n", stamps->normWindow);
     713    if (stamps->normWindow == 0 || stamps->normWindow >= size) {
     714        psError(PS_ERR_UNKNOWN, true, "Unable to determine normalisation window size.");
     715        return false;
    671716    }
    672717
     
    678723    }
    679724
    680 # ifdef TESTING
     725#if 0
    681726    {
    682727        psFits *fits = psFitsOpen ("window.fits", "w");
     
    684729        psFitsClose (fits);
    685730    }
    686 # endif
     731#endif
    687732
    688733    psFree (stats);
    689     psFree (flux);
     734    psFree (flux1);
     735    psFree(flux2);
    690736    psFree (norm1);
    691737    psFree (norm2);
     
    694740
    695741bool pmSubtractionStampsExtract(pmSubtractionStampList *stamps, psImage *image1, psImage *image2,
    696                                 psImage *variance, int kernelSize)
     742                                psImage *variance, int kernelSize, psRegion bounds)
    697743{
    698744    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     
    711757    }
    712758
    713     int numCols = image1->numCols, numRows = image1->numRows; // Size of images
    714759    int size = kernelSize + stamps->footprint; // Size of postage stamps
    715760
     
    720765        }
    721766
    722         if (isnan(stamp->xNorm)) {
    723             stamp->xNorm = 2.0 * (stamp->x - (float)numCols/2.0) / (float)numCols;
    724         }
    725         if (isnan(stamp->yNorm)) {
    726             stamp->yNorm = 2.0 * (stamp->y - (float)numRows/2.0) / (float)numRows;
    727         }
     767        p_pmSubtractionPolynomialNormCoords(&stamp->xNorm, &stamp->yNorm, stamp->x, stamp->y,
     768                                            bounds.x0, bounds.x1, bounds.y0, bounds.y1);
    728769
    729770        int x = stamp->x + 0.5, y = stamp->y + 0.5; // Stamp coordinates
    730         if (x < size || x > numCols - size || y < size || y > numRows - size) {
    731             psError(PS_ERR_UNKNOWN, false, "Stamp %d (%d,%d) is within the image border.\n", i, x, y);
     771        if (x < bounds.x0 + size || x > bounds.x1 - size || y < bounds.y0 + size || y > bounds.y1 - size) {
     772            psError(PS_ERR_UNKNOWN, false, "Stamp %d (%d,%d) is within the region border.\n", i, x, y);
    732773            return false;
    733774        }
     
    788829pmSubtractionStampList *pmSubtractionStampsSetFromSources(const psArray *sources, const psImage *image,
    789830                                                          const psImage *subMask, const psRegion *region,
    790                                                           int size, int footprint, float spacing, float sysErr, float skyErr,
     831                                                          int size, int footprint, float spacing,
     832                                                          float normFrac, float sysErr, float skyErr,
    791833                                                          pmSubtractionMode mode)
    792834{
     
    819861
    820862    pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, image, subMask, region, size,
    821                                                             footprint, spacing, sysErr, skyErr, mode); // Stamps
     863                                                            footprint, spacing, normFrac,
     864                                                            sysErr, skyErr, mode); // Stamps
    822865    psFree(x);
    823866    psFree(y);
     
    833876pmSubtractionStampList *pmSubtractionStampsSetFromFile(const char *filename, const psImage *image,
    834877                                                       const psImage *subMask, const psRegion *region,
    835                                                        int size, int footprint, float spacing, float sysErr, float skyErr,
    836                                                        pmSubtractionMode mode)
     878                                                       int size, int footprint, float spacing, float normFrac,
     879                                                       float sysErr, float skyErr, pmSubtractionMode mode)
    837880{
    838881    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
     
    851894
    852895    pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, image, subMask, region, size, footprint,
    853                                                             spacing, sysErr, skyErr, mode);
     896                                                            spacing, normFrac, sysErr, skyErr, mode);
    854897    psFree(data);
    855898
Note: See TracChangeset for help on using the changeset viewer.