Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourceIO_CMF_PS1_V3.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourceIO_CMF_PS1_V3.c	(revision 29447)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourceIO_CMF_PS1_V3.c	(revision 29448)
@@ -66,5 +66,5 @@
     psF32 xPos, yPos;
     psF32 xErr, yErr;
-    psF32 errMag, chisq, apRadius;
+    psF32 chisq, apRadius;
     psS32 nPix, nDOF;
 
@@ -84,4 +84,14 @@
     }
     float zeroptErr = psMetadataLookupF32 (&status2, imageHeader, "ZPT_ERR");
+
+    // we need a measure of the image quality (FWHM) for this image, in order to get the positional errors
+    float fwhmMajor = psMetadataLookupF32(&status1, readout->analysis, "FWHM_MAJ");
+    if (!status1) {
+	fwhmMajor = psMetadataLookupF32(&status1, readout->analysis, "IQ_FW1");
+    }
+    float fwhmMinor = psMetadataLookupF32(&status1, readout->analysis, "FWHM_MIN");
+    if (!status1) {
+	fwhmMinor = psMetadataLookupF32(&status1, readout->analysis, "IQ_FW2");
+    }
 
     // if the sequence is defined, write these in seq order; otherwise
@@ -124,7 +134,6 @@
 		yErr = dPAR[PM_PAR_YPOS];
             } else {
-		// in linear-fit mode, there is no error on the centroid
-		xErr = source->peak->dx;
-		yErr = source->peak->dy;
+		xErr = fwhmMajor * source->errMag / 2.35;
+		yErr = fwhmMinor * source->errMag / 2.35;
             }
             if (isfinite(PAR[PM_PAR_SXX]) && isfinite(PAR[PM_PAR_SXX]) && isfinite(PAR[PM_PAR_SXX])) {
@@ -139,10 +148,21 @@
             nPix = model->nPix;
             apRadius = source->apRadius;
-            errMag = model->dparams->data.F32[PM_PAR_I0] / model->params->data.F32[PM_PAR_I0];
         } else {
-            xPos = source->peak->xf;
-            yPos = source->peak->yf;
-            xErr = source->peak->dx;
-            yErr = source->peak->dy;
+	    bool useMoments = true;
+	    useMoments = (useMoments && source->moments);          // can't if there are no moments
+	    useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
+	    useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
+
+	    if (source->moments) {
+		xPos = source->moments->Mx;
+		yPos = source->moments->My;
+		xErr = fwhmMajor * source->errMag / 2.35;
+		yErr = fwhmMinor * source->errMag / 2.35;
+	    } else {
+		xPos = source->peak->xf;
+		yPos = source->peak->yf;
+		xErr = source->peak->dx;
+		yErr = source->peak->dy;
+	    }
             axes.major = NAN;
             axes.minor = NAN;
@@ -152,5 +172,4 @@
             nPix = 0;
             apRadius = NAN;
-            errMag = NAN;
         }
 
@@ -168,10 +187,10 @@
         psMetadataAdd (row, PS_LIST_TAIL, "X_PSF",            PS_DATA_F32, "PSF x coordinate",                           xPos);
         psMetadataAdd (row, PS_LIST_TAIL, "Y_PSF",            PS_DATA_F32, "PSF y coordinate",                           yPos);
-        psMetadataAdd (row, PS_LIST_TAIL, "X_PSF_SIG",        PS_DATA_F32, "Sigma in PSF x coordinate",                  xErr); // XXX this is only measured for non-linear fits
-        psMetadataAdd (row, PS_LIST_TAIL, "Y_PSF_SIG",        PS_DATA_F32, "Sigma in PSF y coordinate",                  yErr); // XXX this is only measured for non-linear fits
+        psMetadataAdd (row, PS_LIST_TAIL, "X_PSF_SIG",        PS_DATA_F32, "Sigma in PSF x coordinate",                  xErr);
+        psMetadataAdd (row, PS_LIST_TAIL, "Y_PSF_SIG",        PS_DATA_F32, "Sigma in PSF y coordinate",                  yErr);
         psMetadataAdd (row, PS_LIST_TAIL, "POSANGLE",         PS_DATA_F32, "position angle at source (degrees)",         posAngle*PS_DEG_RAD);
         psMetadataAdd (row, PS_LIST_TAIL, "PLTSCALE",         PS_DATA_F32, "plate scale at source (arcsec/pixel)",       pltScale*PS_DEG_RAD*3600.0);
         psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_MAG",     PS_DATA_F32, "PSF fit instrumental magnitude",             source->psfMag);
-        psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_MAG_SIG", PS_DATA_F32, "Sigma of PSF instrumental magnitude",        errMag);
+        psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_MAG_SIG", PS_DATA_F32, "Sigma of PSF instrumental magnitude",        source->errMag);
         psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_FLUX",    PS_DATA_F32, "PSF fit instrumental flux (counts)",         source->psfFlux);
         psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_FLUX_SIG",PS_DATA_F32, "Sigma of PSF instrumental flux",             source->psfFluxErr);
@@ -179,10 +198,13 @@
         psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RAW",       PS_DATA_F32, "magnitude in reported aperture",             source->apMagRaw);
         psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RADIUS",    PS_DATA_F32, "radius used for aperture mags",              apRadius);
-	// XXX need ap_mag error
-        psMetadataAdd (row, PS_LIST_TAIL, "PEAK_FLUX_AS_MAG", PS_DATA_F32, "Peak flux expressed as magnitude",           peakMag);
+
         psMetadataAdd (row, PS_LIST_TAIL, "CAL_PSF_MAG",      PS_DATA_F32, "PSF Magnitude using supplied calibration",   calMag);
         psMetadataAdd (row, PS_LIST_TAIL, "CAL_PSF_MAG_SIG",  PS_DATA_F32, "measured scatter of zero point calibration", zeroptErr);
+	
+	// NOTE: RA & DEC (both double) need to be on an 8-byte boundary...
         psMetadataAdd (row, PS_LIST_TAIL, "RA_PSF",           PS_DATA_F64, "PSF RA coordinate (degrees)",                ptSky.r*PS_DEG_RAD);
         psMetadataAdd (row, PS_LIST_TAIL, "DEC_PSF",          PS_DATA_F64, "PSF DEC coordinate (degrees)",               ptSky.d*PS_DEG_RAD);
+
+        psMetadataAdd (row, PS_LIST_TAIL, "PEAK_FLUX_AS_MAG", PS_DATA_F32, "Peak flux expressed as magnitude",           peakMag);
         psMetadataAdd (row, PS_LIST_TAIL, "SKY",              PS_DATA_F32, "Sky level",                                  source->sky);
         psMetadataAdd (row, PS_LIST_TAIL, "SKY_SIGMA",        PS_DATA_F32, "Sigma of sky level",                         source->skyErr);
@@ -237,4 +259,5 @@
         psMetadataAdd (row, PS_LIST_TAIL, "FLAGS",            PS_DATA_U32, "psphot analysis flags",                     source->mode);
         psMetadataAdd (row, PS_LIST_TAIL, "FLAGS2",           PS_DATA_U32, "psphot analysis flags",                     source->mode2);
+        psMetadataAdd (row, PS_LIST_TAIL, "PADDING2",         PS_DATA_S32, "more padding", 0);
 
         // XXX not sure how to get this : need to load Nimages with weight?
