Index: /trunk/psLib/src/math/psStats.c
===================================================================
--- /trunk/psLib/src/math/psStats.c	(revision 18875)
+++ /trunk/psLib/src/math/psStats.c	(revision 18876)
@@ -13,6 +13,6 @@
  * use ->min and ->max (PS_STAT_USE_RANGE)
  *
- *  @version $Revision: 1.227 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-07-12 21:08:14 $
+ *  @version $Revision: 1.228 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-08-03 20:54:43 $
  *
  *  Copyright 2006 IfA, University of Hawaii
@@ -310,6 +310,7 @@
     psF32 *input = inVector->data.F32; // Dereference the vector
 
-    // Allocate temporary vectors for the data.
-    psVector *outVector = psVectorAllocEmpty(inVector->n, PS_TYPE_F32); // Vector containing unmasked values
+    // use the temporary vector for the sorted output
+    stats->tmpData = psVectorRecycle (stats->tmpData, inVector->n, PS_TYPE_F32);
+    psVector *outVector = stats->tmpData;
     psF32 *output = outVector->data.F32; // Dereference the vector
 
@@ -333,5 +334,4 @@
     if (count == 0) {
         psError(PS_ERR_BAD_PARAMETER_SIZE, true, "No valid data in input vector.\n");
-        psFree(outVector);
         stats->sampleUQ = NAN;
         stats->sampleLQ = NAN;
@@ -346,5 +346,4 @@
         stats->sampleLQ = NAN;
         stats->sampleMedian = NAN;
-        psFree(outVector);
         return false;
     }
@@ -363,7 +362,4 @@
     stats->results |= PS_STAT_SAMPLE_MEDIAN;
     stats->results |= PS_STAT_SAMPLE_QUARTILE;
-
-    // Free the temporary data structures.
-    psFree(outVector);
 
     // Return "true" on success.
@@ -584,5 +580,8 @@
     // We copy the mask vector, to preserve the original
     psMaskType maskVal = MASK_MARK | maskValInput;
-    psVector *tmpMask = psVectorAlloc(myVector->n, PS_TYPE_U8);
+
+    // use the temporary vector for local temporary mask
+    stats->tmpMask = psVectorRecycle (stats->tmpMask, myVector->n, PS_TYPE_U8);
+    psVector *tmpMask = stats->tmpMask;
     psVectorInit(tmpMask, 0);
     if (maskInput) {
@@ -599,5 +598,4 @@
         psTrace(TRACE, 5, "Call to vectorSampleMedian returned NAN\n");
         psTrace(TRACE, 4, "---- %s(false) end ----\n", __func__);
-        psFree(tmpMask);
         return false;
     }
@@ -609,5 +607,4 @@
         psTrace(TRACE, 5, "Call to vectorSampleStdev returned NAN\n");
         psTrace(TRACE, 4, "---- %s(false) end ----\n", __func__);
-        psFree(tmpMask);
         return false;
     }
@@ -654,23 +651,24 @@
         // b) compute new mean and stdev
         // Allocate a psStats structure for calculating the mean and stdev.
-        psStats *statsTmp = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
-        vectorSampleMean(myVector, errors, tmpMask, maskVal, statsTmp);
-        vectorSampleStdev(myVector, errors, tmpMask, maskVal, statsTmp);
-        psTrace(TRACE, 6, "The new sample mean is %f\n", statsTmp->sampleMean);
-        psTrace(TRACE, 6, "The new sample stdev is %f\n", statsTmp->sampleStdev);
+	// XXX Can we just use this psStats structure to calculate the SAMPLE MEAN and STDEV?
+        // psStats *statsTmp = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
+        // vectorSampleMean(myVector, errors, tmpMask, maskVal, statsTmp);
+        // vectorSampleStdev(myVector, errors, tmpMask, maskVal, statsTmp);
+        vectorSampleMean(myVector, errors, tmpMask, maskVal, stats);
+        vectorSampleStdev(myVector, errors, tmpMask, maskVal, stats);
+        psTrace(TRACE, 6, "The new sample mean is %f\n", stats->sampleMean);
+        psTrace(TRACE, 6, "The new sample stdev is %f\n", stats->sampleStdev);
 
         // If the new mean and stdev are NAN, we must exit the loop.
         // Otherwise, use the new results and continue.
-        if (isnan(statsTmp->sampleMean) || isnan(statsTmp->sampleStdev)) {
-            // Exit loop.
+        if (isnan(stats->sampleMean) || isnan(stats->sampleStdev)) {
             iter = stats->clipIter;
             psError(PS_ERR_UNKNOWN, true, "vectorSampleMean() or vectorSampleStdev() returned a NAN.\n");
-            psFree(tmpMask);
             return false;
         } else {
-            clippedMean = statsTmp->sampleMean;
-            clippedStdev = statsTmp->sampleStdev;
-        }
-        psFree(statsTmp);
+            clippedMean = stats->sampleMean;
+            clippedStdev = stats->sampleStdev;
+        }
+        // psFree(statsTmp);
     }
 
@@ -690,5 +688,4 @@
     psTrace(TRACE, 6, "The final clipped stdev is %f\n", clippedStdev);
 
-    psFree(tmpMask);
     psTrace(TRACE, 4, "---- %s(true) end ----\n", __func__);
     return true;
@@ -2139,8 +2136,10 @@
 /*****************************************************************************/
 
-// We keep statsFree so that we can identify statistics pointers from the memblock
+// free function
 static void statsFree(psStats *stats)
 {
-    // There are non dynamic allocated items
+    if (!stats) return;
+    psFree (stats->tmpData);
+    psFree (stats->tmpMask);
     return;
 }
@@ -2165,4 +2164,6 @@
     stats->nSubsample = 100000;
     stats->options = options;
+    stats->tmpData = NULL;
+    stats->tmpMask = NULL;
 
     psTrace(TRACE, 3, "---- %s() end  ----\n", __func__);
Index: /trunk/psLib/src/math/psStats.h
===================================================================
--- /trunk/psLib/src/math/psStats.h	(revision 18875)
+++ /trunk/psLib/src/math/psStats.h	(revision 18876)
@@ -8,6 +8,6 @@
  * @author GLG, MHPCC
  *
- * @version $Revision: 1.64 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-12-15 01:17:28 $
+ * @version $Revision: 1.65 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-08-03 20:55:09 $
  * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
  */
@@ -86,4 +86,6 @@
     psStatsOptions options;            ///< bitmask of values requested
     psStatsOptions results;            ///< bitmask of values calculated
+    psVector *tmpData;		       ///< temporary vector so repeated calls do not have to realloc
+    psVector *tmpMask;		       ///< temporary vector so repeated calls do not have to realloc
 }
 psStats;
