Index: /branches/eam_branch_20080223/Ohana/src/addstar/src/UpdateImageIDs.c
===================================================================
--- /branches/eam_branch_20080223/Ohana/src/addstar/src/UpdateImageIDs.c	(revision 16649)
+++ /branches/eam_branch_20080223/Ohana/src/addstar/src/UpdateImageIDs.c	(revision 16649)
@@ -0,0 +1,56 @@
+# include "addstar.h"
+
+int UpdateImageIDs (Stars *stars, int Nstars, Image *images, int Nimages) {
+
+  /*** update the image table ***/
+  /* setup image table format and lock */
+  db.mode   = dvo_catalog_catmode (CATMODE);
+  db.format = dvo_catalog_catformat (CATFORMAT);
+  status    = dvo_image_lock (&db, ImageCat, 3600.0, LCK_XCLD);  // shorter timeout?
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+
+  /* load or create the image table */
+  if (db.dbstate == LCK_EMPTY) {
+    if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
+    dvo_image_create (&db, GetZeroPoint());
+  } else {
+    /* position to start of file */
+    Fseek (db.f, 0, SEEK_SET);
+    if (!gfits_fread_header (db.f, &db.header)) {
+      Shutdown ("can't read image table phu %s", db.filename);
+    }
+  }
+
+  status = gfits_scan (&db.header, "IMAGEID", "%d", 1, &imageID);
+  if (!status) {
+    status = gfits_scan (&db.header, "NIMAGES", "%d", 1, &imageID);
+  }
+
+  // XXX should the first image ID be 1, not 0?
+  if (imageID == 0) imageID = 1;
+
+  // update the image IDs already written to stars and images:
+  for (i = 0; i < Nimages; i++) {
+    images[0].imageID += imageID;
+  }
+
+  for (i = 0; i < Nstars; i++) {
+    stars[0].imageID += imageID;
+  }
+
+  imageID += Nimages;
+  status = gfits_modify (&db.header, "IMAGEID", "%d", 1, imageID);
+  
+  // write PHU
+  // position to start of file
+  Fseek (db.f, 0, SEEK_SET);
+  SetProtect (TRUE);
+  if (!gfits_fwrite_header (db.f, &db.header)) {
+    Shutdown ("can't write image table phu %s", db.filename);
+  }
+  SetProtect (FALSE);
+
+  dvo_image_unlock (&db);
+
+  return 
+}
