Index: trunk/psModules/src/pmSubtractSky.c
===================================================================
--- trunk/psModules/src/pmSubtractSky.c	(revision 2848)
+++ trunk/psModules/src/pmSubtractSky.c	(revision 2857)
@@ -6,6 +6,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-12-29 22:44:33 $
+ *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-12-30 22:22:00 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -106,7 +106,11 @@
                 }
             }
-            myStats = psVectorStats(myStats, binVector, NULL, binMask, 1);
+            psStats *rc1 = psVectorStats(myStats, binVector, NULL, binMask, 1);
+            if (rc1 == NULL) {
+                psError(PS_ERR_UNKNOWN, false, "psVectorStats(): could not perform requested statistical operation.  Returning in image.\n");
+                return(origImage);
+            }
             psF64 statValue;
-            psBool rc = p_psGetStatValue(myStats, &statValue);
+            psBool rc = p_psGetStatValue(rc1, &statValue);
 
             if (rc == true) {
@@ -427,5 +431,5 @@
             (fit != PM_FIT_POLYNOMIAL) &&
             (fit != PM_FIT_SPLINE)) {
-        psError(PS_ERR_UNKNOWN, true, "psFit is unallowable (%d)\n", fit);
+        psError(PS_ERR_UNKNOWN, true, "psFit is unallowable (%d).  Returning in image.\n", fit);
         return(in);
     }
@@ -436,4 +440,5 @@
     if ((fitSpec == NULL) ||
             ((fit == PM_FIT_NONE) || (fit == PM_FIT_SPLINE))) {
+        psLogMsg(__func__, PS_LOG_WARN, "Fit specs are poorly defined.  Returning in image.\n");
         return(in);
     }
@@ -452,5 +457,8 @@
             psLogMsg(__func__, PS_LOG_WARN, "WARNING: Multiple statistical options have been requested.\n");
             statOptions = getHighestPriorityStatOption(statOptions);
-
+            if (statOptions == -1) {
+                psError(PS_ERR_UNKNOWN, true, "Not allowable stats->option was specified.  Returning in image.\n");
+                return(in);
+            }
             // Save old input "stats" parameter.
             oldStatOptions = stats->options;
@@ -483,6 +491,16 @@
         // and a mask.
         binnedImage = psImageCopy(binnedImage, origImage, PS_TYPE_F32);
+        if (binnedImage == NULL) {
+            psError(PS_ERR_UNKNOWN, false, "psImageCopy() returned NULL.  Returning in image.\n");
+            return(in);
+        }
+
         if (in->mask != NULL) {
             binnedMaskImage = psImageCopy(binnedMaskImage, in->mask, PS_TYPE_U8);
+            if (binnedMaskImage == NULL) {
+                psError(PS_ERR_UNKNOWN, false, "psImageCopy() returned NULL.  Returning in image.\n");
+                psFree(binnedImage);
+                return(in);
+            }
         } else {
             binnedMaskImage = psImageAlloc(binnedImage->numCols,
@@ -493,4 +511,8 @@
     } else {
         binnedImage = psImageRebin(NULL, origImage, in->mask, 0, binFactor, stats);
+        if (binnedImage == NULL) {
+            psError(PS_ERR_UNKNOWN, false, "psImageRebin() returned NULL.  Returning in image.\n");
+            return(in);
+        }
         binnedMaskImage = psImageAlloc(binnedImage->numCols,
                                        binnedImage->numRows,
@@ -512,12 +534,26 @@
         psF64 binnedStdev;
         psStats *myStats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
-        myStats =  psImageStats(myStats, binnedImage, NULL, 0);
-        p_psGetStatValue(myStats, &binnedMean);
+        psStats *rc =  psImageStats(myStats, binnedImage, NULL, 0);
+        if (rc == NULL) {
+            psError(PS_ERR_UNKNOWN, false, "psImageStats(): could not perform requested statistical operation.  Returning in image.\n");
+            return(in);
+        }
+        if (false == p_psGetStatValue(rc, &binnedMean)) {
+            psError(PS_ERR_UNKNOWN, false, "p_psGetStatValue(): could not determine requested statistical operation.  Returning in image.\n");
+            return(in);
+        }
         psTrace(".psModule.pmSubtractSky", 6,
                 "binned Mean is %f\n", binnedMean);
 
         myStats->options = PS_STAT_SAMPLE_STDEV;
-        myStats =  psImageStats(myStats, binnedImage, NULL, 0);
-        p_psGetStatValue(myStats, &binnedStdev);
+        rc =  psImageStats(myStats, binnedImage, NULL, 0);
+        if (rc == NULL) {
+            psError(PS_ERR_UNKNOWN, false, "psImageStats(): could not perform requested statistical operation.  Returning in image.\n");
+            return(in);
+        }
+        if (false == p_psGetStatValue(myStats, &binnedStdev)) {
+            psError(PS_ERR_UNKNOWN, false, "p_psGetStatValue(): could not determine requested statistical operation.  Returning in image.\n");
+            return(in);
+        }
         psFree(myStats);
         psTrace(".psModule.pmSubtractSky", 6,
@@ -551,4 +587,15 @@
             // Set the pixels in the binned image to that of the polynomial.
             binnedImage = psImageEvalPolynomial(binnedImage, myPoly);
+            if (binnedImage == NULL) {
+                psError(PS_ERR_UNKNOWN, false, "psImageEvalPolynomial() returned NULL.  Returning in image.\n");
+                psFree(binnedMaskImage);
+                if (!((binFactor <= 1) || (stats == NULL))) {
+                    psFree(binnedImage);
+                }
+                if (oldStatOptions != 0) {
+                    stats->options = statOptions;
+                }
+                return(in);
+            }
         } else {
             psLogMsg(__func__, PS_LOG_WARN,
@@ -565,5 +612,13 @@
     } else {
         // We shouldn't get here since we check this above.
-        psError(PS_ERR_UNKNOWN, true, "Unallowable fit type.");
+        psError(PS_ERR_UNKNOWN, true, "Unallowable fit type.  Returning in image.\n");
+        psFree(binnedMaskImage);
+        if (!((binFactor <= 1) || (stats == NULL))) {
+            psFree(binnedImage);
+        }
+        if (oldStatOptions != 0) {
+            stats->options = statOptions;
+        }
+        return(in);
     }
 
