IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/psphot

  • branches/simtest_nebulous_branches/psphot/src

    • Property svn:ignore
      •  

        old new  
        1818psphotVersionDefinitions.h
        1919psphotMomentsStudy
         20psphotPetrosianStudy
         21psphotForced
         22psphotMakePSF
         23psphotStack
  • branches/simtest_nebulous_branches/psphot/src/psphotChoosePSF.c

    r23989 r27840  
    11# include "psphotInternal.h"
    22
    3 void psphotCountPSFStars (psArray *sources) {
    4 
    5     int nPSF = 0;
    6 
    7     // select the candidate PSF stars (pointers to original sources)
    8     for (int i = 0; i < sources->n; i++) {
    9         pmSource *source = sources->data[i];
    10         if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
    11             nPSF ++;
    12         }
    13     }
    14 
    15     fprintf (stderr, "N PSF: %d\n", nPSF);
     3// generate a PSF model for inputs without PSF models already loaded
     4bool psphotChoosePSF (pmConfig *config, const pmFPAview *view)
     5{
     6    bool status = true;
     7
     8    // select the appropriate recipe information
     9    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     10    psAssert (recipe, "missing recipe?");
     11
     12    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     13    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
     14
     15    // skip the chisq image (optionally?)
     16    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     17    if (!status) chisqNum = -1;
     18
     19    // loop over the available readouts
     20    for (int i = 0; i < num; i++) {
     21        if (i == chisqNum) continue; // skip chisq image
     22        if (!psphotChoosePSFReadout (config, view, "PSPHOT.INPUT", i, recipe)) {
     23            psError (PSPHOT_ERR_CONFIG, false, "failed to choose a psf model for PSPHOT.INPUT entry %d", i);
     24            return false;
     25        }
     26    }
     27    return true;
    1628}
    1729
    1830// try PSF models and select best option
    19 pmPSF *psphotChoosePSF (pmReadout *readout, psArray *sources, psMetadata *recipe) {
     31bool psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
    2032
    2133    bool status;
    2234
    2335    psTimerStart ("psphot.choose.psf");
     36
     37    // find the currently selected readout
     38    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
     39    psAssert (file, "missing file?");
     40
     41    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     42    psAssert (readout, "missing readout?");
     43
     44    // do not generate a PSF if we already were supplied one
     45    if (psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF")) {
     46        psLogMsg ("psphot", PS_LOG_DETAIL, "psf model supplied for input file %d", index);
     47        return true;
     48    }
     49
     50    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     51    psAssert (detections, "missing detections?");
     52
     53    psArray *sources = detections->newSources;
     54    psAssert (sources, "missing sources?");
     55
     56    if (!sources->n) {
     57        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping PSF model");
     58        return false;
     59    }
    2460
    2561    // bit-masks to test for good/bad pixels
    2662    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT");
    27     assert (maskVal);
     63    psAssert (maskVal, "missing mask value?");
    2864
    2965    // bit-mask to mark pixels not used in analysis
    3066    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT");
    31     assert (markVal);
     67    psAssert (markVal, "missing mark value?");
    3268
    3369    // maskVal is used to test for rejected pixels, and must include markVal
     
    73109    // get the fixed PSF fit radius
    74110    // XXX check that PSF_FIT_RADIUS < SKY_OUTER_RADIUS
    75     options->radius = psMetadataLookupF32 (&status, recipe, "PSF_FIT_RADIUS");
    76     assert (status);
    77 
    78     // XXX ROBUST seems to be too agressive given the small numbers.
    79     // options->stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
    80     options->stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     111    // options->radius = psMetadataLookupF32 (&status, recipe, "PSF_FIT_RADIUS");
     112    // assert (status);
     113
     114    // values on readout->analysis if we have calculated a Gaussian window function, use that
     115    // for both the PSF fit radius and the aperture radius (scaling SIGMA), otherwise base the value on the recipe value for MOMENTS_GAUSS_SIGMA
     116    float gaussSigma = psMetadataLookupF32 (&status, readout->analysis, "MOMENTS_GAUSS_SIGMA");
     117    if (!status) {
     118        gaussSigma = psMetadataLookupF32 (&status, recipe, "MOMENTS_GAUSS_SIGMA");
     119    }
     120    float fitScale = psMetadataLookupF32(&status, recipe, "PSF_FIT_RADIUS_SCALE");
     121    float apScale = psMetadataLookupF32(&status, recipe, "PSF_APERTURE_SCALE");
     122    options->fitRadius = (int)(fitScale*gaussSigma);
     123    options->apRadius = (int)(apScale*gaussSigma);
     124
     125    // XXX use the same radii for standard analysis as for the PSF creation
     126    psMetadataAddF32(readout->analysis, PS_LIST_TAIL, "PSF_FIT_RADIUS", PS_META_REPLACE, "fit radius", options->fitRadius);
     127    psMetadataAddF32(readout->analysis, PS_LIST_TAIL, "PSF_APERTURE", PS_META_REPLACE, "psf aperture", options->apRadius);
    81128
    82129    // dimensions of the field for which the PSF is defined
     
    99146
    100147    psArray *stars = psArrayAllocEmpty (sources->n);
    101 
    102     // psphotCountPSFStars (sources);
    103148
    104149    // select the candidate PSF stars (pointers to original sources)
     
    115160    }
    116161
    117     // psphotCountPSFStars (sources);
    118 
    119162    // check that the identified psf stars sufficiently cover the region; if not, extend the
    120163    // limits somewhat
    121164    psphotCheckStarDistribution (stars, sources, options);
    122165
    123     // psphotCountPSFStars (sources);
    124 
    125166    psLogMsg ("psphot.pspsf", PS_LOG_DETAIL, "selected candidate %ld PSF objects\n", stars->n);
     167
     168    if (stars->n < 50) {
     169        // ROBUST is too agressive if we only have a small number of PSF stars
     170        options->stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     171    } else {
     172        options->stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
     173    }
    126174
    127175    // get the list pointers for the PSF_MODEL entries
     
    164212        bool status = true;
    165213        status &= psphotMakeFluxScale (readout->image, recipe, psf);
    166         status &= psphotPSFstats (readout, recipe, psf);
     214        status &= psphotPSFstats (readout, psf);
    167215        if (!status) {
    168216            psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
     
    239287        bool status = true;
    240288        status &= psphotMakeFluxScale (readout->image, recipe, psf);
    241         status &= psphotPSFstats (readout, recipe, psf);
     289        status &= psphotPSFstats (readout, psf);
    242290        if (!status) {
    243291            psError(PSPHOT_ERR_PSF, false, "Failed to fit any models when choosing PSF");
     
    289337    // XXX test dump of psf star data and psf-subtracted image
    290338    if (psTraceGetLevel("psphot.psfstars") > 5) {
    291         psphotDumpPSFStars (readout, try, options->radius, maskVal, markVal);
     339        psphotDumpPSFStars (readout, try, options->fitRadius, maskVal, markVal);
    292340    }
    293341
    294342    // save only the best model;
    295     // XXX we are not saving the fitted sources
    296     // XXX do we want to keep them so we may optionally write them out?
    297343    pmPSF *psf = psMemIncrRefCounter(try->psf);
    298344    psFree (models);
    299345
    300     if (!psphotPSFstats (readout, recipe, psf)) {
     346    if (!psphotPSFstats (readout, psf)) {
    301347        psError(PSPHOT_ERR_PSF, false, "cannot measure PSF shape terms");
    302348        psFree(options);
     
    305351    }
    306352
    307     // psphotCountPSFStars (sources);
    308 
    309353    char *modelName = pmModelClassGetName (psf->type);
    310354    psLogMsg ("psphot.pspsf", PS_LOG_INFO, "select psf model: %f sec\n", psTimerMark ("psphot.choose.psf"));
     
    312356
    313357    psFree (options);
    314     return (psf);
     358
     359    // save PSF on readout->analysis
     360    if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot psf model", psf)) {
     361        psError (PSPHOT_ERR_UNKNOWN, false, "problem saving sources on readout");
     362        return false;
     363    }
     364    psFree (psf); // XXX double-check
     365
     366    // move into psphotChoosePSF
     367    psphotVisualShowPSFModel (readout, psf);
     368
     369    return true;
    315370}
    316371
    317372// measure average parameters of the PSF model
    318 bool psphotPSFstats (pmReadout *readout, psMetadata *recipe, pmPSF *psf) {
     373bool psphotPSFstats (pmReadout *readout, pmPSF *psf) {
    319374
    320375    psEllipseShape shape;
     
    322377
    323378    PS_ASSERT_PTR_NON_NULL(readout, false);
    324     PS_ASSERT_PTR_NON_NULL(recipe, false);
    325379    PS_ASSERT_PTR_NON_NULL(psf, false);
    326380
     
    341395            // create modelPSF from this model
    342396            pmModel *modelPSF = pmModelFromPSFforXY (psf, xc, yc, 1.0);
    343             if (!modelPSF) continue;
     397            if (!modelPSF) {
     398                fprintf (stderr, "?");
     399                continue;
     400            }
    344401
    345402            // get the model full-width at half-max
     
    354411
    355412            float FWHM_MINOR = FWHM_MAJOR * (axes.minor / axes.major);
    356             if (!isfinite(FWHM_MAJOR) || !isfinite(FWHM_MINOR)) continue;
     413            if (!isfinite(FWHM_MAJOR) || !isfinite(FWHM_MINOR)) {
     414                fprintf (stderr, "!");
     415                continue;
     416            }
    357417            psVectorAppend (fwhmMajor, FWHM_MAJOR);
    358418            psVectorAppend (fwhmMinor, FWHM_MINOR);
     
    366426    }
    367427
    368     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FWHM_MAJ",   PS_META_REPLACE, "PSF FWHM Major axis (mean)", stats->sampleMean);
    369     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_SG",   PS_META_REPLACE, "PSF FWHM Major axis (sigma)", stats->sampleStdev);
    370     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_LQ",   PS_META_REPLACE, "PSF FWHM Major axis (lower quartile)", stats->sampleLQ);
    371     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_UQ",   PS_META_REPLACE, "PSF FWHM Major axis (upper quartile)", stats->sampleUQ);
     428    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FWHM_MAJ",   PS_META_REPLACE, "PSF FWHM Major axis (mean)", stats->sampleMean);
     429    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MJ_SG",   PS_META_REPLACE, "PSF FWHM Major axis (sigma)", stats->sampleStdev);
     430    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MJ_LQ",   PS_META_REPLACE, "PSF FWHM Major axis (lower quartile)", stats->sampleLQ);
     431    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MJ_UQ",   PS_META_REPLACE, "PSF FWHM Major axis (upper quartile)", stats->sampleUQ);
     432
     433    float fwhmMaj = stats->sampleMean;  // FWHM on major axis
    372434
    373435    if (!psVectorStats (stats, fwhmMinor, NULL, NULL, 0)) {
     
    375437        return false;
    376438    }
    377     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FWHM_MIN",   PS_META_REPLACE, "PSF FWHM Minor axis (mean)", stats->sampleMean);
    378     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_SG",   PS_META_REPLACE, "PSF FWHM Minor axis (sigma)", stats->sampleStdev);
    379     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_LQ",   PS_META_REPLACE, "PSF FWHM Minor axis (lower quartile)", stats->sampleLQ);
    380     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_UQ",   PS_META_REPLACE, "PSF FWHM Minor axis (upper quartile)", stats->sampleUQ);
    381 
    382     psMetadataAddF32 (recipe, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           axes.theta);
    383     psMetadataAddS32 (recipe, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", psf->nPSFstars);
    384     psMetadataAddBool(recipe, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", true);
     439    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FWHM_MIN",   PS_META_REPLACE, "PSF FWHM Minor axis (mean)", stats->sampleMean);
     440    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_SG",   PS_META_REPLACE, "PSF FWHM Minor axis (sigma)", stats->sampleStdev);
     441    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_LQ",   PS_META_REPLACE, "PSF FWHM Minor axis (lower quartile)", stats->sampleLQ);
     442    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_UQ",   PS_META_REPLACE, "PSF FWHM Minor axis (upper quartile)", stats->sampleUQ);
     443
     444    float fwhmMin = stats->sampleMean;  // FWHM on minor axis
     445    if (readout->parent) {
     446        pmChip *chip = readout->parent->parent; // Parent chip
     447        psAssert(chip, "Cell should be attached to a chip.");
     448        psMetadataItem *item = psMetadataLookup(chip->concepts, "CHIP.SEEING"); // Item with chip
     449        item->data.F32 = 0.5 * (fwhmMaj + fwhmMin);
     450    }
     451
     452    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           axes.theta);
     453    psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", psf->nPSFstars);
     454    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", true);
    385455
    386456    psFree (fwhmMajor);
     
    391461
    392462// determine approximate PSF shape parameters based on the moments
    393 bool psphotMomentsStats (pmReadout *readout, psMetadata *recipe, psArray *sources) {
     463bool psphotMomentsStats (pmReadout *readout, psArray *sources) {
    394464
    395465    // without the PSF model, we can only rely on the source->moments
     
    405475
    406476    PS_ASSERT_PTR_NON_NULL(readout, false);
    407     PS_ASSERT_PTR_NON_NULL(recipe, false);
    408477    PS_ASSERT_PTR_NON_NULL(sources, false);
    409478
     
    433502    FWHM_T /= FWHM_N;
    434503
    435     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FWHM_MAJ",   PS_META_REPLACE, "PSF FWHM Major axis (mean)", FWHM_X);
    436     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_SG",   PS_META_REPLACE, "PSF FWHM Major axis (sigma)", 0);
    437     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_LQ",   PS_META_REPLACE, "PSF FWHM Major axis (lower quartile)", 0);
    438     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_UQ",   PS_META_REPLACE, "PSF FWHM Major axis (upper quartile)", 0);
    439 
    440     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FWHM_MIN",   PS_META_REPLACE, "PSF FWHM Minor axis (mean)", FWHM_Y);
    441     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_SG",   PS_META_REPLACE, "PSF FWHM Minor axis (sigma)", 0);
    442     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_LQ",   PS_META_REPLACE, "PSF FWHM Minor axis (lower quartile)", 0);
    443     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_UQ",   PS_META_REPLACE, "PSF FWHM Minor axis (upper quartile)", 0);
    444 
    445     psMetadataAddF32 (recipe, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           FWHM_T);
    446     psMetadataAddS32 (recipe, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", 0);
    447     psMetadataAddBool(recipe, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", false);
     504    if (readout->parent) {
     505        pmChip *chip = readout->parent->parent; // Parent chip
     506        psAssert(chip, "Cell should be attached to a chip.");
     507        psMetadataItem *item = psMetadataLookup(chip->concepts, "CHIP.SEEING"); // Item with chip
     508        item->data.F32 = 0.5 * (FWHM_X + FWHM_Y);
     509    }
     510
     511    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FWHM_MAJ",   PS_META_REPLACE, "PSF FWHM Major axis (mean)", FWHM_X);
     512    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MJ_SG",   PS_META_REPLACE, "PSF FWHM Major axis (sigma)", 0);
     513    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MJ_LQ",   PS_META_REPLACE, "PSF FWHM Major axis (lower quartile)", 0);
     514    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MJ_UQ",   PS_META_REPLACE, "PSF FWHM Major axis (upper quartile)", 0);
     515    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FWHM_MIN",   PS_META_REPLACE, "PSF FWHM Minor axis (mean)", FWHM_Y);
     516    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_SG",   PS_META_REPLACE, "PSF FWHM Minor axis (sigma)", 0);
     517    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_LQ",   PS_META_REPLACE, "PSF FWHM Minor axis (lower quartile)", 0);
     518    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_UQ",   PS_META_REPLACE, "PSF FWHM Minor axis (upper quartile)", 0);
     519    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           FWHM_T);
     520    psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", 0);
     521    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", false);
    448522
    449523    return true;
Note: See TracChangeset for help on using the changeset viewer.