Index: /inundation/ga/storm_surge/pyvolution/cell.py
===================================================================
--- /inundation/ga/storm_surge/pyvolution/cell.py	(revision 481)
+++ /inundation/ga/storm_surge/pyvolution/cell.py	(revision 482)
@@ -6,7 +6,4 @@
 #FIXME add max min x y in general_mesh
 
-#FIXME hacky.  maybe have as an attribute (or class att') for cell?
-MAX_WAY_POINTS = 4
-
 class Cell(TreeNode):
     """class Cell
@@ -16,18 +13,21 @@
 
     Public Methods:
-        Prune()
-        Insert(Waypoint w)
-        Search(Point p, [keywords c])
-        Collapse()
-        Split()
-        Store()
-        Retrieve()
-        Count()
+        prune()
+        insert(point)
+        search(x, y)
+        collapse()
+        split()
+        store()
+        retrieve()
+        count()
     """
   
-    def __init__(self, southern, northern, western, eastern, name):
+    def __init__(self, southern, northern, western, eastern, 
+		 name = 'cell',
+    	         max_points_per_cell = 4):
   
         # Initialise base classes
         TreeNode.__init__(self, string.lower(name))
+	
         # Initialise cell
         self.southern = round(southern,5)    
@@ -36,18 +36,15 @@
         self.eastern = round(eastern,5)
 
-        # Corners
-        #self.SW = Point(self.southern,self.western) 
-        #self.NW = Point(self.northern,self.western) 
-        #self.NE = Point(self.northern,self.eastern)
-        #self.SE = Point(self.southern,self.eastern)
-
         # The points in this cell     
         self.points = []
-        
+	
+	self.max_points_per_cell = max_points_per_cell
+        
+	
     def __repr__(self):
         return self.name  
 
 
-    def Spawn(self):
+    def spawn(self):
         """Create four child cells unless they already exist
         """
@@ -72,84 +69,85 @@
  
 
-    def Search(self, x,y):
-        """Find all waypoints within given radius from point P meeting 
-        specified keywords (an optional comma-separated string).
+    def search(self, x,y):
+        """Find all points within given radius from point P
         """
       
-        waypoints = []
+        points = []
         if self.children:
             for child in self:
-                if child.Contains(None,x=x,y=y):
-                     waypoints += child.Search(x,y)
+                if child.contains(x,y):
+                     points += child.search(x,y)
         else:
             # Leaf node: Get actual waypoints
-            waypoints = self.Retrieve()
+            points = self.retrieve()
             
-        return waypoints
-
-
-
-    def Contains(self, point_id, x= 0, y= 0):
+        return points
+
+
+
+    def contains(*args):    
         """True only if P's coordinates lie within cell boundaries
