Index: /branches/czw_branch/20110406/ippconfig/recipes/fitstypes.mdc
===================================================================
--- /branches/czw_branch/20110406/ippconfig/recipes/fitstypes.mdc	(revision 31620)
+++ /branches/czw_branch/20110406/ippconfig/recipes/fitstypes.mdc	(revision 31621)
@@ -101,7 +101,8 @@
 COMP_STACK     METADATA
         BITPIX          S32     16
-        SCALING         STR     ASINH_STDEV_POSITIVE
-        STDEV.BITS      S32     4
-        STDEV.NUM       F32     10
+        SCALING         STR     ASINH_RANGE
+#	 SCALING        STR      LOG_RANGE
+#        STDEV.BITS      S32     4
+#        STDEV.NUM       F32     100
 	COMPRESSION     STR     RICE
 	TILE.X		S32	0
Index: /branches/czw_branch/20110406/psLib/src/fits/psFitsImage.c
===================================================================
--- /branches/czw_branch/20110406/psLib/src/fits/psFitsImage.c	(revision 31620)
+++ /branches/czw_branch/20110406/psLib/src/fits/psFitsImage.c	(revision 31621)
@@ -164,4 +164,5 @@
     else if (isfinite(bsoften)) {
       info->is_asinh = true;
+      info->is_logscaled = false;
     }
     else {
@@ -510,9 +511,10 @@
     }
     else if (info->is_asinh) {
-      double bsoften;
+      double bsoften,boffset;
       int status;
       status = 0;
       fits_read_key_dbl(fits->fd, "BSOFTEN", &bsoften, NULL, &status);
-      psImage *newImage = psFitsScaleFromDisk(outImage,0.0,bsoften);
+      fits_read_key_dbl(fits->fd, "BOFFSET", &boffset, NULL, &status);
+      psImage *newImage = psFitsScaleFromDisk(outImage,boffset,bsoften);
       psFree (outImage);
       outImage = newImage;
@@ -724,9 +726,24 @@
     if (options&&(!((options->scaling == PS_FITS_SCALE_LOG_RANGE)||
 		    (options->scaling == PS_FITS_SCALE_LOG_MANUAL)||
-		   (options->scaling == PS_FITS_SCALE_LOG_STDEV_POSITIVE)||
-		   (options->scaling == PS_FITS_SCALE_LOG_STDEV_NEGATIVE)||
-		   (options->scaling == PS_FITS_SCALE_LOG_STDEV_BOTH)))) {
+		    (options->scaling == PS_FITS_SCALE_LOG_STDEV_POSITIVE)||
+		    (options->scaling == PS_FITS_SCALE_LOG_STDEV_NEGATIVE)||
+		    (options->scaling == PS_FITS_SCALE_LOG_STDEV_BOTH)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_RANGE)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_MANUAL)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_STDEV_POSITIVE)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_STDEV_NEGATIVE)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_STDEV_BOTH)))) {
       if (psMetadataLookup(header,"BOFFSET")) {
 	psMetadataRemoveKey(header,"BOFFSET");
+      }
+    }	
+    // Remove any BSOFTENvalues that exist in the header if we are not using that scaling anymore
+    if (options&&(!((options->scaling == PS_FITS_SCALE_ASINH_RANGE)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_MANUAL)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_STDEV_POSITIVE)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_STDEV_NEGATIVE)||
+		    (options->scaling == PS_FITS_SCALE_ASINH_STDEV_BOTH)))) {
+      if (psMetadataLookup(header,"BSOFTEN")) {
+	psMetadataRemoveKey(header,"BSOFTEN");
       }
     }	
@@ -757,4 +774,17 @@
 			     "Scaling: TRUE = BZERO + BSCALE * 10**(DISK) + BOFFSET)", &status);
 	}	
