Index: /branches/eam_branches/ipp-20130306/Ohana/src/addstar/include/addstar.h
===================================================================
--- /branches/eam_branches/ipp-20130306/Ohana/src/addstar/include/addstar.h	(revision 35255)
+++ /branches/eam_branches/ipp-20130306/Ohana/src/addstar/include/addstar.h	(revision 35256)
@@ -67,5 +67,5 @@
   IDTYPE *externID;
   char *found;
-} ExtID_Index;
+} ImageIndex;
 
 typedef struct sockaddr_in SockAddress;
@@ -288,4 +288,5 @@
 
 int CheckDuplicateImageIDs (Image *images, off_t Nimages);
+int ImageIndexFileInit ();
 
 int LoadDataSDSS (FILE *f, char *file, Image **images, off_t *nvalid, Stars **stars, unsigned int *Nstars, Header **headers, off_t *extsize, HeaderSet *headerSets, off_t Nimages);
Index: /branches/eam_branches/ipp-20130306/Ohana/src/addstar/src/ExternImageIDs.c
===================================================================
--- /branches/eam_branches/ipp-20130306/Ohana/src/addstar/src/ExternImageIDs.c	(revision 35255)
+++ /branches/eam_branches/ipp-20130306/Ohana/src/addstar/src/ExternImageIDs.c	(revision 35256)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-ExtID_Index *ExtID_Load (char *filename) {
+ImageIndex *ImageIndexLoad (char *filename) {
 
   int i, Ncol;
@@ -10,10 +10,10 @@
   FTable ftable;
 
-  ExtID_Index *extID = NULL;
-  ALLOCATE (extID, ExtID_Index, 1);
+  ImageIndex *index = NULL;
+  ALLOCATE (index, ImageIndex, 1);
 
   FILE *f = fopen (filename, "r");
   if (!f) {
-    fprintf (stderr, "ERROR: cannot open imageID/externID file %s\n", filename);
+    fprintf (stderr, "ERROR: cannot open image index file %s\n", filename);
     return NULL;
   }
@@ -21,10 +21,10 @@
   /* load in PHU segment (ignore) */
   if (!gfits_fread_header (f, &header)) {
-    if (VERBOSE) fprintf (stderr, "can't read imageID/externID header\n");
+    if (VERBOSE) fprintf (stderr, "can't read image index header\n");
     fclose (f);
     return NULL;
   }
   if (!gfits_fread_matrix (f, &matrix, &header)) {
-    if (VERBOSE) fprintf (stderr, "can't read imageID/externID matrix\n");
+    if (VERBOSE) fprintf (stderr, "can't read image index matrix\n");
     gfits_free_header (&header);
     fclose (f);
@@ -48,33 +48,33 @@
 
   char type[16];
-  extID->externID = gfits_get_bintable_column_data (&theader, &ftable, "EXTERN_ID", type, &Nrow, &Ncol);
+  index->externID = gfits_get_bintable_column_data (&theader, &ftable, "EXTERN_ID", type, &Nrow, &Ncol);
   myAssert (!strcmp(type, "int"), "wrong column type");
 
-  extID->imageID  = gfits_get_bintable_column_data (&theader, &ftable, "IMAGE_ID", type, &Nrow, &Ncol);
+  index->imageID  = gfits_get_bintable_column_data (&theader, &ftable, "IMAGE_ID", type, &Nrow, &Ncol);
   myAssert (!strcmp(type, "int"), "wrong column type");
 
-  extID->Nimage = Nrow;
+  index->Nimage = Nrow;
 
   // find the range of externID values
-  extID->minID = -1;
-  extID->maxID = -1;
-  for (i = 0; i < extID->Nimage; i++) {
-    if (extID->minID == -1) extID->minID = extID->externID[i];
-    extID->minID = MIN(extID->minID, extID->externID[i]);
-    extID->maxID = MAX(extID->maxID, extID->externID[i]);
-  }
-  extID->range = extID->maxID- extID->minID + 1;
+  index->minID = -1;
+  index->maxID = -1;
+  for (i = 0; i < index->Nimage; i++) {
+    if (index->minID == -1) index->minID = index->externID[i];
+    index->minID = MIN(index->minID, index->externID[i]);
+    index->maxID = MAX(index->maxID, index->externID[i]);
+  }
+  index->range = index->maxID- index->minID + 1;
   
   // init the index
-  ALLOCATE (extID->found, char, extID->range);
-  for (i = 0; i < extID->range; i++) extID->found[i] = FALSE;
+  ALLOCATE (index->found, char, index->range);
+  for (i = 0; i < index->range; i++) index->found[i] = FALSE;
 
   // generate the index (set this value to the imageID?)
-  for (i = 0; i < extID->Nimage; i++) {
-    IDTYPE N = extID->externID[i] - extID->minID;
-    extID->found[N] = TRUE;
-  }
-
-  return extID;
+  for (i = 0; i < index->Nimage; i++) {
+    IDTYPE N = index->externID[i] - index->minID;
+    index->found[N] = TRUE;
+  }
+
+  return index;
 }
 
@@ -86,5 +86,5 @@
   }
 
-int ExtID_Save (char *filename, ExtID_Index *extID) {
+int ImageIndexSave (char *filename, ImageIndex *index) {
 
   Header header;
@@ -106,10 +106,10 @@
 
   // add the columns to the output array
-  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID",       extID->imageID,  extID->Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "EXTERN_ID",      extID->externID, extID->Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID",       index->imageID,  index->Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "EXTERN_ID",      index->externID, index->Nimage);
 
   FILE *f = fopen (filename, "w");
   if (!f) {
-    fprintf (stderr, "ERROR: cannot open extID file for output %s\n", filename);
+    fprintf (stderr, "ERROR: cannot open index file for output %s\n", filename);
     return FALSE;
   }
@@ -117,14 +117,14 @@
   int status;
   status = gfits_fwrite_header  (f, &header);
-  CHECK_STATUS (status, "ERROR: cannot write header for imageID/externID %s\n", filename);
+  CHECK_STATUS (status, "ERROR: cannot write header for image index %s\n", filename);
 
   status = gfits_fwrite_matrix  (f, &matrix);
-  CHECK_STATUS (status, "ERROR: cannot write matrix for imageID/externID %s\n", filename);
+  CHECK_STATUS (status, "ERROR: cannot write matrix for image index %s\n", filename);
 
   status = gfits_fwrite_Theader (f, &theader);
-  CHECK_STATUS (status, "ERROR: cannot write table header for imageID/externID %s\n", filename);
+  CHECK_STATUS (status, "ERROR: cannot write table header for image index %s\n", filename);
 
   status = gfits_fwrite_table  (f, &ftable);
-  CHECK_STATUS (status, "ERROR: cannot write table data for imageID/externID %s\n", filename);
+  CHECK_STATUS (status, "ERROR: cannot write table data for image index %s\n", filename);
 
   gfits_free_header (&header);
@@ -136,11 +136,11 @@
 
   status = fflush (f);
-  CHECK_STATUS (!status, "ERROR: cannot flush file imageID/externID %s\n", filename);
+  CHECK_STATUS (!status, "ERROR: cannot flush file image index %s\n", filename);
 
   status = fsync (fd);
-  CHECK_STATUS (!status, "ERROR: cannot flush file imageID/externID %s\n", filename);
+  CHECK_STATUS (!status, "ERROR: cannot fsync file image index %s\n", filename);
 
   status = fclose (f);
-  CHECK_STATUS (!status, "ERROR: problem closing imageID/externID file %s\n", filename);
+  CHECK_STATUS (!status, "ERROR: problem closing image index file %s\n", filename);
 
   return TRUE;
@@ -153,47 +153,60 @@
   // load the image ID table
   char filename[DVO_MAX_PATH];
-  snprintf (filename, DVO_MAX_PATH, "%s/ExternImageIDs.fits", CATDIR);
-
-  ExtID_Index *extID = ExtID_Load (filename);
-
-  // if we 
-
-  for (i = 0; extID && (i < Nimages); i++) {
+  snprintf (filename, DVO_MAX_PATH, "%s/ImageIndex.fits", CATDIR);
+
+  ImageIndex *index = ImageIndexLoad (filename);
+
+  if (!index) {
+      fprintf (stderr, "image index file is not found, cannot check for image duplicates\n");
+      exit (2);
+  }
+
+  for (i = 0; i < Nimages; i++) {
     IDTYPE ID = images[i].externID;
     if (ID == 0) continue;
 
-    if (ID < extID->minID) continue;
-    if (ID > extID->maxID) continue;
-
-    offset = ID - extID->minID;
+    if (ID < index->minID) continue;
+    if (ID > index->maxID) continue;
+
+    offset = ID - index->minID;
     myAssert (offset >= 0, "offset out of range?");
-    myAssert (offset <= extID->range, "offset out of range?");
-
-    if (!extID->found[offset]) continue;
+    myAssert (offset <= index->range, "offset out of range?");
+
+    if (!index->found[offset]) continue;
     fprintf (stderr, "duplicate external image ID %lld found, exiting\n", (long long) ID);
     exit (1);
   }
   
-  if (!extID) {
-    ALLOCATE (extID, ExtID_Index, 1);
-    ALLOCATE (extID->imageID,  IDTYPE, 1);
-    ALLOCATE (extID->externID, IDTYPE, 1);
-    extID->Nimage = 0;
-  }
-
   // extend the storage arrays
-  REALLOCATE (extID->imageID,  IDTYPE, extID->Nimage + Nimages);
-  REALLOCATE (extID->externID, IDTYPE, extID->Nimage + Nimages);
+  REALLOCATE (index->imageID,  IDTYPE, index->Nimage + Nimages);
+  REALLOCATE (index->externID, IDTYPE, index->Nimage + Nimages);
 
   // append the new ext IDs and save
   for (i = 0; i < Nimages; i++) {
     if (images[i].externID == 0) continue;
-    extID->imageID[extID->Nimage] = images[i].imageID;
-    extID->externID[extID->Nimage] = images[i].externID;
-    extID->Nimage ++;
+    index->imageID[index->Nimage] = images[i].imageID;
+    index->externID[index->Nimage] = images[i].externID;
+    index->Nimage ++;
   }
 
   // do this as an 'extend' operation
-  ExtID_Save (filename, extID);
+  ImageIndexSave (filename, index);
   return TRUE;
 }
+
+int ImageIndexFileInit () {
+
+  // load the image ID table
+  char filename[DVO_MAX_PATH];
+  snprintf (filename, DVO_MAX_PATH, "%s/ImageIndex.fits", CATDIR);
+
+  ImageIndex *index = NULL;
+  ALLOCATE (index, ImageIndex, 1);
+  ALLOCATE (index->imageID,  IDTYPE, 1);
+  ALLOCATE (index->externID, IDTYPE, 1);
+  index->Nimage = 0;
+
+  // do this as an 'extend' operation
+  ImageIndexSave (filename, index);
+  return TRUE;
+}
Index: /branches/eam_branches/ipp-20130306/Ohana/src/addstar/src/UpdateImageIDs.c
===================================================================
--- /branches/eam_branches/ipp-20130306/Ohana/src/addstar/src/UpdateImageIDs.c	(revision 35255)
+++ /branches/eam_branches/ipp-20130306/Ohana/src/addstar/src/UpdateImageIDs.c	(revision 35256)
@@ -18,5 +18,6 @@
     if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
     dvo_image_create (&db, GetZeroPoint());
-    isEmpty = TRUE;
+    ImageIndexFileInit ();
+  isEmpty = TRUE;
   } else {
     /* position to start of file */
@@ -52,4 +53,5 @@
 
   // set and update the imageID sequence
+  // the file holding the index is created above if this is an empty db
   CheckDuplicateImageIDs (images, Nimages);
 
Index: /branches/eam_branches/ipp-20130306/Ohana/src/addstar/test/simple.dvo
===================================================================
--- /branches/eam_branches/ipp-20130306/Ohana/src/addstar/test/simple.dvo	(revision 35255)
+++ /branches/eam_branches/ipp-20130306/Ohana/src/addstar/test/simple.dvo	(revision 35256)
@@ -52,4 +52,5 @@
   exec mkcmf test.in.txt test.cmf -date 2008/1/1 -time 01:00:00 -radec $RA $DEC -type $1
   if ($TAP_VERBOSE)
+    echo exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2 -quick-airmass
     exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2 -quick-airmass
   else
