Index: branches/eam_branches/ipp-20140226/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/camera/pmFPAfileIO.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/camera/pmFPAfileIO.c	(revision 36619)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/camera/pmReadoutFake.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/camera/pmReadoutFake.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/camera/pmReadoutFake.c	(revision 36619)
@@ -27,4 +27,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/extras/pmThreadTools.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/extras/pmThreadTools.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/extras/pmThreadTools.c	(revision 36619)
@@ -37,4 +37,5 @@
 #include "pmSourcePhotometry.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/extras/pmVisual.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/extras/pmVisual.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/extras/pmVisual.c	(revision 36619)
@@ -41,4 +41,5 @@
 // #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+// #include "pmSourceLensing.h"
 // #include "pmSource.h"
 // #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmPSFEnvelope.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmPSFEnvelope.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmPSFEnvelope.c	(revision 36619)
@@ -27,4 +27,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmSubtractionSimple.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmSubtractionSimple.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmSubtractionSimple.c	(revision 36619)
@@ -34,4 +34,5 @@
 #include "pmSourceSatstar.h"
 
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmSubtractionStamps.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmSubtractionStamps.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/imcombine/pmSubtractionStamps.c	(revision 36619)
@@ -26,4 +26,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/Makefile.am
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/Makefile.am	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/Makefile.am	(revision 36619)
@@ -49,4 +49,5 @@
 	pmSourceIO_CMF_PS1_V3.c \
 	pmSourceIO_CMF_PS1_V4.c \
+	pmSourceIO_CMF_PS1_V5.c \
 	pmSourceIO_CMF_PS1_SV1.c \
 	pmSourceIO_CMF_PS1_SV2.c \
@@ -76,4 +77,5 @@
 	pmGrowthCurve.c \
 	pmSourceMatch.c \
+	pmSourceLensing.c \
 	pmDetEff.c \
 	pmSourceGroups.c \
@@ -125,4 +127,5 @@
 	pmGrowthCurveGenerate.h \
 	pmSourceMatch.h \
+	pmSourceLensing.h \
 	pmDetEff.h \
 	pmSourceGroups.h \
@@ -145,4 +148,5 @@
 pmSourceIO_CMF_PS1_V3.c \
 pmSourceIO_CMF_PS1_V4.c \
+pmSourceIO_CMF_PS1_V5.c \
 pmSourceIO_CMF_PS1_DV1.c \
 pmSourceIO_CMF_PS1_DV2.c \
@@ -163,4 +167,7 @@
 	mksource.pl pmSourceIO_CMF.c.in PS1_V4 pmSourceIO_CMF_PS1_V4.c
 
+pmSourceIO_CMF_PS1_V5.c : pmSourceIO_CMF.c.in mksource.pl
+	mksource.pl pmSourceIO_CMF.c.in PS1_V5 pmSourceIO_CMF_PS1_V5.c
+
 pmSourceIO_CMF_PS1_DV1.c : pmSourceIO_CMF.c.in mksource.pl
 	mksource.pl pmSourceIO_CMF.c.in PS1_DV1 pmSourceIO_CMF_PS1_DV1.c
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/mksource.pl
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/mksource.pl	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/mksource.pl	(revision 36619)
@@ -19,4 +19,5 @@
 	       "PS1_V3", 3,
 	       "PS1_V4", 4,
