Index: /trunk/Ohana/src/opihi/dvo2/skydbsearch.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo2/skydbsearch.c	(revision 5437)
+++ /trunk/Ohana/src/opihi/dvo2/skydbsearch.c	(revision 5438)
@@ -9,70 +9,77 @@
 
 /* find region which overlaps c at given depth (-1 : max depth) */
-SkyRegion *SkyFindPoint (SkyRegion *db, SkyCoord c, int depth) {
+SkyRegion *SkyFindPoint (SkyRegionTable *table, double ra, double dec, int depth) {
+  
+  SkyRegion *region;
+
+  region = table[0].region;
+
+  Ns = 0;
+  Ne = 1;
+
+  while (1) {
+    No = -1;
+    for (i = Ns; (i < Ne) && (i < table[0].Nregion); i++) {
+      if (ra  < region[i].Rmin) continue;
+      if (ra  > region[i].Rmax) continue;
+      if (dec < region[i].Dmin) continue;
+      if (dec > region[i].Dmax) continue;
+      No = i;
+      break;
+    }
+    if (No == -1) return (NULL);
+    if (depth == region[No].depth) return (&region[No]);
+    if ((depth == -1) && (!region[No].child)) return (&region[No]);
+    if ((depth > region[No].depth) && !region[No].child) return (NULL);
+
+    /* need to check Ns, Ne, or guarantee valid range */
+    Ns = region[No].childS;
+    Ne = region[No].childE;
+  }
+}
+
+/* find regions at all levels which overlap c */
+SkyRegionList *SkyFindLevels (SkyRegionTable *table, double ra, double dec) {
+
+  SkyRegion *region;
+  SkyRegionList *list;
   
   Ns = 0;
   Ne = 1;
 
+  N = 0;
+  NREGION = 10;
+  ALLOCATE (list, SkyRegionList, 1);
+  ALLOCATE (list[0].region,  SkyRegion *, NREGION);
+  list[0].Nregion = N;
+
+  region = table[0].region;
+
   while (1) {
     No = -1;
-    for (i = Ns; i < Ne; i++) {
-      if (c.r < db[i].Rmin) continue;
-      if (c.r > db[i].Rmax) continue;
-      if (c.d < db[i].Dmin) continue;
-      if (c.d > db[i].Dmax) continue;
+    for (i = Ns; (i < Ne) && (i < table[0].region); i++) {
+      if (ra  < region[i].Rmin) continue;
+      if (ra  > region[i].Rmax) continue;
+      if (dec < region[i].Dmin) continue;
+      if (dec > region[i].Dmax) continue;
       No = i;
       break;
     }
-    if (No == -1) return ((SkyRegion *) NULL);
-    if (depth == db[No].depth) return (&db[No]);
-    if ((depth > db[No].depth) && !db[No].child) return ((SkyRegion *) NULL);
-
-    /* need to check Ns, Ne, or guarantee valid range */
-    Ns = db[No].childS;
-    Ne = db[No].childE;
-
-    if ((depth > db[No].depth) && db[No].child) continue;
-    return (&db[No]);
-  }
-}
-
-/* find regions at all levels which overlap c */
-SkyRegion **SkyFindLevels (SkyRegion *db, SkyCoord c, int *Nregion) {
-
-  SkyRegion **region;
-  
-  Ns = 0;
-  Ne = 1;
-
-  N = 0;
-  NREGION = 10;
-  ALLOCATE (region, SkyRegion *, NREGION);
-
-  while (1) {
-    No = -1;
-    for (i = Ns; i < Ne; i++) {
-      if (c.r < db[i].Rmin) continue;
-      if (c.r > db[i].Rmax) continue;
-      if (c.d < db[i].Dmin) continue;
-      if (c.d > db[i].Dmax) continue;
-      No = i;
-      break;
-    }
-    if (No == -1) return ((SkyRegion *) NULL);
-
-    region[N] = &db[No];
+    if (No == -1) return (list);
+
+    list[N] = &region[No];
     N++;
     if (N == NREGION) {
       NREGION += 10;
-      REALLOCATE (region, SkyRegion *, NREGION);
+      REALLOCATE (list[0].region, SkyRegion *, NREGION);
     }      
+    list[0].Nregion = N;
 
     /* need to check Ns, Ne, or guarantee valid range */
-    if (db[No].child) {
-      Ns = db[No].childS;
-      Ne = db[No].childE;
+    if (region[No].child) {
+      Ns = region[No].childS;
+      Ne = region[No].childE;
     } else {
-      *Nregion = N; 
-      return (region);
+      return (list);
     }
   }
@@ -102,5 +109,4 @@
     *nlist = Nlist;
     return (list);
-
   }    
 
@@ -109,6 +115,6 @@
   Nlist = 1;
   ALLOCATE (list, SkyRegion *, Nlist);
-  list[0] = &db[0];
-  getchild = db[0].child;
+  list[0] = &region[0];
+  getchild = region[0].child;
 
   while (getchild) {
@@ -160,9 +166,9 @@
 
   for (i = Ns; i < Ne; i++) {
-    if (db[i].Rmax < c1.r) continue;
-    if (db[i].Rmin > c2.r) continue;
-    if (db[i].Dmax < c1.d) continue;
-    if (db[i].Dmin > c2.d) continue;
-    region[Nregion] = &db[i];
+    if (region[i].Rmax < c1.r) continue;
+    if (region[i].Rmin > c2.r) continue;
+    if (region[i].Dmax < c1.d) continue;
+    if (region[i].Dmin > c2.d) continue;
+    region[Nregion] = &region[i];
     Nregion++;
     if (Nregion == NREGION) {
@@ -195,36 +201,36 @@
 
   /* full sky */
-  strcpy (db[0].name, "fullsky.cpt");
-  db[0].Rmin =   0; db[0].Rmax = 360;
-  db[0].Dmin = -90; db[0].Dmax =  90;
-  db[0].depth = 0;
-  db[0].child = FALSE;
+  strcpy (region[0].name, "fullsky.cpt");
+  region[0].Rmin =   0; region[0].Rmax = 360;
+  region[0].Dmin = -90; region[0].Dmax =  90;
+  region[0].depth = 0;
+  region[0].child = FALSE;
   N = 1;
 
-  db[0].childS = N;
+  region[0].childS = N;
   /* north dec bands */
   for (dec = 0; dec < 90; dec += 7.5) {
-    sprintf (db[N].name, "n%04d.cpt", (int) 100*dec);
-    db[N].Rmin =   0; db[N].Rmax = 360;
-    db[N].Dmin = dec; db[N].Dmax = dec + 7.5;
-    db[N].depth = 1;
-    db[N].child = FALSE;
+    sprintf (region[N].name, "n%04d.cpt", (int) 100*dec);
+    region[N].Rmin =   0; region[N].Rmax = 360;
+    region[N].Dmin = dec; region[N].Dmax = dec + 7.5;
+    region[N].depth = 1;
+    region[N].child = FALSE;
     N++;
   }
   /* south dec bands */
   for (dec = 0; dec > -90; dec -= 7.5) {
-    sprintf (db[N].name, "s%04d.cpt", (int) 100*dec);
-    db[N].Rmin =   0;       db[N].Rmax = 360;
-    db[N].Dmin = dec - 7.5; db[N].Dmax = dec;
-    db[N].depth = 1;
-    db[N].child = FALSE;
+    sprintf (region[N].name, "s%04d.cpt", (int) 100*dec);
+    region[N].Rmin =   0;       region[N].Rmax = 360;
+    region[N].Dmin = dec - 7.5; region[N].Dmax = dec;
+    region[N].depth = 1;
+    region[N].child = FALSE;
     N++;
   }
-  db[0].childE = N;
+  region[0].childE = N;
 
   /* subdivide dec bands based on seed */
   Rnumber = 0;
-  childS = db[0].childS;
-  childE = db[0].childE;
+  childS = region[0].childS;
+  childE = region[0].childE;
   for (i = childS; i < childE; i++) {
 
@@ -234,8 +240,9 @@
 
     for (j = 0; j < Nseed; j++) {
-      if (seed[j].Rmin > db[i].Rmax) continue;
-      if (seed[j].Rmax < db[i].Rmin) continue;
-      if (seed[j].Dmin > db[i].Dmax) continue;
-      if (seed[j].Dmax < db[i].Dmin) continue;
+      /* skip seeds outside of parent bound */
+      if (seed[j].Rmin > region[i].Rmax) continue;
+      if (seed[j].Rmax < region[i].Rmin) continue;
+      if (seed[j].Dmin > region[i].Dmax) continue;
+      if (seed[j].Dmax < region[i].Dmin) continue;
       
       for (k = 0; k < Nnew; k++) {
@@ -250,11 +257,17 @@
 	}
       }	
+      /* new region spans full DEC band.  */
+      new[Nnew].Dmin = region[i].Dmin;
+      new[Nnew].Dmax = region[i].Dmax;
+      
+      Ntall = (region[i].Dmax - region[i].Dmin) / (seed[j].Dmax - seed[j].Dmin);
+      
+      /* fi
+
       new[Nnew].Rmin = seed[j].Rmin;
       new[Nnew].Rmax = seed[j].Rmax;
-      new[Nnew].Dmin = db[i].Dmin;
-      new[Nnew].Dmax = db[i].Dmax;
       new[Nnew].depth = 2;
       new[Nnew].child = FALSE;
-      strncpy (root, db[i].name, 5); root[5] = 0;
+      strncpy (root, region[i].name, 5); root[5] = 0;
       sprintf (new[Nnew].name, "%s/r%04d.cpt", root, Rnumber);
       Rnumber ++;      
@@ -267,9 +280,9 @@
     }
     /* update db list */
-    db[i].childS = N;
-    db[i].childE = N + Nnew;
+    region[i].childS = N;
+    region[i].childE = N + Nnew;
     NREGION = N + Nnew + 100;
     REALLOCATE (db, SkyRegion, NREGION);
-    memcpy (&db[N], new, Nnew*sizeof(SkyRegion));
+    memcpy (&region[N], new, Nnew*sizeof(SkyRegion));
     free (new);
     N += Nnew;
@@ -277,6 +290,6 @@
 
   /* subdivide ra strips based on seed */
-  childS = db[db[0].childS].childS;
-  childE = db[db[0].childE - 1].childE;
+  childS = region[region[0].childS].childS;
+  childE = region[region[0].childE - 1].childE;
   nextS = N;
   for (i = childS; i < childE; i++) {
@@ -287,8 +300,8 @@
 
     for (j = 0; j < Nseed; j++) {
-      if (seed[j].Rmin > db[i].Rmax) continue;
-      if (seed[j].Rmax < db[i].Rmin) continue;
-      if (seed[j].Dmin > db[i].Dmax) continue;
-      if (seed[j].Dmax < db[i].Dmin) continue;
+      if (seed[j].Rmin > region[i].Rmax) continue;
+      if (seed[j].Rmax < region[i].Rmin) continue;
+      if (seed[j].Dmin > region[i].Dmax) continue;
+      if (seed[j].Dmax < region[i].Dmin) continue;
       
       for (k = 0; k < Nnew; k++) {
@@ -305,6 +318,6 @@
       new[Nnew].Dmin = seed[j].Dmin;
       new[Nnew].Dmax = seed[j].Dmax;
-      new[Nnew].Rmin = db[i].Rmin;
-      new[Nnew].Rmax = db[i].Rmax;
+      new[Nnew].Rmin = region[i].Rmin;
+      new[Nnew].Rmax = region[i].Rmax;
       new[Nnew].depth = 3;
       new[Nnew].child = FALSE;
@@ -319,9 +332,9 @@
     
     /* update db list */
-    db[i].childS = N;
-    db[i].childE = N + Nnew;
+    region[i].childS = N;
+    region[i].childE = N + Nnew;
     NREGION = N + Nnew + 100;
     REALLOCATE (db, SkyRegion, NREGION);
-    memcpy (&db[N], new, Nnew*sizeof(SkyRegion));
+    memcpy (&region[N], new, Nnew*sizeof(SkyRegion));
     free (new);
     N += Nnew;
@@ -334,17 +347,17 @@
   for (i = childS; i < childE; i++) {
     Rnumber = 0;
-    db[i].childS = N;
-    dDec = (db[i].Dmax - db[i].Dmin) / 5.0;
-    dRa  = (db[i].Rmax - db[i].Rmin) / 5.0;
+    region[i].childS = N;
+    dDec = (region[i].Dmax - region[i].Dmin) / 5.0;
+    dRa  = (region[i].Rmax - region[i].Rmin) / 5.0;
     for (nx = 0; nx < 5; nx++) {
       for (ny = 0; ny < 5; ny++) {
-	db[N].Dmin = db[i].Dmin + dDec * (nx + 0);
-	db[N].Dmax = db[i].Dmax + dDec * (nx + 1);
-	db[N].Rmin = db[i].Rmin + dDec * (nx + 0);
-	db[N].Rmax = db[i].Rmax + dDec * (nx + 1);
-	db[N].depth = 3;
-	db[N].child = FALSE;
-	strncpy (root, db[i].name, 10); root[10] = 0;
-	sprintf (db[N].name, "%s.%02d.cpt", root, Rnumber);
+	region[N].Dmin = region[i].Dmin + dDec * (nx + 0);
+	region[N].Dmax = region[i].Dmax + dDec * (nx + 1);
+	region[N].Rmin = region[i].Rmin + dDec * (nx + 0);
+	region[N].Rmax = region[i].Rmax + dDec * (nx + 1);
+	region[N].depth = 3;
+	region[N].child = FALSE;
+	strncpy (root, region[i].name, 10); root[10] = 0;
+	sprintf (region[N].name, "%s.%02d.cpt", root, Rnumber);
 	Rnumber ++;
 	N ++;
Index: /trunk/Ohana/src/opihi/include/dvo2.h
===================================================================
--- /trunk/Ohana/src/opihi/include/dvo2.h	(revision 5437)
+++ /trunk/Ohana/src/opihi/include/dvo2.h	(revision 5438)
@@ -12,4 +12,14 @@
   float r, d;
 } SkyCoord;
+
+typedef struct {
+  int Nregion;
+  SkyRegion *region;
+} SkyRegionTable;
+
+typedef struct {
+  int Nregion;
+  SkyRegion **region;
+} SkyRegionList;
 
 typedef struct {
