Index: /trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO.c	(revision 13526)
+++ /trunk/psModules/src/objects/pmSourceIO.c	(revision 13527)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.40 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-05-11 03:44:16 $
+ *  @version $Revision: 1.41 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-05-26 02:52:53 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -310,91 +310,99 @@
         }
 
-        // lookup the EXTNAME values used for table data and image header segments
-        char *rule = NULL;
-        // Menu of EXTNAME rules
-        psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES");
-        if (!menu) {
-            psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
-            return false;
-        }
-        // EXTNAME for image header
-        rule = psMetadataLookupStr(&status, menu, "CMF.HEAD");
-        if (!rule) {
-            psError(PS_ERR_UNKNOWN, true, "missing entry for CMF.HEAD in EXTNAME.RULES in camera.config");
-            return false;
-        }
-        headname = pmFPAfileNameFromRule (rule, file, view);
-        // EXTNAME for table data
-        rule = psMetadataLookupStr(&status, menu, "CMF.DATA");
-        if (!rule) {
-            psError(PS_ERR_UNKNOWN, true, "missing entry for CMF.DATA in EXTNAME.RULES in camera.config");
-            return false;
-        }
-        dataname = pmFPAfileNameFromRule (rule, file, view);
-
-        // this header block is new, write it to disk
-        if (hdu->header != file->header) {
-            // XXX I was building a new copy, why?  supplement the output header
-            // outhead = psMetadataCopy (NULL, hdu->header);
-
-            // add EXTNAME, EXTHEAD, EXTTYPE to header
-            // psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "extension name", headname);
-            psMetadataAddStr (hdu->header, PS_LIST_TAIL, "EXTDATA", PS_META_REPLACE, "name of table extension", dataname);
-            psMetadataAddStr (hdu->header, 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 (hdu->header, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
-            }
-
-            // save psphot and psastro metadata in the image and table headers
-            updates = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.HEADER");
-            if (updates) {
-                psMetadataCopy (hdu->header, updates);
-            }
-            updates = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.HEADER");
-            if (updates) {
-                psMetadataCopy (hdu->header, updates);
-            }
-
-            psFitsWriteBlank (file->fits, hdu->header, headname);
-            psTrace ("pmFPAfile", 5, "wrote ext head %s (type: %d)\n", file->filename, file->type);
-            file->header = hdu->header;
-        }
-
-        // create a header to hold the output data
-        outhead = psMetadataAlloc ();
-
-        // determine the output table format
-        psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, "PSPHOT");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, true, "missing recipe PSPHOT in config data");
-            return false;
-        }
-        exttype = psMemIncrRefCounter (psMetadataLookupStr(&status, recipe, "OUTPUT.FORMAT"));
-        if (!exttype) {
-            exttype = psStringCopy ("SMPDATA");
-        }
-
-        // write the links to the image header
-        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);
-
-        // XXX these are case-sensitive since the EXTYPE is case-sensitive
-        status = false;
-        if (!strcmp (exttype, "SMPDATA")) {
-            status = pmSourcesWrite_SMPDATA (file->fits, sources, file->header, outhead, dataname);
-        }
-        if (!strcmp (exttype, "PS1_DEV_0")) {
-            status = pmSourcesWrite_PS1_DEV_0 (file->fits, sources, file->header, outhead, dataname);
-        }
-
-        if (!status) {
-            psError(PS_ERR_IO, false, "writing CMF data to %s with format %s\n", file->filename, exttype);
-            psFree (headname);
-            psFree (dataname);
-            psFree (outhead);
-            return false;
-        }
+	// define the EXTNAME values for the different data segments:
+	{
+	    // lookup the EXTNAME values used for table data and image header segments
+	    char *rule = NULL;
+
+	    // Menu of EXTNAME rules
+	    psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES");
+	    if (!menu) {
+		psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
+		return false;
+	    }
+
+	    // EXTNAME for image header
+	    rule = psMetadataLookupStr(&status, menu, "CMF.HEAD");
+	    if (!rule) {
+		psError(PS_ERR_UNKNOWN, true, "missing entry for CMF.HEAD in EXTNAME.RULES in camera.config");
+		return false;
+	    }
+	    headname = pmFPAfileNameFromRule (rule, file, view);
+
+	    // EXTNAME for table data
+	    rule = psMetadataLookupStr(&status, menu, "CMF.DATA");
+	    if (!rule) {
+		psError(PS_ERR_UNKNOWN, true, "missing entry for CMF.DATA in EXTNAME.RULES in camera.config");
+		return false;
+	    }
+	    dataname = pmFPAfileNameFromRule (rule, file, view);
+	}
+
+	// write out the IMAGE header segment
+	{
+	    // this header block is new, write it to disk
+	    if (hdu->header != file->header) {
+		// add EXTNAME, EXTHEAD, EXTTYPE to header
+		psMetadataAddStr (hdu->header, PS_LIST_TAIL, "EXTDATA", PS_META_REPLACE, "name of table extension", dataname);
+		psMetadataAddStr (hdu->header, 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 (hdu->header, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
+		}
+
+		// save psphot and psastro metadata in the image and table headers
+		updates = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.HEADER");
+		if (updates) {
+		    psMetadataCopy (hdu->header, updates);
+		}
+		updates = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.HEADER");
+		if (updates) {
+		    psMetadataCopy (hdu->header, updates);
+		}
+
+		psFitsWriteBlank (file->fits, hdu->header, headname);
+		psTrace ("pmFPAfile", 5, "wrote ext head %s (type: %d)\n", file->filename, file->type);
+		file->header = hdu->header;
+	    }
+	}
+
+	// write out the TABLE data segment
+	{
+	    // create a header to hold the output data
+	    outhead = psMetadataAlloc ();
+
+	    // determine the output table format
+	    psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, "PSPHOT");
+	    if (!status) {
+		psError(PS_ERR_UNKNOWN, true, "missing recipe PSPHOT in config data");
+		return false;
+	    }
+	    exttype = psMemIncrRefCounter (psMetadataLookupStr(&status, recipe, "OUTPUT.FORMAT"));
+	    if (!exttype) {
+		exttype = psStringCopy ("SMPDATA");
+	    }
+
+	    // write the links to the image header
+	    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);
+
+	    // XXX these are case-sensitive since the EXTYPE is case-sensitive
+	    status = false;
+	    if (!strcmp (exttype, "SMPDATA")) {
+		status = pmSourcesWrite_SMPDATA (file->fits, sources, file->header, outhead, dataname);
+	    }
+	    if (!strcmp (exttype, "PS1_DEV_0")) {
+		status = pmSourcesWrite_PS1_DEV_0 (file->fits, sources, file->header, outhead, dataname);
+	    }
+
+	    if (!status) {
+		psError(PS_ERR_IO, false, "writing CMF data to %s with format %s\n", file->filename, exttype);
+		psFree (headname);
+		psFree (dataname);
+		psFree (outhead);
+		return false;
+	    }
+	}
 
         psTrace ("pmFPAfile", 5, "wrote ext data %s (type: %d)\n", file->filename, file->type);
