Index: trunk/psModules/src/objects/pmPSF.c
===================================================================
--- trunk/psModules/src/objects/pmPSF.c	(revision 14961)
+++ trunk/psModules/src/objects/pmPSF.c	(revision 15000)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-09-21 02:46:25 $
+ *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-24 21:27:58 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -41,26 +41,38 @@
 
 /*****************************************************************************/
-/* DEFINE STATEMENTS                                                         */
-/*****************************************************************************/
-
-/*****************************************************************************/
-/* TYPE DEFINITIONS                                                          */
-/*****************************************************************************/
-
-/*****************************************************************************/
-/* GLOBAL VARIABLES                                                          */
-/*****************************************************************************/
-
-/*****************************************************************************/
-/* FILE STATIC VARIABLES                                                     */
-/*****************************************************************************/
-
-/*****************************************************************************/
-/* FUNCTION IMPLEMENTATION - LOCAL                                           */
-/*****************************************************************************/
-
-/*****************************************************************************/
 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
 /*****************************************************************************/
+
+static void pmPSFOptionsFree (pmPSFOptions *options) {
+
+    if (!options) return;
+
+    psFree (options->stats);
+    return;
+}
+
+pmPSFOptions *pmPSFOptionsAlloc () {
+
+    pmPSFOptions *options = (pmPSFOptions *) psAlloc(sizeof(pmPSFOptions));
+    psMemSetDeallocator(options, (psFreeFunc) pmPSFOptionsFree);
+
+    options->type          = 0;
+
+    options->stats         = NULL;
+
+    options->psfTrendMode  = PM_TREND_NONE;
+    options->psfTrendNx    = 0;
+    options->psfTrendNy    = 0;
+    options->psfFieldNx    = 0;
+    options->psfFieldNy    = 0;
+    options->psfFieldXo    = 0;
+    options->psfFieldYo    = 0;
+
+    options->poissonErrorsPhotLMM = true;
+    options->poissonErrorsPhotLin = false;
+    options->poissonErrorsParams  = true;
+
+    return options;
+}
 
 /*****************************************************************************
@@ -74,4 +86,5 @@
 
     psFree (psf->ChiTrend);
+    psFree (psf->psfTrendStats);
     psFree (psf->ApTrend);
     psFree (psf->FluxScale);
@@ -94,11 +107,12 @@
  Object Normalization
  *****************************************************************************/
