Index: /trunk/Ohana/src/opihi/include/pantasks.h
===================================================================
--- /trunk/Ohana/src/opihi/include/pantasks.h	(revision 36622)
+++ /trunk/Ohana/src/opihi/include/pantasks.h	(revision 36623)
@@ -116,5 +116,5 @@
 
   int active;
-  int priority;
+  int nicelevel;
 
 } Task;
@@ -161,5 +161,5 @@
 
   JobMode     mode;			/* local or controller? */
-  int     priority;
+  int     nicelevel;
   char   *realhost;
 
Index: /trunk/Ohana/src/opihi/include/pcontrol.h
===================================================================
--- /trunk/Ohana/src/opihi/include/pcontrol.h	(revision 36622)
+++ /trunk/Ohana/src/opihi/include/pcontrol.h	(revision 36623)
@@ -120,5 +120,5 @@
   int          exit_status;
   int          Reset;
-  int          priority;
+  int          nicelevel;
   JobMode      mode;
   JobStat      state;
@@ -315,5 +315,5 @@
 Job   *PullJobByID (IDtype JobID, int *StackID);
 Job   *PullJobFromStackByID (int StackID, int ID);
-IDtype AddJob (char *hostname, JobMode mode, int timeout, int priority, int argc, char **argv, int Nxhosts, char **xhosts);
+IDtype AddJob (char *hostname, JobMode mode, int timeout, int nicelevel, int argc, char **argv, int Nxhosts, char **xhosts);
 void   DelJob (Job *job);
 Host  *UnlinkJobAndHost (Job *job);
Index: /trunk/Ohana/src/opihi/pantasks/ControllerOps.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/ControllerOps.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/ControllerOps.c	(revision 36623)
@@ -259,7 +259,7 @@
   }
   
-  if (job[0].priority) {
+  if (job[0].nicelevel) {
     char tmp[64];
-    snprintf (tmp, 64, " -nice %d", job[0].priority);
+    snprintf (tmp, 64, " -nice %d", job[0].nicelevel);
     strcat (cmd, tmp);
   }
Index: /trunk/Ohana/src/opihi/pantasks/JobOps.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/JobOps.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/JobOps.c	(revision 36623)
@@ -107,5 +107,5 @@
     job[0].mode = JOB_CONTROLLER;
   }
-  job[0].priority = task[0].priority;
+  job[0].nicelevel = task[0].nicelevel;
 
   /* we need our own copy of task[0].argv argc is the number of valid args, like the usual command line.  we
Index: /trunk/Ohana/src/opihi/pantasks/LocalJob.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/LocalJob.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/LocalJob.c	(revision 36623)
@@ -162,8 +162,8 @@
 
   /* set nice level for the child process -- maybe I should not exit here... */
