Index: trunk/psModules/src/astrom/pmAstrometryObjects.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryObjects.c	(revision 36834)
+++ trunk/psModules/src/astrom/pmAstrometryObjects.c	(revision 39926)
@@ -225,5 +225,6 @@
     psArray *ref,
     psArray *match,
-    psStats *stats)
+    psStats *stats,
+    const psMetadata *config)
 {
     PS_ASSERT_PTR_NON_NULL(map, NULL);
@@ -232,4 +233,11 @@
     PS_ASSERT_PTR_NON_NULL(match, NULL);
     PS_ASSERT_PTR_NON_NULL(stats, NULL);
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+
+    // sigma of gaussian window to down-weight photometric outliers (ignored if NAN or 0.0)
+    bool status;
+    double photomWindowSigma  = psMetadataLookupF32 (&status, config, "PSASTRO.PHOTOM.WINDOW.SIGMA");
+    bool   photomWindowApply  = !(isnan(photomWindowSigma) || (fabs(photomWindowSigma) < 0.01));
+    double photomWindowFactor = photomWindowApply ? -0.5/PS_SQR(photomWindowSigma) : 0.0;
 
     // reassign values for clip fit
@@ -252,5 +260,6 @@
         y->data.F32[i] = refStar->FP->y;
 
-        wt->data.F32[i] = 1.0;
+	// wt is used as an error (sqrt(variance)) in the fit. the 1.01 prevents the weight from going to 0.0 for perfect matches
+        wt->data.F32[i] = 1.01 - exp(photomWindowFactor*PS_SQR(refStar->magCal - rawStar->magCal)); 
     }
 
@@ -657,4 +666,5 @@
     obj->Color= 0;
     obj->dMag = 0;
+    obj->magCal = 0;
 
     return (obj);
@@ -685,4 +695,5 @@
     obj->Color =  old->Color;
     obj->dMag  =  old->dMag;
+    obj->magCal =  old->magCal;
 
     return(obj);
@@ -774,6 +785,11 @@
     double gridOffset = psMetadataLookupF32 (&status, config, "PSASTRO.GRID.OFFSET");
 
-    // sampling scale of the grid
-    double gridScale  = psMetadataLookupF32 (&status, config, "PSASTRO.GRID.SCALE");
+     // sampling scale of the grid
+     double gridScale  = psMetadataLookupF32 (&status, config, "PSASTRO.GRID.SCALE");
+
+    // sigma of gaussian window to down-weight photometric outliers (ignored if NAN or 0.0)
+    double photomWindowSigma  = psMetadataLookupF32 (&status, config, "PSASTRO.PHOTOM.WINDOW.SIGMA");
+    bool   photomWindowApply  = !(isnan(photomWindowSigma) || (fabs(photomWindowSigma) < 0.01));
+    double photomWindowFactor = photomWindowApply ? -0.5/PS_SQR(photomWindowSigma) : 0.0;
 
     // set the static scaling factors
@@ -816,9 +832,12 @@
             }
 
+	    // XXX should I make the scale factor in front a recipe value?
+	    int Npts = 10 * exp(photomWindowFactor*PS_SQR(ob1->magCal - ob2->magCal)); 
+
             // accumulate bin stats
-            NP[iY][iX] ++;
-            DX[iY][iX] += dX;
-            DY[iY][iX] += dY;
-            D2[iY][iX] += PS_SQR(dX) + PS_SQR(dY);
+            NP[iY][iX] += Npts;
+            DX[iY][iX] += dX*Npts;
+            DY[iY][iX] += dY*Npts;
+            D2[iY][iX] += PS_SQR(dX*Npts) + PS_SQR(dY*Npts);
         }
     }
@@ -1053,4 +1072,9 @@
     double tweakNsigma = psMetadataLookupF32 (&status, recipe, "PSASTRO.TWEAK.NSIGMA");
 
+    // sigma of gaussian window to down-weight photometric outliers (ignored if NAN or 0.0)
+    double photomWindowSigma  = psMetadataLookupF32 (&status, recipe, "PSASTRO.PHOTOM.WINDOW.SIGMA");
+    bool   photomWindowApply  = !(isnan(photomWindowSigma) || (fabs(photomWindowSigma) < 0.01));
+    double photomWindowFactor = photomWindowApply ? -0.5/PS_SQR(photomWindowSigma) : 0.0;
+
     nBin = 2*tweakRange / tweakScale;
     psVector *xHist = psVectorAlloc (nBin, PS_TYPE_F32);
@@ -1079,6 +1103,9 @@
                 continue;
 
-            xHist->data.F32[xBin] += 1.0;
-            yHist->data.F32[yBin] += 1.0;
+	    // XXX should I make the scale factor in front a recipe value?
+	    int Npts = 10 * exp(photomWindowFactor*PS_SQR(ob1->magCal - ob2->magCal));  // sigma = 0.22 mag
+
+            xHist->data.F32[xBin] += Npts;
+            yHist->data.F32[yBin] += Npts;
         }
     }
