IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 6, 2005, 9:55:31 PM (21 years ago)
Author:
eugene
Message:

finshed deblender, dropped local copies of psModule code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psphotBasicDeblend.c

    r5704 r5718  
    11# include "psphot.h"
    22
    3 // try PSF models and select best option
     3bool psphotBasicDeblend (psArray *sources, psMetadata *config, psStats *sky) {
    44
    5 pmPSF *psphotBasicDeblend (psMetadata *config, psArray *sources, psStats *skystats)
    6 {
     5    int N;
     6    bool status;
     7    float threshold;
     8    pmSource *source, *testSource;
    79
    8     // source analysis is done in S/N order (brightest first)
     10    FILE *f = fopen ("deblend.dat", "w");
     11    if (f == NULL) psAbort ("psphot", "can't open deblend.dat output file");
     12    int Nblend = 0;
     13
     14    psTimerStart ("psphot");
     15
     16    // this should be added to the PM_SOURCE flags:
     17    int PM_SOURCE_BLEND = PM_SOURCE_OTHER + 1;
     18
     19    float FRACTION = psMetadataLookupF32 (&status, config, "DEBLEND_PEAK_FRACTION");
     20    float NSIGMA   = psMetadataLookupF32 (&status, config, "DEBLEND_SKY_NSIGMA");
     21    float minThreshold = NSIGMA*sky->sampleStdev;
     22    fprintf (stderr, "min threshold: %f\n", minThreshold);
     23
     24    // we need sources spatially-sorted to find overlaps
    925    sources = psArraySort (sources, psphotSortByY);
    1026
     27    // source analysis is done in peak order (brightest first)
     28    // we use an index for this so the spatial sorting is kept
    1129    psVector *SN = psVectorAlloc (sources->n, PS_DATA_F32);
    1230    for (int i = 0; i < SN->n; i++) {
    13       source = sources->data[i];
    14       SN->data.F32[i] = source->moments->SN;
     31        source = sources->data[i];
     32        SN->data.F32[i] = source->peak->counts;
    1533    }
    1634    psVector *index = psVectorSortIndex (NULL, SN);
    17    
     35    // this results in an index of increasing SN
     36
     37    // temporary array for overlapping objects we find
    1838    psArray *overlap = psArrayAlloc (100);
    1939
    20     for (int i = 0; i < sources->n; i++) {
    21       N = index->data.U32[i];
    22       source = sources->data[N];
    23       overlap->n = 0;
     40    // XXX make sure this results in decreasing, not increasing, SN
     41    for (int i = sources->n - 1; i >= 0; i--) {
     42        N = index->data.U32[i];
     43        source = sources->data[N];
    2444
    25       // search backwards for overlapping sources
    26       for (int j = N - 1; j >= 0; j--) {
    27         testSource = sources->data[j];
    28         if (testSource->moments->x < source->pixels->col0) continue;
    29         if (testSource->moments->x >= source->pixels->col0 + source->pixels->numCols) continue;
    30         if (testSource->moments->y < source->pixels->row0) break;
    31         if (testSource->moments->y >= source->pixels->row0 + source->pixels->numRows) {
    32           fprintf (stderr, "warning: invalid condition\n");
    33           continue;
     45        if (source->type == PM_SOURCE_BLEND) continue;
     46
     47        overlap->n = 0;
     48
     49        // search backwards for overlapping sources
     50        for (int j = N - 1; j >= 0; j--) {
     51            testSource = sources->data[j];
     52            if (testSource->peak->x <  source->pixels->col0) continue;
     53            if (testSource->peak->x >= source->pixels->col0 + source->pixels->numCols) continue;
     54            if (testSource->peak->y <  source->pixels->row0) break;
     55            if (testSource->peak->y >= source->pixels->row0 + source->pixels->numRows) {
     56                fprintf (stderr, "warning: invalid condition\n");
     57                continue;
     58            }
     59            psArrayAdd (overlap, 100, testSource);
    3460        }
    35         psArrayAdd (overlap, 100, testSource);
    36       }
    3761
    38       // search forwards for overlapping sources
    39       for (int j = N + 1; j < sources->n; j++) {
    40         testSource = sources->data[j];
    41         if (testSource->moments->x < source->pixels->col0) continue;
    42         if (testSource->moments->x >= source->pixels->col0 + source->pixels->numCols) continue;
    43         if (testSource->moments->y < source->pixels->row0) {
    44           fprintf (stderr, "warning: invalid condition\n");
    45           continue;
     62        // search forwards for overlapping sources
     63        for (int j = N + 1; j < sources->n; j++) {
     64            testSource = sources->data[j];
     65            if (testSource->peak->x <  source->pixels->col0) continue;
     66            if (testSource->peak->x >= source->pixels->col0 + source->pixels->numCols) continue;
     67            if (testSource->peak->y <  source->pixels->row0) {
     68                fprintf (stderr, "warning: invalid condition\n");
     69                continue;
     70            }
     71            if (testSource->peak->y >= source->pixels->row0 + source->pixels->numRows) break;
     72            psArrayAdd (overlap, 100, testSource);
    4673        }
    47         if (testSource->moments->y >= source->pixels->row0 + source->pixels->numRows) break;
    48         psArrayAdd (overlap, 100, testSource);
    49       }
    5074
    51       // generate source contour (1/4 peak counts)
    52       if (overlap->n > 0) {
    53         // XXX EAM : make the 1/4 user-defined.
    54         threshold = 0.25 * (source->moments->Peak - source->moments->Sky) + source->moments->Sky);
    55         psArray *contour = pmSourceContour_EAM (source->pixels, source->moments->x, source->moments->x, threshold);
     75        // generate source contour (1/4 peak counts)
     76        if (overlap->n > 0) {
     77            // XXX EAM : make the 1/4 user-defined.
     78            // XXX keep threshold from dropping too low (N*sky.sigma)
     79            threshold = FRACTION * (source->peak->counts - source->moments->Sky) + source->moments->Sky;
     80            threshold = PS_MAX (threshold, minThreshold);
    5681
    57         // XXX EAM : the order is wrong: need to check j, j+1 contour points
    58         psVector *xv = contour->data[0];
    59         psVector *yv = contour->data[1];
    60         for (int j = 0; j < xv->n; j++) {
    61           for (int k = 0; k < overlap->n; k++) {
    62             testSource = overlap->data[k];
    63             if (fabs(yv->data.F32[j] - testSource->moments->y) > 0.5) continue;
    64             if (xv->data.F32[j] > testSource->moments->x) continue;
    65             if (xv->data.F32[j] < testSource->moments->x) continue;
    66             // XXX EAM : mark source with flag (blended)
    67           }
    68         } 
     82            // XXX EAM : should the contour input coordinate be in parent or subimage coords? parent, for now
     83            psArray *contour = pmSourceContour_EAM (source->pixels, source->peak->x, source->peak->y, threshold);
     84            if (contour == NULL) {
     85                fprintf (stderr, "below threshold? invalid peak?\n");
     86                continue;
     87            }
     88
     89            // the source contour consists of two vectors, xv and yv.  the contour is
     90            // a series of coordinate pairs, (xv[i],yv[i]) & (xv[i+1],yv[i+1]).  both
     91            // coordinate pairs have the same yv[] value, with xv[i] corresponding to
     92            // the left boundary and xv[i+1] corresponding to the right boundary
     93
     94            psVector *xv = contour->data[0];
     95            psVector *yv = contour->data[1];
     96            for (int k = 0; k < overlap->n; k++) {
     97                testSource = overlap->data[k];
     98                if (testSource->peak->counts > source->peak->counts) continue;
     99                for (int j = 0; j < xv->n; j+=2) {
     100                    if (fabs(yv->data.F32[j] - testSource->peak->y) > 0.5) continue;
     101                    if (xv->data.F32[j+0] > testSource->peak->x) break;
     102                    if (xv->data.F32[j+1] < testSource->peak->x) break;
     103
     104                    int xp0 = source->moments->x - source->pixels->col0;
     105                    int xp1 = source->peak->x - source->pixels->col0;
     106                    int xp2 = testSource->moments->x - source->pixels->col0;
     107                    int xp3 = testSource->peak->x - source->pixels->col0;
     108
     109                    int yp0 = source->moments->y - source->pixels->row0;
     110                    int yp1 = source->peak->y - source->pixels->row0;
     111                    int yp2 = testSource->moments->y - testSource->pixels->row0;
     112                    int yp3 = testSource->peak->y - testSource->pixels->row0;
     113
     114                    fprintf (f, "%d %d (%f, %f) :  %d %d (%f, %f)  vs %f\n",
     115                             source->peak->x, source->peak->y,
     116                             source->pixels->data.F32[yp0][xp0], source->pixels->data.F32[yp1][xp1],
     117                             testSource->peak->x, testSource->peak->y,
     118                             testSource->pixels->data.F32[yp2][xp2], testSource->pixels->data.F32[yp3][xp3], threshold
     119                             );
     120
     121                    testSource->type = PM_SOURCE_BLEND;
     122                    Nblend ++;
     123                    j = xv->n;
     124                }
     125            } 
     126        }
    69127    }
     128    fclose (f);
     129    psLogMsg ("psphot.deblend", 3, "identified %d blended objects (%f)\n", Nblend, psTimerMark ("psphot"));
     130    return true;
     131}
    70132
    71     psLogMsg ("psphot.pspsf", 3, "selected psf model %s, ApResid: %f +/- %f\n", modelName, psf->ApResid, psf->dApResid);
    72133
    73     return (psf);
    74 }
     134
Note: See TracChangeset for help on using the changeset viewer.