Index: /trunk/psLib/src/dataManip/psFunctions.c
===================================================================
--- /trunk/psLib/src/dataManip/psFunctions.c	(revision 2405)
+++ /trunk/psLib/src/dataManip/psFunctions.c	(revision 2406)
@@ -7,6 +7,6 @@
  *  polynomials.  It also contains a Gaussian functions.
  *
- *  @version $Revision: 1.65 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-15 21:13:17 $
+ *  @version $Revision: 1.66 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -14,4 +14,6 @@
  *  XXX: What happens if the polyEVal functions are called with data of the wrong
  *       type?
+ *  XXX: Should the "coeffErr[]" be used as well?  Bug ???.  Ignore coeffErr
+ *
  */
 /*****************************************************************************/
@@ -274,5 +276,4 @@
     Polynomial coefficients will be accessed in [w][x][y][z] fashion.
  
-    XXX: Should the "coeffErr[]" be used as well?
  *****************************************************************************/
 static float ordPolynomial1DEval(float x, const psPolynomial1D* myPoly)
Index: /trunk/psLib/src/dataManip/psMinimize.c
===================================================================
--- /trunk/psLib/src/dataManip/psMinimize.c	(revision 2405)
+++ /trunk/psLib/src/dataManip/psMinimize.c	(revision 2406)
@@ -9,6 +9,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.88 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-12 20:45:27 $
+ *  @version $Revision: 1.89 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -416,9 +416,9 @@
                                   const psArray *coords)
 {
+    PS_PTR_CHECK_NULL(coords, NULL);
+    PS_PTR_CHECK_NULL(params, NULL);
+
     psTrace(".psLib.dataManip.psMinimize", 4,
             "---- psMinimizeLMChi2Gauss1D() begin ----\n");
-
-    PS_PTR_CHECK_NULL(coords, NULL);
-    PS_PTR_CHECK_NULL(params, NULL);
     float x;
     int i;
@@ -432,4 +432,6 @@
     if (deriv == NULL) {
         deriv = psImageAlloc(params->n, coords->n, PS_TYPE_F32);
+    } else {
+        // XXX: Check size of derivative
     }
 
@@ -453,4 +455,54 @@
 }
 
+
+psVector *psMinimizeLMChi2Gauss2D(psImage *deriv,
+                                  const psVector *params,
+                                  const psArray *coords)
+{
+    PS_PTR_CHECK_NULL(coords, NULL);
+    PS_PTR_CHECK_NULL(params, NULL);
+
+    double normalization = params->data.F32[0];
+    double x0 = params->data.F32[1];
+    double y0 = params->data.F32[2];
+    double sigmaX = params->data.F32[3];
+    double sigmaY = params->data.F32[4];
+    double theta = params->data.F32[5];
+    psVector *out = psVectorAlloc(coords->n, PS_TYPE_F32);
+
+    if (deriv == NULL) {
+        deriv = psImageAlloc(params->n, coords->n, PS_TYPE_F32);
+    } else {
+        // XXX: Check size of derivative
+    }
+
+    psTrace(".psLib.dataManip.psMinimize", 4,
+            "---- psMinimizeLMChi2Gauss2D() begin ----\n");
+
+    for (int i=0;i<coords->n;i++) {
+        double x = ((psVector *) coords->data[i])->data.F32[0];
+        double y = ((psVector *) coords->data[i])->data.F32[0];
+
+        double u = - (x-x0)*cos(theta) + (y-y0)*sin(theta);
+        double v = (x-x0)*cos(theta) + (y-y0)*sin(theta);
+
+        double flux = normalization * exp(-( u*u/(2.0 * sigmaX * sigmaX) +
+                                             v*v/(2.0 * sigmaY * sigmaY)))/
+                      (2.0 * M_PI * sigmaX * sigmaY);
+        out->data.F32[i] = flux;
+
+        // XXX: Calculate these correctly.
+        deriv->data.F32[i][0] = 0.0;
+        deriv->data.F32[i][1] = 0.0;
+        deriv->data.F32[i][2] = 0.0;
+        deriv->data.F32[i][3] = 0.0;
+        deriv->data.F32[i][4] = 0.0;
+        deriv->data.F32[i][5] = 0.0;
+    }
+
+    psTrace(".psLib.dataManip.psMinimize", 4,
+            "---- psMinimizeLMChi2Gauss2D() end ----\n");
+    return(out);
+}
 
 /******************************************************************************
Index: /trunk/psLib/src/dataManip/psStats.c
===================================================================
--- /trunk/psLib/src/dataManip/psStats.c	(revision 2405)
+++ /trunk/psLib/src/dataManip/psStats.c	(revision 2406)
@@ -9,6 +9,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.97 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-15 21:13:17 $
+ *  @version $Revision: 1.98 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -146,8 +146,8 @@
 this routine sets stats->sampleMean to NAN.
  *****************************************************************************/
