Index: /trunk/psLib/src/fits/psFits.h
===================================================================
--- /trunk/psLib/src/fits/psFits.h	(revision 42088)
+++ /trunk/psLib/src/fits/psFits.h	(revision 42089)
@@ -60,5 +60,5 @@
     PS_FITS_SCALE_ASINH_STDEV_NEGATIVE, ///< Do asinh scaling, auto-scale to sample stdev, place mean at upper limit
     PS_FITS_SCALE_ASINH_STDEV_BOTH,     ///< Do asinh scaling, auto-scale to sample stdev, place mean at middle
-    PS_FITS_SCALE_ASINH_MANUAL          ///< Manual scaling after doing asinh scaling.
+    PS_FITS_SCALE_ASINH_MANUAL          ///< Manual scaling after doing asinh scaling.(use specified BSCALE, BZERO, BOFFSET, BSOFTEN)
 } psFitsScaling;
 
Index: /trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- /trunk/psLib/src/fits/psFitsImage.c	(revision 42088)
+++ /trunk/psLib/src/fits/psFitsImage.c	(revision 42089)
@@ -744,5 +744,5 @@
       }
     }	
-    // Remove any BSOFTENvalues that exist in the header if we are not using that scaling anymore
+    // Remove any BSOFTEN values 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)||
Index: /trunk/psLib/src/fits/psFitsScale.c
===================================================================
--- /trunk/psLib/src/fits/psFitsScale.c	(revision 42088)
+++ /trunk/psLib/src/fits/psFitsScale.c	(revision 42089)
@@ -963,4 +963,10 @@
         *bzero = options->bzero;
 	*boffset = options->boffset;
+        break;
+      case PS_FITS_SCALE_ASINH_MANUAL:
+        *bscale = options->bscale;
+        *bzero = options->bzero;
+	*boffset = options->boffset;
+	*bsoften = options->bsoften;
         break;
       default:
Index: /trunk/psLib/src/fits/psFitsTableNew.c
===================================================================
--- /trunk/psLib/src/fits/psFitsTableNew.c	(revision 42088)
+++ /trunk/psLib/src/fits/psFitsTableNew.c	(revision 42089)
@@ -97,4 +97,5 @@
 freeTable(psFitsTable *table)
 {
+    if (!table) return;
     for (int col = 0; col < table->numCols; col++) {
         psFitsTableColumn *column = &table->columns[col];
@@ -176,4 +177,7 @@
 {
     // add any element frees here
+    if (!column) return;
+
+    psFree (column->name);
     return;
 }
@@ -195,4 +199,5 @@
     psFitsTableColumn *column = psFitsTableColumnAlloc (name, type);
     psArrayAdd (tableColumns, 10, column);
+    psFree (column);
     return true;
 }
Index: /trunk/psLib/src/math/psMinimizeLMM.c
===================================================================
--- /trunk/psLib/src/math/psMinimizeLMM.c	(revision 42088)
+++ /trunk/psLib/src/math/psMinimizeLMM.c	(revision 42089)
@@ -220,5 +220,6 @@
 
     // XXX should we give up if chisq is nan?
-    psF32 chisq = psMinLM_SetABX(alpha, Beta, params, paramMask, x, y, dy, func);
+    // do not apply reweighting
+    psF32 chisq = psMinLM_SetABX(alpha, Beta, params, paramMask, x, y, dy, false, func);
     if (isnan(chisq)) {
         psTrace ("psLib.math", 5, "psMinLM_SetABX() returned a NAN chisq.\n");
@@ -282,4 +283,8 @@
     return(0.5*dLinear);
 }
+
+// NOTE : reweight is true, the implementation below calculates the alpha,beta terms
+// including a modified weight, but the chi-square value is calculated usig standard
+// weights.  
 
 // alpha, beta, params are already allocated
@@ -292,4 +297,5 @@
     const psVector *y,
     const psVector *dy,
+    bool  reweight, // if true, we calculate the reweighting for each point based on distance from model
     psMinimizeLMChi2Func func)
 {
@@ -306,8 +312,4 @@
     }
 
-    psF32 chisq;
-    psF32 delta;
-    psF32 weight;
-    psF32 ymodel;
     psVector *deriv = psVectorAlloc(params->n, PS_TYPE_F32);
 
