Index: /trunk/psphot/src/psphotOutput.c
===================================================================
--- /trunk/psphot/src/psphotOutput.c	(revision 5753)
+++ /trunk/psphot/src/psphotOutput.c	(revision 5754)
@@ -199,6 +199,5 @@
 
     // create file, write-out header
-    unlink (filename);
-    psFits *fits = psFitsAlloc (filename);
+    psFits *fits = psFitsOpen (filename, "w");
 
     // set NAXIS to 0 : CFITSIO requires isolated header to have NAXIS = 0
@@ -209,5 +208,5 @@
     // psMetadataAdd (imdata->header, PS_LIST_HEAD, "NAXIS", PS_DATA_S32 | PS_META_REPLACE, "", 0);
     psFitsWriteHeader (imdata->header, fits);
-    psFree (fits);
+    psFitsClose (fits);
 
     // re-open, add data to end of file
@@ -336,9 +335,8 @@
     psMetadataAdd (theader, PS_LIST_HEAD, "EXTNAME", PS_DATA_STRING, "extension name", "SMPFILE");
 
-    unlink (filename);
-    psFits *fits = psFitsAlloc (filename);
+    psFits *fits = psFitsOpen (filename, "w");
     psFitsWriteHeader (imdata->header, fits);
     psFitsWriteTable (fits, theader, table);
-    psFree (fits);
+    psFitsClose (fits);
 
     return true;
@@ -542,4 +540,34 @@
 }
 
+// write the moments to an output file
+bool pmMomentsWriteText (psArray *sources, char *filename)
+{
+
+    int i;
+    FILE *f;
+    pmSource *source;
+
+    f = fopen (filename, "w");
+    if (f == NULL) {
+        psLogMsg ("pmMomentsWriteText", 3, "can't open output file for moments%s\n", filename);
+        return false;
+    }
+
+    for (i = 0; i < sources->n; i++) {
+        source = (pmSource *) sources->data[i];
+        if (source->moments == NULL)
+            continue;
+        fprintf (f, "%5d %5d  %7.1f  %7.1f %7.1f  %6.3f %6.3f  %8.1f %7.1f %7.1f %7.1f  %4d %2d\n",
+                 source->peak->x, source->peak->y, source->peak->counts,
+                 source->moments->x, source->moments->y,
+                 source->moments->Sx, source->moments->Sy,
+                 source->moments->Sum, source->moments->Peak,
+                 source->moments->Sky, source->moments->SN,
+                 source->moments->nPixels, source->type);
+    }
+    fclose (f);
+    return true;
+}
+
 // translations between psphot object types and dophot object types
 int pmSourceDophotType (pmSource *source) {
@@ -581,8 +609,47 @@
 int psphotSaveImage (psMetadata *header, psImage *image, char *filename) {
 
-    unlink (filename);
-    psFits *fits = psFitsAlloc (filename);
+    psFits *fits = psFitsOpen (filename, "w");
     psFitsWriteImage (fits, NULL, image, 0);
-    psFree (fits);
+    psFitsClose (fits);
     return (TRUE);
 }
+
+psImage *pmModelPSFatXY (psImage *image, pmModel *modelFLT, pmPSF *psf, int x, int y, int dx, int dy) {
+
+    psRegion region = {x - dx, x + dx, y - dy, y + dy};
+    psImage *sample = psImageSubset (image, region);
+    psImageInit (sample, 0);
+    modelFLT->params->data.F32[2] = x;
+    modelFLT->params->data.F32[3] = y;
+    pmModel *modelPSF = pmModelFromPSF (modelFLT, psf);
+    pmSourceAddModel (sample, NULL, modelPSF, false, false);
+    psFree (modelPSF);
+    return (sample);
+}
+
+
+bool psphotSamplePSFs (pmPSF *psf, psImage *image) {
+
+  // make sample PSFs for 4 corners and the center
+    psImage *sample;
+
+    pmModel *modelFLT = pmModelAlloc (psf->type);
+    modelFLT->params->data.F32[0] = 0;
+    modelFLT->params->data.F32[1] = 1;
+
+    psFits *fits = psFitsOpen ("sample.psf.fits", "w");
+
+    sample = pmModelPSFatXY (image, modelFLT, psf, 25, 25, 25, 25);
+    psFitsWriteImage (fits, NULL, sample, 0);
+    sample = pmModelPSFatXY (image, modelFLT, psf, image->numCols - 25, image->numRows - 25, 25, 25);
+    psFitsWriteImage (fits, NULL, sample, 0);
+    sample = pmModelPSFatXY (image, modelFLT, psf, image->numCols - 25, 25, 25, 25);
+    psFitsWriteImage (fits, NULL, sample, 0);
+    sample = pmModelPSFatXY (image, modelFLT, psf, 25, image->numRows - 25, 25, 25);
+    psFitsWriteImage (fits, NULL, sample, 0);
+    sample = pmModelPSFatXY (image, modelFLT, psf, image->numCols / 2, image->numRows / 2, 25, 25);
+    psFitsWriteImage (fits, NULL, sample, 0);
+
+    psFitsClose (fits);
+    return (TRUE);
+}
Index: /trunk/psphot/src/psphotSetup.c
===================================================================
--- /trunk/psphot/src/psphotSetup.c	(revision 5753)
+++ /trunk/psphot/src/psphotSetup.c	(revision 5754)
@@ -27,9 +27,9 @@
     char *input = psMetadataLookupPtr (&status, config, "IMAGE");
     if (!status) psAbort ("psphot", "input image not specified");
-    psFits *file = psFitsAlloc (input);
+    psFits *file = psFitsOpen (input, "r");
     header = psFitsReadHeader (header, file);
     psImage *tmpimage = psFitsReadImage (NULL, file, region, 0);
     image = psImageCopy (NULL, tmpimage, PS_TYPE_F32);
-    psFree (file);
+    psFitsClose (file);
     
     // psFree (input);
@@ -46,7 +46,7 @@
     char *weightName = psMetadataLookupPtr (&status, config, "WEIGHT");
     if (status == true) {
-	file = psFitsAlloc (weightName);
+	file = psFitsOpen (weightName, "r");
 	weight = psFitsReadImage  (NULL, file, region, 0);
-	psFree (file);
+	psFitsClose (file);
 	// psFree (weightName); XXX - see psFree (input)
     } else {
@@ -64,7 +64,7 @@
     if (status == true) {
 	// XXX EAM require / convert mask to psU8?
-	file = psFitsAlloc (maskName);
+	file = psFitsOpen (maskName, "r");
 	mask  = psFitsReadImage  (NULL, file, region, 0);
-	psFree (file);
+	psFitsClose (file);
 	// psFree (maskName); XXX - see psFree (input)
     } else {
