Index: trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.c	(revision 10829)
+++ trunk/psModules/src/objects/pmSourceIO.c	(revision 10920)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-12-25 01:49:50 $
+ *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-01-05 20:19:23 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -168,4 +168,5 @@
 
     bool status;
+    char *exttype;
     char *realname;
     char *filename;
@@ -248,40 +249,49 @@
 
         // if file does not yet have a PHU, attempt to write one to disk
-        if (file->phu == NULL) {
-            // get the corresponding phu
+        // we only need a PHU if chips->n > 1 and file->fileLevel == FPA
+        // otherwise, the chip header fills the PHU location
+        if ((file->fileLevel == PM_FPA_LEVEL_FPA) && (file->fpa->chips->n > 1) && !file->phu) {
+
+            // find the FPA phu
             phu = pmFPAviewThisPHU (view, file->fpa);
 
             // if there is no PHU, this is a single header+image (extension-less) file
             // if there is a PHU, write it out as a 'blank'
+            outhead = psMetadataAlloc();
             if (phu) {
-                outhead = psMetadataCopy (NULL, phu->header);
-
-                // if we have mosaic-level astrometry information, add it here:
-                updates = psMetadataLookupPtr (&status, file->fpa->analysis, "PSASTRO.HEADER");
-                if (updates) {
-                    psMetadataCopy (outhead, updates);
-                }
-
-                psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
-                // XXX what is the EXTNAME??
-                psFitsWriteBlank (file->fits, outhead, "");
-                file->phu = phu->header;
-                psTrace ("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
-                psFree (outhead);
+                psMetadataCopy (outhead, phu->header);
             }
-        }
+
+            // if we have mosaic-level astrometry information, add it here:
+            updates = psMetadataLookupPtr (&status, file->fpa->analysis, "PSASTRO.HEADER");
+            if (updates) {
+                psMetadataCopy (outhead, updates);
+            }
+
+            psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
+            psFitsWriteBlank (file->fits, outhead, "");
+            file->phu = true;
+            psTrace ("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
+            psFree (outhead);
+        }
+
+        // EXTNAME values used to for table data and image header segments
+        dataname = pmFPAfileNameFromRule (file->extrule, file, view);
+        headname = pmFPAfileNameFromRule (file->extxtra, file, view);
 
         // this header block is new, write it to disk
         if (hdu->header != file->header) {
-            // determine name for header extension
-            headname = pmFPAfileNameFromRule (file->extxtra, file, view);
             outhead = psMetadataCopy (NULL, hdu->header);
-            psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "extension name", headname);
-            if (file->phu == NULL) {
+
+            // add EXTNAME, EXTHEAD, EXTTYPE to header
+            // psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "extension name", headname);
+            psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTDATA", PS_META_REPLACE, "name of table extension", dataname);
+            psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTTYPE", PS_META_REPLACE, "extension type", "IMAGE");
+            if (!file->phu) {
                 // this hdu->header acts as the PHU: set EXTEND to be true
                 psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
             }
 
-            // XXX : see note below about DVO/addstar expectations
+            // save psphot and psastro metadata in the image (not table) header
             updates = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.HEADER");
             if (updates) {
@@ -298,21 +308,20 @@
             file->header = hdu->header;
             psFree (outhead);
-            psFree (headname);
-        }
-
-        // write this table to disk
-        dataname = pmFPAfileNameFromRule (file->extrule, file, view);
-
-        // XXX : currently, this is being added to the TABLE header
-        //       this may be the correct location for a MEF file, but it is
-        //       inconsistent with DVO/addstar expectations.  DVO/addstar
-        //       currently only expects a single header/table pair, and the
-        //       metadata are in the blank (image) header, not the table header
-        //       it may be necessary to put the results from the first readout
-        //       in the blank image header to cover this case.
-        updates = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.HEADER");
-        pmSourcesWriteCMF (file->fits, sources, updates, dataname);
+        }
+
+        // create a header to hold the output data
+        outhead = psMetadataAlloc ();
+
+        exttype = psStringCopy ("SMPDATA");  // XXX derive this from the recipes
+        psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTHEAD", PS_META_REPLACE, "name of image extension w/", headname);
+        psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTTYPE", PS_META_REPLACE, "extension type", exttype);
+        psFree (exttype);
+
+        pmSourcesWriteCMF (file->fits, sources, outhead, dataname);
         psTrace ("pmFPAfile", 5, "wrote ext data %s (type: %d)\n", file->filename, file->type);
+
+        psFree (headname);
         psFree (dataname);
+        psFree (outhead);
         break;
 