@@ -315,13 +317,17 @@
     psImageInit (alpha, 0.0);
     psVectorInit (beta, 0.0);
-    chisq = 0.0;
+    psF32 chisq = 0.0;
 
     // calculate chisq, alpha, beta. alpha & beta only represent unmasked parameters; skip
     // masked ones
     for (psS32 i = 0; i < y->n; i++) {
-        ymodel = func(deriv, params, (psVector *) x->data[i]);
-
-        delta = ymodel - y->data.F32[i];
-        chisq += PS_SQR(delta) * dy->data.F32[i];
+        psF32 ymodel = func(deriv, params, (psVector *) x->data[i]);
+        psF32 delta  = ymodel - y->data.F32[i];
+        psF32 dChi2  = PS_SQR(delta) * dy->data.F32[i];
+	psF32 wtmod  = reweight ? 1.0 / (1.0 + 5.69*dChi2) : 1.0; // see fit1d_irls.c:weight_cauchy
+        chisq += dChi2;
+
+	// if we are doing an IRLS-style fit, we downweight points based on the dChisq
+	// value above: this means setting an additional weight term
 
 	// XXX remove this later:
@@ -337,5 +343,5 @@
             if (paramMask && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[j])) continue;
 
-            weight = deriv->data.F32[j] * dy->data.F32[i];
+            psF32 weight = deriv->data.F32[j] * dy->data.F32[i] * wtmod;
 
             for (int k = 0, K = 0; k <= j; k++) {
@@ -477,5 +483,6 @@
 
     // calculate initial alpha and beta, set chisq (min->value)
-    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func);
+    // do not apply IRLS reweighting yet, even if requested
+    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, false, func);
     if (isnan(min->value)) {
         min->iter = min->maxIter;
@@ -529,5 +536,5 @@
 
         // calculate Chisq for new guess, update Alpha & Beta
-        Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, func);
+        Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, min->useReweighting, func);
         if (isnan(Chisq)) {
             min->iter ++;
@@ -716,5 +723,6 @@
 
     // calculate initial alpha and beta, set chisq (min->value)
-    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func);
+    // do not apply IRLS reweighting yet, even if requested
+    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, false, func);
     if (isnan(min->value)) {
         min->iter = min->maxIter;
@@ -788,5 +796,6 @@
 
         // calculate Chisq for new guess, update Alpha & Beta
-        Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, func);
+	// if requested, modify the weights IRLS-style
+        Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, min->useReweighting, func);
         if (isnan(Chisq)) {
             min->iter ++;
@@ -985,4 +994,5 @@
     min->gainFactorMode = 0;
     min->isInteractive = false;
+    min->useReweighting = false;
     
     return(min);
Index: /trunk/psLib/src/math/psMinimizeLMM.h
===================================================================
--- /trunk/psLib/src/math/psMinimizeLMM.h	(revision 42088)
+++ /trunk/psLib/src/math/psMinimizeLMM.h	(revision 42089)
@@ -90,4 +90,5 @@
     bool chisqConvergence;
     bool isInteractive;
+    bool useReweighting;
 }
 psMinimization;
@@ -194,4 +195,5 @@
     const psVector *y,                 ///< Measurement coordinates
     const psVector *dy,                ///< Weights calculated from y-errors
+    bool  reweight,                    ///< if true, we calculate the reweighting for each point based on distance from model
     psMinimizeLMChi2Func func          ///< Specified function
 );
Index: /trunk/psLib/src/math/psRandom.c
===================================================================
--- /trunk/psLib/src/math/psRandom.c	(revision 42088)
+++ /trunk/psLib/src/math/psRandom.c	(revision 42089)
@@ -38,7 +38,7 @@
 #include "psAssert.h"
 
-
-unsigned long seed = 0;                 // Seed for RNG
-
+// XXX set to non-zero for a test
+# define HARDWIRED_SEED 0
+unsigned long seed = HARDWIRED_SEED;                 // Seed for RNG
 
 psU64 p_psRandomGetSystemSeed(bool log)
@@ -71,4 +71,9 @@
 psU64 psRandomSeed(psU64 value)
 {
+    if (HARDWIRED_SEED) {
+	seed = HARDWIRED_SEED;
+	return seed;
+    }
+
     while (value == 0) {
         value = p_psRandomGetSystemSeed(false);
