IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 7, 2014, 5:38:26 AM (12 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ipp-20140423/psphot/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20140423/psphot/src

  • branches/eam_branches/ipp-20140423/psphot/src/psphotGalaxyShape.c

    r36633 r36828  
    8080    assert (markVal);
    8181
    82     // maskVal is used to test for rejected pixels, and must include markVal
    8382    maskVal |= markVal;
    8483
    85     // bit-mask to mark pixels not used in analysis
    8684    psphotGalaxyShapeOptions *opt = psphotGalaxyShapeOptionsAlloc();
     85    opt->Q = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_Q");
     86    psAssert (status, "missing GALAXY_SHAPES_Q");
     87    opt->NSigma = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_NSIGMA");
     88    psAssert (status, "missing GALAXY_SHAPES_NSIGMA");
     89    opt->clampSN = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_CLAMP_SN");
     90    psAssert (status, "missing GALAXY_SHAPES_NSIGMA");
     91    psString modelTypeToSave = psMetadataLookupStr(&status, recipe, "EXT_MODEL_TYPE_FORCE");
     92    if (modelTypeToSave && strcmp(modelTypeToSave, "ALL") && strcmp(modelTypeToSave, "BEST")) {
     93        opt->extModelType = pmModelClassGetType(modelTypeToSave);
     94    } else {
     95        opt->extModelType = pmModelClassGetType("PS_MODEL_SERSIC");
     96    }
     97
     98#ifdef notdef
    8799    opt->fRmajorMin = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_FR_MAJOR_MIN"); if (!status) opt->fRmajorMin = 0.5;
    88100    opt->fRmajorMax = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_FR_MAJOR_MAX"); if (!status) opt->fRmajorMax = 2.0;
     
    91103    opt->fRminorMax = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_FR_MINOR_MAX"); if (!status) opt->fRminorMax = 2.0;
    92104    opt->fRminorDel = psMetadataLookupF32(&status, recipe, "GALAXY_SHAPES_FR_MINOR_DEL"); if (!status) opt->fRminorDel = 0.1;
     105#endif
    93106
    94107    // what fraction of the PSF is used? (radius in pixels : 2 -> 5x5 box)
     
    238251bool psphotGalaxyShapeGrid (pmSource *source, pmSourceFitOptions *fitOptions, psphotGalaxyShapeOptions *opt, psImageMaskType maskVal, int psfSize) {
    239252
    240     // we use the 0th model to define the initial guess shape
    241     pmModel *model = source->modelFits->data[0];
    242     if (!model) return false;
    243 
    244     pmModelType modelType = model->type;
    245 
    246     // we are using fitOptions->mode : be sure this makes sense
    247     pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
    248     if (!pcm) return false;
    249 
    250     // we are fitting only PM_PAR_I0; the shape elements are generated from a grid
    251     psF32 *PAR = pcm->modelConv->params->data.F32;
    252 
    253     // double check that the guess is carried along...
    254 
    255     // I have some source guess (e0, e1, e2)
    256     psEllipseAxes guessAxes = pmPSF_ModelToAxes (PAR, modelType);
    257 
    258     if (!source->galaxyFits) {
    259         source->galaxyFits = pmSourceGalaxyFitsAlloc();
    260     }
    261 
    262     float fRmajorBest = NAN;
    263     float fRminorBest = NAN;
    264     float chisqBest = NAN;
    265     for (float fRmajor = opt->fRmajorMin; fRmajor < opt->fRmajorMax + 0.5*opt->fRmajorDel; fRmajor += opt->fRmajorDel) {
    266         for (float fRminor = opt->fRminorMin; fRminor < opt->fRminorMax + 0.5*opt->fRminorDel; fRminor += opt->fRminorDel) {
    267  
    268             psEllipseAxes testAxes = guessAxes;
    269             testAxes.major = guessAxes.major * fRmajor;
    270             testAxes.minor = guessAxes.minor * fRminor;
    271            
    272             pmPSF_AxesToModel (PAR, testAxes, modelType);
    273            
    274             psphotGalaxyShapeSource (pcm, source, maskVal, psfSize, true);
    275 
    276             int i = source->galaxyFits->chisq->n - 1;
    277             float flux = source->galaxyFits->Flux->data.F32[i];
    278             if (isfinite(flux)) {
    279                 float thisChisq = source->galaxyFits->chisq->data.F32[i];
    280                 if (isfinite(thisChisq) && isfinite(flux) && (!isfinite(chisqBest) || thisChisq < chisqBest)) {
    281                     chisqBest = thisChisq;
    282                     fRmajorBest = fRmajor;
    283                     fRminorBest = fRminor;
     253
     254    for (int iModel = 0 ; iModel < source->modelFits->n; iModel++) {
     255        pmModel *model = source->modelFits->data[iModel];
     256        if (!model) return false;
     257
     258        pmModelType modelType = model->type;
     259        model->flags = PM_MODEL_STATUS_NONE;
     260
     261        // we are using fitOptions->mode : be sure this makes sense
     262        pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
     263        if (!pcm) return false;
     264
     265        // we are fitting only PM_PAR_I0; the shape elements are generated from a grid
     266        psF32 *PAR = pcm->modelConv->params->data.F32;
     267
     268        pmSourceGalaxyFits *galaxyFits = pmSourceGalaxyFitsAlloc();
     269
     270        // set the options for this source
     271        if (!psphotGalaxyShapeOptionsSet(source, galaxyFits, opt)) {
     272            // this source won't work
     273            psFree(pcm);
     274            psFree(galaxyFits);
     275            continue;
     276        }
     277
     278        if (!source->galaxyFits) {
     279            source->galaxyFits = psArrayAllocEmpty(1);
     280        }
     281        psArrayAdd(source->galaxyFits, 1, galaxyFits);
     282        psFree(galaxyFits);
     283
     284        // I have some source guess (e0, e1, e2)
     285        psEllipseAxes guessAxes = pmPSF_ModelToAxes (PAR, modelType);
     286
     287        float fRmajorBest = NAN;
     288        float fRminorBest = NAN;
     289        float chisqBest = NAN;
     290        for (float fRmajor = galaxyFits->fRmajorMin; fRmajor < galaxyFits->fRmajorMax + 0.5*galaxyFits->fRmajorDel; fRmajor += galaxyFits->fRmajorDel) {
     291            for (float fRminor = galaxyFits->fRminorMin; fRminor < galaxyFits->fRminorMax + 0.5*galaxyFits->fRminorDel; fRminor += galaxyFits->fRminorDel) {
     292     
     293                psEllipseAxes testAxes = guessAxes;
     294                testAxes.major = guessAxes.major * fRmajor;
     295                testAxes.minor = guessAxes.minor * fRminor;
     296               
     297                pmPSF_AxesToModel (PAR, testAxes, modelType);
     298               
     299                psphotGalaxyShapeSource (pcm, source, galaxyFits, maskVal, psfSize, true);
     300
     301                int i = galaxyFits->chisq->n - 1;
     302                float flux = galaxyFits->Flux->data.F32[i];
     303                if (isfinite(flux)) {
     304                    float thisChisq = galaxyFits->chisq->data.F32[i];
     305                    if (isfinite(thisChisq) && isfinite(flux) && (!isfinite(chisqBest) || thisChisq < chisqBest)) {
     306                        chisqBest = thisChisq;
     307                        fRmajorBest = fRmajor;
     308                        fRminorBest = fRminor;
     309                    }
    284310                }
     311                // reset I0 to avoid potential problems on the next iteration
     312                PAR[PM_PAR_I0] = 1.0;
    285313            }
    286             // reset I0 to avoid potential problems on the next iteration
    287             PAR[PM_PAR_I0] = 1.0;
    288         }
    289     }
    290 
    291 #define SAVE_BEST_MODEL
    292 #ifdef SAVE_BEST_MODEL
    293     // Save model with smallest chisq
    294     if (isfinite(chisqBest)) {
    295 #else
    296     // Save model with nominal parameters
    297     {
    298         fRmajorBest = 1;
    299         fRminorBest = 1;
     314        }
     315
     316        if (isfinite(chisqBest)) {
     317            // now save the best fitting model as the source's extended model ...
     318            psEllipseAxes testAxes = guessAxes;
     319
     320            // ... unless this macro is defined
     321#ifndef SAVE_NOMINAL_MODEL
     322            testAxes.major = guessAxes.major * fRmajorBest;
     323            testAxes.minor = guessAxes.minor * fRminorBest;
    300324#endif
    301         // now save the best fitting model as the source's extended model
    302         psEllipseAxes testAxes = guessAxes;
    303         testAxes.major = guessAxes.major * fRmajorBest;
    304         testAxes.minor = guessAxes.minor * fRminorBest;
    305        
    306         pmPSF_AxesToModel (PAR, testAxes, modelType);
    307        
    308         psphotGalaxyShapeSource (pcm, source, maskVal, psfSize, false);
    309 
    310         // Replace modelEXT with this model, if the model is good
    311         if (isfinite(PAR[PM_PAR_I0])) {
    312             psFree (source->modelEXT);
    313 
    314             source->modelEXT = psMemIncrRefCounter (pcm->modelConv);
    315             source->type = PM_SOURCE_TYPE_EXTENDED;
    316             source->mode |= PM_SOURCE_MODE_EXTMODEL;
    317             source->mode |= PM_SOURCE_MODE_NONLINEAR_FIT;
    318 
    319             // cache the model flux
    320             pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma);
    321         }
    322     }
    323 
    324     psFree (pcm);
     325           
     326            pmPSF_AxesToModel (PAR, testAxes, modelType);
     327               
     328            psphotGalaxyShapeSource (pcm, source, galaxyFits, maskVal, psfSize, false);
     329
     330            // Replace modelEXT with the best model from the first of the model fits if one of them is good
     331            if (isfinite(PAR[PM_PAR_I0]) && modelType == opt->extModelType) {
     332                psFree (source->modelEXT);
     333
     334                source->modelEXT = psMemIncrRefCounter (pcm->modelConv);
     335                source->type = PM_SOURCE_TYPE_EXTENDED;
     336                source->mode |= PM_SOURCE_MODE_EXTMODEL;
     337                source->mode |= PM_SOURCE_MODE_NONLINEAR_FIT;
     338
     339                // cache the model flux
     340                pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma);
     341            }
     342        }
     343
     344        psFree (pcm);
     345    }
    325346    return true;
    326347}
     
    328349// fit the given model to the source and find chisq & normalization
    329350// XXX is this a single-component model? sersic with a supplied index, Reff, axis ratio, and theta?
    330 bool psphotGalaxyShapeSource (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, int psfSize, bool saveResults) {
     351bool psphotGalaxyShapeSource (pmPCMdata *pcm, pmSource *source, pmSourceGalaxyFits *galaxyFits, psImageMaskType maskVal, int psfSize, bool saveResults) {
    331352
    332353    PS_ASSERT_PTR_NON_NULL(source, false);
     
    384405
    385406    if (saveResults) {
    386         psVectorAppend (source->galaxyFits->Flux, flux);
    387         psVectorAppend (source->galaxyFits->dFlux, dflux);
    388         psVectorAppend (source->galaxyFits->chisq, Chisq);
    389         source->galaxyFits->nPix = nPix;
     407        psVectorAppend (galaxyFits->Flux, flux);
     408        psVectorAppend (galaxyFits->dFlux, dflux);
     409        psVectorAppend (galaxyFits->chisq, Chisq);
     410        galaxyFits->nPix = nPix;
     411        galaxyFits->modelType = pcm->modelConv->type;
    390412    }
    391413
     
    404426    psMemSetDeallocator(opt, (psFreeFunc) psphotGalaxyShapeOptionsFree);
    405427   
    406     opt->fRmajorMin = 0.5;
    407     opt->fRmajorMax = 2.0;
    408     opt->fRmajorDel = 0.1;
    409     opt->fRminorMin = 0.5;
    410     opt->fRminorMax = 2.0;
    411     opt->fRminorDel = 0.1;
    412 
    413428    return opt;
    414429}
    415430
     431bool psphotGalaxyShapeOptionsSet(pmSource *source, pmSourceGalaxyFits *galaxyFits, psphotGalaxyShapeOptions *options)
     432{
     433    // XXX: put these in recipe
     434    // doesn't make sense to use too small of a spacing
     435    float clampedSN = source->extSN < options->clampSN ? source->extSN : options->clampSN;
     436
     437    float f_del = options->Q / clampedSN;
     438    float f_min = 1 - options->NSigma * f_del;
     439    float f_max = 1 + options->NSigma * f_del;
     440
     441    // if f_min goes negative skip this object
     442    if (f_min < 0) return false;
     443
     444    galaxyFits->fRmajorMin = f_min;
     445    galaxyFits->fRmajorMax = f_max;
     446    galaxyFits->fRmajorDel = f_del;
     447    galaxyFits->fRminorMin = f_min;
     448    galaxyFits->fRminorMax = f_max;
     449    galaxyFits->fRminorDel = f_del;
     450
     451    return true;
     452}
     453
Note: See TracChangeset for help on using the changeset viewer.