Index: trunk/psModules/src/objects/pmSourceIO_CFF.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO_CFF.c	(revision 36375)
+++ trunk/psModules/src/objects/pmSourceIO_CFF.c	(revision 36441)
@@ -65,4 +65,7 @@
     int modelType = pmModelClassGetType ("PS_MODEL_GAUSS");
 
+    // Read lookup table for model classes (if defined)
+    pmModelClassReadHeader(header);
+
     char *PSF_NAME = psMetadataLookupStr (&status, header, "PSFMODEL");
     if (PSF_NAME != NULL) {
@@ -111,9 +114,10 @@
         float theta      = psMetadataLookupF32 (&status, row, "THETA");
 
-        // XXX: we need to put a lookup table in the cff header to define the correspondence of the
-        // model type values in the cff with our models. (We want to use an interger for efficiency
-        // but the value for each model type is set on the organization of the the array in pmModelClass.c
-        // For now use the input values verbatim and trust the user that this is valid value
         int   galaxyModelType = psMetadataLookupS32(&status, row, "MODEL_TYPE");
+        if (status) {
+            galaxyModelType = pmModelClassGetLocalType(galaxyModelType);
+        } else {
+            galaxyModelType = -1;
+        }
         float Sindex     = psMetadataLookupF32 (&status, row, "INDEX"); // Should this be PAR_07 not sersic index
 
@@ -123,5 +127,6 @@
         source->type = PM_SOURCE_TYPE_STAR; // XXX this should be added to the flags
 
-	// XXX we can set this in general, but for a specific image, we need to weed out SATSTARS
+	// XXX we can set this in general, but for a specific image, we need to weed out SATSTARS and
+        // stars that are masked
         if (psfStar) {
 	    source->tmpFlags |= PM_SOURCE_TMPF_CANDIDATE_PSFSTAR;
@@ -180,7 +185,5 @@
 	}
 
-        // XXX: should use < 0 as invalid galaxyModelType
-
-        if (fitGalaxy && galaxyModelType > 0) {
+        if (fitGalaxy && galaxyModelType >= 0) {
             source->modelFits = psArrayAllocEmpty (1);
 	    pmModel *model = pmModelAlloc(galaxyModelType);
@@ -235,4 +238,8 @@
     PS_ASSERT(mdok, false);
 
+    // write the definition of the model class type values to the header
+    psMetadata *outputHeader = psMetadataAlloc();
+    pmModelClassWriteHeader(outputHeader);
+
     psArray *table = psArrayAllocEmpty(sources->n);
 
@@ -246,7 +253,8 @@
         psS32 modelType = 0;
         bool fitGalaxy = false;
-        bool psfStar = false;
+        bool psfStar = (source->mode & PM_SOURCE_MODE_PSFSTAR) ? true : false;
         psF32 sersicIndex = 0;
-        if (source->modelFits == NULL) {
+        // For now only perform galaxy fits on extended objects
+        if (source->modelEXT == NULL) {
             pmModel *model = source->modelPSF;
             if (model == NULL) continue;
@@ -260,5 +268,4 @@
             yPos = model->params->data.F32[PM_PAR_YPOS];
             flux = source->psfFlux;
-            psfStar = (source->mode & PM_SOURCE_MODE_PSFSTAR) ? true : false;
             rMajor = 0;
             rMinor = 0;
@@ -329,14 +336,15 @@
 
         psArrayAdd(table, 100, row);
+        psFree(row);
     }
 
-    if (!psFitsWriteTable(fits, NULL, table, extname)) {
+    if (!psFitsWriteTable(fits, outputHeader, table, extname)) {
         psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
         psFree(table);
-        psFree(header);
+        psFree(outputHeader);
         return false;
     }
     psFree(table);
-    // psFree(header);
+    psFree(outputHeader);
 
     return true;