-void p_psVectorSampleMean(const psVector* restrict myVector,
-                          const psVector* restrict maskVector,
-                          psU32 maskVal,
-                          psStats* stats)
+int p_psVectorSampleMean(const psVector* restrict myVector,
+                         const psVector* restrict maskVector,
+                         psU32 maskVal,
+                         psStats* stats)
 {
     psS32 i = 0;                // Loop index variable
@@ -210,4 +210,10 @@
 
     stats->sampleMean = mean;
+    if (isnan(mean)) {
+        return(0);
+    } else {
+        return(-1);
+    }
+
 }
 
@@ -217,8 +223,8 @@
 this routine sets stats->max to NAN.
  *****************************************************************************/
-void p_psVectorMax(const psVector* restrict myVector,
-                   const psVector* restrict maskVector,
-                   psU32 maskVal,
-                   psStats* stats)
+int p_psVectorMax(const psVector* restrict myVector,
+                  const psVector* restrict maskVector,
+                  psU32 maskVal,
+                  psStats* stats)
 {
     psS32 i = 0;                // Loop index variable
@@ -272,5 +278,7 @@
     } else {
         stats->max = NAN;
-    }
+        return(1);
+    }
+    return(0);
 }
 
@@ -280,8 +288,8 @@
 this routine sets stats->min to NAN.
  *****************************************************************************/
-void p_psVectorMin(const psVector* restrict myVector,
-                   const psVector* restrict maskVector,
-                   psU32 maskVal,
-                   psStats* stats)
+int p_psVectorMin(const psVector* restrict myVector,
+                  const psVector* restrict maskVector,
+                  psU32 maskVal,
+                  psStats* stats)
 {
     psS32 i = 0;                // Loop index variable
@@ -335,5 +343,7 @@
     } else {
         stats->min = NAN;
-    }
+        return(1);
+    }
+    return(0);
 }
 
@@ -1205,8 +1215,14 @@
     // Determine minimum and maximum values in the data vector.
     if (isnan(stats->min)) {
-        p_psVectorMin(myVector, maskVector, maskVal, stats);
-    }
+        if (0 != p_psVectorMin(myVector, maskVector, maskVal, stats)) {
+            psLogMsg(__func__, PS_LOG_WARN,
+                     "WARNING: p_psVectorMin(): p_psVectorMin() reported a NAN mean.\n");
+            return(1);
+        }
+    }
+
+
     if (isnan(stats->max)) {
-        p_psVectorMax(myVector, maskVector, maskVal, stats);
+        if (0 != p_psVectorMax(myVector, maskVector, maskVal, stats)) {}
     }
 
@@ -1771,9 +1787,19 @@
     // ************************************************************************
     if (stats->options & PS_STAT_MAX) {
-        p_psVectorMax(inF32, mask, maskVal, stats);
+        if (0 != p_psVectorMax(inF32, mask, maskVal, stats)) {
+            psError(PS_ERR_UNKNOWN, false,
+                    "Failed to calculate vector maximum");
+            psFree(stats);
+            return(NULL);
+        }
     }
     // ************************************************************************
     if (stats->options & PS_STAT_MIN) {
-        p_psVectorMin(inF32, mask, maskVal, stats);
+        if (0 != p_psVectorMin(inF32, mask, maskVal, stats)) {
+            psError(PS_ERR_UNKNOWN, false,
+                    "Failed to calculate vector minimum");
+            psFree(stats);
+            return(NULL);
+        }
     }
 
