- Timestamp:
- May 29, 2012, 4:44:00 PM (14 years ago)
- Location:
- branches/eam_branches/ipp-20120405/psphot
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20120405/psphot
- Property svn:mergeinfo changed
/branches/eam_branches/ipp-20111122/psphot merged: 33070-33071,33086,33088,33094-33095,33612,33640
- Property svn:mergeinfo changed
-
branches/eam_branches/ipp-20120405/psphot/src
- Property svn:mergeinfo changed
/branches/eam_branches/ipp-20111122/psphot/src merged: 33070-33071,33086,33094,33612,33640
- Property svn:mergeinfo changed
-
branches/eam_branches/ipp-20120405/psphot/src/psphotFitSourcesLinear.c
r32996 r33946 12 12 static bool SetBorderMatrixElements (psSparseBorder *border, pmReadout *readout, psArray *sources, bool constant_weights, int SKY_FIT_ORDER, psImageMaskType markVal); 13 13 14 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources); 15 pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe); 16 bool psphotFreeModelVariance (pmReadout *readout, psArray *sources); 17 14 18 // for now, let's store the detections on the readout->analysis for each readout 15 19 bool psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final) … … 24 28 assert (recipe); 25 29 30 pmSourceFitVarMode fitVarMode = psphotGetFitVarMode (recipe); 31 if (!fitVarMode) { 32 psError (PSPHOT_ERR_CONFIG, true, "failed to get LINEAR_FIT_VARIANCE_MODE"); 33 return false; 34 } 35 // MODEL_VAR requires 2 passes -- in the first, we get the rough fluxes; in the second, we 36 // use the flux to define the model variance before fitting the objects. Other modes only 37 // do a single pass. 38 pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode; 39 26 40 int num = psphotFileruleCount(config, filerule); 27 41 … … 50 64 psAssert (psf, "missing psf?"); 51 65 52 if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final )) {66 if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarModePass1)) { 53 67 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 54 68 return false; 55 69 } 70 71 // the MODEL_VAR weighting scheme requires knowledge of the model fluxes to generate the variance 72 // after we have determined the initial set of fits, then we can generate the variance image and 73 // re-run the fit against that variance. 74 if (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) { 75 // generate the model variance image & source pointers 76 if (!psphotGenerateModelVariance (config, view, file, i, recipe, readout, sources)) { 77 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 78 return false; 79 } 80 81 // replace all sources (use TMPF_SUBTRACTED as test) 82 psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe, false); 83 84 // rerun fit with correct fitVarMode 85 if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarMode)) { 86 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 87 return false; 88 } 89 90 // free the model variance image & source pointers 91 if (!psphotFreeModelVariance (readout, sources)) { 92 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 93 return false; 94 } 95 } 56 96 57 97 psphotVisualShowResidualImage (readout, (num > 0)); … … 62 102 } 63 103 64 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final) { 104 // look up the fit variance mode from the recipe; older recipes do not have the value 105 // 'LINEAR_FIT_VARIANCE_MODE'; in those cases, look for 'CONSTANT_PHOTOMETRIC_WEIGHTS' as a boolean and 106 // set the value to either CONST or IMAGE_VAR 107 pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe) { 108 109 bool status = false; 110 111 char *fitVarModeString = psMetadataLookupStr(&status, recipe, "LINEAR_FIT_VARIANCE_MODE"); 112 if (!status) { 113 bool CONSTANT_PHOTOMETRIC_WEIGHTS = psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS"); 114 if (!status) { 115 psAbort("You must provide a value for LINEAR_FIT_VARIANCE_MODE or CONSTANT_PHOTOMETRIC_WEIGHTS"); 116 } 117 pmSourceFitVarMode fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR; 118 return fitVarMode; 119 } 120 if (!strcasecmp(fitVarModeString, "CONSTANT") || !strcasecmp(fitVarModeString, "CONST")) { 121 return PM_SOURCE_PHOTFIT_CONST; 122 } 123 if (!strcasecmp(fitVarModeString, "IMAGE") || !strcasecmp(fitVarModeString, "IMAGE_VAR")) { 124 return PM_SOURCE_PHOTFIT_IMAGE_VAR; 125 } 126 if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) { 127 return PM_SOURCE_PHOTFIT_MODEL_VAR; 128 } 129 psError (PSPHOT_ERR_CONFIG, false, "Invalid value for LINEAR_FIT_VARIANCE_MODE (%s)", fitVarModeString); 130 return PM_SOURCE_PHOTFIT_NONE; 131 } 132 133 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode) { 65 134 66 135 bool status; … … 99 168 if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined"); 100 169 101 bool CONSTANT_PHOTOMETRIC_WEIGHTS =102 psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS");103 if (!status) {104 psAbort("You must provide a value for the BOOL recipe CONSTANT_PHOTOMETRIC_WEIGHTS");105 }106 170 int SKY_FIT_ORDER = psMetadataLookupS32(&status, recipe, "SKY_FIT_ORDER"); 107 171 if (!status) { … … 231 295 psSparseBorder *border = psSparseBorderAlloc (sparse, nBorder); 232 296 297 // if fitVarMode is MODEL_VAR, then we need to generate the model image variance 298 // XXX we have two possibilities here: 299 300 // 1) do 2 passes, where in the first case we use the CONST weighting, and in the second 301 // use the fitted model values to define the model 302 303 // 2) do a single pass, and use the model guess to define the model variance (but do I trust the Model Guess?) 304 233 305 // fill out the sparse matrix elements and border elements (B) 234 306 // SRCi is the current source of interest … … 238 310 239 311 // diagonal elements of the sparse matrix (auto-cross-product) 240 f = pmSourceModelDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);312 f = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal); 241 313 psSparseMatrixElement (sparse, i, i, f); 242 314 243 // the formal error depends on the weighting scheme244 if ( CONSTANT_PHOTOMETRIC_WEIGHTS) {245 float var = pmSourceModelDotModel (SRCi, SRCi, false, covarFactor, maskVal);315 // if we have used CONSTANT errors, then we need to calculate the value of the parameter error 316 if (fitVarMode != PM_SOURCE_PHOTFIT_IMAGE_VAR) { 317 float var = pmSourceModelDotModel (SRCi, SRCi, PM_SOURCE_PHOTFIT_IMAGE_VAR, covarFactor, maskVal); 246 318 errors->data.F32[i] = 1.0 / sqrt(var); 247 319 } else { … … 251 323 252 324 // find the image x model value 253 f = pmSourceDataDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);325 f = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal); 254 326 psSparseVectorElement (sparse, i, f); 255 327 … … 257 329 switch (SKY_FIT_ORDER) { 258 330 case 1: 259 f = pmSourceModelWeight (SRCi, 1, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);331 f = pmSourceModelWeight (SRCi, 1, fitVarMode, covarFactor, maskVal); 260 332 psSparseBorderElementB (border, i, 1, f); 261 f = pmSourceModelWeight (SRCi, 2, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);333 f = pmSourceModelWeight (SRCi, 2, fitVarMode, covarFactor, maskVal); 262 334 psSparseBorderElementB (border, i, 2, f); 263 335 264 336 case 0: 265 f = pmSourceModelWeight (SRCi, 0, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);337 f = pmSourceModelWeight (SRCi, 0, fitVarMode, covarFactor, maskVal); 266 338 psSparseBorderElementB (border, i, 0, f); 267 339 break; … … 283 355 284 356 // got an overlap; calculate cross-product and add to output array 285 f = pmSourceModelDotModel (SRCi, SRCj, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);357 f = pmSourceModelDotModel (SRCi, SRCj, fitVarMode, covarFactor, maskVal); 286 358 psSparseMatrixElement (sparse, j, i, f); 287 359 } … … 321 393 322 394 // set the sky, sky_x, sky_y components of border matrix 323 SetBorderMatrixElements (border, readout, fitSources, CONSTANT_PHOTOMETRIC_WEIGHTS, SKY_FIT_ORDER, markVal);395 SetBorderMatrixElements (border, readout, fitSources, (fitVarMode == PM_SOURCE_PHOTFIT_CONST), SKY_FIT_ORDER, markVal); 324 396 325 397 psSparseConstraint constraint; … … 479 551 return true; 480 552 } 553 554 bool psphotModelBackgroundReadout(psImage *model, // Model image 555 psImage *modelStdev, // Model stdev image 556 psMetadata *analysis, // Analysis metadata for outputs 557 pmReadout *readout, // Readout for which to generate a background model 558 psImageBinning *binning, // Binning parameters 559 const pmConfig *config,// Configuration 560 bool useVarianceImage 561 ); 562 563 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources) { 564 565 bool status = false; 566 psRegion fullRegion = psRegionSet (0, 0, 0, 0); 567 568 // bit-masks to test for good/bad pixels 569 psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); 570 assert (maskVal); 571 572 // bit-mask to mark pixels not used in analysis 573 psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); 574 assert (markVal); 575 576 // maskVal is used to test for rejected pixels, and must include markVal 577 maskVal |= markVal; 578 579 // create a model variance image 580 psImage *modelVar = psImageCopy (NULL, readout->variance, PS_TYPE_F32); 581 582 // find the binning information 583 psImageBinning *backBinning = psphotBackgroundBinning (modelVar, config); 584 assert (backBinning); 585 586 psImage *varModel = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model 587 psImage *varModelStdev = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model 588 589 if (!psphotModelBackgroundReadout(varModel, varModelStdev, NULL, readout, backBinning, config, true)) { 590 psError(PS_ERR_UNKNOWN, false, "Unable to generate background model"); 591 psFree (varModel); 592 psFree (varModelStdev); 593 return false; 594 } 595 596 // linear interpolation to full-scale 597 if (!psImageUnbin (modelVar, varModel, backBinning)) { 598 psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning"); 599 psFree (varModel); 600 psFree (varModelStdev); 601 return false; 602 } 603 604 psFree (varModel); 605 psFree (varModelStdev); 606 607 // XXX for a test: 608 psphotSaveImage (NULL, modelVar, "model.bck.fits"); 609 610 // insert all of the source models 611 for (int i = 0; i < sources->n; i++) { 612 613 // source of interest 614 pmSource *source = sources->data[i]; 615 616 // skip sources which were not fitted already 617 if (!(source->mode & PM_SOURCE_MODE_LINEAR_FIT)) continue; 618 619 // pixel region appropriate for the source 620 psRegion region = psRegionForImage (source->pixels, fullRegion); 621 622 // define the source->modelVar pixels (view on modelVar image) 623 psAssert (!source->modelVar, "programming error : modelVar should be NULL here"); 624 psAssert (source->modelFlux, "programming error : modelFlux should not be NULL here"); 625 psAssert (source->modelFlux->data.F32, "programming error : modelFlux should not be NULL here"); 626 source->modelVar = psImageSubset(modelVar, region); 627 628 // add the source model to the model variance image 629 pmSourceAdd (source, PM_MODEL_OP_MODELVAR, maskVal); 630 } 631 632 // XXX for a test: 633 psphotSaveImage (NULL, modelVar, "model.var.fits"); 634 psphotSaveImage (NULL, readout->variance, "image.var.fits"); 635 636 // we save the model variance for future reference 637 psMetadataAddImage(readout->analysis, PS_LIST_TAIL, "PSPHOT.MODEL.VAR", PS_META_REPLACE, "model variance", modelVar); 638 psFree (modelVar); 639 640 return true; 641 } 642 643 bool psphotFreeModelVariance (pmReadout *readout, psArray *sources) { 644 645 bool status = false; 646 647 // find the binning information 648 psImage *modelVar = psMetadataLookupPtr(&status, readout->analysis, "PSPHOT.MODEL.VAR"); 649 assert (modelVar); 650 651 psMetadataRemoveKey (readout->analysis, "PSPHOT.MODEL.VAR"); 652 653 // clear modelVar pointers for all of the source models 654 for (int i = 0; i < sources->n; i++) { 655 656 // source of interest 657 pmSource *source = sources->data[i]; 658 psFree (source->modelVar); 659 } 660 661 return true; 662 }
Note:
See TracChangeset
for help on using the changeset viewer.
