Index: trunk/psModules/src/objects/pmSource.c
===================================================================
--- trunk/psModules/src/objects/pmSource.c	(revision 16294)
+++ trunk/psModules/src/objects/pmSource.c	(revision 16819)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-01-15 02:48:10 $
+ *  @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-03-05 01:08:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -513,5 +513,4 @@
         if (!source->moments) {
             source->type = PM_SOURCE_TYPE_STAR;
-            source->mode |= PM_SOURCE_MODE_DEFAULT;
             Nstar++;
             continue;
@@ -542,5 +541,5 @@
         if (Nsatpix > 1) {
             source->type = PM_SOURCE_TYPE_SATURATED;
-            source->mode |= PM_SOURCE_MODE_DEFAULT;
+            source->mode |= PM_SOURCE_MODE_SATURATED;
             Nsat ++;
             continue;
@@ -553,5 +552,5 @@
         if ((sigX < 0.05) || (sigY < 0.05)) {
             source->type = PM_SOURCE_TYPE_DEFECT;
-            source->mode |= PM_SOURCE_MODE_DEFAULT;
+            source->mode |= PM_SOURCE_MODE_DEFECT;
             Ncr ++;
             continue;
@@ -561,5 +560,4 @@
         if ((sigX > (clump.X + 3*clump.dX)) || (sigY > (clump.Y + 3*clump.dY))) {
             source->type = PM_SOURCE_TYPE_EXTENDED;
-            source->mode |= PM_SOURCE_MODE_DEFAULT;
             Next ++;
             continue;
@@ -584,5 +582,4 @@
         // random type of star
         source->type = PM_SOURCE_TYPE_STAR;
-        source->mode |= PM_SOURCE_MODE_DEFAULT;
     }
 
@@ -1006,5 +1003,4 @@
   if (!strcasecmp (name, "PSFMODEL"  )) return PM_SOURCE_MODE_PSFMODEL;
   if (!strcasecmp (name, "EXTMODEL"  )) return PM_SOURCE_MODE_EXTMODEL;
-  if (!strcasecmp (name, "SUBTRACTED")) return PM_SOURCE_MODE_SUBTRACTED;
   if (!strcasecmp (name, "FITTED"    )) return PM_SOURCE_MODE_FITTED;
   if (!strcasecmp (name, "FAIL"      )) return PM_SOURCE_MODE_FAIL;
@@ -1014,8 +1010,10 @@
   if (!strcasecmp (name, "SATSTAR"   )) return PM_SOURCE_MODE_SATSTAR;
   if (!strcasecmp (name, "BLEND"     )) return PM_SOURCE_MODE_BLEND;
-  if (!strcasecmp (name, "LINEAR"    )) return PM_SOURCE_MODE_LINEAR;
-  if (!strcasecmp (name, "TEMPSUB"   )) return PM_SOURCE_MODE_TEMPSUB;
   if (!strcasecmp (name, "EXTERNAL"  )) return PM_SOURCE_MODE_EXTERNAL;
   if (!strcasecmp (name, "BADPSF"    )) return PM_SOURCE_MODE_BADPSF;
+  if (!strcasecmp (name, "DEFECT"    )) return PM_SOURCE_MODE_DEFECT;
+  if (!strcasecmp (name, "SATURATED" )) return PM_SOURCE_MODE_SATURATED;
+  if (!strcasecmp (name, "CRLIMIT"   )) return PM_SOURCE_MODE_CRLIMIT;
+  if (!strcasecmp (name, "SUBTRACTED")) return PM_SOURCE_MODE_SUBTRACTED;
   return PM_SOURCE_MODE_DEFAULT;
 }
@@ -1026,5 +1024,4 @@
     case PM_SOURCE_MODE_PSFMODEL   : return psStringCopy ("PSFMODEL"  );
     case PM_SOURCE_MODE_EXTMODEL   : return psStringCopy ("EXTMODEL"  );
-    case PM_SOURCE_MODE_SUBTRACTED : return psStringCopy ("SUBTRACTED");
     case PM_SOURCE_MODE_FITTED     : return psStringCopy ("FITTED"    );
     case PM_SOURCE_MODE_FAIL       : return psStringCopy ("FAIL"      );
@@ -1034,8 +1031,10 @@
     case PM_SOURCE_MODE_SATSTAR    : return psStringCopy ("SATSTAR"   );
     case PM_SOURCE_MODE_BLEND      : return psStringCopy ("BLEND"     );
-    case PM_SOURCE_MODE_LINEAR     : return psStringCopy ("LINEAR"    );
-    case PM_SOURCE_MODE_TEMPSUB    : return psStringCopy ("TEMPSUB"   );
     case PM_SOURCE_MODE_EXTERNAL   : return psStringCopy ("EXTERNAL"  );
     case PM_SOURCE_MODE_BADPSF     : return psStringCopy ("BADPSF"    );
+    case PM_SOURCE_MODE_DEFECT     : return psStringCopy ("DEFECT"    );
+    case PM_SOURCE_MODE_SATURATED  : return psStringCopy ("SATURATED" );
+    case PM_SOURCE_MODE_CRLIMIT    : return psStringCopy ("CRLIMIT");
+    case PM_SOURCE_MODE_SUBTRACTED : return psStringCopy ("SUBTRACTED");
     default:
       return NULL;
Index: trunk/psModules/src/objects/pmSource.h
===================================================================
--- trunk/psModules/src/objects/pmSource.h	(revision 16294)
+++ trunk/psModules/src/objects/pmSource.h	(revision 16819)
@@ -3,6 +3,6 @@
  * @author EAM, IfA; GLG, MHPCC
  *
- * @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-01-15 02:48:28 $
+ * @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-03-05 01:08:08 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -36,18 +36,19 @@
 typedef enum {
     PM_SOURCE_MODE_DEFAULT    = 0x0000, ///<
-    PM_SOURCE_MODE_PSFMODEL   = 0x0001, ///<
-    PM_SOURCE_MODE_EXTMODEL   = 0x0002, ///<
-    PM_SOURCE_MODE_SUBTRACTED = 0x0004, ///<
-    PM_SOURCE_MODE_FITTED     = 0x0008, ///<
-    PM_SOURCE_MODE_FAIL       = 0x0010, ///<
-    PM_SOURCE_MODE_POOR       = 0x0020, ///<
-    PM_SOURCE_MODE_PAIR       = 0x0040, ///<
-    PM_SOURCE_MODE_PSFSTAR    = 0x0080, ///<
-    PM_SOURCE_MODE_SATSTAR    = 0x0100, ///<
-    PM_SOURCE_MODE_BLEND      = 0x0200, ///<
-    PM_SOURCE_MODE_LINEAR     = 0x0400, ///<
-    PM_SOURCE_MODE_TEMPSUB    = 0x0800, ///< XXX get me a better name!
-    PM_SOURCE_MODE_EXTERNAL   = 0x1000, ///< XXX get me a better name!
-    PM_SOURCE_MODE_BADPSF     = 0x2000, ///< Failed to get good estimate of object's PSF
+    PM_SOURCE_MODE_PSFMODEL   = 0x0001, ///< Source fitted with a psf model (linear or non-linear)
+    PM_SOURCE_MODE_EXTMODEL   = 0x0002, ///< Source fitted with an extended-source model
+    PM_SOURCE_MODE_FITTED     = 0x0004, ///< Source fitted with non-linear model (PSF or EXT; good or bad)
+    PM_SOURCE_MODE_FAIL       = 0x0008, ///< Fit (non-linear) failed (non-converge, off-edge, run to zero)
+    PM_SOURCE_MODE_POOR       = 0x0010, ///< Fit succeeds, but low-SN, high-Chisq, or large (for PSF -- drop?)
+    PM_SOURCE_MODE_PAIR       = 0x0020, ///< Source fitted with a double psf
+    PM_SOURCE_MODE_PSFSTAR    = 0x0040, ///< Source used to define PSF model
+    PM_SOURCE_MODE_SATSTAR    = 0x0080, ///< Source model peak is above saturation
+    PM_SOURCE_MODE_BLEND      = 0x0100, ///< Source is a blend with other sourcers
+    PM_SOURCE_MODE_EXTERNAL   = 0x0200, ///< Source based on supplied input position
+    PM_SOURCE_MODE_BADPSF     = 0x0400, ///< Failed to get good estimate of object's PSF
+    PM_SOURCE_MODE_DEFECT     = 0x0800, ///< Source is thought to be a defect
+    PM_SOURCE_MODE_SATURATED  = 0x1000, ///< Source is thought to be saturation
+    PM_SOURCE_MODE_CRLIMIT    = 0x2000, ///< Source has crNsigma above limit
+    PM_SOURCE_MODE_SUBTRACTED = 0x4000, ///< XXX this flag is actually only used internally (move)
 } pmSourceMode;
 
@@ -57,4 +58,6 @@
  *  simplest measurement of a source is the location and flux of the peak pixel
  *  associated with the source:
+ *
+ *  XXX do I have to re-organize this (again!) to allow an arbitrary set of extended model fits??
  *
  */
Index: trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.c	(revision 16294)
+++ trunk/psModules/src/objects/pmSourceIO.c	(revision 16819)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.53 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-11-10 01:09:20 $
+ *  @version $Revision: 1.54 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-03-05 01:08:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -254,4 +254,5 @@
     char *dataname = NULL;
     char *xsrcname = NULL;
+    char *xfitname = NULL;
     char *headname = NULL;
 
@@ -319,4 +320,5 @@
 	// if this is not TRUE, the output files only contain the psf measurements.
 	bool XSRC_OUTPUT = psMetadataLookupBool(&status, recipe, "SAVE.XSRC");
+	bool XFIT_OUTPUT = psMetadataLookupBool(&status, recipe, "SAVE.XFIT");
 
         // define the EXTNAME values for the different data segments:
@@ -356,4 +358,13 @@
 	      }
 	      xsrcname = pmFPAfileNameFromRule (rule, file, view);
+	    }
+	    if (XFIT_OUTPUT) {
+	      // EXTNAME for extended source data table
+	      rule = psMetadataLookupStr(&status, menu, "CMF.XFIT");
+	      if (!rule) {
+                psError(PS_ERR_UNKNOWN, true, "missing entry for CMF.XFIT in EXTNAME.RULES in camera.config");
+                return false;
+	      }
+	      xfitname = pmFPAfileNameFromRule (rule, file, view);
 	    }
         }