Index: /trunk/psLib/src/math/psMinimize.c
===================================================================
--- /trunk/psLib/src/math/psMinimize.c	(revision 2405)
+++ /trunk/psLib/src/math/psMinimize.c	(revision 2406)
@@ -9,6 +9,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.88 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-12 20:45:27 $
+ *  @version $Revision: 1.89 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -416,9 +416,9 @@
                                   const psArray *coords)
 {
+    PS_PTR_CHECK_NULL(coords, NULL);
+    PS_PTR_CHECK_NULL(params, NULL);
+
     psTrace(".psLib.dataManip.psMinimize", 4,
             "---- psMinimizeLMChi2Gauss1D() begin ----\n");
-
-    PS_PTR_CHECK_NULL(coords, NULL);
-    PS_PTR_CHECK_NULL(params, NULL);
     float x;
     int i;
@@ -432,4 +432,6 @@
     if (deriv == NULL) {
         deriv = psImageAlloc(params->n, coords->n, PS_TYPE_F32);
+    } else {
+        // XXX: Check size of derivative
     }
 
@@ -453,4 +455,54 @@
 }
 
+
+psVector *psMinimizeLMChi2Gauss2D(psImage *deriv,
+                                  const psVector *params,
+                                  const psArray *coords)
+{
+    PS_PTR_CHECK_NULL(coords, NULL);
+    PS_PTR_CHECK_NULL(params, NULL);
+
+    double normalization = params->data.F32[0];
+    double x0 = params->data.F32[1];
+    double y0 = params->data.F32[2];
+    double sigmaX = params->data.F32[3];
+    double sigmaY = params->data.F32[4];
+    double theta = params->data.F32[5];
+    psVector *out = psVectorAlloc(coords->n, PS_TYPE_F32);
+
+    if (deriv == NULL) {
+        deriv = psImageAlloc(params->n, coords->n, PS_TYPE_F32);
+    } else {
+        // XXX: Check size of derivative
+    }
+
+    psTrace(".psLib.dataManip.psMinimize", 4,
+            "---- psMinimizeLMChi2Gauss2D() begin ----\n");
+
+    for (int i=0;i<coords->n;i++) {
+        double x = ((psVector *) coords->data[i])->data.F32[0];
+        double y = ((psVector *) coords->data[i])->data.F32[0];
+
+        double u = - (x-x0)*cos(theta) + (y-y0)*sin(theta);
+        double v = (x-x0)*cos(theta) + (y-y0)*sin(theta);
+
+        double flux = normalization * exp(-( u*u/(2.0 * sigmaX * sigmaX) +
+                                             v*v/(2.0 * sigmaY * sigmaY)))/
+                      (2.0 * M_PI * sigmaX * sigmaY);
+        out->data.F32[i] = flux;
+
+        // XXX: Calculate these correctly.
+        deriv->data.F32[i][0] = 0.0;
+        deriv->data.F32[i][1] = 0.0;
+        deriv->data.F32[i][2] = 0.0;
+        deriv->data.F32[i][3] = 0.0;
+        deriv->data.F32[i][4] = 0.0;
+        deriv->data.F32[i][5] = 0.0;
+    }
+
+    psTrace(".psLib.dataManip.psMinimize", 4,
+            "---- psMinimizeLMChi2Gauss2D() end ----\n");
+    return(out);
+}
 
 /******************************************************************************
Index: /trunk/psLib/src/math/psPolynomial.c
===================================================================
--- /trunk/psLib/src/math/psPolynomial.c	(revision 2405)
+++ /trunk/psLib/src/math/psPolynomial.c	(revision 2406)
@@ -7,6 +7,6 @@
  *  polynomials.  It also contains a Gaussian functions.
  *
- *  @version $Revision: 1.65 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-15 21:13:17 $
+ *  @version $Revision: 1.66 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -14,4 +14,6 @@
  *  XXX: What happens if the polyEVal functions are called with data of the wrong
  *       type?
+ *  XXX: Should the "coeffErr[]" be used as well?  Bug ???.  Ignore coeffErr
+ *
  */
 /*****************************************************************************/
@@ -274,5 +276,4 @@
     Polynomial coefficients will be accessed in [w][x][y][z] fashion.
  
-    XXX: Should the "coeffErr[]" be used as well?
  *****************************************************************************/
 static float ordPolynomial1DEval(float x, const psPolynomial1D* myPoly)
Index: /trunk/psLib/src/math/psSpline.c
===================================================================
--- /trunk/psLib/src/math/psSpline.c	(revision 2405)
+++ /trunk/psLib/src/math/psSpline.c	(revision 2406)
@@ -7,6 +7,6 @@
  *  polynomials.  It also contains a Gaussian functions.
  *
- *  @version $Revision: 1.65 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-15 21:13:17 $
+ *  @version $Revision: 1.66 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -14,4 +14,6 @@
  *  XXX: What happens if the polyEVal functions are called with data of the wrong
  *       type?