+	       "PS1_V5", 5,
     );
 %cmfmodes_dv = ("PS1_DV1", 1,
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_DEV.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_DEV.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_DEV.c	(revision 36619)
@@ -43,4 +43,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_EXP.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_EXP.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_EXP.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_GAUSS.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_GAUSS.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_GAUSS.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_PGAUSS.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_PGAUSS.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_PGAUSS.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_PS1_V1.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_PS1_V1.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_PS1_V1.c	(revision 36619)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_QGAUSS.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_QGAUSS.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_QGAUSS.c	(revision 36619)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_RGAUSS.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_RGAUSS.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_RGAUSS.c	(revision 36619)
@@ -41,4 +41,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_SERSIC.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_SERSIC.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_SERSIC.c	(revision 36619)
@@ -50,4 +50,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_TRAIL.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_TRAIL.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/models/pmModel_TRAIL.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmDetEff.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmDetEff.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmDetEff.c	(revision 36619)
@@ -24,4 +24,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmGrowthCurve.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmGrowthCurve.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmGrowthCurve.c	(revision 36619)
@@ -37,4 +37,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmGrowthCurveGenerate.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmModelUtils.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmModelUtils.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmModelUtils.c	(revision 36619)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmMoments.h
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmMoments.h	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmMoments.h	(revision 36619)
@@ -44,4 +44,6 @@
     float Myyyy;   ///< fourth moment
 
+  // float wSum;    ///< window-weighted sum (NOT needed by lensing)
+
     float Sum;    ///< Pixel sum above sky (background).
     float Peak;   ///< Peak counts above sky.
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPCM_MinimizeChisq.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPCM_MinimizeChisq.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPCM_MinimizeChisq.c	(revision 36619)
@@ -38,4 +38,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPCMdata.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPCMdata.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPCMdata.c	(revision 36619)
@@ -38,4 +38,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSF.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSF.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSF.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSF_IO.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSF_IO.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSF_IO.c	(revision 36619)
@@ -54,4 +54,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtry.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtry.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtry.c	(revision 36619)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryFitEXT.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryFitEXT.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryFitEXT.c	(revision 36619)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceUtils.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryFitPSF.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryFitPSF.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryFitPSF.c	(revision 36619)
@@ -34,4 +34,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryMakePSF.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryMakePSF.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryMakePSF.c	(revision 36619)
@@ -35,4 +35,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryMetric.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryMetric.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryMetric.c	(revision 36619)
@@ -35,4 +35,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryModel.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryModel.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPSFtryModel.c	(revision 36619)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmPhotObj.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmPhotObj.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmPhotObj.c	(revision 36619)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSource.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSource.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSource.c	(revision 36619)
@@ -41,4 +41,5 @@
 #include "pmSourcePhotometry.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
@@ -68,4 +69,6 @@
     psFree(tmp->galaxyFits);
     psFree(tmp->radialAper);
+    psFree(tmp->lensingOBJ);
+    psFree(tmp->lensingPSF);
     psTrace("psModules.objects", 10, "---- end ----\n");
 }
@@ -166,4 +169,6 @@
     source->diffStats = NULL;
     source->galaxyFits = NULL;
+    source->lensingOBJ = NULL;
+    source->lensingPSF = NULL;
     source->radialAper = NULL;
     source->parent = NULL;
@@ -249,4 +254,6 @@
 
     source->region           = in->region;
+
+    // XXX I am not copying the pointers to things like the blends, satstar profile, galaxyFits, etc
 
     return(source);
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSource.h
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSource.h	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSource.h	(revision 36619)
@@ -120,4 +120,6 @@
     pmSourceDiffStats *diffStats;       ///< extra parameters for difference detections
     pmSourceGalaxyFits *galaxyFits;     ///< fits to galaxy models (psphotFullForce only)
+    pmSourceLensing *lensingOBJ;        ///< lensing moments parameters (per object)
+    pmSourceLensing *lensingPSF;        ///< lensing moments parameters (psf, interpolated)
     psArray *radialAper;		///< radial flux in circular apertures
     pmSource *parent;			///< reference to the master source from which this is derived
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceContour.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceContour.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceContour.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceExtendedPars.h
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceExtendedPars.h	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceExtendedPars.h	(revision 36619)
@@ -118,5 +118,4 @@
 pmSourceGalaxyFits *pmSourceGalaxyFitsAlloc (void);
 
-
 /// @}
 # endif /* PM_SOURCE_H */
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitModel.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitModel.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitModel.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourcePhotometry.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitPCM.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitPCM.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitPCM.c	(revision 36619)
@@ -38,4 +38,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourcePhotometry.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitSet.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitSet.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceFitSet.c	(revision 36619)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourcePhotometry.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceGroups.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceGroups.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceGroups.c	(revision 36619)
@@ -25,4 +25,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO.c	(revision 36619)
@@ -47,4 +47,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
@@ -597,4 +598,5 @@
 	    PM_SOURCES_WRITE("PS1_V3",    CMF_PS1_V3);
 	    PM_SOURCES_WRITE("PS1_V4",    CMF_PS1_V4);
+	    PM_SOURCES_WRITE("PS1_V5",    CMF_PS1_V5);
 	    PM_SOURCES_WRITE("PS1_SV1",   CMF_PS1_SV1);
 	    PM_SOURCES_WRITE("PS1_SV2",   CMF_PS1_SV2);
@@ -1116,4 +1118,5 @@
 	    PM_SOURCES_READ_PSF("PS1_V3",    CMF_PS1_V3);
 	    PM_SOURCES_READ_PSF("PS1_V4",    CMF_PS1_V4);
+	    PM_SOURCES_READ_PSF("PS1_V5",    CMF_PS1_V5);
 	    PM_SOURCES_READ_PSF("PS1_SV1",   CMF_PS1_SV1);
 	    PM_SOURCES_READ_PSF("PS1_SV2",   CMF_PS1_SV2);
