Index: trunk/psModules/src/objects/pmPSFtry.c
===================================================================
--- trunk/psModules/src/objects/pmPSFtry.c	(revision 10197)
+++ trunk/psModules/src/objects/pmPSFtry.c	(revision 10198)
@@ -5,6 +5,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-11-20 20:37:50 $
+ *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-11-26 22:24:45 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -46,6 +46,4 @@
     psFree (test->psf);
     psFree (test->sources);
-    // psFree (test->modelEXT);
-    // psFree (test->modelPSF);
     psFree (test->metric);
     psFree (test->metricErr);
@@ -73,6 +71,4 @@
     test->fitMag    = psVectorAlloc (sources->n, PS_TYPE_F64);
     test->mask      = psVectorAlloc (sources->n, PS_TYPE_U8);
-    // test->modelEXT  = psArrayAlloc (sources->n);
-    // test->modelPSF  = psArrayAlloc (sources->n);
 
     test->sources   = psArrayAlloc (sources->n);
@@ -83,6 +79,4 @@
         test->metricErr->data.F64[i] = 0;
         test->fitMag->data.F64[i] = 0;
-        // test->modelEXT->data[i] = NULL;
-        // test->modelPSF->data[i] = NULL;
     }
 
@@ -101,5 +95,5 @@
 
 // generate a pmPSFtry with a copy of the test PSF sources
-pmPSFtry *pmPSFtryModel (psArray *sources, char *modelName, float RADIUS, bool poissonErrors, psPolynomial2D *psfTrendMask)
+pmPSFtry *pmPSFtryModel (psArray *sources, char *modelName, float RADIUS, bool poissonErrors, psPolynomial2D *psfTrendMask, bool applyWeights)
 {
     bool status;
@@ -140,6 +134,4 @@
             continue;
         }
-        // XXX is this still needed?
-        // psfTry->modelEXT->data[i] = psMemIncrRefCounter (source->modelEXT);
         Next ++;
     }
@@ -148,5 +140,5 @@
 
     // stage 2: construct a psf (pmPSF) from this collection of model fits
-    pmPSFFromPSFtry (psfTry);
+    pmPSFFromPSFtry (psfTry, applyWeights);
 
     // stage 3: refit with fixed shape parameters
@@ -168,5 +160,4 @@
         psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_MASK_MARK);
         status = pmSourceFitModel (source, source->modelPSF, PM_SOURCE_FIT_PSF);
-        // pmModelSub (source->pixels, source->mask, source->modelPSF, false, false);
         psImageKeepCircle (source->mask, x, y, RADIUS, "AND", PS_NOT_U8(PM_MASK_MARK));
 
@@ -176,7 +167,4 @@
             continue;
         }
-
-        // otherwise, save the resulting model
-        // psfTry->modelPSF->data[i] = psMemIncrRefCounter (source->modelPSF);
 
         // XXX : use a different aperture radius from the fit radius?
@@ -292,5 +280,5 @@
 
     // XXX test dump of fitted model (dump when tracing?)
-    if (1) {
+    if (psTraceGetLevel("psModules.objects") >= 4) {
         FILE *f = fopen ("resid.dat", "w");
         psVector *apfit = psPolynomial1DEvalVector (poly, r2rflux);
@@ -343,5 +331,5 @@
 Note: some of the array entries may be NULL (failed fits); ignore them.
  *****************************************************************************/
-bool pmPSFFromPSFtry (pmPSFtry *psfTry)
+bool pmPSFFromPSFtry (pmPSFtry *psfTry, bool applyWeight)
 {
     pmPSF *psf = psfTry->psf;
@@ -351,5 +339,9 @@
     psVector *y  = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64);
     psVector *z  = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64);
-    psVector *dz = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64);
+
+    psVector *dz = NULL;
+    if (applyWeight) {
+        dz = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64);
+    }
 
     // construct the x,y terms
@@ -388,6 +380,7 @@
 
             z->data.F64[j] = source->modelEXT->params->data.F32[i];
-            dz->data.F64[j] = source->modelEXT->dparams->data.F32[i];
-            // dz->data.F64[j] = 1; // use the model-fitted error? or S/N?
+            if (applyWeight) {
+                dz->data.F64[j] = source->modelEXT->dparams->data.F32[i];
+            }
 
             // for SXY, we actually fit SXY / (SXX^-2  + SYY^-2)
@@ -396,14 +389,16 @@
             }
         }
-        // XXX does psfTry->mask need to be init to 0 and set to 1 above?
-        if (!psVectorClipFitPolynomial2D(psf->params_NEW->data[i], stats, psfTry->mask, 0xff, z, dz, x, y)) {
+
+        // fit the collection of measured parameters to the PSF 2D model
+        // the mask is carried from previous steps and updated with this operation
+        // the weight is either the parameter error or NULL, depending on 'applyWeights'
+        if (!psVectorClipFitPolynomial2D(psf->params_NEW->data[i], stats, psfTry->mask, 0xff, z, NULL, x, y)) {
             psError(PS_ERR_UNKNOWN, false, "failed to build psf model for parameter %d", i);
             return false;
         }
-        // psTrace ("psphot.psftest", 3, "keeping %d of %d PSF candidates (PSF param %d)\n", Nkeep, psfTry->mask->n, i);
-    }
-
-    // XXX test dump of star parameters vs position (compare with fitted values)
-    if (1) {
+    }
+
+    // test dump of star parameters vs position (compare with fitted values)
+    if (psTraceGetLevel("psModules.objects") >= 4) {
         FILE *f = fopen ("params.dat", "w");
 
