Index: trunk/Ohana/src/addstar/src/addstar.c
===================================================================
--- trunk/Ohana/src/addstar/src/addstar.c	(revision 8304)
+++ trunk/Ohana/src/addstar/src/addstar.c	(revision 8328)
@@ -95,11 +95,14 @@
 
     catalog.filename = skylist[0].filename[i];
-    load_pt_catalog (&catalog, skylist[0].regions[i]);
+    catalog.catformat = dvo_catalog_format (CATFORMAT);  // set the default catformat from config data
+    catalog.catmode   = dvo_catalog_mode (CATMODE);      // set the default catmode from config data
+    catalog.lockmode  = LCK_XCLD;
+    dvo_catalog_open (&catalog, skylist[0].regions[i], Nsecfilt, "w");
 
     /* for only_match, skip empty catalogs XXX EAM : this leaves behind empty files */
     if ((catalog.Nave_disk == 0) && options.only_match) {
-      unlock_catalog (&catalog);
-      free (catalog.filename);
-      free_catalog (&catalog);
+      dvo_catalog_unlock (&catalog);
+      free (catalog.filename);  // can this be done within the dvo_catalog_free function?? 
+      dvo_catalog_free (&catalog);
       continue;
     }
@@ -136,13 +139,14 @@
 
     if (Nsubset == 0) {
+      // XXX remove empty catalogs
       unlock_catalog (&catalog);
     } else {
       SetProtect (TRUE);
-      if (!options.only_images) wcatalog (&catalog);
+      if (!options.only_images) dvo_catalog_save (&catalog);
       SetProtect (FALSE);
-      unlock_catalog (&catalog);
+      dvo_catalog_unlock (&catalog);
     }
-    free_catalog (&catalog);
-    free (catalog.filename);
+    dvo_catalog_free (&catalog);
+    free (catalog.filename); // XXX ????
 
     if (options.mode == M_REFCAT) free (stars);
Index: trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches.c	(revision 8304)
+++ trunk/Ohana/src/addstar/src/find_matches.c	(revision 8328)
@@ -1,7 +1,7 @@
 # include "addstar.h"
 
-void find_matches (SkyRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
-
-  int i, j, n, N, J;
+void find_matches (SkyRegion *region, Stars *stars, int NstarsIn, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
+
+  int i, j, n, N, J, status, Nstars;
   double X, Y, RADIUS, RADIUS2, secz;
   float *X1, *Y1, *X2, *Y2;
@@ -23,7 +23,7 @@
 
   /** allocate local arrays (stars) **/
-  ALLOCATE (X1, float, Nstars);
-  ALLOCATE (Y1, float, Nstars);
-  ALLOCATE (N1, int,   Nstars);
+  ALLOCATE (X1, float, NstarsIn);
+  ALLOCATE (Y1, float, NstarsIn);
+  ALLOCATE (N1, int,   NstarsIn);
 
   /** allocate local arrays (catalog) **/
@@ -55,11 +55,26 @@
     tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
     tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+    tcoords.Npolyterms = 1;
     strcpy (tcoords.ctype, "RA---TAN");
   }
 
   /* build spatial index (RA sort) */
-  for (i = 0; i < Nstars; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], stars[i].R, stars[i].D, &tcoords);
-    N1[i] = i;
+  Nstars = 0;
+  for (i = 0; i < NstarsIn; i++) {
+    status = fRD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
+    if (!status) continue;
+    N1[Nstars] = i;
+    Nstars ++;
+  }
+  if (Nstars < 1) {
+    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", catalog[0].filename);
+    free (catalog[0].found);
+    free (X1);
+    free (Y1);
+    free (N1);
+    free (X2);
+    free (Y2);
+    free (N2);
+    return;
   }
   if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
@@ -333,5 +348,5 @@
 
   /* note stars which have been found in this catalog */
-  for (i = 0; i < Nstars; i++) {
+  for (i = 0; i < NstarsIn; i++) {
     if (stars[i].found > -1) {
       stars[i].found = -2;
@@ -346,4 +361,12 @@
   catalog[0].Nmissing = Nmiss;
   if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", Nstars, Nave, Nmeas, Nmiss, Nmatch);
+
+  free (catalog[0].found);
+  free (X1);
+  free (Y1);
+  free (N1);
+  free (X2);
+  free (Y2);
+  free (N2);
   return;
 }
Index: trunk/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 8304)
+++ trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 8328)
@@ -1,8 +1,7 @@
 # include "addstar.h"
