Index: /branches/eam_branch_20080225/psModules/src/objects/pmSource.h
===================================================================
--- /branches/eam_branch_20080225/psModules/src/objects/pmSource.h	(revision 16658)
+++ /branches/eam_branch_20080225/psModules/src/objects/pmSource.h	(revision 16659)
@@ -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.22.8.1 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-02-25 18:13:08 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -57,4 +57,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: /branches/eam_branch_20080225/psModules/src/objects/pmSourceIO.c
===================================================================
--- /branches/eam_branch_20080225/psModules/src/objects/pmSourceIO.c	(revision 16658)
+++ /branches/eam_branch_20080225/psModules/src/objects/pmSourceIO.c	(revision 16659)
@@ -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.53.10.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-02-25 18:13: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: /branches/eam_branch_20080225/psModules/src/objects/pmSourceIO_PS1_DEV_1.c
===================================================================
--- /branches/eam_branch_20080225/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 16658)
+++ /branches/eam_branch_20080225/psModules/src/objects/pmSourceIO_PS1_DEV_1.c	(revision 16659)
@@ -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.8.6.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-02-25 18:13: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;
+}
