IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 5, 2009, 4:31:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging pap_branch_20090128. Resolved a small number of conflicts. Compiles, but not tested in detail.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/camera/pmFPAMaskWeight.c

    r21183 r21363  
    4949}
    5050
    51 // Create the parent weight images that reside in the HDU
    52 static void createParentWeights(pmHDU *hdu // The HDU for which to create
     51// Create the parent variance images that reside in the HDU
     52static void createParentVariances(pmHDU *hdu // The HDU for which to create
    5353                               )
    5454{
     
    5858    // Generate the parent mask images
    5959    psArray *images = hdu->images;      // Array of images
    60     psArray *weights = hdu->weights;    // Array of weight images
    61     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;
    6464    }
    6565
    6666    for (long i = 0; i < images->n; i++) {
    6767        psImage *image = images->data[i]; // The image for this readout
    68         if (!image || weights->data[i]) {
     68        if (!image || variances->data[i]) {
    6969            continue;
    7070        }
    71         weights->data[i] = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
     71        variances->data[i] = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
    7272    }
    7373
     
    199199}
    200200
    201 bool pmReadoutSetWeight(pmReadout *readout, bool poisson)
     201bool pmReadoutSetVariance(pmReadout *readout, bool poisson)
    202202{
    203203    PS_ASSERT_PTR_NON_NULL(readout, false);
     
    209209    float gain = psMetadataLookupF32(&mdok, cell->concepts, "CELL.GAIN"); // Cell gain
    210210    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");
    212212        return false;
    213213    }
    214214    float readnoise = psMetadataLookupF32(&mdok, cell->concepts, "CELL.READNOISE"); // Cell read noise
    215215    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");
    217217        return false;
    218218    }
     
    223223
    224224    if (poisson) {
    225         // Set weight image to the variance in ADU = f/g + rn^2
     225        // Set variance image to the variance in ADU = f/g + rn^2
    226226        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 weight must 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",
    232232                                               psScalarAlloc(1, PS_TYPE_F32));
    233233    } else {
    234234        // 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, "+",
    242242                                           psScalarAlloc(readnoise*readnoise/gain/gain, PS_TYPE_F32));
    243243
     
    245245}
    246246
    247 // this function creates the weight pixels, or uses the existing weight pixels.  it will set
    248 // the noise pixel values only if the weight image is not supplied
    249 bool pmReadoutGenerateWeight(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
     249bool pmReadoutGenerateVariance(pmReadout *readout, bool poisson)
    250250{
    251251    PS_ASSERT_PTR_NON_NULL(readout, false);
     
    254254    bool mdok = true;                   // Status of MD lookup
    255255
    256     // Create the weight image if required
    257     if (readout->weight)
     256    // Create the variance image if required
     257    if (readout->variance)
    258258        return true;
    259259
    260260    psRegion *trimsec = psMetadataLookupPtr(&mdok, cell->concepts, "CELL.TRIMSEC"); // Trim section
    261261    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");
    263263        return false;
    264264    }
     
    271271    }
    272272
    273     createParentWeights(hdu);
     273    createParentVariances(hdu);
    274274
    275275    // Need to identify which readout we're working with....
     
    280280    }
    281281
    282     psImage *weight = psImageSubset(hdu->weights->data[index], *trimsec); // The weight pixels
    283     if (!weight) {
     282    psImage *variance = psImageSubset(hdu->variances->data[index], *trimsec); // The variance pixels
     283    if (!variance) {
    284284        psString trimsecString = psRegionToString(*trimsec);
    285         psError(PS_ERR_UNKNOWN, false, "Unable to set weight from HDU with trimsec: %s.\n",
     285        psError(PS_ERR_UNKNOWN, false, "Unable to set variance from HDU with trimsec: %s.\n",
    286286                trimsecString);
    287287        psFree(trimsecString);
    288288        return false;
    289289    }
    290     psImageInit(weight, 0);
    291     readout->weight = weight;
    292 
    293     return pmReadoutSetWeight(readout, poisson);
    294 }
    295 
    296 bool pmReadoutGenerateMaskWeight(pmReadout *readout, psImageMaskType satMask, psImageMaskType badMask, bool poisson)
     290    psImageInit(variance, 0);
     291    readout->variance = variance;
     292
     293    return pmReadoutSetVariance(readout, poisson);
     294}
     295
     296bool pmReadoutGenerateMaskVariance(pmReadout *readout, psImageMaskType satMask, psImageMaskType badMask, bool poisson)
    297297{
    298298    PS_ASSERT_PTR_NON_NULL(readout, false);
     
    301301
    302302    success &= pmReadoutGenerateMask(readout, satMask, badMask);
    303     success &= pmReadoutGenerateWeight(readout, poisson);
     303    success &= pmReadoutGenerateVariance(readout, poisson);
    304304
    305305    return success;
    306306}
    307307
    308 bool pmCellGenerateMaskWeight(pmCell *cell, psImageMaskType satMask, psImageMaskType badMask, bool poisson)
     308bool pmCellGenerateMaskVariance(pmCell *cell, psImageMaskType satMask, psImageMaskType badMask, bool poisson)
    309309{
    310310    PS_ASSERT_PTR_NON_NULL(cell, false);
     
    314314    for (int i = 0; i < readouts->n; i++) {
    315315        pmReadout *readout = readouts->data[i]; // The readout
    316         success &= pmReadoutGenerateMaskWeight(readout, poisson, satMask, badMask);
     316        success &= pmReadoutGenerateMaskVariance(readout, poisson, satMask, badMask);
    317317    }
    318318
     
    321321
    322322
    323 bool pmReadoutWeightRenormPixels(const pmReadout *readout, psImageMaskType maskVal,
     323bool pmReadoutVarianceRenormPixels(const pmReadout *readout, psImageMaskType maskVal,
    324324                                 psStatsOptions meanStat, psStatsOptions stdevStat, psRandom *rng)
    325325{
    326326    PM_ASSERT_READOUT_NON_NULL(readout, false);
    327327    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 parts
     328    PM_ASSERT_READOUT_VARIANCE(readout, false);
     329
     330    psImage *image = readout->image, *mask = readout->mask, *variance = readout->variance; // Readout parts
    331331
    332332    if (!psMemIncrRefCounter(rng)) {
     
    334334    }
    335335
    336     psStats *weightStats = psStatsAlloc(meanStat);// Statistics for mean
    337     if (!psImageBackground(weightStats, NULL, weight, mask, maskVal, rng)) {
    338         psError(PS_ERR_UNKNOWN, false, "Unable to measure mean weight for 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);
    340340        psFree(rng);
    341341        return false;
    342342    }
    343     float meanVariance = weightStats->robustMedian; // Mean variance
    344     psFree(weightStats);
     343    float meanVariance = varianceStats->robustMedian; // Mean variance
     344    psFree(varianceStats);
    345345
    346346    psStats *imageStats = psStatsAlloc(stdevStat);// Statistics for mean
     
    356356
    357357    float correction = PS_SQR(stdevImage) / meanVariance; // Correction to take variance to what it should be
    358     psLogMsg("psModules.camera", PS_LOG_INFO, "Renormalising weight map 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));
    360360
    361361    return true;
     
    363363
    364364
    365 bool pmReadoutWeightRenormPhot(const pmReadout *readout, psImageMaskType maskVal, int num, float width,
     365bool pmReadoutVarianceRenormPhot(const pmReadout *readout, psImageMaskType maskVal, int num, float width,
    366366                               psStatsOptions meanStat, psStatsOptions stdevStat, psRandom *rng)
    367367{
    368368    PM_ASSERT_READOUT_NON_NULL(readout, false);
    369369    PM_ASSERT_READOUT_IMAGE(readout, false);
    370     PM_ASSERT_READOUT_WEIGHT(readout, false);
     370    PM_ASSERT_READOUT_VARIANCE(readout, false);
    371371
    372372    if (!psMemIncrRefCounter(rng)) {
     
    374374    }
    375375
    376     psImage *image = readout->image, *mask = readout->mask, *weight = readout->weight; // Readout images
     376    psImage *image = readout->image, *mask = readout->mask, *variance = readout->variance; // Readout images
    377377
    378378    // Measure background
     
    424424        float sumNoise = 0.0;       // Sum for noise measurement
    425425        float sumSource = 0.0;      // Sum for source measurement
    426         float sumWeight = 0.0;      // Sum for weight measurement
     426        float sumVariance = 0.0;      // Sum for variance measurement
    427427        float sumGauss = 0.0, sumGauss2 = 0.0; // Sums of Gaussian kernels
    428428        for (int v = 0, y = yPix - size; v < fullSize; v++, y++) {
    429429            float xSumNoise = 0.0;  // Sum for noise measurement in x
    430430            float xSumSource = 0.0; // Sum for source measurement in x
    431             float xSumWeight = 0.0; // Sum for weight measurement in x
     431            float xSumVariance = 0.0; // Sum for variance measurement in x
    432432            float xSumGauss = 0.0, xSumGauss2 = 0.0; // Sums of Gaussian kernels in x
    433433            float yGauss = gauss->data.F32[v]; // Value of Gaussian in y
     
    441441                xSumNoise += value * xGauss;
    442442                xSumSource += (value + peakFlux * xGauss * yGauss) * xGauss;
    443                 xSumWeight += weight->data.F32[y][x] * xGauss2;
     443                xSumVariance += variance->data.F32[y][x] * xGauss2;
    444444                xSumGauss += xGauss;
    445445                xSumGauss2 += xGauss2;
     
    448448            sumNoise += xSumNoise * yGauss;
    449449            sumSource += xSumSource * yGauss;
    450             sumWeight += xSumWeight * yGauss2;
     450            sumVariance += xSumVariance * yGauss2;
    451451            sumGauss += xSumGauss * yGauss;
    452452            sumGauss2 += xSumGauss2 * yGauss2;
     
    454454
    455455        photMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = ((isfinite(sumNoise) && isfinite(sumSource) &&
    456                                                 isfinite(sumWeight) && sumGauss > 0 && sumGauss2 > 0) ?
     456                                                isfinite(sumVariance) && sumGauss > 0 && sumGauss2 > 0) ?
    457457                                               0 : 0xFF);
    458458
    459459        float smoothImageNoise = sumNoise / sumGauss; // Value of smoothed image pixel for noise
    460460        float smoothImageSource = sumSource / sumGauss; // Value of smoothed image pixel for source
    461         float smoothWeight = sumWeight / sumGauss2; // Value of smoothed weight pixel
     461        float smoothVariance = sumVariance / sumGauss2; // Value of smoothed variance pixel
    462462
    463463        noise->data.F32[i] = smoothImageNoise;
    464464        source->data.F32[i] = smoothImageSource;
    465         guess->data.F32[i] = (sumSource > 0) ? sigFactor * PS_SQR(smoothImageSource) / smoothWeight : 0.0;
     465        guess->data.F32[i] = (sumSource > 0) ? sigFactor * PS_SQR(smoothImageSource) / smoothVariance : 0.0;
    466466        psTrace("psModules.camera", 10, "Flux %d (%d,%d): %f, %f, %f\n",
    467                 i, xPix, yPix, smoothImageNoise, smoothImageSource, smoothWeight);
     467                i, xPix, yPix, smoothImageNoise, smoothImageSource, smoothVariance);
    468468    }
    469469    psFree(gauss);
     
    516516    psFree(photMask);
    517517
    518     psLogMsg("psModules.camera", PS_LOG_INFO, "Renormalising weight map 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));
    520520
    521521    return true;
     
    523523
    524524
    525 bool pmReadoutWeightRenorm(const pmReadout *readout, psImageMaskType maskVal, psStatsOptions meanStat,
     525bool pmReadoutVarianceRenorm(const pmReadout *readout, psImageMaskType maskVal, psStatsOptions meanStat,
    526526                           psStatsOptions stdevStat, int width, psRandom *rng)
    527527{
    528528    PM_ASSERT_READOUT_NON_NULL(readout, false);
    529529    PM_ASSERT_READOUT_IMAGE(readout, false);
    530     PM_ASSERT_READOUT_WEIGHT(readout, false);
     530    PM_ASSERT_READOUT_VARIANCE(readout, false);
    531531    PS_ASSERT_INT_POSITIVE(width, false);
    532532
     
    535535    }
    536536
    537     psImage *image = readout->image, *mask = readout->mask, *weight = readout->weight; // Readout images
     537    psImage *image = readout->image, *mask = readout->mask, *variance = readout->variance; // Readout images
    538538    int numCols = image->numCols, numRows = image->numRows; // Size of images
    539539    int xNum = numCols / width + 1, yNum = numRows / width + 1; // Number of renormalisation regions
     
    554554            psRegion region = psRegionSet(xMin, xMax, yMin, yMax); // Region of interest
    555555            psImage *subImage = psImageSubset(image, region); // Sub-image of the image pixels
    556             psImage *subWeight = psImageSubset(weight, region); // Sub image of the weight pixels
     556            psImage *subVariance = psImageSubset(variance, region); // Sub image of the variance pixels
    557557            psImage *subMask = mask ? psImageSubset(mask, region) : NULL; // Sub-image of the mask pixels
    558558
    559559            if (!psImageBackground(stdevStats, &buffer, subImage, subMask, maskVal, rng) ||
    560                 !psImageBackground(meanStats, &buffer, subWeight, subMask, maskVal, rng)) {
     560                !psImageBackground(meanStats, &buffer, subVariance, subMask, maskVal, rng)) {
    561561                // Nothing we can do about it, but don't want to keel over and die, so do our best to flag it.
    562562                psString regionStr = psRegionToString(region); // String with region
     
    564564                psFree(regionStr);
    565565                psErrorClear();
    566                 psImageInit(subWeight, NAN);
     566                psImageInit(subVariance, NAN);
    567567                if (subMask) {
    568568                    psImageInit(subMask, maskVal);
     
    574574                        "Region [%d:%d,%d:%d] has variance %lf, but mean of variance map is %lf\n",
    575575                        xMin, xMax, yMin, yMax, PS_SQR(stdev), meanVar);
    576                 psBinaryOp(subWeight, subWeight, "*", psScalarAlloc(PS_SQR(stdev) / meanVar, PS_TYPE_F32));
     576                psBinaryOp(subVariance, subVariance, "*", psScalarAlloc(PS_SQR(stdev) / meanVar, PS_TYPE_F32));
    577577            }
    578578
    579579            psFree(subImage);
    580             psFree(subWeight);
     580            psFree(subVariance);
    581581            psFree(subMask);
    582582        }
     
    597597
    598598    psImage *image = readout->image;    // Readout's image
    599     psImage *weight = readout->weight;  // Readout's weight
     599    psImage *variance = readout->variance;  // Readout's variance
    600600    int numCols = image->numCols, numRows = image->numRows; // Size of image
    601601
     
    607607    for (int y = 0; y < numRows; y++) {
    608608        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]))) {
    610610                mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskVal;
    611611            }
     
    627627    psImageMaskType **maskData = readout->mask->data.PS_TYPE_IMAGE_MASK_DATA; // Dereference mask
    628628    psF32 **imageData = readout->image->data.F32;// Dereference image
    629     psF32 **weightData = readout->weight ? readout->weight->data.F32 : NULL; // Dereference weight map
     629    psF32 **varianceData = readout->variance ? readout->variance->data.F32 : NULL; // Dereference variance map
    630630
    631631    for (int y = 0; y < numRows; y++) {
     
    633633            if (maskData[y][x] & maskVal) {
    634634                imageData[y][x] = NAN;
    635                 if (weightData) {
    636                     weightData[y][x] = NAN;
     635                if (varianceData) {
     636                    varianceData[y][x] = NAN;
    637637                }
    638638            }
     
    656656    psImage *image = readout->image;    // Image
    657657    psImage *mask = readout->mask;      // Mask
    658     psImage *weight = readout->weight;  // Weight map
    659 
    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,
    661661                                                             NAN, NAN, maskBad, maskPoor, poorFrac, 0);
    662662    interp->shifting = false;           // Turn off "exact shifts" so we get proper interpolation
     
    666666    psPixels *pixels = psPixelsAllocEmpty(PIXELS_BUFFER); // Pixels that have been interpolated
    667667    psVector *imagePix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_F32); // Corresponding values for image
    668     psVector *weightPix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_F32); // Corresponding values for weight
     668    psVector *variancePix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_F32); // Corresponding values for variance
    669669    psVector *maskPix = psVectorAllocEmpty(PIXELS_BUFFER, PS_TYPE_IMAGE_MASK); // Corresponding values for mask
    670670    // NOTE: maskPix carries the actual image mask values -- do NOT use
     
    675675        for (int x = 0; x < numCols; x++) {
    676676            if (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) {
    677                 double imageValue, weightValue; // Image and weight value from interpolation
     677                double imageValue, varianceValue; // Image and variance value from interpolation
    678678                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);
    680680                if (status == PS_INTERPOLATE_STATUS_ERROR || status == PS_INTERPOLATE_STATUS_OFF) {
    681681                    psError(PS_ERR_UNKNOWN, false, "Unable to interpolate readout at %d,%d", x, y);
     
    683683                    psFree(pixels);
    684684                    psFree(imagePix);
    685                     psFree(weightPix);
     685                    psFree(variancePix);
    686686                    psFree(maskPix);
    687687                    return false;
     
    694694                pixels = psPixelsAdd(pixels, PIXELS_BUFFER, x, y);
    695695                imagePix = psVectorExtend(imagePix, PIXELS_BUFFER, 1);
    696                 weightPix = psVectorExtend(weightPix, PIXELS_BUFFER, 1);
     696                variancePix = psVectorExtend(variancePix, PIXELS_BUFFER, 1);
    697697                maskPix = psVectorExtend(maskPix, PIXELS_BUFFER, 1);
    698698                imagePix->data.F32[numBad] = imageValue;
    699                 weightPix->data.F32[numBad] = weightValue;
     699                variancePix->data.F32[numBad] = varianceValue;
    700700                maskPix->data.PS_TYPE_IMAGE_MASK_DATA[numBad] = maskValue;
    701701                numBad++;
     
    709709        int x = pixels->data[i].x, y = pixels->data[i].y; // Coordinates of pixel
    710710        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];
    712712        mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] = maskPix->data.PS_TYPE_IMAGE_MASK_DATA[i];
    713713    }
     
    715715    psFree(pixels);
    716716    psFree(imagePix);
    717     psFree(weightPix);
     717    psFree(variancePix);
    718718    psFree(maskPix);
    719719
Note: See TracChangeset for help on using the changeset viewer.