+	if (options&&(((options->scaling == PS_FITS_SCALE_ASINH_RANGE)||
+		       (options->scaling == PS_FITS_SCALE_ASINH_MANUAL)||
+		       (options->scaling == PS_FITS_SCALE_ASINH_STDEV_POSITIVE)||
+		       (options->scaling == PS_FITS_SCALE_ASINH_STDEV_NEGATIVE)||
+		       (options->scaling == PS_FITS_SCALE_ASINH_STDEV_BOTH)))) {
+	  fits_write_key_dbl(fits->fd, "BOFFSET", boffset, 12,
+			     "Scaling: NORM = DISK + BOFFSET", &status);
+	  fits_write_key_dbl(fits->fd, "BSOFTEN", bsoften, 12,
+			     "Scaling: TRUE = 2 * BSOFTEN * sinh(NORM/a)",&status);
+
+
+	}	
+
         if (psFitsError(status, true, "Could not write BSCALE/BZERO headers to file.")) {
             success = false;
Index: /branches/czw_branch/20110406/psLib/src/fits/psFitsScale.c
===================================================================
--- /branches/czw_branch/20110406/psLib/src/fits/psFitsScale.c	(revision 31620)
+++ /branches/czw_branch/20110406/psLib/src/fits/psFitsScale.c	(revision 31621)
@@ -214,4 +214,5 @@
       case PS_FITS_SCALE_STDEV_POSITIVE:
       case PS_FITS_SCALE_LOG_STDEV_POSITIVE:
+      case PS_FITS_SCALE_ASINH_STDEV_POSITIVE:
         // Put (mean - N sigma) at the lowest possible value: predominantly positive images
         imageVal = mean - options->stdevNum * stdev;
@@ -220,4 +221,5 @@
       case PS_FITS_SCALE_STDEV_NEGATIVE:
       case PS_FITS_SCALE_LOG_STDEV_NEGATIVE:
+      case PS_FITS_SCALE_ASINH_STDEV_NEGATIVE:
         // Put (mean + N sigma) at the highest possible value: predominantly negative images
         imageVal = mean + options->stdevNum * stdev;
@@ -226,4 +228,5 @@
       case PS_FITS_SCALE_STDEV_BOTH:
       case PS_FITS_SCALE_LOG_STDEV_BOTH:
+      case PS_FITS_SCALE_ASINH_STDEV_BOTH:
         // Put mean right in the middle: images with an equal abundance of positive and negative values
         imageVal = mean;
@@ -460,4 +463,5 @@
 static bool asinhStdev(double *bscale, // Scaling, to return
 		       double *bzero,  // Zero point, to return
+		       double *boffset, // asinh flux zeropoint, to return
 		       double *bsoften, // asinh softening parameter, to return
 		       const psImage *image, // Image to scale
@@ -469,4 +473,5 @@
   psAssert(bscale, "impossible");
   psAssert(bzero, "impossible");
+  psAssert(boffset, "impossible");
   psAssert(bsoften, "impossible");
   psAssert(image, "impossible");
@@ -578,4 +583,5 @@
     float a = 1.0857362; // 2.5 * log(e);
     *bsoften = sqrt(a) * stdev;
+    *boffset = mean;
     //    float m0 = 0; // Can I just arbitrarily set this?
     
@@ -584,8 +590,14 @@
       for (int y = 0; y < numRows; y++) {
 	for (int x = 0; x < numCols; x++) {
-/* 	  if (x == 2331 && y == 2843) { */
-/* 	    fprintf(stderr,"psFS32: %d %d %g %g %g\n",x,y,offset,image->data.F32[y][x],log10(image->data.F32[y][x] - offset)); */
+/* 	  if (x == 266 && y == 4584) { */
+/* 	    fprintf(stderr,"psFS32: %d %d %g %g %g %g %g\n",x,y,*boffset,*bsoften,image->data.F32[y][x],log10(image->data.F32[y][x] - *boffset), */
+/* 		    a * asinh((image->data.F32[y][x] - *boffset) / (2 * *bsoften))); */
 /* 	  } */
-	  copy->data.F32[y][x] = -1.0 * a * asinh( image->data.F32[y][x] / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	  if (isfinite(image->data.F32[y][x])) {
+	    copy->data.F32[y][x] = a * asinh( (image->data.F32[y][x] - *boffset) / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	  }
+	  else {
+	    copy->data.F32[y][x] = image->data.F32[y][x];
+	  }
 	}
       }
@@ -595,5 +607,10 @@
 	  for (int x = 0; x < numCols; x++) {
 	    //	    fprintf(stderr,"psFS64: %d %d %g %g %g\n",x,y,offset,image->data.F64[y][x],log10(image->data.F64[y][x] - offset));
-	    copy->data.F32[y][x] = -1.0 * a * asinh( image->data.F32[y][x] / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	    if (isfinite(image->data.F64[y][x])) {
+	      copy->data.F64[y][x] = a * asinh( (image->data.F64[y][x] - *boffset) / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	    }
+	    else {
+	      copy->data.F64[y][x] = image->data.F64[y][x];
+	    }	  
 	  }
 	}
@@ -616,4 +633,5 @@
 static bool asinhRange(double *bscale, // Scaling, to return
 		       double *bzero,  // Zero point, to return
+		       double *boffset, // asinh flux zeropoint, to return
 		       double *bsoften, // asinh softening parameter, to return
 		       const psImage *image, // Image to scale
@@ -734,4 +752,5 @@
     float a = 1.0857362; // 2.5 * log(e);
     *bsoften = sqrt(a) * stdev;
+    *boffset = mean;
     // float m0 = 0; // Can I just arbitrarily set this?
     
@@ -743,5 +762,15 @@
 /* 	    fprintf(stderr,"psFS32: %d %d %g %g %g\n",x,y,offset,image->data.F32[y][x],log10(image->data.F32[y][x] - offset)); */
 /* 	  } */
-	  copy->data.F32[y][x] = -1.0 * a * asinh( image->data.F32[y][x] / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+/* 	  if (x == 266 && y == 4584) { */
+/* 	    fprintf(stderr,"psFS32: %d %d %g %g %g %g %g\n",x,y,*boffset,*bsoften,image->data.F32[y][x],log10(image->data.F32[y][x] - *boffset), */
+/* 		    a * asinh((image->data.F32[y][x] - *boffset) / (2 * *bsoften))); */
+/* 	  } */
+
+	  if (isfinite(image->data.F32[y][x])) {
+	    copy->data.F32[y][x] = a * asinh( (image->data.F32[y][x] - *boffset) / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	  }
+	  else {
+	    copy->data.F32[y][x] = image->data.F32[y][x];
+	  }
 	}
       }
@@ -751,5 +780,10 @@
 	  for (int x = 0; x < numCols; x++) {
 	    //	    fprintf(stderr,"psFS64: %d %d %g %g %g\n",x,y,offset,image->data.F64[y][x],log10(image->data.F64[y][x] - offset));
-	    copy->data.F32[y][x] = -1.0 * a * asinh( image->data.F32[y][x] / (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	    if (isfinite(image->data.F64[y][x])) {
+	      copy->data.F64[y][x] = a * asinh( (image->data.F64[y][x] - *boffset)/ (2 * *bsoften));// - 2.5 * log10(b) + m0;
+	    }
+	    else {
+	      copy->data.F64[y][x] = image->data.F32[y][x];
+	    }
 	  }
 	}
@@ -761,5 +795,5 @@
     }
       
-    // Do regular scaling on the logarithm image
+    // Do regular scaling on the asinh image
     if (!scaleRange(bscale, bzero, copy, options)) {
       psError(PS_ERR_UNKNOWN, false, "Unable to set BSCALE and BZERO from stdev");
@@ -851,5 +885,5 @@
         break;
       case PS_FITS_SCALE_ASINH_RANGE:
-	if (!asinhRange(bscale,bzero,bsoften,image,mask,maskVal,options)) {
+	if (!asinhRange(bscale,bzero,boffset,bsoften,image,mask,maskVal,options)) {
 	  psError(PS_ERR_UNKNOWN, false, "Unable to set BSCALE and BZERO from range");
 	  return false;
@@ -859,5 +893,5 @@
       case PS_FITS_SCALE_ASINH_STDEV_NEGATIVE:
       case PS_FITS_SCALE_ASINH_STDEV_BOTH:
-	if (!asinhStdev(bscale, bzero,bsoften, image, mask, maskVal, options)) {
+	if (!asinhStdev(bscale, bzero,boffset,bsoften, image, mask, maskVal, options)) {
 	  psError(PS_ERR_UNKNOWN, false, "Unable to set BSCALE and BZERO from stdev");
 	  return false;
@@ -888,6 +922,6 @@
 
 
-    psTrace("psLib.fits", 3, "BSCALE = %.10lf, BZERO = %.10lf, BOFFSET = %.10lf, BLANK = %ld\n",
-	    *bscale, *bzero, *boffset, *blank);
+    psTrace("psLib.fits", 3, "BSCALE = %.10lf, BZERO = %.10lf, BOFFSET = %.10lf, BSOFTEN = %.10lf, BLANK = %ld\n",
+	    *bscale, *bzero, *boffset, *bsoften, *blank);
     return true;
 }
@@ -956,5 +990,10 @@
 		  (options->scaling == PS_FITS_SCALE_LOG_STDEV_NEGATIVE)|| \
 		  (options->scaling == PS_FITS_SCALE_LOG_STDEV_BOTH)) {	\
-  		value = log10( (IN)->data.INTYPE[y][x] - boffset ); \
+		if (isfinite((IN)->data.INTYPE[y][x])) {		\
+		  value = log10( (IN)->data.INTYPE[y][x] - boffset );	\
+		}							\
+		else {							\
+		  value = (IN)->data.INTYPE[y][x];			\
+		}							\
 	      }								\
 	      else if ((options->scaling == PS_FITS_SCALE_ASINH_RANGE)||	\
@@ -963,5 +1002,14 @@
 		  (options->scaling == PS_FITS_SCALE_ASINH_STDEV_NEGATIVE)|| \
 		  (options->scaling == PS_FITS_SCALE_ASINH_STDEV_BOTH)) {	\
-  		value = -1.0 * 1.0857362 * (asinh( (IN)->data.INTYPE[y][x] - bsoften)); \
+		if (isfinite((IN)->data.INTYPE[y][x])) {		\
+		  value = 1.0857362 * (asinh( ((IN)->data.INTYPE[y][x] - boffset) / (2.0 * bsoften))); \
+		}							\
+		else {							\
+		  value = (IN)->data.INTYPE[y][x];			\
+		}							\
+		if ((x == 1000)&&(y == 1000)) {				\
+		  fprintf(stderr,"ASINH: %f %f %f",			\
+			  (IN)->data.INTYPE[y][x],value,bsoften);	\
+		}							\
 	      }								\
 		else { \
@@ -1056,10 +1104,10 @@
       for (int y = 0; y < numRows; y++) { \
           for (int x = 0; x < numCols; x++) { \
-	    if (boffset) { \
+	    if (bsoften) {						\
+	      out->data.OUTTYPE[y][x] = 2 * bsoften * sinh(image->data.INTYPE[y][x] / (1.0857362)) + boffset; \
+            }								\
+	    else if (boffset) {					\
 	      out->data.OUTTYPE[y][x] = pow(10,image->data.INTYPE[y][x]) + boffset;; \
              }									\
-	    else if (bsoften) { \
-	      out->data.OUTTYPE[y][x] = sinh(image->data.INTYPE[y][x] * 2 * bsoften) / (-1.0 * 1.0857362);; \
-            } \
           } \
       } \
