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:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/psModules

  • branches/simtest_nebulous_branches/psModules/src/objects/pmSource.c

    r24874 r27840  
    33 *  Functions to define and manipulate sources on images
    44 *
    5  *  @author GLG, MHPCC
    6  *  @author EAM, IfA: significant modifications.
     5 *  @author EAM, IfA
     6 *  @author GLG, MHPCC (initial code base)
    77 *
    88 *  @version $Revision: 1.70 $ $Name: not supported by cvs2svn $
    99 *  @date $Date: 2009-02-16 22:29:59 $
    10  *
    11  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    12  *
     10 *  Copyright 2009 Institute for Astronomy, University of Hawaii
    1311 */
    1412
     
    4846    psFree(tmp->maskView);
    4947    psFree(tmp->modelFlux);
    50     psFree(tmp->psfFlux);
     48    psFree(tmp->psfImage);
    5149    psFree(tmp->moments);
    5250    psFree(tmp->modelPSF);
     
    5452    psFree(tmp->modelFits);
    5553    psFree(tmp->extpars);
     54    psFree(tmp->moments);
     55    psFree(tmp->diffStats);
    5656    psFree(tmp->blends);
    5757    psTrace("psModules.objects", 10, "---- end ----\n");
     
    7070    psFree (source->maskView);
    7171    psFree (source->modelFlux);
    72     psFree (source->psfFlux);
     72    psFree (source->psfImage);
    7373
    7474    source->pixels = NULL;
     
    7777    source->maskView = NULL;
    7878    source->modelFlux = NULL;
    79     source->psfFlux = NULL;
     79    source->psfImage = NULL;
    8080    return;
    8181}
     
    105105    source->maskView = NULL;
    106106    source->modelFlux = NULL;
    107     source->psfFlux = NULL;
     107    source->psfImage = NULL;
    108108    source->moments = NULL;
    109109    source->blends = NULL;
     
    115115    source->tmpFlags = 0;
    116116    source->extpars = NULL;
     117    source->diffStats = NULL;
     118
    117119    source->region = psRegionSet(NAN, NAN, NAN, NAN);
    118120    psMemSetDeallocator(source, (psFreeFunc) sourceFree);
    119121
    120122    // default values are NAN
    121     source->psfMag = NAN;
     123    source->psfMag     = NAN;
     124    source->psfFlux    = NAN;
     125    source->psfFluxErr = NAN;
    122126    source->extMag = NAN;
    123127    source->errMag = NAN;
     
    176180    source->type = in->type;
    177181    source->mode = in->mode;
     182    source->imageID = in->imageID;
    178183
    179184    return(source);
     
    261266        mySource->modelFlux = NULL;
    262267
    263         // drop the old psfFlux pixels and force the user to re-create
    264         psFree (mySource->psfFlux);
    265         mySource->psfFlux = NULL;
     268        // drop the old psfImage pixels and force the user to re-create
     269        psFree (mySource->psfImage);
     270        mySource->psfImage = NULL;
    266271    }
    267272    return extend;
     
    277282// psphot-specific function which applies the recipe values
    278283// only apply selection to sources within specified region
    279 pmPSFClump pmSourcePSFClump(psRegion *region, psArray *sources, psMetadata *recipe)
     284pmPSFClump pmSourcePSFClump(psImage **savedImage, psRegion *region, psArray *sources, float PSF_SN_LIM, float PSF_CLUMP_GRID_SCALE, psF32 SX_MAX, psF32 SY_MAX, psF32 AR_MAX)
    280285{
    281286    psTrace("psModules.objects", 10, "---- begin ----\n");
    282287
    283288    psArray *peaks  = NULL;
    284     pmPSFClump errorClump = {-1.0, -1.0, 0.0, 0.0};
    285     pmPSFClump emptyClump = {+0.0, +0.0, 0.0, 0.0};
     289    pmPSFClump errorClump = {-1.0, -1.0, 0.0, 0.0, 0, 0.0};
     290    pmPSFClump emptyClump = {+0.0, +0.0, 0.0, 0.0, 0, 0.0};
    286291    pmPSFClump psfClump;
    287292
    288293    PS_ASSERT_PTR_NON_NULL(sources, errorClump);
    289     PS_ASSERT_PTR_NON_NULL(recipe, errorClump);
    290 
    291     bool status = true;                 // Status of MD lookup
    292     float PSF_SN_LIM = psMetadataLookupF32(&status, recipe, "PSF_SN_LIM");
    293     if (!status) {
    294         PSF_SN_LIM = 0;
    295     }
    296     float PSF_CLUMP_GRID_SCALE = psMetadataLookupF32(&status, recipe, "PSF_CLUMP_GRID_SCALE");
    297     if (!status) {
    298         PSF_CLUMP_GRID_SCALE = 0.1;
    299     }
    300294
    301295    // find the sigmaX, sigmaY clump
    302296    {
    303         psF32 SX_MAX = psMetadataLookupF32(&status, recipe, "MOMENTS_SX_MAX");
    304         if (!status) {
    305             psWarning("MOMENTS_SX_MAX not set in recipe");
    306             SX_MAX = 10.0;
    307         }
    308         psF32 SY_MAX = psMetadataLookupF32(&status, recipe, "MOMENTS_SY_MAX");
    309         if (!status) {
    310             psWarning("MOMENTS_SY_MAX not set in recipe");
    311             SY_MAX = 10.0;
    312         }
    313         psF32 AR_MAX = psMetadataLookupF32(&status, recipe, "MOMENTS_AR_MAX");
    314         if (!status) {
    315             psWarning("MOMENTS_AR_MAX not set in recipe");
    316             AR_MAX =  3.0;
    317         }
    318297        psF32 AR_MIN = 1.0 / AR_MAX;
    319298
    320299        // construct a sigma-plane image
    321         int numCols = SX_MAX / PSF_CLUMP_GRID_SCALE, numRows = SY_MAX / PSF_CLUMP_GRID_SCALE; // Size of sigma-plane image
     300        int numCols = 1 + SX_MAX / PSF_CLUMP_GRID_SCALE; // Size of sigma-plane image
     301        int numRows = 1 + SY_MAX / PSF_CLUMP_GRID_SCALE; // Size of sigma-plane image
    322302        psTrace("psModules.objects", 10, "sigma-plane dimensions: %dx%d\n", numCols, numRows);
    323303        psImage *splane = psImageAlloc(numCols, numRows, PS_TYPE_F32); // sigma-plane image
     
    332312            }
    333313
    334             int x = src->peak->x, y = src->peak->y; // Coordinates of peak
    335             if (x < region->x0 || x > region->x1 || y < region->y0 || y > region->y1) {
    336                 continue;
    337             }
     314            if (region) {
     315                int x = src->peak->x, y = src->peak->y; // Coordinates of peak
     316                if (x < region->x0 || x > region->x1 || y < region->y0 || y > region->y1) {
     317                    continue;
     318                }
     319            }
    338320
    339321            if (src->mode & PM_SOURCE_MODE_BLEND) {
    340322                continue;
    341323            }
     324
     325            if (!src->moments->nPixels) continue;
    342326
    343327            if (src->moments->SN < PSF_SN_LIM) {
     
    384368
    385369        // find the peak in this image
    386         psStats *stats = psStatsAlloc (PS_STAT_MAX);
     370        psStats *stats = psStatsAlloc (PS_STAT_MAX | PS_STAT_SAMPLE_STDEV);
    387371        if (!psImageStats (stats, splane, NULL, 0)) {
    388372            psError(PS_ERR_UNKNOWN, false, "Unable to get image statistics.\n");
     
    394378        psTrace ("psModules.objects", 2, "clump threshold is %f\n", stats[0].max/2);
    395379
    396         const bool keep_psf_clump = psMetadataLookupBool(NULL, recipe, "KEEP_PSF_CLUMP");
    397         if (keep_psf_clump)
    398         {
    399             psMetadataAdd(recipe, PS_LIST_TAIL,
    400                           "PSF_CLUMP", PS_DATA_IMAGE, "Image of PSF coefficients", splane);
     380        psfClump.nSigma = stats->sampleStdev;
     381
     382        if (savedImage) {
     383            *savedImage = psMemIncrRefCounter(splane);
    401384        }
    402385        psFree (splane);
     
    404387
    405388        // if we failed to find a valid peak, return the empty clump (failure signal)
    406         if (peaks == NULL)
     389        if (peaks == NULL) {
     390            psError(PS_ERR_UNKNOWN, false, "failure in peak analysis for PSF clump.\n");
     391            psFree (peaks);
     392            return emptyClump;
     393        }
     394
     395        if (peaks->n == 0)
    407396        {
    408397            psLogMsg ("psphot", 3, "failed to find a peak in the PSF clump image\n");
     
    412401                psLogMsg ("psphot", 3, "no significant peak\n");
    413402            }
     403            psFree (peaks);
    414404            return (emptyClump);
    415405        }
     
    430420        psTrace ("psModules.objects", 2, "clump is at %d, %d (%f)\n", clump->x, clump->y, clump->value);
    431421
     422        // XXX store the mean sigma?
     423        float meanSigma = psfClump.nSigma;
     424        psfClump.nStars = clump->value;
     425        psfClump.nSigma = clump->value / meanSigma;
     426
    432427        // define section window for clump
    433428        minSx = clump->x * PSF_CLUMP_GRID_SCALE - 2.0*PSF_CLUMP_GRID_SCALE;
     
    452447                continue;
    453448
    454             if (tmpSrc->peak->x < region->x0) continue;
    455             if (tmpSrc->peak->x > region->x1) continue;
    456             if (tmpSrc->peak->y < region->y0) continue;
    457             if (tmpSrc->peak->y > region->y1) continue;
     449            if (region) {
     450                if (tmpSrc->peak->x < region->x0) continue;
     451                if (tmpSrc->peak->x > region->x1) continue;
     452                if (tmpSrc->peak->y < region->y0) continue;
     453                if (tmpSrc->peak->y > region->y1) continue;
     454            }
    458455
    459456            if (tmpSrc->moments->Mxx < minSx)
     
    511508*****************************************************************************/
    512509
    513 bool pmSourceRoughClass(psRegion *region, psArray *sources, psMetadata *recipe, pmPSFClump clump, psImageMaskType maskSat)
     510bool pmSourceRoughClass(psRegion *region, psArray *sources, float PSF_SN_LIM, float PSF_CLUMP_NSIGMA, pmPSFClump clump, psImageMaskType maskSat)
    514511{
    515512    psTrace("psModules.objects", 10, "---- begin ----");
    516513
    517514    PS_ASSERT_PTR_NON_NULL(sources, false);
    518     PS_ASSERT_PTR_NON_NULL(recipe, false);
    519515
    520516    int Nsat     = 0;
     
    529525    psVector *starsn_peaks = psVectorAllocEmpty (sources->n, PS_TYPE_F32);
    530526    psVector *starsn_moments = psVectorAllocEmpty (sources->n, PS_TYPE_F32);
    531 
    532     // get basic parameters, or set defaults
    533     bool status;
    534     float PSF_SN_LIM = psMetadataLookupF32 (&status, recipe, "PSF_SN_LIM");
    535     if (!status) PSF_SN_LIM = 20.0;
    536     float PSF_CLUMP_NSIGMA = psMetadataLookupF32 (&status, recipe, "PSF_CLUMP_NSIGMA");
    537     if (!status) PSF_CLUMP_NSIGMA = 1.5;
    538 
    539     // float INNER_RADIUS = psMetadataLookupF32 (&status, recipe, "SKY_INNER_RADIUS");
    540527
    541528    pmSourceMode noMoments = PM_SOURCE_MODE_MOMENTS_FAILURE | PM_SOURCE_MODE_SKYVAR_FAILURE | PM_SOURCE_MODE_SKY_FAILURE | PM_SOURCE_MODE_BELOW_MOMENTS_SN;
     
    893880
    894881    // if we already have a cached image, re-use that memory
    895     source->psfFlux = psImageCopy (source->psfFlux, source->pixels, PS_TYPE_F32);
    896     psImageInit (source->psfFlux, 0.0);
     882    source->psfImage = psImageCopy (source->psfImage, source->pixels, PS_TYPE_F32);
     883    psImageInit (source->psfImage, 0.0);
    897884
    898885    // in some places (psphotEnsemble), we need a normalized version
    899886    // in others, we just want the model.  which is more commonly used?
    900     // psfFlux always has unity normalization (I0 = 1.0)
    901     pmModelAdd (source->psfFlux, source->maskObj, source->modelPSF, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
     887    // psfImage always has unity normalization (I0 = 1.0)
     888    pmModelAdd (source->psfImage, source->maskObj, source->modelPSF, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
    902889    return true;
    903890}
     
    918905    pmModel *model = pmSourceGetModel (NULL, source);
    919906    if (model == NULL) return false;  // model must be defined
     907
     908    bool addNoise = mode & PM_MODEL_OP_NOISE;
    920909
    921910    if (source->modelFlux) {
     
    940929
    941930        psF32 **target = source->pixels->data.F32;
    942         if (mode & PM_MODEL_OP_NOISE) {
    943             // XXX need to scale by the gain...
     931        if (addNoise) {
     932            // when adding noise, we assume the shape and Io have been modified
    944933            target = source->variance->data.F32;
    945934        }
    946935
    947         // XXX need to respect the source and model masks?
    948936        for (int iy = 0; iy < source->modelFlux->numRows; iy++) {
    949937            int oy = iy + dY;
     
    959947            }
    960948        }
     949        if (!addNoise) {
     950            if (add) {
     951                source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     952            } else {
     953                source->tmpFlags |= PM_SOURCE_TMPF_SUBTRACTED;
     954            }
     955        }
    961956        return true;
    962957    }
    963958
    964959    psImage *target = source->pixels;
    965     if (mode & PM_MODEL_OP_NOISE) {
     960    if (addNoise) {
    966961        target = source->variance;
    967962    }
    968963
    969     if (add) {
    970         status = pmModelAddWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
    971     } else {
    972         status = pmModelSubWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
     964    if (!addNoise) {
     965        if (add) {
     966            status = pmModelAddWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
     967            source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     968        } else {
     969            status = pmModelSubWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
     970            source->tmpFlags |= PM_SOURCE_TMPF_SUBTRACTED;
     971        }
    973972    }
    974973
     
    10601059    psF32 fA = (A->peak == NULL) ? 0 : A->peak->y;
    10611060    psF32 fB = (B->peak == NULL) ? 0 : B->peak->y;
     1061
     1062    psF32 diff = fA - fB;
     1063    if (diff > FLT_EPSILON) return (+1);
     1064    if (diff < FLT_EPSILON) return (-1);
     1065    return (0);
     1066}
     1067
     1068// sort by X (ascending)
     1069int pmSourceSortByX (const void **a, const void **b)
     1070{
     1071    pmSource *A = *(pmSource **)a;
     1072    pmSource *B = *(pmSource **)b;
     1073
     1074    psF32 fA = (A->peak == NULL) ? 0 : A->peak->x;
     1075    psF32 fB = (B->peak == NULL) ? 0 : B->peak->x;
    10621076
    10631077    psF32 diff = fA - fB;
Note: See TracChangeset for help on using the changeset viewer.