Index: trunk/stac/src/stac.c
===================================================================
--- trunk/stac/src/stac.c	(revision 3660)
+++ trunk/stac/src/stac.c	(revision 3666)
@@ -2,4 +2,5 @@
 #include "pslib.h"
 #include "stac.h"
+#include "stacConfig.h"
 #include <sys/time.h>
 
@@ -47,8 +48,8 @@
 
     // Read input files
-    psArray *inputs = stacReadImages(config);
+    psArray *inputs = stacReadImages(config->inputs);
 
     // Read maps
-    psArray *maps = stacReadMaps(config);
+    psArray *maps = stacReadMaps(config->inputs);
 
     psTrace("stac.time", 1, "I/O completed at %f seconds\n", getTime() - startTime);
@@ -56,12 +57,25 @@
     // Get size, if not input
     if (config->outnx == 0 || config->outny == 0) {
-	stacSize(config, inputs, maps);
+	stacSize(&config->outnx, &config->outny, &config->xMapDiff, &config->yMapDiff, inputs, maps);
     }
 
     // Invert maps
-    psArray *inverseMaps = stacInvertMaps(maps, config);
+    psArray *inverseMaps = stacInvertMaps(maps, config->outnx, config->outny);
 
     // Generate errors
-    psArray *errors = stacErrorImages(inputs, config);
+    psArray *errors = stacErrorImages(inputs, config->gain, config->readnoise);
+#ifdef TESTING
+    // Write error images out to check
+    for (int i = 0; i < inputs->n; i++) {
+	char errName[MAXCHAR];		// Filename of error image
+	sprintf(errName,"%s.err",config->inputs->data[i]);
+	psFits *errorFile = psFitsAlloc(errName);
+	if (!psFitsWriteImage(errorFile, NULL, error, 0, NULL)) {
+	    psErrorStackPrint(stderr, "Unable to write image: %s\n", errName);
+	}
+	psTrace("stac", 1, "Error image written to %s\n", errName);
+	psFree(errorFile);
+    }
+#endif
 
     // Transform inputs and errors
@@ -69,22 +83,71 @@
     psArray *transformedErrors = NULL;
     (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, NULL, NULL, NULL, NULL,
-			config);
-
+			config->outnx, config->outny);
     psTrace("stac.time",1,"Transformation completed at %f seconds\n", getTime() - startTime);
+
+#ifdef TESTING
+    // Write transformed images out to check
+    for (int i = 0; i < inputs->n; i++) {
+	char shiftName[MAXCHAR];	// Filename of shift image
+	char errName[MAXCHAR];		// Filename of error image
+	sprintf(shiftName,"%s.shift1",config->inputs->data[n],numTransforms);
+	sprintf(errName,"%s.shifterr1",config->inputs->data[n],numTransforms);
+	psTrace("stac.transform.test", 6,
+		"Output files have size: %dx%d\n",outImage->numCols,outImage->numRows);
+	psFits *shiftFile = psFitsAlloc(shiftName);
+	psFits *errFile = psFitsAlloc(errName);
+	if (!psFitsWriteImage(shiftFile, NULL, outImage, 0, NULL)) {
+	    psErrorStackPrint(stderr, "Unable to write image: %s\n", shiftName);
+	}
+	psTrace("stac", 1, "Shifted image written to %s\n", shiftName);
+	if (!psFitsWriteImage(errFile, NULL, outError, 0, NULL)) {
+	    psErrorStackPrint(stderr, "Unable to write image: %s\n", errName);
+	}
+	psTrace("stac", 1, "Shifted error image written to %s\n", errName);
+	psFree(shiftFile);
+	psFree(errFile);
+    }
+#endif
+
+
 
     psVector *scales = NULL;		// Relative scales between images
     psVector *offsets = NULL;		// Offsets between images
-    (void)stacScales(&scales, &offsets, transformed, config);
+    (void)stacScales(&scales, &offsets, transformed, config->starFile, config->starMapFile, config->xMapDiff,
+		     config->yMapDiff, config->aper);
     // Rescale the images
     (void)stacRescale(transformed, transformedErrors, NULL, scales, offsets);
+    // Set the saturation and bad values
+    psVector *saturated = psVectorAlloc(transformed->n, PS_TYPE_F32); // Saturation limits
+    psVector *bad = psVectorAlloc(transformed->n, PS_TYPE_F32);	// Bad limits
+    for (int i = 0; i < transformed->n; i++) {
+	saturated->data.F32[i] = (config->saturated - offsets->data.F32[i]) / scales->data.F32[i];
+	bad->data.F32[i] = (config->bad - offsets->data.F32[i]) / scales->data.F32[i];
+    }
 
     // Combine with rejection
     psArray *rejected = NULL;
     psImage *combined = NULL;
-    (void)stacCombine(&combined, &rejected, transformed, transformedErrors, config->nReject, NULL, config);
+    (void)stacCombine(&combined, &rejected, transformed, transformedErrors, config->nReject, NULL, saturated,
+		      bad, config->reject);
 
     psTrace("stac.time",1,"First combination completed at %f seconds\n", getTime() - startTime);
 
-#ifdef TESTING
+
+#ifdef TESTING
+    // Write rejection images out to check
+    for (int i = 0; i < nImages; i++) {
+	char rejName[MAXCHAR];	// Filename of rejection image
+	sprintf(rejName,"%s.shiftrej",config->inputs->data[i]);
+	
+	psFits *rejFile = psFitsAlloc(rejName);
+	if (!psFitsWriteImage(rejFile, NULL, (*rejected)->data[i], 0, NULL)) {
+	    psErrorStackPrint(stderr, "Unable to write image: %s\n", rejName);
+	}
+	psTrace("stac", 1, "Rejection image written to %s\n", rejName);
+	psFree(rejFile);
+    }
+
+    // Write out pre-combined image
     char preName[MAXCHAR];		// Filename of precombined image
     sprintf(preName,"%s.pre",config->output);
@@ -116,5 +179,6 @@
 
     // Transform rejected pixels to source frame
-    psArray *rejectedSource = stacRejection(inputs, rejected, regions, maps, inverseMaps, config);
+    psArray *rejectedSource = stacRejection(inputs, rejected, regions, maps, inverseMaps, config->frac,
+					    config->grad, config->inputs);
 
     // Get regions of interest in the output frame
@@ -134,10 +198,11 @@
     // Redo transformation with the masks and scales/offsets
     (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, rejectedSource,
-			combineRegion, scales, offsets, config);
+			combineRegion, scales, offsets, config->outnx, config->outny);
 
     // Combine the newly-transformed CR-free images, no rejection
     psFree(rejected);
     rejected = NULL;
-    (void)stacCombine(&combined, &rejected, transformed, transformedErrors, 0, combineRegion, config);
+    (void)stacCombine(&combined, &rejected, transformed, transformedErrors, 0, combineRegion, saturated,
+		      bad, config->reject);
 
     // Write output image
