IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 35383


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

ppStack changes to fix source matching bug.

Location:
trunk
Files:
5 edited

Legend:

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

    r34089 r35383  
    88// #define TESTING                         // Enable debugging output
    99// #define TESTING_REUSE_CONV           // Enable debugging for re-using convolved outputs from previous run
     10
     11// #define TESTING_CZW
    1012
    1113#ifdef TESTING_REUSE_CONV
     
    264266            // Add the background into the target image
    265267            psImage *bgImage = stackBackgroundModel(readout, config); // Image of background
    266             psBinaryOp(fake->image, fake->image, "+", bgImage);
     268            psBinaryOp(fake->image, fake->image, "+", bgImage);
    267269            psFree(bgImage);
    268270#endif
    269271
    270 #ifdef TESTING
     272#ifdef TESTING_CZW
    271273            {
    272274                pmHDU *hdu = pmHDUFromCell(readout->parent);
    273275                psString name = NULL;
    274                 psStringAppend(&name, "fake_%03d.fits", index);
     276                psStringAppend(&name, "/local/tmp/fake_%03d.fits", index);
    275277                pmStackVisualPlotTestImage(fake->image, name);
    276278                psFits *fits = psFitsOpen(name, "w");
     
    282284                pmHDU *hdu = pmHDUFromCell(readout->parent);
    283285                psString name = NULL;
    284                 psStringAppend(&name, "real_%03d.fits", index);
     286                psStringAppend(&name, "/local/tmp/real_%03d.fits", index);
    285287                pmStackVisualPlotTestImage(readout->image, name);
    286288                psFits *fits = psFitsOpen(name, "w");
    287289                psFree(name);
    288290                psFitsWriteImage(fits, hdu->header, readout->image, 0, NULL);
     291                psFitsClose(fits);
     292            }
     293            {
     294                pmHDU *hdu = pmHDUFromCell(readout->parent);
     295                psString name = NULL;
     296                psStringAppend(&name, "/local/tmp/real_var_%03d.fits", index);
     297                pmStackVisualPlotTestImage(readout->variance, name);
     298                psFits *fits = psFitsOpen(name, "w");
     299                psFree(name);
     300                psFitsWriteImage(fits, hdu->header, readout->variance, 0, NULL);
    289301                psFitsClose(fits);
    290302            }
     
    355367
    356368
    357 #ifdef TESTING
     369#ifdef TESTING_CZW
    358370            {
    359371                pmHDU *hdu = pmHDUFromCell(readout->parent);
    360372                psString name = NULL;
    361                 psStringAppend(&name, "conv_%03d.fits", index);
     373                psStringAppend(&name, "/local/tmp/conv_%03d.fits", index);
    362374                pmStackVisualPlotTestImage(conv->image, name);
    363375                psFits *fits = psFitsOpen(name, "w");
     
    369381                pmHDU *hdu = pmHDUFromCell(readout->parent);
    370382                psString name = NULL;
    371                 psStringAppend(&name, "diff_%03d.fits", index);
     383                psStringAppend(&name, "/local/tmp/conv_var_%03d.fits", index);
     384                pmStackVisualPlotTestImage(conv->variance, name);
     385                psFits *fits = psFitsOpen(name, "w");
     386                psFree(name);
     387                psFitsWriteImage(fits, hdu->header, conv->variance, 0, NULL);
     388                psFitsClose(fits);
     389            }
     390            {
     391                pmHDU *hdu = pmHDUFromCell(readout->parent);
     392                psString name = NULL;
     393                psStringAppend(&name, "/local/tmp/diff_%03d.fits", index);
    372394                pmStackVisualPlotTestImage(fake->image, name);
    373395                psFits *fits = psFitsOpen(name, "w");
     
    510532    }
    511533
    512     if (!stackRenormaliseReadout(config, readout)) {
    513         psFree(rng);
    514         psFree(bg);
    515         return false;
    516     }
    517 
     534#ifdef TESTING_CZW
     535    // CZW begin check of weight before renormalize step
    518536    // Measure the variance level for the weighting
    519537    if (psMetadataLookupBool(NULL, recipe, "WEIGHTS")) {
     
    529547        options->weightings->data.F32[index] = 1.0;
    530548    }
    531     psLogMsg("ppStack", PS_LOG_INFO, "Weighting for image %d is %f\n",
    532              index, options->weightings->data.F32[index]);
     549    psLogMsg("ppStack", PS_LOG_INFO, "Prenorm Weighting for image %d is %g covar %g median %g\n",
     550             index, options->weightings->data.F32[index],psImageCovarianceFactor(readout->covariance),
     551             psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN));
     552    // CZW end
     553#endif
     554    if (!stackRenormaliseReadout(config, readout)) {
     555        psFree(rng);
     556        psFree(bg);
     557        return false;
     558    }
     559   
     560    // Measure the variance level for the weighting
     561    if (psMetadataLookupBool(NULL, recipe, "WEIGHTS")) {
     562        if (!psImageBackground(bg, NULL, readout->variance, readout->mask, maskVal | maskBad, rng)) {
     563            psError(PPSTACK_ERR_DATA, false, "Can't measure mean variance for image.");
     564            psFree(rng);
     565            psFree(bg);
     566            return false;
     567        }
     568        options->weightings->data.F32[index] = 1.0 / (psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN) *
     569                                                      psImageCovarianceFactor(readout->covariance));
     570    } else {
     571        options->weightings->data.F32[index] = 1.0;
     572    }
     573    psLogMsg("ppStack", PS_LOG_INFO, "Weighting for image %d is %g covar %g median %g\n",
     574             index, options->weightings->data.F32[index],psImageCovarianceFactor(readout->covariance),
     575             psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN));
    533576
    534577    psFree(rng);
  • 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
  • trunk/ppStack/src/ppStackTarget.c

    r34089 r35383  
    6060        }
    6161        float xSource, ySource;         // Coordinates of source
    62         coordsFromSource(&xSource, &ySource, NULL, source);
     62        float Smag;                      // magnitude of source
     63        coordsFromSource(&xSource, &ySource, &Smag, source);
    6364
    6465        coords->data.F64[0] = xSource;
     
    6667
    6768        psVector *indices = psTreeAllWithin(tree, coords, exclusion); // Number within exclusion zone
    68         psTrace("ppStack", 9, "Source at %.0lf,%.0lf has %ld sources in exclusion zone",
    69                 coords->data.F64[0], coords->data.F64[1], indices->n);
     69        psTrace("ppStack", 5, "Filtering: Source at %.0lf %.0lf with mag %f has %ld sources in exclusion zone",
     70                coords->data.F64[0], coords->data.F64[1], Smag, indices->n);
    7071        if (indices->n == 1) {
    7172            // Only itself inside the exclusion zone
  • trunk/psModules/src/objects/pmSourceMatch.c

    r35240 r35383  
    630630
    631631psVector *pmSourceMatchRelphot(const psArray *matches, // Array of matches
     632                               psArray *matchedSources, // Array of averaged sources
    632633                               const psVector *zp, // Zero points for each image (including airmass term)
    633634                               float tol, // Relative tolerance for convergence
     
    648649    PS_ASSERT_VECTOR_TYPE(zp, PS_TYPE_F32, NULL);
    649650    PS_ASSERT_FLOAT_LARGER_THAN(transClip, 0.0, NULL);
    650 
     651    PS_ASSERT_ARRAY_NON_NULL(matchedSources, NULL);
     652   
    651653    sys1 *= sys1;
    652654    sys2 *= sys2;
     
    742744    }
    743745
     746    for (int i = 0; i < matches->n; i++) {
     747      pmSource *source = pmSourceAlloc();
     748      source->psfMag = stars->data.F32[i];
     749      psArrayAdd(matchedSources,matchedSources->n,source);
     750    }
     751   
    744752    psFree(photo);
    745753    psFree(badImage);
  • trunk/psModules/src/objects/pmSourceMatch.h

    r35240 r35383  
    7474/// Perform relative photometry to calibrate images
    7575psVector *pmSourceMatchRelphot(const psArray *matches, // Array of matches
     76                               psArray *matchedSources, // Array of average sources
    7677                               const psVector *zp, // Zero points for each image (including airmass term)
    7778                               float tol, // Relative tolerance for convergence
Note: See TracChangeset for help on using the changeset viewer.