-pmPSF *pmPSFAlloc (pmModelType type, bool poissonErrors, psPolynomial2D *psfTrendMask)
+pmPSF *pmPSFAlloc (pmPSFOptions *options)
 {
     int Nparams;
 
     pmPSF *psf = (pmPSF *) psAlloc(sizeof(pmPSF));
-
-    psf->type     = type;
+    psMemSetDeallocator(psf, (psFreeFunc) pmPSFFree);
+
+    psf->type     = options->type;
     psf->chisq    = 0.0;
     psf->ApResid  = 0.0;
@@ -108,5 +122,8 @@
     psf->nPSFstars  = 0;
     psf->nApResid   = 0;
-    psf->poissonErrors = poissonErrors;
+
+    psf->poissonErrorsPhotLMM = options->poissonErrorsPhotLMM;
+    psf->poissonErrorsPhotLin = options->poissonErrorsPhotLin;
+    psf->poissonErrorsParams = options->poissonErrorsParams;
 
     // the ApTrend components are (x, y).  It may be represented with a polynomial or with a
@@ -121,5 +138,5 @@
     psf->FluxScale = NULL;
 
-    if (psf->poissonErrors) {
+    if (psf->poissonErrorsPhotLMM) {
         psf->ChiTrend = psPolynomial1DAlloc (PS_POLYNOMIAL_ORD, 1);
     } else {
@@ -133,5 +150,5 @@
     psf->residuals = NULL;
 
-    Nparams = pmModelClassParameterCount (type);
+    Nparams = pmModelClassParameterCount (options->type);
     if (!Nparams) {
         psError(PS_ERR_UNKNOWN, true, "Undefined pmModelType");
@@ -139,4 +156,7 @@
     }
     psf->params = psArrayAlloc(Nparams);
+    
+    // save the trend stats on the psf for use in pmPSFFromPSFtry
+    psf->psfTrendStats = psMemIncrRefCounter (options->stats);
 
     // the order of the PSF parameter (X,Y) fits is determined by the psfTrendMask polynomial
@@ -147,27 +167,30 @@
     // PM_PAR_XPOS, etc)
 
-    if (psfTrendMask) {
+    psImageBinning *binning = psImageBinningAlloc();
+    binning->nXruff = options->psfTrendNx;
+    binning->nYruff = options->psfTrendNy;
+    binning->nXfine = options->psfFieldNx;
+    binning->nYfine = options->psfFieldNy;
+    psImageBinningSetScale (binning, PS_IMAGE_BINNING_CENTER);
+    psImageBinningSetSkipByOffset (binning, options->psfFieldXo, options->psfFieldYo);
+
+    psf->fieldNx = options->psfFieldNx;
+    psf->fieldNy = options->psfFieldNy;
+    psf->fieldXo = options->psfFieldXo;
+    psf->fieldYo = options->psfFieldYo;
+
+    // define the parameter trends
+    if (options->psfTrendMode != PM_TREND_NONE) {
         for (int i = 0; i < psf->params->n; i++) {
-            if (i == PM_PAR_SKY)
-                continue;
-            if (i == PM_PAR_I0)
-                continue;
-            if (i == PM_PAR_XPOS)
-                continue;
-            if (i == PM_PAR_YPOS)
-                continue;
-
-            psPolynomial2D *param = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, psfTrendMask->nX, psfTrendMask->nY);
-            for (int nx = 0; nx < param->nX + 1; nx++) {
-                for (int ny = 0; ny < param->nY + 1; ny++) {
-                    param->mask[nx][ny] = psfTrendMask->mask[nx][ny];
-                }
-            }
-            psf->params->data[i] = param;
+            if (i == PM_PAR_SKY) continue;
+            if (i == PM_PAR_I0) continue;
+            if (i == PM_PAR_XPOS) continue;
+            if (i == PM_PAR_YPOS) continue;
+
+            psf->params->data[i] = pmTrend2DNoImageAlloc (options->psfTrendMode, binning, options->stats);
         }
     }
-
-    psMemSetDeallocator(psf, (psFreeFunc) pmPSFFree);
-    return(psf);
+    psFree (binning);
+    return psf;
 }
 
@@ -300,7 +323,11 @@
     va_start(ap, sxy);
 
-    pmModelType type = pmModelClassGetType (typeName);
-    psPolynomial2D *psfTrend = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, 0, 0);
-    pmPSF *psf = pmPSFAlloc (type, true, psfTrend);
+    pmPSFOptions *options = pmPSFOptionsAlloc ();
+    options->type = pmModelClassGetType (typeName);
+    options->psfTrendMode = PM_TREND_POLY_ORD;
+    options->psfTrendNx = 0;
+    options->psfTrendNy = 0;
+
+    pmPSF *psf = pmPSFAlloc (options);
 
     psVector *par = psVectorAlloc (psf->params->n, PS_TYPE_F32);
@@ -311,23 +338,24 @@
     psEllipsePol pol = pmPSF_ModelToFit(par->data.F32);
 
+    pmTrend2D *trend = NULL;
+
     // set the psf shape parameters
-    psPolynomial2D *poly = NULL;
-    poly = psf->params->data[PM_PAR_E0];
-    poly->coeff[0][0] = pol.e0;
-
-    poly = psf->params->data[PM_PAR_E1];
-    poly->coeff[0][0] = pol.e1;
-
-    poly = psf->params->data[PM_PAR_E2];
-    poly->coeff[0][0] = pol.e2;
+    trend = psf->params->data[PM_PAR_E0];
+    trend->poly->coeff[0][0] = pol.e0;
+
+    trend = psf->params->data[PM_PAR_E1];
+    trend->poly->coeff[0][0] = pol.e1;
+
+    trend = psf->params->data[PM_PAR_E2];
+    trend->poly->coeff[0][0] = pol.e2;
 
     for (int i = PM_PAR_SXY + 1; i < psf->params->n; i++) {
-        poly = psf->params->data[i];
-        poly->coeff[0][0] = (psF32)va_arg(ap, psF64);
+	trend = psf->params->data[i];
+        trend->poly->coeff[0][0] = (psF32)va_arg(ap, psF64);
     }
     va_end(ap);
 
     psFree (par);
-    psFree (psfTrend);
+    psFree (options);
     return psf;
 }
