Index: trunk/Ohana/src/relphot/Makefile
===================================================================
--- trunk/Ohana/src/relphot/Makefile	(revision 8328)
+++ trunk/Ohana/src/relphot/Makefile	(revision 8386)
@@ -28,5 +28,4 @@
 $(SRC)/select_images.$(ARCH).o	 \
 $(SRC)/load_catalogs.$(ARCH).o	 \
-$(SRC)/gcatalog.$(ARCH).o 	 \
 $(SRC)/bcatalog.$(ARCH).o	 \
 $(SRC)/GridOps.v2.$(ARCH).o	 \
@@ -37,14 +36,11 @@
 $(SRC)/plot_scatter.$(ARCH).o	 \
 $(SRC)/plotstuff.$(ARCH).o	 \
-$(SRC)/misc.$(ARCH).o		 \
+$(SRC)/sort.$(ARCH).o		 \
 $(SRC)/reload_catalogs.$(ARCH).o \
-$(SRC)/image-db.$(ARCH).o \
-$(SRC)/free_catalogs.$(ARCH).o   \
 $(SRC)/setMrelFinal.$(ARCH).o 	 \
 $(SRC)/write_coords.$(ARCH).o 	 \
 $(SRC)/setExclusions.$(ARCH).o 	 \
 $(SRC)/Shutdown.$(ARCH).o 	 \
-$(SRC)/SetSignals.$(ARCH).o 	 \
-$(SRC)/wcatalog.$(ARCH).o
+$(SRC)/SetSignals.$(ARCH).o 	
 
 OLD = \
Index: trunk/Ohana/src/relphot/src/gcatalog.c
===================================================================
--- trunk/Ohana/src/relphot/src/gcatalog.c	(revision 8328)
+++ 	(revision )
@@ -1,26 +1,0 @@
-# include "relphot.h"
-
-int gcatalog (Catalog *catalog, int FINAL) {
-  
-  if (FINAL) {
-    catalog[0].catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-  } else {
-    catalog[0].catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
-  }    
-
-  /* CATMODE and CATFORMAT determined from catalog */
-
-  if (!load_catalog (catalog, VERBOSE)) {
-    fprintf (stderr, "ERROR: failure loading catalog %s\n", catalog[0].filename);
-    exit (1);
-  }
-
-  if (VERBOSE) {
-    fprintf (stderr, "read %d stars from catalog file %s (%d measurements)\n", 
-	     catalog[0].Naverage, catalog[0].filename, catalog[0].Nmeasure);
-  }
-  return (TRUE);
-}
-
-
-
Index: trunk/Ohana/src/relphot/src/load_catalogs.c
===================================================================
--- trunk/Ohana/src/relphot/src/load_catalogs.c	(revision 8328)
+++ trunk/Ohana/src/relphot/src/load_catalogs.c	(revision 8386)
@@ -8,36 +8,28 @@
   if (VERBOSE) fprintf (stderr, "loading catalog data\n");
 
-  // XXX skip over the empty catalogs? don't add to the list?
   ALLOCATE (catalog, Catalog, skylist[0].Nregions);
 
-  /* load data from each region file, only use bright stars */
-  // we use LCK_SOFT since we assume the image is locked 
+  // load data from each region file, only use bright stars
   for (i = 0; i < skylist[0].Nregions; i++) {
+
+    // set up the basic catalog info
     tcatalog.filename = skylist[0].filename[i];
+    tcatalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
+    tcatalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
+    tcatalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_SECF; // don't need to load all data at this point
 
-    tcatalog.catformat = dvo_catalog_format (CATFORMAT);  // set the default catformat from config data
-    tcatalog.catmode   = dvo_catalog_mode (CATMODE);      // set the default catmode from config data
-    tcatalog.lockmode  = LCK_SOFT;
-    dvo_catalog_open (&tcatalog, skylist[0].regions[i], Nsecfilt, "r");
-
-    switch (lock_catalog (&tcatalog, LCK_SOFT)) {
-    case 0:
-      fprintf (stderr, "ERROR: can't lock file %s\n", tcatalog.filename);
-      exit (1);
-    case 1:
-      gcatalog (&tcatalog, FALSE); /* load from disk */
-      break;
-    case 2:
-      if (VERBOSE) fprintf (stderr, "no data in %s, skipping\n", tcatalog.filename);
-      tcatalog.Naverage = 0;
-      tcatalog.Nmeasure = 0;
-      break;
-    default:
-      fprintf (stderr, "weird lock_catalog exit state\n");
+    if (!dvo_catalog_open (&tcatalog, skylist[0].regions[i], VERBOSE, "r")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", tcatalog.filename);
       exit (1);
     }
+    if (VERBOSE && !tcatalog.Nave_disk) fprintf (stderr, "no data in %s, skipping\n", tcatalog.filename);
+
+    // select only the brighter stars
     bcatalog (&catalog[i], &tcatalog);
+    dvo_catalog_unlock (&tcatalog);
+    dvo_catalog_free (&tcatalog);
   }
 
+  // XXX keep this test?
   Nstar = 0;
   for (i = 0; i < skylist[0].Nregions; i++) {
@@ -49,5 +41,12 @@
   }
 
+  // XXX consider only returning the populated catalogs
   *Ncatalog = skylist[0].Nregions;
   return (catalog);
 }
