Index: trunk/psModules/src/detrend/pmShutterCorrection.c
===================================================================
--- trunk/psModules/src/detrend/pmShutterCorrection.c	(revision 8886)
+++ trunk/psModules/src/detrend/pmShutterCorrection.c	(revision 8926)
@@ -101,14 +101,15 @@
     // XXX we could examine the top 2 or 3 values and decide if we
     // extended exptime enough or median clip.
-    pars->scale = counts->data.F64[N-1];
+    pars->scale = counts->data.F32[N-1];
 
     // fit a line to the lowest three points and extrapolate to 0.0
-    tmpX = psVectorAlloc (2, PS_TYPE_F64);
-    tmpY = psVectorAlloc (2, PS_TYPE_F64);
-
-    tmpX->data.F64[0] = exptime->data.F64[0];
-    tmpX->data.F64[1] = exptime->data.F64[1];
-    tmpY->data.F64[0] = counts->data.F64[0];
-    tmpY->data.F64[1] = counts->data.F64[1];
+    tmpX = psVectorAlloc (2, PS_TYPE_F32);
+    tmpY = psVectorAlloc (2, PS_TYPE_F32);
+    tmpX->n = tmpY->n = 2;
+
+    tmpX->data.F32[0] = exptime->data.F32[0];
+    tmpX->data.F32[1] = exptime->data.F32[1];
+    tmpY->data.F32[0] = counts->data.F32[0];
+    tmpY->data.F32[1] = counts->data.F32[1];
 
     // fit a line and extrapolate the fit to 0.0
@@ -125,11 +126,16 @@
     float value = pars->scale * (1 + ratio) / 2.0;
 
-    int Nm = psVectorBracket (exptime, value, (ratio < 1.0));
-    int Np = (Nm == N - 1) ? Nm - 1 : Nm + 1;
-
-    tmpX->data.F64[0] = counts->data.F64[Nm];
-    tmpX->data.F64[1] = counts->data.F64[Np];
-    tmpY->data.F64[0] = exptime->data.F64[Nm];
-    tmpY->data.F64[1] = exptime->data.F64[Np];
+    int Np;
+    if (ratio < 1.0) {
+        Np = psVectorBracket (counts, value, true);
+    } else {
+        Np = psVectorBracketDescend (counts, value, true);
+    }
+    int Nm = (Np == 0) ? 1 : Np - 1;
+
+    tmpX->data.F32[0] = counts->data.F32[Nm];
+    tmpX->data.F32[1] = counts->data.F32[Np];
+    tmpY->data.F32[0] = exptime->data.F32[Nm];
+    tmpY->data.F32[1] = exptime->data.F32[Np];
 
     // fit a line and extrapolate the fit to counts = A (1 + dTk/dTo) : exptime = dTo
@@ -146,5 +152,5 @@
 
 // linear fit to the counts and exptime, given a value for offref
-pmShutterCorrPars *pmShutterCorrectionLinFit (psVector *exptime, psVector *counts, float offref)
+pmShutterCorrPars *pmShutterCorrectionLinFit (psVector *exptime, psVector *counts, psVector *cntError, float offref)
 {
 
@@ -152,4 +158,5 @@
     psVector *x = psVectorAlloc (exptime->n, PS_TYPE_F32);
     psVector *y = psVectorAlloc (exptime->n, PS_TYPE_F32);
+    x->n = y->n = exptime->n;
 
     for (int i = 0; i < exptime->n; i++) {
@@ -167,5 +174,5 @@
     line->coeff[1][1] = 0;
 
-    psVectorFitPolynomial2D (line, NULL, 0, counts, NULL, x, y);
+    psVectorFitPolynomial2D (line, NULL, 0, counts, cntError, x, y);
 
     pmShutterCorrPars *pars = pmShutterCorrParsAlloc ();
@@ -174,4 +181,8 @@
     pars->scale  = line->coeff[1][0];
     pars->offset = line->coeff[0][1] / line->coeff[1][0];
+
+    psFree (x);
+    psFree (y);
+    psFree (line);
 
     return (pars);
@@ -196,5 +207,5 @@
 
 // non-linear fit to the counts and exptime, given a guess for the three parameters
-pmShutterCorrPars *pmShutterCorrectionFullFit (psVector *exptime, psVector *counts, pmShutterCorrPars *guess)
+pmShutterCorrPars *pmShutterCorrectionFullFit (psVector *exptime, psVector *counts, psVector *cntError, pmShutterCorrPars *guess)
 {
 
@@ -221,19 +232,14 @@
     // construct the coordinate and value entries (y is counts)
     psArray *x = psArrayAlloc(exptime->n);
-    psVector *y = counts;
-    psVector *yErr = psVectorAlloc(exptime->n, PS_TYPE_F32);
-    x->n = yErr->n = exptime->n;
+    x->n = exptime->n;
 
     for (int i = 0; i < exptime->n; i++) {
         psVector *coord = psVectorAlloc(1, PS_TYPE_F32);
         coord->n = 1;
-
         coord->data.F32[0] = exptime->data.F32[i];
-
         x->data[i] = coord;
-        yErr->data.F32[i] = sqrt(y->data.F32[i]);
-    }
-
-    psMinimizeLMChi2(myMin, covar, params, constrain, x, y, yErr, pmShutterCorrectionModel);
+    }
+
+    psMinimizeLMChi2(myMin, covar, params, constrain, x, counts, cntError, pmShutterCorrectionModel);
 
     pmShutterCorrPars *pars = pmShutterCorrParsAlloc ();
@@ -243,4 +249,8 @@
     pars->offref = params->data.F32[2];
 
-    return (pars);
-}
+    psFree (myMin);
+    psFree (params);
+    psFree (x);
+
+    return (pars);
+}
Index: trunk/psModules/src/detrend/pmShutterCorrection.h
===================================================================
--- trunk/psModules/src/detrend/pmShutterCorrection.h	(revision 8886)
+++ trunk/psModules/src/detrend/pmShutterCorrection.h	(revision 8926)
@@ -48,6 +48,6 @@
  *  @author Eugene Magnier, IfA
  *
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-09-22 21:00:53 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-09-25 01:06:28 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -66,4 +66,4 @@
 pmShutterCorrPars *pmShutterCorrParsAlloc ();
 pmShutterCorrPars *pmShutterCorrectionGuess (psVector *exptime, psVector *counts);
-pmShutterCorrPars *pmShutterCorrectionLinFit (psVector *exptime, psVector *counts, float offref);
-pmShutterCorrPars *pmShutterCorrectionFullFit (psVector *exptime, psVector *counts, pmShutterCorrPars *guess);
+pmShutterCorrPars *pmShutterCorrectionLinFit (psVector *exptime, psVector *counts, psVector *cntError, float offref);
+pmShutterCorrPars *pmShutterCorrectionFullFit (psVector *exptime, psVector *counts, psVector *cntError, pmShutterCorrPars *guess);
