Index: /branches/eam_branch_20090208/psModules/src/objects/pmSourceIO.c
===================================================================
--- /branches/eam_branch_20090208/psModules/src/objects/pmSourceIO.c	(revision 21487)
+++ /branches/eam_branch_20090208/psModules/src/objects/pmSourceIO.c	(revision 21488)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.69.12.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-12 04:42:05 $
+ *  @version $Revision: 1.69.12.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-15 19:54:12 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -493,5 +493,5 @@
                 status = pmSourcesWrite_PS1_CAL_0 (file->fits, readout, sources, file->header, outhead, dataname);
             }
-            if (!strcmp (exttype, "CMF_PS1_V1")) {
+            if (!strcmp (exttype, "PS1_V1")) {
                 status = pmSourcesWrite_CMF_PS1_V1 (file->fits, readout, sources, file->header, outhead, dataname);
             }
@@ -503,5 +503,5 @@
                   status = pmSourcesWrite_PS1_CAL_0_XSRC (file->fits, readout, sources, file->header, xsrcname, recipe);
               }
-	      if (!strcmp (exttype, "CMF_PS1_V1")) {
+	      if (!strcmp (exttype, "PS1_V1")) {
 		  status = pmSourcesWrite_CMF_PS1_V1_XSRC (file->fits, sources, xsrcname, recipe);
 	      }
@@ -514,5 +514,5 @@
                   status = pmSourcesWrite_PS1_CAL_0_XFIT (file->fits, readout, sources, file->header, xfitname);
               }
-              if (!strcmp (exttype, "CMF_PS1_V1")) {
+              if (!strcmp (exttype, "PS1_V1")) {
                   status = pmSourcesWrite_CMF_PS1_V1_XFIT (file->fits, sources, xfitname);
               }
@@ -938,6 +938,6 @@
                 sources = pmSourcesRead_PS1_DEV_1 (file->fits, hdu->header);
             }
-            if (!strcmp (exttype, "PS1_CAL_0")) {
-                sources = pmSourcesRead_PS1_CAL_0 (file->fits, hdu->header);
+            if (!strcmp (exttype, "PS1_V1")) {
+                sources = pmSourcesRead_CMF_PS1_V1 (file->fits, hdu->header);
             }
         }
Index: /branches/eam_branch_20090208/psModules/src/objects/pmSourceIO_CMF_PS1_V1.c
===================================================================
--- /branches/eam_branch_20090208/psModules/src/objects/pmSourceIO_CMF_PS1_V1.c	(revision 21487)
+++ /branches/eam_branch_20090208/psModules/src/objects/pmSourceIO_CMF_PS1_V1.c	(revision 21488)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-12 04:42:37 $
+ *  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-15 19:54:28 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -75,14 +75,29 @@
     }
 
-    // let's write these out in S/N order
-    sources = psArraySort (sources, pmSourceSortBySN);
+    // if the sequence is defined, write these in seq order; otherwise
+    // write them in S/N order:
+    if (sources->n > 0) {
+        pmSource *source = (pmSource *) sources->data[0];
+	if (source->seq == -1) {
+	  // let's write these out in S/N order
+	  sources = psArraySort (sources, pmSourceSortBySN);
+	} else {
+	  sources = psArraySort (sources, pmSourceSortBySeq);
+	}
+    }
 
     table = psArrayAllocEmpty (sources->n);
 
     // we write out PSF-fits for all sources, regardless of quality.  the source flags tell us the state
