Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 8342)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 8386)
@@ -280,4 +280,5 @@
 int dvo_catalog_catformat (char *catformat);
 int dvo_catalog_catmode (char *catmode);
+void dvo_catalog_test (Catalog *catalog, int halt);
 
 /* catmode-specific APIs */
Index: trunk/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 8342)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 8386)
@@ -1,4 +1,44 @@
 # include <dvo.h>
 # define DEBUG 1
+
+void dvo_catalog_test (Catalog *catalog, int halt) {
+
+  Catalog *subcat;
+
+  // fprintf (stderr, "catalog: Naverage = %d, average = %zx\n", catalog[0].Naverage, (size_t) catalog[0].average);
+  // fprintf (stderr, "catalog: Nmeasure = %d, measure = %zx\n", catalog[0].Nmeasure, (size_t) catalog[0].measure);
+  // fprintf (stderr, "catalog: Nmissing = %d, missing = %zx\n", catalog[0].Nmissing, (size_t) catalog[0].missing);
+  // fprintf (stderr, "catalog: Nsecfilt = %d, secfilt = %zx\n", catalog[0].Nsecfilt, (size_t) catalog[0].secfilt);
+
+  if (!catalog[0].measure || !catalog[0].secfilt) {
+    fprintf (stderr, "error: %s\n", catalog[0].filename);
+    if (halt) abort ();
+  }
+
+  // XXX test that things are correctly initialized
+  if (catalog[0].catmode != DVO_MODE_SPLIT) return;
+
+  subcat = catalog[0].measure_catalog;
+  if (subcat) {
+    if (subcat[0].measure_catalog || subcat[0].secfilt_catalog || subcat[0].missing_catalog) {
+      fprintf (stderr, "error in init\n");
+      abort ();
+    }
+  }
+  subcat = catalog[0].missing_catalog;
+  if (subcat) {
+    if (subcat[0].measure_catalog || subcat[0].secfilt_catalog || subcat[0].missing_catalog) {
+      fprintf (stderr, "error in init\n");
+      abort ();
+    }
+  }
+  subcat = catalog[0].secfilt_catalog;
+  if (subcat) {
+    if (subcat[0].measure_catalog || subcat[0].secfilt_catalog || subcat[0].missing_catalog) {
+      fprintf (stderr, "error in init\n");
+      abort ();
+    }
+  }
+}
 
 int dvo_catalog_catformat (char *catformat) {
@@ -35,7 +75,7 @@
     catalog[0].catflags = 0;
     catalog[0].sorted = 0;
-
-    gfits_init_header (&catalog[0].header);
-  }
+  }
+
+  gfits_init_header (&catalog[0].header);
 
   // the following describe the catalog files on disk
@@ -102,5 +142,5 @@
   // attempt to unlink an empty file
   fd = fileno (catalog[0].f);