@@ -1389,4 +1392,5 @@
 	PM_SOURCES_READ_XSRC("PS1_V3",    CMF_PS1_V3);
 	PM_SOURCES_READ_XSRC("PS1_V4",    CMF_PS1_V4);
+	PM_SOURCES_READ_XSRC("PS1_V5",    CMF_PS1_V5);
 	PM_SOURCES_READ_XSRC("PS1_SV1",   CMF_PS1_SV1);
 	PM_SOURCES_READ_XSRC("PS1_SV2",   CMF_PS1_SV2);
@@ -1428,4 +1432,5 @@
 	PM_SOURCES_READ_XFIT("PS1_V3",    CMF_PS1_V3);
 	PM_SOURCES_READ_XFIT("PS1_V4",    CMF_PS1_V4);
+	PM_SOURCES_READ_XFIT("PS1_V5",    CMF_PS1_V5);
 	PM_SOURCES_READ_XFIT("PS1_SV1",   CMF_PS1_SV1);
 	PM_SOURCES_READ_XFIT("PS1_SV2",   CMF_PS1_SV2);
@@ -1466,4 +1471,5 @@
 	PM_SOURCES_READ_XRAD("PS1_V3",    CMF_PS1_V3);
 	PM_SOURCES_READ_XRAD("PS1_V4",    CMF_PS1_V4);
+	PM_SOURCES_READ_XRAD("PS1_V5",    CMF_PS1_V5);
 	PM_SOURCES_READ_XRAD("PS1_SV1",   CMF_PS1_SV1);
 	PM_SOURCES_READ_XRAD("PS1_SV2",   CMF_PS1_SV2);
@@ -1504,4 +1510,5 @@
 	PM_SOURCES_READ_XGAL("PS1_V3",    CMF_PS1_V3);
 	PM_SOURCES_READ_XGAL("PS1_V4",    CMF_PS1_V4);
+	PM_SOURCES_READ_XGAL("PS1_V5",    CMF_PS1_V5);
 	PM_SOURCES_READ_XGAL("PS1_SV1",   CMF_PS1_SV1);
 	PM_SOURCES_READ_XGAL("PS1_SV2",   CMF_PS1_SV2);
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO.h
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO.h	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO.h	(revision 36619)
@@ -37,4 +37,5 @@
 MK_PROTO(CMF_PS1_V3);
 MK_PROTO(CMF_PS1_V4);
+MK_PROTO(CMF_PS1_V5);
 MK_PROTO(CMF_PS1_SV1);
 MK_PROTO(CMF_PS1_SV2);
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CFF.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CFF.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CFF.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CMF.c.in
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CMF.c.in	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CMF.c.in	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
@@ -178,4 +179,29 @@
 	@>PS1_V2,PS1_SV?@ 	  psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M4C",      PS_DATA_F32, "fourth momemt cos theta",                    moments.M_c4);
 	@>PS1_V2,PS1_SV?@ 	  psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M4S",      PS_DATA_F32, "fourth momemt sin theta",                    moments.M_s4);
