- Timestamp:
- Mar 5, 2012, 5:19:48 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 4 edited
-
. (modified) (1 prop)
-
psphot (modified) (1 prop)
-
psphot/src (modified) (2 props)
-
psphot/src/psphotExtendedSourceFits.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/psphot
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/psphot/src
- Property svn:ignore
-
old new 22 22 psphotMakePSF 23 23 psphotStack 24 psphotModelTest
-
- Property svn:mergeinfo set to
- Property svn:ignore
-
branches/meh_branches/ppstack_test/psphot/src/psphotExtendedSourceFits.c
r31673 r33415 6 6 bool status = true; 7 7 8 fprintf (stdout, "\n"); 9 psLogMsg ("psphot", PS_LOG_INFO, "--- psphot Extended Source Fits ---"); 10 8 11 // select the appropriate recipe information 9 12 psMetadata *recipe = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE); … … 12 15 // perform full extended source non-linear fits? 13 16 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"); 15 18 return true; 16 19 } … … 45 48 int Nfail = 0; 46 49 50 psphotSersicModelClassInit(); 51 47 52 psTimerStart ("psphot.extended"); 48 53 … … 103 108 104 109 if (item->type != PS_DATA_METADATA) { 110 // XXX we could cull the bad entries or build a validated model folder 105 111 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 folder107 112 } 108 113 … … 213 218 214 219 // wait for the threads to finish and manage results 215 if (!psThreadPoolWait (false )) {220 if (!psThreadPoolWait (false, true)) { 216 221 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 217 222 psFree(AnalysisRegion); … … 247 252 psFree(AnalysisRegion); 248 253 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); 250 259 psLogMsg ("psphot", PS_LOG_INFO, " %d convolved models (%d passed)\n", Nconvolve, NconvolvePass); 251 260 psLogMsg ("psphot", PS_LOG_INFO, " %d plain models (%d passed)\n", Nplain, NplainPass); … … 265 274 int Nplain = 0; 266 275 int NplainPass = 0; 267 bool savePics = false; 268 float radius; 276 float fitRadius, windowRadius; 269 277 psScalar *scalar = NULL; 270 pmMoments psfMoments;271 278 272 279 // arguments: readout, sources, models, region, psfSize, maskVal, markVal … … 275 282 psMetadata *models = job->args->data[2]; 276 283 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); 278 285 psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[5],PS_TYPE_IMAGE_MASK_DATA); 279 286 psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[6],PS_TYPE_IMAGE_MASK_DATA); 280 281 // pthread_t tid = pthread_self(); // Thread identifier282 287 283 288 // Define source fitting parameters for extended source fits … … 299 304 300 305 // 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; 302 307 if (source->type == PM_SOURCE_TYPE_DEFECT) continue; 303 308 if (source->type == PM_SOURCE_TYPE_SATURATED) continue; … … 309 314 if (source->peak->y > region->y1) continue; 310 315 311 // if model is NULL, we don't have a starting guess312 // XXX use the parameters guessed from moments313 // 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 317 316 // replace object in image 318 317 if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) { … … 321 320 Next ++; 322 321 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); // NOTE : 6 allocs 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. 366 329 367 330 // allocate the array to store the model fits … … 380 343 381 344 // 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"); 383 346 assert (status); 384 347 385 348 // limit selection to some SN limit 386 349 // 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) { 388 358 Nfaint ++; 389 359 continue; … … 398 368 assert (status); 399 369 400 // XXX psTraceSetLevel ("psLib.math.psMinimizeLMChi2", 6);401 // XXX psTraceSetLevel ("psphot.psphotModelWithPSF_LMM", 6);402 403 370 // fit the model as convolved or not 404 371 pmModel *modelFit = NULL; 405 372 if (convolved) { 406 modelFit = psphotFitPCM (readout, source, fitOptions, modelType, maskVal, markVal, psfSize); 373 // NOTE : 4 more allocs to here 374 modelFit = psphotFitPCM (readout, source, fitOptions, modelType, maskVal, markVal, psfSize); // NOTE : 2313 allocs in here 407 375 if (!modelFit) { 408 376 psTrace ("psphot", 5, "failed to fit psf-conv model for object at %f, %f", source->moments->Mx, source->moments->My); … … 410 378 continue; 411 379 } 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); 380 psTrace ("psphot", 4, "fit psf-conv model for %f, %f : %s chisq = %f (npix: %d, niter: %d)\n", 381 source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter); 413 382 Nconvolve ++; 414 383 if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) { … … 425 394 continue; 426 395 } 427 p mSourceCacheModel (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);396 psTrace ("psphot", 4, "fit plain model for %f, %f : %s chisq = %f (npix: %d, niter: %d)\n", 397 source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter); 429 398 Nplain ++; 430 399 if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) { … … 434 403 } 435 404 436 // save each of the model flux images and store the best 437 psArrayAdd (modelFluxes, 4, source->modelFlux); 405 // XXX deprecate? 406 // XXX pmSourceExtFitPars *extFitPars = pmSourceExtFitParsAlloc(); 407 // XXX extFitPars->Mxx = source->moments->Mxx; 408 // XXX extFitPars->Mxy = source->moments->Mxy; 409 // XXX extFitPars->Myy = source->moments->Myy; 410 // XXX extFitPars->Mrf = source->moments->Mrf; 411 // XXX extFitPars->Mrh = source->moments->Mrh; 412 // XXX extFitPars->peakMag = (source->peak->rawFlux > 0) ? -2.5*log10(source->peak->rawFlux) : NAN; 413 414 // save kron mag, but assign apMag & psfMag on output (not yet calculated) 415 // XXX extFitPars->krMag = -2.5*log10(source->moments->KronFlux); 416 417 // psArrayAdd (source->extFitPars, 4, extFitPars); 418 // psFree (extFitPars); 438 419 439 420 // test for fit quality / result 440 modelFit->fitRadius = radius;421 modelFit->fitRadius = fitRadius; 441 422 psArrayAdd (source->modelFits, 4, modelFit); 442 423 … … 463 444 } 464 445 446 // clear the circular mask 447 psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); 448 465 449 if (minModel == -1) { 466 // re-subtract the object, leave local sky450 // no valid extended fit; re-subtract the object, leave local sky 467 451 psTrace ("psphot", 5, "failed to fit extended source model to object at %f, %f", source->moments->Mx, source->moments->My); 468 452 469 // replace original model, subtract it 470 psFree (source->modelFlux); 471 source->modelFlux = modelFluxStart; 472 473 *source->moments = psfMoments; 453 // ensure the modelEXT is cached 454 pmSourceCacheModel (source, maskVal); 455 474 456 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal); 475 457 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 }488 458 continue; 489 459 } … … 493 463 source->modelEXT = psMemIncrRefCounter (source->modelFits->data[minModel]); 494 464 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; 465 // adjust the window so the subtraction covers the faint wings 466 psphotSetRadiusMoments(&fitRadius, &windowRadius, readout, source, markVal); 467 468 // cache the model flux 469 if (source->modelEXT->isPCM) { 470 pmPCMCacheModel (source, maskVal, psfSize); 471 } else { 472 pmSourceCacheModel (source, maskVal); 473 } 501 474 502 475 // subtract the best fit from the object, leave local sky 503 476 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal); 504 477 505 // the initial model flux is no longer needed506 psFree (modelFluxStart);507 psFree (modelFluxes);508 509 478 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); 510 479 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 }522 480 } 523 481 psFree (fitOptions); 524 525 // fprintf (stderr, "xfit : tried %ld objects\n", sources->n);526 482 527 483 // 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.