@@ -405,4 +416,12 @@
             psFree (exttype);
 
+	    // if we request XSRC output, add the XSRC name to this header
+	    if (xsrcname) {
+	      psMetadataAddStr (outhead, PS_LIST_TAIL, "XSRCNAME", PS_META_REPLACE, "name of XSRC table extension", xsrcname);
+	    }
+	    if (xfitname) {
+	      psMetadataAddStr (outhead, PS_LIST_TAIL, "XFITNAME", PS_META_REPLACE, "name of XFIT table extension", xfitname);
+	    }
+
             // XXX these are case-sensitive since the EXTYPE is case-sensitive
             status = false;
@@ -416,4 +435,14 @@
                 status = pmSourcesWrite_PS1_DEV_1 (file->fits, sources, file->header, outhead, dataname, xsrcname);
             }
+	    if (xsrcname) {
+	      if (!strcmp (exttype, "PS1_DEV_1")) {
+                status = pmSourcesWrite_PS1_DEV_1_XSRC (file->fits, sources, xsrcname);
+	      }
+	    }
+	    if (xfitname) {
+	      if (!strcmp (exttype, "PS1_DEV_1")) {
+                status = pmSourcesWrite_PS1_DEV_1_XFIT (file->fits, sources, xfitname);
+	      }
+	    }
             if (!status) {
                 psError(PS_ERR_IO, false, "writing CMF data to %s with format %s\n", file->filename, exttype);
Index: trunk/psModules/src/objects/pmSourceIO.h
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.h	(revision 16294)
+++ trunk/psModules/src/objects/pmSourceIO.h	(revision 16819)
@@ -4,6 +4,6 @@
  * @author EAM, IfA; GLG, MHPCC
  *
- * @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-11-10 01:09:20 $
+ * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-03-05 01:08:08 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  *
@@ -28,4 +28,5 @@
 bool pmSourcesWrite_PS1_DEV_1 (psFits *fits, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, char *xsrcname);
 bool pmSourcesWrite_PS1_DEV_1_XSRC (psFits *fits, psArray *sources, char *extname);
+bool pmSourcesWrite_PS1_DEV_1_XFIT (psFits *fits, psArray *sources, char *extname);
 
 bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
Index: trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 16294)
+++ trunk/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 16819)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-02-03 22:04:47 $
+ *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-03-05 01:08:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -61,9 +61,4 @@
     psF32 xPos, yPos;
     psF32 xErr, yErr;
