Index: trunk/psLib/src/fits/psFitsScale.c
===================================================================
--- trunk/psLib/src/fits/psFitsScale.c	(revision 27069)
+++ trunk/psLib/src/fits/psFitsScale.c	(revision 27097)
@@ -15,6 +15,7 @@
 #include "psImage.h"
 #include "psFits.h"
+#include "psStats.h"
+#include "psImageStats.h"
 #include "psImageBackground.h"
-#include "psStats.h"
 #include "psImageStructManip.h"
 
@@ -29,4 +30,7 @@
 #define MEAN_STAT PS_STAT_ROBUST_MEDIAN // Statistic to use for mean
 #define STDEV_STAT PS_STAT_ROBUST_STDEV // Statistic to use for stdev
+
+#define DESPERATE_MEAN_STAT PS_STAT_SAMPLE_MEDIAN // Statistic to use for mean when deperate
+#define DESPERATE_STDEV_STAT PS_STAT_SAMPLE_QUARTILE // Statistic to use for stdev when desperate
 
 
@@ -118,14 +122,52 @@
     psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
     psStats *stats = psStatsAlloc(MEAN_STAT | STDEV_STAT); // Statistics object
-    if (!psImageBackground(stats, NULL, image, mask, maskVal, rng)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to perform statistics on image");
-        psFree(rng);
-        psFree(stats);
-        return false;
-    }
+    psVector *sample = NULL;                               // Sample of pixels
+    double mean, stdev;                                    // Mean and standard deviation
+    if (!psImageBackground(stats, &sample, image, mask, maskVal, rng)) {
+        // It could be because the image is entirely masked, in which case we don't want to error
+        if (sample->n == 0) {
+            psLogMsg("psLib.fits", PS_LOG_DETAIL, "Image has no good pixels, setting BSCALE = 1, BZERO = 0");
+            psErrorClear();
+            *bscale = 1.0;
+            *bzero = 0.0;
+            psFree(rng);
+            psFree(stats);
+            psFree(sample);
+            return true;
+        }
+        psLogMsg("psLib.fits", PS_LOG_DETAIL,
+                 "Couldn't measure background statistics for image quantisation; retrying.");
+        psErrorClear();
+        // Retry using all the available pixels
+        stats->nSubsample = image->numCols * image->numRows + 1;
+        if (!psImageStats(stats, image, mask, maskVal)) {
+            psLogMsg("psLib.fits", PS_LOG_DETAIL,
+                     "Couldn't measure background statistics for image quantisation (attempt 2); retrying.");
+            psErrorClear();
+            // Retry with desperate statistic
+            stats->options = DESPERATE_MEAN_STAT | DESPERATE_STDEV_STAT;
+            if (!psImageStats(stats, image, mask, maskVal)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics for image");
+                psFree(rng);
+                psFree(stats);
+                psFree(sample);
+                return false;
+            } else {
+                // Desperate retry
+                mean = psStatsGetValue(stats, DESPERATE_MEAN_STAT);
+                stdev = psStatsGetValue(stats, DESPERATE_STDEV_STAT);
+            }
+        } else {
+            // Retry with all available pixels
+            mean = psStatsGetValue(stats, MEAN_STAT);
+            stdev = psStatsGetValue(stats, STDEV_STAT);
+        }
+    } else {
+        // First attempt
+        mean = psStatsGetValue(stats, MEAN_STAT);
+        stdev = psStatsGetValue(stats, STDEV_STAT);
+    }
+    psFree(sample);
     psFree(rng);
-
-    double mean = psStatsGetValue(stats, MEAN_STAT); // Mean
-    double stdev = psStatsGetValue(stats, STDEV_STAT); // Standard deviation
     psFree(stats);
     if (!isfinite(mean) || !isfinite(stdev)) {
