Index: /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/Makefile
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/Makefile	(revision 38712)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/Makefile	(revision 38713)
@@ -124,4 +124,5 @@
 $(SRC)/dvorepair.$(ARCH).o \
 $(SRC)/dvorepairFixCPT.$(ARCH).o \
+$(SRC)/dvorepairFixWarpIDs.$(ARCH).o \
 $(SRC)/dvorepairImagesVsMeasures.$(ARCH).o \
 $(SRC)/dvorepairDeleteImageList.$(ARCH).o \
Index: /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/include/dvomerge.h
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/include/dvomerge.h	(revision 38712)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/include/dvomerge.h	(revision 38713)
@@ -246,3 +246,4 @@
 
 int dvorepair_by_objID (int argc, char **argv);
-
+int dvorepairFixWarpIDs (int argc, char **argv);
+
Index: /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepair.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepair.c	(revision 38712)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepair.c	(revision 38713)
@@ -10,4 +10,6 @@
   dvorepair_help(argc, argv);
   
+  if (!strcmp(argv[1], "-fix-warp-ids")) dvorepairFixWarpIDs(argc, argv);
+
   if (!strcmp(argv[1], "-fix-cpt")) dvorepairFixCPT(argc, argv);
   if (!strcmp(argv[1], "-fix-cpt-by-objID")) dvorepair_by_objID(argc, argv);
Index: /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepairFixImages.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepairFixImages.c	(revision 38712)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepairFixImages.c	(revision 38713)
@@ -255,5 +255,5 @@
 
   /* setup image table format and lock */
-  gfits_db_init (oldDB);
+  gfits_db_init (&db);
   db.mode   = oldDB->mode;
   db.format = oldDB->format;
@@ -266,7 +266,8 @@
   /* load or create the image table */
   myAssert (db.dbstate == LCK_EMPTY, "do not overwrite exiting image table");
-  myAssert (db.format == DVO_FORMAT_PS1_V2, "format mismatch");
+  // myAssert (db.format == DVO_FORMAT_PS1_V2, "format mismatch");
 
   dvo_image_create (&db, GetZeroPoint());
+  gfits_copy_header (&oldDB->header, &db.header);
   
   // replace the existing buffer with the image array
@@ -277,6 +278,8 @@
   nbytes = gfits_data_size (db.ftable.header);
   db.ftable.datasize = nbytes;
-  db.ftable.buffer = (char *) imageOut;
-  REALLOCATE (db.ftable.buffer, char, MAX (nbytes, 1));
+
+  free (db.ftable.buffer);
+  ALLOCATE (db.ftable.buffer, char, MAX (nbytes, 1));
+  memcpy (db.ftable.buffer, imageOut, nbytes);
   memset (&db.ftable.buffer[Nx*Nout], ' ', nbytes - Nx*Nout);
 
@@ -295,4 +298,6 @@
   dvo_image_unlock (&db);
 
+  gfits_db_free (&db); 
+
   return TRUE;
 }
