IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 10, 2013, 2:55:11 PM (13 years ago)
Author:
eugene
Message:

merge changes from eam_branches/ipp-20130904

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/psphot

  • trunk/psphot/src

  • trunk/psphot/src/psphotExtendedSourceFits.c

    r36108 r36375  
    6565    psphotVisualShowImage(readout);
    6666
    67     // psphotSaveImage (NULL, readout->image, "test.01.fits");
    68 
    6967    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    7068    psAssert (detections, "missing detections?");
     
    111109    if (!status || !isfinite(fitMaxTol) || fitMaxTol <= 0) {
    112110        fitMaxTol = 1.0;
     111    }
     112
     113    float fitNsigmaConv = psMetadataLookupF32 (&status, recipe, "EXT_FIT_NSIGMA_CONV"); // number of sigma for the convolution
     114    if (!status || !isfinite(fitNsigmaConv) || fitNsigmaConv <= 0) {
     115        fitNsigmaConv = 5.0;
    113116    }
    114117
     
    137140    fitOptions->minTol         = fitMinTol;
    138141    fitOptions->maxTol         = fitMaxTol;
     142    fitOptions->nsigma         = fitNsigmaConv;
    139143
    140144    fitOptions->gainFactorMode   = gainFactorMode;
    141145    fitOptions->chisqConvergence = chisqConvergence;
    142146    fitOptions->isInteractive    = isInteractive;
     147
     148    // use poissonian errors or local-sky errors
     149    fitOptions->poissonErrors = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS_POISSON");
     150    if (!status) fitOptions->poissonErrors = true;
    143151
    144152    // maskVal is used to test for rejected pixels, and must include markVal
     
    285293            psArrayAdd(job->args, 1, cells->data[j]); // sources
    286294            psArrayAdd(job->args, 1, models);
     295
    287296            // Allocate a metadata iterator here because psMetadataIteratorAlloc/Free are not thread safe
    288297            psMetadataIterator *iter = psMetadataIteratorAlloc (models, PS_LIST_HEAD, NULL);
     
    386395    psphotSersicModelClassCleanup();
    387396
    388     // psphotSaveImage (NULL, readout->image, "test.02.fits");
    389 
    390397    psphotVisualShowResidualImage (readout, false);
    391398
     
    428435    // psTraceSetLevel ("psLib.math.psMinimizeLMChi2_Alt", 5);
    429436
     437    pmModelStatus badModel = PM_MODEL_STATUS_NONE;
     438    badModel |= PM_MODEL_STATUS_BADARGS;
     439    badModel |= PM_MODEL_STATUS_OFFIMAGE;
     440    badModel |= PM_MODEL_STATUS_NAN_CHISQ;
     441    badModel |= PM_MODEL_SERSIC_PCM_FAIL_GUESS;
     442    badModel |= PM_MODEL_SERSIC_PCM_FAIL_GRID;
     443    badModel |= PM_MODEL_PCM_FAIL_GUESS;
     444
    430445    // choose the sources of interest
    431446    for (int i = 0; i < sources->n; i++) {
     
    433448        pmSource *source = sources->data[i];
    434449
    435         // skip PSF-like and non-astronomical objects
    436         if (!(source->mode & PM_SOURCE_MODE_EXT_LIMIT)) continue;
    437         if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    438         if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    439 
    440         // skip saturated stars modeled with a radial profile
    441         if (source->mode2 & PM_SOURCE_MODE2_SATSTAR_PROFILE) continue;
    442 
    443         // XXX this should use peak?
     450        // rules for measuring petrosian parameters for specific objects are set in
     451        // psphotChooseAnalysisOptions.c
     452        if (!(source->tmpFlags & PM_SOURCE_TMPF_EXT_FIT)) continue;
     453
     454        // limit selection by analysis region (XXX move this into psphotChooseAnalysisOption?)
    444455        if (source->peak->x < region->x0) continue;
    445456        if (source->peak->y < region->y0) continue;
    446457        if (source->peak->x > region->x1) continue;
    447458        if (source->peak->y > region->y1) continue;
    448 
    449 
    450         // XXX for a test, just do the obvious trail
    451         // XXX if (source->peak->xf < 1100) continue;
    452         // XXX if (source->peak->xf > 1400) continue;
    453         // XXX if (source->peak->yf >  245) continue;
    454459
    455460        // replace object in image
     
    462467        psphotSetRadiusMomentsExact(&fitRadius, &windowRadius, readout, source, markVal); // NOTE : 6 allocs
    463468
    464         // XXX WATCH OUT HERE!!
    465         // fitRadius = 30;
    466 
    467469        // UPDATE : we have changed the moments calculation.  There is now an iteration within
    468470        // psphotKronMasked to determine moments appropriate for a larger object.  The values
     
    475477        }
    476478
     479# ifdef TEST_OBJECT
    477480        bool testObject = false;
    478         // testObject |= ((fabs(source->peak->xf -  179) < 5) && (fabs(source->peak->yf - 1138) < 5));
    479         // testObject |= ((fabs(source->peak->xf - 5047) < 5) && (fabs(source->peak->yf -  151) < 5));
    480         // testObject |= ((fabs(source->peak->xf - 3929) < 5) && (fabs(source->peak->yf - 4109) < 5));
    481         // testObject |= ((fabs(source->peak->xf -  915) < 5) && (fabs(source->peak->yf - 5998) < 5));
    482         // testObject |= ((fabs(source->peak->xf - 5406) < 5) && (fabs(source->peak->yf -  326) < 5));
     481        testObject |= ((fabs(source->peak->xf -  179) < 5) && (fabs(source->peak->yf - 1138) < 5));
    483482        if (testObject) {
    484483            fprintf (stderr, "test object @ %f, %f\n", source->peak->xf, source->peak->yf);
     
    486485            psTraceSetLevel ("psphot.psphotExtendedSourceFits_Threaded", 5);
    487486        }
     487# endif
    488488
    489489        // loop here over the models chosen for each source (exclude by S/N)
     
    501501          assert (status);
    502502
    503           // limit selection to some SN limit
    504           bool skipSource = false;
    505           if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) {
    506               skipSource = (source->moments->KronFlux < FIT_SN_LIM * source->moments->KronFluxErr);
    507           } else {
    508               skipSource = (sqrt(source->peak->detValue) < FIT_SN_LIM);
    509           }
    510           if (skipSource) {
     503          // limit selection to some SN limit for specific models (this value only applies if > EXTENDED_SOURCE_SN_LIM)
     504          if (isfinite(FIT_SN_LIM)) {
     505            if (source->moments->KronFlux < FIT_SN_LIM * source->moments->KronFluxErr) {
    511506              Nfaint ++;
    512507              continue;
     508            }
    513509          }
     510
     511          source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_RUN;
    514512
    515513          // check on the model type
     
    529527                  psTrace ("psphot", 5, "failed to fit psf-conv model for object at %f, %f", source->moments->Mx, source->moments->My);
    530528                  Nfail ++;
     529                  source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_FAIL;
    531530                  continue;
    532531              }
     
    534533                       source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter);
    535534              Nconvolve ++;
    536               if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {
     535              if (!(modelFit->flags & badModel)) {
    537536                  NconvolvePass ++;
    538537                  source->mode |= PM_SOURCE_MODE_EXTENDED_FIT;
     
    548547                      Nfail ++;
    549548                      doneFits = true;
     549                      source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_FAIL;
    550550                      continue;
    551551                  }
    552552                  psTrace ("psphot", 4, "fit plain model for %f, %f : %s chisq = %f (npix: %d, niter: %d)\n", source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter);
    553553                  Nplain ++;
    554                   if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {
     554                  if (!(modelFit->flags & badModel)) {
    555555                      NplainPass ++;
    556556                      source->mode |= PM_SOURCE_MODE_EXTENDED_FIT;
     
    576576                          fprintf (stderr, "update window : %f %f : %f -> %f\n", source->peak->xf, source->peak->yf, fitRadius, 2*fitRadius);
    577577                          psphotSetWindowTrail (&fitRadius, &windowRadius, readout, source, markVal, fitRadius*2.0);
     578                          source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_RETRY;
    578579                      }
    579580                  }
    580581              }
    581582          }
    582           // XXX really need to do this in a cleaner way:
    583           if (!modelFit) continue;
     583          psAssert (modelFit, "modelFit not set?");
    584584
    585585          // test for fit quality / result
     
    590590        }
    591591
     592        // we are allowed to fit both stars and non-stars here -- if we have fitted
     593        // something which we think is a star, we should use that model to subtract the
     594        // object from the image.
     595        if (source->type == PM_SOURCE_TYPE_STAR) {
     596          // ensure the modelPSF is cached
     597          pmSourceCacheModel (source, maskVal);
     598          pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     599          continue;
     600        }
     601
    592602        // evaluate the relative quality of the models, choose one
     603        // the PSF model might be the best fit : allow it to succeed
    593604        float minChisq = NAN;
    594605        int minModel = -1;
     
    596607            pmModel *model = source->modelFits->data[i];
    597608
     609            // skip the really bad fits
    598610            if (!(model->flags & PM_MODEL_STATUS_FITTED)) continue;
    599 
    600             if (model->flags & (PM_MODEL_STATUS_BADARGS)) continue;
     611            if (model->flags & badModel) continue;
     612
    601613            // if (model->flags & (PM_MODEL_STATUS_NONCONVERGE)) continue;
    602             if (model->flags & (PM_MODEL_STATUS_OFFIMAGE)) continue;
    603614
    604615            if ((minModel < 0) || (model->chisq < minChisq)) {
     
    618629          pmSourceCacheModel (source, maskVal);
    619630
     631# if (PS_TRACE_ON)
    620632          pmModel *model = source->modelFits->data[0];
    621633          int flags = 0xffffffff;
     
    623635            flags = model->flags;
    624636          }
    625 
    626637          fprintf (stderr, "failed to fit extended source model to object %d @ %f, %f (%x)\n", source->id, source->moments->Mx, source->moments->My, flags);
     638#endif
    627639          pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    628640
     641          source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_NONE;
    629642          continue;
    630643        }
     
    643656        if (source->modelEXT->isPCM) {
    644657            // fprintf (stderr, "subtract PCM extended source model for object %d @ %f, %f\n", source->id, source->moments->Mx, source->moments->My);
    645             pmPCMCacheModel (source, maskVal, psfSize);
     658            pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma);
    646659        } else {
    647660            // fprintf (stderr, "subtract non-PCM extended source model for object %d @ %f, %f\n", source->id, source->moments->Mx, source->moments->My);
    648661            pmSourceCacheModel (source, maskVal);
    649662        }
     663        source->modelEXT->flags |= PM_MODEL_BEST_FIT;
    650664
    651665        // subtract the best fit from the object, leave local sky
     
    655669        psTrace ("psphot", 5, "extended source model for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    656670
     671# ifdef TEST_OBJECT
    657672        if (testObject) {
    658673            psTraceSetLevel ("psModules.objects.pmPCM_MinimizeChisq", 0);
    659674            psTraceSetLevel ("psphot.psphotExtendedSourceFits_Threaded", 0);
    660675        }
     676# endif
    661677    }
    662678
Note: See TracChangeset for help on using the changeset viewer.