Index: trunk/psModules/src/imsubtract/pmSubtractSky.c
===================================================================
--- trunk/psModules/src/imsubtract/pmSubtractSky.c	(revision 5294)
+++ trunk/psModules/src/imsubtract/pmSubtractSky.c	(revision 5516)
@@ -6,6 +6,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-10-12 21:02:04 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-15 20:09:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -18,6 +18,10 @@
 #include<math.h>
 #include "pslib.h"
-#include "psConstants.h"
 #include "pmSubtractSky.h"
+
+// XXX: Get rid of the.  Create pmUtils.h
+psImage *p_psDetermineTrimmedImage(
+    pmReadout *in
+);
 
 /******************************************************************************
@@ -152,4 +156,6 @@
  
 XXX: Use your brain and figure out the analytical expression.
+ 
+XXX: Why isn't it simply (xOrder+1) * (yOrder+1)?
  *****************************************************************************/
 static psS32 CalculatePolyTerms(psS32 xOrder, psS32 yOrder)
@@ -170,8 +176,9 @@
         }
     }
-
     psTrace("SubtractSky.CalculatePolyTerms", 4,
             "Exiting CalculatePolyTerms(%d, %d) -> %d\n", xOrder, yOrder, localPolyTerms);
     return(localPolyTerms);
+
+    //    return((xOrder+1) * (yOrder+1));
 }
 
@@ -283,7 +290,8 @@
 XXX: Different trace message facilities in use here.
  *****************************************************************************/
-static psPolynomial2D *ImageFitPolynomial(psPolynomial2D *myPoly,
-        psImage *dataImage,
-        psImage *maskImage)
+static psPolynomial2D *ImageFitPolynomial(
+    psPolynomial2D *myPoly,
+    psImage *dataImage,
+    psImage *maskImage)
 {
     psTrace("SubtractSky.ImageFitPolynomial", 4,
@@ -471,4 +479,8 @@
     PS_ASSERT_READOUT_NON_EMPTY(in, NULL);
     PS_ASSERT_READOUT_TYPE(in, PS_TYPE_F32, NULL);
+    PS_WARN_PTR_NON_NULL(in->parent);
+    if (in->parent != NULL) {
+        PS_WARN_PTR_NON_NULL(in->parent->concepts);
+    }
     psTrace(".psModule.pmSubtractSky", 4,
             "---- pmSubtractSky() begin ----\n");
@@ -492,5 +504,10 @@
         return(in);
     }
-    psImage *origImage = in->image;
+
+    //
+    // Determine trimmed image from metadata.
+    //
+
+    psImage *trimmedImg = p_psDetermineTrimmedImage(in);
     psImage *binnedImage = NULL;
     psPolynomial2D *myPoly = NULL;
@@ -539,5 +556,5 @@
         // No binning is required here.  Simply create a copy of the image
         // and a mask.
-        binnedImage = psImageCopy(binnedImage, origImage, PS_TYPE_F32);
+        binnedImage = psImageCopy(binnedImage, trimmedImg, PS_TYPE_F32);
         if (binnedImage == NULL) {
             psError(PS_ERR_UNKNOWN, false, "psImageCopy() returned NULL.  Returning in image.\n");
@@ -559,5 +576,5 @@
         }
     } else {
-        binnedImage = psImageRebin(NULL, origImage, in->mask, 0, binFactor, stats);
+        binnedImage = psImageRebin(NULL, trimmedImg, in->mask, 0, binFactor, stats);
         if (binnedImage == NULL) {
             psError(PS_ERR_UNKNOWN, false, "psImageRebin() returned NULL.  Returning in image.\n");
@@ -677,12 +694,12 @@
     if (binFactor <= 1) {
         // The binned image is the same size as the original image.
-        for (psS32 row = 0; row < origImage->numRows ; row++) {
-            for (psS32 col = 0; col < origImage->numCols ; col++) {
-                origImage->data.F32[row][col]-= binnedImage->data.F32[row][col];
+        for (psS32 row = 0; row < trimmedImg->numRows ; row++) {
+            for (psS32 col = 0; col < trimmedImg->numCols ; col++) {
+                trimmedImg->data.F32[row][col]-= binnedImage->data.F32[row][col];
             }
         }
     } else {
-        for (psS32 row = 0; row < origImage->numRows ; row++) {
-            for (psS32 col = 0; col < origImage->numCols ; col++) {
+        for (psS32 row = 0; row < trimmedImg->numRows ; row++) {
+            for (psS32 col = 0; col < trimmedImg->numCols ; col++) {
                 // We calculate the F32 value of the pixel coordinates in the
                 // binned image and then use a pixel interpolation routine to
@@ -700,5 +717,5 @@
                                      binnedImage, binColF64, binRowF64,
                                      NULL, 0, 0.0, PS_INTERPOLATE_BILINEAR);
-                origImage->data.F32[row][col]-= binPixel;
+                trimmedImg->data.F32[row][col]-= binPixel;
 
                 psTrace(".psModule.pmSubtractSky", 8,