+
+	// Lensing parameters:
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SM_OBJ",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->X11);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SM_OBJ",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->X12);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SM_OBJ",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->X22);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SM_OBJ",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->e1);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SM_OBJ",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->e2);
+
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SH_OBJ",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->X11);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SH_OBJ",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->X12);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SH_OBJ",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->X22);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SH_OBJ",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->e1); 
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SH_OBJ",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->e2); 
+
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SM_PSF",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->X11);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SM_PSF",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->X12);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SM_PSF",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->X22);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SM_PSF",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->e1); 
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SM_PSF",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->e2); 
+
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SH_PSF",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->X11);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SH_PSF",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->X12);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SH_PSF",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->X22);
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SH_PSF",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->e1); 
+	@>PS1_V4@                 psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SH_PSF",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->e2); 
 
         @>PS1_V2,PS1_SV?,>PS1_DV1@ psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_R1",       PS_DATA_F32, "first radial moment",                        moments.Mrf);
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CMP.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CMP.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_CMP.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_MatchedRefs.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_MatchedRefs.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_MatchedRefs.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_OBJ.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_OBJ.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_OBJ.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_CAL_0.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_CAL_0.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_CAL_0.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_DEV_0.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_DEV_0.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_DEV_0.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_DEV_1.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_RAW.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_RAW.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_RAW.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_SMPDATA.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_SMPDATA.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_SMPDATA.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_SX.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_SX.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceIO_SX.c	(revision 36619)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceLensing.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceLensing.c	(revision 36619)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceLensing.c	(revision 36619)
@@ -0,0 +1,112 @@
+/** @file  pmSourceLensing.c
+ *
+ *  Functions to measure the local sky and sky variance for sources on images
+ *  @author EAM, IfA: 
+ *  @date $Date: 2014-03-20 $
+ *
+ *  Copyright 2014 Ifa, University of Hawaii
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include <pslib.h>
+
+#include "pmMoments.h"
+#include "pmSourceLensing.h"
+
+static void pmLensingParsFree (pmLensingPars *pars) {
+  if (!pars) return;
+  return;
+}
+
+pmLensingPars *pmLensingParsAlloc () {
+
+  pmLensingPars *tmp = (pmLensingPars *) psAlloc(sizeof(pmLensingPars));;
+  psMemSetDeallocator(tmp, (psFreeFunc) pmLensingParsFree);
+
+  tmp->X11 = NAN;
+  tmp->X12 = NAN;
+  tmp->X22 = NAN;
+
+  tmp->e1 = NAN;
+  tmp->e2 = NAN;
+
+  return tmp;
+}
+
+static void pmSourceLensingFree (pmSourceLensing *lensing) {
+  if (!lensing) return;
+  psFree (lensing->smear);
+  psFree (lensing->shear);
+  return;
+}
+
+pmSourceLensing *pmSourceLensingAlloc () {
+
+  pmSourceLensing *tmp = (pmSourceLensing *) psAlloc(sizeof(pmSourceLensing));;
+  psMemSetDeallocator(tmp, (psFreeFunc) pmSourceLensingFree);
+
+  tmp->smear = NULL;
+  tmp->shear = NULL;
+
+  return tmp;
+}
+
+// need to supply the moments and the window-function sigma
+bool pmSourceLensingShearFromMoments (pmSourceLensing *lensing, pmMoments *moments, float sigma) {
+  
+  if (!lensing->shear) {
+    lensing->shear = pmLensingParsAlloc();
+  }
+  
+  pmLensingPars *shear = lensing->shear;
+
+  float R = 1.0 / (moments->Mxx + moments->Myy);
+  float s2 = 1.0 / PS_SQR(sigma);
+  // NOTE : not used by shear : float s4 = PS_SQR(s2);
+
+  shear->X11 = R*(2.0*(moments->Mxx + moments->Myy) - s2 * (moments->Mxxxx - 2.0*moments->Mxxyy + moments->Myyyy));
+
+  shear->X22 = R*(2.0*(moments->Mxx + moments->Myy) - s2 * 4.0 * moments->Mxxyy);
+
+  shear->X12 = R*2.0*s2*(moments->Mxyyy - moments->Mxxxy);
+
+  shear->e1  = R*(2.0*(moments->Mxx - moments->Myy) + s2 * (moments->Myyyy - moments->Mxxxx));
+
+  shear->e2  = R*(4.0*moments->Mxy - 2.0*s2*(moments->Mxxxy + moments->Mxyyy));
+  
+  return true;
+}
+
+// need to supply the moments and the window-function sigma
+// NOTE: I'm using the coefficients from Hoekstra et al 1998, not KSB96
+bool pmSourceLensingSmearFromMoments (pmSourceLensing *lensing, pmMoments *moments, float sigma) {
+  
+  if (!lensing->smear) {
+    lensing->smear = pmLensingParsAlloc();
+  }
+  
+  pmLensingPars *smear = lensing->smear;
+
+  float R = 1.0 / (moments->Mxx + moments->Myy);
+  float s2 = 1.0 / PS_SQR(sigma);
+  float s4 = PS_SQR(s2);
+
+  smear->X11 = R*(1.0 - s2*(moments->Mxx + moments->Myy) + 0.25*s4 * (moments->Mxxxx - 2.0*moments->Mxxyy + moments->Myyyy));
+
+  smear->X22 = R*(1.0 - s2*(moments->Mxx + moments->Myy) + 1.00*s4 * (moments->Mxxyy));
+
+  smear->X12 = R*0.5*s4*(moments->Mxxxy - moments->Mxyyy);
+
+  smear->e1  = R*(s2*(moments->Myy - moments->Mxx) + 0.25*s4 * (moments->Myyyy - moments->Mxxxx));
+
+  smear->e2  = R*(0.5*s4*(moments->Mxxxy + moments->Mxyyy) - 2.0*s2*moments->Mxy);
+  
+  return true;
+}
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceLensing.h
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceLensing.h	(revision 36619)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceLensing.h	(revision 36619)
@@ -0,0 +1,36 @@
+/* @file  pmSourceLensing.h
+ *
+ * @author EAM, IfA
+ *
+ * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2014-03-20 02:31:25 $
+ * Copyright 2014 IfA, University of Hawaii
+ */
+
+# ifndef PM_SOURCE_LENSING_H
+# define PM_SOURCE_LENSING_H
+
+/// @addtogroup Objects Object Detection / Analysis Functions
+/// @{
+
+typedef struct {
+  float X11;
+  float X12;
+  float X22;
+  float e1;
+  float e2;
+} pmLensingPars;
+
+typedef struct {
+  pmLensingPars *smear;
+  pmLensingPars *shear;
+} pmSourceLensing; 
+
+pmLensingPars *pmLensingParsAlloc ();
+pmSourceLensing *pmSourceLensingAlloc ();
+
+bool pmSourceLensingShearFromMoments (pmSourceLensing *lensing, pmMoments *moments, float sigma);
+bool pmSourceLensingSmearFromMoments (pmSourceLensing *lensing, pmMoments *moments, float sigma);
+
+/// @}
+# endif /* PM_SOURCE_LENSING_H */
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceMatch.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceMatch.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceMatch.c	(revision 36619)
@@ -25,4 +25,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceMoments.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceMoments.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceMoments.c	(revision 36619)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
@@ -435,4 +436,6 @@
 	}
     }
