Changeset 21363 for trunk/psModules/src/camera/pmFPAMaskWeight.c
- Timestamp:
- Feb 5, 2009, 4:31:25 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/camera/pmFPAMaskWeight.c (modified) (37 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/camera/pmFPAMaskWeight.c
r21183 r21363 49 49 } 50 50 51 // Create the parent weightimages that reside in the HDU52 static void createParent Weights(pmHDU *hdu // The HDU for which to create51 // Create the parent variance images that reside in the HDU 52 static void createParentVariances(pmHDU *hdu // The HDU for which to create 53 53 ) 54 54 { … … 58 58 // Generate the parent mask images 59 59 psArray *images = hdu->images; // Array of images 60 psArray * weights = hdu->weights; // Array of weightimages61 if (! weights) {62 weights = psArrayAlloc(images->n);63 hdu-> weights = weights;60 psArray *variances = hdu->variances; // Array of variance images 61 if (!variances) { 62 variances = psArrayAlloc(images->n); 63 hdu->variances = variances; 64 64 } 65 65 66 66 for (long i = 0; i < images->n; i++) { 67 67 psImage *image = images->data[i]; // The image for this readout 68 if (!image || weights->data[i]) {68 if (!image || variances->data[i]) { 69 69 continue; 70 70 } 71 weights->data[i] = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);71 variances->data[i] = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32); 72 72 } 73 73 … … 199 199 } 200 200 201 bool pmReadoutSet Weight(pmReadout *readout, bool poisson)201 bool pmReadoutSetVariance(pmReadout *readout, bool poisson) 202 202 { 203 203 PS_ASSERT_PTR_NON_NULL(readout, false); … … 209 209 float gain = psMetadataLookupF32(&mdok, cell->concepts, "CELL.GAIN"); // Cell gain 210 210 if (!mdok || isnan(gain)) { 211 psError(PS_ERR_IO, true, "CELL.GAIN is not set --- unable to set weight.\n");211 psError(PS_ERR_IO, true, "CELL.GAIN is not set --- unable to set variance.\n"); 212 212 return false; 213 213 } 214 214 float readnoise = psMetadataLookupF32(&mdok, cell->concepts, "CELL.READNOISE"); // Cell read noise 215 215 if (!mdok || isnan(readnoise)) { 216 psError(PS_ERR_IO, true, "CELL.READNOISE is not set --- unable to set weight.\n");216 psError(PS_ERR_IO, true, "CELL.READNOISE is not set --- unable to set variance.\n"); 217 217 return false; 218 218 } … … 223 223 224 224 if (poisson) { 225 // Set weightimage to the variance in ADU = f/g + rn^2225 // Set variance image to the variance in ADU = f/g + rn^2 226 226 psImage *image = readout->image; // The image pixels 227 readout-> weight = (psImage*)psBinaryOp(readout->weight, image, "/", psScalarAlloc(gain, PS_TYPE_F32));228 229 // a negative weight is non-sensical. if the image value drops below 1, the weightmust be 1.230 readout-> weight = (psImage*)psUnaryOp(readout->weight, readout->weight, "abs");231 readout-> weight = (psImage*)psBinaryOp(readout->weight, readout->weight, "max",227 readout->variance = (psImage*)psBinaryOp(readout->variance, image, "/", psScalarAlloc(gain, PS_TYPE_F32)); 228 229 // a negative variance is non-sensical. if the image value drops below 1, the variance must be 1. 230 readout->variance = (psImage*)psUnaryOp(readout->variance, readout->variance, "abs"); 231 readout->variance = (psImage*)psBinaryOp(readout->variance, readout->variance, "max", 232 232 psScalarAlloc(1, PS_TYPE_F32)); 233 233 } else { 234 234 // Just use the read noise 235 if (!readout-> weight) {236 readout-> weight= psImageAlloc(readout->image->numCols, readout->image->numRows, PS_TYPE_F32);237 } 238 psImageInit(readout-> weight, 0.0);239 } 240 241 readout-> weight = (psImage*)psBinaryOp(readout->weight, readout->weight, "+",235 if (!readout->variance) { 236 readout->variance = psImageAlloc(readout->image->numCols, readout->image->numRows, PS_TYPE_F32); 237 } 238 psImageInit(readout->variance, 0.0); 239 } 240 241 readout->variance = (psImage*)psBinaryOp(readout->variance, readout->variance, "+", 242 242 psScalarAlloc(readnoise*readnoise/gain/gain, PS_TYPE_F32)); 243 243 … … 245 245 } 246 246 247 // this function creates the weight pixels, or uses the existing weightpixels. it will set248 // the noise pixel values only if the weightimage is not supplied249 bool pmReadoutGenerate Weight(pmReadout *readout, bool poisson)247 // this function creates the variance pixels, or uses the existing variance pixels. it will set 248 // the noise pixel values only if the variance image is not supplied 249 bool pmReadoutGenerateVariance(pmReadout *readout, bool poisson) 250 250 { 251 251 PS_ASSERT_PTR_NON_NULL(readout, false); … … 254 254 bool mdok = true; // Status of MD lookup 255 255 256 // Create the weightimage if required257 if (readout-> weight)256 // Create the variance image if required 257 if (readout->variance) 258 258 return true; 259 259 260 260 psRegion *trimsec = psMetadataLookupPtr(&mdok, cell->concepts, "CELL.TRIMSEC"); // Trim section 261 261 if (!mdok || psRegionIsNaN(*trimsec)) { 262 psError(PS_ERR_IO, true, "CELL.TRIMSEC is not set --- unable to set weight.\n");262 psError(PS_ERR_IO, true, "CELL.TRIMSEC is not set --- unable to set variance.\n"); 263 263 return false; 264 264 } … … 271 271 } 272 272 273 createParent Weights(hdu);273 createParentVariances(hdu); 274 274 275 275 // Need to identify which readout we're working with.... … … 280 280 } 281 281 282 psImage * weight = psImageSubset(hdu->weights->data[index], *trimsec); // The weightpixels283 if (! weight) {282 psImage *variance = psImageSubset(hdu->variances->data[index], *trimsec); // The variance pixels 283 if (!variance) { 284 284 psString trimsecString = psRegionToString(*trimsec); 285 psError(PS_ERR_UNKNOWN, false, "Unable to set weightfrom HDU with trimsec: %s.\n",285 psError(PS_ERR_UNKNOWN, false, "Unable to set variance from HDU with trimsec: %s.\n", 286 286 trimsecString); 287 287 psFree(trimsecString); 288 288 return false; 289 289 } 290 psImageInit( weight, 0);291 readout-> weight = weight;292 293 return pmReadoutSet Weight(readout, poisson);294 } 295 296 bool pmReadoutGenerateMask Weight(pmReadout *readout, psImageMaskType satMask, psImageMaskType badMask, bool poisson)290 psImageInit(variance, 0); 291 readout->variance = variance; 292 293 return pmReadoutSetVariance(readout, poisson); 294 } 295 296 bool pmReadoutGenerateMaskVariance(pmReadout *readout, psImageMaskType satMask, psImageMaskType badMask, bool poisson) 297 297 { 298 298 PS_ASSERT_PTR_NON_NULL(readout, false); … … 301 301 302 302 success &= pmReadoutGenerateMask(readout, satMask, badMask); 303 success &= pmReadoutGenerate Weight(readout, poisson);303 success &= pmReadoutGenerateVariance(readout, poisson); 304 304 305 305 return success; 306 306 } 307 307 308 bool pmCellGenerateMask Weight(pmCell *cell, psImageMaskType satMask, psImageMaskType badMask, bool poisson)308 bool pmCellGenerateMaskVariance(pmCell *cell, psImageMaskType satMask, psImageMaskType badMask, bool poisson) 309 309 { 310 310 PS_ASSERT_PTR_NON_NULL(cell, false); … … 314 314 for (int i = 0; i < readouts->n; i++) { 315 315 pmReadout *readout = readouts->data[i]; // The readout 316 success &= pmReadoutGenerateMask Weight(readout, poisson, satMask, badMask);316 success &= pmReadoutGenerateMaskVariance(readout, poisson, satMask, badMask); 317 317 } 318 318 … … 321 321 322 322 323 bool pmReadout WeightRenormPixels(const pmReadout *readout, psImageMaskType maskVal,323 bool pmReadoutVarianceRenormPixels(const pmReadout *readout, psImageMaskType maskVal, 324 324 psStatsOptions meanStat, psStatsOptions stdevStat, psRandom *rng) 325 325 { 326 326 PM_ASSERT_READOUT_NON_NULL(readout, false); 327 327 PM_ASSERT_READOUT_IMAGE(readout, false); 328 PM_ASSERT_READOUT_ WEIGHT(readout, false);329 330 psImage *image = readout->image, *mask = readout->mask, * weight = readout->weight; // Readout parts328 PM_ASSERT_READOUT_VARIANCE(readout, false); 329 330 psImage *image = readout->image, *mask = readout->mask, *variance = readout->variance; // Readout parts 331 331 332 332 if (!psMemIncrRefCounter(rng)) { … … 334 334 } 335 335 336 psStats * weightStats = psStatsAlloc(meanStat);// Statistics for mean337 if (!psImageBackground( weightStats, NULL, weight, mask, maskVal, rng)) {338 psError(PS_ERR_UNKNOWN, false, "Unable to measure mean weightfor image");339 psFree( weightStats);336 psStats *varianceStats = psStatsAlloc(meanStat);// Statistics for mean 337 if (!psImageBackground(varianceStats, NULL, variance, mask, maskVal, rng)) { 338 psError(PS_ERR_UNKNOWN, false, "Unable to measure mean variance for image"); 339 psFree(varianceStats); 340 340 psFree(rng); 341 341 return false; 342 342 } 343 float meanVariance = weightStats->robustMedian; // Mean variance344 psFree( weightStats);343 float meanVariance = varianceStats->robustMedian; // Mean variance 344 psFree(varianceStats); 345 345 346 346 psStats *imageStats = psStatsAlloc(stdevStat);// Statistics for mean … … 356 356 357 357 float correction = PS_SQR(stdevImage) / meanVariance; // Correction to take variance to what it should be 358 psLogMsg("psModules.camera", PS_LOG_INFO, "Renormalising weightmap by %f", correction);359 psBinaryOp( weight, weight, "*", psScalarAlloc(correction, PS_TYPE_F32));358 psLogMsg("psModules.camera", PS_LOG_INFO, "Renormalising variance map by %f", correction); 359 psBinaryOp(variance, variance, "*", psScalarAlloc(correction, PS_TYPE_F32)); 360 360 361 361 return true; … … 363 363 364 364 365 bool pmReadout WeightRenormPhot(const pmReadout *readout, psImageMaskType maskVal, int num, float width,365 bool pmReadoutVarianceRenormPhot(const pmReadout *readout, psImageMaskType maskVal, int num, float width, 366 366 psStatsOptions meanStat, psStatsOptions stdevStat, psRandom *rng) 367 367 { 368 368 PM_ASSERT_READOUT_NON_NULL(readout, false); 369 369 PM_ASSERT_READOUT_IMAGE(readout, false); 370 PM_ASSERT_READOUT_ WEIGHT(readout, false);370 PM_ASSERT_READOUT_VARIANCE(readout, false); 371 371 372 372 if (!psMemIncrRefCounter(rng)) { … … 374 374 } 375 375 376 psImage *image = readout->image, *mask = readout->mask, * weight = readout->weight; // Readout images376 psImage *image = readout->image, *mask = readout->mask, *variance = readout->variance; // Readout images 377 377 378 378 // Measure background … … 424 424 float sumNoise = 0.0; // Sum for noise measurement 425 425 float sumSource = 0.0; // Sum for source measurement 426 float sum Weight = 0.0; // Sum for weightmeasurement426 float sumVariance = 0.0; // Sum for variance measurement 427 427 float sumGauss = 0.0, sumGauss2 = 0.0; // Sums of Gaussian kernels 428 428 for (int v = 0, y = yPix - size; v < fullSize; v++, y++) { 429 429 float xSumNoise = 0.0; // Sum for noise measurement in x 430 430 float xSumSource = 0.0; // Sum for source measurement in x 431 float xSum Weight = 0.0; // Sum for weightmeasurement in x431 float xSumVariance = 0.0; // Sum for variance measurement in x 432 432 float xSumGauss = 0.0, xSumGauss2 = 0.0; // Sums of Gaussian kernels in x 433 433 float yGauss = gauss->data.F32[v]; // Value of Gaussian in y … … 441 441 xSumNoise += value * xGauss; 442 442 xSumSource += (value + peakFlux * xGauss * yGauss) * xGauss; 443 xSum Weight += weight->data.F32[y][x] * xGauss2;443 xSumVariance += variance->data.F32[y][x] * xGauss2; 444 444 xSumGauss += xGauss; 445 445 xSumGauss2 += xGauss2; … … 448 448 sumNoise += xSumNoise * yGauss; 449 449 sumSource += xSumSource * yGauss; 450 sum Weight += xSumWeight* yGauss2;450 sumVariance += xSumVariance * yGauss2; 451 451 sumGauss += xSumGauss * yGauss; 452 452 sumGauss2 += xSumGauss2 * yGauss2; … … 454 454 455 455 photMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = ((isfinite(sumNoise) && isfinite(sumSource) && 456 isfinite(sum Weight) && sumGauss > 0 && sumGauss2 > 0) ?456 isfinite(sumVariance) && sumGauss > 0 && sumGauss2 > 0) ? 457 457 0 : 0xFF); 458 458 459 459 float smoothImageNoise = sumNoise / sumGauss; // Value of smoothed image pixel for noise 460 460 float smoothImageSource = sumSource / sumGauss; // Value of smoothed image pixel for source 461 float smooth Weight = sumWeight / sumGauss2; // Value of smoothed weightpixel461 float smoothVariance = sumVariance / sumGauss2; // Value of smoothed variance pixel 462 462 463 463 noise->data.F32[i] = smoothImageNoise; 464 464 source->data.F32[i] = smoothImageSource; 465 guess->data.F32[i] = (sumSource > 0) ? sigFactor * PS_SQR(smoothImageSource) / smooth Weight: 0.0;465 guess->data.F32[i] = (sumSource > 0) ? sigFactor * PS_SQR(smoothImageSource) / smoothVariance : 0.0; 466 466 psTrace("psModules.camera", 10, "Flux %d (%d,%d): %f, %f, %f\n", 467 i, xPix, yPix, smoothImageNoise, smoothImageSource, smooth Weight);467 i, xPix, yPix, smoothImageNoise, smoothImageSource, smoothVariance); 468 468 } 469 469 psFree(gauss); … … 516 516 psFree(photMask); 517 517 518 psLogMsg("psModules.camera", PS_LOG_INFO, "Renormalising weightmap by %f", meanRatio);519 psBinaryOp( weight, weight, "*", psScalarAlloc(meanRatio, PS_TYPE_F32));518 psLogMsg("psModules.camera", PS_LOG_INFO, "Renormalising variance map by %f", meanRatio); 519 psBinaryOp(variance, variance, "*", psScalarAlloc(meanRatio, PS_TYPE_F32)); 520 520 521 521 return true; … … 523 523 524 524 525 bool pmReadout WeightRenorm(const pmReadout *readout, psImageMaskType maskVal, psStatsOptions meanStat,525 bool pmReadoutVarianceRenorm(const pmReadout *readout, psImageMaskType maskVal, psStatsOptions meanStat, 526 526 psStatsOptions stdevStat, int width, psRandom *rng) 527 527 { 528 528 PM_ASSERT_READOUT_NON_NULL(readout, false); 529 529 PM_ASSERT_READOUT_IMAGE(readout, false); 530 PM_ASSERT_READOUT_ WEIGHT(readout, false);530 PM_ASSERT_READOUT_VARIANCE(readout, false); 531 531 PS_ASSERT_INT_POSITIVE(width, false); 532 532 … … 535 535 } 536 536 537 psImage *image = readout->image, *mask = readout->mask, * weight = readout->weight; // Readout images537 psImage *image = readout->image, *mask = readout->mask, *variance = readout->variance; // Readout images 538 538 int numCols = image->numCols, numRows = image->numRows; // Size of images 539 539 int xNum = numCols / width + 1, yNum = numRows / width + 1; // Number of renormalisation regions … … 554 554 psRegion region = psRegionSet(xMin, xMax, yMin, yMax); // Region of interest 555 555 psImage *subImage = psImageSubset(image, region); // Sub-image of the image pixels 556 psImage *sub Weight = psImageSubset(weight, region); // Sub image of the weightpixels556 psImage *subVariance = psImageSubset(variance, region); // Sub image of the variance pixels 557 557 psImage *subMask = mask ? psImageSubset(mask, region) : NULL; // Sub-image of the mask pixels 558 558 559 559 if (!psImageBackground(stdevStats, &buffer, subImage, subMask, maskVal, rng) || 560 !psImageBackground(meanStats, &buffer, sub Weight, subMask, maskVal, rng)) {560 !psImageBackground(meanStats, &buffer, subVariance, subMask, maskVal, rng)) { 561 561 // Nothing we can do about it, but don't want to keel over and die, so do our best to flag it. 562 562 psString regionStr = psRegionToString(region); // String with region … … 564 564 psFree(regionStr); 565 565 psErrorClear(); 566 psImageInit(sub Weight, NAN);566 psImageInit(subVariance, NAN); 567 567 if (subMask) { 568 568 psImageInit(subMask, maskVal); … … 574 574 "Region [%d:%d,%d:%d] has variance %lf, but mean of variance map is %lf\n", 575 575 xMin, xMax, yMin, yMax, PS_SQR(stdev), meanVar); 576 psBinaryOp(sub Weight, subWeight, "*", psScalarAlloc(PS_SQR(stdev) / meanVar, PS_TYPE_F32));576 psBinaryOp(subVariance, subVariance, "*", psScalarAlloc(PS_SQR(stdev) / meanVar, PS_TYPE_F32)); 577 577 } 578 578 579 579 psFree(subImage); 580 psFree(sub Weight);580 psFree(subVariance); 581 581 psFree(subMask); 582 582 } … … 597 597 598 598 psImage *image = readout->image; // Readout's image 599 psImage * weight = readout->weight; // Readout's weight599 psImage *variance = readout->variance; // Readout's variance 600 600 int numCols = image->numCols, numRows = image->numRows; // Size of image 601 601 … … 607 607 for (int y = 0; y < numRows; y++) { 608 608 for (int x = 0; x < numCols; x++) { 609 if (!isfinite(image->data.F32[y][x]) || ( weight && !isfinite(weight->data.F32[y][x]))) {609 if (!isfinite(image->data.F32[y][x]) || (variance && !isfinite(variance->data.F32[y][x]))) { 610 610 mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskVal; 611 611 } … … 627 627 psImageMaskType **maskData = readout->mask->data.PS_TYPE_IMAGE_MASK_DATA; // Dereference mask 628 628 psF32 **imageData = readout->image->data.F32;// Dereference image 629 psF32 ** weightData = readout->weight ? readout->weight->data.F32 : NULL; // Dereference weightmap629 psF32 **varianceData = readout->variance ? readout->variance->data.F32 : NULL; // Dereference variance map 630 630 631 631 for (int y = 0; y < numRows; y++) { … … 633 633 if (maskData[y][x] & maskVal) { 634 634 imageData[y][x] = NAN; 635 if ( weightData) {636 weightData[y][x] = NAN;635 if (varianceData) { 636 varianceData[y][x] = NAN; 637 637 } 638 638 } … … 656 656 psImage *image = readout->image; // Image 657 657 psImage *mask = readout->mask; // Mask 658 psImage * weight = readout->weight; // Weightmap659 660 psImageInterpolation *interp = psImageInterpolationAlloc(mode, image, weight, mask, maskVal,658 psImage *variance = readout->variance; // Variance map 659 660 psImageInterpolation *interp = psImageInterpolationAlloc(mode, image, variance, mask, maskVal, 661 661 NAN, NAN, maskBad, maskPoor, poorFrac, 0); 662 662 interp->shifting = false; // Turn off "exact shifts" so we get proper interpolation … … 666 666 psPixels *pixels = psPixelsAllocEmpty(PIXELS_BUFFER); // Pixels that have been interpolated 667 667 psVector *imagePix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_F32); // Corresponding values for image 668 psVector * weightPix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_F32); // Corresponding values for weight668 psVector *variancePix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_F32); // Corresponding values for variance 669 669 psVector *maskPix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_IMAGE_MASK); // Corresponding values for mask 670 670 // NOTE: maskPix carries the actual image mask values -- do NOT use … … 675 675 for (int x = 0; x < numCols; x++) { 676 676 if (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) { 677 double imageValue, weightValue; // Image and weightvalue from interpolation677 double imageValue, varianceValue; // Image and variance value from interpolation 678 678 psImageMaskType maskValue = 0; // Mask value from interpolation 679 psImageInterpolateStatus status = psImageInterpolate(&imageValue, & weightValue, &maskValue, x, y, interp);679 psImageInterpolateStatus status = psImageInterpolate(&imageValue, &varianceValue, &maskValue, x, y, interp); 680 680 if (status == PS_INTERPOLATE_STATUS_ERROR || status == PS_INTERPOLATE_STATUS_OFF) { 681 681 psError(PS_ERR_UNKNOWN, false, "Unable to interpolate readout at %d,%d", x, y); … … 683 683 psFree(pixels); 684 684 psFree(imagePix); 685 psFree( weightPix);685 psFree(variancePix); 686 686 psFree(maskPix); 687 687 return false; … … 694 694 pixels = psPixelsAdd(pixels, PIXELS_BUFFER, x, y); 695 695 imagePix = psVectorExtend(imagePix, PIXELS_BUFFER, 1); 696 weightPix = psVectorExtend(weightPix, PIXELS_BUFFER, 1);696 variancePix = psVectorExtend(variancePix, PIXELS_BUFFER, 1); 697 697 maskPix = psVectorExtend(maskPix, PIXELS_BUFFER, 1); 698 698 imagePix->data.F32[numBad] = imageValue; 699 weightPix->data.F32[numBad] = weightValue;699 variancePix->data.F32[numBad] = varianceValue; 700 700 maskPix->data.PS_TYPE_IMAGE_MASK_DATA[numBad] = maskValue; 701 701 numBad++; … … 709 709 int x = pixels->data[i].x, y = pixels->data[i].y; // Coordinates of pixel 710 710 image->data.F32[y][x] = imagePix->data.F32[i]; 711 weight->data.F32[y][x] = weightPix->data.F32[i];711 variance->data.F32[y][x] = variancePix->data.F32[i]; 712 712 mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] = maskPix->data.PS_TYPE_IMAGE_MASK_DATA[i]; 713 713 } … … 715 715 psFree(pixels); 716 716 psFree(imagePix); 717 psFree( weightPix);717 psFree(variancePix); 718 718 psFree(maskPix); 719 719
Note:
See TracChangeset
for help on using the changeset viewer.