Index: /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepairFixWarpIDs.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepairFixWarpIDs.c	(revision 38713)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/dvorepairFixWarpIDs.c	(revision 38713)
@@ -0,0 +1,232 @@
+# include "dvomerge.h"
+
+static int *warpIDs = NULL;
+static int *projIDs = NULL;
+static int *cellIDs = NULL;
+static int *fileIDs = NULL;
+
+# define STRFAIL { fprintf (stderr, "failure on image %s\n", image[i].name); continue; }
+
+int load_warp_ids (char *idfile, int *nfiles);
+int warpIDs_bisection (int *warpIDs, int threshold, int Nvalues);
+
+int dvorepairFixWarpIDs (int argc, char **argv) {
+
+  FITS_DB db;  // database handle pointing to input image table
+
+  off_t Nimage;
+  
+  int N;
+
+  Image *image;
+
+  N = get_argument (argc, argv, "-fix-warp-ids");
+  myAssert(N == 1, "programming error: -fix-images must be first from main");
+  remove_argument (N, &argc, argv);
+
+  if (argc != 3) {
+    fprintf (stderr, "USAGE: dvorepair -fix-warp-ids (catdir.list) (warp.ids)\n");
+    fprintf (stderr, "  catdir.list : list of databases of interest\n");
+    fprintf (stderr, "  warp.ids : list of warp_id, skycell.id, warp_skyfile_id map\n");
+    exit (2);
+  }
+
+  char *catdir_list  = argv[1];
+  char *idfile = argv[2];
+
+  // load the warp_id table
+  int Nfiles = 0;
+  load_warp_ids (idfile, &Nfiles);
+  fprintf (stderr, "loaded %d warp,proj,cell,file matches\n", Nfiles);
+
+  char name[DVO_MAX_PATH];
+  char catdir[DVO_MAX_PATH];
+  char imageFilenameOld[DVO_MAX_PATH];
+  char imageFilenameNew[DVO_MAX_PATH];
+
+  // read the list of catdirs and fix image tables for each 
+
+  FILE *f = fopen (catdir_list, "r");
+  myAssert (f, "failed to open catdir.list %s\n", catdir_list);
+
+  while (fscanf (f, "%s", catdir) != EOF) {
+    snprintf (imageFilenameOld, DVO_MAX_PATH, "%s/Images.dat", catdir);
+    snprintf (imageFilenameNew, DVO_MAX_PATH, "%s/Images.dat.fixed", catdir);
+    
+    if ((image = LoadImages (&db, imageFilenameOld, &Nimage)) == NULL) {
+      fprintf (stderr, "error loading images\n");
+      exit (1);
+    }
+
+    int i, j;
+    for (i = 0; i < Nimage; i++) {
+      if (image[i].externID) continue;
+      
+      strcpy (name, image[i].name);
+    
+      char *p0 = strchr (name  , '.'); if (!p0) STRFAIL;
+      char *p1 = strchr (p0 + 1, '.'); if (!p1) STRFAIL;
+      char *p2 = strchr (p1 + 1, '.'); if (!p2) STRFAIL;
+      char *p3 = strchr (p2 + 1, '.'); if (!p3) STRFAIL;
+      char *p4 = strchr (p3 + 1, '.'); if (!p4) STRFAIL;
+      char *p5 = strchr (p4 + 1, '.'); if (!p5) STRFAIL;
+      char *p6 = strchr (p5 + 1, '.'); if (!p6) STRFAIL;
+    
+      *p6 = 0;
+      int myWarpID = atoi (p5 + 1);
+
+      *p3 = 0;
+      *p4 = 0;
+      int myProjID = atoi (p2 + 1);
+      int myCellID = atoi (p3 + 1);
+
+      int Nlo = warpIDs_bisection (warpIDs, myWarpID, Nfiles);
+
+      int found = FALSE;
+      for (j = Nlo; !found && (j < Nfiles) && (warpIDs[j] <= myWarpID); j++) {
+	if (projIDs[j] != myProjID) continue;
+	if (cellIDs[j] != myCellID) continue;
+
+	// fprintf (stderr, "found it! (%d,%d,%d) = (%d,%d,%d) : %d\n", myWarpID, myProjID, myCellID, warpIDs[j], projIDs[j], cellIDs[j], fileIDs[j]);
+
+	image[i].externID = fileIDs[j];
+	image[i].sourceID = 34;
+
+	found = TRUE;
+      }
+      if (!found) {
+	// fprintf (stderr, "did NOT find it! (%d,%d,%d)\n", myWarpID, myProjID, myCellID);
+      }
+    }
+    SaveImages(&db, imageFilenameNew, image, Nimage);
+    gfits_db_free (&db);
+  }
+  free (warpIDs);
+  free (projIDs);
+  free (cellIDs);
+  free (fileIDs);
+
+  fclose (f);
+  ohana_memdump (TRUE);
+
+  exit (0);
+}
+
+int load_warp_ids (char *idfile, int *nfiles) {
+
+  // load the warp ids
+  FILE *f = fopen (idfile, "r");
+  myAssert (f, "failed to open warp file");
+
+  int NBUFFER = 30000000;
+
+  char *buffer;
+  ALLOCATE (buffer, char, NBUFFER);
+
+  int Nfiles = 0;
+  int NFILES = 1000;
+  ALLOCATE (warpIDs, int, NFILES);
+  ALLOCATE (projIDs, int, NFILES);
+  ALLOCATE (cellIDs, int, NFILES);
+  ALLOCATE (fileIDs, int, NFILES);
+
+  int skipFirst = TRUE;
+
+  int Nstart = 0;
+  int Ntotal = 0;
+  while (TRUE) {
+    int Nbytes = NBUFFER - 1 - Nstart;
+    bzero (&buffer[Nstart], Nbytes + 1);
+
+    int Nread = fread (&buffer[Nstart], 1, Nbytes, f);
+
+    Ntotal += Nread;
+    fprintf (stderr, "reading block: %d %d %d %d\n", Nstart, Nbytes, Nread, Ntotal);
+
+    if (ferror (f)) {
+      perror ("error reading data file");
+      break;
+    }
+    if (Nread == 0) break; // end of the file
+
+    int Nlines = 0;
+
+    int bufferStatus = TRUE; 
+    char *c0 = buffer; // c0 always marks the start of a line
+    while (bufferStatus) {
+      char *c1 = strchr (c0, '\n'); // find the end of this current line
+      if (!c1) {
+	Nstart = strlen (c0);
+	memmove (buffer, c0, Nstart);
+	bufferStatus = FALSE;
+	continue;
+      }
+      *c1 = 0; // mark the end of the line 
+      Nlines ++;
+
+      if (skipFirst) {
+	skipFirst = FALSE;
+	c0 = c1 + 1;
+	continue;
+      }
+
+      int warp_id, proj_id, cell_id, file_id;
+      int Nscan = sscanf (c0, "%d skycell.%d.%d %d", &warp_id, &proj_id, &cell_id, &file_id);
+      myAssert (Nscan == 4, "invalid line");
+
+      warpIDs[Nfiles] = warp_id;
+      projIDs[Nfiles] = proj_id;
+      cellIDs[Nfiles] = cell_id;
+      fileIDs[Nfiles] = file_id;
+      Nfiles ++;
+
+      if (Nfiles == NFILES) {
+	NFILES += 1000;
+	REALLOCATE (warpIDs, int, NFILES);
+	REALLOCATE (projIDs, int, NFILES);
+	REALLOCATE (cellIDs, int, NFILES);
+	REALLOCATE (fileIDs, int, NFILES);
+      }
+      c0 = c1 + 1;
+    }
+  }
+  free (buffer);
+  fclose (f);
+
+  isortfour (warpIDs, projIDs, cellIDs, fileIDs, Nfiles);
+
+  *nfiles = Nfiles;
+  return TRUE;
+}
+
+// return the index of the last value < threshold 
+int warpIDs_bisection (int *values, int threshold, int Nvalues) {
+
+  int Nlo = 0; 
+  int Nhi = Nvalues - 1;
+
+  if (Nvalues < 1) return (-1);
+  if (threshold < values[Nlo]) return (-1);
+
+  if (Nvalues < 2) return (0);
+  if (threshold > values[Nhi]) return (-1);
+
+  int N;
+  while (Nhi - Nlo > 4) {
+    N = 0.5*(Nlo + Nhi);
+    if (values[N] < threshold) {
+      Nlo = MAX(N, 0);
+    } else {
+      Nhi = MIN(N + 1, Nvalues - 1);
+    }
+  }
+  // values[Nlo] < threshold 
+  // values[Nhi] >= threshold 
+
+  for (N = Nlo; N < Nhi; N++) {
+    if (values[N] >= threshold) {
+      return (N-1);
+    }
+  }
+  return (N);
+}
Index: /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/help.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/help.c	(revision 38712)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/dvomerge/src/help.c	(revision 38713)
@@ -172,4 +172,5 @@
 
   fprintf (stderr, "USAGE\n");
+  fprintf (stderr, "  dvorepair -fix-warp-ids (catdir) (idfile) - regenerate images.dat warp_skyfile_ids (EXTERN_ID) values\n");
   fprintf (stderr, "  dvorepair -fix-cpt (images) (rootlist) - regenerate cpt & cps files from the cpm files\n");
   fprintf (stderr, "  dvorepair -images-vs-measures (catdir) (Ntol) - find images with too many missing detections\n");