-
-    // if we request XSRC output, add the XSRC name to this header
-    if (xsrcname) {
-      psMetadataAddStr (tableHeader, PS_LIST_TAIL, "XSRCNAME", PS_META_REPLACE, "name of XSRC table extension", xsrcname);
-    }
 
     // let's write these out in S/N order
@@ -155,8 +150,4 @@
     psFree (table);
 
-    if (xsrcname) {
-      pmSourcesWrite_PS1_DEV_1_XSRC (fits, sources, xsrcname);
-    }
-
     return true;
 }
@@ -284,7 +275,4 @@
     // we write out all sources, regardless of quality.  the source flags tell us the state
     for (i = 0; i < sources->n; i++) {
-	// source->seq is set during the PSF output step, which must be called
-	// XXX test here if seq is set?
-
 	// skip source if it is not a ext sourc
 	// XXX we have two places that extended source parameters are measured:
@@ -305,4 +293,5 @@
 	if (source->extpars == NULL) continue;
 
+	// XXX I need to split the extended models from the extended aperture measurements
 	PAR = model->params->data.F32;
 	dPAR = model->dparams->data.F32;
@@ -312,4 +301,5 @@
 	yErr = dPAR[PM_PAR_YPOS];
 
+	// XXX for the aperture values, I probably can / should just refer to the psf position and not write any of this
 	axes = pmPSF_ModelToAxes (PAR, 20.0);
 
@@ -329,5 +319,5 @@
         psMetadataAdd (row, PS_LIST_TAIL, "EXT_THETA",        PS_DATA_F32, "EXT orientation angle",                      axes.theta);
 
-	// XXX at the moment, this will be a fixed size table, but perhaps have multiple output formats depending on what is measured?
+	// XXX at the moment, this will be a fixed sized table, but perhaps have multiple output formats depending on what is measured?
 
 	// other values that I need to report:
@@ -401,2 +391,93 @@
     return true;
 }