-# define DEBUG 1
-
-void find_matches_closest (SkyRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
-
-  int i, j, n, N, J, Jmin;
+
+void find_matches_closest (SkyRegion *region, Stars *stars, int NstarsIn, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
+
+  int i, j, n, N, J, Jmin, status, Nstars;
   double X, Y, RADIUS, RADIUS2, Rmin, secz;
   float *X1, *Y1, *X2, *Y2;
@@ -24,7 +23,7 @@
 
   /** allocate local arrays (stars) **/
-  ALLOCATE (X1, float, Nstars);
-  ALLOCATE (Y1, float, Nstars);
-  ALLOCATE (N1, int,   Nstars);
+  ALLOCATE (X1, float, NstarsIn);
+  ALLOCATE (Y1, float, NstarsIn);
+  ALLOCATE (N1, int,   NstarsIn);
 
   /** allocate local arrays (catalog) **/
@@ -40,5 +39,5 @@
   NMEAS = Nmeas = catalog[0].Nmeasure;
   NMISS = Nmiss = catalog[0].Nmissing;
-  
+
   /* project onto rectilinear grid with 1 arcsec pixels */
   /* we keep the original crpix1,2 and crref1,2 */
@@ -56,14 +55,29 @@
     tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
     tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+    tcoords.Npolyterms = 1;
     strcpy (tcoords.ctype, "RA---TAN");
   }
 
-  /* build spatial index (RA sort) */
-  for (i = 0; i < Nstars; i++) {
-    fRD_to_XY (&X1[i], &Y1[i], stars[i].R, stars[i].D, &tcoords);
-    N1[i] = i;
+  /* build spatial index (RA sort) referencing input array sequence */
+  Nstars = 0;
+  for (i = 0; i < NstarsIn; i++) {
+    status = fRD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
+    if (!status) continue;
+    N1[Nstars] = i;
+    Nstars ++;
+  }
+  if (Nstars < 1) {
+    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", catalog[0].filename);
+    free (catalog[0].found);
+    free (X1);
+    free (Y1);
+    free (N1);
+    free (X2);
+    free (Y2);
+    free (N2);
+    return;
   }
   if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
-  
+
   /* build spatial index (RA sort) */
   for (i = 0; i < Nave; i++) {
@@ -149,8 +163,4 @@
     n = N2[Jmin];
     N = N1[i];
-
-    if (DEBUG) fprintf (stderr, "matched %f,%f and %f,%f\n", 
-			catalog[0].average[n].R, catalog[0].average[n].D,
-			stars[N].R, stars[N].D);
 
     /* add to end of measurement list */
@@ -329,5 +339,4 @@
   }
       
-  free (catalog[0].found);
   REALLOCATE (catalog[0].average, Average, Nave);
   REALLOCATE (catalog[0].measure, Measure, Nmeas);
@@ -344,5 +353,5 @@
 
   /* note stars which have been found in this catalog */
-  for (i = 0; i < Nstars; i++) {
+  for (i = 0; i < NstarsIn; i++) {
     if (stars[i].found > -1) {
       stars[i].found = -2;
@@ -356,5 +365,13 @@
   catalog[0].Nmeasure = Nmeas;
   catalog[0].Nmissing = Nmiss;
-  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", Nstars, Nave, Nmeas, Nmiss, Nmatch);
+  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", NstarsIn, Nave, Nmeas, Nmiss, Nmatch);
+
+  free (catalog[0].found);
+  free (X1);
+  free (Y1);
+  free (N1);
+  free (X2);
+  free (Y2);
+  free (N2);
   return;
 }
Index: trunk/Ohana/src/addstar/src/gcatalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/gcatalog.c	(revision 8304)
+++ trunk/Ohana/src/addstar/src/gcatalog.c	(revision 8328)
@@ -8,44 +8,14 @@
 
   /* read catalog header */
-  if (!load_catalog (catalog, VERBOSE)) {
+  if (!dvo_catalog_load (catalog, VERBOSE)) {
     fprintf (stderr, "ERROR: failure loading catalog\n");
     exit (1);
   }
 
-  /* should this be moved into save_catalog?? */
-  status = gfits_scan (&catalog[0].header, "SORTED", "%t", 1, &catalog[0].sorted);
-  if (!status) catalog[0].sorted = TRUE;
-  /* XXX EAM - is this a good choice?  should the default be 'FALSE'? */
-
   /* check Nsecfile value, update if needed */
   Nsecfilt = GetPhotcodeNsecfilt ();
-  if (catalog[0].Nsecfilt < Nsecfilt) {
-
-    int i, j, Nextra, in, out;
-    SecFilt *insec, *outsec;
-
-    Nextra = Nsecfilt - catalog[0].Nsecfilt;
-    insec = catalog[0].secfilt;
-    ALLOCATE (outsec, SecFilt, catalog[0].Naverage * Nsecfilt);
-    for (in = out = i = 0; i < catalog[0].Naverage; i++) {
-      for (j = 0; j < catalog[0].Nsecfilt; j++, in++, out++) {
-	outsec[out].M_PS  = insec[in].M_PS;
-	outsec[out].dM_PS = insec[in].dM_PS;
-	outsec[out].Xm = insec[in].Xm;
-      }
-      for (j = 0; j < Nextra; j++, out++) {
-	outsec[out].M_PS  = NO_MAG;
-	outsec[out].dM_PS = NO_MAG;
-	outsec[out].Xm    = NO_MAG;
-      }
-    }
-    free (catalog[0].secfilt);
-    catalog[0].secfilt = outsec;
-    catalog[0].Nsecfilt = Nsecfilt;
-  }
-
-  if (catalog[0].Nsecfilt > Nsecfilt) {
+  if (!dvo_catalog_check (catalog, Nsecfilt, TRUE)) {
     fprintf (stderr, "ERROR: can't reduce number of secondary filters\n");
-    exit (1);
+    exit (2);
   }
   return (TRUE);
Index: trunk/Ohana/src/addstar/src/load_pt_catalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/load_pt_catalog.c	(revision 8304)
+++ trunk/Ohana/src/addstar/src/load_pt_catalog.c	(revision 8328)
@@ -1,12 +1,15 @@
 # include "addstar.h"
 
+// XXX can I replace this with dvo_catalog_open ()
+// XXX set the catalog properties before the function call (not as args)
 int load_pt_catalog (Catalog *catalog, SkyRegion *region) {
   
-  if (!check_file_access (catalog[0].filename, TRUE, TRUE)) {
-    exit (1);
-  }
+  if (!check_file_access (catalog[0].filename, TRUE, TRUE)) exit (1);
 
   if (VERBOSE) fprintf (stderr, "adding to %s\n", catalog[0].filename);
     
+  /* check Nsecfile value, update if needed */
+  Nsecfilt = GetPhotcodeNsecfilt ();
+
   switch (lock_catalog (catalog, LCK_XCLD)) {
   case 0:
@@ -14,9 +17,16 @@
     exit (1);
   case 1:
-    gcatalog (catalog); /* load from disk */
-    if (VERBOSE) fprintf (stderr, "loading existing file %s\n", catalog[0].filename);
+    if (!dvo_catalog_load (catalog, VERBOSE)) {
+      fprintf (stderr, "ERROR: failure loading catalog\n");
+      exit (1);
+    }
+    if (!dvo_catalog_check (catalog, Nsecfilt, TRUE)) {
+      fprintf (stderr, "ERROR: can't reduce number of secondary filters\n");
+      exit (2);
+    }
+    if (VERBOSE) fprintf (stderr, "loaded existing file %s\n", catalog[0].filename);
     break;
   case 2:
-    mkcatalog (region, catalog); /* fills in new header info */
+    dvo_catalog_create (region, catalog, Nsecfilt, CATFORMAT, CATMODE); /* fills in new header info */
     if (VERBOSE) fprintf (stderr, "creating new file %s\n", catalog[0].filename);
     break;