+    // NOT needed : source->moments->wSum = Sum;
+
     source->moments->Mxx = XX/Sum;
     source->moments->Mxy = XY/Sum;
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceOutputs.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceOutputs.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceOutputs.c	(revision 36619)
@@ -33,4 +33,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePhotometry.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePhotometry.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePhotometry.c	(revision 36619)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotApResid.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotApResid.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotApResid.c	(revision 36619)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotMoments.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotMoments.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotMoments.c	(revision 36619)
@@ -45,4 +45,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotPSFModel.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotPSFModel.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourcePlotPSFModel.c	(revision 36619)
@@ -46,4 +46,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceSky.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceSky.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceSky.c	(revision 36619)
@@ -41,4 +41,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceUtils.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceUtils.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceUtils.c	(revision 36619)
@@ -41,4 +41,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceVisual.c
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceVisual.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/objects/pmSourceVisual.c	(revision 36619)
@@ -23,4 +23,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: branches/eam_branches/ipp-20140226/psModules/src/psmodules.h
===================================================================
--- branches/eam_branches/ipp-20140226/psModules/src/psmodules.h	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psModules/src/psmodules.h	(revision 36619)
@@ -135,4 +135,5 @@
 #include <pmSourceSatstar.h>
 #include <pmSourceDiffStats.h>
+#include <pmSourceLensing.h>
 #include <pmSource.h>
 #include <pmSourceFitModel.h>
Index: branches/eam_branches/ipp-20140226/psphot/src/Makefile.am
===================================================================
--- branches/eam_branches/ipp-20140226/psphot/src/Makefile.am	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psphot/src/Makefile.am	(revision 36619)
@@ -208,4 +208,5 @@
 	psphotMakeGrowthCurve.c	       \
 	psphotMagnitudes.c	       \
+	psphotLensing.c	       \
 	psphotSetMaskBits.c	       \
 	psphotSkyReplace.c	       \
Index: branches/eam_branches/ipp-20140226/psphot/src/psphot.h
===================================================================
--- branches/eam_branches/ipp-20140226/psphot/src/psphot.h	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psphot/src/psphot.h	(revision 36619)
@@ -144,4 +144,8 @@
 bool            psphotMagnitudesReadout(pmConfig *config, psMetadata *recipe, const pmFPAview *view, pmReadout *readout, psArray *sources, pmPSF *psf);
 bool            psphotMagnitudes_Threaded (psThreadJob *job);
