IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 6, 2011, 1:32:31 PM (15 years ago)
Author:
eugene
Message:
  • clarified the output to show the stages and their timing more clearly
  • psphotStackReadout now uses the same functions as psphotReadout (deprecated FitLinearStack, ExtendedAnalysisByObject, RadialAnalysisByObject, etc)
  • psphotStack now does 2nd (5 sigma) detection pass and radial photometry of the un-matched image (as well as the matched images)
  • for speed, fitting uses a smaller subset of pixels than subtraction: the model is extended when the subtraction is performed to avoid a discontinuity
  • extended source analysis (petrosians) is threaded
  • iterative kron radius / magnitude analysis with down-weighted neighbors replaces the masked neighbor kron analysis. the result of this analysis is used by fitting guesses and is the value written to the PSF and XFIT tables.
  • the target matched PSFs are determined in advance and the 0 element of this array is always the unmatched image (with target PSF of NAN)
  • dQ, the difference between the upper and lower quartile, is measured (but not yet used to adjust the sky level. sky should be FITTED_MEAN - 2.5*dQ
  • fake sources inserted for the efficiency analysis are removed at the end of the analysis
  • radial apertures is now threaded
  • edge case petrosian mags are now handled without aborts (may still have a problem with the measured error of the petrosian radius)
  • new sersic model guess based on the psf size, the moments, and the kron flux. the psf - kron mag is used to guess the index, then the index is used to convert measured moments into model guess parameters. this uses empirical relationships between the quantities generated from fake images.
  • define the fit region and window region based on the 1st radial moments
  • use the kron S/N to determine if analysis is performed for extended sources, not the psf S/N
  • remove the old 'basic deblend' which did not really do any deblending and is superceeded by the deblending in KronIterate and the footprint analysis
Location:
trunk/psphot
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot

  • trunk/psphot/src

    • Property svn:ignore
      •  

        old new  
        2222psphotMakePSF
        2323psphotStack
         24psphotModelTest
  • trunk/psphot/src/psphotExtendedSourceFits.c

    r31673 r32348  
    66    bool status = true;
    77
     8    fprintf (stdout, "\n");
     9    psLogMsg ("psphot", PS_LOG_INFO, "--- psphot Extended Source Fits ---");
     10
    811    // select the appropriate recipe information
    912    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     
    1215    // perform full extended source non-linear fits?
    1316    if (!psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS")) {
    14         psLogMsg ("psphot", PS_LOG_INFO, "skipping extended source measurements\n");
     17        psLogMsg ("psphot", PS_LOG_INFO, "skipping extended source fits\n");
    1518        return true;
    1619    }
     
    4548    int Nfail = 0;
    4649
     50    psphotSersicModelClassInit();
     51
    4752    psTimerStart ("psphot.extended");
    4853
     
    103108
    104109      if (item->type != PS_DATA_METADATA) {
     110        // XXX we could cull the bad entries or build a validated model folder
    105111        psAbort ("Invalid type for EXTENDED_SOURCE_MODEL entry %s, not a metadata folder", item->name);
    106         // XXX we could cull the bad entries or build a validated model folder
    107112      }
    108113
     
    247252    psFree(AnalysisRegion);
    248253
    249     psLogMsg ("psphot", PS_LOG_INFO, "extended source model fits: %f sec for %d objects\n", psTimerMark ("psphot.extended"), Next);
     254    psphotSersicModelClassCleanup();
     255
     256    psphotVisualShowResidualImage (readout, false);
     257
     258    psLogMsg ("psphot", PS_LOG_WARN, "extended source model fits: %f sec for %d objects\n", psTimerMark ("psphot.extended"), Next);
    250259    psLogMsg ("psphot", PS_LOG_INFO, "  %d convolved models (%d passed)\n", Nconvolve, NconvolvePass);
    251260    psLogMsg ("psphot", PS_LOG_INFO, "  %d plain models (%d passed)\n", Nplain, NplainPass);
     
    265274    int Nplain = 0;
    266275    int NplainPass = 0;
    267     bool savePics = false;
    268     float radius;
     276    float fitRadius, windowRadius;
    269277    psScalar *scalar = NULL;
    270     pmMoments psfMoments;
    271278
    272279    // arguments: readout, sources, models, region, psfSize, maskVal, markVal
     
    275282    psMetadata *models      = job->args->data[2];
    276283    psRegion *region        = job->args->data[3];
    277     int psfSize             = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     284    int psfSize             = PS_SCALAR_VALUE(job->args->data[4],S32);
    278285    psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[5],PS_TYPE_IMAGE_MASK_DATA);
    279286    psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[6],PS_TYPE_IMAGE_MASK_DATA);
    280 
    281     // pthread_t tid = pthread_self();     // Thread identifier
    282287
    283288    // Define source fitting parameters for extended source fits
     
    299304
    300305        // skip PSF-like and non-astronomical objects
    301         if (source->type == PM_SOURCE_TYPE_STAR) continue;
     306        if (!(source->mode & PM_SOURCE_MODE_EXT_LIMIT)) continue;
    302307        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    303308        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     
    309314        if (source->peak->y > region->y1) continue;
    310315
    311         // if model is NULL, we don't have a starting guess
    312         // XXX use the parameters guessed from moments
    313         // if (source->modelEXT == NULL) continue;
    314 
    315         // fprintf (stderr, "fit %d,%d in thread %d\n", source->peak->x, source->peak->y, (int) tid);
    316 
    317316        // replace object in image
    318317        if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) {
     
    321320        Next ++;
    322321
    323         // set the radius based on the footprint (also sets the mask pixels)
    324         if (!psphotSetRadiusFootprint(&radius, readout, source, markVal, 1.0)) {
    325             fprintf (stderr, "skipping (1) %f, %f\n", source->peak->xf, source->peak->yf);
    326             pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    327             // XXX raise an error of some kind
    328             continue;
    329         }
    330 
    331         // XXX note that this changes the source moments that are published...
    332         // recalculate the source moments using the larger extended-source moments radius
    333         // at this stage, skip Gaussian windowing, and do not clip pixels by S/N
    334         // this uses the footprint to judge both radius and aperture?
    335         // XXX save the psf-based moments for output
    336         psfMoments = *source->moments;
    337         if (!pmSourceMoments (source, radius, 0.0, 0.0, 0.0, maskVal)) {
    338             // subtract the best fit from the object, leave local sky
    339             fprintf (stderr, "skipping (2) %f, %f\n", source->peak->xf, source->peak->yf);
    340             *source->moments = psfMoments;
    341             pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    342             // XXX raise an error flag of some kind
    343             continue;
    344         }
    345 
    346         // save the modelFlux here in case we need to subtract it (for failure)
    347         psImage *modelFluxStart = psMemIncrRefCounter (source->modelFlux);
    348         if (!modelFluxStart) {
    349             pmSourceCacheModel (source, maskVal);
    350             modelFluxStart = psMemIncrRefCounter (source->modelFlux);
    351             if (!modelFluxStart) {
    352                 fprintf (stderr, "skipping (3) %f, %f\n", source->peak->xf, source->peak->yf);
    353                 *source->moments = psfMoments;
    354                 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    355                 // XXX raise an error of some kind?
    356                 continue;
    357             }
    358         }
    359        
    360         if (savePics) {
    361           psphotSaveImage (NULL, readout->image, "image.xp.fits");
    362         }
    363 
    364         // array to store the pointers to the model flux images while the models are being fitted
    365         psArray *modelFluxes = psArrayAllocEmpty (models->list->n);
     322        // set the fit radius based on the first radial moment (also sets the mask pixels)
     323        psphotSetRadiusMomentsExact(&fitRadius, &windowRadius, readout, source, markVal);
     324
     325        // UPDATE : we have changed the moments calculation.  There is now an iteration within
     326        // psphotKronMasked to determine moments appropriate for a larger object.  The values
     327        // Mrf, KronFlux, and KronFluxErr are calculated for the iterated radius.  The other
     328        // values are left at the psf-based values.
    366329
    367330        // allocate the array to store the model fits
     
    380343
    381344          // check the SNlim and skip model if source is too faint
    382           float SNlim = psMetadataLookupF32 (&status, model, "SNLIM_VALUE");
     345          float FIT_SN_LIM = psMetadataLookupF32 (&status, model, "SNLIM_VALUE");
    383346          assert (status);
    384347
    385348          // limit selection to some SN limit
    386349          // assert (source->peak); // how can a source not have a peak?
    387           if (sqrt(source->peak->detValue) < SNlim) {
     350          // limit selection to some SN limit
     351          bool skipSource = false;
     352          if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) {
     353              skipSource = (source->moments->KronFlux < FIT_SN_LIM * source->moments->KronFluxErr);
     354          } else {
     355              skipSource = (sqrt(source->peak->detValue) < FIT_SN_LIM);
     356          }
     357          if (skipSource) {
    388358              Nfaint ++;
    389359              continue;
     
    397367          bool convolved = psMetadataLookupBool (&status, model, "PSF_CONVOLVED_VALUE");
    398368          assert (status);
    399 
    400           // XXX psTraceSetLevel ("psLib.math.psMinimizeLMChi2", 6);
    401           // XXX psTraceSetLevel ("psphot.psphotModelWithPSF_LMM", 6);
    402369
    403370          // fit the model as convolved or not
     
    410377                  continue;
    411378              }
    412               psTrace ("psphot", 4, "fit psf-conv model for %f, %f : %s chisq = %f\n", source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq);
     379              psTrace ("psphot", 4, "fit psf-conv model for %f, %f : %s chisq = %f (npix: %d, niter: %d)\n",
     380                       source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter);
    413381              Nconvolve ++;
    414382              if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {
     
    425393                  continue;
    426394              }
    427               pmSourceCacheModel (source, maskVal);
    428               psTrace ("psphot", 4, "fit plain model for %f, %f : %s chisq = %f\n", source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq);
     395              psTrace ("psphot", 4, "fit plain model for %f, %f : %s chisq = %f (npix: %d, niter: %d)\n",
     396                       source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter);
    429397              Nplain ++;
    430398              if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {
     
    434402          }
    435403
    436           // save each of the model flux images and store the best
    437           psArrayAdd (modelFluxes, 4, source->modelFlux);
     404          // XXX deprecate?
     405          // XXX pmSourceExtFitPars *extFitPars = pmSourceExtFitParsAlloc();
     406          // XXX extFitPars->Mxx = source->moments->Mxx;
     407          // XXX extFitPars->Mxy = source->moments->Mxy;
     408          // XXX extFitPars->Myy = source->moments->Myy;
     409          // XXX extFitPars->Mrf = source->moments->Mrf;
     410          // XXX extFitPars->Mrh = source->moments->Mrh;
     411          // XXX extFitPars->peakMag = (source->peak->rawFlux > 0) ? -2.5*log10(source->peak->rawFlux) : NAN;
     412         
     413          // save kron mag, but assign apMag & psfMag on output (not yet calculated)
     414          // XXX extFitPars->krMag = -2.5*log10(source->moments->KronFlux);
     415
     416          // psArrayAdd (source->extFitPars, 4, extFitPars);
     417          // psFree (extFitPars);
    438418
    439419          // test for fit quality / result
    440           modelFit->fitRadius = radius;
     420          modelFit->fitRadius = fitRadius;
    441421          psArrayAdd (source->modelFits, 4, modelFit);
    442422
     
    463443        }
    464444
     445        // clear the circular mask
     446        psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
     447
    465448        if (minModel == -1) {
    466           // re-subtract the object, leave local sky
     449          // no valid extended fit; re-subtract the object, leave local sky
    467450          psTrace ("psphot", 5, "failed to fit extended source model to object at %f, %f", source->moments->Mx, source->moments->My);
    468451
    469           // replace original model, subtract it
    470           psFree (source->modelFlux);
    471           source->modelFlux = modelFluxStart;
    472 
    473           *source->moments = psfMoments;
     452          // ensure the modelEXT is cached
     453          pmSourceCacheModel (source, maskVal);
     454
    474455          pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    475456
    476           psFree (modelFluxes);
    477 
    478           if (savePics) {
    479               psphotSaveImage (NULL, readout->image, "image.xp.fits");
    480               char key[10];
    481               fprintf (stdout, "continue? ");
    482               if (!fgets (key, 8, stdin)) {
    483                   psWarning("Couldn't read anything.");
    484               } else if (key[0] == 'n') {
    485                   savePics = false;
    486               }
    487           }
    488457          continue;
    489458        }
     
    493462        source->modelEXT = psMemIncrRefCounter (source->modelFits->data[minModel]);
    494463
    495         // save the modelFlux for the best model
    496         psFree (source->modelFlux);
    497         source->modelFlux = psMemIncrRefCounter (modelFluxes->data[minModel]);
    498 
    499         // replace the original moments
    500         *source->moments = psfMoments;
     464        // adjust the window so the subtraction covers the faint wings
     465        psphotSetRadiusMoments(&fitRadius, &windowRadius, readout, source, markVal);
     466
     467        // cache the model flux
     468        if (source->modelEXT->isPCM) {
     469            pmPCMCacheModel (source, maskVal, psfSize);
     470        } else {
     471            pmSourceCacheModel (source, maskVal);
     472        }
    501473
    502474        // subtract the best fit from the object, leave local sky
    503475        pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    504476
    505         // the initial model flux is no longer needed
    506         psFree (modelFluxStart);
    507         psFree (modelFluxes);
    508 
    509477        psTrace ("psphot", 4, "best ext model for %f, %f : %s chisq = %f\n", source->moments->Mx, source->moments->My, pmModelClassGetName (source->modelEXT->type), source->modelEXT->chisq);
    510478        psTrace ("psphot", 5, "extended source model for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    511 
    512         if (savePics) {
    513           psphotSaveImage (NULL, readout->image, "image.xm.fits");
    514           char key[10];
    515           fprintf (stdout, "continue? ");
    516           if (!fgets (key, 8, stdin)) {
    517               psWarning("Couldn't read anything.");
    518           } else if (key[0] == 'n') {
    519               savePics = false;
    520           }
    521         }
    522479    }
    523480    psFree (fitOptions);
    524 
    525     // fprintf (stderr, "xfit : tried %ld objects\n", sources->n);
    526481
    527482    // change the value of a scalar on the array (wrap this and put it in psArray.h)
Note: See TracChangeset for help on using the changeset viewer.