-  if (job[0].priority) {
-      status = setpriority (PRIO_PROCESS, pid, job[0].priority);
+  if (job[0].nicelevel) {
+      status = setpriority (PRIO_PROCESS, pid, job[0].nicelevel);
       if (status == -1) {
-	  gprint (GP_ERR, "error setting priority\n");
+	  gprint (GP_ERR, "error setting nice level\n");
 	  perror ("setpriority: ");
 	  exit (2);
Index: /trunk/Ohana/src/opihi/pantasks/TaskOps.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/TaskOps.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/TaskOps.c	(revision 36623)
@@ -487,5 +487,5 @@
 
   NewTask[0].active = TRUE;
-  NewTask[0].priority = 0;
+  NewTask[0].nicelevel = 0;
   return (NewTask);
 }
Index: /trunk/Ohana/src/opihi/pantasks/init.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/init.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/init.c	(revision 36623)
@@ -34,5 +34,5 @@
   {1, "halt",       halt,          "halt the scheduler (no job harvesting)"},
   {1, "host",       task_host,     "define host machine for a task"},
-  {1, "nice",       task_nice,     "set nice priority level for a task"},
+  {1, "nice",       task_nice,     "set nice level for a task"},
   {1, "ipptool2book", ipptool2book, "convert queue with ipptool output to book"},
   {1, "kill",       kill_job,      "kill job"},
Index: /trunk/Ohana/src/opihi/pantasks/init_server.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/init_server.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/init_server.c	(revision 36623)
@@ -35,5 +35,5 @@
   {1, "delete",     delete_job,    "delete job"},
   {1, "host",       task_host,     "define host machine for a task"},
-  {1, "nice",       task_nice,     "set nice priority level for a task"},
+  {1, "nice",       task_nice,     "set nice level for a task"},
   {1, "ipptool2book", ipptool2book, "convert queue with ipptool output to book"},
   {1, "kill",       kill_job,      "kill job"},
Index: /trunk/Ohana/src/opihi/pantasks/task_nice.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/task_nice.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pantasks/task_nice.c	(revision 36623)
@@ -4,9 +4,9 @@
 
   char *endptr = NULL;
-  int priority;
+  int nicelevel;
   Task *task;
 
   if (argc != 2) {
-    gprint (GP_ERR, "USAGE: nice (priority)\n");
+    gprint (GP_ERR, "USAGE: nice (nicelevel)\n");
     return (FALSE);
   }
@@ -20,15 +20,15 @@
   }
 
-  priority = strtol (argv[1], &endptr, 10);
+  nicelevel = strtol (argv[1], &endptr, 10);
   if (*endptr) goto fail;
-  if (priority < 0) goto fail;
-  if (priority > 20) goto fail;
+  if (nicelevel < 0) goto fail;
+  if (nicelevel > 20) goto fail;
 
-  task[0].priority = priority;
+  task[0].nicelevel = nicelevel;
   JobTaskUnlock();
   return (TRUE);
 
 fail:
-    gprint (GP_ERR, "ERROR: nice (priority) -- priority must be an integer 0 to 20\n");
+    gprint (GP_ERR, "ERROR: nice (nicelevel) -- nicelevel must be an integer 0 to 20\n");
     return (FALSE);
 }
