- Timestamp:
- May 29, 2012, 5:16:09 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
/trunk/psphot (added) merged: 33731,33761,33837,33839-33842,33877,33879,33881,33883,33889-33890,33894,33910,33913-33915,33917,33935
- Property svn:mergeinfo changed
-
branches/eam_branches/ipp-20120405/psphot/src
- Property svn:mergeinfo changed
/trunk/psphot/src (added) merged: 33731,33761,33837,33839-33842,33877,33879,33881,33883,33889-33890,33894,33910,33913-33915,33917,33935
- Property svn:mergeinfo changed
-
branches/eam_branches/ipp-20120405/psphot/src/psphotKronIterate.c
r33946 r33953 1 1 # include "psphotInternal.h" 2 # ifndef ROUND 3 # define ROUND(X) ((int) ((X) + 0.5*SIGN(X))) 4 # endif 5 6 bool psphotKronWindowSetSource(pmSource *source, psImage *kronWindow, bool useKronRadius, bool insert); 7 bool psphotKronWindowMag (pmSource *source, psImage *kronWindow, float radius, float minKronRadius, psImageMaskType maskVal); 2 3 bool psphotKronWindowSetSource(pmSource *source, psImage *kronWindow, bool useKronRadius, bool insert, bool oldWindow); 4 bool psphotKronWindowMag (pmSource *source, psImage *kronWindow, float radius, float minKronRadius, psImageMaskType maskVal, bool applyWeight, psImage *smoothedPixels); 5 8 6 9 7 bool psphotKronIterate (pmConfig *config, const pmFPAview *view, const char *filerule) … … 11 9 bool status = true; 12 10 13 // return true;14 15 11 fprintf (stdout, "\n"); 16 12 psLogMsg ("psphot", PS_LOG_INFO, "--- psphot Kron Iterate ---"); … … 46 42 // psAssert (psf, "missing psf?"); 47 43 48 if (!psphotKronIterateReadout (config, recipe, view, readout, sources, psf)) {44 if (!psphotKronIterateReadout (config, recipe, view, filerule, readout, sources, psf, i)) { 49 45 psError (PSPHOT_ERR_CONFIG, false, "failed to measure magnitudes for %s entry %d", filerule, i); 50 46 return false; … … 58 54 bool psphotVisualRangeImage (int kapaFD, psImage *inImage, const char *name, int channel, float min, float max); 59 55 60 bool psphotKronIterateReadout(pmConfig *config, psMetadata *recipe, const pmFPAview *view, pmReadout *readout, psArray *sources, pmPSF *psf) {56 bool psphotKronIterateReadout(pmConfig *config, psMetadata *recipe, const pmFPAview *view, const char * filerule, pmReadout *readout, psArray *sources, pmPSF *psf, int index) { 61 57 62 58 bool status = false; … … 69 65 psTimerStart ("psphot.kron"); 70 66 67 71 68 // determine the number of allowed threads 72 69 int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads … … 87 84 int KRON_ITERATIONS = psMetadataLookupS32 (&status, recipe, "KRON_ITERATIONS"); 88 85 if (!status) { 89 KRON_ITERATIONS = 2; 86 KRON_ITERATIONS = 1; 87 } 88 89 bool KRON_APPLY_WEIGHT = psMetadataLookupBool (&status, recipe, "KRON_APPLY_WEIGHT"); 90 if (!status) { 91 KRON_APPLY_WEIGHT = true; 92 } 93 94 bool KRON_APPLY_WINDOW = psMetadataLookupBool (&status, recipe, "KRON_APPLY_WINDOW"); 95 if (!status) { 96 KRON_APPLY_WINDOW = false; 97 } 98 bool KRON_SMOOTH = psMetadataLookupBool (&status, recipe, "KRON_SMOOTH"); 99 if (!status) { 100 KRON_SMOOTH = false; 101 } 102 float KRON_SMOOTH_SIGMA = psMetadataLookupF32 (&status, recipe, "KRON_SMOOTH_SIGMA"); 103 if (!status) { 104 KRON_SMOOTH_SIGMA = 1.7; 105 } 106 float KRON_SMOOTH_NSIGMA = psMetadataLookupS32 (&status, recipe, "KRON_SMOOTH_NSIGMA"); 107 if (!status) { 108 KRON_SMOOTH_NSIGMA = 2; 90 109 } 91 110 … … 119 138 // set a window function for each source based on the moments 120 139 // (this skips really bad sources (no peak, no moments, DEFECT) 121 psphotKronWindowSetSource (source, kronWindow, false, true); 140 psphotKronWindowSetSource (source, kronWindow, false, true, KRON_APPLY_WINDOW); 141 } 142 143 // We measure the Kron Radius on a smoothed copy of the readout image 144 psImage *smoothedImage = NULL; 145 if (KRON_SMOOTH) { 146 // Build the smoothed source image 147 // Replace the subtracted sources 148 psphotReplaceAllSourcesReadout(config, view, filerule, index, recipe, false); 149 // Copy the image and smooth 150 psTimerStart ("psphot.kron.smooth"); 151 smoothedImage = psImageCopy(NULL, readout->image, PS_TYPE_F32); 152 psImageSmooth(smoothedImage, KRON_SMOOTH_SIGMA, KRON_SMOOTH_NSIGMA); 153 psLogMsg ("psphot.kron", PS_LOG_INFO, "smoothed image %f sec\n", psTimerMark ("psphot.kron.smooth")); 154 155 // remove the sources 156 psphotRemoveAllSourcesReadout( config, view, filerule, index, recipe, false ); 157 // Now subtract smooth versions of the sources from the smoothed image 158 psTimerStart ("psphot.kron.smooth.sources"); 159 for (int i=0; i< sources->n; i++) { 160 pmSource *source = sources->data[i]; 161 // If source has been subtracted from the readout image subtract a "smoothed" version from the smoothedImage 162 if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) { 163 // cache copy of smoothedPixels in the source 164 // tmpPtr is for use by a single "module" and must be null otherwise 165 psAssert(source->tmpPtr == NULL, "source->tmpPtr is not null!"); 166 167 psImage *smoothedPixels = psImageSubset(smoothedImage, source->region); 168 source->tmpPtr = (psPtr) smoothedPixels; 169 pmSourceSmoothOp(source, PM_MODEL_OP_FUNC, smoothedPixels, KRON_SMOOTH_SIGMA, false, maskVal, 0, 0); 170 } 171 } 172 psLogMsg ("psphot.kron", PS_LOG_INFO, "removed %ld smoothed sources %f sec\n", sources->n, psTimerMark ("psphot.kron.smooth.sources")); 173 122 174 } 123 175 … … 146 198 PS_ARRAY_ADD_SCALAR(job->args, MIN_KRON_RADIUS, PS_TYPE_F32); 147 199 PS_ARRAY_ADD_SCALAR(job->args, KRON_ITERATIONS, PS_TYPE_S32); 200 PS_ARRAY_ADD_SCALAR(job->args, (psS32) KRON_APPLY_WEIGHT, PS_TYPE_S32); 201 PS_ARRAY_ADD_SCALAR(job->args, (psS32) KRON_APPLY_WINDOW, PS_TYPE_S32); 202 PS_ARRAY_ADD_SCALAR(job->args, KRON_SMOOTH_SIGMA, PS_TYPE_F32); 203 psArrayAdd(job->args, 1, smoothedImage); 148 204 149 205 // set this to 0 to run without threading 150 # if ( 0)206 # if (1) 151 207 if (!psThreadJobAddPending(job)) { 152 208 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); … … 156 212 if (!psphotKronIterate_Threaded(job)) { 157 213 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 214 // psFree(AnalysisRegion); 158 215 return false; 159 216 } … … 179 236 psFree (cellGroups); 180 237 psFree (kronWindow); 238 psFree (smoothedImage); 181 239 182 240 psLogMsg ("psphot.kron", PS_LOG_WARN, "measure masked kron magnitudes : %f sec for %ld objects\n", psTimerMark ("psphot.kron"), sources->n); … … 194 252 float MIN_KRON_RADIUS = PS_SCALAR_VALUE(job->args->data[6],F32); 195 253 int KRON_ITERATIONS = PS_SCALAR_VALUE(job->args->data[7],S32); 196 197 // XXX TEST : set iteration to 1 254 bool KRON_APPLY_WEIGHT = PS_SCALAR_VALUE(job->args->data[8],S32); 255 bool KRON_APPLY_WINDOW = PS_SCALAR_VALUE(job->args->data[9],S32); 256 float KRON_SMOOTH_SIGMA = PS_SCALAR_VALUE(job->args->data[10],F32); 257 psImage *smoothedImage = job->args->data[11]; 258 259 // psImage *smoothedPixels = NULL; 198 260 for (int j = 0; j < KRON_ITERATIONS; j++) { 199 261 for (int i = 0; i < sources->n; i++) { … … 202 264 if (!source->peak) continue; // XXX how can we have a peak-less source? 203 265 204 // allocate space formoments266 // check status of this source's moments 205 267 if (!source->moments) continue; 268 if (!source->tmpFlags & PM_SOURCE_TMPF_MOMENTS_MEASURED) continue; 269 if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue; 206 270 207 271 // replace object in image 208 272 bool reSubtract = false; 273 psImage *smoothedPixels = NULL; 209 274 if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) { 210 pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal); 275 pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal); 276 smoothedPixels = (psImage *) source->tmpPtr; 277 if (smoothedPixels) { 278 // psFree(source->tmpPtr); 279 // smoothedPixels = psImageSubset(smoothedImage, source->region); 280 pmSourceSmoothOp(source, PM_MODEL_OP_FUNC, smoothedPixels, KRON_SMOOTH_SIGMA, true, maskVal, 0, 0); 281 // source->tmpPtr = smoothedPixels; 282 } 211 283 reSubtract = true; 212 284 } … … 223 295 // XXX float windowRadius = PS_MIN(PS_MAX(RADIUS, 4.0*source->moments->Mrf), maxWindow); 224 296 225 // Sextractor apparently takes something like the skyRadius window, measures the moments inside that window,226 // then uses an elliptical outline based on the 2nd moments, but 6x the size227 228 297 // XXX TEST : use a window based on the radial profile numbers: max is skyRadius, min is RADIUS 229 298 // if we lack the skyRadius (eg MATCHED sources), go to the default value 230 float maxWindow, windowRadius; 231 232 if (j == 0) { 233 maxWindow = isfinite(source->skyRadius) ? source->skyRadius : RADIUS; 234 windowRadius = PS_MAX(RADIUS, maxWindow); 235 } else { 236 maxWindow = isfinite(source->moments->Mrf) ? 6.0*source->moments->Mrf : RADIUS; 237 windowRadius = PS_MAX(RADIUS, maxWindow); 238 } 239 240 #ifdef notdef 241 fprintf(stderr, "Redefining pixels for source: %d %4d %4d new radius: %f\n", 242 i, source->peak->x, source->peak->y, windowRadius+2); 243 #endif 299 float maxWindow; 300 if (j == 0) { 301 maxWindow = isfinite(source->skyRadius) ? source->skyRadius : RADIUS; 302 } else { 303 maxWindow = isfinite(source->moments->Mrf) ? 6.0*source->moments->Mrf : RADIUS; 304 } 305 float windowRadius = PS_MAX(RADIUS, maxWindow); 306 244 307 // re-allocate image, weight, mask arrays for each peak with box big enough to fit BIG_RADIUS 245 pmSourceRedefinePixels (source, readout, source->peak->x, source->peak->y, windowRadius + 2);308 bool extend = pmSourceRedefinePixels (source, readout, source->peak->x, source->peak->y, windowRadius + 2); 246 309 psAssert (source->pixels, "WTF?"); 310 if (extend && smoothedPixels) { 311 psFree(source->tmpPtr); 312 smoothedPixels = psImageSubset(smoothedImage, source->region); 313 psAssert (smoothedPixels, "WTF?"); 314 source->tmpPtr = (psPtr) smoothedPixels ; 315 } 316 247 317 248 318 // clear the window function for this source based on the moments 249 psphotKronWindowSetSource (source, kronWindow, (j > 0), false); 250 251 // this function populates moments->Mrf,KronFlux,KronFluxErr 252 psphotKronWindowMag (source, kronWindow, windowRadius, MIN_KRON_RADIUS, maskVal); 253 psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); 254 255 // set a window function for each source based on the moments 256 psphotKronWindowSetSource (source, kronWindow, true, true); 319 // Note this function also applies cuts on the source and returns false if it 320 // does not meet the requirements for measuring the Kron Radius or Magnitude. 321 // Not that it performs that function even if KRON_APPLY_WINDOW is false 322 if (psphotKronWindowSetSource (source, kronWindow, (j > 0), false, KRON_APPLY_WINDOW)) { 323 324 // this function populates moments->Mrf,KronFlux,KronFluxErr 325 psphotKronWindowMag (source, kronWindow, windowRadius, MIN_KRON_RADIUS, maskVal, KRON_APPLY_WEIGHT, smoothedPixels); 326 psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); 327 328 // set a window function for each source based on the moments 329 psphotKronWindowSetSource (source, kronWindow, true, true, KRON_APPLY_WINDOW); 330 } 257 331 258 332 // if we subtracted it above, re-subtract the object, leave local sky 259 333 if (reSubtract) { 260 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal); 334 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal); 335 if (smoothedPixels) { 336 pmSourceSmoothOp(source, PM_MODEL_OP_FUNC, smoothedPixels, KRON_SMOOTH_SIGMA, false, maskVal, 0, 0); 337 if (j + 1 == KRON_ITERATIONS) { 338 // We're done with the smoothedPixels 339 psFree(source->tmpPtr); 340 source->tmpPtr = NULL; 341 smoothedPixels = NULL; 342 } 343 } 261 344 } 262 345 } 263 346 } 347 // psFree(smoothedPixels); 264 348 return true; 265 349 } 266 350 267 bool psphotKronWindowMag (pmSource *source, psImage *kronWindow, float radius, float minKronRadius, psImageMaskType maskVal) { 351 bool psphotKronWindowMag (pmSource *source, psImage *kronWindow, float radius, float minKronRadius, psImageMaskType maskVal, 352 bool applyWeight, psImage *smoothedPixels) { 268 353 269 354 PS_ASSERT_PTR_NON_NULL(source, false); … … 273 358 274 359 psF32 R2 = PS_SQR(radius); 275 float rsigma2 = 0.5 / PS_SQR(radius/2.0);360 float rsigma2 = applyWeight ? 0.5 / R2 : 0; 276 361 277 362 // a note about coordinates: coordinates of objects throughout psphot refer to the primary … … 286 371 // Xn = SUM (x - xc)^n * (z - sky) 287 372 373 288 374 psF32 RF = 0.0; 289 375 psF32 RS = 0.0; … … 304 390 int Ywo = source->pixels->row0; 305 391 306 psF32 **vPix = source->pixels->data.F32; 392 psF32 **vPix; 393 if (smoothedPixels) { 394 vPix = smoothedPixels->data.F32; 395 } else { 396 vPix = source->pixels->data.F32; 397 } 307 398 psF32 **vWin = kronWindow->data.F32; 308 399 psF32 **vWgt = source->variance->data.F32; … … 377 468 float Var = 0.0; 378 469 float Win = 0.0; 470 471 // set vPix to the source pixels (it may have been set to the 472 // smoothed image above) 473 vPix = source->pixels->data.F32; 474 379 475 380 476 for (psS32 row = 0; row < source->pixels->numRows ; row++) { … … 415 511 } 416 512 417 bool psphotKronWindowSetSource(pmSource *source, psImage *kronWindow, bool useKronRadius, bool insert ) {513 bool psphotKronWindowSetSource(pmSource *source, psImage *kronWindow, bool useKronRadius, bool insert, bool applyWindow) { 418 514 419 515 if (!source) return false; … … 422 518 if (source->type == PM_SOURCE_TYPE_DEFECT) return false; 423 519 if (source->type == PM_SOURCE_TYPE_SATURATED) return false; 520 if (!source->tmpFlags & PM_SOURCE_TMPF_MOMENTS_MEASURED) return false; 521 if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) return false; 424 522 psAssert(kronWindow, "need a window"); 523 524 if (!isfinite(source->moments->Mrf) || source->moments->Mrf < 0 ) return false; 525 526 if (!applyWindow) { 527 return true; 528 } 425 529 426 530 // we have a source with moments Mx, My, Mxx, Mxy, Myy. we just need to define a Gaussian that has … … 433 537 float Yo = source->moments->My; 434 538 435 float Mxx = source->moments->Mxx; 436 float Mxy = source->moments->Mxy; 437 float Myy = source->moments->Myy; 438 439 float Mmajor = 0.5*(Mxx + Myy) + 0.5*sqrt(PS_SQR(Mxx - Myy) + 4.0*PS_SQR(Mxy)); 440 float Mminor = 0.5*(Mxx + Myy) - 0.5*sqrt(PS_SQR(Mxx - Myy) + 4.0*PS_SQR(Mxy)); 441 442 // Mxx, Mxy, Myy define the elliptical shape, but Mrf defines the width 443 float scale = PS_SQR(0.5 * source->moments->Mrf) / Mmajor; 444 445 float Sxx = scale * Mmajor * Mminor / Myy; // sigma_x^2 446 float Sxy = Mxy / (scale * Mmajor * Mminor); 447 float Syy = scale * Mmajor * Mminor / Mxx; // sigma_y^2 448 449 float Smajor = sqrt(Mmajor); 539 psEllipseMoments moments; 540 moments.x2 = source->moments->Mxx; 541 moments.y2 = source->moments->Myy; 542 moments.xy = source->moments->Mxy; 543 544 psEllipseAxes axes = psEllipseMomentsToAxes(moments, 20.0); 545 if (! (isfinite(axes.major) && isfinite(axes.minor) && isfinite(axes.theta)) ) { 546 // Shall we log a proper warning? This happens often with matched sources (forced photometry) 547 // fprintf(dump, "invalid axes found id: %4d major: %f minor: %f theta: %f\n", source->id, axes.major, axes.minor, axes.theta); 548 return false; 549 } 550 551 // Why this factor of 0.5 ? 552 float scale = 0.5 * source->moments->Mrf / axes.major; 553 axes.major *= scale; 554 axes.minor *= scale; 555 556 psEllipseShape shape = psEllipseAxesToShape(axes); 557 if (! (isfinite(shape.sx) && isfinite(shape.sy) && isfinite(shape.sxy)) ) { 558 // Shall we log a proper warning? This happens often with matched sources (forced photometry) 559 // fprintf(dump, "invalid shape found id: %d sx: %f sy %f sxy: %f\n", source->id, shape.sx, shape.sy, shape.sxy); 560 return false; 561 } 562 563 float Smajor = axes.major; 450 564 451 565 int minX = PS_MIN(PS_MAX(Xo - 5*Smajor, 0), Nx - 1); … … 454 568 int maxY = PS_MIN(PS_MAX(Yo + 5*Smajor, 0), Ny - 1); 455 569 456 float rMxx = 0.5 / Sxx; 457 float rMyy = 0.5 / Syy; 458 570 float rMxx = 0.5 / PS_SQR(shape.sx); 571 float rMyy = 0.5 / PS_SQR(shape.sy); 572 float Sxy = -1. * shape.sxy; // factor of -1 is included to match the previous window function 573 // implementation. XXX: Is this correct? 574 459 575 for (int iy = minY; iy < maxY; iy++) { 460 576 for (int ix = minX; ix < maxX; ix++) { … … 467 583 float f = insert ? 1.001 - exp(-z) : 1.0 / (1.001 - exp(-z)); 468 584 469 kronWindow->data.F32[iy][ix] *= f;585 kronWindow->data.F32[iy][ix] *= f; 470 586 } 471 587 } 588 472 589 return true; 473 590 }
Note:
See TracChangeset
for help on using the changeset viewer.