+
+/* this function loads all relevant catalog files for the first pass.  it currently loads the data
+   read only (SOFT lock) since it assumes the image table has been locked. if we go to the new
+   addstar locking paradigm, in which the images and catalogs are updated independently, then we may
+   need to use an XCLD lock here.  
+*/
Index: trunk/Ohana/src/relphot/src/misc.c
===================================================================
--- trunk/Ohana/src/relphot/src/misc.c	(revision 8328)
+++ 	(revision )
@@ -1,182 +1,0 @@
-# include "relphot.h"
-
-void sortA (double *X, int N) {
-
-  int l,j,ir,i;
-  double tX;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-  }
-}
-
-void sortB (double *X, double *Y, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-  }
-}
-
-void sortC (double *X, double *Y, double *F1, double *F2, int N) {
-
-  int l,j,ir,i;
-  double tX, tY;
-  double t1, t2;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      t1 = F1[l];
-      t2 = F2[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      t1 = F1[ir];
-      F1[ir] = F1[0];
-      t2 = F2[ir];
-      F2[ir] = F2[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	F1[0] = t1;
-	F2[0] = t2;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	F1[i] = F1[j];
-	F2[i] = F2[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    F1[i] = t1;
-    F2[i] = t2;
-  }
-}
-
-void sortD (double *X, double *Y, double *Z, int N) {
-
-  int l,j,ir,i;
-  double tX, tY, tZ;
-  
-  if (N < 2) return;
-
-  l = N >> 1;
-  ir = N - 1;
-  for (;;) {
-    if (l > 0) {
-      l--;
-      tX = X[l];
-      tY = Y[l];
-      tZ = Z[l];
-    }
-    else {
-      tX = X[ir];
-      X[ir] = X[0];
-      tY = Y[ir];
-      Y[ir] = Y[0];
-      tZ = Z[ir];
-      Z[ir] = Z[0];
-      if (--ir == 0) {
-	X[0] = tX;
-	Y[0] = tY;
-	Z[0] = tZ;
-	return;
-      }
-    }
-    i = l;
-    j = (l << 1) + 1;
-    while (j <= ir) {
-      if (j < ir && X[j] < X[j+1]) j++;
-      if (tX < X[j]) {
-	X[i] = X[j];
-	Y[i] = Y[j];
-	Z[i] = Z[j];
-	j += (i=j) + 1;
-      }
-      else j = ir + 1;
-    }
-    X[i] = tX;
-    Y[i] = tY;
-    Z[i] = tZ;
-  }
-}
Index: trunk/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- trunk/Ohana/src/relphot/src/reload_catalogs.c	(revision 8328)
+++ trunk/Ohana/src/relphot/src/reload_catalogs.c	(revision 8386)
@@ -14,5 +14,5 @@
     catalog.filename = skylist[0].filename[i];
 
-    // XXX only update existing db tables
+    // only update existing db tables
     status = stat (catalog.filename, &filestat);
     if ((status == -1) && (errno == ENOENT)) {
@@ -20,19 +20,20 @@
       continue;
     }
-    switch (lock_catalog (&catalog, LCK_XCLD)) {
-      case 0: // file cannot be locked
-	fprintf (stderr, "ERROR: can't lock file %s\n", catalog.filename);
-	exit (1);
-      case 1: // file is now locked
-	gcatalog (&catalog, TRUE); /* load from disk */
-	break;
-      case 2: // file is empty
-	if (VERBOSE) fprintf (stderr, "no data in file %s, skipping\n", catalog.filename);
-	unlock_catalog (&catalog);
+
+    catalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
+    catalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+
+    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog.filename);
+      exit (1);
+    }
+    if (VERBOSE && (catalog.Nave_disk == 0)) {
+	fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+	dvo_catalog_unlock (&catalog);
+	dvo_catalog_free (&catalog);
 	continue;
-      default:
-	fprintf (stderr, "weird lock_catalog exit state %s\n", catalog.filename);
-	exit (1);
     }
+
     initImageBins  (&catalog, 1);
     initMosaicBins (&catalog, 1);
@@ -43,6 +44,7 @@
 
     setMrelFinal (&catalog);
-    wcatalog (&catalog); 
-    unlock_catalog (&catalog);
+    dvo_catalog_save (&catalog, VERBOSE); 
+    dvo_catalog_unlock (&catalog);
+    dvo_catalog_free (&catalog);
 
     freeImageBins (1);
Index: trunk/Ohana/src/relphot/src/relphot.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot.c	(revision 8328)
+++ trunk/Ohana/src/relphot/src/relphot.c	(revision 8386)
@@ -88,5 +88,5 @@
 
   /* at this point, we have correct cal coeffs in the image/mosaic structures */
-  free_catalogs (catalog, Ncatalog);
+  for (i = 0; i < Ncatalog; i++) dvo_catalog_free (&catalog[i]);
   freeImageBins (Ncatalog);
   freeMosaicBins (Ncatalog);
Index: trunk/Ohana/src/relphot/src/sort.c
===================================================================
--- trunk/Ohana/src/relphot/src/sort.c	(revision 8386)
+++ trunk/Ohana/src/relphot/src/sort.c	(revision 8386)
@@ -0,0 +1,182 @@
+# include "relphot.h"
+
+void sortA (double *X, int N) {
+
+  int l,j,ir,i;
+  double tX;
+  
+  if (N < 2) return;
+
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+    }
+    else {
+      tX = X[ir];
+      X[ir] = X[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+  }
+}
+
+void sortB (double *X, double *Y, int N) {
+
+  int l,j,ir,i;
+  double tX, tY;
+  
+  if (N < 2) return;
+
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+      tY = Y[l];
+    }
+    else {
+      tX = X[ir];
+      X[ir] = X[0];
+      tY = Y[ir];
+      Y[ir] = Y[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	Y[0] = tY;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	Y[i] = Y[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+    Y[i] = tY;
+  }
+}
+
+void sortC (double *X, double *Y, double *F1, double *F2, int N) {
+
+  int l,j,ir,i;
+  double tX, tY;
+  double t1, t2;
+  
+  if (N < 2) return;
+
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+      tY = Y[l];
+      t1 = F1[l];
+      t2 = F2[l];
+    }
+    else {
+      tX = X[ir];
+      X[ir] = X[0];
+      tY = Y[ir];
+      Y[ir] = Y[0];
+      t1 = F1[ir];
+      F1[ir] = F1[0];
+      t2 = F2[ir];
+      F2[ir] = F2[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	Y[0] = tY;
+	F1[0] = t1;
+	F2[0] = t2;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	Y[i] = Y[j];
+	F1[i] = F1[j];
+	F2[i] = F2[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+    Y[i] = tY;
+    F1[i] = t1;
+    F2[i] = t2;
+  }
+}
+
+void sortD (double *X, double *Y, double *Z, int N) {
+
+  int l,j,ir,i;
+  double tX, tY, tZ;
+  
+  if (N < 2) return;
+
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+      tY = Y[l];
+      tZ = Z[l];
+    }
+    else {
+      tX = X[ir];
+      X[ir] = X[0];
+      tY = Y[ir];
+      Y[ir] = Y[0];
+      tZ = Z[ir];
+      Z[ir] = Z[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	Y[0] = tY;
+	Z[0] = tZ;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	Y[i] = Y[j];
+	Z[i] = Z[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+    Y[i] = tY;
+    Z[i] = tZ;
+  }
+}
