Index: trunk/psModules/src/camera/pmFPAWrite.c
===================================================================
--- trunk/psModules/src/camera/pmFPAWrite.c	(revision 21266)
+++ trunk/psModules/src/camera/pmFPAWrite.c	(revision 21279)
@@ -107,5 +107,4 @@
     if (writeBlank || writeImage) {
 
-        pmFPAUpdateNames(cell->parent->parent, cell->parent, cell);
         pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
                                  PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE;
@@ -157,5 +156,4 @@
 
         if (writeBlank || writeImage) {
-            pmFPAUpdateNames(chip->parent, chip, NULL);
             pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
                                      PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE;
@@ -221,5 +219,4 @@
 
         if (writeBlank || writeImage) {
-            pmFPAUpdateNames(fpa, NULL, NULL);
             pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
                                      PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE;
@@ -255,16 +252,34 @@
 
 // Update the FPA.OBS, CHIP.NAME and CELL.NAME in the FITS header, if required
-bool pmFPAUpdateNames(pmFPA *fpa, pmChip *chip, pmCell *cell)
-{
-    pmHDU *hdu = pmHDUGetHighest(fpa, chip, cell); // Highest HDU, i.e., the PHU
-    if (!hdu) {
+bool pmFPAUpdateNames(pmFPA *fpa, pmChip *chip, pmCell *cell, psS64 imageId, psS64 sourceId)
+{
+    pmHDU *hduHigh = pmHDUGetHighest(fpa, chip, cell); // Highest HDU, i.e., the PHU
+    if (!hduHigh) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find PHU.\n");
+        return false;
+    }
+    if (!hduHigh->header) {
+        hduHigh->header = psMetadataAlloc();
+    }
+    if (!pmHDUWriteIdentifiers(hduHigh, imageId, sourceId)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to write identifiers to header.");
+        return false;
+    }
+
+    pmHDU *hduLow = pmHDUGetLowest(fpa, chip, cell); // Lowest HDU, i.e., the extension
+    if (!hduLow) {
         psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find HDU.\n");
         return false;
     }
-    if (!hdu->header) {
-        hdu->header = psMetadataAlloc();
-    }
+    if (!hduLow->header) {
+        hduLow->header = psMetadataAlloc();
+    }
+    if (hduLow != hduHigh && !pmHDUWriteIdentifiers(hduLow, imageId, sourceId)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to write identifiers to header.");
+        return false;
+    }
+
     bool mdok;                          // Status of MD lookup
-    psMetadata *fileData = psMetadataLookupMetadata(&mdok, hdu->format, "FILE"); // File information
+    psMetadata *fileData = psMetadataLookupMetadata(&mdok, hduHigh->format, "FILE"); // File information
     if (!mdok || !fileData) {
         psError(PS_ERR_UNKNOWN, true, "Unable to find FILE information in camera format.\n");
@@ -275,5 +290,5 @@
         if (mdok && fpaObsHdr && strlen(fpaObsHdr) > 0) {
             const char *fpaObs = psMetadataLookupStr(NULL, fpa->concepts, "FPA.OBS");
-            psMetadataAddStr(hdu->header, PS_LIST_TAIL, fpaObsHdr, PS_META_REPLACE,
+            psMetadataAddStr(hduHigh->header, PS_LIST_TAIL, fpaObsHdr, PS_META_REPLACE,
                              "Observation identifier", fpaObs);
         }
@@ -298,5 +313,6 @@
         }
 
-        psMetadataAddStr(hdu->header, PS_LIST_TAIL, contentKey, PS_META_REPLACE, "Content of file", content);
+        psMetadataAddStr(hduHigh->header, PS_LIST_TAIL, contentKey, PS_META_REPLACE,
+                         "Content of file", content);
         psFree(content);                // Drop reference
     }
Index: trunk/psModules/src/camera/pmFPAWrite.h
===================================================================
--- trunk/psModules/src/camera/pmFPAWrite.h	(revision 21266)
+++ trunk/psModules/src/camera/pmFPAWrite.h	(revision 21279)
@@ -4,6 +4,6 @@
  * @author Paul Price, IfA
  *
- * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-06-17 22:16:38 $
+ * @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2009-02-04 02:39:36 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -165,6 +165,13 @@
                    );
 
-// XXX better name, please
-bool pmFPAUpdateNames(pmFPA *fpa, pmChip *chip, pmCell *cell);
+// Update the header before writing to be consistent
+//
+// XXX Would like a better name
+bool pmFPAUpdateNames(pmFPA *fpa,       ///< FPA
+                      pmChip *chip,     ///< Chip, or NULL
+                      pmCell *cell,     ///< Cell, or NULL
+                      psS64 imageId,    ///< Image identifier
+                      psS64 sourceId    ///< Source identifier
+    );
 
 /// @}
Index: trunk/psModules/src/camera/pmFPAfile.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfile.c	(revision 21266)
+++ trunk/psModules/src/camera/pmFPAfile.c	(revision 21279)
@@ -98,4 +98,7 @@
 
     file->save = false;
+
+    file->imageId = 0;
+    file->sourceId = 0;
 
     return file;
Index: trunk/psModules/src/camera/pmFPAfile.h
===================================================================
--- trunk/psModules/src/camera/pmFPAfile.h	(revision 21266)
+++ trunk/psModules/src/camera/pmFPAfile.h	(revision 21279)
@@ -4,6 +4,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-09-02 19:06:16 $
+ * @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2009-02-04 02:39:36 $
  * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
  */
@@ -106,4 +106,6 @@
     psMetadata *format;                 // Camera format
     psString formatName;                // name of the camera format
+
+    psS64 imageId, sourceId;            // Image and source identifiers
 } pmFPAfile;
 
Index: trunk/psModules/src/camera/pmFPAfileFitsIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 21266)
+++ trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 21279)
@@ -178,5 +178,5 @@
           pmChip *chip = pmFPAviewThisChip(view, fpa); // Chip of interest, or NULL
           pmCell *cell = pmFPAviewThisCell(view, fpa); // Cell of interest, or NULL
-          if (!pmFPAUpdateNames(fpa, chip, cell)) {
+          if (!pmFPAUpdateNames(fpa, chip, cell, file->imageId, file->sourceId)) {
               psError(PS_ERR_UNKNOWN, false, "Unable to update names in header.");
               return false;
Index: trunk/psModules/src/camera/pmHDU.c
===================================================================
--- trunk/psModules/src/camera/pmHDU.c	(revision 21266)
+++ trunk/psModules/src/camera/pmHDU.c	(revision 21279)
@@ -243,2 +243,26 @@
     return hduWrite(hdu, hdu->weights, hdu->masks, maskVal, fits);
 }
+
+bool pmHDUWriteIdentifiers(pmHDU *hdu, psS64 imageId, psS64 sourceId)
+{
+    PS_ASSERT_PTR_NON_NULL(hdu, false);
+
+    // XXX Get header keyword name from camera configuration
+
+    psMetadataAddS64(hdu->header, PS_LIST_TAIL, "IMAGEID", PS_META_REPLACE, "Image identifier", imageId);
+    psMetadataAddS64(hdu->header, PS_LIST_TAIL, "SOURCEID", PS_META_REPLACE, "Source identifier", sourceId);
+    return true;
+}
+
+bool pmHDUReadIdentifiers(psS64 *imageId, psS64 *sourceId, const pmHDU *hdu)
+{
+    PS_ASSERT_PTR_NON_NULL(hdu, false);
+
+    // XXX Get header keyword name from camera configuration
+
+    bool imageOK, sourceOK;             // Status of MD lookups
+    *imageId = psMetadataLookupS64(&imageOK, hdu->header, "IMAGEID");
+    *sourceId = psMetadataLookupS64(&sourceOK, hdu->header, "SOURCEID");
+
+    return imageOK && sourceOK;
+}
Index: trunk/psModules/src/camera/pmHDU.h
===================================================================
--- trunk/psModules/src/camera/pmHDU.h	(revision 21266)
+++ trunk/psModules/src/camera/pmHDU.h	(revision 21279)
@@ -4,6 +4,6 @@
  * @author Paul Price, IfA
  *
- * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-09-05 08:21:35 $
+ * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2009-02-04 02:39:36 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -85,4 +85,17 @@
     );
 
+
+/// Read identifiers from FITS header
+bool pmHDUReadIdentifiers(psS64 *imageId, ///< Image identifer, returned
+                          psS64 *sourceId, ///< Source identifier, returned
+                          const pmHDU *hdu ///< HDU from which to read
+    );
+
+/// Write identifiers to FITS header
+bool pmHDUWriteIdentifiers(pmHDU *hdu, ///< HDU to which to write
+                           psS64 imageId, ///< Image identifer
+                           psS64 sourceId ///< Source identifier
+                           );
+
 /// @}
 #endif