-  if (fstat (fd, &filestat)) {
+  if (!fstat (fd, &filestat)) {
     if (filestat.st_size == 0) {
       unlink (catalog[0].filename);
@@ -121,16 +161,17 @@
 // returns FALSE if a catalog could not be opened
 // returns TRUE if the catalog was empty
+enum {DVO_OPEN_NONE, DVO_OPEN_READ, DVO_OPEN_WRITE, DVO_OPEN_UPDATE);
 int dvo_catalog_open (Catalog *catalog, SkyRegion *region, int VERBOSE, char *iomode) {
 
-  int Nsecfilt;
-  int readonly;
-
-  readonly = -1;
-  if (!strcasecmp (iomode, "r")) readonly = TRUE;
-  if (!strcasecmp (iomode, "w")) readonly = FALSE;
-  if (readonly == -1) return (FALSE);
+  int Nsecfilt, mode;
+
+  mode = DVO_OPEN_NONE;
+  if (!strcasecmp (iomode, "r")) mode = DVO_OPEN_READ;
+  if (!strcasecmp (iomode, "w")) mode = DVO_OPEN_WRITE;
+  if (!strcasecmp (iomode, "a")) mode = DVO_OPEN_UPDATE;
+  if (mode == DVO_OPEN_NONE) return (FALSE);
 
   catalog[0].lockmode  = LCK_XCLD;
-  if (readonly) catalog[0].lockmode  = LCK_SOFT;
+  if (mode == DVO_OPEN_READ) catalog[0].lockmode  = LCK_SOFT;
 
   // XXX make a backup?  always?
@@ -161,5 +202,5 @@
     break;
   case 2:
-    if (readonly) return (TRUE);
+    if (DVO_OPEN_READ || DVO_OPEN_UPDATE) return (TRUE);
     dvo_catalog_create (region, catalog, Nsecfilt); /* fills in new header info */
     if (VERBOSE) fprintf (stderr, "creating new file %s\n", catalog[0].filename);
@@ -177,4 +218,6 @@
   char measure[80];
 
+  dvo_catalog_init (catalog, FALSE);
+
   // load the main catalog header, determine characteristics
   if (!gfits_fread_header (catalog[0].f, &catalog[0].header)) {
@@ -200,9 +243,4 @@
   catalog[0].catformat = DVO_FORMAT_UNDEF;
 
-  catalog[0].average = NULL;
-  catalog[0].secfilt = NULL;
-  catalog[0].measure = NULL;
-  catalog[0].missing = NULL;
-
   switch (catalog[0].catmode) {
     case DVO_MODE_RAW:
@@ -217,4 +255,5 @@
       if (VERBOSE) fprintf (stderr, "reading catalog (mode DVO_MODE_SPLIT)\n");
       dvo_catalog_load_split (catalog, VERBOSE);
+      dvo_catalog_test (catalog, TRUE);
       break;
     default:
Index: trunk/Ohana/src/libdvo/src/dvo_catalog_create.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog_create.c	(revision 8342)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog_create.c	(revision 8386)
@@ -1,4 +1,4 @@
 # include <dvo.h>
-# define DEBUG 1
+# define DEBUG 0
 
 // create a new dvo catalog file (if split, lock extra files as well?)
Index: trunk/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 8342)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 8386)
@@ -60,4 +60,5 @@
     gfits_free_header (&header);
   } 
+  dvo_catalog_test (catalog, FALSE);
 
   /*** Measure Table ***/
@@ -68,7 +69,5 @@
   if (catalog[0].catflags & LOAD_MEAS) {
     ALLOCATE (measure, Catalog, 1);
-    measure[0].measure_catalog = NULL;
-    measure[0].missing_catalog = NULL;
-    measure[0].secfilt_catalog = NULL;
+    dvo_catalog_init (measure, TRUE);
 
     /* get split filename from main header (paths relative to cpt file) */
@@ -90,15 +89,15 @@
     /* matrix should be empty */
     if (!gfits_fread_matrix (measure[0].f, &matrix, &measure[0].header)) {
-      if (VERBOSE) fprintf (stderr, "can't read primary matrix");
+      if (VERBOSE) fprintf (stderr, "can't read primary matrix\n");
       return (FALSE);
     }
     /* read Measure table header */
     if (!gfits_fread_header (measure[0].f, &header)) {
-      if (VERBOSE) fprintf (stderr, "can't read measure PHU header");
+      if (VERBOSE) fprintf (stderr, "can't read measure PHU header\n");
       return (FALSE);
     }
     /* read Measure table data */
     if (!gfits_fread_ftable_data (measure[0].f, &ftable)) {
-      if (VERBOSE) fprintf (stderr, "can't read table measure data");
+      if (VERBOSE) fprintf (stderr, "can't read table measure data\n");
       return (FALSE);
     }
@@ -112,8 +111,10 @@
     gfits_free_matrix (&matrix);
   }
+  dvo_catalog_test (catalog, FALSE);
 
   /* (Meta Load) */
   if (catalog[0].catflags & LOAD_MEAS_META) {
     ALLOCATE (measure, Catalog, 1);
+    dvo_catalog_init (measure, TRUE);
 
     /* get split filename from main header (paths relative to cpt file) */
@@ -142,4 +143,5 @@
   }
   catalog[0].measure_catalog = measure;
+  dvo_catalog_test (catalog, FALSE);
 
   /*** Missing Table ***/
@@ -148,7 +150,5 @@
   if (catalog[0].catflags & LOAD_MISS) {
     ALLOCATE (missing, Catalog, 1);
-    missing[0].measure_catalog = NULL;
-    missing[0].missing_catalog = NULL;
-    missing[0].secfilt_catalog = NULL;
+    dvo_catalog_init (missing, TRUE);
 
     /* get split filename from main header (paths relative to cpt file) */
@@ -170,15 +170,15 @@
     /* matrix should be empty */
     if (!gfits_fread_matrix (missing[0].f, &matrix, &missing[0].header)) {
-      if (VERBOSE) fprintf (stderr, "can't read primary matrix");
+      if (VERBOSE) fprintf (stderr, "can't read primary matrix\n");
       return (FALSE);
     }
     /* read Missing table header */
     if (!gfits_fread_header (missing[0].f, &header)) {
-      if (VERBOSE) fprintf (stderr, "can't read table missing header");
+      if (VERBOSE) fprintf (stderr, "can't read table missing header\n");
       return (FALSE);
     }
     /* read Missing table data */
     if (!gfits_fread_ftable_data (missing[0].f, &ftable)) {
-      if (VERBOSE) fprintf (stderr, "can't read table missing data");
+      if (VERBOSE) fprintf (stderr, "can't read table missing data\n");
       return (FALSE);
     }
@@ -192,4 +192,5 @@
   } 
   catalog[0].missing_catalog = missing;
+  dvo_catalog_test (catalog, FALSE);
 
   /*** Secfilt Table ***/
@@ -198,7 +199,5 @@
   if (catalog[0].catflags & LOAD_SECF) {
     ALLOCATE (secfilt, Catalog, 1);
-    secfilt[0].measure_catalog = NULL;
-    secfilt[0].missing_catalog = NULL;
-    secfilt[0].secfilt_catalog = NULL;
+    dvo_catalog_init (secfilt, TRUE);
 
     /* get split filename from main header (paths relative to cpt file) */
@@ -220,15 +219,15 @@
     /* matrix should be empty */
     if (!gfits_fread_matrix (secfilt[0].f, &matrix, &secfilt[0].header)) {
-      if (VERBOSE) fprintf (stderr, "can't read primary matrix");
+      if (VERBOSE) fprintf (stderr, "can't read primary matrix\n");
       return (FALSE);
     }
     /* read secfilt table header */
     if (!gfits_fread_header (secfilt[0].f, &header)) {
-      if (VERBOSE) fprintf (stderr, "can't read table secfilt header");
+      if (VERBOSE) fprintf (stderr, "can't read table secfilt header\n");
       return (FALSE);
     }
     /* read secfilt table data */
     if (!gfits_fread_ftable_data (secfilt[0].f, &ftable)) {
-      if (VERBOSE) fprintf (stderr, "can't read table secfilt data");
+      if (VERBOSE) fprintf (stderr, "can't read table secfilt data\n");
       return (FALSE);
     }
@@ -242,4 +241,5 @@
   } 
   catalog[0].secfilt_catalog = secfilt;
+  dvo_catalog_test (catalog, FALSE);
 
   /* save the current number so we can do partial updates */
Index: trunk/Ohana/src/libdvo/src/skyregion_gsc.c
===================================================================
--- trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 8342)
+++ trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 8386)
@@ -2,4 +2,5 @@
 # define NDECBANDS 24
 # define NDIV 4
+# define DEBUG 0
 
 static int DecLines[] = {593, 584, 551, 530, 522, 465, 406, 362, 280, 198, 123, 25, 597, 578, 574, 577, 534, 499, 442, 376, 294, 212, 144, 48};
@@ -85,5 +86,5 @@
   L0.regions[0].childE  =  NDECBANDS;
   strcpy (L0.regions[0].name, "fullsky");
-  // SkyRegionPrint (&L0.regions[0]);
+  if (DEBUG) SkyRegionPrint (&L0.regions[0]);
 
   /* allocate space for all levels */
@@ -93,4 +94,8 @@
   ALLOCATE (L3.regions, SkyRegion, 1);
   ALLOCATE (L4.regions, SkyRegion, 1);
+
+  // skipLines = 0;
+  // for (i = 0; i < 16; i++) skipLines += DecLines[i];
+  // for (i = 16; i < 17; i++) {
 
   /* L1 : dec bands */
@@ -107,5 +112,5 @@
     L1.regions[i].table    = -1;
     strcpy (L1.regions[i].name, DecNames[i]);
-    // SkyRegionPrint (&L1.regions[i]);
+    if (DEBUG) SkyRegionPrint (&L1.regions[i]);
 
     /* build the L2 regions for this L1 region (based on zones) */
@@ -123,4 +128,5 @@
     /* subdivide each zone */
     for (j = 0; j < Nzones; j++) {
+      if (DEBUG) fprintf (stderr, "zone: %d : %f - %f : %f - %f\n", j, zones[j].Rmin, zones[j].Rmax, zones[j].Dmin, zones[j].Dmax);
       SkyTableL2fromZone (&L2, &L3, &L4, band, &zones[j], i);
     }
@@ -273,5 +279,5 @@
     Dmax = MAX (regions[i].Dmax, Dmax);
     dDec = regions[i].Dmax - regions[i].Dmin;
-    if (dDec != zones[Nz].dDec) {
+    if (fabs(dDec - zones[Nz].dDec) > 0.001) {
       /* we've found the end of the current zone */
       zones[Nz].L3end = i;
@@ -347,5 +353,5 @@
     *p = '/';
     strcpy (L2[0].regions[Nr].name, name);
-    // SkyRegionPrint (&L2[0].regions[Nr]);
+    if (DEBUG) SkyRegionPrint (&L2[0].regions[Nr]);
 
     /* childS and childE are set in SkyTableL3fromL2 */
@@ -383,5 +389,5 @@
     L3[0].regions[Nr].childE   =  0;
 
-    // SkyRegionPrint (&L3[0].regions[Nr]);
+    if (DEBUG) SkyRegionPrint (&L3[0].regions[Nr]);
     /* name is set for the band in SkyRegionForDecBand */
 
@@ -432,5 +438,5 @@
       sprintf (name, "%s.%02d", L3[0].name, Nbox);
       strcpy (L4[0].regions[Nr].name, name);
-      // SkyRegionPrint (&L4[0].regions[Nr]);
+      if (DEBUG) SkyRegionPrint (&L4[0].regions[Nr]);
 
       Nr ++;
