IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 29, 2010, 3:55:49 PM (16 years ago)
Author:
eugene
Message:

update merges from trunk

Location:
branches/eam_branches/20100225
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20100225

  • branches/eam_branches/20100225/psLib/src/fits/psFitsScale.c

    r25439 r27517  
    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
     124    double mean, stdev;                                    // Mean and standard deviation
    120125    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;
     126        // It could be because the image is entirely masked, in which case we don't want to error
     127        bool good = false;              // Any good pixels?
     128
     129
     130// Find good pixels in an image, by image type
     131#define GOOD_PIXELS_CASE(TYPE) \
     132      case PS_TYPE_##TYPE: \
     133        for (int y = 0; y < image->numRows && !good; y++) { \
     134            for (int x = 0; x < image->numCols && !good; x++) { \
     135                if (mask && (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal)) { \
     136                    continue; \
     137                } \
     138                if (!isfinite(image->data.TYPE[y][x])) { \
     139                    continue; \
     140                } \
     141                good = true; \
     142            } \
     143        } \
     144        break;
     145
     146        switch (image->type.type) {
     147            GOOD_PIXELS_CASE(F32);
     148            GOOD_PIXELS_CASE(F64);
     149          default:
     150            psAbort("Unsupported case: %x", image->type.type);
     151        }
     152
     153        if (!good) {
     154            psLogMsg("psLib.fits", PS_LOG_DETAIL, "Image has no good pixels, setting BSCALE = 1, BZERO = 0");
     155            psErrorClear();
     156            *bscale = 1.0;
     157            *bzero = 0.0;
     158            psFree(rng);
     159            psFree(stats);
     160            return true;
     161        }
     162
     163        // There are some good pixels in there somewhere; psImageBackground just didn't find them
     164        psLogMsg("psLib.fits", PS_LOG_DETAIL,
     165                 "Couldn't measure background statistics for image quantisation; retrying.");
     166        psErrorClear();
     167        // Retry using all the available pixels
     168        stats->nSubsample = image->numCols * image->numRows + 1;
     169        if (!psImageStats(stats, image, mask, maskVal)) {
     170            psLogMsg("psLib.fits", PS_LOG_DETAIL,
     171                     "Couldn't measure background statistics for image quantisation (attempt 2); retrying.");
     172            psErrorClear();
     173            // Retry with desperate statistic
     174            stats->options = DESPERATE_MEAN_STAT | DESPERATE_STDEV_STAT;
     175            if (!psImageStats(stats, image, mask, maskVal)) {
     176                psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics for image");
     177                psFree(rng);
     178                psFree(stats);
     179                return false;
     180            } else {
     181                // Desperate retry
     182                mean = psStatsGetValue(stats, DESPERATE_MEAN_STAT);
     183                stdev = psStatsGetValue(stats, DESPERATE_STDEV_STAT);
     184            }
     185        } else {
     186            // Retry with all available pixels
     187            mean = psStatsGetValue(stats, MEAN_STAT);
     188            stdev = psStatsGetValue(stats, STDEV_STAT);
     189        }
     190    } else {
     191        // First attempt
     192        mean = psStatsGetValue(stats, MEAN_STAT);
     193        stdev = psStatsGetValue(stats, STDEV_STAT);
    125194    }
    126195    psFree(rng);
    127 
    128     double mean = psStatsGetValue(stats, MEAN_STAT); // Mean
    129     double stdev = psStatsGetValue(stats, STDEV_STAT); // Standard deviation
    130196    psFree(stats);
    131197    if (!isfinite(mean) || !isfinite(stdev)) {
     
    318384                } else { \
    319385                    value = (value - zero) * scale; \
    320                     if (options->fuzz) { \
     386                    if (options->fuzz && (value - (int)value != 0.0)) { \
    321387                       /* Add random factor [-0.5,0.5): adds a variance of 1/12, */ \
    322388                       /* but preserves the expectation value */ \
Note: See TracChangeset for help on using the changeset viewer.