+
+bool            psphotLensing (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotLensingReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+bool            psphotLensingPSFtrendsReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
 
 bool            psphotEfficiency (pmConfig *config, const pmFPAview *view, const char *filerule);
Index: branches/eam_branches/ipp-20140226/psphot/src/psphotLensing.c
===================================================================
--- branches/eam_branches/ipp-20140226/psphot/src/psphotLensing.c	(revision 36619)
+++ branches/eam_branches/ipp-20140226/psphot/src/psphotLensing.c	(revision 36619)
@@ -0,0 +1,289 @@
+# include "psphotInternal.h"
+
+// calculate lensing parameters (we have the moments already)
+bool psphotLensing (pmConfig *config, const pmFPAview *view, const char *filerule)
+{
+    bool status = true;
+
+    fprintf (stdout, "\n");
+    psLogMsg ("psphot", PS_LOG_INFO, "--- psphot Lensing Parameters ---");
+
+    // select the appropriate recipe information
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+    psAssert (recipe, "missing recipe?");
+
+    int num = psphotFileruleCount(config, filerule);
+
+    // skip the chisq image (optionally?)
+    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
+    if (!status) chisqNum = -1;
+
+    // loop over the available readouts
+    for (int i = 0; i < num; i++) {
+        if (i == chisqNum) continue; // skip chisq image
+        if (!psphotLensingReadout (config, view, filerule, i, recipe)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to measure lensing parameters for %s entry %d", filerule, i);
+            return false;
+        }
+        if (!psphotLensingPSFtrendsReadout (config, view, filerule, i, recipe)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to measure lensing parameters for %s entry %d", filerule, i);
+            return false;
+        }
+    }
+    return true;
+}
+
+// this block measures the lensing parameters for all objects
+bool psphotLensingReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe)
+{
+    bool status;
+    pmSource *source;
+
+    psTimerStart ("psphot.lensing");
+
+    // find the currently selected readout
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
+    psAssert (file, "missing file?");
+
+    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+    psAssert (readout, "missing readout?");
+
+    // if we have measured the window, we will be saving the modified version of these recipe values on readout->analysis
+    float SIGMA = psMetadataLookupF32 (&status, readout->analysis, "MOMENTS_GAUSS_SIGMA");
+    if (!status) {
+        SIGMA = psMetadataLookupF32 (&status, recipe, "MOMENTS_GAUSS_SIGMA");
+    }
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+
+    psArray *sources = detections->allSources;
+    psAssert (sources, "missing sources?");
+
+    if (!sources->n) {
+        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping lensing");
+        return true;
+    }
+
+    for (int i = 0; i < sources->n; i++) {
+        source = sources->data[i];
+
+	// skip saturated stars modeled with a radial profile 
+        if (source->mode2 & PM_SOURCE_MODE2_SATSTAR_PROFILE) continue;
+        if (source->mode  & PM_SOURCE_MODE_SATSTAR) continue;
+
+        if (source->mode &  PM_SOURCE_MODE_CR_LIMIT) continue;
+        if (source->mode &  PM_SOURCE_MODE_DEFECT) continue;
+
+        pmMoments *moments = source->moments;
+
+	if (!source->lensingOBJ) {
+	  source->lensingOBJ = pmSourceLensingAlloc ();
+	}
+
+	// XXX big objects (eg, saturated stars) use 3*SIGMA.  I'm ignoring this for now
+	pmSourceLensingShearFromMoments (source->lensingOBJ, moments, SIGMA);
+	pmSourceLensingSmearFromMoments (source->lensingOBJ, moments, SIGMA);
+    }
+
+    psLogMsg ("psphot.lensing", PS_LOG_DETAIL, "calculate lensing parameters for %d objects\n", (int) sources->n);
+
+    return true;
+}
+
+// generate image maps for the Xij,ei elements based only on good stars
+bool psphotLensingPSFtrendsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe)
+{
+    int Nfail = 0;
+    bool status;
+    pmSource *source;
+
+    psTimerStart ("psphot.lensing");
+
+    // find the currently selected readout
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
+    psAssert (file, "missing file?");
+
+    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+    psAssert (readout, "missing readout?");
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+
+    psArray *sources = detections->allSources;
+    psAssert (sources, "missing sources?");
+
+    if (!sources->n) {
+        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping lensing psf trends");
+        return true;
+    }
+
+    // gather the stats to assess the aperture residuals
+    int Npsf = 0;
+    psVector *psfX11sm = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psfX12sm = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psfX22sm = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psf_e1sm = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psf_e2sm = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psfX11sh = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psfX12sh = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psfX22sh = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psf_e1sh = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *psf_e2sh = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *xPos     = psVectorAllocEmpty (300, PS_TYPE_F32);
+    psVector *yPos     = psVectorAllocEmpty (300, PS_TYPE_F32);
+
+    for (int i = 0; i < sources->n; i++) {
+        source = sources->data[i];
+
+	// only use good stars:
+        if (source->type != PM_SOURCE_TYPE_STAR) continue;
+        if (source->mode &  PM_SOURCE_MODE_SATSTAR) continue;
+        if (source->mode &  PM_SOURCE_MODE_BLEND) continue;
+        if (source->mode &  PM_SOURCE_MODE_FAIL) continue;
+        if (source->mode &  PM_SOURCE_MODE_POOR) continue;
+
+	// skip saturated stars modeled with a radial profile 
+        if (source->mode2 & PM_SOURCE_MODE2_SATSTAR_PROFILE) continue;
+
+        if (source->mode &  PM_SOURCE_MODE_EXT_LIMIT) continue;
+        if (source->mode &  PM_SOURCE_MODE_CR_LIMIT) continue;
+        if (source->mode &  PM_SOURCE_MODE_DEFECT) continue;
+        if (source->mode2 & PM_SOURCE_MODE2_MATCHED) continue; // XXX ???
+
+	// use psf - ap to limit to good stars?
+	// XXX need to be sure if this is before or after ap corrections are done
+        if (!isfinite(source->apMag)) continue;
+	if (!isfinite(source->psfMag)) continue;
+
+        // XXX make this user-configurable?
+        if (source->psfMagErr > 0.01) continue;
+
+        // aperture residual for this source
+        float dap = source->apMag - source->psfMag;
+
+	// good stars have |dap| < 0.2
+        if (fabs(dap) > 0.2) {
+            Nfail ++;
+            psTrace ("psphot", 3, "fail : bad dap: %f", dap);
+            continue;
+        }
+
+	pmModel *model = source->modelPSF;
+
+        psVectorAppend (xPos, model->params->data.F32[PM_PAR_XPOS]);
+        psVectorAppend (yPos, model->params->data.F32[PM_PAR_YPOS]);
+        psVectorAppend (psfX11sm, source->lensingOBJ->smear->X11);
+        psVectorAppend (psfX12sm, source->lensingOBJ->smear->X12);
+        psVectorAppend (psfX22sm, source->lensingOBJ->smear->X22);
+        psVectorAppend (psf_e1sm, source->lensingOBJ->smear->e1);
+        psVectorAppend (psf_e2sm, source->lensingOBJ->smear->e2);
+        psVectorAppend (psfX11sh, source->lensingOBJ->shear->X11);
+        psVectorAppend (psfX12sh, source->lensingOBJ->shear->X12);
+        psVectorAppend (psfX22sh, source->lensingOBJ->shear->X22);
+        psVectorAppend (psf_e1sh, source->lensingOBJ->shear->e1);
+        psVectorAppend (psf_e2sh, source->lensingOBJ->shear->e2);
+	Npsf ++;
+    }
+
+    // this is a bit tricky, because we have two cases (MAP vs POLY), and they have a different
+    // definition for 'order' (order_MAP = order_POLY + 1).  in addition, we have a
+    // user-specified MAX order, which we should respect, regardless of the mode
+
+    // set the max order (0 = constant) which the number of psf stars can support:
+    // rule of thumb: require 3 stars per 'cell' (order+1)^2
+    int LensingTrendOrder = 0;
+    if (Npsf >= 12) LensingTrendOrder = 1; // 4 cells
+    if (Npsf >= 27) LensingTrendOrder = 2; // 9 cells
+    if (Npsf >= 48) LensingTrendOrder = 3; // 16 cells
+    if (Npsf >  75) LensingTrendOrder = 4; // 25 cells
+
+    pmTrend2DMode mode = PM_TREND_MAP;
+    if (mode == PM_TREND_MAP) {
+        LensingTrendOrder ++;
+    }
+
+    // Nx,Ny are the superpixel size; but I need to choose them to allow a max of LensingTrendOrder values
+    // I'm not sure this is really sensible, but... 
+    int NX = readout->image->numCols;
+    int NY = readout->image->numRows;
+    int Nx, Ny;
+    if (NX > NY) {
+      Nx = LensingTrendOrder;
+      Ny = PS_MAX (1, (int)(LensingTrendOrder * (NY / (float)(NX)) + 0.5));
+    } else {
+      Ny = LensingTrendOrder;
+      Nx = PS_MAX (1, (int)(LensingTrendOrder * (NX / (float)(NY)) + 0.5));
+    }
+
+    // XXX allow user to set this optionally?
+    psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
+
+    // measure Trend2D for the current spatial scale
+    pmTrend2D *trendX11sm = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trendX12sm = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trendX22sm = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trend_e1sm = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trend_e2sm = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+
+    pmTrend2D *trendX11sh = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trendX12sh = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trendX22sh = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trend_e1sh = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+    pmTrend2D *trend_e2sh = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
+
+    bool goodFit = false;
+    if (!pmTrend2DFit (&goodFit, trendX11sm, NULL, 0xff, xPos, yPos, psfX11sm, NULL)) { psWarning ("failed to measure X11 smear trend"); }
+    if (!pmTrend2DFit (&goodFit, trendX12sm, NULL, 0xff, xPos, yPos, psfX12sm, NULL)) { psWarning ("failed to measure X12 smear trend"); }
+    if (!pmTrend2DFit (&goodFit, trendX22sm, NULL, 0xff, xPos, yPos, psfX22sm, NULL)) { psWarning ("failed to measure X22 smear trend"); }
+    if (!pmTrend2DFit (&goodFit, trend_e1sm, NULL, 0xff, xPos, yPos, psf_e1sm, NULL)) { psWarning ("failed to measure  e1 smear trend"); }
+    if (!pmTrend2DFit (&goodFit, trend_e2sm, NULL, 0xff, xPos, yPos, psf_e2sm, NULL)) { psWarning ("failed to measure  e2 smear trend"); }
+
+    if (!pmTrend2DFit (&goodFit, trendX11sh, NULL, 0xff, xPos, yPos, psfX11sh, NULL)) { psWarning ("failed to measure X11 shear trend"); }
+    if (!pmTrend2DFit (&goodFit, trendX12sh, NULL, 0xff, xPos, yPos, psfX12sh, NULL)) { psWarning ("failed to measure X12 shear trend"); }
+    if (!pmTrend2DFit (&goodFit, trendX22sh, NULL, 0xff, xPos, yPos, psfX22sh, NULL)) { psWarning ("failed to measure X22 shear trend"); }
+    if (!pmTrend2DFit (&goodFit, trend_e1sh, NULL, 0xff, xPos, yPos, psf_e1sh, NULL)) { psWarning ("failed to measure  e1 shear trend"); }
+    if (!pmTrend2DFit (&goodFit, trend_e2sh, NULL, 0xff, xPos, yPos, psf_e2sh, NULL)) { psWarning ("failed to measure  e2 shear trend"); }
+
+    // evaluate the PSF trend maps at the location of all sources
+    for (int i = 0; i < sources->n; i++) {
+        source = sources->data[i];
+
+	pmModel *model = source->modelPSF;
+
+	float xPos = model->params->data.F32[PM_PAR_XPOS];
+	float yPos = model->params->data.F32[PM_PAR_YPOS];
+
+	if (!source->lensingPSF) {
+	  source->lensingPSF = pmSourceLensingAlloc ();
+	}
+	pmSourceLensing *lensing = source->lensingPSF;
+
+	if (!lensing->shear) {
+	  lensing->shear = pmLensingParsAlloc();
+	}
+	pmLensingPars *shear = lensing->shear;
+
+	shear->X11 = pmTrend2DEval (trendX11sh, xPos, yPos);
+	shear->X12 = pmTrend2DEval (trendX12sh, xPos, yPos);
+	shear->X22 = pmTrend2DEval (trendX22sh, xPos, yPos);
+	shear->e1  = pmTrend2DEval (trend_e1sh, xPos, yPos);
+	shear->e2  = pmTrend2DEval (trend_e2sh, xPos, yPos);
+	
+	if (!lensing->smear) {
+	  lensing->smear = pmLensingParsAlloc();
+	}
+	pmLensingPars *smear = lensing->smear;
+
+	smear->X11 = pmTrend2DEval (trendX11sm, xPos, yPos);
+	smear->X12 = pmTrend2DEval (trendX12sm, xPos, yPos);
+	smear->X22 = pmTrend2DEval (trendX22sm, xPos, yPos);
+	smear->e1  = pmTrend2DEval (trend_e1sm, xPos, yPos);
+	smear->e2  = pmTrend2DEval (trend_e2sm, xPos, yPos);
+    }
+
+    psLogMsg ("psphot.lensing", PS_LOG_DETAIL, "calculate lensing parameters for %d objects\n", (int) sources->n);
+
+    return true;
+}
+
Index: branches/eam_branches/ipp-20140226/psphot/src/psphotReadout.c
===================================================================
--- branches/eam_branches/ipp-20140226/psphot/src/psphotReadout.c	(revision 36563)
+++ branches/eam_branches/ipp-20140226/psphot/src/psphotReadout.c	(revision 36619)
@@ -340,7 +340,14 @@
     // calculate source magnitudes
     if (!psphotMagnitudes(config, view, filerule)) { // pass 1 (detections->allSources)
-      psErrorStackPrint(stderr, "Unable to do magnitudes.");
+	psErrorStackPrint(stderr, "Unable to do magnitudes.");
         psErrorClear();
     }
+
+    // calculate lensing parameters
+    if (!psphotLensing(config, view, filerule)) {
+	psErrorStackPrint(stderr, "Unable to do lensing parameters.");
+        psErrorClear();
+    }
+
     if (!psphotEfficiency(config, view, filerule)) { // pass 1
         psErrorStackPrint(stderr, "Unable to determine detection efficiencies from fake sources");
@@ -353,12 +360,12 @@
     // replace background in residual image
     if (!psphotSkyReplace (config, view, filerule)) { // pass 1
-      psErrorStackPrint(stderr, "Unable to replace sky");
-      psErrorClear();
+	psErrorStackPrint(stderr, "Unable to replace sky");
+	psErrorClear();
     }
 
     // drop the references to the image pixels held by each source
     if (!psphotSourceFreePixels (config, view, filerule)) { // pass 1
-      psErrorStackPrint(stderr, "Unable to free source pixels");
-      psErrorClear();
+	psErrorStackPrint(stderr, "Unable to free source pixels");
+	psErrorClear();
     }
 