-        """
-
-        if point_id <> None:
-            x = self.__class__.domain.coordinates[point_id][0]
-            y = self.__class__.domain.coordinates[point_id][1]
-        #print "x",x
-        #print "y",y
-        #print "self.southern",self.southern
-        #print "self.northern",self.northern
-        #print "self.western",self.western
-        #print "self.eastern",self.eastern
-        if y <  self.southern: return 0
-        if y >= self.northern: return 0
-        if x <  self.western:  return 0
-        if x >= self.eastern:  return 0
-        return 1
-    
-    def Contains_old(self, P):
-        """True only if P's coordinates lie within cell boundaries
-        """
-    
-        if P.latitude  <  self.southern: return 0
-        if P.latitude  >= self.northern: return 0
-        if P.longitude <  self.western:  return 0
-        if P.longitude >= self.eastern:  return 0
-        return 1
+	This methods has two forms:
+	
+	cell.contains(index)
+	  #True if cell contains indexed point
+	cell.contains(x, y)
+	  #True if cell contains point (x,y)	
+
+        """
+	
+	self = args[0]
+	if len(args) == 2:
+	    point_id = int(args[1])
+            x = self.__class__.mesh.coordinates[point_id][0]
+            y = self.__class__.mesh.coordinates[point_id][1]	    
+	elif len(args) == 3:
+	    x = float(args[1])
+	    y = float(args[2])
+	else:
+	    msg = 'Number of arguments to method must be two or three'
+	    raise msg    		      
+	
+        if y <  self.southern: return False
+        if y >= self.northern: return False
+        if x <  self.western:  return False
+        if x >= self.eastern:  return False
+        return True
     
     
-    def Insert(self, W):
-        """Insert waypoint in existing tree structure below self
-           and split if necessary
+    def insert(self, points, split = False):
+        """insert point(s) in existing tree structure below self
+           and split if requested
         """
   
-        # Call insert for each element of a list of waypoints
-        if type(W) == types.ListType:
-            for w in W:
-                self.Insert(w)
-            return
-   
-        # Find appropriate cell
-        if self.children:
-            for child in self:
-                if child.Contains(W):
-                    child.Insert(W)
-                    break
-        else:
-            # Cell is a leaf cell. Insert waypoint in database table
-            if self.Contains(W):
-                self.Store(W)
-                
-                #FIXME? - where was this called before?  
-                self.Split()
+        # Call insert for each element of a list of points
+        if type(points) == types.ListType:
+            for point in points:
+                self.insert(point, split)
+        else:
+            #Only one point given as argument	
+            point = points
+	
+            # Find appropriate cell
+            if self.children is not None:
+                for child in self:
+                    if child.contains(point):
+                        child.insert(point, split)
+                        break
             else:
-                #FIXME rasie exception
-               raise "point not in region"
-
-
-    def Store(self,objects):
-        #import storage
-        #storage = storage.Factory()
+                # self is a leaf cell: insert point into cell
+                if self.contains(point):
+                    self.store(point)
+                else:
+                    raise 'point not in region'
+		
+		
+        #Split datastructure if requested        
+	if split is True:
+            self.split()
+		
+
+
+    def store(self,objects):
         
         if type(objects) not in [types.ListType,types.TupleType]:
@@ -157,11 +155,7 @@
         else:
             self.points.extend(objects)
-            
-        #for object in objects:
-        #    storage.Store(self.name,object)
-
-
-
-    def Retrieve(self):
+
+
+    def retrieve(self):
          objects = []
          if self.children is None:
@@ -169,10 +163,10 @@
          else:  
              for child in self:
-                 objects += child.Retrieve()
+                 objects += child.retrieve()
          return objects  
 
 
-    def Count(self, keywords=None):
-        """Retrieve number of stored objects beneath this node inclusive
+    def count(self, keywords=None):
+        """retrieve number of stored objects beneath this node inclusive
         """
         
@@ -180,5 +174,5 @@
         if self.children:
             for child in self:
-                num_waypoint = num_waypoint + child.Count()
+                num_waypoint = num_waypoint + child.count()
         else:
             num_waypoint = len(self.points)
@@ -186,14 +180,25 @@
   
 
-    def Clear(self):
+    def clear(self):
         self.Prune()   # TreeNode method
 
 
-    def ClearLeafNode(self):
+    def clear_leaf_node(self):
+        """Clears storage in leaf node.
+	Called from Treenod.
+	Must exist.	
+	"""
         self.points = []
-
-
-
-    def Split(self, threshold=None):
+	
+	
+    def clear_internal_node(self):
+        """Called from Treenode.    
+	Must exist.
+	"""
+        pass
+
+
+
+    def split(self, threshold=None):
         """
         Partition cell when number of contained waypoints exceeds 
@@ -201,30 +206,35 @@
         child cell.
         """
-        if  threshold == None:
-            threshold = MAX_WAY_POINTS
-        #FIXME, mincellsize removed.  base it on side lenght, if needed
-        
+        if threshold == None:
+           threshold = self.max_points_per_cell
+	    
+        #FIXME, mincellsize removed.  base it on side length, if needed
+        
+	#Protect against silly thresholds such as -1
+	if threshold < 1:
+	    return
+	
         if not self.children:               # Leaf cell
-            if self.Count() > threshold : #and self.radius > mincellsize: 
+            if self.count() > threshold :   
+                #Split is needed
+                points = self.retrieve()    # Get points from leaf cell
+                self.clear()                # and remove them from storage
                 
