IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 9, 2013, 1:33:36 PM (13 years ago)
Author:
watersc1
Message:

ppStack changes to fix source matching bug.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppStack/src/ppStackSources.c

    r34174 r35383  
    1111#endif
    1212
    13 #ifdef TESTING
     13// #define TESTING_CZW
     14
     15#ifdef TESTING_CZW
    1416// Dump matches to a file
    1517static void dumpMatches(const char *filename, // File to which to dump
     
    2830        psVectorInit(magErr, NAN);
    2931        for (int j = 0; j < match->num; j++) {
     32          if (match->mask) {
    3033            if (match->mask->data.PS_TYPE_VECTOR_MASK_DATA[j]) {
    31                 continue;
    32             }
     34              continue;
     35            }
     36          }
    3337            int index = match->image->data.U32[j]; // Image index
    3438            mag->data.F32[index] = match->mag->data.F32[j] - zp->data.F32[index];
     
    3943        }
    4044        for (int j = 0; j < num; j++) {
    41             fprintf(outMatches, "%f (%f) ", mag->data.F32[j], magErr->data.F32[j]);
     45          fprintf(outMatches, "%f %f %f %f %f %f\t",
     46                  match->mag->data.F32[j],
     47                  zp->data.F32[j],
     48                  trans ? trans->data.F32[j] : NAN,
     49                  magErr->data.F32[j],
     50                  match->x->data.F32[j],
     51                  match->y->data.F32[j]);
    4252        }
    4353        fprintf(outMatches, "\n");
     
    271281        return false;
    272282    }
    273 
    274 #ifdef TESTING
     283    options->sources = psMemIncrRefCounter(matches);
     284    psArray *matchedSources = psArrayAlloc(0);
     285
     286#ifdef TESTING_CZW
    275287    dumpMatches("source_match.dat", num, matches, zp, NULL);
    276288#endif
    277289
    278290    if (options->matchZPs) {
    279         psVector *trans = pmSourceMatchRelphot(matches, zp, tol, iter1, starRej1, starSys1,
     291
     292      psVector *trans = pmSourceMatchRelphot(matches, matchedSources, zp, tol, iter1, starRej1, starSys1,
    280293                                               iter2, starRej2, starSys2, starLimit,
    281294                                               transIter, transRej, transThresh); // Transparencies per image
     
    324337
    325338        // m_inst_o - m_inst_i = zp[i] - zpTarget - c1 * airmassTarget - 2.5log(sumExpTime) - trans_i
    326 
     339#ifdef TESTING_CZW
     340    dumpMatches("source_match2.dat", num, matches, zp, trans);
     341#endif
    327342        if (options->matchZPs) {
    328343            options->norm = psVectorAlloc(num, PS_TYPE_F32);
    329344            for (int i = 0; i < num; i++) {
    330                 if (inputMask->data.U8[i] || !isfinite(trans->data.F32[i])) {
    331                     continue;
    332                 }
     345              if (!isfinite(trans->data.F32[i])) {
     346                continue;
     347              }
    333348                psArray *sources = sourceLists->data[i]; // Sources of interest
    334349                float magCorr = zp->data.F32[i] - trans->data.F32[i] - 2.5*log10(options->sumExposure) - airmassTerm * airmassTarget;
     
    341356                options->norm->data.F32[i] = magCorr;
    342357                psLogMsg("ppStack", PS_LOG_INFO,
    343                          "Applying scale correction to image %d: %f mag (%f)\n",
    344                          i, magCorr, trans->data.F32[i]);
     358                         "Applying scale correction to image %d: %f mag (%f) (with inputMask value %d)\n",
     359                         i, magCorr, trans->data.F32[i],inputMask->data.U8[i]);
    345360
    346361                for (int j = 0; j < sources->n; j++) {
    347362                    pmSource *source = sources->data[j]; // Source of interest
     363                    source->psfMag += magCorr;
     364                    source->apMag += magCorr;
     365
    348366                    if (!source) {
    349367                        continue;
    350368                    }
    351369                    // XXX need to apply to apMag as well
    352                     source->psfMag += magCorr;
    353                     source->apMag += magCorr;
     370                    psTrace("ppStack",5,"Source corrections: %d %d %f %f %f %f %f %f %f %f\n",i,j,
     371                            source->peak->xf,source->peak->yf,
     372                            source->psfMag,source->psfMagErr,
     373                            airmassTerm,airmassTarget,2.5 * log10(options->sumExposure),
     374                            source->psfMag + magCorr);
    354375                }
    355376            }
    356377        }
     378        matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
    357379
    358380        if (zpExpNum == numGoodImages) {
     
    366388
    367389
    368 #ifdef TESTING
    369         dumpMatches("source_mags.dat", num, matches, zp, trans);
    370 #endif
    371390        psFree(trans);
    372391
    373 #ifdef TESTING
     392#ifdef TESTING_CZW
    374393        // Double check: all transparencies should be zero
    375394        {
    376395            psArray *matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
     396            psArray *matchedSources = psArrayAlloc(matches->n);
    377397            if (!matches) {
    378398                psError(PPSTACK_ERR_DATA, false, "Unable to match sources");
     
    380400                return false;
    381401            }
    382             psVector *trans = pmSourceMatchRelphot(matches, zp, tol, iter1, starRej1, starSys1,
     402            psVector *trans = pmSourceMatchRelphot(matches, matchedSources, zp, tol, iter1, starRej1, starSys1,
    383403                                                   iter2, starRej2, starSys2, starLimit,
    384404                                                   transIter, transRej, transThresh); // Transparencies
     
    386406                fprintf(stderr, "Transparency of image %d: %f\n", i, trans->data.F32[i]);
    387407            }
     408
    388409            psFree(trans);
    389410            psFree(matches);
    390         }
    391 #endif
    392     }
    393 
    394     psFree(zp);
     411            psFree(matchedSources);
     412        }
     413#endif
     414    }
     415
     416
    395417    psFree(zpExp);
    396418
    397419#ifdef ASTROMETRY
     420    // CZW: This is off by default.
    398421    // Position offsets
    399422    {
     
    436459
    437460#if (defined TESTING && defined ASTROMETRY)
     461    // CZW: This is off by default.
    438462        // Double check: all offsets should be zero
    439463        {
     
    458482    if (options->photometry) {
    459483        // Save best matches for future photometry
    460         psArray *sourcesBest = options->sources = psArrayAllocEmpty(matches->n);
    461 
     484      psArray *sourcesBest = options->sources = psArrayAllocEmpty(matches->n);
    462485        // XXX something of a hack: require at least 2 detections or the nominated fraction of the max possible
    463486        //int minMatches = PS_MAX(2, fracMatch * num);// Minimum number of matches required
    464487        //MEH - if many poor images, will hit oddity with num fraction. use numGoodImages instead
    465         int minMatches = PS_MAX(2, fracMatch * numGoodImages);// Minimum number of matches required
    466 
    467         int good0 = 0;
    468         for (int i = 0; i < num; i++) {
    469             if (inputMask->data.U8[i]) {
    470                 continue;
    471             }
    472             good0 = i;
    473             break;
     488      int minMatches = PS_MAX(2, fracMatch * numGoodImages);// Minimum number of matches required
     489     
     490      int good0 = 0;
     491      for (int i = 0; i < num; i++) {
     492        if (inputMask->data.U8[i]) {
     493          continue;
    474494        }
    475 
    476         for (int i = 0; i < matches->n; i++) {
    477             pmSourceMatch *match = matches->data[i]; // Match of interest
    478             if (match->num < minMatches) {
    479                 continue;
    480             }
    481 
    482             // We need to grab a single instance of this source: just take the first available
    483             // MEH - if first available is a rejected image, then problem. so take first non-rejected instance above
    484             //int image = match->image->data.S32[0]; // Index of image
    485             //int index = match->index->data.S32[0]; // Index of source within image
    486             int image = match->image->data.S32[good0]; // Index of image
    487             int index = match->index->data.S32[good0]; // Index of source within image
    488             psArray *sources = sourceLists->data[image]; // Sources for image
    489             pmSource *source = sources->data[index]; // Source of interest
    490 
    491             psArrayAdd(sourcesBest, sourcesBest->n, source);
    492         }
    493         psLogMsg("ppStack", PS_LOG_INFO, "Selected %ld sources for photometry analysis", sourcesBest->n);
    494     }
    495 
     495        good0 = i;
     496        break;
     497      }
     498     
     499      for (int i = 0; i < matches->n; i++) {
     500        pmSourceMatch *match = matches->data[i]; // Match of interest
     501         if (match->num < minMatches) {
     502           continue;
     503         }
     504
     505         // We need to grab a single instance of this source: just take the first available
     506         // MEH - if first available is a rejected image, then problem. so take first non-rejected instance above
     507         //      int image = match->image->data.S32[0]; // Index of image
     508         // int index = match->index->data.S32[0]; // Index of source within image
     509         int image = match->image->data.S32[good0]; // Index of image
     510         int index = match->index->data.S32[good0]; // Index of source within image
     511         psArray *sources = sourceLists->data[image]; // Sources for image
     512         pmSource *source = sources->data[index]; // Source of interest
     513
     514         // CZW: Accept the first source for all properties, except for the magnitudes.
     515         //      We have average magnitudes from relphot, so we should use those
     516         pmSource *photsource = matchedSources->data[i];
     517         if ((photsource)&&(isfinite(photsource->psfMag))) {
     518           // CZW: photsource is the average relphot mag, which is equal to <m_i + zpIN_j>
     519           //      (primed are averaged values for this discussion)
     520           //      Therefore, the correct magnitude for psfMag' is:
     521           //         psfMag' = (photsource - zpIN_j + magCorr)
     522           //      apMag - psfMag should be retained, so:
     523           //         apMag - psfMag = apMag' - psfMag'
     524           //         apMag' = apMag + psfMag' - psfMag
     525           source->apMag  = source->apMag + (photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image] - source->psfMag);
     526           source->psfMag = photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image];
     527           
     528           psTrace("ppStack", 5, "Best Source %d/%ld @ %f %f %f %f %f %d",i,sourcesBest->n,source->peak->xf,source->peak->yf,
     529                   source->psfMag,
     530                   photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image] ,
     531                   source->psfMagErr,-1);
     532           psArrayAdd(sourcesBest, sourcesBest->n, source);
     533         }
     534        }
     535      psLogMsg("ppStack", PS_LOG_INFO, "Selected %ld sources for photometry analysis", sourcesBest->n);
     536
     537    }
     538    psFree(zp);
    496539    psFree(matches);
    497540
Note: See TracChangeset for help on using the changeset viewer.