Index: trunk/Ohana/src/addstar/src/findskycell.c
===================================================================
--- trunk/Ohana/src/addstar/src/findskycell.c	(revision 34290)
+++ trunk/Ohana/src/addstar/src/findskycell.c	(revision 34291)
@@ -6,5 +6,5 @@
 				        4.044944,   4.090909,   4.186047,   4.285714,   4.390244,   4.556962,   4.736842,   5.000000,   
 				        5.294118,   5.625000,   6.000000,   6.545455,   7.200000,   8.000000,   9.230769,  10.909091,  
-				      13.333333,  17.142857,  24.000000,  40.000000,  360.000000};
+				       13.333333,  17.142857,  24.000000,  40.000000, 360.000000};
 
 // in the general case, projection cell centers are arbitrary
@@ -113,6 +113,10 @@
   ALLOCATE (tree.NBAND, int, tree.Nzone);
 
-  ALLOCATE (tree.RA_origin, double, tree.Nzone);
-  ALLOCATE (tree.RA_offset, double, tree.Nzone);
+  ALLOCATE (tree.RA_origin,   double, tree.Nzone);
+  ALLOCATE (tree.RA_offset,   double, tree.Nzone);
+  ALLOCATE (tree.DEC_min,     double, tree.Nzone);
+  ALLOCATE (tree.DEC_max,     double, tree.Nzone);
+  ALLOCATE (tree.DEC_min_raw, double, tree.Nzone);
+  ALLOCATE (tree.DEC_max_raw, double, tree.Nzone);
 
   ALLOCATE (tree.ra,   double *, tree.Nzone);
@@ -138,4 +142,12 @@
     tree.RA_origin[zone] = -0.5*RA_offset_RINGS_V3[zone];
     tree.RA_offset[zone] = RA_offset_RINGS_V3[zone];
+
+    // set the starting values here.  In fact, the MIN and MAX values need to be tweaked 
+    // on the basis of the real images...
+    tree.DEC_min_raw[zone] = +90.0;
+    tree.DEC_max_raw[zone] = -90.0;
+    tree.DEC_min[zone] = NAN;
+    tree.DEC_max[zone] = NAN;
+
     ALLOCATE (tree.ra[zone],   double, tree.NBAND[zone]);
     ALLOCATE (tree.dec[zone],  double, tree.NBAND[zone]);
@@ -179,4 +191,50 @@
     tree.dec[zone][band] = dec;
     tree.cell[zone][band] = i;
+
+    // what are the min and max DEC values for this zone? (test center and corners of top and bottom edge)  
+
+    if (dec > 0.0) {
+      // min DEC at bottom of the cell at the center
+      x = 0.5*image[i].NX;
+      y = 0.0*image[i].NY;
+      XY_to_RD (&ra, &dec, x, y, &image[i].coords);
+      tree.DEC_min_raw[zone] = MIN(tree.DEC_min_raw[zone], dec);
+  
+      // max DEC : find the intersection between the RA boundary and the top of the cell 
+      double ra_band_min = tree.RA_origin[zone] + tree.RA_offset[zone] * band;
+  
+      // does the parity matter?
+      x = 0.0*image[i].NX;
+      y = 1.0*image[i].NY;
+  
+      int Niter;
+      for (Niter = 0; Niter < 3; Niter ++) {
+        XY_to_RD (&ra, &dec, x, y, &image[i].coords);
+        RD_to_XY (&x, &y, ra_band_min, dec, &image[i].coords);
+        y = 1.0*image[i].NY;
+      }
+      tree.DEC_max_raw[zone] = MAX(tree.DEC_max_raw[zone], dec);
+    } else {
+      // max DEC at top of the cell at the center
+      x = 0.5*image[i].NX;
+      y = 1.0*image[i].NY;
+      XY_to_RD (&ra, &dec, x, y, &image[i].coords);
+      tree.DEC_max_raw[zone] = MAX(tree.DEC_max_raw[zone], dec);
+  
+      // max DEC : find the intersection between the RA boundary and the bottom of the cell 
+      double ra_band_min = tree.RA_origin[zone] + tree.RA_offset[zone] * band;
+  
+      // does the parity matter? (NO)
+      x = 0.0*image[i].NX;
+      y = 0.0*image[i].NY;
+  
+      int Niter;
+      for (Niter = 0; Niter < 3; Niter ++) {
+        XY_to_RD (&ra, &dec, x, y, &image[i].coords);
+        RD_to_XY (&x, &y, ra_band_min, dec, &image[i].coords);
+        y = 0.0*image[i].NY;
+      }
+      tree.DEC_min_raw[zone] = MIN(tree.DEC_min_raw[zone], dec);
+    }
     memcpy (tree.name[zone][band], image[i].name, BOUNDARY_TREE_NAME_LENGTH);
   }
@@ -206,4 +264,18 @@
   }
 
+  // figure out the max band value for each zone?
+  int Nm = 0;
+  tree.DEC_min[Nm] = -90.0;
+  tree.DEC_max[Nm] = 0.5*(tree.DEC_max_raw[Nm] + tree.DEC_min_raw[Nm + 1]);
+
+  int Np = tree.Nzone - 1;
+  tree.DEC_min[Np] = 0.5*(tree.DEC_min_raw[Np] + tree.DEC_max_raw[Np - 1]);
+  tree.DEC_max[Np] = +90.0;
+
+  for (zone = 1; zone < tree.Nzone - 1; zone++) {
+    tree.DEC_min[zone] = 0.5*(tree.DEC_min_raw[zone] + tree.DEC_max_raw[zone - 1]);
+    tree.DEC_max[zone] = 0.5*(tree.DEC_max_raw[zone] + tree.DEC_min_raw[zone + 1]);
+  }
+
   struct timeval start, stop;
   gettimeofday (&start, (void *) NULL);
Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 34290)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 34291)
@@ -305,4 +305,8 @@
   double *RA_origin;
   double *RA_offset;