-                waypoints = self.Retrieve() # Get waypoints from leaf cell
-                self.Clear()                # and remove them from storage
-                
-                self.Spawn()                # Spawn child cells and move
-                for wp in waypoints:        # waypoints to appropriate child
+                self.spawn()                # Spawn child cells and move
+                for p in points:            # points to appropriate child
                     for child in self:
-                        if child.Contains(wp):
-                            child.Insert(wp) 
+                        if child.contains(p):
+                            child.insert(p) 
                             break
     
         if self.children:                   # Parent cell
-            for child in self:              # Split (possibly newly created) 
-                child.Split(threshold)      # child cells recursively
+            for child in self:              # split (possibly newly created) 
+                child.split(threshold)      # child cells recursively
                 
 
 
-    def Collapse(self,threshold=None):
-        """
-        Collapse child cells into immediate parent if total number of contained waypoints
+    def collapse(self,threshold=None):
+        """
+        collapse child cells into immediate parent if total number of contained waypoints
         in subtree below is less than or equal to threshold.
         All waypoints are then moved into parent cell and
@@ -233,14 +243,15 @@
         
         if threshold is None:
-            threshold = config['maxwaypoints']
+            threshold = self.max_points_per_cell	
+
 
         if self.children:                   # Parent cell    
-            if self.Count() <= threshold:   # Collapse
-                waypoints = self.Retrieve() # Get all waypoints from child cells
-                self.Clear()                # Remove children, self is now a leaf node
-                self.Insert(waypoints)      # Insert all waypoints in local storage
+            if self.count() <= threshold:   # collapse
+                points = self.retrieve()    # Get all points from child cells
+                self.clear()                # Remove children, self is now a leaf node
+                self.insert(points)         # Insert all points in local storage
             else:                          
                 for child in self:          # Check if any sub tree can be collapsed
-                    child.Collapse(threshold)
+                    child.collapse(threshold)
 
 
@@ -260,9 +271,10 @@
                 s += child.Get_tree(depth+1)
         else:
-            s += '(#wp=%d)\n' %(self.Count())
+            s += '(#wp=%d)\n' %(self.count())
 
         return s
 
-    def Show(self,depth=0):
+	
+    def show(self, depth=0):
         """Traverse tree below self
            Print for each node the name and
@@ -271,16 +283,17 @@
         if depth == 0:
             print 
-        print "%s%s:" % ('  '*depth, self.name),
+        print "%s%s" % ('  '*depth, self.name),
         if self.children:
             print
             for child in self.children:
-                child.Show(depth+1)
-        else:
-            print '#wp=%d, c=(%.2f,%.2f)'\
-                  %(self.Count(), self.latitude, self.longitude)
-
-            
-
-    def ShowAll(self,depth=0):
+                child.show(depth+1)
+        else:
+            print '(xmin=%.2f, xmax=%.2f, ymin=%.2f, ymax=%.2f): [%d]'\
+		  %(self.western, self.eastern,
+		    self.southern, self.northern,
+		    self.count()) 
+
+
+    def show_all(self,depth=0):
         """Traverse tree below self
            Print for each node the name and if it is a leaf all its objects
@@ -292,10 +305,10 @@
             print
             for child in self.children:
-                child.ShowAll(depth+1)
-        else:
-            print '%s' %self.Retrieve()
-
-
-    def Stats(self,depth=0,min_rad=sys.maxint,max_depth=0,max_points=0):
+                child.show_all(depth+1)
+        else:
+            print '%s' %self.retrieve()
+
+
+    def stats(self,depth=0,min_rad=sys.maxint,max_depth=0,max_points=0):
         """Traverse tree below self and find minimal cell radius,
            maximumtree depth and maximum number of waypoints per leaf.
@@ -308,14 +321,17 @@
         else:
             #FIXME remvoe radius stuff
-            min_rad = sys.maxint
+            #min_rad = sys.maxint
             #if self.radius < min_rad:   min_rad = self.radius
-            if depth > max_depth:       max_depth = depth
-            num_points = self.Count()
+            if depth > max_depth: max_depth = depth
+            num_points = self.count()
             if num_points > max_points: max_points = num_points
 
-        return min_rad, max_depth, max_points    
-
-    def initialise(cls,domain):
-        cls.domain = domain
+        #return min_rad, max_depth, max_points    
+	return max_depth, max_points    
+	
+
+    #Class initialisation method	
+    def initialise(cls, mesh):
+        cls.mesh = mesh
 
     initialise = classmethod(initialise)
Index: /inundation/ga/storm_surge/pyvolution/general_mesh.py
===================================================================
--- /inundation/ga/storm_surge/pyvolution/general_mesh.py	(revision 481)
+++ /inundation/ga/storm_surge/pyvolution/general_mesh.py	(revision 482)
@@ -1,4 +1,4 @@
     
-class General_Mesh:
+class General_mesh:
     """Collection of triangular elements (purely geometric)
 