+ *  XXX: Should the "coeffErr[]" be used as well?  Bug ???.  Ignore coeffErr
+ *
  */
 /*****************************************************************************/
@@ -274,5 +276,4 @@
     Polynomial coefficients will be accessed in [w][x][y][z] fashion.
  
-    XXX: Should the "coeffErr[]" be used as well?
  *****************************************************************************/
 static float ordPolynomial1DEval(float x, const psPolynomial1D* myPoly)
Index: /trunk/psLib/src/math/psStats.c
===================================================================
--- /trunk/psLib/src/math/psStats.c	(revision 2405)
+++ /trunk/psLib/src/math/psStats.c	(revision 2406)
@@ -9,6 +9,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.97 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-15 21:13:17 $
+ *  @version $Revision: 1.98 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-23 19:35:30 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -146,8 +146,8 @@
 this routine sets stats->sampleMean to NAN.
  *****************************************************************************/
-void p_psVectorSampleMean(const psVector* restrict myVector,
-                          const psVector* restrict maskVector,
-                          psU32 maskVal,
-                          psStats* stats)
+int p_psVectorSampleMean(const psVector* restrict myVector,
+                         const psVector* restrict maskVector,
+                         psU32 maskVal,
+                         psStats* stats)
 {
     psS32 i = 0;                // Loop index variable
@@ -210,4 +210,10 @@
 
     stats->sampleMean = mean;
+    if (isnan(mean)) {
+        return(0);
+    } else {
+        return(-1);
+    }
+
 }
 
@@ -217,8 +223,8 @@
 this routine sets stats->max to NAN.
  *****************************************************************************/
-void p_psVectorMax(const psVector* restrict myVector,
-                   const psVector* restrict maskVector,
-                   psU32 maskVal,
-                   psStats* stats)
+int p_psVectorMax(const psVector* restrict myVector,
+                  const psVector* restrict maskVector,
+                  psU32 maskVal,
+                  psStats* stats)
 {
     psS32 i = 0;                // Loop index variable
@@ -272,5 +278,7 @@
     } else {
         stats->max = NAN;
-    }
+        return(1);
+    }
+    return(0);
 }
 
@@ -280,8 +288,8 @@
 this routine sets stats->min to NAN.
  *****************************************************************************/
-void p_psVectorMin(const psVector* restrict myVector,
-                   const psVector* restrict maskVector,
-                   psU32 maskVal,
-                   psStats* stats)
+int p_psVectorMin(const psVector* restrict myVector,
+                  const psVector* restrict maskVector,
+                  psU32 maskVal,
+                  psStats* stats)
 {
     psS32 i = 0;                // Loop index variable
@@ -335,5 +343,7 @@
     } else {
         stats->min = NAN;
-    }
+        return(1);
+    }
+    return(0);
 }
 
@@ -1205,8 +1215,14 @@
     // Determine minimum and maximum values in the data vector.
     if (isnan(stats->min)) {
-        p_psVectorMin(myVector, maskVector, maskVal, stats);
-    }
+        if (0 != p_psVectorMin(myVector, maskVector, maskVal, stats)) {
+            psLogMsg(__func__, PS_LOG_WARN,
+                     "WARNING: p_psVectorMin(): p_psVectorMin() reported a NAN mean.\n");
+            return(1);
+        }
+    }
+
+
     if (isnan(stats->max)) {
-        p_psVectorMax(myVector, maskVector, maskVal, stats);
+        if (0 != p_psVectorMax(myVector, maskVector, maskVal, stats)) {}
     }
 
@@ -1771,9 +1787,19 @@
     // ************************************************************************
     if (stats->options & PS_STAT_MAX) {
-        p_psVectorMax(inF32, mask, maskVal, stats);
+        if (0 != p_psVectorMax(inF32, mask, maskVal, stats)) {
+            psError(PS_ERR_UNKNOWN, false,
+                    "Failed to calculate vector maximum");
+            psFree(stats);
+            return(NULL);
+        }
     }
     // ************************************************************************
     if (stats->options & PS_STAT_MIN) {
-        p_psVectorMin(inF32, mask, maskVal, stats);
+        if (0 != p_psVectorMin(inF32, mask, maskVal, stats)) {
+            psError(PS_ERR_UNKNOWN, false,
+                    "Failed to calculate vector minimum");
+            psFree(stats);
+            return(NULL);
+        }
     }
 
