Index: trunk/psModules/src/objects/pmPSF.c
===================================================================
--- trunk/psModules/src/objects/pmPSF.c	(revision 14846)
+++ trunk/psModules/src/objects/pmPSF.c	(revision 14936)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-08-24 00:11:02 $
+ *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-21 00:05:35 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -29,4 +29,5 @@
 #include "pmResiduals.h"
 #include "pmGrowthCurve.h"
+#include "pmTrend2D.h"
 #include "pmPSF.h"
 #include "pmModel.h"
@@ -108,10 +109,8 @@
     psf->poissonErrors = poissonErrors;
 
-    // the ApTrend components are (x, y, r2rflux, flux)
-    // the ApTrend order is defined by the masking
-    // initially we set it to SKYBIAS (no X,Y variation)
-    // this is adjusted in psphotApResid based on the user's choices
-    psf->ApTrend = psPolynomial4DAlloc (PS_POLYNOMIAL_ORD, 2, 2, 1, 1);
-    pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
+    // the ApTrend components are (x, y).  It may be represented with a polynomial or with a
+    // psImageMap.  We set it initially to NULL. the user must allocate it before using.
+    // psf->ApTrend = pmTrend2DAlloc (PM_TREND_MAP, Nx, Ny, 1, 1, stats);
+    psf->ApTrend = NULL;
 
     if (psf->poissonErrors) {
@@ -166,21 +165,4 @@
 }
 
-// zero and mask out all terms:
-static bool maskAllApTerms (psPolynomial4D *trend)
-{
-
-    for (int i = 0; i < trend->nX + 1; i++) {
-        for (int j = 0; j < trend->nY + 1; j++) {
-            for (int k = 0; k < trend->nZ + 1; k++) {
-                for (int m = 0; m < trend->nT + 1; m++) {
-                    trend->mask[i][j][k][m] = 1;  // mask coeff
-                    trend->coeff[i][j][k][m] = 0;  // zero coeff
-                }
-            }
-        }
-    }
-    return true;
-}
-
 // the PSF models the \sigma_{xy} variation of the elliptical contour as a function of position in the image with a
 // polynomial.  an individual object has a contour of the form (x^2/2sx^2) + (y^2/2sy^2) + sxy*x*y
@@ -344,113 +326,4 @@
 }
 
-/***********************************************
- * this function masks the psf.ApTrend polynomial
- * to enable the specific subset of the coefficients
- **********************************************/
-bool pmPSFMaskApTrend (psPolynomial4D *trend,
-                       pmPSFApTrendOptions option)
-{
-
-    switch (option) {
-    case PM_PSF_APTREND_NONE:
-        maskAllApTerms (trend);
-        return true;
-
-    case PM_PSF_APTREND_CONSTANT:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        return true;
-
-    case PM_PSF_APTREND_SKYBIAS:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[0][0][1][0] = 0;  // unmask skybias
-        return true;
-
-    case PM_PSF_APTREND_SKYSAT:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[0][0][1][0] = 0;  // unmask skybias
-        trend->mask[0][0][0][1] = 0;  // unmask skysat
-        return true;
-
-    case PM_PSF_APTREND_XY_LIN:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[1][0][0][0] = 0;  // unmask x
-        trend->mask[0][1][0][0] = 0;  // unmask y
-        return true;
-
-    case PM_PSF_APTREND_XY_QUAD:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[1][0][0][0] = 0;  // unmask x
-        trend->mask[2][0][0][0] = 0;  // unmask x^2
-        trend->mask[1][1][0][0] = 0;  // unmask x y
-        trend->mask[0][1][0][0] = 0;  // unmask y
-        trend->mask[0][2][0][0] = 0;  // unmask y^2
-        return true;
-
-    case PM_PSF_APTREND_SKY_XY_LIN:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[1][0][0][0] = 0;  // unmask x
-        trend->mask[0][1][0][0] = 0;  // unmask y
-        trend->mask[0][0][1][0] = 0;  // unmask skybias
-        return true;
-
-    case PM_PSF_APTREND_SKYSAT_XY_LIN:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[1][0][0][0] = 0;  // unmask x
-        trend->mask[0][1][0][0] = 0;  // unmask y
-        trend->mask[0][0][1][0] = 0;  // unmask skybias
-        trend->mask[0][0][0][1] = 0;  // unmask skysat
-        return true;
-
-    case PM_PSF_APTREND_ALL:
-    default:
-        maskAllApTerms (trend);
-        trend->mask[0][0][0][0] = 0;  // unmask constant
-        trend->mask[0][0][1][0] = 0;  // unmask skybias
-        trend->mask[0][0][0][1] = 0;  // unmask skysat
-
-        trend->mask[1][0][0][0] = 0;  // unmask x
-        trend->mask[2][0][0][0] = 0;  // unmask x^2
-        trend->mask[1][1][0][0] = 0;  // unmask x y
-        trend->mask[0][1][0][0] = 0;  // unmask y
-        trend->mask[0][2][0][0] = 0;  // unmask y^2
-        return true;
-    }
-    return false;
-}
-
-pmPSFApTrendOptions pmPSFApTrendOptionFromName (char *name)
-{
-
-    // no correction
-    if (!strcasecmp (name, "NONE"))
-        return PM_PSF_APTREND_NONE;
-    if (!strcasecmp (name, "CONSTANT"))
-        return PM_PSF_APTREND_CONSTANT;
-    if (!strcasecmp (name, "SKYBIAS"))
-        return PM_PSF_APTREND_SKYBIAS;
-    if (!strcasecmp (name, "SKYSAT"))
-        return PM_PSF_APTREND_SKYSAT;
-    if (!strcasecmp (name, "XY_LIN"))
-        return PM_PSF_APTREND_XY_LIN;
-    if (!strcasecmp (name, "XY_QUAD"))
-        return PM_PSF_APTREND_XY_QUAD;
-    if (!strcasecmp (name, "SKY_XY_LIN"))
-        return PM_PSF_APTREND_SKY_XY_LIN;
-    if (!strcasecmp (name, "SKYSAT_XY_LIN"))
-        return PM_PSF_APTREND_SKYSAT_XY_LIN;
-    if (!strcasecmp (name, "ALL"))
-        return PM_PSF_APTREND_ALL;
-
-    psError(PS_ERR_UNKNOWN, true, "Unknown APTREND value: %s", name);
-    return PM_PSF_APTREND_ERROR;
-}
-
 // we generate the growth curve for the center of the image with the specified psf model
 bool pmGrowthCurveGenerate (pmReadout *readout, pmPSF *psf, bool ignore, psMaskType maskVal, psMaskType mark)