Index: /trunk/Ohana/src/opihi/pclient/job.c
===================================================================
--- /trunk/Ohana/src/opihi/pclient/job.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pclient/job.c	(revision 36623)
@@ -3,11 +3,11 @@
 int job (int argc, char **argv) {
 
-  int i, N, pid, status, priority;
+  int i, N, pid, status, nicelevel;
   char **targv;
 
-  priority = 0;
+  nicelevel = 0;
   if ((N = get_argument (argc, argv, "-nice"))) {
     remove_argument (N, &argc, argv);
-    priority = atoi (argv[N]);
+    nicelevel = atoi (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -62,8 +62,8 @@
 
   /* set nice level for the child process */
-  if (priority) {
-      status = setpriority (PRIO_PROCESS, pid, priority);
+  if (nicelevel) {
+      status = setpriority (PRIO_PROCESS, pid, nicelevel);
       if (status == -1) {
-	  gprint (GP_ERR, "error setting priority\n");
+	  gprint (GP_ERR, "error setting nicelevel\n");
 	  perror ("setpriority: ");
 	  exit (2);
Index: /trunk/Ohana/src/opihi/pcontrol/JobOps.c
===================================================================
--- /trunk/Ohana/src/opihi/pcontrol/JobOps.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pcontrol/JobOps.c	(revision 36623)
@@ -207,5 +207,5 @@
 }
 
-IDtype AddJob (char *hostname, JobMode mode, int timeout, int priority, int argc, char **argv, int Nxhosts, char **xhosts) {
+IDtype AddJob (char *hostname, JobMode mode, int timeout, int nicelevel, int argc, char **argv, int Nxhosts, char **xhosts) {
 
   int JobID;
@@ -232,5 +232,5 @@
 
   job[0].mode     = mode;
-  job[0].priority = priority;
+  job[0].nicelevel = nicelevel;
 
   job[0].state = 0;
Index: /trunk/Ohana/src/opihi/pcontrol/StartJob.c
===================================================================
--- /trunk/Ohana/src/opihi/pcontrol/StartJob.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pcontrol/StartJob.c	(revision 36623)
@@ -25,7 +25,7 @@
   bzero (line, Nline);
   strcpy (line, "job");
-  if (job[0].priority) {
+  if (job[0].nicelevel) {
     char tmp[64];
-    snprintf (tmp, 64, " -nice %d", job[0].priority);
+    snprintf (tmp, 64, " -nice %d", job[0].nicelevel);
     strcat (line, tmp);
   }
Index: /trunk/Ohana/src/opihi/pcontrol/job.c
===================================================================
--- /trunk/Ohana/src/opihi/pcontrol/job.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pcontrol/job.c	(revision 36623)
@@ -5,5 +5,5 @@
   char *Host = NULL;
   char **targv = NULL;
-  int i, N, Mode, targc, Timeout, priority;
+  int i, N, Mode, targc, Timeout, nicelevel;
   IDtype JobID;
   char **xhosts = NULL;
@@ -43,8 +43,8 @@
   }
 
-  priority = 0;
+  nicelevel = 0;
   if ((N = get_argument (argc, argv, "-nice"))) {
     remove_argument (N, &argc, argv);
-    priority = atoi (argv[N]);
+    nicelevel = atoi (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -76,5 +76,5 @@
 
   // a JobID < 0 mean the job was not accepted
-  JobID = AddJob (Host, Mode, Timeout, priority, targc, targv, Nxhosts, xhosts);
+  JobID = AddJob (Host, Mode, Timeout, nicelevel, targc, targv, Nxhosts, xhosts);
   gprint (GP_LOG, "JobID: %d\n", (int) JobID);
   return (TRUE);
@@ -82,5 +82,5 @@
  usage:
     gprint (GP_ERR, "USAGE: job [options] (arg0) (arg1) ... (argN)\n");
-    gprint (GP_ERR, "  options: -host, +host, -timeout, -xhost (host)\n");
+    gprint (GP_ERR, "  options: -host, +host, -timeout, -xhost (host) -nice (level)\n");
     gprint (GP_ERR, "  arguments of the form @MAX_THREADS@ will be replaced when the job is launched\n");
 
Index: /trunk/Ohana/src/opihi/pcontrol/status.c
===================================================================
--- /trunk/Ohana/src/opihi/pcontrol/status.c	(revision 36622)
+++ /trunk/Ohana/src/opihi/pcontrol/status.c	(revision 36623)
@@ -154,5 +154,5 @@
 
     PrintID (GP_LOG, job[0].JobID);
-    gprint (GP_LOG, " %2d ", job[0].priority);
+    gprint (GP_LOG, " %2d ", job[0].nicelevel);
     for (j = 0; j < job[0].argc; j++) {
       gprint (GP_LOG, "%s ", job[0].argv[j]);
Index: /trunk/ippTools/src/fftool.c
===================================================================
--- /trunk/ippTools/src/fftool.c	(revision 36622)
+++ /trunk/ippTools/src/fftool.c	(revision 36623)
@@ -180,5 +180,5 @@
     psString whereClause = psDBGenerateWhereConditionSQL(skycalWhereMD, NULL);
     psStringAppend(&where, "\nAND %s", whereClause);
-    psStringAppend(&select, where);
+    psStringAppend(&select, " %s", where);
     psFree(whereClause);
     psFree(skycalWhereMD);
Index: /trunk/ippTools/src/remotetool.c
===================================================================
--- /trunk/ippTools/src/remotetool.c	(revision 36622)
+++ /trunk/ippTools/src/remotetool.c	(revision 36623)
@@ -149,5 +149,5 @@
     psFree(limitString);
   }
-  if (!p_psDBRunQueryF(config->dbh, query)) {
+  if (!p_psDBRunQuery(config->dbh, query)) {
     psError(PS_ERR_UNKNOWN, false, "database error");
     psFree(query);
@@ -313,5 +313,5 @@
 {
   PS_ASSERT_PTR_NON_NULL(config, false);
-  PXOPT_LOOKUP_S64(remote_id,  config->args, "-remote_id",    true, false);
+  // PXOPT_LOOKUP_S64(remote_id,  config->args, "-remote_id",    true, false);
   
   psMetadata *where = psMetadataAlloc();
@@ -341,5 +341,5 @@
 {
   PS_ASSERT_PTR_NON_NULL(config, false);
-  PXOPT_LOOKUP_S64(remote_id,  config->args, "-remote_id",    true, false);
+  // PXOPT_LOOKUP_S64(remote_id,  config->args, "-remote_id",    true, false);
   
   psMetadata *where = psMetadataAlloc();
Index: /trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 36622)
+++ /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 36623)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/camera/pmReadoutFake.c
===================================================================
--- /trunk/psModules/src/camera/pmReadoutFake.c	(revision 36622)
+++ /trunk/psModules/src/camera/pmReadoutFake.c	(revision 36623)
@@ -27,4 +27,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/extras/pmThreadTools.c
===================================================================
--- /trunk/psModules/src/extras/pmThreadTools.c	(revision 36622)
+++ /trunk/psModules/src/extras/pmThreadTools.c	(revision 36623)
@@ -37,4 +37,5 @@
 #include "pmSourcePhotometry.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/extras/pmVisual.c
===================================================================
--- /trunk/psModules/src/extras/pmVisual.c	(revision 36622)
+++ /trunk/psModules/src/extras/pmVisual.c	(revision 36623)
@@ -41,4 +41,5 @@
 // #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+// #include "pmSourceLensing.h"
 // #include "pmSource.h"
 // #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/imcombine/pmPSFEnvelope.c
===================================================================
--- /trunk/psModules/src/imcombine/pmPSFEnvelope.c	(revision 36622)
+++ /trunk/psModules/src/imcombine/pmPSFEnvelope.c	(revision 36623)
@@ -27,4 +27,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/imcombine/pmSubtractionSimple.c
===================================================================
--- /trunk/psModules/src/imcombine/pmSubtractionSimple.c	(revision 36622)
+++ /trunk/psModules/src/imcombine/pmSubtractionSimple.c	(revision 36623)
@@ -34,4 +34,5 @@
 #include "pmSourceSatstar.h"
 
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/imcombine/pmSubtractionStamps.c
===================================================================
--- /trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 36622)
+++ /trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 36623)
@@ -26,4 +26,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/objects/Makefile.am
===================================================================
--- /trunk/psModules/src/objects/Makefile.am	(revision 36622)
+++ /trunk/psModules/src/objects/Makefile.am	(revision 36623)
@@ -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: /trunk/psModules/src/objects/mksource.pl
===================================================================
--- /trunk/psModules/src/objects/mksource.pl	(revision 36622)
+++ /trunk/psModules/src/objects/mksource.pl	(revision 36623)
@@ -19,4 +19,5 @@
 	       "PS1_V3", 3,
 	       "PS1_V4", 4,
+	       "PS1_V5", 5,
     );
 %cmfmodes_dv = ("PS1_DV1", 1,
Index: /trunk/psModules/src/objects/models/pmModel_DEV.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_DEV.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_DEV.c	(revision 36623)
@@ -43,4 +43,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_EXP.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_EXP.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_EXP.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_GAUSS.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_GAUSS.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_GAUSS.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_PGAUSS.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_PGAUSS.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_PGAUSS.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_PS1_V1.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_PS1_V1.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_PS1_V1.c	(revision 36623)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_QGAUSS.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_QGAUSS.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_QGAUSS.c	(revision 36623)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_RGAUSS.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_RGAUSS.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_RGAUSS.c	(revision 36623)
@@ -41,4 +41,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_SERSIC.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_SERSIC.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_SERSIC.c	(revision 36623)
@@ -50,4 +50,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/models/pmModel_TRAIL.c
===================================================================
--- /trunk/psModules/src/objects/models/pmModel_TRAIL.c	(revision 36622)
+++ /trunk/psModules/src/objects/models/pmModel_TRAIL.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmDetEff.c
===================================================================
--- /trunk/psModules/src/objects/pmDetEff.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmDetEff.c	(revision 36623)
@@ -24,4 +24,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmGrowthCurve.c
===================================================================
--- /trunk/psModules/src/objects/pmGrowthCurve.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmGrowthCurve.c	(revision 36623)
@@ -37,4 +37,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmGrowthCurveGenerate.c
===================================================================
--- /trunk/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmModelUtils.c
===================================================================
--- /trunk/psModules/src/objects/pmModelUtils.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmModelUtils.c	(revision 36623)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmMoments.h
===================================================================
--- /trunk/psModules/src/objects/pmMoments.h	(revision 36622)
+++ /trunk/psModules/src/objects/pmMoments.h	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmPCM_MinimizeChisq.c
===================================================================
--- /trunk/psModules/src/objects/pmPCM_MinimizeChisq.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPCM_MinimizeChisq.c	(revision 36623)
@@ -38,4 +38,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPCMdata.c
===================================================================
--- /trunk/psModules/src/objects/pmPCMdata.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPCMdata.c	(revision 36623)
@@ -38,4 +38,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPSF.c
===================================================================
--- /trunk/psModules/src/objects/pmPSF.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSF.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- /trunk/psModules/src/objects/pmPSF_IO.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSF_IO.c	(revision 36623)
@@ -54,4 +54,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPSFtry.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtry.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSFtry.c	(revision 36623)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPSFtryFitEXT.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtryFitEXT.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSFtryFitEXT.c	(revision 36623)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceUtils.h"
Index: /trunk/psModules/src/objects/pmPSFtryFitPSF.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtryFitPSF.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSFtryFitPSF.c	(revision 36623)
@@ -34,4 +34,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
@@ -120,5 +121,5 @@
     pmSourceVisualShowModelFits (psfTry->psf, psfTry->sources, maskVal);
 
-    psLogMsg ("psphot.psftry", PS_LOG_MINUTIA, "fit psf:   %f sec for %d of %ld sources\n", psTimerMark ("psf.fit"), Npsf, psfTry->sources->n);
+    psLogMsg ("psphot.psftry", PS_LOG_MINUTIA, "fit psf:   %f sec for %d of %ld sources (%d x %d model)\n", psTimerMark ("psf.fit"), Npsf, psfTry->sources->n, psfTry->psf->trendNx, psfTry->psf->trendNy);
     psTrace ("psModules.object", 3, "keeping %d of %ld PSF candidates (PSF)\n", Npsf, psfTry->sources->n);
 
Index: /trunk/psModules/src/objects/pmPSFtryMakePSF.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtryMakePSF.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSFtryMakePSF.c	(revision 36623)
@@ -35,4 +35,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPSFtryMetric.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtryMetric.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSFtryMetric.c	(revision 36623)
@@ -35,4 +35,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmPSFtryModel.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtryModel.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPSFtryModel.c	(revision 36623)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
@@ -111,10 +112,19 @@
 
     // set the max order (0 = constant) which the number of psf stars can support:
+    int MaxOrderForStars = 0;
+
+    // we require only 3 stars for n = 0, increase stars / cell for higher order
+    if (sources->n >=  16) MaxOrderForStars = 1; //  4 cells, 4 per cell
+    if (sources->n >=  54) MaxOrderForStars = 2; //  9 cells, 6 per cell
+    if (sources->n >= 128) MaxOrderForStars = 3; // 16 cells, 8 per cell
+    if (sources->n >= 300) MaxOrderForStars = 4; // 25 cells, 12 per cell
+    if (sources->n >  576) MaxOrderForStars = 5; // 36 cells, 16 per cell
+
     // rule of thumb: require 3 stars per 'cell' (order+1)^2
-    int MaxOrderForStars = 0;
-    if (sources->n >= 12) MaxOrderForStars = 1; // 4 cells
-    if (sources->n >= 27) MaxOrderForStars = 2; // 9 cells
-    if (sources->n >= 48) MaxOrderForStars = 3; // 16 cells
-    if (sources->n >  75) MaxOrderForStars = 4; // 25 cells
+    // if (sources->n >= 12) MaxOrderForStars = 1; // 4 cells
+    // if (sources->n >= 27) MaxOrderForStars = 2; // 9 cells
+    // if (sources->n >= 48) MaxOrderForStars = 3; // 16 cells
+    // if (sources->n >= 75) MaxOrderForStars = 4; // 25 cells
+    // if (sources->n > 108) MaxOrderForStars = 5; // 36 cells
 
     int orderMax = PS_MAX (options->psfTrendNx, options->psfTrendNy);
Index: /trunk/psModules/src/objects/pmPhotObj.c
===================================================================
--- /trunk/psModules/src/objects/pmPhotObj.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmPhotObj.c	(revision 36623)
@@ -36,4 +36,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/objects/pmSource.c
===================================================================
--- /trunk/psModules/src/objects/pmSource.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSource.c	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSource.h
===================================================================
--- /trunk/psModules/src/objects/pmSource.h	(revision 36622)
+++ /trunk/psModules/src/objects/pmSource.h	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSourceContour.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceContour.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceContour.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/objects/pmSourceExtendedPars.h
===================================================================
--- /trunk/psModules/src/objects/pmSourceExtendedPars.h	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceExtendedPars.h	(revision 36623)
@@ -118,5 +118,4 @@
 pmSourceGalaxyFits *pmSourceGalaxyFitsAlloc (void);
 
-
 /// @}
 # endif /* PM_SOURCE_H */
Index: /trunk/psModules/src/objects/pmSourceFitModel.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceFitModel.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceFitModel.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourcePhotometry.h"
Index: /trunk/psModules/src/objects/pmSourceFitPCM.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceFitPCM.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceFitPCM.c	(revision 36623)
@@ -38,4 +38,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourcePhotometry.h"
Index: /trunk/psModules/src/objects/pmSourceFitSet.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceFitSet.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceFitSet.c	(revision 36623)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourcePhotometry.h"
Index: /trunk/psModules/src/objects/pmSourceGroups.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceGroups.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceGroups.c	(revision 36623)
@@ -25,4 +25,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO.c	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSourceIO.h
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO.h	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO.h	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSourceIO_CFF.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_CFF.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_CFF.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_CMF.c.in
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_CMF.c.in	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_CMF.c.in	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
@@ -178,4 +179,37 @@
 	@>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:
+	if (source->lensingOBJ && source->lensingOBJ->smear) {
+	  @>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);
+	}
+
+	if (source->lensingOBJ && source->lensingOBJ->shear) {
+	  @>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); 
+	}
+
+	if (source->lensingOBJ && source->lensingPSF->smear) {
+	  @>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); 
+	}
+
+	if (source->lensingOBJ && source->lensingPSF->shear) {
+	  @>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: /trunk/psModules/src/objects/pmSourceIO_CMP.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_CMP.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_CMP.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_MatchedRefs.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_MatchedRefs.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_MatchedRefs.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_OBJ.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_OBJ.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_OBJ.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_PS1_CAL_0.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_PS1_CAL_0.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_PS1_CAL_0.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_0.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_0.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_0.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_RAW.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_RAW.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_RAW.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_SMPDATA.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_SMPDATA.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_SMPDATA.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceIO_SX.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO_SX.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceIO_SX.c	(revision 36623)
@@ -44,4 +44,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceLensing.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceLensing.c	(revision 36623)
+++ /trunk/psModules/src/objects/pmSourceLensing.c	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSourceLensing.h
===================================================================
--- /trunk/psModules/src/objects/pmSourceLensing.h	(revision 36623)
+++ /trunk/psModules/src/objects/pmSourceLensing.h	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSourceMatch.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceMatch.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceMatch.c	(revision 36623)
@@ -25,4 +25,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceMoments.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceMoments.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceMoments.c	(revision 36623)
@@ -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: /trunk/psModules/src/objects/pmSourceOutputs.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceOutputs.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceOutputs.c	(revision 36623)
@@ -33,4 +33,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourcePhotometry.c
===================================================================
--- /trunk/psModules/src/objects/pmSourcePhotometry.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourcePhotometry.c	(revision 36623)
@@ -40,4 +40,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourcePlotApResid.c
===================================================================
--- /trunk/psModules/src/objects/pmSourcePlotApResid.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourcePlotApResid.c	(revision 36623)
@@ -42,4 +42,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourcePlotMoments.c
===================================================================
--- /trunk/psModules/src/objects/pmSourcePlotMoments.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourcePlotMoments.c	(revision 36623)
@@ -45,4 +45,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourcePlotPSFModel.c
===================================================================
--- /trunk/psModules/src/objects/pmSourcePlotPSFModel.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourcePlotPSFModel.c	(revision 36623)
@@ -46,4 +46,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/objects/pmSourceSky.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceSky.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceSky.c	(revision 36623)
@@ -41,4 +41,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/objects/pmSourceUtils.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceUtils.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceUtils.c	(revision 36623)
@@ -41,4 +41,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 
Index: /trunk/psModules/src/objects/pmSourceVisual.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceVisual.c	(revision 36622)
+++ /trunk/psModules/src/objects/pmSourceVisual.c	(revision 36623)
@@ -23,4 +23,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSourceSatstar.h"
+#include "pmSourceLensing.h"
 #include "pmSource.h"
 #include "pmSourceFitModel.h"
Index: /trunk/psModules/src/psmodules.h
===================================================================
--- /trunk/psModules/src/psmodules.h	(revision 36622)
+++ /trunk/psModules/src/psmodules.h	(revision 36623)
@@ -135,4 +135,5 @@
 #include <pmSourceSatstar.h>
 #include <pmSourceDiffStats.h>
+#include <pmSourceLensing.h>
 #include <pmSource.h>
 #include <pmSourceFitModel.h>
Index: /trunk/psphot/src/Makefile.am
===================================================================
--- /trunk/psphot/src/Makefile.am	(revision 36622)
+++ /trunk/psphot/src/Makefile.am	(revision 36623)
@@ -208,4 +208,5 @@
 	psphotMakeGrowthCurve.c	       \
 	psphotMagnitudes.c	       \
+	psphotLensing.c	       \
 	psphotSetMaskBits.c	       \
 	psphotSkyReplace.c	       \
Index: /trunk/psphot/src/psphot.h
===================================================================
--- /trunk/psphot/src/psphot.h	(revision 36622)
+++ /trunk/psphot/src/psphot.h	(revision 36623)
@@ -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: /trunk/psphot/src/psphotApResid.c
===================================================================
--- /trunk/psphot/src/psphotApResid.c	(revision 36622)
+++ /trunk/psphot/src/psphotApResid.c	(revision 36623)
@@ -214,5 +214,5 @@
 
         // XXX make this user-configurable?
-        if (source->psfMagErr > 0.01) continue;
+        if (source->psfMagErr > 0.03) continue;
 
         // aperture residual for this source
@@ -267,10 +267,11 @@
 
     // 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
+    // we require only 3 stars for n = 0, increase stars / cell for higher order
     int MaxOrderForStars = 0;
-    if (Npsf >= 12) MaxOrderForStars = 1; // 4 cells
-    if (Npsf >= 27) MaxOrderForStars = 2; // 9 cells
-    if (Npsf >= 48) MaxOrderForStars = 3; // 16 cells
-    if (Npsf >  75) MaxOrderForStars = 4; // 25 cells
+    if (Npsf >=  16) MaxOrderForStars = 1; // 4 cells
+    if (Npsf >=  54) MaxOrderForStars = 2; // 9 cells
+    if (Npsf >= 128) MaxOrderForStars = 3; // 16 cells
+    if (Npsf >= 300) MaxOrderForStars = 4; // 25 cells
+    if (Npsf >  576) MaxOrderForStars = 5; // 36 cells
 
     pmTrend2DMode mode = PM_TREND_MAP;
Index: /trunk/psphot/src/psphotChoosePSF.c
===================================================================
--- /trunk/psphot/src/psphotChoosePSF.c	(revision 36622)
+++ /trunk/psphot/src/psphotChoosePSF.c	(revision 36623)
@@ -399,5 +399,5 @@
     char *modelName = pmModelClassGetName (psf->type);
     psLogMsg ("psphot.pspsf", PS_LOG_WARN, "select psf model: %f sec\n", psTimerMark ("psphot.choose.psf"));
-    psLogMsg ("psphot.pspsf", PS_LOG_INFO, "psf model %s, ApResid: %f +/- %f\n", modelName, psf->ApResid, psf->dApResid);
+    psLogMsg ("psphot.pspsf", PS_LOG_INFO, "psf model %s, ApResid: %f +/- %f (%d x %d model)\n", modelName, psf->ApResid, psf->dApResid, psf->trendNx, psf->trendNy);
 
     psFree (options);
Index: /trunk/psphot/src/psphotLensing.c
===================================================================
--- /trunk/psphot/src/psphotLensing.c	(revision 36623)
+++ /trunk/psphot/src/psphotLensing.c	(revision 36623)
@@ -0,0 +1,319 @@
+# 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.03) 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:
+    // we require only 3 stars for n = 0, increase stars / cell for higher order
+    int LensingTrendOrder = 0;
+    if (Npsf >=  16) LensingTrendOrder = 1; // 4 cells
+    if (Npsf >=  54) LensingTrendOrder = 2; // 9 cells
+    if (Npsf >= 128) LensingTrendOrder = 3; // 16 cells
+    if (Npsf >= 300) LensingTrendOrder = 4; // 25 cells
+    if (Npsf >  576) LensingTrendOrder = 5; // 36 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));
+    }
+
+    psLogMsg ("psphot.lensing", PS_LOG_DETAIL, "generate lensing maps using %d objects (%d x %d grid)\n", Npsf, Nx, Ny);
+
+    // 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];
+	pmPeak *peak = source->peak;
+	if (!peak) continue;
+
+	float xPos = peak->xf;
+	float yPos = peak->yf;
+
+	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);
+    }
+
+    psFree (trendX11sm);
+    psFree (trendX12sm);
+    psFree (trendX22sm);
+    psFree (trend_e1sm);
+    psFree (trend_e2sm);
+	              
+    psFree (trendX11sh);
+    psFree (trendX12sh);
+    psFree (trendX22sh);
+    psFree (trend_e1sh);
+    psFree (trend_e2sh);
+
+    psFree (psfX11sm);
+    psFree (psfX12sm);
+    psFree (psfX22sm);
+    psFree (psf_e1sm);
+    psFree (psf_e2sm);
+    psFree (psfX11sh);
+    psFree (psfX12sh);
+    psFree (psfX22sh);
+    psFree (psf_e1sh);
+    psFree (psf_e2sh);
+    psFree (xPos    );
+    psFree (yPos    );
+
+    psFree (stats);
+
+    psLogMsg ("psphot.lensing", PS_LOG_DETAIL, "calculate lensing parameters for %d objects: %f sec\n", (int) sources->n, psTimerMark ("psphot.lensing"));
+
+    return true;
+}
+
Index: /trunk/psphot/src/psphotReadout.c
===================================================================
--- /trunk/psphot/src/psphotReadout.c	(revision 36622)
+++ /trunk/psphot/src/psphotReadout.c	(revision 36623)
@@ -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();
     }
 