+  double *DEC_min;
+  double *DEC_max;
+  double *DEC_min_raw;
+  double *DEC_max_raw;
 
   int *Nband;
Index: trunk/Ohana/src/libdvo/src/BoundaryTree.c
===================================================================
--- trunk/Ohana/src/libdvo/src/BoundaryTree.c	(revision 34290)
+++ trunk/Ohana/src/libdvo/src/BoundaryTree.c	(revision 34291)
@@ -59,7 +59,11 @@
  
     // need to create and assign to flat-field correction
-    GET_COLUMN_RAW(tree->Nband,     "NBAND",  	 int);
-    GET_COLUMN_RAW(tree->RA_origin, "RA_ORIGIN", double);
-    GET_COLUMN_RAW(tree->RA_offset, "RA_OFFSET", double);
+    GET_COLUMN_RAW(tree->Nband,       "NBAND",  	 int);
+    GET_COLUMN_RAW(tree->RA_origin,   "RA_ORIGIN",   double);
+    GET_COLUMN_RAW(tree->RA_offset,   "RA_OFFSET",   double);
+    GET_COLUMN_RAW(tree->DEC_min  ,   "DEC_MIN",     double);
+    GET_COLUMN_RAW(tree->DEC_max  ,   "DEC_MAX",     double);
+    GET_COLUMN_RAW(tree->DEC_min_raw, "DEC_MIN_RAW", double);
+    GET_COLUMN_RAW(tree->DEC_max_raw, "DEC_MAX_RAW", double);
     gfits_free_header (&theader);
     gfits_free_table  (&ftable);
@@ -171,8 +175,12 @@
     gfits_create_table_header (&theader, "BINTABLE", "ZONE_DATA");
 
-    gfits_define_bintable_column (&theader, "J", "ZONE",      "zone sequence number", "none", 1.0, 0.0);
-    gfits_define_bintable_column (&theader, "J", "NBAND",     "number of cells in each zone", "none", 1.0, 0.0);
-    gfits_define_bintable_column (&theader, "D", "RA_ORIGIN", "origin of ra cell sequence", "degree", 1.0, 0.0);
-    gfits_define_bintable_column (&theader, "D", "RA_OFFSET", "offset per cell of ra cell sequence", "degree/cell", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "ZONE",      	"zone sequence number", "none", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "NBAND",     	"number of cells in each zone", "none", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "RA_ORIGIN", 	"origin of ra cell sequence", "degree", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "RA_OFFSET", 	"offset per cell of ra cell sequence", "degree/cell", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "DEC_MIN",   	"min dec for zone", "degree", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "DEC_MAX",   	"max dec for zone", "degree", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "DEC_MIN_RAW", "min dec for zone", "degree", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "DEC_MAX_RAW", "max dec for zone", "degree", 1.0, 0.0);
 
     // generate the output array that carries the data
@@ -188,8 +196,12 @@
 
     // add the columns to the output array
-    gfits_set_bintable_column (&theader, &ftable, "ZONE",   	zone,            tree->Nzone);
-    gfits_set_bintable_column (&theader, &ftable, "NBAND",   	tree->Nband,     tree->Nzone);
-    gfits_set_bintable_column (&theader, &ftable, "RA_ORIGIN", 	tree->RA_origin, tree->Nzone);
-    gfits_set_bintable_column (&theader, &ftable, "RA_OFFSET", 	tree->RA_offset, tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "ZONE",   	 zone,              tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "NBAND",   	 tree->Nband,       tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "RA_ORIGIN", 	 tree->RA_origin,   tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "RA_OFFSET", 	 tree->RA_offset,   tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "DEC_MIN", 	 tree->DEC_min,     tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "DEC_MAX", 	 tree->DEC_max,     tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "DEC_MIN_RAW", tree->DEC_min_raw, tree->Nzone);
+    gfits_set_bintable_column (&theader, &ftable, "DEC_MAX_RAW", tree->DEC_max_raw, tree->Nzone);
     free (zone);
 
@@ -283,4 +295,18 @@
   if (*zone >= tree->Nzone) return FALSE;
 
+  // test if:
+  // (a) DEC_min,DEC_max of *zone are defined (not NAN)
+  // (b) dec for *zone falls in range DEC_min[*zone] <= dec < DEC_max[*zone]
+  //     migrate up or down
+
+  // TEST int zone_raw = *zone;
+  if (isfinite(tree->DEC_min[*zone])) {
+    if (dec >= tree->DEC_max[*zone]) (*zone) ++;
+    if (dec <  tree->DEC_min[*zone]) (*zone) --;
+    // TEST assert (dec <  tree->DEC_max[*zone]);
+    // TEST assert (dec >= tree->DEC_min[*zone]);
+  }
+  // TEST assert (zone_raw > -1);
+
   // now select the RA bin for that zone
   *band = (ra - tree->RA_origin[*zone]) / tree->RA_offset[*zone];