Index: trunk/psModules/src/objects/pmPSF.h
===================================================================
--- trunk/psModules/src/objects/pmPSF.h	(revision 14846)
+++ trunk/psModules/src/objects/pmPSF.h	(revision 14936)
@@ -6,6 +6,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-08-24 00:11:02 $
+ * @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-21 00:05:35 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -49,6 +49,7 @@
     pmModelType type;			///< PSF Model in use
     psArray *params;			///< Model parameters (psPolynomial2D)
-    psPolynomial1D *ChiTrend;         ///< Chisq vs flux fit (correction for systematic errors)
-    psPolynomial4D *ApTrend;            ///< ApResid vs (x,y,rflux) (rflux = ten(0.4*mInst))
+    psPolynomial1D *ChiTrend;           ///< Chisq vs flux fit (correction for systematic errors)
+    pmTrend2D *ApTrend;                 ///< ApResid vs (x,y)
+    pmTrend2D *FluxScale;               ///< Flux for PSF at (x,y) for normalization = 1.0
     float ApResid;                      ///< apMag - psfMag (for PSF stars)
     float dApResid;                     ///< scatter of ApResid
Index: trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.c	(revision 14846)
+++ trunk/psModules/src/objects/pmPSF_IO.c	(revision 14936)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-08-24 00:11:02 $
+ *  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-21 00:05:57 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -40,4 +40,5 @@
 #include "pmGrowthCurve.h"
 #include "pmResiduals.h"
+#include "pmTrend2D.h"
 #include "pmPSF.h"
 #include "pmModel.h"
@@ -354,5 +355,5 @@
 
     // XXX save the growth curve
-    // XXX save the ApResid fit
+    // XXX save ApTrend (as image?)
 
 # if (0)
@@ -636,5 +637,8 @@
         psPolynomial2DtoMetadata (metadata, poly, "PSF_PAR%02d", i);
     }
-    psPolynomial4DtoMetadata (metadata, psf->ApTrend, "APTREND");
+
+    // XXX fix this
+    psWarning ("APTREND is currently missing");
+    // psPolynomial4DtoMetadata (metadata, psf->ApTrend, "APTREND");
 
     psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_AP_RESID", PS_DATA_F32, "aperture residual", psf->ApResid);
@@ -681,4 +685,9 @@
         psf->params->data[i] = poly;
     }
+
+    // load the APTREND data
+    // XXX fix this to work with pmTrend2D
+    psWarning ("APTREND is not being read");
+    # if (0)
     sprintf (keyword, "APTREND");
     psMetadata *folder = psMetadataLookupPtr (&status, metadata, keyword);
@@ -686,4 +695,5 @@
     psFree (psf->ApTrend);
     psf->ApTrend = poly;
+    # endif
 
     psf->ApResid = psMetadataLookupF32 (&status, metadata, "PSF_AP_RESID");
