IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38383


Ignore:
Timestamp:
Jun 5, 2015, 12:26:38 PM (11 years ago)
Author:
bills
Message:

Various changes that make psphotStack -updatemode get good residual images for
extended sources and psfs that were modeled well originally

File:
1 edited

Legend:

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

    r38353 r38383  
    5858    }
    5959
    60     // the intitial run.
     60    // Load the psf from the previous run.
     61    // Note this must be done before we load the sources because it is used.
     62    // I believe only to set the residuals
    6163    if (!psphotLoadPSF (config, view, STACK_RAW)) {
    6264        // this only happens if we had a programming error in psphotLoadPSF
     
    7880    }
    7981
     82#ifdef MEASURE_PSF
     83    if (!psphotChoosePSF (config, view, STACK_RAW, true)) {
     84        psLogMsg ("psphot", 3, "failure to construct a psf model");
     85        return psphotReadoutCleanup (config, view, STACK_RAW);
     86    }
     87    if (!strcasecmp (breakPt, "PSFMODEL")) {
     88        return psphotReadoutCleanup (config, view, STACK_RAW);
     89    }
     90#endif
     91
    8092    if (!psphotDeblendSatstars (config, view, STACK_RAW)) {
    8193        psError (PSPHOT_ERR_UNKNOWN, false, "failed on satstar deblend analysis");
    8294        return psphotReadoutCleanup (config, view, STACK_RAW);
    8395    }
     96
    8497    if (!psphotMergeSources (config, view, STACK_RAW)) {
    8598        psError (PSPHOT_ERR_UNKNOWN, false, "failed to merge sources");
     
    218231        COPY_PARAM( "MSKY_NX" );
    219232        COPY_PARAM( "MSKY_NY" );
     233        COPY_PARAM( "CHIP_SEEING" );
    220234}
    221235
     
    294308    psphotInitRadiusEXT (recipe, readout);
    295309
     310    // not really fitting but need to get some things set up to instantiate the pcm models
     311
     312    pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc();
     313    fitOptions->mode = PM_SOURCE_FIT_EXT_AND_SKY;
     314    fitOptions->covarFactor    = psImageCovarianceFactorForAperture(readout->covariance, 10.0); // Covariance matrix
     315    // XXX: change all of this status checking to asserts. All should be defined with the current recipe.
     316    float fitNsigmaConv = psMetadataLookupF32 (&status, recipe, "EXT_FIT_NSIGMA_CONV"); // number of sigma for the convolution
     317    if (!status || !isfinite(fitNsigmaConv) || fitNsigmaConv <= 0) {
     318            fitNsigmaConv = 5.0;
     319    }
     320    fitOptions->nsigma         = fitNsigmaConv;
     321    // Poisson or Constant weight for chisq tests?
     322    fitOptions->poissonErrors = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS_POISSON");
     323    if (!status) fitOptions->poissonErrors = true;
     324    int psfSize  = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
     325    assert (status);
     326
    296327    for (int i = 0; i < sources->n; i++) {
    297328        pmSource *source = sources->data[i];
     329        source->imageID = index;     // XXX is this necessary?
    298330        pmModel *modelPSF = source->modelPSF;
    299         if (!modelPSF) {
    300             continue;
    301         }
    302331        // free any previous radial aperture measurements
    303332        psFree(source->radialAper);
    304333
    305334        // Guess Models ususally does this
    306         psphotCheckRadiusPSF (readout, source,  modelPSF, markVal);
    307         source->modelPSF->residuals = psf->residuals;
     335        if (modelPSF) {
     336            psphotCheckRadiusPSF (readout, source,  modelPSF, markVal);
     337            source->modelPSF->residuals = psf->residuals;
     338        }
     339        if (!(source->mode & PM_SOURCE_MODE_PSFMODEL)) {
     340            // The cmf loaders unconditionallly create psf models even if the source did not have one.
     341            fprintf(stderr, "source %d %5d should not have a psf model 0x%08X%08X\n", index, source->seq, source->mode2, source->mode);
     342            psFree(source->modelPSF) ;
     343        }
    308344
    309345        // make sure that the window radius is large enough. Should we do this regardless of whether
     
    318354        }
    319355       
     356        // If we find a good model we will cache it below.
    320357        bool goodModel = false;
    321         // the cmf readers do not set PM_PAR_I0 for the models.
    322         // Calculate it here.
     358
     359        // the cmf readers do not set PM_PAR_I0 for the models so we calculate it here.
    323360        // We may want to put this code in the cmf readers but for now I don't want to change psphot operation
    324         // for anything except psphotStack -updatemode
    325         if (isfinite(source->psfFlux)) {
     361        // for anything except psphotStack -updatemode.
     362        if (modelPSF && isfinite(source->psfFlux)) {
     363            // The cmf reader is incorrectly setting the sky parameter for psf models
     364            modelPSF->params->data.F32[PM_PAR_SKY] = 0.0;
     365            modelPSF->dparams->data.F32[PM_PAR_SKY] = 0.0;
     366            // Calculate flux for normalized model
     367            float saveI0 = modelPSF->params->data.F32[PM_PAR_I0];
    326368            modelPSF->params->data.F32[PM_PAR_I0] = 1.0;
    327369            float normFlux = modelPSF->class->modelFlux(modelPSF->params);
    328             modelPSF->params->data.F32[PM_PAR_I0] =  source->psfFlux / normFlux;
    329             goodModel = true;
    330         }
    331         if (source->modelFits && source->modelFits->n) {
    332             for (int m = 0; m < source->modelFits->n; m++) {
    333                 pmModel *model = source->modelFits->data[m];
    334                 if (isfinite(model->mag)) {
    335                     // calculate flux from model magnitude
    336                     float modelFlux = pow(10., -0.4 * model->mag);
    337                     model->params->data.F32[PM_PAR_I0] = 1.0;
    338                     float normFlux = modelPSF->class->modelFlux(modelPSF->params);
    339                     model->params->data.F32[PM_PAR_I0] =  modelFlux / normFlux;
    340                     if (model == source->modelEXT) {
    341                         goodModel = true;
    342                     }
    343                 } else {
    344                     // nan model magnitude no way to calculate the flux.
    345                     // The flux was probably negative.
    346                     // If this is the extended model for this source we can't use the model.
    347                     // Set source type to star to prevent source subtraction from crashing.
    348                     // This is somewhat rare .2% of extened sources. Is there another way that we can estimate
    349                     // the flux, or more to the point the value for PM_PAR_I0?
    350                     if (model == source->modelEXT && source->type == PM_SOURCE_TYPE_EXTENDED) {
    351                         source->type = PM_SOURCE_TYPE_STAR;
    352                         goodModel = false;
    353                     }
     370            float psfFlux = source->psfFlux;
     371#define APPLY_APCORR
     372#ifdef APPLY_APCORR
     373            double apTrend = pmTrend2DEval (psf->ApTrend, (float)source->peak->x, (float)source->peak->y);
     374            double adjustment = pow(10.0, -0.4*apTrend);
     375            if (isfinite(adjustment) && adjustment != 0.0) {
     376                psfFlux = psfFlux / adjustment;
     377            } else {
     378                fprintf(stderr, "apTrend adjustment for source %5d invalid: %f\n", source->seq, adjustment);
     379            }
     380#endif
     381            float newI0 =  psfFlux / normFlux;
     382            if (isfinite (newI0) ) {
     383                // psf model looks to be good.
     384                modelPSF->params->data.F32[PM_PAR_I0] = newI0;
     385#ifdef USE_SAVED_I0
     386                // I am testing with a modified cmf that saves PM_PAR_I0
     387                modelPSF->params->data.F32[PM_PAR_I0] = saveI0;
     388#endif
     389                // fprintf (stderr, "%5d %12f %12f %12f\n", source->seq, saveI0, newI0, saveI0 - newI0);
     390                goodModel = true;
     391            } else {
     392                psAssert(isfinite (modelPSF->params->data.F32[PM_PAR_I0]), "got infinite I0 for psf model");
     393            }
     394        }
     395
     396        // It is rather expensive initializing all of the extended models. Lets only initialize
     397        // the extended model that will be used if any.
     398
     399        bool isPSF = false;
     400        pmModel *model = pmSourceGetModel (&isPSF, source);
     401        if (model && !isPSF) {
     402            // selected model is extended
     403            goodModel = false;
     404            // calculate flux from the previously measured model magnitude. It will be nan if the
     405            // flux was negative unfortunately.
     406            if (isfinite(model->mag)) {
     407                float modelFlux = pow(10., -0.4 * model->mag);
     408
     409                // XXX: We are assuming here that we only use PCM models. Get from recipe.
     410                pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
     411                if (!pcm) {
     412                    psError (PSPHOT_ERR_UNKNOWN, false, "failed to to initialize PCM for model");
     413                    return false;
    354414                }
     415                model->params->data.F32[PM_PAR_I0] = 1.0;
     416                pmPCMMakeModel (source, model, pcm->nsigma, maskVal, psfSize);
     417                float normFlux = model->class->modelFlux(model->params);
     418                float I0 = modelFlux / normFlux;
     419                if (isfinite(I0)) {
     420                    model->params->data.F32[PM_PAR_I0] = I0;
     421                    goodModel = true;
     422                    // Usually this it is set when doing the fit. Since we are instantiating the model we
     423                    // set it explicitly.
     424                    model->isPCM = true;
     425                }
     426                psFree(pcm);
     427            }
     428            if (!goodModel) {
     429                // We do not have a usable extended model for this source.
     430                // The original flux was probably negative.
     431                // This is somewhat rare. I saw it in about .2% of extended sources in my first test.
     432                // Set source type to star to prevent source subtraction from crashing.
     433                // this may cause model psf to be used in places.
     434                // XXX However we aren't going to cache it below.
     435                source->type = PM_SOURCE_TYPE_STAR;
     436                model = NULL;
    355437            }
    356438        }
     
    373455        }
    374456
    375         // Should we use pmPCMCacheModel for extended sources?
    376         // XXX: are the extended models ready?
    377         if (goodModel) {
    378             pmSourceCacheModel (source, maskVal); // ALLOC x14 (!)
    379         }
    380     }
    381 
    382     // as a test drop psf so we can try and remeasure it
    383 //    psMetadataRemoveKey(readout->analysis, "PSPHOT.PSF");
     457        if (goodModel && model) {
     458            if (model->isPCM) {
     459                pmPCMCacheModel (source, maskVal, psfSize, fitNsigmaConv);
     460            } else {
     461                pmSourceCacheModel (source, maskVal);
     462            }
     463        }
     464    }
     465
     466    psFree(fitOptions);
     467
     468#ifdef MEASURE_PSF
     469    // need to drop the psf model in order for it to be re-measured
     470    // Hmm, will this invalidate the results from above where we used the loaded one?
     471    psMetadataRemoveKey(readout->analysis, "PSPHOT.PSF");
     472#endif
    384473
    385474    return true;
Note: See TracChangeset for help on using the changeset viewer.