+
+bool pmSourcesWrite_PS1_DEV_1_XFIT (psFits *fits, psArray *sources, char *extname)
+{
+
+    psArray *table;
+    psMetadata *row;
+    int i;
+    psF32 *PAR, *dPAR;
+    psEllipseAxes axes;
+    psF32 xPos, yPos;
+    psF32 xErr, yErr;
+
+    // create a header to hold the output data
+    psMetadata *outhead = psMetadataAlloc ();
+
+    // write the links to the image header
+    psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "xsrc table extension", extname);
+
+    // let's write these out in S/N order
+    sources = psArraySort (sources, pmSourceSortBySN);
+
+    table = psArrayAllocEmpty (sources->n);
+
+    // we write out all sources, regardless of quality.  the source flags tell us the state
+    for (i = 0; i < sources->n; i++) {
+	// skip source if it is not a ext sourc
+	// XXX we have two places that extended source parameters are measured:
+	// psphotExtendedSources, which measures the aperture-like parameters and (potentially) the psf-convolved extended source models,
+	// psphotFitEXT, which does the simple extended source model fit (not psf-convolved)
+	// should we require both?
+
+	pmSource *source = sources->data[i];
+
+	// XXX need to have an array of model fits; loop over the array and write out all
+	// which we calculated
+
+        // choose the convolved EXT model, if available, otherwise the simple one
+	// XXX should not need to choose: write both out
+        pmModel *model = source->modelConv;
+        if (model == NULL) {
+	    model = source->modelEXT;
+	}
+        if (model == NULL) continue;
+
+	PAR = model->params->data.F32;
+	dPAR = model->dparams->data.F32;
+	xPos = PAR[PM_PAR_XPOS];
+	yPos = PAR[PM_PAR_YPOS];
+	xErr = dPAR[PM_PAR_XPOS];
+	yErr = dPAR[PM_PAR_YPOS];
+
+	axes = pmPSF_ModelToAxes (PAR, 20.0);
+
+        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_EXT",            PS_DATA_F32, "EXT model x coordinate",                     xPos);
+        psMetadataAdd (row, PS_LIST_TAIL, "Y_EXT",            PS_DATA_F32, "EXT model y coordinate",                     yPos);
+        psMetadataAdd (row, PS_LIST_TAIL, "X_EXT_SIG",        PS_DATA_F32, "Sigma in EXT x coordinate",                  xErr);
+        psMetadataAdd (row, PS_LIST_TAIL, "Y_EXT_SIG",        PS_DATA_F32, "Sigma in EXT y coordinate",                  yErr);
+        psMetadataAdd (row, PS_LIST_TAIL, "EXT_INST_MAG",     PS_DATA_F32, "EXT fit instrumental magnitude",             source->extMag);
+        psMetadataAdd (row, PS_LIST_TAIL, "EXT_INST_MAG_SIG", PS_DATA_F32, "Sigma of PSF instrumental magnitude",        source->errMag);
+
+        // XXX these should be major and minor, not 'x' and 'y'
+        psMetadataAdd (row, PS_LIST_TAIL, "EXT_WIDTH_MAJ",    PS_DATA_F32, "EXT width in x coordinate",                  axes.major);
+        psMetadataAdd (row, PS_LIST_TAIL, "EXT_WIDTH_MIN",    PS_DATA_F32, "EXT width in y coordinate",                  axes.minor);
+        psMetadataAdd (row, PS_LIST_TAIL, "EXT_THETA",        PS_DATA_F32, "EXT orientation angle",                      axes.theta);
+
+	// XXX other parameters which may be set.
+	// XXX flag / value to define the model
+
+        psArrayAdd (table, 100, row);
+        psFree (row);
+    }
+
+    if (table->n == 0) {
+        psFitsWriteBlank (fits, outhead, extname);
+        psFree (table);
+        return true;
+    }
+
+    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
+    if (!psFitsWriteTable (fits, outhead, table, extname)) {
+        psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
+        psFree(table);
+        return false;
+    }
+    psFree (table);
+
+    return true;
+}
