Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 18155)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 18156)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-05-14 01:27:25 $
+ *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-06-17 02:46:05 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -287,6 +287,7 @@
             if (!psFitsScaleDetermine(bscale, bzero, blank, image, fits)) {
                 // We can't have the write dying for this reason --- try to save it somehow!
-                psWarning("Unable to determine BSCALE and BZERO for image --- setting to 1.0, 0.0");
+                psWarning("Unable to determine BSCALE and BZERO for image --- refusing to quantise.");
                 psErrorClear();
+                return psMemIncrRefCounter((psImage*)image);
             }
         } else {
@@ -521,6 +522,7 @@
     int numRows = image->numRows;       // Number of rows for image
     int status = 0;                     // Status from cfitsio
-
-    double bscale = 0.0, bzero = 0.0;   // Scale and zero point to put in header (*already* applied to data)
+    psFitsCompression *compress = NULL; // FITS compression parameters; to save state
+
+    double bscale = NAN, bzero = NAN;   // Scale and zero point to put in header
     long blank = 0;                     // Blank (undefined) value for image
     psFitsFloat floatType;              // Custom floating-point convention type
@@ -529,5 +531,14 @@
     if (!diskImage) {
         psError(PS_ERR_UNKNOWN, false, "Unable to convert image to desired disk format.");
-        return false;
+        goto INSERT_FAIL;
+    }
+
+    if (!isfinite(bscale) || !isfinite(bzero)) {
+        // Couldn't scale, so don't compress.  Save compression parameters for later
+        compress = psFitsCompressionGet(fits);
+        if (!psFitsSetCompression(fits, PS_FITS_COMPRESS_NONE, NULL, 0, 0, 0)) {
+            psError(PS_ERR_IO, false, "Unable to unset compression.");
+            goto INSERT_FAIL;
+        }
     }
 
@@ -537,6 +548,5 @@
     int dataType;                       // cfitsio data type
     if (!p_psFitsTypeToCfitsio(diskImage->type.type, &bitPix, &cfitsioBzero, &dataType)) {
-        psFree(diskImage);
-        return false;
+        goto INSERT_FAIL;
     }
     if (cfitsioBzero != 0.0) {
@@ -578,10 +588,12 @@
         fits_insert_img(fits->fd, bitPix, naxis, naxes, &status);
     }
+    if (psFitsError(status, true, "Could not create image HDU.")) {
+        goto INSERT_FAIL;
+    }
 
     // write the header, if any.
     if (header && !psFitsWriteHeader(fits, header)) {
         psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
-        psFree(diskImage);
-        return false;
+        goto INSERT_FAIL;
     }
 
@@ -597,6 +609,5 @@
                            "Scaling: TRUE = BZERO + BSCALE * DISK", &status);
         if (psFitsError(status, true, "Could not write BSCALE/BZERO headers to file.")) {
-            psFree(diskImage);
-            return false;
+            goto INSERT_FAIL;
         }
     }
@@ -610,6 +621,5 @@
         fits_set_imgnull(fits->fd, blank, &status);
         if (psFitsError(status, true, "Could not write BLANK header to file.")) {
-            psFree(diskImage);
-            return false;
+            goto INSERT_FAIL;
         }
     }
@@ -634,13 +644,18 @@
         }
     }
-
+    if (psFitsError(status, true, "Could not write image to file.")) {
+        goto INSERT_FAIL;
+    }
+
+    return true;
+
+INSERT_FAIL:
     psFree(diskImage);
-
-    if (psFitsError(status, true, "Could not write image to file.")) {
-        return false;
-    }
-
-    return true;
-
+    if (compress) {
+        // Restore compression state
+        psFitsCompressionApply(fits, compress);
+        psFree(compress);
+    }
+    return false;
 }
 
@@ -666,6 +681,7 @@
     int numCols = input->numCols;
     int numRows = input->numRows;
-
-    double bscale = 0.0, bzero = 0.0;   // Scale and zero point to put in header (*already* applied to data)
+    psFitsCompression *compress = NULL; // FITS compression parameters; to save state
+
+    double bscale = NAN, bzero = NAN;   // Scale and zero point to put in header
     long blank = 0;                     // Blank (undefined) value for image
     psFitsFloat floatType;              // Custom floating-point convention type
@@ -674,5 +690,14 @@
     if (!diskImage) {
         psError(PS_ERR_UNKNOWN, false, "Unable to convert image to desired disk format.");
-        return false;
+        goto UPDATE_FAIL;
+    }
+
+    if (!isfinite(bscale) || !isfinite(bzero)) {
+        // Couldn't scale, so don't compress.  Save compression parameters for later
+        compress = psFitsCompressionGet(fits);
+        if (!psFitsSetCompression(fits, PS_FITS_COMPRESS_NONE, NULL, 0, 0, 0)) {
+            psError(PS_ERR_IO, false, "Unable to unset compression.");
+            goto UPDATE_FAIL;
+        }
     }
 
@@ -682,6 +707,5 @@
     int dataType;                       // cfitsio data type
     if (!p_psFitsTypeToCfitsio(diskImage->type.type, &bitPix, &cfitsioBzero, &dataType)) {
-        psFree(diskImage);
-        return false;
+        goto UPDATE_FAIL;
     }
     if (cfitsioBzero != 0.0) {
@@ -694,5 +718,5 @@
     psAssert(!options || bitPix == options->bitpix || options->bitpix == 0, "impossible");
 
-    //check to see if the HDU has the same datatype
+    // Check to see if the HDU has the same datatype
     int fileBitpix;
     int naxis;
@@ -705,6 +729,5 @@
         psError(PS_ERR_BAD_PARAMETER_SIZE, true,
                 _("Current FITS HDU has %ld z-planes, but z-plane %d was specified."), nAxes[2], z);
-        psFree(diskImage);
-        return false;
+        goto UPDATE_FAIL;
     }
 
@@ -728,6 +751,5 @@
                 "Input image [size of %ix%i] at position (%i,%i) does not all lay in the %lix%li FITS image.",
                 numCols, numRows, x0, y0, nAxes[0], nAxes[1]);
-        psFree(diskImage);
-        return false;
+        goto UPDATE_FAIL;
     }
 
@@ -736,14 +758,19 @@
     // appropriate scale and zero (because we want to apply a randomiser to the quantisation).
     fits_set_bscale(fits->fd, 1.0, cfitsioBzero, &status);
-
     fits_write_subset(fits->fd, dataType, firstPixel, lastPixel, diskImage->data.V[0], &status);
-
+    if (psFitsError(status, true, "Could not write data to file.")) {
+        goto UPDATE_FAIL;
+    }
+
+    return true;
+
+UPDATE_FAIL:
     psFree(diskImage);
-
-    if (psFitsError(status, true, "Could not write data to file.")) {
-        return false;
-    }
-
-    return true;
+    if (compress) {
+        // Restore compression state
+        psFitsCompressionApply(fits, compress);
+        psFree(compress);
+    }
+    return false;
 }
 
Index: trunk/psLib/src/fits/psFitsScale.c
===================================================================
--- trunk/psLib/src/fits/psFitsScale.c	(revision 18155)
+++ trunk/psLib/src/fits/psFitsScale.c	(revision 18156)
@@ -176,6 +176,6 @@
     PS_ASSERT_FITS_NON_NULL(fits, false);
 
-    *bscale = 1.0;
-    *bzero = 0.0;
+    *bscale = NAN;
+    *bzero = NAN;
     *blank = 0;
 
