IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 27097


Ignore:
Timestamp:
Feb 25, 2010, 5:05:40 PM (16 years ago)
Author:
Paul Price
Message:

Multiple retries to measure background statistics, to avoid writing HUGE files just for a lot of NANs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/fits/psFitsScale.c

    r25439 r27097  
    1515#include "psImage.h"
    1616#include "psFits.h"
     17#include "psStats.h"
     18#include "psImageStats.h"
    1719#include "psImageBackground.h"
    18 #include "psStats.h"
    1920#include "psImageStructManip.h"
    2021
     
    2930#define MEAN_STAT PS_STAT_ROBUST_MEDIAN // Statistic to use for mean
    3031#define STDEV_STAT PS_STAT_ROBUST_STDEV // Statistic to use for stdev
     32
     33#define DESPERATE_MEAN_STAT PS_STAT_SAMPLE_MEDIAN // Statistic to use for mean when deperate
     34#define DESPERATE_STDEV_STAT PS_STAT_SAMPLE_QUARTILE // Statistic to use for stdev when desperate
    3135
    3236
     
    118122    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
    119123    psStats *stats = psStatsAlloc(MEAN_STAT | STDEV_STAT); // Statistics object
    120     if (!psImageBackground(stats, NULL, image, mask, maskVal, rng)) {
    121         psError(PS_ERR_UNKNOWN, false, "Unable to perform statistics on image");
    122         psFree(rng);
    123         psFree(stats);
    124         return false;
    125     }
     124    psVector *sample = NULL;                               // Sample of pixels
     125    double mean, stdev;                                    // Mean and standard deviation
     126    if (!psImageBackground(stats, &sample, image, mask, maskVal, rng)) {
     127        // It could be because the image is entirely masked, in which case we don't want to error
     128        if (sample->n == 0) {
     129            psLogMsg("psLib.fits", PS_LOG_DETAIL, "Image has no good pixels, setting BSCALE = 1, BZERO = 0");
     130            psErrorClear();
     131            *bscale = 1.0;
     132            *bzero = 0.0;
     133            psFree(rng);
     134            psFree(stats);
     135            psFree(sample);
     136            return true;
     137        }
     138        psLogMsg("psLib.fits", PS_LOG_DETAIL,
     139                 "Couldn't measure background statistics for image quantisation; retrying.");
     140        psErrorClear();
     141        // Retry using all the available pixels
     142        stats->nSubsample = image->numCols * image->numRows + 1;
     143        if (!psImageStats(stats, image, mask, maskVal)) {
     144            psLogMsg("psLib.fits", PS_LOG_DETAIL,
     145                     "Couldn't measure background statistics for image quantisation (attempt 2); retrying.");
     146            psErrorClear();
     147            // Retry with desperate statistic
     148            stats->options = DESPERATE_MEAN_STAT | DESPERATE_STDEV_STAT;
     149            if (!psImageStats(stats, image, mask, maskVal)) {
     150                psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics for image");
     151                psFree(rng);
     152                psFree(stats);
     153                psFree(sample);
     154                return false;
     155            } else {
     156                // Desperate retry
     157                mean = psStatsGetValue(stats, DESPERATE_MEAN_STAT);
     158                stdev = psStatsGetValue(stats, DESPERATE_STDEV_STAT);
     159            }
     160        } else {
     161            // Retry with all available pixels
     162            mean = psStatsGetValue(stats, MEAN_STAT);
     163            stdev = psStatsGetValue(stats, STDEV_STAT);
     164        }
     165    } else {
     166        // First attempt
     167        mean = psStatsGetValue(stats, MEAN_STAT);
     168        stdev = psStatsGetValue(stats, STDEV_STAT);
     169    }
     170    psFree(sample);
    126171    psFree(rng);
    127 
    128     double mean = psStatsGetValue(stats, MEAN_STAT); // Mean
    129     double stdev = psStatsGetValue(stats, STDEV_STAT); // Standard deviation
    130172    psFree(stats);
    131173    if (!isfinite(mean) || !isfinite(stdev)) {
Note: See TracChangeset for help on using the changeset viewer.