Index: trunk/psphot/src/setup.c
===================================================================
--- trunk/psphot/src/setup.c	(revision 4189)
+++ trunk/psphot/src/setup.c	(revision 4215)
@@ -1,53 +1,62 @@
 # include "psphot.h"
 
-bool setup (psImage **image, psImage **mask, psImage **noise, psMetadata **header, psMetadata **config) 
+psImageData *setup (psMetadata *config) 
 {
-    int       Nfail;
-    psFits   *file = NULL;		// FITS pointer to input image
     psRegion  region = {0,0,0,0};	// a region representing the entire array
 
     psTimerStart ("psphot");
 
-    char *input = pmConfigLookupSTR (&status, *config, *header, "INPUT");
-    file = psFitsAlloc (input);
+    char *input = psMetadataLookupSTR (&status, config, "INPUT");
+    psFits *file = psFitsAlloc (input);
 
-    *header = psMetadataAlloc ();
-    psMetadataAdd (*header, PS_LIST_HEAD, "COMMENT", PS_META_MULTI, "folder for comment", NULL);
-    psMetadataAdd (*header, PS_LIST_HEAD, "HISTORY", PS_META_MULTI, "folder for history", NULL);
-    *header = psFitsReadHeader (*header, file);
-    *image  = psFitsReadImage  (*image,  file, region, 0);
+    psImageData *imdata = psAlloc(sizeof(psImageData));
+    psMetadata *header = psMetadataAlloc ();
+    psMetadataAdd (header, PS_LIST_HEAD, "COMMENT", PS_META_MULTI, "folder for comment", NULL);
+    psMetadataAdd (header, PS_LIST_HEAD, "HISTORY", PS_META_MULTI, "folder for history", NULL);
+    header = psFitsReadHeader (header, file);
+    psImage *image = psFitsReadImage (NULL, file, region, 0);
     psFree (file);
 
     // grab these values from the approrpiate location (image header if necessary)
     bool  status   = false;
-    float RDNOISE  = pmConfigLookupF32 (&status, *config, *header, "RDNOISE");
-    float GAIN     = pmConfigLookupF32 (&status, *config, *header, "GAIN");
-    psMetadataAdd (*config, PS_LIST_TAIL, "RDNOISE", PS_META_F32 | PS_META_REPLACE, "read noise value used", RDNOISE);
-    psMetadataAdd (*config, PS_LIST_TAIL, "GAIN",    PS_META_F32 | PS_META_REPLACE, "gain value used", GAIN);
+    float RDNOISE  = pmConfigLookupF32 (&status, config, header, "RDNOISE");
+    float GAIN     = pmConfigLookupF32 (&status, config, header, "GAIN");
+    psMetadataAdd (config, PS_LIST_TAIL, "RDNOISE", PS_META_F32 | PS_META_REPLACE, "read noise value used", RDNOISE);
+    psMetadataAdd (config, PS_LIST_TAIL, "GAIN",    PS_META_F32 | PS_META_REPLACE, "gain value used", GAIN);
 
-    // load the noise image if it exists, otherwise build from input
-    char *noiseName = pmConfigLookupSTR (&status, *config, *header, "NOISE");
+    imdata->image = image;
+    imdata->header = header;
+
+    // load the noise image if it is supplied, otherwise build from input
+    char *noiseName = psMetadataLookupSTR (&status, config, "NOISE");
     if (status == true) {
       file = psFitsAlloc (noiseName);
-      *noise  = psFitsReadImage  (*noise, file, region, 0);
+      imdata->noise = psFitsReadImage  (NULL, file, region, 0);
       psFree (file);
     } else {
-      *value = psScalar (1.0 / GAIN);
-      *noise = BinaryOp (NULL, *image, '/', value);
+      psScalar *value = psScalarAlloc (1.0 / GAIN, PS_TYPE_F64);
+      imdata->noise = BinaryOp (NULL, imdata->image, '/', value);
       psFree (value);
 
-      *value = psScalar (PS_SQR(RDNOISE/GAIN));
-      *noise = BinaryOp (*noise, *noise, '+', value);
+      psScalar *value = psScalarAlloc (PS_SQR(RDNOISE/GAIN), PS_TYPE_F64);
+      imdata->noise = BinaryOp (imdata->noise, imdata->noise, '+', value);
       psFree (value);
     }
 
-    char *maskName = pmConfigLookupSTR (&status, *config, *header, "MASK");
+    char *maskName = psMetadataLookupSTR (&status, config, "MASK");
     if (status == true) {
       file = psFitsAlloc (maskName);
-      *mask  = psFitsReadImage  (*mask, file, region, 0);
+      imdata->mask  = psFitsReadImage  (NULL, file, region, 0);
+      // check if U8 & force?
       psFree (file);
     } else {
-      *mask = psImageAlloc (image->numCols, image->numRows, PS_TYPE_U8);
+      imdata->mask = psImageAlloc (image->numCols, image->numRows, PS_TYPE_U8);
     }
+
+    float XBORDER  = psMetadataLookupF32 (&status, config, "XBORDER");
+    float YBORDER  = psMetadataLookupF32 (&status, config, "YBORDER");
+    psRegion *keep = psRegionAlloc (XBORDER, -XBORDER, YBORDER, -YBORDER);
+    keep           = psRegionForImage (keep, image, keep);
+    psImageKeepRegion (imdata->mask, keep, 0x01);
 
     psLogMsg ("psphot", 3, "load data: %f sec\n", psTimerMark ("psphot"));
