IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9336


Ignore:
Timestamp:
Oct 5, 2006, 5:06:30 PM (20 years ago)
Author:
Paul Price
Message:

Adding iteration and masking to shutter measurement

Location:
trunk/psModules/src/detrend
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/detrend/pmShutterCorrection.c

    r9315 r9336  
    6060#include <pslib.h>
    6161
     62#include "pmFPA.h"
    6263#include "pmShutterCorrection.h"
    6364#include "psVectorBracket.h"
     
    193194        const psVector *counts,
    194195        const psVector *cntError,
    195         float offref
     196        const psVector *mask,
     197        float offref,
     198        int nIter,
     199        float rej,
     200        psMaskType maskVal
    196201                                              )
    197202{
     
    232237    line->coeff[1][1] = 0;
    233238
    234     if (!psVectorFitPolynomial2D(line, NULL, 0, counts, cntError, x, y)) {
     239    psStats *stats = psStatsAlloc(0);
     240    stats->clipSigma = rej;
     241    stats->clipIter = nIter;
     242
     243    if (!psVectorClipFitPolynomial2D(line, stats, mask, maskVal, counts, cntError, x, y)) {
    235244        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to fit shutter correction.\n");
     245        psFree(stats);
    236246        psFree(x);
    237247        psFree(y);
     
    239249        return NULL;
    240250    }
     251    psFree(stats);
    241252
    242253    pmShutterCorrection *corr = pmShutterCorrectionAlloc();
     
    355366                                    psStatsOptions meanStat, // Statistic to use for mean
    356367                                    psStatsOptions stdevStat, // Statistic to use for stdev
     368                                    int nIter, // Number of iterations per pixel
     369                                    float rej, // Rejection limit
    357370                                    psMaskType maskVal // Mask value
    358371                                   )
     
    382395        if (!image) {
    383396            continue;
     397        }
     398        if (image->type.type != PS_TYPE_F32) {
     399            psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Bad type for image: %x\n", image->type.type);
     400            goto MEASURE_ERROR;
    384401        }
    385402        if (numRows == 0 || numCols == 0) {
     
    401418            goto MEASURE_ERROR;
    402419        }
     420        if (masks) {
     421            psImage *mask = masks->data[i];
     422            if (mask) {
     423                if (mask->type.type != PS_TYPE_U8) {
     424                    psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Bad type for mask: %x\n", mask->type.type);
     425                    goto MEASURE_ERROR;
     426                }
     427                if (mask->numRows != numRows || mask->numCols != numCols) {
     428                    psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     429                            "Mask sizes don't match: %dx%d vs %dx%d\n", mask->numCols, mask->numRows,
     430                            numCols, numRows);
     431                    goto MEASURE_ERROR;
     432                }
     433            }
     434        }
    403435
    404436        // Measure statistics
     
    407439        }
    408440        refs->data.F32[i] = psStatsGetValue(stats, meanStat);
     441        psTrace("psModules.detrend", 3, "Reference value for image %ld = %f\n", i, refs->data.F32[i]);
    409442        if (refs->data.F32[i] <= 0.0) {
    410443            psError(PS_ERR_UNKNOWN, true, "Measured non-positive reference value.\n");
     
    424457            samplesMean->data.F32[j][i] = psStatsGetValue(stats, meanStat) * refs->data.F32[i];
    425458            samplesStdev->data.F32[j][i] = psStatsGetValue(stats, stdevStat) * refs->data.F32[i];
     459            psTrace("psModules.detrend", 5, "Image %ld, sample %d: %f +/- %f\n", i, j,
     460                    samplesMean->data.F32[j][i], samplesStdev->data.F32[j][i]);
    426461        }
    427462    }
     
    437472        errors = psImageRow(errors, samplesStdev, i);
    438473        pmShutterCorrection *guess = pmShutterCorrectionGuess(exptimes, counts); // Guess at correction
    439         pmShutterCorrection *params = pmShutterCorrectionFullFit(exptimes, counts, errors, guess); // Correc'n
    440         if (isfinite(params->offref)) {
    441             meanRef += params->offref;
     474        pmShutterCorrection *corr = pmShutterCorrectionFullFit(exptimes, counts, errors, guess); // Correct'n
     475        psTrace("psModules.detrend", 5, "Sample reference value: %f\n", corr->offref);
     476        if (isfinite(corr->offref)) {
     477            meanRef += corr->offref;
    442478            numGood++;
    443479        }
    444         psFree(params);
     480        psFree(corr);
    445481        psFree(guess);
    446482    }
     
    455491    }
    456492    meanRef /= (float)numGood;
     493    psTrace("psModules.detrend", 3, "Mean reference value: %f\n", meanRef);
    457494
    458495    psImage *shutter = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Shutter correction image
    459496    //psImage *pattern = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Illumination pattern
     497    psVector *mask = psVectorAlloc(num, PS_TYPE_U8); // Mask for each image
     498    mask->n = num;
     499    psVectorInit(mask, 0);
     500    psTrace("psModules.detrend", 2, "Performing linear fit on individual pixels...\n");
    460501    for (int y = 0; y < numRows; y++) {
    461502        for (int x = 0; x < numCols; x++) {
    462503            for (int i = 0; i < num; i++) {
    463504                psImage *image = images->data[i]; // Image of interest
     505                psImage *maskImage;     // Mask image
     506                if (masks && (maskImage = masks->data[i])) {
     507                    mask->data.U8[i] = maskImage->data.U8[y][x];
     508                }
    464509                counts->data.F32[i] = image->data.F32[y][x] * refs->data.F32[i];
    465510                errors->data.F32[i] = sqrtf(image->data.F32[y][x]) * refs->data.F32[i];
    466511            }
    467512
    468             pmShutterCorrection *corr = pmShutterCorrectionLinFit(exptimes, counts, errors, meanRef);
     513            pmShutterCorrection *corr = pmShutterCorrectionLinFit(exptimes, counts, errors, mask, meanRef,
     514                                        nIter, rej, maskVal);
    469515            shutter->data.F32[y][x] = corr->offset;
    470516            //pattern->data.F32[y][x] = corr->scale;
     
    488534    return NULL;
    489535}
     536
     537
     538bool pmShutterCorrectionApply(pmReadout *readout, // Readout to which to apply shutter correction
     539                              const pmReadout *shutter // Shutter correction readout
     540                             )
     541{
     542    PS_ASSERT_PTR_NON_NULL(readout, false);
     543    PS_ASSERT_PTR_NON_NULL(shutter, false);
     544    PS_ASSERT_IMAGE_NON_NULL(readout->image, false);
     545    PS_ASSERT_IMAGE_NON_NULL(shutter->image, false);
     546    PS_ASSERT_IMAGE_TYPE(readout->image, PS_TYPE_F32, false);
     547    PS_ASSERT_IMAGE_TYPE(shutter->image, PS_TYPE_F32, false);
     548
     549    psRegion region = psRegionSet(readout->col0, readout->col0 + readout->image->numCols,
     550                                  readout->row0, readout->row0 + readout->image->numRows); // Detector region
     551
     552    pmCell *cell = readout->parent;     // Parent cell
     553    if (!cell) {
     554        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     555                "Parent cell is NULL --- unable to determine exposure time.\n");
     556        return false;
     557    }
     558    float exptime = psMetadataLookupF32(NULL, cell->concepts, "CELL.EXPOSURE"); // Exposure time
     559    if (!isfinite(exptime) || exptime < 0.0) {
     560        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Bad exposure time: %f.\n", exptime);
     561        return false;
     562    }
     563
     564    psImage *shutterImage = psImageSubset(shutter->image, region); // Subimage with shutter
     565    if (!shutterImage) {
     566        psString regionString = psRegionToString(region);
     567        psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Size mismatch: %s vs %dx%d\n",
     568                regionString, shutter->image->numCols, shutter->image->numRows);
     569        psFree(regionString);
     570        psFree(shutterImage);
     571        return false;
     572    }
     573    psImage *image = readout->image;    // Image to correct
     574    for (int y = 0; y < image->numRows; y++) {
     575        for (int x = 0; x < image->numCols; x++) {
     576            image->data.F32[y][x] *= exptime / (exptime + shutterImage->data.F32[y][x]);
     577        }
     578    }
     579
     580    psFree(shutterImage);
     581
     582    return true;
     583}
  • trunk/psModules/src/detrend/pmShutterCorrection.h

    r9315 r9336  
    4848 *  @author Eugene Magnier, IfA
    4949 *
    50  *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    51  *  @date $Date: 2006-10-05 23:36:26 $
     50 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     51 *  @date $Date: 2006-10-06 03:06:30 $
    5252 *
    5353 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    8282        const psVector *counts, // Counts for each exposure
    8383        const psVector *cntError, // Error in the counts, for each exp.
    84         float offref // Reference time offset
     84        const psVector *mask, // Mask for each exposure
     85        float offref, // Reference time offset
     86        int nIter, // Number of iterations
     87        float rej, // Rejection threshold (sigma)
     88        psMaskType maskVal // Mask value
    8589                                              );
    8690
     
    99103                                    psStatsOptions meanStat, // Statistic to use for mean
    100104                                    psStatsOptions stdevStat, // Statistic to use for stdev
     105                                    int nIter, // Number of iterations
     106                                    float rej, // Rejection threshold (sigma)
    101107                                    psMaskType maskVal // Mask value
    102108                                   );
    103109
     110// Apply a shutter correction
     111bool pmShutterCorrectionApply(pmReadout *readout, // Readout to which to apply shutter correction
     112                              const pmReadout *shutter // Shutter correction readout
     113                             );
     114
    104115#endif
Note: See TracChangeset for help on using the changeset viewer.