Index: /inundation/ga/storm_surge/pyvolution/least_squares.py
===================================================================
--- /inundation/ga/storm_surge/pyvolution/least_squares.py	(revision 481)
+++ /inundation/ga/storm_surge/pyvolution/least_squares.py	(revision 482)
@@ -25,5 +25,5 @@
 class ShapeError(exceptions.Exception): pass
 
-from general_mesh import General_Mesh
+from general_mesh import General_mesh
 from Numeric import zeros, array, Float, Int, dot, transpose
 from LinearAlgebra import solve_linear_equations
@@ -174,5 +174,5 @@
         
         #Build underlying mesh
-        self.mesh = General_Mesh(vertex_coordinates, triangles)
+        self.mesh = General_mesh(vertex_coordinates, triangles)
 
         #Smoothing parameter
Index: /inundation/ga/storm_surge/pyvolution/mesh.py
===================================================================
--- /inundation/ga/storm_surge/pyvolution/mesh.py	(revision 481)
+++ /inundation/ga/storm_surge/pyvolution/mesh.py	(revision 482)
@@ -5,7 +5,7 @@
 """
 
-from general_mesh import General_Mesh
-
-class Mesh(General_Mesh):
+from general_mesh import General_mesh
+
+class Mesh(General_mesh):
     """Collection of triangular elements (purely geometric)
 
@@ -67,5 +67,5 @@
         from Numeric import array, zeros, Int, Float, maximum, sqrt, sum
 
-        General_Mesh.__init__(self,coordinates, triangles)
+        General_mesh.__init__(self,coordinates, triangles)
 
         N = self.number_of_elements
@@ -205,5 +205,5 @@
         
         Postcondition:
-          self.vertexlist is build
+          self.vertexlist is built
         """
 
Index: /inundation/ga/storm_surge/pyvolution/test_cell.py
===================================================================
--- /inundation/ga/storm_surge/pyvolution/test_cell.py	(revision 481)
+++ /inundation/ga/storm_surge/pyvolution/test_cell.py	(revision 482)
@@ -2,5 +2,6 @@
 from cell import Cell
 
-from domain import *
+#from domain import *
+from general_mesh import General_mesh as Mesh
 
 import types, sys
@@ -11,5 +12,5 @@
 
     def setUp(self):
-        self.cell = Cell(100,140,0,40,'cell')
+        self.cell = Cell(100, 140, 0, 40, 'cell')
         
         a = [3, 107]
@@ -26,10 +27,12 @@
         vertices = [ [1,0,2],[1,3,4], [1,2,3], [5,4,7], [4,6,7]] 
 
-        conserved_quantities = ['level', 'xmomentum', 'ymomentum']
-        other_quantities = ['elevation', 'friction']
-        
-        domain = Domain(points, vertices, None,
-                        conserved_quantities, other_quantities)
-        Cell.initialise(domain)
+	#FIXME: Why?
+        #conserved_quantities = ['level', 'xmomentum', 'ymomentum']
+        #other_quantities = ['elevation', 'friction']
+        #domain = Domain(points, vertices, None,
+        #                conserved_quantities, other_quantities)
+	
+	mesh = Mesh(points, vertices)
+        Cell.initialise(mesh)
         
     def tearDown(self):
@@ -37,29 +40,86 @@
 
     def test_add_points_2_cell(self):
-        self.cell.Insert(0)
-        self.cell.Insert(1)
+        self.cell.insert(0)
+        self.cell.insert(1)
         
-        result = self.cell.Retrieve()
-        #result = self.cell.Get_tree()
-        #print  result 
-        assert type(result) in [types.ListType,types.TupleType], 'should be a list'        
+        result = self.cell.retrieve()
+        assert type(result) in [types.ListType,types.TupleType],\
+                                'should be a list'        
         self.assertEqual(len(result),2)
         
     def test_add_points_2_cellII(self):
-        self.cell.Insert([0,1,2,3,4,5,6,7])
+        self.cell.insert([0,1,2,3,4,5,6,7])
         
-        result = self.cell.Retrieve()
-        #result = self.cell.Get_tree()
-        #print  result 
-        assert type(result) in [types.ListType,types.TupleType], 'should be a list'        
+        result = self.cell.retrieve()
+        assert type(result) in [types.ListType,types.TupleType],\
+	                        'should be a list'        
         self.assertEqual(len(result),8)
 
 
     def test_search(self):
-        self.cell.Insert([0,1,2,3,4,5,6,7])
+        self.cell.insert([0,1,2,3,4,5,6,7])
+	self.cell.split(4)
         
-        result =  self.cell.Search(x = 1, y = 101)
-        assert type(result) in [types.ListType,types.TupleType], 'should be a list'        
+        result =  self.cell.search(x = 1, y = 101)
+        assert type(result) in [types.ListType,types.TupleType],\
+	                        'should be a list'        
         self.assertEqual(result, [0,1,2,3])
+	
+	
+    def test_clear_1(self):
+        self.cell.insert([0,1,2,3,4,5,6,7])
+	assert self.cell.count() == 8	
+	self.cell.clear()
+	
+        #This one actually revealed a bug :-)
+	assert self.cell.count() == 0	
+		
+    def test_clear_2(self):
+        self.cell.insert([0,1,2,3,4,5,6,7])
+	assert self.cell.count() == 8	
+	self.cell.split(2)
+	assert self.cell.count() == 8		
+		
+	self.cell.clear()
+	assert self.cell.count() == 0	
+	
+	
+	
+    def test_split(self):
+        self.cell.insert([0,1,2,3,4,5,6,7], split = False)    
+        
+	#No children yet
+	assert self.cell.children is None 
+	assert self.cell.count() == 8
+
+        #Split
+	self.cell.split(4)
+	#self.cell.show()
+	#self.cell.show_all()
+	
+	
+	#Now there are children
+	assert self.cell.children is not None 
+	assert self.cell.count() == 8	
+	
+	
+		
+    def test_collapse(self):
+        self.cell.insert([0,1,2,3,4,5,6,7], split = False)    
+        
+        #Split maximally
+	self.cell.split(1)
+	
+	#Now there are children
+	assert self.cell.children is not None 
+	assert self.cell.count() == 8	
+	
+	#Collapse
+	self.cell.collapse(8)
+	
+	
+	#No children
+	assert self.cell.children is None 
+	assert self.cell.count() == 8	
 
 #-------------------------------------------------------------
Index: /inundation/ga/storm_surge/pyvolution/treenode.py
===================================================================
--- /inundation/ga/storm_surge/pyvolution/treenode.py	(revision 481)
+++ /inundation/ga/storm_surge/pyvolution/treenode.py	(revision 482)
@@ -85,9 +85,16 @@
     def Prune(self):
         if self.children is None:                  # Leaf node
-            if callable(self.ClearLeafNode):
-                self.ClearLeafNode()
+            if callable(self.clear_leaf_node):
+                self.clear_leaf_node()
+	    else:
+	        msg = 'Node must have a method named "clear_leaf_node"' 
+		raise msg	
         else:
-            if callable(self.ClearInternalNode):   # Internal node
-                self.ClearInternalNode()
+            if callable(self.clear_internal_node):   # Internal node
+                self.clear_internal_node()
+	    else:
+	        msg = 'Node must have a method named "clear_internal_node"' 
+		raise msg	
+				
             for child in self.children:
                 child.Prune()
