Index: /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/include/dvo.h	(revision 33868)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/include/dvo.h	(revision 33869)
@@ -69,4 +69,7 @@
   NAN_U_INT   = (signed int) 0xffffffff,
 } DVO_INT_NAN;
+
+// max path length
+# define DVO_MAX_PATH 1024
 
 /* RegImage.flag values */
@@ -268,5 +271,5 @@
   int Nhosts;
   HostInfo *hosts;
-  unsigned short *index;
+  short *index;
 } HostTable;
 
@@ -516,4 +519,7 @@
 void dvo_catalog_test (Catalog *catalog, int halt);
 
+int dvo_catalog_backup (Catalog *catalog, int primary);
+int dvo_catalog_unlink_backup (Catalog *catalog, int primary);
+
 /* catmode-specific APIs */
 int dvo_catalog_load_raw (Catalog *catalog, int VERBOSE);
Index: /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/HostTable.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/HostTable.c	(revision 33868)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/HostTable.c	(revision 33869)
@@ -121,9 +121,9 @@
   table->hosts = hosts;
 
-  ALLOCATE (table->index, unsigned short, maxID + 1);
+  ALLOCATE (table->index, short, maxID + 1);
   for (i = 0; i <= maxID; i++) table->index[i] = -1;
 
   for (i = 0; i < table->Nhosts; i++) {
-    if (table->index[table->hosts[i].hostID] == -1) {
+    if (table->index[table->hosts[i].hostID] != -1) {
       fprintf (stderr, "error: duplicate hostID %d\n", table->hosts[i].hostID);
       exit (1);
Index: /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/dvo_catalog.c	(revision 33868)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/dvo_catalog.c	(revision 33869)
@@ -499,2 +499,103 @@
 }
 
+// make a backup of this catalog (including the measure, secfilt, and missing tables as needed)
+int dvo_catalog_backup (Catalog *catalog, int primary) {
+
+  // skip empty cpt files
+  if (primary && !catalog->Naves_disk) {
+    return TRUE;
+  }
+
+  char tmpfilename[DVO_MAX_PATH];
+  int status = snprintf (tmpfilename, DVO_MAX_PATH, "%s~", catalog->filename);
+  if (status >= DVO_MAX_PATH) {
+    fprintf (stderr, "path name too long: %s\n", catalog->filename);
+    return FALSE;
+  }
+      
+  // unlock the catalog here (closes file as well) (also closes subcat files Measure, Secfilt, Missing)
+  if (primary) {
+    status = dvo_catalog_unlock (catalog);
+    if (!status) {
+      fprintf (stderr, "failed to unlock catalog %s\n", catalog->filename);
+      return FALSE;
+    }
+  }
+
+  status = rename (catalog->filename, tmpfilename);
+  if (status) {
+    fprintf (stderr, "failed to rename catalog %s\n", catalog->filename);
+    return FALSE;
+  }
+      
+  // keep the same lockmode
+  int lockmode = catalog->lockmode;
+
+  // lock the new catalog file here (re-opens file as well) (does NOT open/lock the subcat files)
+  status = dvo_catalog_lock (catalog, lockmode);
+  if (!status) {
+    fprintf (stderr, "failed to lock new catalog file %s\n", catalog->filename);
+    return FALSE;
+  }
+
+  if (catalog[0].catmode == DVO_MODE_SPLIT) {
+    if (catalog[0].measure_catalog != NULL) {
+      if (!dvo_catalog_backup (catalog[0].measure_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+    if (catalog[0].missing_catalog != NULL) {
+      if (!dvo_catalog_backup (catalog[0].missing_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+    if (catalog[0].secfilt_catalog != NULL) {
+      if (!dvo_catalog_backup (catalog[0].secfilt_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+  }
+  return TRUE;
+}
+
+// make a backup of this catalog (including the measure, secfilt, and missing tables as needed)
+int dvo_catalog_unlink_backup (Catalog *catalog, int primary) {
+
+  if (primary && !catalog->Naves_disk) {
+    // skip empty files (empty when read, but output may not be empty)
+    return TRUE;
+  }
+
+  char tmpfilename[DVO_MAX_PATH];
+  int status = snprintf (tmpfilename, DVO_MAX_PATH, "%s~", catalog->filename);
+  if (status >= DVO_MAX_PATH) {
+    fprintf (stderr, "path name too long: %s\n", catalog->filename);
+    return FALSE;
+  }
+      
+  status = unlink (tmpfilename);
+  if (status) {
+    fprintf (stderr, "failed to unlink catalog %s\n", catalog->filename);
+    return FALSE;
+  }
+      
+  if (catalog[0].catmode == DVO_MODE_SPLIT) {
+    if (catalog[0].measure_catalog != NULL) {
+      if (!dvo_catalog_unlink_backup (catalog[0].measure_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+    if (catalog[0].missing_catalog != NULL) {
+      if (!dvo_catalog_unlink_backup (catalog[0].missing_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+    if (catalog[0].secfilt_catalog != NULL) {
+      if (!dvo_catalog_unlink_backup (catalog[0].secfilt_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+  }
+  return TRUE;
+}
+
Index: /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 33868)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 33869)
@@ -80,5 +80,5 @@
   // write PHU header
   if (!gfits_fwrite_header  (catalog->f, &catalog->header)) {
-    fprintf (stderr, "can't write primary header");
+    fprintf (stderr, "can't write primary header\n");
     return (FALSE);
   }
@@ -87,5 +87,5 @@
   gfits_create_matrix (&catalog->header, &matrix);
   if (!gfits_fwrite_matrix  (catalog->f, &matrix)) {
-    fprintf (stderr, "can't write primary matrix");
+    fprintf (stderr, "can't write primary matrix\n");
     gfits_free_matrix (&matrix);
     return (FALSE);
@@ -95,5 +95,5 @@
   // write the table data
   if (!gfits_fwrite_ftable_range (catalog->f, ftable, start, Nrows, Ndisk, Ntotal)) {
-    fprintf (stderr, "can't write table data");
+    fprintf (stderr, "can't write table data\n");
     return (FALSE);
   }
