- Timestamp:
- Jul 17, 2014, 12:30:45 PM (12 years ago)
- Location:
- branches/eam_branches/ipp-ops-20130712/psphot
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-ops-20130712/psphot
- Property svn:mergeinfo deleted
-
branches/eam_branches/ipp-ops-20130712/psphot/src
- Property svn:ignore
-
old new 24 24 psphotModelTest 25 25 psphotMinimal 26 psphotFullForce 27 psmakecff 28 psphotFullForceSummary
-
- Property svn:mergeinfo deleted
- Property svn:ignore
-
branches/eam_branches/ipp-ops-20130712/psphot/src/psphotExtendedSourceFits.c
r35769 r37066 111 111 } 112 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; 116 } 117 113 118 bool chisqConvergence = psMetadataLookupBool (&status, recipe, "LMM_FIT_CHISQ_CONVERGENCE"); // Fit tolerance 114 119 if (!status) { … … 123 128 } 124 129 130 // perform full extended source non-linear fits? 131 bool isInteractive = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS_INTERACTIVE"); 132 if (!status) isInteractive = false; 133 125 134 // Define source fitting parameters for extended source fits 126 135 pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc(); 127 fitOptions->mode = PM_SOURCE_FIT_EXT ;136 fitOptions->mode = PM_SOURCE_FIT_EXT_AND_SKY; 128 137 fitOptions->saveCovariance = true; // XXX make this a user option? 129 138 fitOptions->covarFactor = psImageCovarianceFactorForAperture(readout->covariance, 10.0); // Covariance matrix … … 131 140 fitOptions->minTol = fitMinTol; 132 141 fitOptions->maxTol = fitMaxTol; 142 fitOptions->nsigma = fitNsigmaConv; 133 143 134 144 fitOptions->gainFactorMode = gainFactorMode; 135 145 fitOptions->chisqConvergence = chisqConvergence; 146 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; 136 151 137 152 // maskVal is used to test for rejected pixels, and must include markVal … … 278 293 psArrayAdd(job->args, 1, cells->data[j]); // sources 279 294 psArrayAdd(job->args, 1, models); 295 280 296 // Allocate a metadata iterator here because psMetadataIteratorAlloc/Free are not thread safe 281 297 psMetadataIterator *iter = psMetadataIteratorAlloc (models, PS_LIST_HEAD, NULL); … … 296 312 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfail 297 313 298 // set this to 0 to run without threading 299 # if (1) 300 if (!psThreadJobAddPending(job)) { 301 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 302 psFree(AnalysisRegion); 303 psFree (fitOptions); 304 psFree (models); 305 psphotSersicModelClassCleanup(); 306 return false; 307 } 308 # else 309 if (!psphotExtendedSourceFits_Threaded(job)) { 310 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 311 psFree(AnalysisRegion); 312 psFree (fitOptions); 313 psFree (models); 314 psphotSersicModelClassCleanup(); 315 return false; 314 // XXX TEST 315 if (!isInteractive) { 316 if (!psThreadJobAddPending(job)) { 317 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 318 psFree(AnalysisRegion); 319 psFree (fitOptions); 320 psFree (models); 321 psphotSersicModelClassCleanup(); 322 return false; 323 } 324 } else { 325 // run without threading 326 if (!psphotExtendedSourceFits_Threaded(job)) { 327 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 328 psFree(AnalysisRegion); 329 psFree (fitOptions); 330 psFree (models); 331 psphotSersicModelClassCleanup(); 332 return false; 333 } 334 psScalar *scalar = NULL; 335 scalar = job->args->data[9]; 336 Next += scalar->data.S32; 337 scalar = job->args->data[10]; 338 Nconvolve += scalar->data.S32; 339 scalar = job->args->data[11]; 340 NconvolvePass += scalar->data.S32; 341 scalar = job->args->data[12]; 342 Nplain += scalar->data.S32; 343 scalar = job->args->data[13]; 344 NplainPass += scalar->data.S32; 345 scalar = job->args->data[14]; 346 Nfaint += scalar->data.S32; 347 scalar = job->args->data[15]; 348 Nfail += scalar->data.S32; 349 psFree(job->args->data[3]); // iterator allocated above 350 psFree(job); 316 351 } 317 psScalar *scalar = NULL;318 scalar = job->args->data[9];319 Next += scalar->data.S32;320 scalar = job->args->data[10];321 Nconvolve += scalar->data.S32;322 scalar = job->args->data[11];323 NconvolvePass += scalar->data.S32;324 scalar = job->args->data[12];325 Nplain += scalar->data.S32;326 scalar = job->args->data[13];327 NplainPass += scalar->data.S32;328 scalar = job->args->data[14];329 Nfaint += scalar->data.S32;330 scalar = job->args->data[15];331 Nfail += scalar->data.S32;332 psFree(job->args->data[3]); // iterator allocated above333 psFree(job);334 # endif335 352 } 336 353 … … 418 435 // psTraceSetLevel ("psLib.math.psMinimizeLMChi2_Alt", 5); 419 436 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 420 445 // choose the sources of interest 421 446 for (int i = 0; i < sources->n; i++) { … … 423 448 pmSource *source = sources->data[i]; 424 449 425 // skip PSF-like and non-astronomical objects 426 if (!(source->mode & PM_SOURCE_MODE_EXT_LIMIT)) continue; 427 if (source->type == PM_SOURCE_TYPE_DEFECT) continue; 428 if (source->type == PM_SOURCE_TYPE_SATURATED) continue; 429 430 // skip saturated stars modeled with a radial profile 431 if (source->mode2 & PM_SOURCE_MODE2_SATSTAR_PROFILE) continue; 432 433 // 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?) 434 455 if (source->peak->x < region->x0) continue; 435 456 if (source->peak->y < region->y0) continue; 436 457 if (source->peak->x > region->x1) continue; 437 458 if (source->peak->y > region->y1) continue; 438 439 440 // XXX for a test, just do the obvious trail441 // XXX if (source->peak->xf < 1100) continue;442 // XXX if (source->peak->xf > 1400) continue;443 // XXX if (source->peak->yf > 245) continue;444 459 445 460 // replace object in image … … 452 467 psphotSetRadiusMomentsExact(&fitRadius, &windowRadius, readout, source, markVal); // NOTE : 6 allocs 453 468 454 // XXX WATCH OUT HERE!!455 // fitRadius = 30;456 457 469 // UPDATE : we have changed the moments calculation. There is now an iteration within 458 470 // psphotKronMasked to determine moments appropriate for a larger object. The values … … 464 476 source->modelFits = psArrayAllocEmpty (models->list->n); 465 477 } 478 479 # ifdef TEST_OBJECT 480 bool testObject = false; 481 testObject |= ((fabs(source->peak->xf - 179) < 5) && (fabs(source->peak->yf - 1138) < 5)); 482 if (testObject) { 483 fprintf (stderr, "test object @ %f, %f\n", source->peak->xf, source->peak->yf); 484 psTraceSetLevel ("psModules.objects.pmPCM_MinimizeChisq", 5); 485 psTraceSetLevel ("psphot.psphotExtendedSourceFits_Threaded", 5); 486 } 487 # endif 466 488 467 489 // loop here over the models chosen for each source (exclude by S/N) … … 479 501 assert (status); 480 502 481 // limit selection to some SN limit 482 bool skipSource = false; 483 if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) { 484 skipSource = (source->moments->KronFlux < FIT_SN_LIM * source->moments->KronFluxErr); 485 } else { 486 skipSource = (sqrt(source->peak->detValue) < FIT_SN_LIM); 487 } 488 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) { 489 506 Nfaint ++; 490 507 continue; 508 } 491 509 } 510 511 source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_RUN; 492 512 493 513 // check on the model type … … 507 527 psTrace ("psphot", 5, "failed to fit psf-conv model for object at %f, %f", source->moments->Mx, source->moments->My); 508 528 Nfail ++; 529 source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_FAIL; 509 530 continue; 510 531 } … … 512 533 source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter); 513 534 Nconvolve ++; 514 if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {535 if (!(modelFit->flags & badModel)) { 515 536 NconvolvePass ++; 516 537 source->mode |= PM_SOURCE_MODE_EXTENDED_FIT; … … 526 547 Nfail ++; 527 548 doneFits = true; 549 source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_FAIL; 528 550 continue; 529 551 } 530 552 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); 531 553 Nplain ++; 532 if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {554 if (!(modelFit->flags & badModel)) { 533 555 NplainPass ++; 534 556 source->mode |= PM_SOURCE_MODE_EXTENDED_FIT; … … 544 566 float Ymax = PAR[PM_PAR_YPOS] + 0.5*PAR[PM_PAR_LENGTH]*cos(PAR[PM_PAR_THETA]); 545 567 546 if (false && (source->peak->xf > 1100) && 547 (source->peak->xf < 1400) && 548 (source->peak->yf < 245)) { 568 if ((fabs(source->peak->xf - 2572) < 20) && (fabs(source->peak->yf - 5874) < 20)) { 549 569 fprintf (stderr, "src vs fit : %d %d - %d %d | %f %f - %f %f\n", 550 570 source->pixels->col0, source->pixels->row0, … … 556 576 fprintf (stderr, "update window : %f %f : %f -> %f\n", source->peak->xf, source->peak->yf, fitRadius, 2*fitRadius); 557 577 psphotSetWindowTrail (&fitRadius, &windowRadius, readout, source, markVal, fitRadius*2.0); 578 source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_RETRY; 558 579 } 559 580 } 560 581 } 561 582 } 562 // XXX really need to do this in a cleaner way: 563 if (!modelFit) continue; 583 psAssert (modelFit, "modelFit not set?"); 564 584 565 585 // test for fit quality / result … … 570 590 } 571 591 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 572 602 // evaluate the relative quality of the models, choose one 603 // the PSF model might be the best fit : allow it to succeed 573 604 float minChisq = NAN; 574 605 int minModel = -1; … … 576 607 pmModel *model = source->modelFits->data[i]; 577 608 609 // skip the really bad fits 578 610 if (!(model->flags & PM_MODEL_STATUS_FITTED)) continue; 579 580 if (model->flags & (PM_MODEL_STATUS_BADARGS)) continue; 581 if (model->flags & (PM_MODEL_STATUS_NONCONVERGE)) continue; 582 if (model->flags & (PM_MODEL_STATUS_OFFIMAGE)) continue; 611 if (model->flags & badModel) continue; 612 613 // if (model->flags & (PM_MODEL_STATUS_NONCONVERGE)) continue; 583 614 584 615 if ((minModel < 0) || (model->chisq < minChisq)) { … … 598 629 pmSourceCacheModel (source, maskVal); 599 630 631 # if (PS_TRACE_ON) 632 pmModel *model = source->modelFits->data[0]; 633 int flags = 0xffffffff; 634 if (model) { 635 flags = model->flags; 636 } 637 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 600 639 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal); 601 640 641 source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_NONE; 602 642 continue; 603 643 } … … 615 655 // cache the model flux 616 656 if (source->modelEXT->isPCM) { 617 pmPCMCacheModel (source, maskVal, psfSize); 657 // fprintf (stderr, "subtract PCM extended source model for object %d @ %f, %f\n", source->id, source->moments->Mx, source->moments->My); 658 pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma); 618 659 } else { 660 // fprintf (stderr, "subtract non-PCM extended source model for object %d @ %f, %f\n", source->id, source->moments->Mx, source->moments->My); 619 661 pmSourceCacheModel (source, maskVal); 620 662 } 663 source->modelEXT->flags |= PM_MODEL_BEST_FIT; 621 664 622 665 // subtract the best fit from the object, leave local sky … … 625 668 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); 626 669 psTrace ("psphot", 5, "extended source model for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My); 670 671 # ifdef TEST_OBJECT 672 if (testObject) { 673 psTraceSetLevel ("psModules.objects.pmPCM_MinimizeChisq", 0); 674 psTraceSetLevel ("psphot.psphotExtendedSourceFits_Threaded", 0); 675 } 676 # endif 627 677 } 628 678
Note:
See TracChangeset
for help on using the changeset viewer.
