IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7437


Ignore:
Timestamp:
Jun 8, 2006, 10:57:13 AM (20 years ago)
Author:
eugene
Message:

moved functions to correspond to order used

File:
1 edited

Legend:

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

    r7331 r7437  
    22
    33// given a source with an existing modelPSF, attempt a full PSF fit, subtract if successful
     4
     5bool psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf) {
     6
     7    float x, y, dR;
     8
     9    // if this source is not a possible blend, just fit as PSF
     10    if ((source->blends == NULL) || (source->mode & PM_SOURCE_MODE_SATSTAR)) {
     11        bool status = psphotFitPSF (readout, source, psf);
     12        return status;
     13    }
     14    psTrace ("psphot.blend", 5, "trying blend...\n");
     15
     16    // save the PSF model from the Ensemble fit
     17    pmModel *PSF = pmModelCopy (source->modelPSF);
     18    if (isnan(PSF->params->data.F32[1])) psAbort ("psphot", "nan in blend fit primary");
     19
     20    x = PSF->params->data.F32[2];
     21    y = PSF->params->data.F32[3];
     22
     23    psArray *modelSet = psArrayAlloc (source->blends->n + 1);
     24    psArrayAdd (modelSet, 16, PSF);
     25   
     26    psArray *sourceSet = psArrayAlloc (source->blends->n + 1);
     27    psArrayAdd (sourceSet, 16, source);
     28
     29    // we need to include all blends in the fit (unless primary is saturated?)
     30    dR = 0;
     31    for (int i = 0; i < source->blends->n; i++) {
     32        pmSource *blend = source->blends->data[i];
     33
     34        // find the blend which is furthest from source
     35        dR = PS_MAX (dR, hypot (blend->peak->x - x, blend->peak->y - y));
     36
     37        // create the model and guess parameters for this blend
     38        pmModel *model = pmModelAlloc (PSF->type);
     39        for (int j = 0; j < model->params->n; j++) {
     40            model->params->data.F32[j] = PSF->params->data.F32[j];
     41            model->dparams->data.F32[j] = PSF->dparams->data.F32[j];
     42        }
     43
     44        // XXX assume local sky is 0.0?
     45        model->params->data.F32[1] = blend->moments->Peak - blend->moments->Sky;
     46        if (isnan(model->params->data.F32[1])) psAbort ("psphot", "nan in blend fit");
     47        model->params->data.F32[2] = blend->peak->x;
     48        model->params->data.F32[3] = blend->peak->y;
     49
     50        // add this blend to the list
     51        psArrayAdd (modelSet, 16, model);
     52        psArrayAdd (sourceSet, 16, blend);
     53
     54        // free to avoid double counting model
     55        psFree (model);
     56    }
     57
     58    // extend source radius as needed
     59    psphotCheckRadiusPSFBlend (readout, source, PSF, dR);
     60
     61    // fit PSF model (set/unset the pixel mask)
     62    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "OR", PM_MASK_MARK);
     63    pmSourceFitSet (source, modelSet, PM_SOURCE_FIT_PSF);
     64    psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "AND", NOT_U8(PM_MASK_MARK));
     65
     66    // correct model chisq for flux trend
     67    double chiTrend = psPolynomial1DEval (psf->ChiTrend, PSF->params->data.F32[1]);
     68    PSF->chisqNorm = PSF->chisq / chiTrend;
     69
     70    // evaluate the blend objects, subtract if good, free otherwise
     71    for (int i = 1; i < modelSet->n; i++) {
     72        pmSource *blend = sourceSet->data[i];
     73        pmModel *model  = modelSet->data[i];
     74
     75        // correct model chisq for flux trend
     76        chiTrend = psPolynomial1DEval (psf->ChiTrend, model->params->data.F32[1]);
     77        model->chisqNorm = model->chisq / chiTrend;
     78
     79        // if this one failed, skip it
     80        if (!psphotEvalPSF (blend, model)) {
     81            psTrace ("psphot.blend", 5, "failed on blend %d of %d\n", i, modelSet->n);
     82            continue;
     83        }
     84
     85        // otherwise, supply the resulting model to the corresponding blend
     86        blend->modelPSF = psMemIncrRefCounter (model);
     87        psTrace ("psphot.blend", 5, "fitted blend as PSF\n");
     88        pmModelSub (source->pixels, source->mask, model, false, false);
     89        blend->mode |=  PM_SOURCE_MODE_SUBTRACTED;
     90        blend->mode &= ~PM_SOURCE_MODE_TEMPSUB;
     91    }
     92    psFree (modelSet);
     93    psFree (sourceSet);
     94
     95    // evaluate the primary object
     96    if (!psphotEvalPSF (source, PSF)) {
     97        psTrace ("psphot.blend", 5, "failed on blend 0 of %d\n", modelSet->n);
     98        psFree (PSF);
     99        return false;
     100    }
     101
     102    psTrace ("psphot.blend", 5, "fitted primary as PSF\n");
     103    pmModelSub (source->pixels, source->mask, PSF, false, false);
     104    psFree (source->modelPSF);
     105    source->modelPSF = PSF;
     106    source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
     107    source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
     108    return true;
     109}
    4110
    5111bool psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf) {
     
    26132    chiTrend = psPolynomial1DEval (psf->ChiTrend, PSF->params->data.F32[1]);
    27133    PSF->chisqNorm = PSF->chisq / chiTrend;
    28     // PSF->chisqNorm = PSF->chisq;
    29134
    30135    // does the PSF model succeed?
     
    43148    source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
    44149    source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
    45     return true;
    46 }
    47 
     150
     151    return true;
     152}
    48153
    49154static float EXT_MIN_SN;
     
    244349}
    245350
    246 bool psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf) {
    247 
    248     float x, y, dR;
    249 
    250     // if this source is not a possible blend, just fit as PSF
    251     if ((source->blends == NULL) || (source->mode & PM_SOURCE_MODE_SATSTAR)) {
    252         bool status = psphotFitPSF (readout, source, psf);
    253         return status;
    254     }
    255     psTrace ("psphot.blend", 5, "trying blend...\n");
    256 
    257     // save the PSF model from the Ensemble fit
    258     pmModel *PSF = pmModelCopy (source->modelPSF);
    259     if (isnan(PSF->params->data.F32[1])) psAbort ("psphot", "nan in blend fit primary");
    260 
    261     x = PSF->params->data.F32[2];
    262     y = PSF->params->data.F32[3];
    263 
    264     psArray *modelSet = psArrayAlloc (source->blends->n + 1);
    265     psArrayAdd (modelSet, 16, PSF);
    266    
    267     psArray *sourceSet = psArrayAlloc (source->blends->n + 1);
    268     psArrayAdd (sourceSet, 16, source);
    269 
    270     // we need to include all blends in the fit (unless primary is saturated?)
    271     dR = 0;
    272     for (int i = 0; i < source->blends->n; i++) {
    273         pmSource *blend = source->blends->data[i];
    274 
    275         // find the blend which is furthest from source
    276         dR = PS_MAX (dR, hypot (blend->peak->x - x, blend->peak->y - y));
    277 
    278         // create the model and guess parameters for this blend
    279         pmModel *model = pmModelAlloc (PSF->type);
    280         for (int j = 0; j < model->params->n; j++) {
    281             model->params->data.F32[j] = PSF->params->data.F32[j];
    282             model->dparams->data.F32[j] = PSF->dparams->data.F32[j];
    283         }
    284 
    285         // XXX assume local sky is 0.0?
    286         model->params->data.F32[1] = blend->moments->Peak - blend->moments->Sky;
    287         if (isnan(model->params->data.F32[1])) psAbort ("psphot", "nan in blend fit");
    288         model->params->data.F32[2] = blend->peak->x;
    289         model->params->data.F32[3] = blend->peak->y;
    290 
    291         // add this blend to the list
    292         psArrayAdd (modelSet, 16, model);
    293         psArrayAdd (sourceSet, 16, blend);
    294 
    295         // free to avoid double counting model
    296         psFree (model);
    297     }
    298 
    299     // extend source radius as needed
    300     psphotCheckRadiusPSFBlend (readout, source, PSF, dR);
    301 
    302     // fit PSF model (set/unset the pixel mask)
    303     psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "OR", PM_MASK_MARK);
    304     pmSourceFitSet (source, modelSet, PM_SOURCE_FIT_PSF);
    305     psImageKeepCircle (source->mask, x, y, PSF->radiusTMP, "AND", NOT_U8(PM_MASK_MARK));
    306 
    307     // correct model chisq for flux trend
    308     double chiTrend = psPolynomial1DEval (psf->ChiTrend, PSF->params->data.F32[1]);
    309     PSF->chisqNorm = PSF->chisq / chiTrend;
    310 
    311     // evaluate the blend objects, subtract if good, free otherwise
    312     for (int i = 1; i < modelSet->n; i++) {
    313         pmSource *blend = sourceSet->data[i];
    314         pmModel *model  = modelSet->data[i];
    315 
    316         // correct model chisq for flux trend
    317         chiTrend = psPolynomial1DEval (psf->ChiTrend, model->params->data.F32[1]);
    318         model->chisqNorm = model->chisq / chiTrend;
    319 
    320         // if this one failed, skip it
    321         if (!psphotEvalPSF (blend, model)) {
    322             psTrace ("psphot.blend", 5, "failed on blend %d of %d\n", i, modelSet->n);
    323             continue;
    324         }
    325 
    326         // otherwise, supply the resulting model to the corresponding blend
    327         blend->modelPSF = psMemIncrRefCounter (model);
    328         psTrace ("psphot.blend", 5, "fitted blend as PSF\n");
    329         pmModelSub (source->pixels, source->mask, model, false, false);
    330         blend->mode |=  PM_SOURCE_MODE_SUBTRACTED;
    331         blend->mode &= ~PM_SOURCE_MODE_TEMPSUB;
    332     }
    333     psFree (modelSet);
    334     psFree (sourceSet);
    335 
    336     // evaluate the primary object
    337     if (!psphotEvalPSF (source, PSF)) {
    338         psTrace ("psphot.blend", 5, "failed on blend 0 of %d\n", modelSet->n);
    339         psFree (PSF);
    340         return false;
    341     }
    342 
    343     psTrace ("psphot.blend", 5, "fitted primary as PSF\n");
    344     pmModelSub (source->pixels, source->mask, PSF, false, false);
    345     psFree (source->modelPSF);
    346     source->modelPSF = PSF;
    347     source->mode |=  PM_SOURCE_MODE_SUBTRACTED;
    348     source->mode &= ~PM_SOURCE_MODE_TEMPSUB;
    349     return true;
    350 }
Note: See TracChangeset for help on using the changeset viewer.