Index: /branches/eam_branch_20070830/psModules/src/objects/pmTrend2D.c
===================================================================
--- /branches/eam_branch_20070830/psModules/src/objects/pmTrend2D.c	(revision 14777)
+++ /branches/eam_branch_20070830/psModules/src/objects/pmTrend2D.c	(revision 14778)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-09-04 18:35:51 $
+ *  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-07 20:24:34 $
  *
  *  Copyright 2004 Institute for Astronomy, University of Hawaii
@@ -22,4 +22,5 @@
         return;
 
+    psFree (trend->stats);
     psFree (trend->poly);
     psFree (trend->map);
@@ -27,5 +28,5 @@
 }
 
-pmTrend2D *pmTrend2DAlloc (pmTrend2DMode mode, int nXout, int nYout, int nXtrend, int nYtrend, psStats *stats)
+pmTrend2D *pmTrend2DAlloc (pmTrend2DMode mode, psImage *image, int nXtrend, int nYtrend, psStats *stats)
 {
     pmTrend2D *trend = (pmTrend2D *) psAlloc(sizeof(pmTrend2D));
@@ -35,4 +36,5 @@
     trend->poly = NULL;
     trend->stats = psMemIncrRefCounter (stats);
+    trend->mode = mode;
 	
     switch (mode) {
@@ -45,17 +47,16 @@
 	break;
 
-      case PM_TREND_MAP:
-	// XXX *** I need the output image dimensions here!
+      case PM_TREND_MAP: {
+	  // binning defines the map scale relationship
+	  psImageBinning *binning = psImageBinningAlloc();
+	  binning->nXfine = image->numCols;
+	  binning->nYfine = image->numRows;
+	  binning->nXruff = nXtrend;
+	  binning->nYruff = nYtrend;
 
-	// function is defined over the range 0-1000, 0-1000
-	psImageBinning *binning = psImageBinningAlloc();
-	binning->nXfine = nXout;
-	binning->nYfine = nYout;
-	binning->nXruff = nXtrend;
-	binning->nYruff = nYtrend;
-
-	trend->map = psImageMapAlloc (NULL, binning, stats);
-	psFree (binning);
-	break;
+	  trend->map = psImageMapAlloc (image, binning, stats);
+	  psFree (binning);
+	  break;
+      }
 
       default:
@@ -64,2 +65,109 @@
     return (trend);
 }
+
+pmTrend2D *pmTrend2DFieldAlloc (pmTrend2DMode mode, int nXfield, int nYfield, int nXtrend, int nYtrend, psStats *stats)
+{
+    pmTrend2D *trend = (pmTrend2D *) psAlloc(sizeof(pmTrend2D));
+    psMemSetDeallocator(trend, (psFreeFunc) pmTrend2DFree);
+
+    trend->map = NULL;
+    trend->poly = NULL;
+    trend->stats = psMemIncrRefCounter (stats);
+    trend->mode = mode;
+	
+    switch (mode) {
+      case PM_TREND_POLY_ORD:
+	trend->poly = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXtrend, nYtrend);
+	break;
+
+      case PM_TREND_POLY_CHEB:
+	trend->poly = psPolynomial2DAlloc (PS_POLYNOMIAL_CHEB, nXtrend, nYtrend);
+	break;
+
+      case PM_TREND_MAP: {
+	  // binning defines the map scale relationship
+	  psImageBinning *binning = psImageBinningAlloc();
+	  binning->nXfine = nXfield;
+	  binning->nYfine = nYfield;
+	  binning->nXruff = nXtrend;
+	  binning->nYruff = nYtrend;
+
+	  trend->map = psImageMapAlloc (NULL, binning, stats);
+	  psFree (binning);
+	  break;
+      }
+
+      default:
+	psAbort ("error");
+    }
+    return (trend);
+}
+
+bool pmTrend2DFit (pmTrend2D *trend, psVector *x, psVector *y, psVector *f, psVector *df) {
+
+    bool status;
+
+    psVector *mask = psVectorAlloc (x->n, PS_TYPE_MASK);
+
+    switch (trend->mode) {
+      case PM_TREND_POLY_ORD:
+      case PM_TREND_POLY_CHEB:
+        status = psVectorClipFitPolynomial2D (trend->poly, trend->stats, mask, 0xff, f, df, x, y);
+	// we can use the API here which adjusts the polynomial order based on the number
+	// of points in the image, and potentially based on the fractional range of the
+	// data?
+	break;
+
+      case PM_TREND_MAP:
+	// XXX supply fraction from trend elements
+	status = psImageMapGenerateScale (trend->map, x, y, f, df, 0.1);
+	break;
+
+      default:
+	psAbort ("error");
+    }
+    psFree (mask);
+    return status;
+}
+
+double pmTrend2DEval (pmTrend2D *trend, float x, float y) {
+
+    double result;
+
+    if (!trend) return 0.0;
+
+    switch (trend->mode) {
+      case PM_TREND_POLY_ORD:
+      case PM_TREND_POLY_CHEB:
+	result = psPolynomial2DEval (trend->poly, x, y);
+	break;
+
+      case PM_TREND_MAP:
+	result = psImageMapEval (trend->map, x, y);
+	break;
+
+      default:
+	psAbort ("error");
+    }
+    return result;
+}
+
+psVector *pmTrend2DEvalVector (pmTrend2D *trend, psVector *x, psVector *y) {
+
+    psVector *result;
+
+    switch (trend->mode) {
+      case PM_TREND_POLY_ORD:
+      case PM_TREND_POLY_CHEB:
+	result = psPolynomial2DEvalVector (trend->poly, x, y);
+	break;
+
+      case PM_TREND_MAP:
+	result = psImageMapEvalVector (trend->map, x, y);
+	break;
+
+      default:
+	psAbort ("error");
+    }
+    return result;
+}
Index: /branches/eam_branch_20070830/psModules/src/objects/pmTrend2D.h
===================================================================
--- /branches/eam_branch_20070830/psModules/src/objects/pmTrend2D.h	(revision 14777)
+++ /branches/eam_branch_20070830/psModules/src/objects/pmTrend2D.h	(revision 14778)
@@ -5,6 +5,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.1.2.1 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-09-04 18:35:51 $
+ * @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-07 20:24:34 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -17,21 +17,28 @@
 
 typedef enum {
-  PM_TREND_POLY_ORD,
-  PM_TREND_POLY_CHEB,
-  PM_TREND_MAP,
+    PM_TREND_POLY_ORD,
+    PM_TREND_POLY_CHEB,
+    PM_TREND_MAP,
 } pmTrend2DMode;
 
 typedef struct {
-  psPolynomial2D *poly;
-  psImageMap *map;
-  pmTrend2DMode mode; // POLY_ORD, POLY_CHEB, MAP
+    psPolynomial2D *poly;
+    psImageMap *map;
+    psStats *stats;
+    pmTrend2DMode mode; // POLY_ORD, POLY_CHEB, MAP
 } pmTrend2D;
 
-// allocate a pmTrend2D structure.  nX,nY is order for the polynomials, max number of grid cells for
+// allocate a pmTrend2D structure tied to an image dimensions.  nXtrend,nYtrend is the order for the polynomials, max number of grid cells for
 // psImageMap
-pmTrend2D *pmTrend2DAlloc (pmTrend2DMode mode, int nXout, int nYout, int nXtrend, int nYtrend, psStats *stats);
+pmTrend2D *pmTrend2DAlloc (pmTrend2DMode mode, psImage *image, int nXtrend, int nYtrend, psStats *stats);
+
+// allocate a pmTrend2D tied to an abstract field with size nXfield,nYfield 
+pmTrend2D *pmTrend2DFieldAlloc (pmTrend2DMode mode, int nXfield, int nYfield, int nXtrend, int nYtrend, psStats *stats);
 
 bool pmTrend2DFit (pmTrend2D *trend, psVector *x, psVector *y, psVector *f, psVector *df);
 
+double pmTrend2DEval (pmTrend2D *trend, float x, float y);
+psVector *pmTrend2DEvalVector (pmTrend2D *trend, psVector *x, psVector *y);
+
 /// @}
 # endif