+    // by the time we call this function, all values should be assigned.  let's use asserts to be sure in some cases.
     for (i = 0; i < sources->n; i++) {
         pmSource *source = (pmSource *) sources->data[i];
+
+	// If source->seq is -1, source was generated in this analysis.  If source->seq is
+	// not -1, source was read from elsewhere: in the latter case, preserve the source
+	// ID.  source.seq is used instead of source.id since the latter is a const
+	// generated on Alloc, and would thus be wrong for read in sources.
 	if (source->seq == -1) {
-	    source->seq = i;
+	  source->seq = i;
 	}
 
@@ -95,6 +110,12 @@
             xPos = PAR[PM_PAR_XPOS];
             yPos = PAR[PM_PAR_YPOS];
-            xErr = dPAR[PM_PAR_XPOS];
-            yErr = dPAR[PM_PAR_YPOS];
+	    if (source->mode & PM_SOURCE_MODE_NONLINEAR_FIT) {
+	      xErr = dPAR[PM_PAR_XPOS];
+	      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;
+	    }	      
 	    if (isfinite(PAR[PM_PAR_SXX]) && isfinite(PAR[PM_PAR_SXX]) && isfinite(PAR[PM_PAR_SXX])) {
 		axes = pmPSF_ModelToAxes (PAR, 20.0);
@@ -134,5 +155,4 @@
 
         row = psMetadataAlloc ();
-        // XXX we are not writing out the mode (flags) or the type (psf, ext, etc)
         psMetadataAdd (row, PS_LIST_TAIL, "IPP_IDET",         PS_DATA_U32, "IPP detection identifier index",             source->seq);
         psMetadataAdd (row, PS_LIST_TAIL, "X_PSF",            PS_DATA_F32, "PSF x coordinate",                           xPos);
@@ -146,5 +166,5 @@
         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, "AP_MAG_STANDARD",  PS_DATA_F32, "magnitude in standard aperture",             source->apMag);
+        psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG",           PS_DATA_F32, "magnitude in standard aperture",             source->apMag);
         psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RADIUS",    PS_DATA_F32, "radius used for aperture mags",              apRadius);
         psMetadataAdd (row, PS_LIST_TAIL, "PEAK_FLUX_AS_MAG", PS_DATA_F32, "Peak flux expressed as magnitude",           peakMag);
@@ -170,7 +190,9 @@
         psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_YY",        PS_DATA_F32, "second moments (Y*Y)",                      source->moments->Myy);
 
+        psMetadataAdd (row, PS_LIST_TAIL, "FLAGS",            PS_DATA_U32, "psphot analysis flags",                      source->mode);
+
         // XXX not sure how to get this : need to load Nimages with weight?
         psMetadataAdd (row, PS_LIST_TAIL, "N_FRAMES",         PS_DATA_U16, "Number of frames overlapping source center", nImageOverlap);
-        psMetadataAdd (row, PS_LIST_TAIL, "FLAGS",            PS_DATA_U16, "psphot analysis flags",                      source->mode);
+        psMetadataAdd (row, PS_LIST_TAIL, "PADDING",          PS_DATA_S16, "padding", 0);
 
         psArrayAdd (table, 100, row);
@@ -227,5 +249,5 @@
     psArray *sources = psArrayAlloc(numSources); // Array of sources, to return
 
-    // convert the table to the pmSource entries
+    // convert the table to the pmSource entriesa
     for (int i = 0; i < numSources; i++) {
         psMetadata *row = psFitsReadTableRow(fits, i); // Table row
@@ -245,16 +267,17 @@
         dPAR[PM_PAR_XPOS] = psMetadataLookupF32 (&status, row, "X_PSF_SIG");
         dPAR[PM_PAR_YPOS] = psMetadataLookupF32 (&status, row, "Y_PSF_SIG");
-        axes.major        = psMetadataLookupF32 (&status, row, "PSF_WIDTH_X");
-        axes.minor        = psMetadataLookupF32 (&status, row, "PSF_WIDTH_Y");
+        axes.major        = psMetadataLookupF32 (&status, row, "PSF_MAJOR");
+        axes.minor        = psMetadataLookupF32 (&status, row, "PSF_MINOR");
         axes.theta        = psMetadataLookupF32 (&status, row, "PSF_THETA");
 
         PAR[PM_PAR_SKY]   = psMetadataLookupF32 (&status, row, "SKY");
         dPAR[PM_PAR_SKY]  = psMetadataLookupF32 (&status, row, "SKY_SIGMA");
-        source->sky = PAR[PM_PAR_SKY];
-        source->skyErr = dPAR[PM_PAR_SKY];
+        source->sky       = PAR[PM_PAR_SKY];
+        source->skyErr    = dPAR[PM_PAR_SKY];
 
         // XXX use these to determine PAR[PM_PAR_I0]?
         source->psfMag    = psMetadataLookupF32 (&status, row, "PSF_INST_MAG");
         source->errMag    = psMetadataLookupF32 (&status, row, "PSF_INST_MAG_SIG");
+        source->apMag     = psMetadataLookupF32 (&status, row, "AP_MAG");
 
 	// XXX this scaling is incorrect: does not include the 2 \pi AREA factor 
@@ -264,17 +287,29 @@
         pmPSF_AxesToModel (PAR, axes);
 
-        float lflux = psMetadataLookupF32 (&status, row, "PEAK_FLUX_AS_MAG");
-        float flux = (isfinite(lflux)) ? pow(10.0, -0.4*lflux) : NAN;
-        source->peak = pmPeakAlloc(PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], flux, PM_PEAK_LONE);
-        source->peak->flux = flux;
+        float peakMag     = psMetadataLookupF32 (&status, row, "PEAK_FLUX_AS_MAG");
+        float peakFlux    = (isfinite(peakMag)) ? pow(10.0, -0.4*peakMag) : NAN;
+
+	// recreate the peak to match (xPos, yPos) +/- (xErr, yErr)
+        source->peak = pmPeakAlloc(PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], peakFlux, PM_PEAK_LONE);
+        source->peak->flux = peakFlux;
+        source->peak->dx   = dPAR[PM_PAR_XPOS];
+        source->peak->dy   = dPAR[PM_PAR_YPOS];
 
         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
-
-	// note that some older versions used PSF_PROBABILITY: this was not well defined.
-        model->chisq      = psMetadataLookupF32 (&status, row, "PSF_CHISQ");
         source->crNsigma  = psMetadataLookupF32 (&status, row, "CR_NSIGMA");
         source->extNsigma = psMetadataLookupF32 (&status, row, "EXT_NSIGMA");
 
-        source->mode      = psMetadataLookupU16 (&status, row, "FLAGS");
+	// note that some older versions used PSF_PROBABILITY: this was not well defined.
+        model->chisq 	  = psMetadataLookupF32 (&status, row, "PSF_CHISQ");
+        model->nDOF  	  = psMetadataLookupS32 (&status, row, "PSF_NDOF");
+        model->nPix  	  = psMetadataLookupS32 (&status, row, "PSF_NPIX");
+        model->radiusFit  = psMetadataLookupS32 (&status, row, "AP_MAG_RADIUS");
+
+	source->moments = pmMomentsAlloc ();
+        source->moments->Mxx = psMetadataLookupF32 (&status, row, "MOMENTS_XX");
+        source->moments->Mxy = psMetadataLookupF32 (&status, row, "MOMENTS_XY");
+        source->moments->Myy = psMetadataLookupF32 (&status, row, "MOMENTS_YY");
+
+        source->mode = psMetadataLookupU32 (&status, row, "FLAGS");
         assert (status);
 
