Index: /trunk/anuga_core/source/anuga/fit_interpolate/test/test_all.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test/test_all.py	(revision 9479)
+++ /trunk/anuga_core/source/anuga/fit_interpolate/test/test_all.py	(revision 9479)
@@ -0,0 +1,83 @@
+"""Regression testing framework
+This module will search for scripts in the same directory named
+test_*.py.  Each such script should be a test suite that tests a
+module through PyUnit. This script will aggregate all
+found test suites into one big test suite and run them all at once.
+"""
+
+# Author: Mark Pilgrim
+# Modified by Ole Nielsen
+
+import unittest
+import os
+
+
+#List files that should be excluded from the testing process.
+#E.g. if they are known to fail and under development
+#exclude = ['test_interpolate_sww.py'] 
+
+
+def get_test_files(path):
+
+    import sys
+
+    files = os.listdir(path)
+
+    #Check sub directories
+    test_files = []
+    for file in files:
+        if os.path.isdir(file):
+            sys.path.append(file)
+            #print 'Recursing into', file
+            test_files += get_test_files(path + os.sep + file)
+        elif file[:5] == 'test_' and file[-2:] == 'py':
+            #print 'Appending', file
+            test_files.append(file)
+        else:
+            pass
+    return test_files
+
+
+
+def regressionTest():
+    import sys, os, re, unittest
+    path = os.path.split(sys.argv[0])[0] or os.getcwd()
+
+
+    files = get_test_files(path)
+
+
+
+    #test = re.compile('^test_[\w]*.py$', re.IGNORECASE)
+    #files = filter(test.search, files)
+
+
+    try:
+        files.remove(__file__)  #Remove self from list (Ver 2.3. or later)
+    except:
+        files.remove('test_all.py')
+
+    print 'Testing:'
+    for file in files:
+        print '  ' + file
+
+    if globals().has_key('exclude'):
+        for file in exclude:
+            files.remove(file)
+            print 'WARNING: File '+ file + ' excluded from testing'
+
+
+    filenameToModuleName = lambda f: os.path.splitext(f)[0]
+    #print "files",files 
+    moduleNames = map(filenameToModuleName, files)
+    #print "moduleNames",moduleNames
+    modules = map(__import__, moduleNames)
+    load = unittest.defaultTestLoader.loadTestsFromModule
+    return unittest.TestSuite(map(load, modules))
+
+################################################################################
+
+if __name__ == '__main__':
+    suite = regressionTest()
+    runner = unittest.TextTestRunner() #(verbosity=2)
+    runner.run(suite)
Index: /trunk/anuga_core/source/anuga/fit_interpolate/test/test_fit.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test/test_fit.py	(revision 9479)
+++ /trunk/anuga_core/source/anuga/fit_interpolate/test/test_fit.py	(revision 9479)
@@ -0,0 +1,1227 @@
+#!/usr/bin/env python
+
+"""
+Note, fitting/blocking is also tested in
+test_quantity.test_set_values_from_UTM_pts.
+"""
+
+#TEST
+import sys
+import unittest
+from math import sqrt
+import tempfile
+import os
+
+from fit import *
+from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
+from anuga.utilities.sparse import Sparse, Sparse_CSR
+from anuga.coordinate_transforms.geo_reference import Geo_reference
+from anuga.utilities.numerical_tools import ensure_numeric
+from anuga.geospatial_data.geospatial_data import Geospatial_data
+from anuga.shallow_water.shallow_water_domain import Domain
+
+import numpy as num
+
+
+def distance(x, y):
+    return sqrt(num.sum((num.array(x)-num.array(y))**2))
+
+def linear_function(point):
+    point = num.array(point)
+    return point[:,0]+point[:,1]
+
+
+class Test_Fit(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+
+    def test_smooth_attributes_to_mesh(self):
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        points = [a, b, c]
+        triangles = [ [1,0,2] ]   #bac
+
+        d1 = [1.0, 1.0]
+        d2 = [1.0, 3.0]
+        d3 = [3.0,1.0]
+        z1 = 2
+        z2 = 4
+        z3 = 4
+        data_coords = [d1, d2, d3]
+
+        z = [z1, z2, z3]
+        fit = Fit(points, triangles, alpha=0)
+        #print "interp.get_A()", interp.get_A()
+        fit._build_matrix_AtA_Atz(ensure_numeric(data_coords),
+                                  ensure_numeric(z))
+        #print "Atz - from fit", fit.Atz
+        #print "AtA - from fit", fit.AtA.todense()
+        #print "z",z 
+
+        assert num.allclose(fit.Atz, [2.8, 3.6, 3.6], atol=1e-7)
+
+        f = fit.fit()
+        
+        answer = [0, 5., 5.]
+
+        #print "f\n",f
+        #print "answer\n",answer
+
+        assert num.allclose(f, answer, atol=1e-7)
+
+    def test_smooth_att_to_meshII(self):
+
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        points = [a, b, c]
+        triangles = [ [1,0,2] ]   #bac
+
+        d1 = [1.0, 1.0]
+        d2 = [1.0, 2.0]
+        d3 = [3.0,1.0]
+        data_coords = [d1, d2, d3]
+        z = linear_function(data_coords)
+        #print "z",z 
+
+        interp = Fit(points, triangles, alpha=0.0)
+        f = interp.fit(data_coords, z)
+        answer = linear_function(points)
+        #print "f\n",f
+        #print "answer\n",answer
+
+        assert num.allclose(f, answer)
+
+    def test_smooth_attributes_to_meshIII(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+        point_coords = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [0.0,2.0],
+                        [1.0, 1.0],
+                        [2.0, 1.0],
+                        [0.0,0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2,-0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [3.0,1.0]]
+
+        z = linear_function(point_coords)
+        interp = Fit(vertices, triangles,
+                                alpha=0.0)
+
+        #print 'z',z
+        f = interp.fit(point_coords,z)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+
+   
+    def test_smooth_attributes_to_meshIV(self):
+        # Testing 2 attributes smoothed to the mesh
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        points = [a, b, c]
+        triangles = [ [1,0,2] ]   #bac
+
+        d1 = [1.0, 1.0]
+        d2 = [1.0, 3.0]
+        d3 = [3.0, 1.0]
+        z1 = [2, 4]
+        z2 = [4, 8]
+        z3 = [4, 8]
+        data_coords = [d1, d2, d3]
+
+        z = [z1, z2, z3]
+        fit = Fit(points, triangles, alpha=0)
+        
+        f =  fit.fit(data_coords,z)
+        answer = [[0,0], [5., 10.], [5., 10.]]
+        assert num.allclose(f, answer)
+
+    def test_smooth_attributes_to_mesh_build_fit_subset(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+        interp = Fit(vertices, triangles,
+                                alpha=0.0)
+
+        point_coords = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [0.0,2.0],
+                        [1.0, 1.0],
+                       ]
+
+        z = linear_function(point_coords)
+
+        f = interp.build_fit_subset(point_coords,z)
+        
+        point_coords = [
+                        [2.0, 1.0],
+                        [0.0,0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2,-0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [3.0,1.0]]
+        
+        z = linear_function(point_coords)
+
+        f = interp.build_fit_subset(point_coords,z)
+        
+        #print 'z',z
+        f = interp.fit()
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+
+        # test fit 2 mesh as well.
+    def test_fit_file_blocking(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+        interp = Fit(vertices, triangles,
+                     alpha=0.0)
+
+
+        fileName = tempfile.mktemp(".ddd")
+        file = open(fileName,"w")
+        file.write(" x,y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        
+        f = interp.fit(fileName, max_read_lines=2)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+        os.remove(fileName)
+
+    def test_fit_to_mesh_UTM_file(self):
+        #Get (enough) datapoints
+        data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65],
+                       [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]]
+
+        data_geo_spatial = Geospatial_data(data_points,
+                                           points_are_lats_longs=True)
+        points_UTM = data_geo_spatial.get_data_points(absolute=True)
+        attributes = linear_function(points_UTM)
+        att = 'elevation'
+        
+        #Create .txt file
+        txt_file = tempfile.mktemp(".txt")
+        file = open(txt_file,"w")
+        file.write(" x,y," + att + " \n")
+        for data_point, attribute in map(None, points_UTM, attributes):
+            row = str(data_point[0]) + ',' + str(data_point[1]) \
+                  + ',' + str(attribute)
+            #print "row", row 
+            file.write(row + "\n")
+        file.close()
+
+        # setting up the mesh
+        a = [240000, 7620000]
+        b = [240000, 7680000]
+        c = [300000, 7620000]
+        points = [a, b, c]
+        elements = [[0,2,1]]
+        f = fit_to_mesh(txt_file, points, elements, 
+                        alpha=0.0, max_read_lines=2)
+        answer = linear_function(points)
+        #print "f",f
+        #print "answer",answer 
+        assert num.allclose(f, answer)
+
+        # Delete file!
+        os.remove(txt_file)
+        
+    def cache_test_fit_to_mesh_pts(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".txt")
+        file = open(fileName,"w")
+        file.write(" x, y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        geo = Geospatial_data(fileName)
+        fileName_pts = tempfile.mktemp(".pts")
+        points = geo.get_data_points(absolute=True)
+        atts = geo.get_attributes()
+        f = fit_to_mesh(points,atts, vertices, triangles,
+                                alpha=0.0, max_read_lines=2,
+                        use_cache=True, verbose=True)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+        os.remove(fileName)
+       
+    def test_fit_to_mesh_pts(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".txt")
+        file = open(fileName,"w")
+        file.write(" x, y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        geo = Geospatial_data(fileName)
+        fileName_pts = tempfile.mktemp(".pts")
+        geo.export_points_file(fileName_pts)
+        f = fit_to_mesh(fileName_pts, vertices, triangles,
+                                alpha=0.0, max_read_lines=2)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+        os.remove(fileName)
+        os.remove(fileName_pts)
+        
+    def test_fit_to_mesh_pts_passing_mesh_in(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".txt")
+        file = open(fileName,"w")
+        file.write(" x, y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        geo = Geospatial_data(fileName)
+        fileName_pts = tempfile.mktemp(".pts")
+        geo.export_points_file(fileName_pts)
+        f = fit_to_mesh(fileName_pts, vertices, triangles,
+                                alpha=0.0, max_read_lines=2)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+        os.remove(fileName)
+        os.remove(fileName_pts)
+        
+    def test_fit_to_mesh(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".ddd")
+        file = open(fileName,"w")
+        file.write(" x,y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        
+        f = fit_to_mesh(fileName, vertices, triangles,
+                                alpha=0.0, max_read_lines=2)
+                        #use_cache=True, verbose=True)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+    
+        os.remove(fileName)
+
+    def test_fit_to_mesh_using_jacobi_precon(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".ddd")
+        file = open(fileName,"w")
+        file.write(" x,y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        f = fit_to_mesh(fileName, vertices, triangles,
+                                alpha=0.0, max_read_lines=2, use_c_cg=True, cg_precon='Jacobi')
+                        #use_cache=True, verbose=True)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+
+        assert num.allclose(f, answer)
+    
+        os.remove(fileName)
+
+    def test_fit_to_mesh_using_jacobi_precon_no_c_cg(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".ddd")
+        file = open(fileName,"w")
+        file.write(" x,y, elevation \n\
+-2.0, 2.0, 0.\n\
+-1.0, 1.0, 0.\n\
+0.0, 2.0 , 2.\n\
+1.0, 1.0 , 2.\n\
+ 2.0,  1.0 ,3. \n\
+ 0.0,  0.0 , 0.\n\
+ 1.0,  0.0 , 1.\n\
+ 0.0,  -1.0, -1.\n\
+ -0.2, -0.5, -0.7\n\
+ -0.9, -1.5, -2.4\n\
+ 0.5,  -1.9, -1.4\n\
+ 3.0,  1.0 , 4.\n")
+        file.close()
+        f = fit_to_mesh(fileName, vertices, triangles,
+                                alpha=0.0, max_read_lines=2, use_c_cg=False, cg_precon='Jacobi')
+                        #use_cache=True, verbose=True)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+
+        assert num.allclose(f, answer)
+    
+        os.remove(fileName)
+
+    def test_fit_to_mesh_2_atts(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        fileName = tempfile.mktemp(".ddd")
+        file = open(fileName,"w")
+        # the 2nd att name is wacky so it's the first key off a hash table
+        file.write(" x,y, elevation, afriqction \n\
+-2.0, 2.0, 0., 0.\n\
+-1.0, 1.0, 0., 0.\n\
+0.0, 2.0 , 2., 20.\n\
+1.0, 1.0 , 2., 20.\n\
+ 2.0,  1.0 ,3., 30. \n\
+ 0.0,  0.0 , 0., 0.\n\
+ 1.0,  0.0 , 1., 10.\n\
+ 0.0,  -1.0, -1., -10.\n\
+ -0.2, -0.5, -0.7, -7.\n\
+ -0.9, -1.5, -2.4, -24. \n\
+ 0.5,  -1.9, -1.4, -14. \n\
+ 3.0,  1.0 , 4., 40. \n")
+        file.close()
+        
+        f = fit_to_mesh(fileName, vertices, triangles,
+                        alpha=0.0, 
+                        attribute_name='elevation', max_read_lines=2)
+        answer = linear_function(vertices)
+        #print "f\n",f
+        #print "answer\n",answer
+        assert num.allclose(f, answer)
+        os.remove(fileName)
+
+
+
+
+        
+    def test_fit_and_interpolation(self):
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe, daf, dae
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+        #Get (enough) datapoints
+        data_points = [[ 0.66666667, 0.66666667],
+                       [ 1.33333333, 1.33333333],
+                       [ 2.66666667, 0.66666667],
+                       [ 0.66666667, 2.66666667],
+                       [ 0.0, 1.0],
+                       [ 0.0, 3.0],
+                       [ 1.0, 0.0],
+                       [ 1.0, 1.0],
+                       [ 1.0, 2.0],
+                       [ 1.0, 3.0],
+                       [ 2.0, 1.0],
+                       [ 3.0, 0.0],
+                       [ 3.0, 1.0]]
+
+        z = linear_function(data_points)
+        interp = Fit(points, triangles,
+                                alpha=0.0)
+
+        answer = linear_function(points)
+
+        f = interp.fit(data_points, z)
+
+        #print "f",f
+        #print "answer",answer
+        assert num.allclose(f, answer)
+
+    def test_fit_and_interpolation_using_jacobi_precon(self):
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe, daf, dae
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+        #Get (enough) datapoints
+        data_points = [[ 0.66666667, 0.66666667],
+                       [ 1.33333333, 1.33333333],
+                       [ 2.66666667, 0.66666667],
+                       [ 0.66666667, 2.66666667],
+                       [ 0.0, 1.0],
+                       [ 0.0, 3.0],
+                       [ 1.0, 0.0],
+                       [ 1.0, 1.0],
+                       [ 1.0, 2.0],
+                       [ 1.0, 3.0],
+                       [ 2.0, 1.0],
+                       [ 3.0, 0.0],
+                       [ 3.0, 1.0]]
+
+        z = linear_function(data_points)
+        interp = Fit(points, triangles,
+                                alpha=0.0,cg_precon='Jacobi')
+
+        answer = linear_function(points)
+
+        f = interp.fit(data_points, z)
+
+        #print "f",f
+        #print "answer",answer
+        assert num.allclose(f, answer,atol=5)
+
+        
+    def test_smoothing_and_interpolation(self):
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe, daf, dae
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+        #Get (too few!) datapoints
+        data_points = [[ 0.66666667, 0.66666667],
+                       [ 1.33333333, 1.33333333],
+                       [ 2.66666667, 0.66666667],
+                       [ 0.66666667, 2.66666667]]
+
+        z = linear_function(data_points)
+        answer = linear_function(points)
+
+        #Make interpolator with too few data points and no smoothing
+        interp = Fit(points, triangles, alpha=0.0)
+        #Must raise an exception
+        try:
+            f = interp.fit(data_points,z)
+        except TooFewPointsError:
+            pass
+
+        #Now try with smoothing parameter
+        interp = Fit(points, triangles, alpha=1.0e-13)
+
+        f = interp.fit(data_points,z)
+        #f will be different from answer due to smoothing
+        assert num.allclose(f, answer,atol=5)
+
+
+    #Tests of smoothing matrix
+    def test_smoothing_matrix_one_triangle(self):
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+
+        vertices = [ [1,0,2] ]   #bac
+
+        interp = Fit(points, vertices)
+
+        assert num.allclose(interp.get_D(), [[1, -0.5, -0.5],
+                                             [-0.5, 0.5, 0],
+                                             [-0.5, 0, 0.5]])
+
+        #Define f(x,y) = x
+        f = num.array([0,0,2]) #Value at global vertex 2
+
+        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
+        #           int 1 dx dy = area = 2
+        assert num.dot(num.dot(f, interp.get_D()), f) == 2
+
+        #Define f(x,y) = y
+        f = num.array([0,2,0])  #Value at global vertex 1
+
+        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
+        #           int 1 dx dy = area = 2
+        assert num.dot(num.dot(f, interp.get_D()), f) == 2
+
+        #Define f(x,y) = x+y
+        f = num.array([0,2,2])  #Values at global vertex 1 and 2
+
+        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
+        #           int 2 dx dy = 2*area = 4
+        assert num.dot(num.dot(f, interp.get_D()), f) == 4
+
+
+    def test_smoothing_matrix_more_triangles(self):
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0,0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe, daf, dae
+        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+        interp = Fit(points, vertices)
+
+
+        #assert allclose(interp.get_D(), [[1, -0.5, -0.5],
+        #                           [-0.5, 0.5, 0],
+        #                           [-0.5, 0, 0.5]])
+
+        #Define f(x,y) = x
+        f = num.array([0,0,2,0,2,4]) #f evaluated at points a-f
+
+        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
+        #           int 1 dx dy = total area = 8
+        assert num.dot(num.dot(f, interp.get_D()), f) == 8
+
+        #Define f(x,y) = y
+        f = num.array([0,2,0,4,2,0]) #f evaluated at points a-f
+
+        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
+        #           int 1 dx dy = area = 8
+        assert num.dot(num.dot(f, interp.get_D()), f) == 8
+
+        #Define f(x,y) = x+y
+        f = num.array([0,2,2,4,4,4])  #f evaluated at points a-f
+
+        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
+        #           int 2 dx dy = 2*area = 16
+        assert num.dot(num.dot(f, interp.get_D()), f) == 16
+
+
+
+    def test_fit_and_interpolation_with_different_origins(self):
+        """Fit a surface to one set of points. Then interpolate that surface
+        using another set of points.
+        This test tests situtaion where points and mesh belong to a different
+        coordinate system as defined by origin.
+        """
+
+        #Setup mesh used to represent fitted function
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe, daf, dae
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+        #Datapoints to fit from
+        data_points1 = [[ 0.66666667, 0.66666667],
+                        [ 1.33333333, 1.33333333],
+                        [ 2.66666667, 0.66666667],
+                        [ 0.66666667, 2.66666667],
+                        [ 0.0, 1.0],
+                        [ 0.0, 3.0],
+                        [ 1.0, 0.0],
+                        [ 1.0, 1.0],
+                        [ 1.0, 2.0],
+                        [ 1.0, 3.0],
+                        [ 2.0, 1.0],
+                        [ 3.0, 0.0],
+                        [ 3.0, 1.0]]
+
+
+        #First check that things are OK when using same origin
+        mesh_origin = (56, 290000, 618000) #zone, easting, northing
+        data_origin = (56, 290000, 618000) #zone, easting, northing
+
+
+        #Fit surface to mesh
+        interp = Fit(points, triangles, 
+                               alpha=0.0,
+                               mesh_origin = mesh_origin)
+
+        data_geo_spatial = Geospatial_data(data_points1,
+                         geo_reference = Geo_reference(56, 290000, 618000))
+        z = linear_function(data_points1) #Example z-values
+        f = interp.fit(data_geo_spatial, z)   #Fitted values at vertices
+
+        #Shift datapoints according to new origins
+        for k in range(len(data_points1)):
+            data_points1[k][0] += mesh_origin[1] - data_origin[1]
+            data_points1[k][1] += mesh_origin[2] - data_origin[2]
+
+
+
+        #Fit surface to mesh
+        interp = Fit(points, triangles, alpha=0.0)
+        #Fitted values at vertices (using same z as before)
+        f1 = interp.fit(data_points1,z) 
+
+        assert num.allclose(f,f1), 'Fit should have been unaltered'
+
+
+    def test_smooth_attributes_to_mesh_function(self):
+        #Testing 2 attributes smoothed to the mesh
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        points = [a, b, c]
+        triangles = [ [1,0,2] ]   #bac
+
+        d1 = [1.0, 1.0]
+        d2 = [1.0, 3.0]
+        d3 = [3.0, 1.0]
+        z1 = [2, 4]
+        z2 = [4, 8]
+        z3 = [4, 8]
+        data_coords = [d1, d2, d3]
+        z = [z1, z2, z3]
+
+        f = fit_to_mesh(data_coords, vertex_coordinates=points,
+                        triangles=triangles,
+                        point_attributes=z, alpha=0.0)
+        answer = [[0, 0], [5., 10.], [5., 10.]]
+
+        assert num.allclose(f, answer)
+
+    def test_fit_to_mesh_w_georef(self):
+        """Simple check that georef works at the fit_to_mesh level
+        """
+        
+        from anuga.coordinate_transforms.geo_reference import Geo_reference
+
+        #Mesh
+        vertex_coordinates = [[0.76, 0.76],
+                              [0.76, 5.76],
+                              [5.76, 0.76]]
+        triangles = [[0,2,1]]
+
+        mesh_geo = Geo_reference(56,-0.76,-0.76)
+        #print "mesh_geo.get_absolute(vertex_coordinates)", \
+         #     mesh_geo.get_absolute(vertex_coordinates)
+
+        #Data                       
+        data_points = [[ 201.0, 401.0],
+                       [ 201.0, 403.0],
+                       [ 203.0, 401.0]]
+
+        z = [2, 4, 4]
+        
+        data_geo = Geo_reference(56,-200,-400)
+
+        #print "data_geo.get_absolute(data_points)", \
+        #      data_geo.get_absolute(data_points)
+        
+        #Fit
+        zz = fit_to_mesh(data_points, vertex_coordinates=vertex_coordinates,
+                         triangles=triangles,
+                         point_attributes=z,
+                         data_origin = data_geo.get_origin(),
+                         mesh_origin = mesh_geo.get_origin(),
+                         alpha = 0)
+        assert num.allclose( zz, [0,5,5] )
+
+
+    def test_fit_to_mesh_file2domain(self):
+        from load_mesh.loadASCII import import_mesh_file, \
+             export_mesh_file
+        import tempfile
+        import os
+
+        # create a .tsh file, no user outline
+        mesh_dic = {}
+        mesh_dic['vertices'] = [[0.0, 0.0],
+                                          [0.0, 5.0],
+                                          [5.0, 0.0]]
+        mesh_dic['triangles'] =  [[0, 2, 1]]
+        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
+        mesh_dic['triangle_tags'] = ['']
+        mesh_dic['vertex_attributes'] = [[], [], []]
+        mesh_dic['vertiex_attribute_titles'] = []
+        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
+        mesh_dic['segment_tags'] = ['external',
+                                                  'external',
+                                                  'external']
+        mesh_file = tempfile.mktemp(".tsh")
+        export_mesh_file(mesh_file,mesh_dic)
+
+        # create a points .csv file
+        point_file = tempfile.mktemp(".csv")
+        fd = open(point_file,'w')
+        fd.write("x,y, elevation, stage \n\
+        1.0, 1.0,2.,4 \n\
+        1.0, 3.0,4,8 \n\
+        3.0,1.0,4.,8 \n")
+        fd.close()
+
+        mesh_output_file = tempfile.mktemp(".tsh") 
+        fit_to_mesh_file(mesh_file,
+                         point_file,
+                         mesh_output_file,
+                         alpha = 0.0)
+        # load in the .tsh file we just wrote
+        mesh_dic = import_mesh_file(mesh_output_file)
+        #print "mesh_dic",mesh_dic
+        ans =[[0.0, 0.0],
+              [5.0, 10.0],
+              [5.0,10.0]]
+        assert num.allclose(mesh_dic['vertex_attributes'],ans)
+
+        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
+                        ['elevation','stage'],
+                        'test_fit_to_mesh_file failed')
+        domain = Domain(mesh_output_file, use_cache=True, verbose=False)
+        
+        answer = [0., 5., 5.]
+        assert num.allclose(domain.quantities['elevation'].vertex_values,
+                            answer)
+        #clean up
+        os.remove(mesh_file)
+        os.remove(point_file)
+        os.remove(mesh_output_file)
+
+    def test_fit_to_mesh_file3(self):
+        from load_mesh.loadASCII import import_mesh_file, \
+             export_mesh_file
+        import tempfile
+        import os
+
+        # create a .tsh file, no user outline
+        mesh_dic = {}
+        mesh_dic['vertices'] = [[0.76, 0.76],
+                                          [0.76, 5.76],
+                                          [5.76, 0.76]]
+        mesh_dic['triangles'] =  [[0, 2, 1]]
+        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
+        mesh_dic['triangle_tags'] = ['']
+        mesh_dic['vertex_attributes'] = [[], [], []]
+        mesh_dic['vertiex_attribute_titles'] = []
+        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
+        mesh_dic['segment_tags'] = ['external',
+                                                  'external',
+                                                  'external']
+        mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)
+        mesh_file = tempfile.mktemp(".tsh")
+        export_mesh_file(mesh_file,mesh_dic)
+
+        # create a points .csv file
+        point_file = tempfile.mktemp(".csv")
+        fd = open(point_file,'w')
+        fd.write("x,y, elevation, stage \n\
+        1.0, 1.0,2.,4 \n\
+        1.0, 3.0,4,8 \n\
+        3.0,1.0,4.,8 \n")
+        fd.close()
+        
+        mesh_output_file = tempfile.mktemp(".tsh")
+        fit_to_mesh_file(mesh_file,
+                         point_file,
+                         mesh_output_file,
+                         alpha = 0.0)
+        # load in the .tsh file we just wrote
+        mesh_dic = import_mesh_file(mesh_output_file)
+        #print "mesh_dic",mesh_dic
+        ans =[[0.0, 0.0],
+              [5.0, 10.0],
+              [5.0,10.0]]
+        assert num.allclose(mesh_dic['vertex_attributes'],ans)
+
+        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
+                        ['elevation','stage'],
+                        'test_fit_to_mesh_file failed')
+
+        #clean up
+        os.remove(mesh_file)
+        os.remove(point_file)
+        os.remove(mesh_output_file)
+
+    def test_fit_to_mesh_fileII(self):
+        from load_mesh.loadASCII import import_mesh_file, \
+             export_mesh_file
+        import tempfile
+        import os
+
+        # create a .tsh file, no user outline
+        mesh_dic = {}
+        mesh_dic['vertices'] = [[0.0, 0.0],
+                                [0.0, 5.0],
+                                [5.0, 0.0]]
+        mesh_dic['triangles'] =  [[0, 2, 1]]
+        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
+        mesh_dic['triangle_tags'] = ['']
+        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
+        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
+        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
+        mesh_dic['segment_tags'] = ['external',
+                                                  'external',
+                                                  'external']
+        mesh_file = tempfile.mktemp(".tsh")
+        export_mesh_file(mesh_file,mesh_dic)
+
+        # create a points .csv file
+        point_file = tempfile.mktemp(".csv")
+        fd = open(point_file,'w')
+        fd.write("x,y,elevation, stage \n\
+        1.0, 1.0,2.,4 \n\
+        1.0, 3.0,4,8 \n\
+        3.0,1.0,4.,8 \n")
+        fd.close()
+
+        mesh_output_file = "new_triangle.tsh"
+        fit_to_mesh_file(mesh_file,
+                         point_file,
+                         mesh_output_file,
+                         alpha = 0.0)
+        # load in the .tsh file we just wrote
+        mesh_dic = import_mesh_file(mesh_output_file)
+
+        assert num.allclose(mesh_dic['vertex_attributes'],
+                            [[1.0, 2.0,0.0, 0.0],
+                             [1.0, 2.0,5.0, 10.0],
+                             [1.0, 2.0,5.0,10.0]])
+
+        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
+                        ['density', 'temp','elevation','stage'],
+                        'test_fit_to_mesh_file failed')
+
+        #clean up
+        os.remove(mesh_file)
+        os.remove(mesh_output_file)
+        os.remove(point_file)
+
+    def test_fit_to_mesh_file_errors(self):
+        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
+        import tempfile
+        import os
+
+        # create a .tsh file, no user outline
+        mesh_dic = {}
+        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
+        mesh_dic['triangles'] =  [[0, 2, 1]]
+        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
+        mesh_dic['triangle_tags'] = ['']
+        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
+        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
+        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
+        mesh_dic['segment_tags'] = ['external', 'external','external']
+        mesh_file = tempfile.mktemp(".tsh")
+        export_mesh_file(mesh_file,mesh_dic)
+
+        # create a bad points .csv file
+        point_file = tempfile.mktemp(".csv")
+        fd = open(point_file,'w')
+        fd.write("x,y,elevation stage \n\
+        1.0, 1.0,2.,4 \n\
+        1.0, 3.0,4,8 \n\
+        3.0,1.0,4.,8 \n")
+        fd.close()
+
+        mesh_output_file = "new_triangle.tsh"
+        try:
+            fit_to_mesh_file(mesh_file, point_file,
+                             mesh_output_file, display_errors = False)
+        except SyntaxError:
+            pass
+        else:
+            #self.failUnless(0 ==1,  'Bad file did not raise error!')
+            raise Exception('Bad file did not raise error!')
+            
+        #clean up
+        os.remove(mesh_file)
+        os.remove(point_file)
+
+    def test_fit_to_mesh_file_errorsII(self):
+        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
+        import tempfile
+        import os
+
+        # create a .tsh file, no user outline
+        mesh_file = tempfile.mktemp(".tsh")
+        fd = open(mesh_file,'w')
+        fd.write("unit testing a bad .tsh file \n")
+        fd.close()
+
+        # create a points .csv file
+        point_file = tempfile.mktemp(".csv")
+        fd = open(point_file,'w')
+        fd.write("x,y,elevation, stage \n\
+        1.0, 1.0,2.,4 \n\
+        1.0, 3.0,4,8 \n\
+        3.0,1.0,4.,8 \n")
+        fd.close()
+
+        mesh_output_file = "new_triangle.tsh"
+        try:
+            fit_to_mesh_file(mesh_file, point_file,
+                             mesh_output_file, display_errors = False)
+        except IOError:
+            pass
+        else:
+            raise Exception('Bad file did not raise error!')
+            
+        #clean up
+        os.remove(mesh_file)
+        os.remove(point_file)
+
+    def test_fit_to_mesh_file_errorsIII(self):
+        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
+        import tempfile
+        import os
+
+        # create a .tsh file, no user outline
+        mesh_dic = {}
+        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
+        mesh_dic['triangles'] =  [[0, 2, 1]]
+        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
+        mesh_dic['triangle_tags'] = ['']
+        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
+        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
+        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
+        mesh_dic['segment_tags'] = ['external', 'external','external']
+        mesh_file = tempfile.mktemp(".tsh")
+        export_mesh_file(mesh_file,mesh_dic)
+
+
+        # create a points .csv file
+        point_file = tempfile.mktemp(".csv")
+        fd = open(point_file,'w')
+        fd.write("x,y,elevation, stage \n\
+        1.0, 1.0,2.,4 \n\
+        1.0, 3.0,4,8 \n\
+        3.0,1.0,4.,8 \n")
+        fd.close()
+
+        #This a deliberately illegal filename to invoke the error.
+        mesh_output_file = ".../\z\z:ya.tsh"        
+
+        try:
+            fit_to_mesh_file(mesh_file, point_file,
+                             mesh_output_file, display_errors = False)
+        except IOError:
+            pass
+        else:
+            raise Exception('Bad file did not raise error!')
+        
+        #clean up
+        os.remove(mesh_file)
+        os.remove(point_file)
+  
+    def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self):
+
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        d = [1.0, 1.0]
+        e = [18.0, 1000.0]
+        
+        points = [a, b, c, d, e]
+        triangles = [ [1,0,2] ]   #bac
+
+        d1 = [1.0, 1.0]
+        d2 = [1.0, 2.0]
+        d3 = [3.0,1.0]
+        d4 = [2.0,3.0]
+        d5 = [2.0,2.0]
+        d6 = [1.0,3.0]
+        data_coords = [d1, d2, d3, d4, d5, d6]
+        z = linear_function(data_coords)
+        #print "z",z 
+
+        interp = Fit(points, triangles, alpha=0.0)
+        
+        try:
+            f = interp.fit(data_coords, z)
+        except VertsWithNoTrianglesError:
+            pass
+        else:
+            raise Exception('Verts with no triangles did not raise error!')
+        
+        #f = interp.fit(data_coords, z)
+        #answer = linear_function(points)
+
+        #  Removing the bad verts that we don't care about
+        #f = f[0:3]
+        #answer = answer[0:3]
+        #assert allclose(f, answer)
+
+#-------------------------------------------------------------
+if __name__ == "__main__":
+    suite = unittest.makeSuite(Test_Fit,'test')
+    runner = unittest.TextTestRunner() #verbosity=1)
+    runner.run(suite)
+
Index: /trunk/anuga_core/source/anuga/fit_interpolate/test/test_interpolate.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test/test_interpolate.py	(revision 9479)
+++ /trunk/anuga_core/source/anuga/fit_interpolate/test/test_interpolate.py	(revision 9479)
@@ -0,0 +1,1949 @@
+#!/usr/bin/env python
+
+#TEST
+
+#import time, os
+
+
+import sys
+import os
+import unittest
+from math import sqrt
+import tempfile
+import csv
+
+from anuga.file.netcdf import NetCDFFile
+
+import numpy as num
+
+
+# ANUGA code imports
+import anuga
+from interpolate import *
+from anuga.coordinate_transforms.geo_reference import Geo_reference
+from anuga.utilities.numerical_tools import mean, NAN
+from anuga.file.sww import SWW_file
+from anuga.shallow_water.shallow_water_domain import Domain
+from abstract_2d_finite_volumes.quantity import Quantity
+from anuga.geospatial_data.geospatial_data import Geospatial_data
+from anuga.pmesh.mesh import Mesh
+
+def distance(x, y):
+    return sqrt(num.sum((num.array(x)-num.array(y))**2))
+
+def linear_function(point):
+    point = num.array(point)
+    return point[:,0]+point[:,1]
+
+
+class Test_Interpolate(unittest.TestCase):
+
+    def setUp(self):
+
+        import time
+        from mesh_factory import rectangular
+
+
+        #Create basic mesh
+        points, vertices, boundary = rectangular(2, 2)
+
+        #Create shallow water domain
+        domain = Domain(points, vertices, boundary)
+        domain.default_order=2
+
+
+        #Set some field values
+        domain.set_quantity('elevation', lambda x,y: -x)
+        domain.set_quantity('friction', 0.03)
+
+
+        ######################
+        # Boundary conditions
+        B = anuga.Transmissive_boundary(domain)
+        domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B})
+
+
+        ######################
+        #Initial condition - with jumps
+
+        bed = domain.quantities['elevation'].vertex_values
+        stage = num.zeros(bed.shape, num.float)
+
+        h = 0.3
+        for i in range(stage.shape[0]):
+            if i % 2 == 0:
+                stage[i,:] = bed[i,:] + h
+            else:
+                stage[i,:] = bed[i,:]
+
+        domain.set_quantity('stage', stage)
+
+        domain.distribute_to_vertices_and_edges()
+
+
+        self.domain = domain
+
+        C = domain.get_vertex_coordinates()
+        self.X = C[:,0:6:2].copy()
+        self.Y = C[:,1:6:2].copy()
+
+        self.F = bed
+
+
+
+    def tearDown(self):
+        pass
+
+    def test_datapoint_at_centroid(self):
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]   #bac
+
+        data = [ [2.0/3, 2.0/3] ] #Use centroid as one data point
+
+        interp = Interpolate(points, vertices)
+        A, _, _, _ = interp._build_interpolation_matrix_A(data)
+        assert num.allclose(A.todense(), [[1./3, 1./3, 1./3]])
+
+    def test_datapoint_in_hole(self):
+        # create 3 right-angled triangles arranged in a bigger triangle       
+        a = [0.0, 0.0] #0
+        b = [0.0, 2.0] #1
+        c = [2.0,0.0]  #2
+        d = [0.0,4.0]  #3
+        e = [2.0,2.0]  #4
+        f = [4.0,0.0]  #5
+        points = [a, b, c, d, e, f]
+        vertices = [ [1,0,2], [3,1,4], [4,2,5] ]   #bac dbe ecf
+
+        point_in_hole = [1.5, 1.5] # a point in the hole
+        data = [ [20, 20], [0.3, 0.3], point_in_hole, [2.5, 0.3], [30, 30] ] #some points inside and outside the hole
+
+        # any function for the vertices, we don't care about the result
+        f = num.array([linear_function(points), 2*linear_function(points)])
+        f = num.transpose(f)
+
+        interp = Interpolate(points, vertices)
+        interp.interpolate(f, data)
+        
+        assert not set(interp.inside_poly_indices).intersection(set(interp.outside_poly_indices)), \
+               'Some points are in both lists!'    
+        assert len(interp.inside_poly_indices) == 2
+        assert len(interp.outside_poly_indices) == 3
+        
+        interp.outside_poly_indices.sort()
+        assert interp.outside_poly_indices[1] == 2, \
+               'third outside point should be inside the hole!'
+
+    def test_simple_interpolation_example(self):
+        
+        from mesh_factory import rectangular
+
+        # Create basic mesh
+        points, vertices, boundary = rectangular(1, 3)
+
+        # Create shallow water domain
+        domain = Domain(points, vertices, boundary)
+
+        #----------------
+        #Constant values
+        #----------------        
+        quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3],
+                                    [4,4,4],[5,5,5]])
+
+
+        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
+        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
+        # FIXME: This concat should roll into get_vertex_values
+
+
+        # Get interpolated values at centroids
+        interpolation_points = domain.get_centroid_coordinates()
+        answer = quantity.get_values(location='centroids')
+
+        I = Interpolate(vertex_coordinates, triangles)
+        result = I.interpolate(vertex_values, interpolation_points)
+        assert num.allclose(result, answer)
+
+
+        #----------------
+        # Variable values
+        #----------------
+        quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7],
+                                    [1,4,-9],[2,5,0]])
+        
+        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
+        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
+        # FIXME: This concat should roll into get_vertex_values
+
+
+        # Get interpolated values at centroids
+        interpolation_points = domain.get_centroid_coordinates()
+        answer = quantity.get_values(location='centroids')
+
+        I = Interpolate(vertex_coordinates, triangles)
+        result = I.interpolate(vertex_values, interpolation_points)
+        assert num.allclose(result, answer)        
+        
+
+    def test_simple_interpolation_example_using_direct_interface(self):
+        
+        from mesh_factory import rectangular
+
+        # Create basic mesh
+        points, vertices, boundary = rectangular(1, 3)
+
+        # Create shallow water domain
+        domain = Domain(points, vertices, boundary)
+
+        #----------------
+        # Constant values
+        #----------------        
+        quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3],
+                                    [4,4,4],[5,5,5]])
+
+
+        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
+        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
+        # FIXME: This concat should roll into get_vertex_values
+
+
+        # Get interpolated values at centroids
+        interpolation_points = domain.get_centroid_coordinates()
+        answer = quantity.get_values(location='centroids')
+
+        result = interpolate(vertex_coordinates, triangles, vertex_values, interpolation_points)
+        assert num.allclose(result, answer)
+
+
+        #----------------
+        # Variable values
+        #----------------
+        quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7],
+                                    [1,4,-9],[2,5,0]])
+        
+        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
+        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
+        # FIXME: This concat should roll into get_vertex_values
+
+
+        # Get interpolated values at centroids
+        interpolation_points = domain.get_centroid_coordinates()
+        answer = quantity.get_values(location='centroids')
+
+        result = interpolate(vertex_coordinates, triangles,
+                             vertex_values, interpolation_points)
+        assert num.allclose(result, answer)        
+        
+        
+    def test_simple_interpolation_example_using_direct_interface_and_caching(self):
+        
+        from mesh_factory import rectangular
+
+        # Create basic mesh
+        points, vertices, boundary = rectangular(1, 3)
+
+        # Create shallow water domain
+        domain = Domain(points, vertices, boundary)
+
+        #----------------
+        # First call
+        #----------------
+        quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7],
+                                    [1,4,-9],[2,5,0]])
+        
+        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
+        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
+        # FIXME: This concat should roll into get_vertex_values
+
+        # Get interpolated values at centroids
+        interpolation_points = domain.get_centroid_coordinates()
+        answer = quantity.get_values(location='centroids')
+        result = interpolate(vertex_coordinates, triangles,
+                             vertex_values, interpolation_points,
+                             use_cache=True,
+                             verbose=False)
+        assert num.allclose(result, answer)                
+
+        # Second call using the cache
+        result = interpolate(vertex_coordinates, triangles,
+                             vertex_values, interpolation_points,
+                             use_cache=True,
+                             verbose=False)
+        assert num.allclose(result, answer)                        
+        
+        
+    def test_quad_tree(self):
+        p0 = [-10.0, -10.0]
+        p1 = [20.0, -10.0]
+        p2 = [-10.0, 20.0]
+        p3 = [10.0, 50.0]
+        p4 = [30.0, 30.0]
+        p5 = [50.0, 10.0]
+        p6 = [40.0, 60.0]
+        p7 = [60.0, 40.0]
+        p8 = [-66.0, 20.0]
+        p9 = [10.0, -66.0]
+
+        points = [p0, p1, p2, p3, p4, p5, p6, p7, p8, p9]
+        triangles = [ [0, 1, 2],
+                      [3, 2, 4],
+                      [4, 2, 1],
+                      [4, 1, 5],
+                      [3, 4, 6],
+                      [6, 4, 7],
+                      [7, 4, 5],
+                      [8, 0, 2],
+                      [0, 9, 1]]
+
+        data = [ [4,4] ]
+        interp = Interpolate(points, triangles)
+        #print "PDSG - interp.get_A()", interp.get_A()
+        answer =  [ [ 0.06666667,  0.46666667,  0.46666667,  0.,
+                      0., 0. , 0., 0., 0., 0.]]
+
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        assert num.allclose(A.todense(), answer)
+        
+        #interp.set_point_coordinates([[-30, -30]]) #point outside of mesh
+        #print "PDSG - interp.get_A()", interp.get_A()
+        data = [[-30, -30]]
+        answer =  [ [ 0.0,  0.0,  0.0,  0.,
+                      0., 0. , 0., 0., 0., 0.]]
+        
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)        
+        assert num.allclose(A.todense(), answer)
+
+
+        #point outside of quad tree root cell
+        #interp.set_point_coordinates([[-70, -70]])
+        #print "PDSG - interp.get_A()", interp.get_A()
+        data = [[-70, -70]]
+        answer =  [ [ 0.0,  0.0,  0.0,  0.,
+                      0., 0. , 0., 0., 0., 0.]]
+                      
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)        
+        assert num.allclose(A.todense(), answer)
+
+
+    def test_datapoints_at_vertices(self):
+        #Test that data points coinciding with vertices yield a diagonal matrix
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]   #bac
+
+        data = points #Use data at vertices
+
+        interp = Interpolate(points, vertices)
+        answer = [[1., 0., 0.],
+                   [0., 1., 0.],
+                   [0., 0., 1.]]
+                   
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        assert num.allclose(A.todense(), answer)
+
+
+    def test_datapoints_on_edge_midpoints(self):
+        #Try datapoints midway on edges -
+        #each point should affect two matrix entries equally
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]   #bac
+
+        data = [ [0., 1.], [1., 0.], [1., 1.] ]
+        answer =  [[0.5, 0.5, 0.0],  #Affects vertex 1 and 0
+                    [0.5, 0.0, 0.5],  #Affects vertex 0 and 2
+                    [0.0, 0.5, 0.5]]
+        interp = Interpolate(points, vertices)
+
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        assert num.allclose(A.todense(), answer)
+
+    def test_datapoints_on_edges(self):
+        #Try datapoints on edges -
+        #each point should affect two matrix entries in proportion
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]   #bac
+
+        data = [ [0., 1.5], [1.5, 0.], [1.5, 0.5] ]
+        answer =  [[0.25, 0.75, 0.0],  #Affects vertex 1 and 0
+                   [0.25, 0.0, 0.75],  #Affects vertex 0 and 2
+                   [0.0, 0.25, 0.75]]
+
+        interp = Interpolate(points, vertices)
+
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        assert num.allclose(A.todense(), answer)
+
+
+    def test_arbitrary_datapoints(self):
+        #Try arbitrary datapoints
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]   #bac
+
+        data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44] ]
+
+        interp = Interpolate(points, vertices)
+        #print "interp.get_A()", interp.get_A()
+        
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        results = A.todense()
+        assert num.allclose(num.sum(results, axis=1), 1.0)
+
+        
+    def test_arbitrary_datapoints_return_centroids(self):
+        #Try arbitrary datapoints, making sure they return
+        #an interpolation matrix for the intersected triangle's
+        #centroid.
+        
+        a = [1.0, 0.0]
+        b = [0.0, 3.0]
+        c = [4.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]
+
+        data = [ [1.2, 1.5], [1.123, 1.768], [2.43, 0.44] ]
+
+        interp = Interpolate(points, vertices)
+        
+        third = [1.0/3.0, 1.0/3.0, 1.0/3.0]
+        answer = [third, third, third]
+        
+        A,_,_,_ = interp._build_interpolation_matrix_A(data, output_centroids=True)
+        results = A.todense()
+        assert num.allclose(results, answer)        
+        
+        
+    def test_arbitrary_datapoints_some_outside(self):
+        #Try arbitrary datapoints one outside the triangle.
+        #That one should be ignored
+        
+
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0,0.0]
+        points = [a, b, c]
+        vertices = [ [1,0,2] ]   #bac
+
+        data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44], [5.0, 7.0]]
+
+        interp = Interpolate(points, vertices)
+        
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        results = A.todense()
+        assert num.allclose(num.sum(results, axis=1), [1,1,1,0])
+
+
+
+    # this causes a memory error in scipy.sparse
+    def test_more_triangles(self):
+
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0,1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0,-2.0]
+        f = [1.0, -2.0] #5
+
+        points = [a, b, c, d,e,f]
+        triangles = [[0,1,3],[1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc
+
+        #Data points
+        data = [ [-3., 2.0], [-2, 1], [0.0, 1], [0, 3], [2, 3], [-1.0/3,-4./3] ]
+        interp = Interpolate(points, triangles)
+
+        answer = [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0],    #Affects point d
+                  [0.5, 0.0, 0.0, 0.5, 0.0, 0.0],    #Affects points a and d
+                  [0.75, 0.25, 0.0, 0.0, 0.0, 0.0],  #Affects points a and b
+                  [0.0, 0.5, 0.0, 0.5, 0.0, 0.0],    #Affects points a and d
+                  [0.25, 0.75, 0.0, 0.0, 0.0, 0.0],  #Affects points a and b
+                  [1./3, 0.0, 0.0, 0.0, 1./3, 1./3]] #Affects points a, e and f
+
+
+        A,_,_,_ = interp._build_interpolation_matrix_A(data)
+        A = A.todense()
+        for i in range(A.shape[0]):
+            for j in range(A.shape[1]):
+                if not num.allclose(A[i,j], answer[i][j]):
+                    print i,j,':',A[i,j], answer[i][j]
+
+
+        #results = interp._build_interpolation_matrix_A(data).todense()
+
+        assert num.allclose(A, answer)
+    
+    def test_geo_ref(self):
+        v0 = [0.0, 0.0]
+        v1 = [0.0, 5.0]
+        v2 = [5.0, 0.0]
+
+        vertices_absolute = [v0, v1, v2]
+        triangles = [ [1,0,2] ]   #bac
+
+        geo = Geo_reference(57,100, 500)
+
+        vertices = geo.change_points_geo_ref(vertices_absolute)
+        #print "vertices",vertices 
+        
+        d0 = [1.0, 1.0]
+        d1 = [1.0, 2.0]
+        d2 = [3.0, 1.0]
+        point_coords = [ d0, d1, d2]
+
+        interp = Interpolate(vertices, triangles, mesh_origin=geo)
+        f = linear_function(vertices_absolute)
+        z = interp.interpolate(f, point_coords)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+        
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+        
+     
+    def test_sigma_epsilon(self):
+        """
+        def test_sigma_epsilon(self):
+            Testing ticket 168. I could not reduce the bug to this small
+            test though.
+        
+        """
+        v0 = [22031.25, 59687.5]
+        v1 = [22500., 60000.]
+        v2 = [22350.31640625, 59716.71484375]
+
+        vertices = [v0, v1, v2]
+        triangles = [ [1,0,2] ]   #bac
+
+        
+        point_coords = [[22050., 59700.]]
+
+        interp = Interpolate(vertices, triangles)
+        f = linear_function(vertices)
+        z = interp.interpolate(f, point_coords)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+        
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+        
+    def test_Geospatial_verts(self):
+        v0 = [0.0, 0.0]
+        v1 = [0.0, 5.0]
+        v2 = [5.0, 0.0]
+
+        vertices_absolute = [v0, v1, v2]
+        triangles = [ [1,0,2] ]   #bac
+
+        geo = Geo_reference(57,100, 500)
+        vertices = geo.change_points_geo_ref(vertices_absolute)
+        geopoints = Geospatial_data(vertices,geo_reference = geo)
+        #print "vertices",vertices 
+        
+        d0 = [1.0, 1.0]
+        d1 = [1.0, 2.0]
+        d2 = [3.0, 1.0]
+        point_coords = [ d0, d1, d2]
+
+        interp = Interpolate(geopoints, triangles)
+        f = linear_function(vertices_absolute)
+        z = interp.interpolate(f, point_coords)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+        
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+        
+    def test_interpolate_attributes_to_points(self):
+        v0 = [0.0, 0.0]
+        v1 = [0.0, 5.0]
+        v2 = [5.0, 0.0]
+
+        vertices = [v0, v1, v2]
+        triangles = [ [1,0,2] ]   #bac
+
+        d0 = [1.0, 1.0]
+        d1 = [1.0, 2.0]
+        d2 = [3.0, 1.0]
+        point_coords = [ d0, d1, d2]
+
+        interp = Interpolate(vertices, triangles)
+        f = linear_function(vertices)
+        z = interp.interpolate(f, point_coords)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+    def test_interpolate_attributes_to_pointsII(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        point_coords = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [0.0, 2.0],
+                        [1.0, 1.0],
+                        [2.0, 1.0],
+                        [0.0, 0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [3.0, 1.0]]
+
+        interp = Interpolate(vertices, triangles)
+        f = linear_function(vertices)
+        z = interp.interpolate(f, point_coords)
+        answer = linear_function(point_coords)
+        #print "z",z
+        #print "answer",answer
+        assert num.allclose(z, answer)
+
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+        
+    def test_interpolate_attributes_to_pointsIII(self):
+        #Test linear interpolation of known values at vertices to
+        #new points inside a triangle
+        
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        d = [5.0, 5.0]
+
+        vertices = [a, b, c, d]
+        triangles = [ [1,0,2], [2,3,1] ]   #bac, cdb
+
+        #Points within triangle 1
+        d0 = [1.0, 1.0]
+        d1 = [1.0, 2.0]
+        d2 = [3.0, 1.0]
+
+        #Point within triangle 2
+        d3 = [4.0, 3.0]
+
+        #Points on common edge
+        d4 = [2.5, 2.5]
+        d5 = [4.0, 1.0]
+
+        #Point on common vertex
+        d6 = [0., 5.]
+        
+        point_coords = [d0, d1, d2, d3, d4, d5, d6]
+
+        interp = Interpolate(vertices, triangles)
+
+        #Known values at vertices
+        #Functions are x+y, x+2y, 2x+y, x-y-5
+        f = [ [0., 0., 0., -5.],        # (0,0)
+              [5., 10., 5., -10.],      # (0,5)
+              [5., 5., 10.0, 0.],       # (5,0)
+              [10., 15., 15., -5.]]     # (5,5)
+
+        z = interp.interpolate(f, point_coords)
+        answer = [ [2., 3., 3., -5.],   # (1,1)
+                   [3., 5., 4., -6.],   # (1,2)
+                   [4., 5., 7., -3.],   # (3,1)
+                   [7., 10., 11., -4.], # (4,3)
+                   [5., 7.5, 7.5, -5.], # (2.5, 2.5)
+                   [5., 6., 9., -2.],   # (4,1)
+                   [5., 10., 5., -10.]]  # (0,5)
+
+        #print "***********"
+        #print "z",z
+        #print "answer",answer
+        #print "***********"
+
+        assert num.allclose(z, answer)
+
+
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+        
+    def test_interpolate_point_outside_of_mesh(self):
+        #Test linear interpolation of known values at vertices to
+        #new points inside a triangle
+        
+        a = [0.0, 0.0]
+        b = [0.0, 5.0]
+        c = [5.0, 0.0]
+        d = [5.0, 5.0]
+
+        vertices = [a, b, c, d]
+        triangles = [ [1,0,2], [2,3,1] ]   #bac, cdb
+
+        #Far away point
+        d7 = [-1., -1.]
+        
+        point_coords = [ d7]
+        interp = Interpolate(vertices, triangles)
+
+        #Known values at vertices
+        #Functions are x+y, x+2y, 2x+y, x-y-5
+        f = [ [0., 0., 0., -5.],        # (0,0)
+              [5., 10., 5., -10.],      # (0,5)
+              [5., 5., 10.0, 0.],       # (5,0)
+              [10., 15., 15., -5.]]     # (5,5)
+
+        z = interp.interpolate(f, point_coords) #, verbose=True)
+        answer = num.array([ [NAN, NAN, NAN, NAN]]) # (-1,-1)
+
+        #print "***********"
+        #print "z",z
+        #print "answer",answer
+        #print "***********"
+
+        #Should an error message be returned if points are outside
+        # of the mesh?
+        # A warning message is printed, if verbose is on.
+
+        for i in range(4):
+            self.failUnless( z[0,i] == answer[0,i], 'Fail!')
+        
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+
+        #print "z",z 
+        #print "answer",answer
+        
+        for i in range(4):
+            self.failUnless( z[0,i] == answer[0,i], 'Fail!')
+        
+        
+    def test_interpolate_attributes_to_pointsIV(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        point_coords = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [0.0, 2.0],
+                        [1.0, 1.0],
+                        [2.0, 1.0],
+                        [0.0, 0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [3.0, 1.0]]
+
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices),2*linear_function(vertices)])
+        f = num.transpose(f)
+        #print "f",f
+        z = interp.interpolate(f, point_coords)
+        answer = [linear_function(point_coords),
+                  2*linear_function(point_coords) ]
+        answer = num.transpose(answer)
+        #print "z",z
+        #print "answer",answer
+        assert num.allclose(z, answer)
+
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+    def test_interpolate_blocking(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        point_coords = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [0.0, 2.0],
+                        [1.0, 1.0],
+                        [2.0, 1.0],
+                        [0.0, 0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [3.0, 1.0]]
+
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices),2*linear_function(vertices)])
+        f = num.transpose(f)
+        #print "f",f
+        for blocking_max in range(len(point_coords)+2):
+        #if True:
+         #   blocking_max = 5
+            z = interp.interpolate(f, point_coords,
+                                   start_blocking_len=blocking_max)
+            answer = [linear_function(point_coords),
+                      2*linear_function(point_coords) ]
+            answer = num.transpose(answer)
+            #print "z",z
+            #print "answer",answer
+            assert num.allclose(z, answer)
+            
+        f = num.array([linear_function(vertices),2*linear_function(vertices),
+                       2*linear_function(vertices) - 100])
+        f = num.transpose(f)
+        #print "f",f
+        for blocking_max in range(len(point_coords)+2):
+        #if True:
+         #   blocking_max = 5
+            z = interp.interpolate(f, point_coords,
+                                   start_blocking_len=blocking_max)
+            answer = num.array([linear_function(point_coords),
+                                2*linear_function(point_coords) ,
+                                2*linear_function(point_coords)-100])
+            z = num.transpose(z)
+            #print "z",z
+            #print "answer",answer
+            assert num.allclose(z, answer)
+
+    def test_interpolate_geo_spatial(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        point_coords_absolute = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [0.0, 2.0],
+                        [1.0, 1.0],
+                        [2.0, 1.0],
+                        [0.0, 0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [3.0, 1.0]]
+
+        geo = Geo_reference(57,100, 500)
+        point_coords = geo.change_points_geo_ref(point_coords_absolute)
+        point_coords = Geospatial_data(point_coords,geo_reference = geo)
+        
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices),2*linear_function(vertices)])
+        f = num.transpose(f)
+        #print "f",f
+        for blocking_max in range(14):
+        #if True:
+         #   blocking_max = 5
+            z = interp.interpolate(f, point_coords,
+                                   start_blocking_len=blocking_max)
+            answer = [linear_function(point_coords.get_data_points( \
+                      absolute = True)),
+                      2*linear_function(point_coords.get_data_points( \
+                      absolute = True)) ]
+            answer = num.transpose(answer)
+            #print "z",z
+            #print "answer",answer
+            assert num.allclose(z, answer)
+            
+        f = num.array([linear_function(vertices),2*linear_function(vertices),
+                       2*linear_function(vertices) - 100])
+        f = num.transpose(f)
+        #print "f",f
+        for blocking_max in range(14):
+        #if True:
+         #   blocking_max = 5
+            z = interp.interpolate(f, point_coords,
+                                   start_blocking_len=blocking_max)
+            answer = num.array([linear_function(point_coords.get_data_points( \
+                                                              absolute = True)),
+                                                              2*linear_function(point_coords.get_data_points( \
+                                                              absolute = True)) ,
+                                                              2*linear_function(point_coords.get_data_points( \
+                                                              absolute = True))-100])
+            z = num.transpose(z)
+            #print "z",z
+            #print "answer",answer
+            assert num.allclose(z, answer)
+
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+        
+    def test_interpolate_geo_spatial(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+        point_coords_absolute = [[-2.0,  2.0],
+                                 [-1.0,  1.0],
+                                 [ 0.0,  2.0],
+                                 [ 1.0,  1.0],
+                                 [ 2.0,  1.0],
+                                 [ 0.0,  0.0],
+                                 [ 1.0,  0.0],
+                                 [ 0.0, -1.0],
+                                 [-0.2, -0.5],
+                                 [-0.9, -1.5],
+                                 [ 0.5, -1.9],
+                                 [ 3.0,  1.0]]
+
+        geo = Geo_reference(57, 100, 500)
+        point_coords = geo.change_points_geo_ref(point_coords_absolute)
+        point_coords = Geospatial_data(point_coords, geo_reference=geo)
+        
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices), 2*linear_function(vertices)])
+        f = num.transpose(f)
+        z = interp.interpolate_block(f, point_coords)
+        answer = [linear_function(point_coords.get_data_points(absolute=True)),
+                  2*linear_function(point_coords.get_data_points(absolute=True))
+                 ]
+        answer = num.transpose(answer)
+        msg = ('Expected z\n%s\nto be close to answer\n%s'
+               % (str(z), str(answer)))
+        assert num.allclose(z, answer), msg
+            
+        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
+
+        msg = ('Expected z\n%s\nto be close to answer\n%s'
+               % (str(z), str(answer)))
+        assert num.allclose(z, answer)
+
+        
+    def test_interpolate_reuse_if_None(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        point_coords = [[-2.0,  2.0],
+                        [-1.0,  1.0],
+                        [ 0.0,  2.0],
+                        [ 1.0,  1.0],
+                        [ 2.0,  1.0],
+                        [ 0.0,  0.0],
+                        [ 1.0,  0.0],
+                        [ 0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [ 0.5, -1.9],
+                        [ 3.0,  1.0]]
+
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices),2*linear_function(vertices)])
+        f = num.transpose(f)
+        z = interp.interpolate(f, point_coords,
+                               start_blocking_len=20)
+        answer = [linear_function(point_coords),
+                  2*linear_function(point_coords) ]
+        answer = num.transpose(answer)
+        #print "z",z
+        #print "answer",answer
+        assert num.allclose(z, answer)
+        assert num.allclose(interp._A_can_be_reused, True)
+
+        z = interp.interpolate(f)
+        assert num.allclose(z, answer)
+        
+        # This causes blocking to occur. 
+        z = interp.interpolate(f, start_blocking_len=10)
+        assert num.allclose(z, answer)
+        assert num.allclose(interp._A_can_be_reused, False)
+
+        #A is recalculated
+        z = interp.interpolate(f)
+        assert num.allclose(z, answer)
+        assert num.allclose(interp._A_can_be_reused, True)
+        
+        interp = Interpolate(vertices, triangles)
+        #Must raise an exception, no points specified
+        try:
+            z = interp.interpolate(f)
+        except:
+            pass
+        
+    def xxtest_interpolate_reuse_if_same(self):
+
+        # This on tests that repeated identical interpolation
+        # points makes use of precomputed matrix (Ole)
+        # This is not really a test and is disabled for now
+        
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+
+        point_coords = [[-2.0,  2.0],
+                        [-1.0,  1.0],
+                        [ 0.0,  2.0],
+                        [ 1.0,  1.0],
+                        [ 2.0,  1.0],
+                        [ 0.0,  0.0],
+                        [ 1.0,  0.0],
+                        [ 0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [ 0.5, -1.9],
+                        [ 3.0,  1.0]]
+
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices), 2*linear_function(vertices)])
+        f = num.transpose(f)
+        z = interp.interpolate(f, point_coords)
+        answer = [linear_function(point_coords),
+                  2*linear_function(point_coords) ]
+        answer = num.transpose(answer)
+
+        assert num.allclose(z, answer)
+        assert num.allclose(interp._A_can_be_reused, True)
+
+
+        z = interp.interpolate(f)    # None
+        assert num.allclose(z, answer)        
+        z = interp.interpolate(f, point_coords) # Repeated (not really a test)        
+        assert num.allclose(z, answer)
+        
+
+
+    def test_interpolation_interface_time_only(self):
+
+        # Test spatio-temporal interpolation
+        # Test that spatio temporal function performs the correct
+        # interpolations in both time and space
+        
+
+
+        #Three timesteps
+        time = [1.0, 5.0, 6.0]
+        
+
+        #One quantity
+        Q = num.zeros( (3,6), num.float )
+
+        #Linear in time and space
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        
+        for i, t in enumerate(time):
+            Q[i, :] = t*linear_function(points)
+
+            
+        #Check basic interpolation of one quantity using averaging
+        #(no interpolation points or spatial info)
+        I = Interpolation_function(time, [mean(Q[0,:]),
+                                          mean(Q[1,:]),
+                                          mean(Q[2,:])])
+
+
+
+        #Check temporal interpolation
+        for i in [0,1,2]:
+            assert num.allclose(I(time[i]), mean(Q[i,:]))
+
+        #Midway    
+        assert num.allclose(I( (time[0] + time[1])/2 ),
+                            (I(time[0]) + I(time[1]))/2 )
+
+        assert num.allclose(I( (time[1] + time[2])/2 ),
+                            (I(time[1]) + I(time[2]))/2 )
+
+        assert num.allclose(I( (time[0] + time[2])/2 ),
+                            (I(time[0]) + I(time[2]))/2 )                 
+
+        #1/3
+        assert num.allclose(I( (time[0] + time[2])/3 ),
+                            (I(time[0]) + I(time[2]))/3 )                         
+
+
+        #Out of bounds checks
+        try:
+            I(time[0]-1) 
+        except:
+            pass
+        else:
+            raise Exception('Should raise exception')
+
+        try:
+            I(time[-1]+1) 
+        except:
+            pass
+        else:
+            raise Exception('Should raise exception')
+
+
+        
+
+    def test_interpolation_interface_spatial_only(self):
+        # Test spatio-temporal interpolation with constant time
+        
+        #Three timesteps
+        time = [1.0, 5.0, 6.0]
+               
+        #Setup mesh used to represent fitted function
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+
+        #New datapoints where interpolated values are sought
+        interpolation_points = [[ 0.0, 0.0],
+                                [ 0.5, 0.5],
+                                [ 0.7, 0.7],
+                                [ 1.0, 0.5],
+                                [ 2.0, 0.4],
+                                [ 2.8, 1.2]]
+
+
+        #One quantity linear in space
+        Q = linear_function(points)
+
+
+        #Check interpolation of one quantity using interpolaton points
+        I = Interpolation_function(time, Q,
+                                   vertex_coordinates = points,
+                                   triangles = triangles, 
+                                   interpolation_points = interpolation_points,
+                                   verbose = False)
+
+
+        answer = linear_function(interpolation_points)
+
+        t = time[0]
+        for j in range(50): #t in [1, 6]
+            for id in range(len(interpolation_points)):
+                assert num.allclose(I(t, id), answer[id])
+            t += 0.1    
+
+        try:    
+            I(1)
+        except:
+            pass
+        else:
+            raise Exception('Should raise exception')
+
+            
+    def test_interpolation_interface(self):
+        # Test spatio-temporal interpolation
+        # Test that spatio temporal function performs the correct
+        # interpolations in both time and space
+    
+        #Three timesteps
+        time = [1.0, 5.0, 6.0]    
+
+        #Setup mesh used to represent fitted function
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+
+        #New datapoints where interpolated values are sought
+        interpolation_points = [[ 0.0, 0.0],
+                                [ 0.5, 0.5],
+                                [ 0.7, 0.7],
+                                [ 1.0, 0.5],
+                                [ 2.0, 0.4],
+                                [ 2.8, 1.2]]
+
+        #One quantity
+        Q = num.zeros( (3,6), num.float )
+
+        #Linear in time and space
+        for i, t in enumerate(time):
+            Q[i, :] = t*linear_function(points)
+
+        #Check interpolation of one quantity using interpolaton points)
+        I = Interpolation_function(time, Q,
+                                   vertex_coordinates = points,
+                                   triangles = triangles, 
+                                   interpolation_points = interpolation_points,
+                                   verbose = False)
+
+        answer = linear_function(interpolation_points)
+        
+        t = time[0]
+        for j in range(50): #t in [1, 6]
+            for id in range(len(interpolation_points)):
+                assert num.allclose(I(t, id), t*answer[id])
+            t += 0.1    
+
+        try:    
+            I(1)
+        except:
+            pass
+        else:
+            raise Exception('Should raise exception')
+
+
+
+    def test_interpolation_interface_with_time_thinning(self):
+        # Test spatio-temporal interpolation
+        # Test that spatio temporal function performs the correct
+        # interpolations in both time and space
+    
+        # Eight timesteps
+        time = [1.0, 2.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0]    
+
+        # Setup mesh used to represent fitted function
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        # bac, bce, ecf, dbe
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+
+        # New datapoints where interpolated values are sought
+        interpolation_points = [[ 0.0, 0.0],
+                                [ 0.5, 0.5],
+                                [ 0.7, 0.7],
+                                [ 1.0, 0.5],
+                                [ 2.0, 0.4],
+                                [ 2.8, 1.2]]
+
+        # One quantity
+        Q = num.zeros((8,6), num.float)
+
+        # Linear in time and space
+        for i, t in enumerate(time):
+            Q[i, :] = t*linear_function(points)
+
+        # Check interpolation of one quantity using interpolaton points) using default
+        # time_thinning of 1
+        I = Interpolation_function(time, Q,
+                                   vertex_coordinates=points,
+                                   triangles=triangles, 
+                                   interpolation_points=interpolation_points,
+                                   verbose=False)
+
+        answer = linear_function(interpolation_points)
+
+        
+        t = time[0]
+        for j in range(90): #t in [1, 10]
+            for id in range(len(interpolation_points)):
+                assert num.allclose(I(t, id), t*answer[id])
+            t += 0.1    
+
+
+        # Now check time_thinning
+        I = Interpolation_function(time, Q,
+                                   vertex_coordinates=points,
+                                   triangles=triangles, 
+                                   interpolation_points=interpolation_points,
+                                   time_thinning=2,
+                                   verbose=False)
+
+
+        assert len(I.time) == 4
+        assert( num.allclose(I.time, [1.0, 4.0, 7.0, 9.0] ))    
+
+        answer = linear_function(interpolation_points)
+
+        t = time[0]
+        for j in range(80): #t in [1, 9]
+            for id in range(len(interpolation_points)):
+                assert num.allclose(I(t, id), t*answer[id])
+            t += 0.1    
+
+
+
+    def test_interpolation_precompute_points(self):
+        # looking at a discrete mesh
+        #
+    
+        #Three timesteps
+        time = [0.0, 60.0]    
+
+        #Setup mesh used to represent fitted function
+        points = [[ 15., -20.],
+                  [ 15.,  10.],
+                  [  0., -20.],
+                  [  0.,  10.],
+                  [  0., -20.],
+                  [ 15.,  10.]]
+        
+        triangles = [[0, 1, 2],
+                     [3, 4, 5]]
+
+        #New datapoints where interpolated values are sought
+        interpolation_points = [[ 1.,  0.], [0.,1.]]
+
+        #One quantity
+        Q = num.zeros( (2,6), num.float )
+
+        #Linear in time and space
+        for i, t in enumerate(time):
+            Q[i, :] = t*linear_function(points)
+        #print "Q", Q
+
+
+        
+        interp = Interpolate(points, triangles)
+        f = num.array([linear_function(points),2*linear_function(points)])
+        f = num.transpose(f)
+        #print "f",f
+        z = interp.interpolate(f, interpolation_points)
+        answer = [linear_function(interpolation_points),
+                  2*linear_function(interpolation_points) ]
+        answer = num.transpose(answer)
+        #print "z",z
+        #print "answer",answer
+        assert num.allclose(z, answer)
+
+
+        #Check interpolation of one quantity using interpolaton points)
+        I = Interpolation_function(time, Q,
+                                   vertex_coordinates = points,
+                                   triangles = triangles, 
+                                   interpolation_points = interpolation_points,
+                                   verbose = False)
+        
+        #print "I.precomputed_values", I.precomputed_values
+
+        msg = 'Interpolation failed'
+        assert num.allclose(I.precomputed_values['Attribute'][1], [60, 60]), msg
+        #self.failUnless( I.precomputed_values['Attribute'][1] == 60.0,
+        #                ' failed')
+        
+    def test_interpolation_function_outside_point(self):
+        # Test spatio-temporal interpolation
+        # Test that spatio temporal function performs the correct
+        # interpolations in both time and space
+    
+        # Three timesteps
+        time = [1.0, 5.0, 6.0]    
+
+        # Setup mesh used to represent fitted function
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+
+        # New datapoints where interpolated values are sought
+        interpolation_points = [[ 0.0, 0.0],
+                                [ 0.5, 0.5],
+                                [ 0.7, 0.7],
+                                [ 1.0, 0.5],
+                                [ 2.0, 0.4],
+                                [ 545354534, 4354354353]] # outside the mesh
+
+        # One quantity
+        Q = num.zeros( (3,6), num.float )
+
+        # Linear in time and space
+        for i, t in enumerate(time):
+            Q[i, :] = t*linear_function(points)
+
+        # Check interpolation of one quantity using interpolaton points)
+
+        I = Interpolation_function(time, Q,
+                                   vertex_coordinates = points,
+                                   triangles = triangles, 
+                                   interpolation_points = interpolation_points,
+                                   verbose = False)
+        
+        
+        assert num.alltrue(I.precomputed_values['Attribute'][:,4] != NAN)
+        assert num.sometrue(I.precomputed_values['Attribute'][:,5] == NAN)
+
+        #X = I.precomputed_values['Attribute'][1,:]
+        #print X
+        #print take(X, X == NAN)
+        #print where(X == NAN, range(len(X)), 0)        
+        
+        answer = linear_function(interpolation_points)
+          
+        t = time[0]
+        for j in range(50): #t in [1, 6]
+            for id in range(len(interpolation_points)-1):
+                assert num.allclose(I(t, id), t*answer[id])
+            t += 0.1
+            
+        # Now test the point outside the mesh
+        t = time[0]
+        for j in range(50): #t in [1, 6]
+            self.failUnless(I(t, 5) == NAN, 'Fail!')
+            t += 0.1  
+            
+        try:    
+            I(1)
+        except:
+            pass
+        else:
+            raise Exception('Should raise exception')
+
+
+    def test_interpolation_function_time(self):
+        #Test a long time series with an error in it (this did cause an
+        #error once)
+        
+
+        time = num.array(\
+        [0.00000000e+00, 5.00000000e-02, 1.00000000e-01,   1.50000000e-01,
+        2.00000000e-01,   2.50000000e-01,   3.00000000e-01,   3.50000000e-01,
+        4.00000000e-01,   4.50000000e-01,   5.00000000e-01,   5.50000000e-01,
+        6.00000000e-01,   6.50000000e-01,   7.00000000e-01,   7.50000000e-01,
+        8.00000000e-01,   8.50000000e-01,   9.00000000e-01,   9.50000000e-01,
+        1.00000000e-00,   1.05000000e+00,   1.10000000e+00,   1.15000000e+00,
+        1.20000000e+00,   1.25000000e+00,   1.30000000e+00,   1.35000000e+00,
+        1.40000000e+00,   1.45000000e+00,   1.50000000e+00,   1.55000000e+00,
+        1.60000000e+00,   1.65000000e+00,   1.70000000e+00,   1.75000000e+00,
+        1.80000000e+00,   1.85000000e+00,   1.90000000e+00,   1.95000000e+00,
+        2.00000000e+00,   2.05000000e+00,   2.10000000e+00,   2.15000000e+00,
+        2.20000000e+00,   2.25000000e+00,   2.30000000e+00,   2.35000000e+00,
+        2.40000000e+00,   2.45000000e+00,   2.50000000e+00,   2.55000000e+00,
+        2.60000000e+00,   2.65000000e+00,   2.70000000e+00,   2.75000000e+00,
+        2.80000000e+00,   2.85000000e+00,   2.90000000e+00,   2.95000000e+00,
+        3.00000000e+00,   3.05000000e+00,   9.96920997e+36,   3.15000000e+00,
+        3.20000000e+00,   3.25000000e+00,   3.30000000e+00,   3.35000000e+00,
+        3.40000000e+00,   3.45000000e+00,   3.50000000e+00,   3.55000000e+00,
+        3.60000000e+00,   3.65000000e+00,   3.70000000e+00,   3.75000000e+00,
+        3.80000000e+00,   3.85000000e+00,   3.90000000e+00,   3.95000000e+00,
+        4.00000000e+00,   4.05000000e+00,   4.10000000e+00,   4.15000000e+00,
+        4.20000000e+00,   4.25000000e+00,   4.30000000e+00,   4.35000000e+00,
+        4.40000000e+00,   4.45000000e+00,   4.50000000e+00,   4.55000000e+00,
+        4.60000000e+00,   4.65000000e+00,   4.70000000e+00,   4.75000000e+00,
+        4.80000000e+00,   4.85000000e+00,   4.90000000e+00,   4.95000000e+00,
+        5.00000000e+00,   5.05000000e+00,   5.10000000e+00,   5.15000000e+00,
+        5.20000000e+00,   5.25000000e+00,   5.30000000e+00,   5.35000000e+00,
+        5.40000000e+00,   5.45000000e+00,   5.50000000e+00,   5.55000000e+00,
+        5.60000000e+00,   5.65000000e+00,   5.70000000e+00,   5.75000000e+00,
+        5.80000000e+00,   5.85000000e+00,   5.90000000e+00,   5.95000000e+00,
+        6.00000000e+00,   6.05000000e+00,   6.10000000e+00,   6.15000000e+00,
+        6.20000000e+00,   6.25000000e+00,   6.30000000e+00,   6.35000000e+00,
+        6.40000000e+00,   6.45000000e+00,   6.50000000e+00,   6.55000000e+00,
+        6.60000000e+00,   6.65000000e+00,   6.70000000e+00,   6.75000000e+00,
+        6.80000000e+00,   6.85000000e+00,   6.90000000e+00,   6.95000000e+00,
+        7.00000000e+00,   7.05000000e+00,   7.10000000e+00,   7.15000000e+00,
+        7.20000000e+00,   7.25000000e+00,   7.30000000e+00,   7.35000000e+00,
+        7.40000000e+00,   7.45000000e+00,   7.50000000e+00,   7.55000000e+00,
+        7.60000000e+00,   7.65000000e+00,   7.70000000e+00,   7.75000000e+00,
+        7.80000000e+00,   7.85000000e+00,   7.90000000e+00,   7.95000000e+00,
+        8.00000000e+00,   8.05000000e+00,   8.10000000e+00,   8.15000000e+00,
+        8.20000000e+00,   8.25000000e+00,   8.30000000e+00,   8.35000000e+00,
+        8.40000000e+00,   8.45000000e+00,   8.50000000e+00,   8.55000000e+00,
+        8.60000000e+00,   8.65000000e+00,   8.70000000e+00,   8.75000000e+00,
+        8.80000000e+00,   8.85000000e+00,   8.90000000e+00,   8.95000000e+00,
+        9.00000000e+00,   9.05000000e+00,   9.10000000e+00,   9.15000000e+00,
+        9.20000000e+00,   9.25000000e+00,   9.30000000e+00,   9.35000000e+00,
+        9.40000000e+00,   9.45000000e+00,   9.50000000e+00,   9.55000000e+00,
+        9.60000000e+00,   9.65000000e+00,   9.70000000e+00,   9.75000000e+00,
+        9.80000000e+00,   9.85000000e+00,   9.90000000e+00,   9.95000000e+00,
+        1.00000000e+01,   1.00500000e+01,   1.01000000e+01,   1.01500000e+01,
+        1.02000000e+01,   1.02500000e+01,   1.03000000e+01,   1.03500000e+01,
+        1.04000000e+01,   1.04500000e+01,   1.05000000e+01,   1.05500000e+01,
+        1.06000000e+01,   1.06500000e+01,   1.07000000e+01,   1.07500000e+01,
+        1.08000000e+01,   1.08500000e+01,   1.09000000e+01,   1.09500000e+01,
+        1.10000000e+01,   1.10500000e+01,   1.11000000e+01,   1.11500000e+01,
+        1.12000000e+01,   1.12500000e+01,   1.13000000e+01,   1.13500000e+01,
+        1.14000000e+01,   1.14500000e+01,   1.15000000e+01,   1.15500000e+01,
+        1.16000000e+01,   1.16500000e+01,   1.17000000e+01,   1.17500000e+01,
+        1.18000000e+01,   1.18500000e+01,   1.19000000e+01,   1.19500000e+01,
+        1.20000000e+01,   1.20500000e+01,   1.21000000e+01,   1.21500000e+01,
+        1.22000000e+01,   1.22500000e+01,   1.23000000e+01,   1.23500000e+01,
+        1.24000000e+01,   1.24500000e+01,   1.25000000e+01,   1.25500000e+01,
+        1.26000000e+01,   1.26500000e+01,   1.27000000e+01,   1.27500000e+01,
+        1.28000000e+01,   1.28500000e+01,   1.29000000e+01,   1.29500000e+01,
+        1.30000000e+01,   1.30500000e+01,   1.31000000e+01,   1.31500000e+01,
+        1.32000000e+01,   1.32500000e+01,   1.33000000e+01,   1.33500000e+01,
+        1.34000000e+01,   1.34500000e+01,   1.35000000e+01,   1.35500000e+01,
+        1.36000000e+01,   1.36500000e+01,   1.37000000e+01,   1.37500000e+01,
+        1.38000000e+01,   1.38500000e+01,   1.39000000e+01,   1.39500000e+01,
+        1.40000000e+01,   1.40500000e+01,   1.41000000e+01,   1.41500000e+01,
+        1.42000000e+01,   1.42500000e+01,   1.43000000e+01,   1.43500000e+01,
+        1.44000000e+01,   1.44500000e+01,   1.45000000e+01,   1.45500000e+01,
+        1.46000000e+01,   1.46500000e+01,   1.47000000e+01,   1.47500000e+01,
+        1.48000000e+01,   1.48500000e+01,   1.49000000e+01,   1.49500000e+01,
+        1.50000000e+01,   1.50500000e+01,   1.51000000e+01,   1.51500000e+01,
+        1.52000000e+01,   1.52500000e+01,   1.53000000e+01,   1.53500000e+01,
+        1.54000000e+01,   1.54500000e+01,   1.55000000e+01,   1.55500000e+01,
+        1.56000000e+01,   1.56500000e+01,   1.57000000e+01,   1.57500000e+01,
+        1.58000000e+01,   1.58500000e+01,   1.59000000e+01,   1.59500000e+01,
+        1.60000000e+01,   1.60500000e+01,   1.61000000e+01,   1.61500000e+01,
+        1.62000000e+01,   1.62500000e+01,   1.63000000e+01,   1.63500000e+01,
+        1.64000000e+01,   1.64500000e+01,   1.65000000e+01,   1.65500000e+01,
+        1.66000000e+01,   1.66500000e+01,   1.67000000e+01,   1.67500000e+01,
+        1.68000000e+01,   1.68500000e+01,   1.69000000e+01,   1.69500000e+01,
+        1.70000000e+01,   1.70500000e+01,   1.71000000e+01,   1.71500000e+01,
+        1.72000000e+01,   1.72500000e+01,   1.73000000e+01,   1.73500000e+01,
+        1.74000000e+01,   1.74500000e+01,   1.75000000e+01,   1.75500000e+01,
+        1.76000000e+01,   1.76500000e+01,   1.77000000e+01,   1.77500000e+01,
+        1.78000000e+01,   1.78500000e+01,   1.79000000e+01,   1.79500000e+01,
+        1.80000000e+01,   1.80500000e+01,   1.81000000e+01,   1.81500000e+01,
+        1.82000000e+01,   1.82500000e+01,   1.83000000e+01,   1.83500000e+01,
+        1.84000000e+01,   1.84500000e+01,   1.85000000e+01,   1.85500000e+01,
+        1.86000000e+01,   1.86500000e+01,   1.87000000e+01,   1.87500000e+01,
+        1.88000000e+01,   1.88500000e+01,   1.89000000e+01,   1.89500000e+01,
+        1.90000000e+01,   1.90500000e+01,   1.91000000e+01,   1.91500000e+01,
+        1.92000000e+01,   1.92500000e+01,   1.93000000e+01,   1.93500000e+01,
+        1.94000000e+01,   1.94500000e+01,   1.95000000e+01,   1.95500000e+01,
+        1.96000000e+01,   1.96500000e+01,   1.97000000e+01,   1.97500000e+01,
+        1.98000000e+01,   1.98500000e+01,   1.99000000e+01,   1.99500000e+01,
+        2.00000000e+01,   2.00500000e+01,   2.01000000e+01,   2.01500000e+01,
+        2.02000000e+01,   2.02500000e+01,   2.03000000e+01,   2.03500000e+01,
+        2.04000000e+01,   2.04500000e+01,   2.05000000e+01,   2.05500000e+01,
+        2.06000000e+01,   2.06500000e+01,   2.07000000e+01,   2.07500000e+01,
+        2.08000000e+01,   2.08500000e+01,   2.09000000e+01,   2.09500000e+01,
+        2.10000000e+01,   2.10500000e+01,   2.11000000e+01,   2.11500000e+01,
+        2.12000000e+01,   2.12500000e+01,   2.13000000e+01,   2.13500000e+01,
+        2.14000000e+01,   2.14500000e+01,   2.15000000e+01,   2.15500000e+01,
+        2.16000000e+01,   2.16500000e+01,   2.17000000e+01,   2.17500000e+01,
+        2.18000000e+01,   2.18500000e+01,   2.19000000e+01,   2.19500000e+01,
+        2.20000000e+01,   2.20500000e+01,   2.21000000e+01,   2.21500000e+01,
+        2.22000000e+01,   2.22500000e+01,   2.23000000e+01,   2.23500000e+01,
+        2.24000000e+01,   2.24500000e+01,   2.25000000e+01])
+
+        #print 'Diff', time[1:] - time[:-1] 
+
+        #Setup mesh used to represent fitted function
+        a = [0.0, 0.0]
+        b = [0.0, 2.0]
+        c = [2.0, 0.0]
+        d = [0.0, 4.0]
+        e = [2.0, 2.0]
+        f = [4.0, 0.0]
+
+        points = [a, b, c, d, e, f]
+        #bac, bce, ecf, dbe
+        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
+
+
+        #New datapoints where interpolated values are sought
+        interpolation_points = [[ 0.0, 0.0],
+                                [ 0.5, 0.5],
+                                [ 0.7, 0.7],
+                                [ 1.0, 0.5],
+                                [ 2.0, 0.4],
+                                [ 545354534, 4354354353]] # outside the mesh
+
+        #One quantity
+        Q = num.zeros( (len(time),6), num.float )
+
+        #Linear in time and space
+        for i, t in enumerate(time):
+            Q[i, :] = t*linear_function(points)
+
+        #Check interpolation of one quantity using interpolaton points)
+        try:
+            I = Interpolation_function(time, Q,
+                                       vertex_coordinates = points,
+                                       triangles = triangles, 
+                                       interpolation_points = interpolation_points,
+                                       verbose = False)
+        except:
+            pass
+        else:
+            raise Exception('Should raise exception due to time being non-monotoneous')
+      
+
+    def test_points_outside_the_polygon(self):
+        a = [-1.0, 0.0]
+        b = [3.0, 4.0]
+        c = [4.0, 1.0]
+        d = [-3.0, 2.0] #3
+        e = [-1.0, -2.0]
+        f = [1.0, -2.0] #5
+
+        vertices = [a, b, c, d,e,f]
+        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
+
+        point_coords = [[-2.0, 2.0],
+                        [-1.0, 1.0],
+                        [9999.0, 9999.0], # point Outside poly
+                        [-9999.0, 1.0], # point Outside poly
+                        [2.0, 1.0],
+                        [0.0, 0.0],
+                        [1.0, 0.0],
+                        [0.0, -1.0],
+                        [-0.2, -0.5],
+                        [-0.9, -1.5],
+                        [0.5, -1.9],
+                        [999999, 9999999]] # point Outside poly
+        geo_data = Geospatial_data(data_points = point_coords)
+
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices),2*linear_function(vertices)])
+        f = num.transpose(f)
+        #print "f",f
+        z = interp.interpolate(f, geo_data)
+        #z = interp.interpolate(f, point_coords)
+        answer = [linear_function(point_coords),
+                  2*linear_function(point_coords) ]
+        answer = num.transpose(answer)
+        answer[2,:] = [NAN, NAN]
+        answer[3,:] = [NAN, NAN]
+        answer[11,:] = [NAN, NAN]
+        #print "z",z
+        #print "answer _ fixed",answer
+        assert num.allclose(z[0:1], answer[0:1])
+        assert num.allclose(z[4:10], answer[4:10])
+        for i in [2,3,11]:
+            self.failUnless( z[i,1] == answer[11,1], 'Fail!')
+            self.failUnless( z[i,0] == answer[11,0], 'Fail!')
+
+
+
+
+    def test_points_in_hole(self):
+
+        v0  = [0.0,     0.0]
+        v1  = [1.0/3.0, 0.0]
+        v2  = [2.0/3.0, 0.0]
+        v3  = [1.0,     0.0]
+        v4  = [0.0,     1.0/3.0]
+        v5  = [1.0/3.0, 1.0/3.0]
+        v6  = [2.0/3.0, 1.0/3.0]
+        v7  = [1.0,     1.0/3.0]
+        v8  = [0.0,     2.0/3.0]
+        v9  = [1.0/3.0, 2.0/3.0]
+        v10 = [2.0/3.0, 2.0/3.0]
+        v11 = [1.0,     2.0/3.0]
+        v12 = [0.0,     1.0]
+        v13 = [1.0/3.0, 1.0]
+        v14 = [2.0/3.0, 1.0]
+        v15 = [1.0,     1.0]
+
+
+        vertices = [v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15]
+
+        triangles = [[1,4,0], [1,5,4],    [2,5,1], [2,6,5],       [3,6,2],  [3,7,6],
+                     [5,8,4], [5,9,8],                            [7,10,6], [7,11,10],
+                     [9,12,8], [9,13,12], [10,13,9], [10,14,13], [11,14,10], [11,15,14]]
+
+
+
+
+        point_coords = [[0.25, 0.25],
+                        [0.25, 0.2],
+                        [10.0, 12.0], # point Outside poly
+                        [0.5, 0.5], # point in hole
+                        [0.5, 0.4], # point in hole
+                        [0.0, 0.0],
+                        [1.0, 0.0]]
+
+        geo_data = Geospatial_data(data_points = point_coords)
+
+        interp = Interpolate(vertices, triangles)
+        f = num.array([linear_function(vertices),2*linear_function(vertices)])
+        f = num.transpose(f)
+        #print "f",f
+        z = interp.interpolate(f, geo_data)
+        #z = interp.interpolate(f, point_coords)
+        answer = [linear_function(point_coords),
+                  2*linear_function(point_coords) ]
+        answer = num.transpose(answer)
+        answer[2,:] = [NAN, NAN]
+        answer[3,:] = [NAN, NAN]
+        answer[4,:] = [NAN, NAN]
+        #print "z",z
+        #print "answer _ fixed",answer
+        assert num.allclose(z[0:1], answer[0:1])
+        assert num.allclose(z[5:6], answer[5:6])
+        for i in [2,3,4]:
+            self.failUnless( z[i,1] == answer[2,1], 'Fail!')
+            self.failUnless( z[i,0] == answer[2,0], 'Fail!')
+
+
+    def test_interpolate_sww2csv(self):
+
+        def elevation_function(x, y):
+            return -x
+        
+        # Create mesh
+        mesh_file = tempfile.mktemp(".tsh")    
+        points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]]
+        m = Mesh()
+        m.add_vertices(points)
+        m.auto_segment()
+        m.generate_mesh(verbose=False)
+        m.export_mesh_file(mesh_file)
+        
+        # Create shallow water domain
+        domain = Domain(mesh_file)
+        os.remove(mesh_file)
+        
+        domain.default_order = 2
+
+        # This test was made before tight_slope_limiters were introduced
+        # Since were are testing interpolation values this is OK
+        domain.tight_slope_limiters = 0 
+
+        # Set some field values
+        domain.set_quantity('elevation', elevation_function)
+        domain.set_quantity('friction', 0.03)
+        domain.set_quantity('xmomentum', 3.0)
+        domain.set_quantity('ymomentum', 4.0)
+
+        ######################
+        # Boundary conditions
+        B = anuga.Transmissive_boundary(domain)
+        domain.set_boundary( {'exterior': B})
+
+        # This call mangles the stage values.
+        domain.distribute_to_vertices_and_edges()
+        domain.set_quantity('stage', 1.0)
+
+
+        domain.set_name('datatest' + str(time.time()))
+        domain.smooth = True
+        domain.reduction = mean
+
+        sww = SWW_file(domain)
+        sww.store_connectivity()
+        sww.store_timestep()
+        domain.set_quantity('stage', 10.0) # This is automatically limited
+        # So it will not be less than the elevation
+        domain.time = 2.
+        sww.store_timestep()
+
+        # Test the function
+        points = [[5.0,1.],[0.5,2.]]
+        depth_file = tempfile.mktemp(".csv") 
+        velocity_x_file = tempfile.mktemp(".csv") 
+        velocity_y_file = tempfile.mktemp(".csv") 
+        interpolate_sww2csv(sww.filename, points, depth_file,
+                            velocity_x_file,
+                            velocity_y_file,
+                            verbose=False)
+
+        depth_answers_array = [[0.0, 6.0, 1.5], [2.0, 15., 10.5]] 
+        velocity_x_answers_array = [[0.0, 3./6.0, 3./1.5],
+                                    [2.0, 3./15., 3/10.5]]
+        velocity_y_answers_array = [[0.0, 4./6.0, 4./1.5],
+                                    [2.0, 4./15., 4./10.5]]
+        depth_file_handle = file(depth_file)
+        depth_reader = csv.reader(depth_file_handle)
+        depth_reader.next()
+        velocity_x_file_handle = file(velocity_x_file)
+        velocity_x_reader = csv.reader(velocity_x_file_handle)
+        velocity_x_reader.next()
+        for depths, velocitys, depth_answers, velocity_answers in map(None,
+                                              depth_reader,
+                                              velocity_x_reader,
+                                              depth_answers_array,
+                                              velocity_x_answers_array):
+            for i in range(len(depths)):
+                #print "depths",depths[i] 
+                #print "depth_answers",depth_answers[i]
+                #print "velocitys",velocitys[i] 
+                #print "velocity_answers_array", velocity_answers[i]
+                msg = 'Interpolation failed'
+                assert num.allclose(float(depths[i]), depth_answers[i]), msg
+                assert num.allclose(float(velocitys[i]), velocity_answers[i]), msg
+
+        velocity_y_file_handle = file(velocity_y_file)
+        velocity_y_reader = csv.reader(velocity_y_file_handle)
+        velocity_y_reader.next()
+        for velocitys, velocity_answers in map(None,
+                                              velocity_y_reader,
+                                              velocity_y_answers_array):
+            for i in range(len(depths)):
+                #print "depths",depths[i] 
+                #print "depth_answers",depth_answers[i]
+                #print "velocitys",velocitys[i] 
+                #print "velocity_answers_array", velocity_answers[i]
+                msg = 'Interpolation failed'
+                assert num.allclose(float(depths[i]), depth_answers[i]), msg
+                assert num.allclose(float(velocitys[i]), velocity_answers[i]), msg
+                
+        # clean up
+        depth_file_handle.close()
+        velocity_y_file_handle.close()
+        velocity_x_file_handle.close()
+        #print "sww.filename",sww.filename 
+        os.remove(sww.filename)
+        os.remove(depth_file)
+        os.remove(velocity_x_file)
+        os.remove(velocity_y_file)
+
+        
+    def test_interpolate_one_point_many_triangles(self):
+        z0 = [2.0, 5.0]
+
+        v0 = [0.0, 0.0]
+        v1 = [1.0, 0.0]
+        v2 = [2.0, 0.0]
+        v3 = [3.0, 0.0]
+        v4 = [4.0, 0.0]
+        v5 = [5.0, 0.0]
+        v6 = [6.0, 0.0]
+        v7 = [0.0, 10.0]
+        v8 = [1.0, 10.0]
+        v9 = [2.0, 10.0]
+        v10= [3.0, 10.0]
+        v11= [4.0, 10.0]
+        v12= [5.0, 10.0]
+        v13= [6.0, 10.0]
+        
+        vertices = [z0,v0, v1, v2, v3,v4 ,v5, v6, v7, v8, v9, v10, v11,
+                    v12, v13]
+        triangles = [
+                      [0,1,2],
+                      [0,2,3],
+                      [0,3,4],
+                      [0,4,5],
+                      [0,5,6],
+                      [0,6,7],
+                      [0,9,8],
+                      [0,10,9],
+                      [0,11,10],
+                      [0,12,11],
+                      [0,13,12],
+                      [0,14,13]
+                      ]
+
+        d0 = [1.0, 1.0]
+        d1 = [1.0, 2.0]
+        d2 = [3.0, 1.0]
+        point_coords = [ d0, d1, d2]
+        try:
+            interp = Interpolate(vertices, triangles)
+        except RuntimeError:
+            self.failUnless(0 ==1,  'quad fails with 14 verts at the same \
+            position. Should be able to handle any number.')
+        f = linear_function(vertices)
+        z = interp.interpolate(f, point_coords)
+        answer = linear_function(point_coords)
+
+        #print "z",z 
+        #print "answer",answer 
+        assert num.allclose(z, answer)
+
+################################################################################
+
+if __name__ == "__main__":
+    suite = unittest.makeSuite(Test_Interpolate,'test')
+    runner = unittest.TextTestRunner() #verbosity=1)
+    runner.run(suite)
+
Index: /trunk/anuga_core/source/anuga/fit_interpolate/test/test_interpolate2d.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test/test_interpolate2d.py	(revision 9479)
+++ /trunk/anuga_core/source/anuga/fit_interpolate/test/test_interpolate2d.py	(revision 9479)
@@ -0,0 +1,469 @@
+"""Test suite for interpolate2d.py
+
+Ole Nielsen, 2011
+"""
+
+__author__ = 'Ole Nielsen <ole.moller.nielsen@gmail.com>'
+__revision__ = '$Format:%H$'
+__date__ = '01/11/2011'
+__license__ = 'GPL v3'
+__copyright__ = 'Copyright 2012, Australia Indonesia Facility for '
+__copyright__ += 'Disaster Reduction'
+
+import unittest
+import numpy
+
+from interpolate2d import interpolate2d
+
+
+# Auxiliary functions for testing
+def nanallclose(x, y, rtol=1.0e-5, atol=1.0e-8):
+    """Numpy allclose function which allows NaN
+
+    Args:
+        * x, y: Either scalars or numpy arrays
+
+    Returns:
+        * True or False
+
+    Note:
+        Returns True if all non-nan elements pass.
+    """
+
+    xn = numpy.isnan(x)
+    yn = numpy.isnan(y)
+    if numpy.any(xn != yn):
+        # Presence of NaNs is not the same in x and y
+        return False
+
+    if numpy.all(xn):
+        # Everything is NaN.
+        # This will also take care of x and y being NaN scalars
+        return True
+
+    # Filter NaN's out
+    if numpy.any(xn):
+        x = x[-xn]
+        y = y[-yn]
+
+    # Compare non NaN's and return
+    return numpy.allclose(x, y, rtol=rtol, atol=atol)
+
+
+def axes2points(x, y):
+    """Generate all combinations of grid point coordinates from x and y axes
+
+    Args:
+        * x: x coordinates (array)
+        * y: y coordinates (array)
+
+    Returns:
+        * P: Nx2 array consisting of coordinates for all
+             grid points defined by x and y axes. The x coordinate
+             will vary the fastest to match the way 2D numpy
+             arrays are laid out by default ('C' order). That way,
+             the x and y coordinates will match a corresponding
+             2D array A when flattened (A.flat[:] or A.reshape(-1))
+
+    Note:
+        Example
+
+        x = [1, 2, 3]
+        y = [10, 20]
+
+        P = [[1, 10],
+             [2, 10],
+             [3, 10],
+             [1, 20],
+             [2, 20],
+             [3, 20]]
+    """
+
+    # Reverse y coordinates to have them start at bottom of array
+    y = numpy.flipud(y)
+
+    # Repeat x coordinates for each y (fastest varying)
+    X = numpy.kron(numpy.ones(len(y)), x)
+
+    # Repeat y coordinates for each x (slowest varying)
+    Y = numpy.kron(y, numpy.ones(len(x)))
+
+    # Check
+    N = len(X)
+    assert len(Y) == N
+
+    # Create Nx2 array of x and y coordinates
+    X = numpy.reshape(X, (N, 1))
+    Y = numpy.reshape(Y, (N, 1))
+    P = numpy.concatenate((X, Y), axis=1)
+
+    # Return
+    return P
+
+
+def linear_function(x, y):
+    """Auxiliary function for use with interpolation test
+    """
+
+    return x + y / 2.0
+
+
+class Test_interpolate(unittest.TestCase):
+
+    def test_linear_interpolation_basic(self):
+        """Interpolation library works for linear function - basic test
+        """
+
+        # Define pixel centers along each direction
+        x = [1.0, 2.0, 4.0]
+        y = [5.0, 9.0]
+
+        # Define ny by nx array with corresponding values
+        A = numpy.zeros((len(x), len(y)))
+
+        # Define values for each x, y pair as a linear function
+        for i in range(len(x)):
+            for j in range(len(y)):
+                A[i, j] = linear_function(x[i], y[j])
+
+        # Test first that original points are reproduced correctly
+        for i, xi in enumerate(x):
+            for j, eta in enumerate(y):
+                val = interpolate2d(x, y, A, [(xi, eta)], mode='linear')[0]
+                ref = linear_function(xi, eta)
+                assert numpy.allclose(val, ref, rtol=1e-12, atol=1e-12)
+
+        # Then test that genuinly interpolated points are correct
+        xis = numpy.linspace(x[0], x[-1], 10)
+        etas = numpy.linspace(y[0], y[-1], 10)
+        points = axes2points(xis, etas)
+
+        vals = interpolate2d(x, y, A, points, mode='linear')
+        refs = linear_function(points[:, 0], points[:, 1])
+        assert numpy.allclose(vals, refs, rtol=1e-12, atol=1e-12)
+
+    def test_constant_interpolation_basic(self):
+        """Interpolation library works for piecewise constant function
+        """
+
+        # Define pixel centers along each direction
+        x = numpy.array([1.0, 2.0, 4.0])
+        y = numpy.array([5.0, 9.0])
+
+        # Define ny by nx array with corresponding values
+        A = numpy.zeros((len(x), len(y)))
+
+        # Define values for each x, y pair as a linear function
+        for i in range(len(x)):
+            for j in range(len(y)):
+                A[i, j] = linear_function(x[i], y[j])
+
+        # Then test that interpolated points are always assigned value of
+        # closest neighbour
+        xis = numpy.linspace(x[0], x[-1], 10)
+        etas = numpy.linspace(y[0], y[-1], 10)
+        points = axes2points(xis, etas)
+
+        vals = interpolate2d(x, y, A, points, mode='constant')
+
+        # Find upper neighbours for each interpolation point
+        xi = points[:, 0]
+        eta = points[:, 1]
+        idx = numpy.searchsorted(x, xi, side='left')
+        idy = numpy.searchsorted(y, eta, side='left')
+
+        # Get the four neighbours for each interpolation point
+        x0 = x[idx - 1]
+        x1 = x[idx]
+        y0 = y[idy - 1]
+        y1 = y[idy]
+
+        z00 = A[idx - 1, idy - 1]
+        z01 = A[idx - 1, idy]
+        z10 = A[idx, idy - 1]
+        z11 = A[idx, idy]
+
+        # Location coefficients
+        alpha = (xi - x0) / (x1 - x0)
+        beta = (eta - y0) / (y1 - y0)
+
+        refs = numpy.zeros(len(vals))
+        for i in range(len(refs)):
+            if alpha[i] < 0.5 and beta[i] < 0.5:
+                refs[i] = z00[i]
+
+            if alpha[i] >= 0.5 and beta[i] < 0.5:
+                refs[i] = z10[i]
+
+            if alpha[i] < 0.5 and beta[i] >= 0.5:
+                refs[i] = z01[i]
+
+            if alpha[i] >= 0.5 and beta[i] >= 0.5:
+                refs[i] = z11[i]
+
+        assert numpy.allclose(vals, refs, rtol=1e-12, atol=1e-12)
+
+    def test_linear_interpolation_range(self):
+        """Interpolation library works for linear function - a range of cases
+        """
+
+        for x in [[1.0, 2.0, 4.0], [-20, -19, 0], numpy.arange(200) + 1000]:
+            for y in [[5.0, 9.0], [100, 200, 10000]]:
+
+                # Define ny by nx array with corresponding values
+                A = numpy.zeros((len(x), len(y)))
+
+                # Define values for each x, y pair as a linear function
+                for i in range(len(x)):
+                    for j in range(len(y)):
+                        A[i, j] = linear_function(x[i], y[j])
+
+                # Test that linearly interpolated points are correct
+                xis = numpy.linspace(x[0], x[-1], 100)
+                etas = numpy.linspace(y[0], y[-1], 100)
+                points = axes2points(xis, etas)
+
+                vals = interpolate2d(x, y, A, points, mode='linear')
+                refs = linear_function(points[:, 0], points[:, 1])
+                assert numpy.allclose(vals, refs, rtol=1e-12, atol=1e-12)
+
+    def test_linear_interpolation_nan_points(self):
+        """Interpolation library works with interpolation points being NaN
+
+        This is was the reason for bug reported in:
+        https://github.com/AIFDR/riab/issues/155
+        """
+
+        # Define pixel centers along each direction
+        x = [1.0, 2.0, 4.0]
+        y = [5.0, 9.0]
+
+        # Define ny by nx array with corresponding values
+        A = numpy.zeros((len(x), len(y)))
+
+        # Define values for each x, y pair as a linear function
+        for i in range(len(x)):
+            for j in range(len(y)):
+                A[i, j] = linear_function(x[i], y[j])
+
+        # Then test that interpolated points can contain NaN
+        xis = numpy.linspace(x[0], x[-1], 10)
+        etas = numpy.linspace(y[0], y[-1], 10)
+        
+        xis[6:7] = numpy.nan
+        etas[3] = numpy.nan
+        points = axes2points(xis, etas)
+        
+        vals = interpolate2d(x, y, A, points, mode='linear')
+        refs = linear_function(points[:, 0], points[:, 1])
+        
+        assert nanallclose(vals, refs, rtol=1e-12, atol=1e-12)
+
+    def test_linear_interpolation_nan_array(self):
+        """Interpolation library works (linear mode) with grid points being NaN
+        """
+
+        # Define pixel centers along each direction
+        x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
+        y = [4.0, 5.0, 7.0, 9.0, 11.0, 13.0]
+
+        # Define ny by nx array with corresponding values
+        A = numpy.zeros((len(x), len(y)))
+
+        # Define values for each x, y pair as a linear function
+        for i in range(len(x)):
+            for j in range(len(y)):
+                A[i, j] = linear_function(x[i], y[j])
+        A[2, 3] = numpy.nan  # (x=2.0, y=9.0): NaN
+
+        # Then test that interpolated points can contain NaN
+        xis = numpy.linspace(x[0], x[-1], 12)
+        etas = numpy.linspace(y[0], y[-1], 10)
+        points = axes2points(xis, etas)
+
+
+        vals = interpolate2d(x, y, A, points, mode='linear')
+        refs = linear_function(points[:, 0], points[:, 1])
+
+        # Set reference result with expected NaNs and compare
+        for i, (xi, eta) in enumerate(points):
+            if (1.0 < xi <= 3.0) and (7.0 < eta <= 11.0):
+                refs[i] = numpy.nan
+
+        assert nanallclose(vals, refs, rtol=1e-12, atol=1e-12)
+
+    def test_interpolation_random_array_and_nan(self):
+        """Interpolation library (constant and linear) works with NaN
+        """
+
+        # Define pixel centers along each direction
+        x = numpy.arange(20) * 1.0
+        y = numpy.arange(25) * 1.0
+
+        # Define ny by nx array with corresponding values
+        A = numpy.zeros((len(x), len(y)))
+
+        # Define arbitrary values for each x, y pair
+        numpy.random.seed(17)
+        A = numpy.random.random((len(x), len(y))) * 10
+
+        # Create islands of NaN
+        A[5, 13] = numpy.nan
+        A[6, 14] = A[6, 18] = numpy.nan
+        A[7, 14:18] = numpy.nan
+        A[8, 13:18] = numpy.nan
+        A[9, 12:19] = numpy.nan
+        A[10, 14:17] = numpy.nan
+        A[11, 15] = numpy.nan
+
+        A[15, 5:6] = numpy.nan
+
+        # Creat interpolation points
+        xis = numpy.linspace(x[0], x[-1], 39)   # Hit all mid points
+        etas = numpy.linspace(y[0], y[-1], 73)  # Hit thirds
+        points = axes2points(xis, etas)
+
+        for mode in ['linear', 'constant']:
+            vals = interpolate2d(x, y, A, points, mode=mode)
+
+            # Calculate reference result with expected NaNs and compare
+            i = j = 0
+            for k, (xi, eta) in enumerate(points):
+
+                # Find indices of nearest higher value in x and y
+                i = numpy.searchsorted(x, xi)
+                j = numpy.searchsorted(y, eta)
+
+                if i > 0 and j > 0:
+
+                    # Get four neigbours
+                    A00 = A[i - 1, j - 1]
+                    A01 = A[i - 1, j]
+                    A10 = A[i, j - 1]
+                    A11 = A[i, j]
+
+                    if numpy.allclose(xi, x[i]):
+                        alpha = 1.0
+                    else:
+                        alpha = 0.5
+
+                    if numpy.allclose(eta, y[j]):
+                        beta = 1.0
+                    else:
+                        beta = eta - y[j - 1]
+
+                    if mode == 'linear':
+                        if numpy.any(numpy.isnan([A00, A01, A10, A11])):
+                            ref = numpy.nan
+                        else:
+                            ref = (A00 * (1 - alpha) * (1 - beta) +
+                                   A01 * (1 - alpha) * beta +
+                                   A10 * alpha * (1 - beta) +
+                                   A11 * alpha * beta)
+                    elif mode == 'constant':
+                        assert alpha >= 0.5  # Only case in this test
+
+                        if beta < 0.5:
+                            ref = A10
+                        else:
+                            ref = A11
+                    else:
+                        msg = 'Unknown mode: %s' % mode
+                        raise Exception(msg)
+
+                    #print i, j, xi, eta, alpha, beta, vals[k], ref
+                    assert nanallclose(vals[k], ref, rtol=1e-12, atol=1e-12)
+
+    def test_linear_interpolation_outside_domain(self):
+        """Interpolation library sensibly handles values outside the domain
+        """
+
+        # Define pixel centers along each direction
+        x = [1.0, 2.0, 4.0]
+        y = [5.0, 9.0]
+
+        # Define ny by nx array with corresponding values
+        A = numpy.zeros((len(x), len(y)))
+
+        # Define values for each x, y pair as a linear function
+        for i in range(len(x)):
+            for j in range(len(y)):
+                A[i, j] = linear_function(x[i], y[j])
+
+        # Simple example first for debugging
+        xis = numpy.linspace(0.9, 4.0, 4)
+        etas = numpy.linspace(5, 9.1, 3)
+        points = axes2points(xis, etas)
+        refs = linear_function(points[:, 0], points[:, 1])
+
+        vals = interpolate2d(x, y, A, points, mode='linear',
+                             bounds_error=False)
+        msg = ('Length of interpolation points %i differs from length '
+               'of interpolated values %i' % (len(points), len(vals)))
+        assert len(points) == len(vals), msg
+        for i, (xi, eta) in enumerate(points):
+            if xi < x[0] or xi > x[-1] or eta < y[0] or eta > y[-1]:
+                assert numpy.isnan(vals[i])
+            else:
+                msg = ('Got %.15f for (%f, %f), expected %.15f'
+                       % (vals[i], xi, eta, refs[i]))
+                assert numpy.allclose(vals[i], refs[i],
+                                      rtol=1.0e-12, atol=1.0e-12), msg
+
+        # Try a range of combinations of points outside domain
+        # with error_bounds True
+        for lox in [x[0], x[0] - 1]:
+            for hix in [x[-1], x[-1] + 1]:
+                for loy in [y[0], y[0] - 1]:
+                    for hiy in [y[-1], y[-1] + 1]:
+
+                        # Then test that points outside domain can be handled
+                        xis = numpy.linspace(lox, hix, 4)
+                        etas = numpy.linspace(loy, hiy, 4)
+                        points = axes2points(xis, etas)
+
+                        if lox < x[0] or hix > x[-1] or \
+                                loy < x[0] or hiy > y[-1]:
+                            try:
+                                vals = interpolate2d(x, y, A, points,
+                                                     mode='linear',
+                                                     bounds_error=True)
+                            except Exception, e:
+                                pass
+                            else:
+                                msg = 'Should have raise bounds error'
+                                raise Exception(msg)
+
+        # Try a range of combinations of points outside domain with
+        # error_bounds False
+        for lox in [x[0], x[0] - 1, x[0] - 10]:
+            for hix in [x[-1], x[-1] + 1, x[-1] + 5]:
+                for loy in [y[0], y[0] - 1, y[0] - 10]:
+                    for hiy in [y[-1], y[-1] + 1, y[-1] + 10]:
+
+                        # Then test that points outside domain can be handled
+                        xis = numpy.linspace(lox, hix, 10)
+                        etas = numpy.linspace(loy, hiy, 10)
+                        points = axes2points(xis, etas)
+                        refs = linear_function(points[:, 0], points[:, 1])
+                        vals = interpolate2d(x, y, A, points,
+                                             mode='linear', bounds_error=False)
+
+                        assert len(points) == len(vals), msg
+                        for i, (xi, eta) in enumerate(points):
+                            if xi < x[0] or xi > x[-1] or\
+                                    eta < y[0] or eta > y[-1]:
+                                msg = 'Expected NaN for %f, %f' % (xi, eta)
+                                assert numpy.isnan(vals[i]), msg
+                            else:
+                                msg = ('Got %.15f for (%f, %f), expected '
+                                       '%.15f' % (vals[i], xi, eta, refs[i]))
+                                assert numpy.allclose(vals[i], refs[i],
+                                                      rtol=1.0e-12,
+                                                      atol=1.0e-12), msg
+
+
+if __name__ == '__main__':
+    suite = unittest.makeSuite(Test_interpolate, 'test')
+    runner = unittest.TextTestRunner(verbosity=2)
+    runner.run(suite)
Index: /trunk/anuga_core/source/anuga/fit_interpolate/test/test_search_functions.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test/test_search_functions.py	(revision 9479)
+++ /trunk/anuga_core/source/anuga/fit_interpolate/test/test_search_functions.py	(revision 9479)
@@ -0,0 +1,236 @@
+#!/usr/bin/env python
+
+
+import unittest
+from anuga.pmesh.mesh_quadtree import MeshQuadtree
+
+from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
+from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular
+from anuga.geometry.quad import Cell
+from anuga.geometry.aabb import AABB
+from anuga.utilities.numerical_tools import ensure_numeric
+from anuga.geometry.polygon import is_inside_polygon, is_inside_triangle    
+
+import numpy as num
+
+class Test_search_functions(unittest.TestCase):
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    def NOtest_that_C_extension_compiles(self):
+        FN = 'search_functions_ext.c'
+        try:
+            import search_functions_ext
+        except:
+            from compile import compile
+
+            try:
+                compile(FN)
+            except:
+                raise Exception('Could not compile %s' % FN)
+            else:
+                import search_functions_ext
+
+
+
+    def test_off_and_boundary(self):
+        """test_off: Test a point off the mesh
+        """
+
+        points, vertices, boundary = rectangular(1, 1, 1, 1)
+        mesh = Mesh(points, vertices, boundary)
+
+        #Test that points are arranged in a counter clock wise order
+        mesh.check_integrity()
+
+        root = MeshQuadtree(mesh)
+        root.set_last_triangle()
+
+        found, s0, s1, s2, k = root.search_fast([-0.2, 10.7])
+        assert found is False
+
+        found, s0, s1, s2, k = root.search_fast([0, 0])
+        assert found is True
+        
+        
+    def test_small(self):
+        """test_small: Two triangles
+        """
+
+        points, vertices, boundary = rectangular(1, 1, 1, 1)
+        mesh = Mesh(points, vertices, boundary)
+
+        #Test that points are arranged in a counter clock wise order
+        mesh.check_integrity()
+
+        root = MeshQuadtree(mesh)
+        root.set_last_triangle()
+
+        x = [0.2, 0.7]
+        found, s0, s1, s2, k = root.search_fast(x)
+        assert k == 1 # Triangle one
+        assert found is True        
+        
+    def test_bigger(self):
+        """test_bigger
+        
+        test larger mesh
+        """
+
+        points, vertices, boundary = rectangular(4, 4, 1, 1)
+        mesh = Mesh(points, vertices, boundary)
+
+        #Test that points are arranged in a counter clock wise order
+        mesh.check_integrity()
+
+        root = MeshQuadtree(mesh)
+        root.set_last_triangle()
+
+        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
+                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
+                  [10, 3]]:
+            
+            found, s0, s1, s2, k = root.search_fast(ensure_numeric(x))                                   
+                                         
+            if k >= 0:
+                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
+                assert is_inside_polygon(x, V)
+                assert found is True
+                #print k, x
+            else:
+                assert found is False                
+
+        
+
+    def test_large(self):
+        """test_larger mesh and different quad trees
+        """
+
+        points, vertices, boundary = rectangular(10, 12, 1, 1)
+        mesh = Mesh(points, vertices, boundary)
+
+        #Test that points are arranged in a counter clock wise order
+        mesh.check_integrity()
+
+        
+
+        root = MeshQuadtree(mesh)
+        root.set_last_triangle()
+        #print m, root.show()
+
+        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
+                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
+                  [10, 3]]:
+            
+            found, s0, s1, s2, k = root.search_fast(x)
+
+            if k >= 0:
+                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
+                assert is_inside_triangle(x, V, closed=True)
+                assert is_inside_polygon(x, V)
+                assert found is True
+            else:
+                assert found is False                
+
+        
+            if k == 0: return    
+    # NOTE PADARN: This function is no longer exposed
+    # have passed this test - but could expose
+    # c function if deemed neccesary.
+    def test_underlying_function(self):
+        """test_larger mesh and different quad trees
+        """
+        return
+        points, vertices, boundary = rectangular(2, 2, 1, 1)
+        mesh = Mesh(points, vertices, boundary)
+
+        root = MeshQuadtree(mesh)
+        root.set_last_triangle()
+
+        # One point
+        x = ensure_numeric([0.5, 0.5])
+
+        found, sigma0, sigma1, sigma2, k = \
+               root._search_triangles_of_vertices(root.search(x), x)
+
+        if k >= 0:
+            V = mesh.get_vertex_coordinates(k) # nodes for triangle k
+            assert is_inside_polygon(x, V)
+            assert found is True
+        else:
+            assert found is False                
+
+        
+
+        # More points    
+        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
+                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
+                  [10, 3]]:
+                
+            triangles = root.search(x)
+
+            #print x, candidate_vertices
+            found, sigma0, sigma1, sigma2, k = \
+                   root._search_triangles_of_vertices(triangles,
+                                                 ensure_numeric(x))
+            if k >= 0:
+                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
+                assert is_inside_polygon(x, V)
+                assert found is True
+            else:
+                assert found is False
+
+                
+
+    def expanding_search(self):
+        """test_larger mesh and different quad trees
+        """
+        
+        p0 = [2,1]
+        p1 = [4,1]
+        p2 = [4.,4]
+        p3 = [2,4]
+        p4 = [5,4]
+
+        p5 = [-1,-1]
+        p6 = [1,-1]
+        p7 = [1,1]
+        p8 = [-1,1]
+
+        points = [p0,p1,p2, p3,p4,p5,p6,p7,p8]
+        #
+        vertices = [[0,1,2],[0,2,3],[1,4,2],[5,6,7], [5,7,8]]
+        mesh = Mesh(points, vertices)
+
+        # Don't do this, want to control the max and mins
+        #root = build_quadtree(mesh, max_points_per_cell=4)
+    
+
+        root = Cell(-3, 9, -3, 9,
+                    max_points_per_cell = 4)
+        #Insert indices of all vertices
+        root.insert( range(mesh.number_of_nodes) )
+
+        #Build quad tree and return
+        root.split()
+        
+        # One point
+        #x = [3.5, 1.5]
+        x = [2.5, 1.5]
+        element_found, sigma0, sigma1, sigma2, k = root.search_fast(x)
+        # One point
+        x = [3.00005, 2.999994]
+        element_found, sigma0, sigma1, sigma2, k = root.search_fast(x)
+        assert element_found is True
+        assert k == 1
+        
+
+#-------------------------------------------------------------
+if __name__ == "__main__":
+    suite = unittest.makeSuite(Test_search_functions, 'test')
+    runner = unittest.TextTestRunner(verbosity=1)
+    runner.run(suite)
+    
Index: unk/anuga_core/source/anuga/fit_interpolate/test_all.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test_all.py	(revision 9478)
+++ 	(revision )
@@ -1,83 +1,0 @@
-"""Regression testing framework
-This module will search for scripts in the same directory named
-test_*.py.  Each such script should be a test suite that tests a
-module through PyUnit. This script will aggregate all
-found test suites into one big test suite and run them all at once.
-"""
-
-# Author: Mark Pilgrim
-# Modified by Ole Nielsen
-
-import unittest
-import os
-
-
-#List files that should be excluded from the testing process.
-#E.g. if they are known to fail and under development
-#exclude = ['test_interpolate_sww.py'] 
-
-
-def get_test_files(path):
-
-    import sys
-
-    files = os.listdir(path)
-
-    #Check sub directories
-    test_files = []
-    for file in files:
-        if os.path.isdir(file):
-            sys.path.append(file)
-            #print 'Recursing into', file
-            test_files += get_test_files(path + os.sep + file)
-        elif file[:5] == 'test_' and file[-2:] == 'py':
-            #print 'Appending', file
-            test_files.append(file)
-        else:
-            pass
-    return test_files
-
-
-
-def regressionTest():
-    import sys, os, re, unittest
-    path = os.path.split(sys.argv[0])[0] or os.getcwd()
-
-
-    files = get_test_files(path)
-
-
-
-    #test = re.compile('^test_[\w]*.py$', re.IGNORECASE)
-    #files = filter(test.search, files)
-
-
-    try:
-        files.remove(__file__)  #Remove self from list (Ver 2.3. or later)
-    except:
-        files.remove('test_all.py')
-
-    print 'Testing:'
-    for file in files:
-        print '  ' + file
-
-    if globals().has_key('exclude'):
-        for file in exclude:
-            files.remove(file)
-            print 'WARNING: File '+ file + ' excluded from testing'
-
-
-    filenameToModuleName = lambda f: os.path.splitext(f)[0]
-    #print "files",files 
-    moduleNames = map(filenameToModuleName, files)
-    #print "moduleNames",moduleNames
-    modules = map(__import__, moduleNames)
-    load = unittest.defaultTestLoader.loadTestsFromModule
-    return unittest.TestSuite(map(load, modules))
-
-################################################################################
-
-if __name__ == '__main__':
-    suite = regressionTest()
-    runner = unittest.TextTestRunner() #(verbosity=2)
-    runner.run(suite)
Index: unk/anuga_core/source/anuga/fit_interpolate/test_fit.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test_fit.py	(revision 9478)
+++ 	(revision )
@@ -1,1227 +1,0 @@
-#!/usr/bin/env python
-
-"""
-Note, fitting/blocking is also tested in
-test_quantity.test_set_values_from_UTM_pts.
-"""
-
-#TEST
-import sys
-import unittest
-from math import sqrt
-import tempfile
-import os
-
-from fit import *
-from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
-from anuga.utilities.sparse import Sparse, Sparse_CSR
-from anuga.coordinate_transforms.geo_reference import Geo_reference
-from anuga.utilities.numerical_tools import ensure_numeric
-from anuga.geospatial_data.geospatial_data import Geospatial_data
-from anuga.shallow_water.shallow_water_domain import Domain
-
-import numpy as num
-
-
-def distance(x, y):
-    return sqrt(num.sum((num.array(x)-num.array(y))**2))
-
-def linear_function(point):
-    point = num.array(point)
-    return point[:,0]+point[:,1]
-
-
-class Test_Fit(unittest.TestCase):
-
-    def setUp(self):
-        pass
-
-    def tearDown(self):
-        pass
-
-
-    def test_smooth_attributes_to_mesh(self):
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        points = [a, b, c]
-        triangles = [ [1,0,2] ]   #bac
-
-        d1 = [1.0, 1.0]
-        d2 = [1.0, 3.0]
-        d3 = [3.0,1.0]
-        z1 = 2
-        z2 = 4
-        z3 = 4
-        data_coords = [d1, d2, d3]
-
-        z = [z1, z2, z3]
-        fit = Fit(points, triangles, alpha=0)
-        #print "interp.get_A()", interp.get_A()
-        fit._build_matrix_AtA_Atz(ensure_numeric(data_coords),
-                                  ensure_numeric(z))
-        #print "Atz - from fit", fit.Atz
-        #print "AtA - from fit", fit.AtA.todense()
-        #print "z",z 
-
-        assert num.allclose(fit.Atz, [2.8, 3.6, 3.6], atol=1e-7)
-
-        f = fit.fit()
-        
-        answer = [0, 5., 5.]
-
-        #print "f\n",f
-        #print "answer\n",answer
-
-        assert num.allclose(f, answer, atol=1e-7)
-
-    def test_smooth_att_to_meshII(self):
-
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        points = [a, b, c]
-        triangles = [ [1,0,2] ]   #bac
-
-        d1 = [1.0, 1.0]
-        d2 = [1.0, 2.0]
-        d3 = [3.0,1.0]
-        data_coords = [d1, d2, d3]
-        z = linear_function(data_coords)
-        #print "z",z 
-
-        interp = Fit(points, triangles, alpha=0.0)
-        f = interp.fit(data_coords, z)
-        answer = linear_function(points)
-        #print "f\n",f
-        #print "answer\n",answer
-
-        assert num.allclose(f, answer)
-
-    def test_smooth_attributes_to_meshIII(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-        point_coords = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [0.0,2.0],
-                        [1.0, 1.0],
-                        [2.0, 1.0],
-                        [0.0,0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2,-0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [3.0,1.0]]
-
-        z = linear_function(point_coords)
-        interp = Fit(vertices, triangles,
-                                alpha=0.0)
-
-        #print 'z',z
-        f = interp.fit(point_coords,z)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-
-   
-    def test_smooth_attributes_to_meshIV(self):
-        # Testing 2 attributes smoothed to the mesh
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        points = [a, b, c]
-        triangles = [ [1,0,2] ]   #bac
-
-        d1 = [1.0, 1.0]
-        d2 = [1.0, 3.0]
-        d3 = [3.0, 1.0]
-        z1 = [2, 4]
-        z2 = [4, 8]
-        z3 = [4, 8]
-        data_coords = [d1, d2, d3]
-
-        z = [z1, z2, z3]
-        fit = Fit(points, triangles, alpha=0)
-        
-        f =  fit.fit(data_coords,z)
-        answer = [[0,0], [5., 10.], [5., 10.]]
-        assert num.allclose(f, answer)
-
-    def test_smooth_attributes_to_mesh_build_fit_subset(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-        interp = Fit(vertices, triangles,
-                                alpha=0.0)
-
-        point_coords = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [0.0,2.0],
-                        [1.0, 1.0],
-                       ]
-
-        z = linear_function(point_coords)
-
-        f = interp.build_fit_subset(point_coords,z)
-        
-        point_coords = [
-                        [2.0, 1.0],
-                        [0.0,0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2,-0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [3.0,1.0]]
-        
-        z = linear_function(point_coords)
-
-        f = interp.build_fit_subset(point_coords,z)
-        
-        #print 'z',z
-        f = interp.fit()
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-
-        # test fit 2 mesh as well.
-    def test_fit_file_blocking(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-        interp = Fit(vertices, triangles,
-                     alpha=0.0)
-
-
-        fileName = tempfile.mktemp(".ddd")
-        file = open(fileName,"w")
-        file.write(" x,y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        
-        f = interp.fit(fileName, max_read_lines=2)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-        os.remove(fileName)
-
-    def test_fit_to_mesh_UTM_file(self):
-        #Get (enough) datapoints
-        data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65],
-                       [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]]
-
-        data_geo_spatial = Geospatial_data(data_points,
-                                           points_are_lats_longs=True)
-        points_UTM = data_geo_spatial.get_data_points(absolute=True)
-        attributes = linear_function(points_UTM)
-        att = 'elevation'
-        
-        #Create .txt file
-        txt_file = tempfile.mktemp(".txt")
-        file = open(txt_file,"w")
-        file.write(" x,y," + att + " \n")
-        for data_point, attribute in map(None, points_UTM, attributes):
-            row = str(data_point[0]) + ',' + str(data_point[1]) \
-                  + ',' + str(attribute)
-            #print "row", row 
-            file.write(row + "\n")
-        file.close()
-
-        # setting up the mesh
-        a = [240000, 7620000]
-        b = [240000, 7680000]
-        c = [300000, 7620000]
-        points = [a, b, c]
-        elements = [[0,2,1]]
-        f = fit_to_mesh(txt_file, points, elements, 
-                        alpha=0.0, max_read_lines=2)
-        answer = linear_function(points)
-        #print "f",f
-        #print "answer",answer 
-        assert num.allclose(f, answer)
-
-        # Delete file!
-        os.remove(txt_file)
-        
-    def cache_test_fit_to_mesh_pts(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".txt")
-        file = open(fileName,"w")
-        file.write(" x, y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        geo = Geospatial_data(fileName)
-        fileName_pts = tempfile.mktemp(".pts")
-        points = geo.get_data_points(absolute=True)
-        atts = geo.get_attributes()
-        f = fit_to_mesh(points,atts, vertices, triangles,
-                                alpha=0.0, max_read_lines=2,
-                        use_cache=True, verbose=True)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-        os.remove(fileName)
-       
-    def test_fit_to_mesh_pts(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".txt")
-        file = open(fileName,"w")
-        file.write(" x, y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        geo = Geospatial_data(fileName)
-        fileName_pts = tempfile.mktemp(".pts")
-        geo.export_points_file(fileName_pts)
-        f = fit_to_mesh(fileName_pts, vertices, triangles,
-                                alpha=0.0, max_read_lines=2)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-        os.remove(fileName)
-        os.remove(fileName_pts)
-        
-    def test_fit_to_mesh_pts_passing_mesh_in(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".txt")
-        file = open(fileName,"w")
-        file.write(" x, y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        geo = Geospatial_data(fileName)
-        fileName_pts = tempfile.mktemp(".pts")
-        geo.export_points_file(fileName_pts)
-        f = fit_to_mesh(fileName_pts, vertices, triangles,
-                                alpha=0.0, max_read_lines=2)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-        os.remove(fileName)
-        os.remove(fileName_pts)
-        
-    def test_fit_to_mesh(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".ddd")
-        file = open(fileName,"w")
-        file.write(" x,y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        
-        f = fit_to_mesh(fileName, vertices, triangles,
-                                alpha=0.0, max_read_lines=2)
-                        #use_cache=True, verbose=True)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-    
-        os.remove(fileName)
-
-    def test_fit_to_mesh_using_jacobi_precon(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".ddd")
-        file = open(fileName,"w")
-        file.write(" x,y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        f = fit_to_mesh(fileName, vertices, triangles,
-                                alpha=0.0, max_read_lines=2, use_c_cg=True, cg_precon='Jacobi')
-                        #use_cache=True, verbose=True)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-
-        assert num.allclose(f, answer)
-    
-        os.remove(fileName)
-
-    def test_fit_to_mesh_using_jacobi_precon_no_c_cg(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".ddd")
-        file = open(fileName,"w")
-        file.write(" x,y, elevation \n\
--2.0, 2.0, 0.\n\
--1.0, 1.0, 0.\n\
-0.0, 2.0 , 2.\n\
-1.0, 1.0 , 2.\n\
- 2.0,  1.0 ,3. \n\
- 0.0,  0.0 , 0.\n\
- 1.0,  0.0 , 1.\n\
- 0.0,  -1.0, -1.\n\
- -0.2, -0.5, -0.7\n\
- -0.9, -1.5, -2.4\n\
- 0.5,  -1.9, -1.4\n\
- 3.0,  1.0 , 4.\n")
-        file.close()
-        f = fit_to_mesh(fileName, vertices, triangles,
-                                alpha=0.0, max_read_lines=2, use_c_cg=False, cg_precon='Jacobi')
-                        #use_cache=True, verbose=True)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-
-        assert num.allclose(f, answer)
-    
-        os.remove(fileName)
-
-    def test_fit_to_mesh_2_atts(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        fileName = tempfile.mktemp(".ddd")
-        file = open(fileName,"w")
-        # the 2nd att name is wacky so it's the first key off a hash table
-        file.write(" x,y, elevation, afriqction \n\
--2.0, 2.0, 0., 0.\n\
--1.0, 1.0, 0., 0.\n\
-0.0, 2.0 , 2., 20.\n\
-1.0, 1.0 , 2., 20.\n\
- 2.0,  1.0 ,3., 30. \n\
- 0.0,  0.0 , 0., 0.\n\
- 1.0,  0.0 , 1., 10.\n\
- 0.0,  -1.0, -1., -10.\n\
- -0.2, -0.5, -0.7, -7.\n\
- -0.9, -1.5, -2.4, -24. \n\
- 0.5,  -1.9, -1.4, -14. \n\
- 3.0,  1.0 , 4., 40. \n")
-        file.close()
-        
-        f = fit_to_mesh(fileName, vertices, triangles,
-                        alpha=0.0, 
-                        attribute_name='elevation', max_read_lines=2)
-        answer = linear_function(vertices)
-        #print "f\n",f
-        #print "answer\n",answer
-        assert num.allclose(f, answer)
-        os.remove(fileName)
-
-
-
-
-        
-    def test_fit_and_interpolation(self):
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe, daf, dae
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-        #Get (enough) datapoints
-        data_points = [[ 0.66666667, 0.66666667],
-                       [ 1.33333333, 1.33333333],
-                       [ 2.66666667, 0.66666667],
-                       [ 0.66666667, 2.66666667],
-                       [ 0.0, 1.0],
-                       [ 0.0, 3.0],
-                       [ 1.0, 0.0],
-                       [ 1.0, 1.0],
-                       [ 1.0, 2.0],
-                       [ 1.0, 3.0],
-                       [ 2.0, 1.0],
-                       [ 3.0, 0.0],
-                       [ 3.0, 1.0]]
-
-        z = linear_function(data_points)
-        interp = Fit(points, triangles,
-                                alpha=0.0)
-
-        answer = linear_function(points)
-
-        f = interp.fit(data_points, z)
-
-        #print "f",f
-        #print "answer",answer
-        assert num.allclose(f, answer)
-
-    def test_fit_and_interpolation_using_jacobi_precon(self):
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe, daf, dae
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-        #Get (enough) datapoints
-        data_points = [[ 0.66666667, 0.66666667],
-                       [ 1.33333333, 1.33333333],
-                       [ 2.66666667, 0.66666667],
-                       [ 0.66666667, 2.66666667],
-                       [ 0.0, 1.0],
-                       [ 0.0, 3.0],
-                       [ 1.0, 0.0],
-                       [ 1.0, 1.0],
-                       [ 1.0, 2.0],
-                       [ 1.0, 3.0],
-                       [ 2.0, 1.0],
-                       [ 3.0, 0.0],
-                       [ 3.0, 1.0]]
-
-        z = linear_function(data_points)
-        interp = Fit(points, triangles,
-                                alpha=0.0,cg_precon='Jacobi')
-
-        answer = linear_function(points)
-
-        f = interp.fit(data_points, z)
-
-        #print "f",f
-        #print "answer",answer
-        assert num.allclose(f, answer,atol=5)
-
-        
-    def test_smoothing_and_interpolation(self):
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe, daf, dae
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-        #Get (too few!) datapoints
-        data_points = [[ 0.66666667, 0.66666667],
-                       [ 1.33333333, 1.33333333],
-                       [ 2.66666667, 0.66666667],
-                       [ 0.66666667, 2.66666667]]
-
-        z = linear_function(data_points)
-        answer = linear_function(points)
-
-        #Make interpolator with too few data points and no smoothing
-        interp = Fit(points, triangles, alpha=0.0)
-        #Must raise an exception
-        try:
-            f = interp.fit(data_points,z)
-        except TooFewPointsError:
-            pass
-
-        #Now try with smoothing parameter
-        interp = Fit(points, triangles, alpha=1.0e-13)
-
-        f = interp.fit(data_points,z)
-        #f will be different from answer due to smoothing
-        assert num.allclose(f, answer,atol=5)
-
-
-    #Tests of smoothing matrix
-    def test_smoothing_matrix_one_triangle(self):
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-
-        vertices = [ [1,0,2] ]   #bac
-
-        interp = Fit(points, vertices)
-
-        assert num.allclose(interp.get_D(), [[1, -0.5, -0.5],
-                                             [-0.5, 0.5, 0],
-                                             [-0.5, 0, 0.5]])
-
-        #Define f(x,y) = x
-        f = num.array([0,0,2]) #Value at global vertex 2
-
-        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
-        #           int 1 dx dy = area = 2
-        assert num.dot(num.dot(f, interp.get_D()), f) == 2
-
-        #Define f(x,y) = y
-        f = num.array([0,2,0])  #Value at global vertex 1
-
-        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
-        #           int 1 dx dy = area = 2
-        assert num.dot(num.dot(f, interp.get_D()), f) == 2
-
-        #Define f(x,y) = x+y
-        f = num.array([0,2,2])  #Values at global vertex 1 and 2
-
-        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
-        #           int 2 dx dy = 2*area = 4
-        assert num.dot(num.dot(f, interp.get_D()), f) == 4
-
-
-    def test_smoothing_matrix_more_triangles(self):
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0,0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe, daf, dae
-        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-        interp = Fit(points, vertices)
-
-
-        #assert allclose(interp.get_D(), [[1, -0.5, -0.5],
-        #                           [-0.5, 0.5, 0],
-        #                           [-0.5, 0, 0.5]])
-
-        #Define f(x,y) = x
-        f = num.array([0,0,2,0,2,4]) #f evaluated at points a-f
-
-        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
-        #           int 1 dx dy = total area = 8
-        assert num.dot(num.dot(f, interp.get_D()), f) == 8
-
-        #Define f(x,y) = y
-        f = num.array([0,2,0,4,2,0]) #f evaluated at points a-f
-
-        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
-        #           int 1 dx dy = area = 8
-        assert num.dot(num.dot(f, interp.get_D()), f) == 8
-
-        #Define f(x,y) = x+y
-        f = num.array([0,2,2,4,4,4])  #f evaluated at points a-f
-
-        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
-        #           int 2 dx dy = 2*area = 16
-        assert num.dot(num.dot(f, interp.get_D()), f) == 16
-
-
-
-    def test_fit_and_interpolation_with_different_origins(self):
-        """Fit a surface to one set of points. Then interpolate that surface
-        using another set of points.
-        This test tests situtaion where points and mesh belong to a different
-        coordinate system as defined by origin.
-        """
-
-        #Setup mesh used to represent fitted function
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe, daf, dae
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-        #Datapoints to fit from
-        data_points1 = [[ 0.66666667, 0.66666667],
-                        [ 1.33333333, 1.33333333],
-                        [ 2.66666667, 0.66666667],
-                        [ 0.66666667, 2.66666667],
-                        [ 0.0, 1.0],
-                        [ 0.0, 3.0],
-                        [ 1.0, 0.0],
-                        [ 1.0, 1.0],
-                        [ 1.0, 2.0],
-                        [ 1.0, 3.0],
-                        [ 2.0, 1.0],
-                        [ 3.0, 0.0],
-                        [ 3.0, 1.0]]
-
-
-        #First check that things are OK when using same origin
-        mesh_origin = (56, 290000, 618000) #zone, easting, northing
-        data_origin = (56, 290000, 618000) #zone, easting, northing
-
-
-        #Fit surface to mesh
-        interp = Fit(points, triangles, 
-                               alpha=0.0,
-                               mesh_origin = mesh_origin)
-
-        data_geo_spatial = Geospatial_data(data_points1,
-                         geo_reference = Geo_reference(56, 290000, 618000))
-        z = linear_function(data_points1) #Example z-values
-        f = interp.fit(data_geo_spatial, z)   #Fitted values at vertices
-
-        #Shift datapoints according to new origins
-        for k in range(len(data_points1)):
-            data_points1[k][0] += mesh_origin[1] - data_origin[1]
-            data_points1[k][1] += mesh_origin[2] - data_origin[2]
-
-
-
-        #Fit surface to mesh
-        interp = Fit(points, triangles, alpha=0.0)
-        #Fitted values at vertices (using same z as before)
-        f1 = interp.fit(data_points1,z) 
-
-        assert num.allclose(f,f1), 'Fit should have been unaltered'
-
-
-    def test_smooth_attributes_to_mesh_function(self):
-        #Testing 2 attributes smoothed to the mesh
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        points = [a, b, c]
-        triangles = [ [1,0,2] ]   #bac
-
-        d1 = [1.0, 1.0]
-        d2 = [1.0, 3.0]
-        d3 = [3.0, 1.0]
-        z1 = [2, 4]
-        z2 = [4, 8]
-        z3 = [4, 8]
-        data_coords = [d1, d2, d3]
-        z = [z1, z2, z3]
-
-        f = fit_to_mesh(data_coords, vertex_coordinates=points,
-                        triangles=triangles,
-                        point_attributes=z, alpha=0.0)
-        answer = [[0, 0], [5., 10.], [5., 10.]]
-
-        assert num.allclose(f, answer)
-
-    def test_fit_to_mesh_w_georef(self):
-        """Simple check that georef works at the fit_to_mesh level
-        """
-        
-        from anuga.coordinate_transforms.geo_reference import Geo_reference
-
-        #Mesh
-        vertex_coordinates = [[0.76, 0.76],
-                              [0.76, 5.76],
-                              [5.76, 0.76]]
-        triangles = [[0,2,1]]
-
-        mesh_geo = Geo_reference(56,-0.76,-0.76)
-        #print "mesh_geo.get_absolute(vertex_coordinates)", \
-         #     mesh_geo.get_absolute(vertex_coordinates)
-
-        #Data                       
-        data_points = [[ 201.0, 401.0],
-                       [ 201.0, 403.0],
-                       [ 203.0, 401.0]]
-
-        z = [2, 4, 4]
-        
-        data_geo = Geo_reference(56,-200,-400)
-
-        #print "data_geo.get_absolute(data_points)", \
-        #      data_geo.get_absolute(data_points)
-        
-        #Fit
-        zz = fit_to_mesh(data_points, vertex_coordinates=vertex_coordinates,
-                         triangles=triangles,
-                         point_attributes=z,
-                         data_origin = data_geo.get_origin(),
-                         mesh_origin = mesh_geo.get_origin(),
-                         alpha = 0)
-        assert num.allclose( zz, [0,5,5] )
-
-
-    def test_fit_to_mesh_file2domain(self):
-        from load_mesh.loadASCII import import_mesh_file, \
-             export_mesh_file
-        import tempfile
-        import os
-
-        # create a .tsh file, no user outline
-        mesh_dic = {}
-        mesh_dic['vertices'] = [[0.0, 0.0],
-                                          [0.0, 5.0],
-                                          [5.0, 0.0]]
-        mesh_dic['triangles'] =  [[0, 2, 1]]
-        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
-        mesh_dic['triangle_tags'] = ['']
-        mesh_dic['vertex_attributes'] = [[], [], []]
-        mesh_dic['vertiex_attribute_titles'] = []
-        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
-        mesh_dic['segment_tags'] = ['external',
-                                                  'external',
-                                                  'external']
-        mesh_file = tempfile.mktemp(".tsh")
-        export_mesh_file(mesh_file,mesh_dic)
-
-        # create a points .csv file
-        point_file = tempfile.mktemp(".csv")
-        fd = open(point_file,'w')
-        fd.write("x,y, elevation, stage \n\
-        1.0, 1.0,2.,4 \n\
-        1.0, 3.0,4,8 \n\
-        3.0,1.0,4.,8 \n")
-        fd.close()
-
-        mesh_output_file = tempfile.mktemp(".tsh") 
-        fit_to_mesh_file(mesh_file,
-                         point_file,
-                         mesh_output_file,
-                         alpha = 0.0)
-        # load in the .tsh file we just wrote
-        mesh_dic = import_mesh_file(mesh_output_file)
-        #print "mesh_dic",mesh_dic
-        ans =[[0.0, 0.0],
-              [5.0, 10.0],
-              [5.0,10.0]]
-        assert num.allclose(mesh_dic['vertex_attributes'],ans)
-
-        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
-                        ['elevation','stage'],
-                        'test_fit_to_mesh_file failed')
-        domain = Domain(mesh_output_file, use_cache=True, verbose=False)
-        
-        answer = [0., 5., 5.]
-        assert num.allclose(domain.quantities['elevation'].vertex_values,
-                            answer)
-        #clean up
-        os.remove(mesh_file)
-        os.remove(point_file)
-        os.remove(mesh_output_file)
-
-    def test_fit_to_mesh_file3(self):
-        from load_mesh.loadASCII import import_mesh_file, \
-             export_mesh_file
-        import tempfile
-        import os
-
-        # create a .tsh file, no user outline
-        mesh_dic = {}
-        mesh_dic['vertices'] = [[0.76, 0.76],
-                                          [0.76, 5.76],
-                                          [5.76, 0.76]]
-        mesh_dic['triangles'] =  [[0, 2, 1]]
-        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
-        mesh_dic['triangle_tags'] = ['']
-        mesh_dic['vertex_attributes'] = [[], [], []]
-        mesh_dic['vertiex_attribute_titles'] = []
-        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
-        mesh_dic['segment_tags'] = ['external',
-                                                  'external',
-                                                  'external']
-        mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)
-        mesh_file = tempfile.mktemp(".tsh")
-        export_mesh_file(mesh_file,mesh_dic)
-
-        # create a points .csv file
-        point_file = tempfile.mktemp(".csv")
-        fd = open(point_file,'w')
-        fd.write("x,y, elevation, stage \n\
-        1.0, 1.0,2.,4 \n\
-        1.0, 3.0,4,8 \n\
-        3.0,1.0,4.,8 \n")
-        fd.close()
-        
-        mesh_output_file = tempfile.mktemp(".tsh")
-        fit_to_mesh_file(mesh_file,
-                         point_file,
-                         mesh_output_file,
-                         alpha = 0.0)
-        # load in the .tsh file we just wrote
-        mesh_dic = import_mesh_file(mesh_output_file)
-        #print "mesh_dic",mesh_dic
-        ans =[[0.0, 0.0],
-              [5.0, 10.0],
-              [5.0,10.0]]
-        assert num.allclose(mesh_dic['vertex_attributes'],ans)
-
-        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
-                        ['elevation','stage'],
-                        'test_fit_to_mesh_file failed')
-
-        #clean up
-        os.remove(mesh_file)
-        os.remove(point_file)
-        os.remove(mesh_output_file)
-
-    def test_fit_to_mesh_fileII(self):
-        from load_mesh.loadASCII import import_mesh_file, \
-             export_mesh_file
-        import tempfile
-        import os
-
-        # create a .tsh file, no user outline
-        mesh_dic = {}
-        mesh_dic['vertices'] = [[0.0, 0.0],
-                                [0.0, 5.0],
-                                [5.0, 0.0]]
-        mesh_dic['triangles'] =  [[0, 2, 1]]
-        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
-        mesh_dic['triangle_tags'] = ['']
-        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
-        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
-        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
-        mesh_dic['segment_tags'] = ['external',
-                                                  'external',
-                                                  'external']
-        mesh_file = tempfile.mktemp(".tsh")
-        export_mesh_file(mesh_file,mesh_dic)
-
-        # create a points .csv file
-        point_file = tempfile.mktemp(".csv")
-        fd = open(point_file,'w')
-        fd.write("x,y,elevation, stage \n\
-        1.0, 1.0,2.,4 \n\
-        1.0, 3.0,4,8 \n\
-        3.0,1.0,4.,8 \n")
-        fd.close()
-
-        mesh_output_file = "new_triangle.tsh"
-        fit_to_mesh_file(mesh_file,
-                         point_file,
-                         mesh_output_file,
-                         alpha = 0.0)
-        # load in the .tsh file we just wrote
-        mesh_dic = import_mesh_file(mesh_output_file)
-
-        assert num.allclose(mesh_dic['vertex_attributes'],
-                            [[1.0, 2.0,0.0, 0.0],
-                             [1.0, 2.0,5.0, 10.0],
-                             [1.0, 2.0,5.0,10.0]])
-
-        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
-                        ['density', 'temp','elevation','stage'],
-                        'test_fit_to_mesh_file failed')
-
-        #clean up
-        os.remove(mesh_file)
-        os.remove(mesh_output_file)
-        os.remove(point_file)
-
-    def test_fit_to_mesh_file_errors(self):
-        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
-        import tempfile
-        import os
-
-        # create a .tsh file, no user outline
-        mesh_dic = {}
-        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
-        mesh_dic['triangles'] =  [[0, 2, 1]]
-        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
-        mesh_dic['triangle_tags'] = ['']
-        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
-        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
-        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
-        mesh_dic['segment_tags'] = ['external', 'external','external']
-        mesh_file = tempfile.mktemp(".tsh")
-        export_mesh_file(mesh_file,mesh_dic)
-
-        # create a bad points .csv file
-        point_file = tempfile.mktemp(".csv")
-        fd = open(point_file,'w')
-        fd.write("x,y,elevation stage \n\
-        1.0, 1.0,2.,4 \n\
-        1.0, 3.0,4,8 \n\
-        3.0,1.0,4.,8 \n")
-        fd.close()
-
-        mesh_output_file = "new_triangle.tsh"
-        try:
-            fit_to_mesh_file(mesh_file, point_file,
-                             mesh_output_file, display_errors = False)
-        except SyntaxError:
-            pass
-        else:
-            #self.failUnless(0 ==1,  'Bad file did not raise error!')
-            raise Exception('Bad file did not raise error!')
-            
-        #clean up
-        os.remove(mesh_file)
-        os.remove(point_file)
-
-    def test_fit_to_mesh_file_errorsII(self):
-        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
-        import tempfile
-        import os
-
-        # create a .tsh file, no user outline
-        mesh_file = tempfile.mktemp(".tsh")
-        fd = open(mesh_file,'w')
-        fd.write("unit testing a bad .tsh file \n")
-        fd.close()
-
-        # create a points .csv file
-        point_file = tempfile.mktemp(".csv")
-        fd = open(point_file,'w')
-        fd.write("x,y,elevation, stage \n\
-        1.0, 1.0,2.,4 \n\
-        1.0, 3.0,4,8 \n\
-        3.0,1.0,4.,8 \n")
-        fd.close()
-
-        mesh_output_file = "new_triangle.tsh"
-        try:
-            fit_to_mesh_file(mesh_file, point_file,
-                             mesh_output_file, display_errors = False)
-        except IOError:
-            pass
-        else:
-            raise Exception('Bad file did not raise error!')
-            
-        #clean up
-        os.remove(mesh_file)
-        os.remove(point_file)
-
-    def test_fit_to_mesh_file_errorsIII(self):
-        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
-        import tempfile
-        import os
-
-        # create a .tsh file, no user outline
-        mesh_dic = {}
-        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
-        mesh_dic['triangles'] =  [[0, 2, 1]]
-        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
-        mesh_dic['triangle_tags'] = ['']
-        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
-        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
-        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
-        mesh_dic['segment_tags'] = ['external', 'external','external']
-        mesh_file = tempfile.mktemp(".tsh")
-        export_mesh_file(mesh_file,mesh_dic)
-
-
-        # create a points .csv file
-        point_file = tempfile.mktemp(".csv")
-        fd = open(point_file,'w')
-        fd.write("x,y,elevation, stage \n\
-        1.0, 1.0,2.,4 \n\
-        1.0, 3.0,4,8 \n\
-        3.0,1.0,4.,8 \n")
-        fd.close()
-
-        #This a deliberately illegal filename to invoke the error.
-        mesh_output_file = ".../\z\z:ya.tsh"        
-
-        try:
-            fit_to_mesh_file(mesh_file, point_file,
-                             mesh_output_file, display_errors = False)
-        except IOError:
-            pass
-        else:
-            raise Exception('Bad file did not raise error!')
-        
-        #clean up
-        os.remove(mesh_file)
-        os.remove(point_file)
-  
-    def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self):
-
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        d = [1.0, 1.0]
-        e = [18.0, 1000.0]
-        
-        points = [a, b, c, d, e]
-        triangles = [ [1,0,2] ]   #bac
-
-        d1 = [1.0, 1.0]
-        d2 = [1.0, 2.0]
-        d3 = [3.0,1.0]
-        d4 = [2.0,3.0]
-        d5 = [2.0,2.0]
-        d6 = [1.0,3.0]
-        data_coords = [d1, d2, d3, d4, d5, d6]
-        z = linear_function(data_coords)
-        #print "z",z 
-
-        interp = Fit(points, triangles, alpha=0.0)
-        
-        try:
-            f = interp.fit(data_coords, z)
-        except VertsWithNoTrianglesError:
-            pass
-        else:
-            raise Exception('Verts with no triangles did not raise error!')
-        
-        #f = interp.fit(data_coords, z)
-        #answer = linear_function(points)
-
-        #  Removing the bad verts that we don't care about
-        #f = f[0:3]
-        #answer = answer[0:3]
-        #assert allclose(f, answer)
-
-#-------------------------------------------------------------
-if __name__ == "__main__":
-    suite = unittest.makeSuite(Test_Fit,'test')
-    runner = unittest.TextTestRunner() #verbosity=1)
-    runner.run(suite)
-
Index: unk/anuga_core/source/anuga/fit_interpolate/test_interpolate.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test_interpolate.py	(revision 9478)
+++ 	(revision )
@@ -1,1949 +1,0 @@
-#!/usr/bin/env python
-
-#TEST
-
-#import time, os
-
-
-import sys
-import os
-import unittest
-from math import sqrt
-import tempfile
-import csv
-
-from anuga.file.netcdf import NetCDFFile
-
-import numpy as num
-
-
-# ANUGA code imports
-import anuga
-from interpolate import *
-from anuga.coordinate_transforms.geo_reference import Geo_reference
-from anuga.utilities.numerical_tools import mean, NAN
-from anuga.file.sww import SWW_file
-from anuga.shallow_water.shallow_water_domain import Domain
-from abstract_2d_finite_volumes.quantity import Quantity
-from anuga.geospatial_data.geospatial_data import Geospatial_data
-from anuga.pmesh.mesh import Mesh
-
-def distance(x, y):
-    return sqrt(num.sum((num.array(x)-num.array(y))**2))
-
-def linear_function(point):
-    point = num.array(point)
-    return point[:,0]+point[:,1]
-
-
-class Test_Interpolate(unittest.TestCase):
-
-    def setUp(self):
-
-        import time
-        from mesh_factory import rectangular
-
-
-        #Create basic mesh
-        points, vertices, boundary = rectangular(2, 2)
-
-        #Create shallow water domain
-        domain = Domain(points, vertices, boundary)
-        domain.default_order=2
-
-
-        #Set some field values
-        domain.set_quantity('elevation', lambda x,y: -x)
-        domain.set_quantity('friction', 0.03)
-
-
-        ######################
-        # Boundary conditions
-        B = anuga.Transmissive_boundary(domain)
-        domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B})
-
-
-        ######################
-        #Initial condition - with jumps
-
-        bed = domain.quantities['elevation'].vertex_values
-        stage = num.zeros(bed.shape, num.float)
-
-        h = 0.3
-        for i in range(stage.shape[0]):
-            if i % 2 == 0:
-                stage[i,:] = bed[i,:] + h
-            else:
-                stage[i,:] = bed[i,:]
-
-        domain.set_quantity('stage', stage)
-
-        domain.distribute_to_vertices_and_edges()
-
-
-        self.domain = domain
-
-        C = domain.get_vertex_coordinates()
-        self.X = C[:,0:6:2].copy()
-        self.Y = C[:,1:6:2].copy()
-
-        self.F = bed
-
-
-
-    def tearDown(self):
-        pass
-
-    def test_datapoint_at_centroid(self):
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]   #bac
-
-        data = [ [2.0/3, 2.0/3] ] #Use centroid as one data point
-
-        interp = Interpolate(points, vertices)
-        A, _, _, _ = interp._build_interpolation_matrix_A(data)
-        assert num.allclose(A.todense(), [[1./3, 1./3, 1./3]])
-
-    def test_datapoint_in_hole(self):
-        # create 3 right-angled triangles arranged in a bigger triangle       
-        a = [0.0, 0.0] #0
-        b = [0.0, 2.0] #1
-        c = [2.0,0.0]  #2
-        d = [0.0,4.0]  #3
-        e = [2.0,2.0]  #4
-        f = [4.0,0.0]  #5
-        points = [a, b, c, d, e, f]
-        vertices = [ [1,0,2], [3,1,4], [4,2,5] ]   #bac dbe ecf
-
-        point_in_hole = [1.5, 1.5] # a point in the hole
-        data = [ [20, 20], [0.3, 0.3], point_in_hole, [2.5, 0.3], [30, 30] ] #some points inside and outside the hole
-
-        # any function for the vertices, we don't care about the result
-        f = num.array([linear_function(points), 2*linear_function(points)])
-        f = num.transpose(f)
-
-        interp = Interpolate(points, vertices)
-        interp.interpolate(f, data)
-        
-        assert not set(interp.inside_poly_indices).intersection(set(interp.outside_poly_indices)), \
-               'Some points are in both lists!'    
-        assert len(interp.inside_poly_indices) == 2
-        assert len(interp.outside_poly_indices) == 3
-        
-        interp.outside_poly_indices.sort()
-        assert interp.outside_poly_indices[1] == 2, \
-               'third outside point should be inside the hole!'
-
-    def test_simple_interpolation_example(self):
-        
-        from mesh_factory import rectangular
-
-        # Create basic mesh
-        points, vertices, boundary = rectangular(1, 3)
-
-        # Create shallow water domain
-        domain = Domain(points, vertices, boundary)
-
-        #----------------
-        #Constant values
-        #----------------        
-        quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3],
-                                    [4,4,4],[5,5,5]])
-
-
-        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
-        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
-        # FIXME: This concat should roll into get_vertex_values
-
-
-        # Get interpolated values at centroids
-        interpolation_points = domain.get_centroid_coordinates()
-        answer = quantity.get_values(location='centroids')
-
-        I = Interpolate(vertex_coordinates, triangles)
-        result = I.interpolate(vertex_values, interpolation_points)
-        assert num.allclose(result, answer)
-
-
-        #----------------
-        # Variable values
-        #----------------
-        quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7],
-                                    [1,4,-9],[2,5,0]])
-        
-        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
-        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
-        # FIXME: This concat should roll into get_vertex_values
-
-
-        # Get interpolated values at centroids
-        interpolation_points = domain.get_centroid_coordinates()
-        answer = quantity.get_values(location='centroids')
-
-        I = Interpolate(vertex_coordinates, triangles)
-        result = I.interpolate(vertex_values, interpolation_points)
-        assert num.allclose(result, answer)        
-        
-
-    def test_simple_interpolation_example_using_direct_interface(self):
-        
-        from mesh_factory import rectangular
-
-        # Create basic mesh
-        points, vertices, boundary = rectangular(1, 3)
-
-        # Create shallow water domain
-        domain = Domain(points, vertices, boundary)
-
-        #----------------
-        # Constant values
-        #----------------        
-        quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3],
-                                    [4,4,4],[5,5,5]])
-
-
-        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
-        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
-        # FIXME: This concat should roll into get_vertex_values
-
-
-        # Get interpolated values at centroids
-        interpolation_points = domain.get_centroid_coordinates()
-        answer = quantity.get_values(location='centroids')
-
-        result = interpolate(vertex_coordinates, triangles, vertex_values, interpolation_points)
-        assert num.allclose(result, answer)
-
-
-        #----------------
-        # Variable values
-        #----------------
-        quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7],
-                                    [1,4,-9],[2,5,0]])
-        
-        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
-        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
-        # FIXME: This concat should roll into get_vertex_values
-
-
-        # Get interpolated values at centroids
-        interpolation_points = domain.get_centroid_coordinates()
-        answer = quantity.get_values(location='centroids')
-
-        result = interpolate(vertex_coordinates, triangles,
-                             vertex_values, interpolation_points)
-        assert num.allclose(result, answer)        
-        
-        
-    def test_simple_interpolation_example_using_direct_interface_and_caching(self):
-        
-        from mesh_factory import rectangular
-
-        # Create basic mesh
-        points, vertices, boundary = rectangular(1, 3)
-
-        # Create shallow water domain
-        domain = Domain(points, vertices, boundary)
-
-        #----------------
-        # First call
-        #----------------
-        quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7],
-                                    [1,4,-9],[2,5,0]])
-        
-        x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False)
-        vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 )
-        # FIXME: This concat should roll into get_vertex_values
-
-        # Get interpolated values at centroids
-        interpolation_points = domain.get_centroid_coordinates()
-        answer = quantity.get_values(location='centroids')
-        result = interpolate(vertex_coordinates, triangles,
-                             vertex_values, interpolation_points,
-                             use_cache=True,
-                             verbose=False)
-        assert num.allclose(result, answer)                
-
-        # Second call using the cache
-        result = interpolate(vertex_coordinates, triangles,
-                             vertex_values, interpolation_points,
-                             use_cache=True,
-                             verbose=False)
-        assert num.allclose(result, answer)                        
-        
-        
-    def test_quad_tree(self):
-        p0 = [-10.0, -10.0]
-        p1 = [20.0, -10.0]
-        p2 = [-10.0, 20.0]
-        p3 = [10.0, 50.0]
-        p4 = [30.0, 30.0]
-        p5 = [50.0, 10.0]
-        p6 = [40.0, 60.0]
-        p7 = [60.0, 40.0]
-        p8 = [-66.0, 20.0]
-        p9 = [10.0, -66.0]
-
-        points = [p0, p1, p2, p3, p4, p5, p6, p7, p8, p9]
-        triangles = [ [0, 1, 2],
-                      [3, 2, 4],
-                      [4, 2, 1],
-                      [4, 1, 5],
-                      [3, 4, 6],
-                      [6, 4, 7],
-                      [7, 4, 5],
-                      [8, 0, 2],
-                      [0, 9, 1]]
-
-        data = [ [4,4] ]
-        interp = Interpolate(points, triangles)
-        #print "PDSG - interp.get_A()", interp.get_A()
-        answer =  [ [ 0.06666667,  0.46666667,  0.46666667,  0.,
-                      0., 0. , 0., 0., 0., 0.]]
-
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        assert num.allclose(A.todense(), answer)
-        
-        #interp.set_point_coordinates([[-30, -30]]) #point outside of mesh
-        #print "PDSG - interp.get_A()", interp.get_A()
-        data = [[-30, -30]]
-        answer =  [ [ 0.0,  0.0,  0.0,  0.,
-                      0., 0. , 0., 0., 0., 0.]]
-        
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)        
-        assert num.allclose(A.todense(), answer)
-
-
-        #point outside of quad tree root cell
-        #interp.set_point_coordinates([[-70, -70]])
-        #print "PDSG - interp.get_A()", interp.get_A()
-        data = [[-70, -70]]
-        answer =  [ [ 0.0,  0.0,  0.0,  0.,
-                      0., 0. , 0., 0., 0., 0.]]
-                      
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)        
-        assert num.allclose(A.todense(), answer)
-
-
-    def test_datapoints_at_vertices(self):
-        #Test that data points coinciding with vertices yield a diagonal matrix
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]   #bac
-
-        data = points #Use data at vertices
-
-        interp = Interpolate(points, vertices)
-        answer = [[1., 0., 0.],
-                   [0., 1., 0.],
-                   [0., 0., 1.]]
-                   
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        assert num.allclose(A.todense(), answer)
-
-
-    def test_datapoints_on_edge_midpoints(self):
-        #Try datapoints midway on edges -
-        #each point should affect two matrix entries equally
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]   #bac
-
-        data = [ [0., 1.], [1., 0.], [1., 1.] ]
-        answer =  [[0.5, 0.5, 0.0],  #Affects vertex 1 and 0
-                    [0.5, 0.0, 0.5],  #Affects vertex 0 and 2
-                    [0.0, 0.5, 0.5]]
-        interp = Interpolate(points, vertices)
-
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        assert num.allclose(A.todense(), answer)
-
-    def test_datapoints_on_edges(self):
-        #Try datapoints on edges -
-        #each point should affect two matrix entries in proportion
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]   #bac
-
-        data = [ [0., 1.5], [1.5, 0.], [1.5, 0.5] ]
-        answer =  [[0.25, 0.75, 0.0],  #Affects vertex 1 and 0
-                   [0.25, 0.0, 0.75],  #Affects vertex 0 and 2
-                   [0.0, 0.25, 0.75]]
-
-        interp = Interpolate(points, vertices)
-
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        assert num.allclose(A.todense(), answer)
-
-
-    def test_arbitrary_datapoints(self):
-        #Try arbitrary datapoints
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]   #bac
-
-        data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44] ]
-
-        interp = Interpolate(points, vertices)
-        #print "interp.get_A()", interp.get_A()
-        
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        results = A.todense()
-        assert num.allclose(num.sum(results, axis=1), 1.0)
-
-        
-    def test_arbitrary_datapoints_return_centroids(self):
-        #Try arbitrary datapoints, making sure they return
-        #an interpolation matrix for the intersected triangle's
-        #centroid.
-        
-        a = [1.0, 0.0]
-        b = [0.0, 3.0]
-        c = [4.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]
-
-        data = [ [1.2, 1.5], [1.123, 1.768], [2.43, 0.44] ]
-
-        interp = Interpolate(points, vertices)
-        
-        third = [1.0/3.0, 1.0/3.0, 1.0/3.0]
-        answer = [third, third, third]
-        
-        A,_,_,_ = interp._build_interpolation_matrix_A(data, output_centroids=True)
-        results = A.todense()
-        assert num.allclose(results, answer)        
-        
-        
-    def test_arbitrary_datapoints_some_outside(self):
-        #Try arbitrary datapoints one outside the triangle.
-        #That one should be ignored
-        
-
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0,0.0]
-        points = [a, b, c]
-        vertices = [ [1,0,2] ]   #bac
-
-        data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44], [5.0, 7.0]]
-
-        interp = Interpolate(points, vertices)
-        
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        results = A.todense()
-        assert num.allclose(num.sum(results, axis=1), [1,1,1,0])
-
-
-
-    # this causes a memory error in scipy.sparse
-    def test_more_triangles(self):
-
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0,1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0,-2.0]
-        f = [1.0, -2.0] #5
-
-        points = [a, b, c, d,e,f]
-        triangles = [[0,1,3],[1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc
-
-        #Data points
-        data = [ [-3., 2.0], [-2, 1], [0.0, 1], [0, 3], [2, 3], [-1.0/3,-4./3] ]
-        interp = Interpolate(points, triangles)
-
-        answer = [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0],    #Affects point d
-                  [0.5, 0.0, 0.0, 0.5, 0.0, 0.0],    #Affects points a and d
-                  [0.75, 0.25, 0.0, 0.0, 0.0, 0.0],  #Affects points a and b
-                  [0.0, 0.5, 0.0, 0.5, 0.0, 0.0],    #Affects points a and d
-                  [0.25, 0.75, 0.0, 0.0, 0.0, 0.0],  #Affects points a and b
-                  [1./3, 0.0, 0.0, 0.0, 1./3, 1./3]] #Affects points a, e and f
-
-
-        A,_,_,_ = interp._build_interpolation_matrix_A(data)
-        A = A.todense()
-        for i in range(A.shape[0]):
-            for j in range(A.shape[1]):
-                if not num.allclose(A[i,j], answer[i][j]):
-                    print i,j,':',A[i,j], answer[i][j]
-
-
-        #results = interp._build_interpolation_matrix_A(data).todense()
-
-        assert num.allclose(A, answer)
-    
-    def test_geo_ref(self):
-        v0 = [0.0, 0.0]
-        v1 = [0.0, 5.0]
-        v2 = [5.0, 0.0]
-
-        vertices_absolute = [v0, v1, v2]
-        triangles = [ [1,0,2] ]   #bac
-
-        geo = Geo_reference(57,100, 500)
-
-        vertices = geo.change_points_geo_ref(vertices_absolute)
-        #print "vertices",vertices 
-        
-        d0 = [1.0, 1.0]
-        d1 = [1.0, 2.0]
-        d2 = [3.0, 1.0]
-        point_coords = [ d0, d1, d2]
-
-        interp = Interpolate(vertices, triangles, mesh_origin=geo)
-        f = linear_function(vertices_absolute)
-        z = interp.interpolate(f, point_coords)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-        
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-        
-     
-    def test_sigma_epsilon(self):
-        """
-        def test_sigma_epsilon(self):
-            Testing ticket 168. I could not reduce the bug to this small
-            test though.
-        
-        """
-        v0 = [22031.25, 59687.5]
-        v1 = [22500., 60000.]
-        v2 = [22350.31640625, 59716.71484375]
-
-        vertices = [v0, v1, v2]
-        triangles = [ [1,0,2] ]   #bac
-
-        
-        point_coords = [[22050., 59700.]]
-
-        interp = Interpolate(vertices, triangles)
-        f = linear_function(vertices)
-        z = interp.interpolate(f, point_coords)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-        
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-        
-    def test_Geospatial_verts(self):
-        v0 = [0.0, 0.0]
-        v1 = [0.0, 5.0]
-        v2 = [5.0, 0.0]
-
-        vertices_absolute = [v0, v1, v2]
-        triangles = [ [1,0,2] ]   #bac
-
-        geo = Geo_reference(57,100, 500)
-        vertices = geo.change_points_geo_ref(vertices_absolute)
-        geopoints = Geospatial_data(vertices,geo_reference = geo)
-        #print "vertices",vertices 
-        
-        d0 = [1.0, 1.0]
-        d1 = [1.0, 2.0]
-        d2 = [3.0, 1.0]
-        point_coords = [ d0, d1, d2]
-
-        interp = Interpolate(geopoints, triangles)
-        f = linear_function(vertices_absolute)
-        z = interp.interpolate(f, point_coords)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-        
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-        
-    def test_interpolate_attributes_to_points(self):
-        v0 = [0.0, 0.0]
-        v1 = [0.0, 5.0]
-        v2 = [5.0, 0.0]
-
-        vertices = [v0, v1, v2]
-        triangles = [ [1,0,2] ]   #bac
-
-        d0 = [1.0, 1.0]
-        d1 = [1.0, 2.0]
-        d2 = [3.0, 1.0]
-        point_coords = [ d0, d1, d2]
-
-        interp = Interpolate(vertices, triangles)
-        f = linear_function(vertices)
-        z = interp.interpolate(f, point_coords)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-    def test_interpolate_attributes_to_pointsII(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        point_coords = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [0.0, 2.0],
-                        [1.0, 1.0],
-                        [2.0, 1.0],
-                        [0.0, 0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [3.0, 1.0]]
-
-        interp = Interpolate(vertices, triangles)
-        f = linear_function(vertices)
-        z = interp.interpolate(f, point_coords)
-        answer = linear_function(point_coords)
-        #print "z",z
-        #print "answer",answer
-        assert num.allclose(z, answer)
-
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-        
-    def test_interpolate_attributes_to_pointsIII(self):
-        #Test linear interpolation of known values at vertices to
-        #new points inside a triangle
-        
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        d = [5.0, 5.0]
-
-        vertices = [a, b, c, d]
-        triangles = [ [1,0,2], [2,3,1] ]   #bac, cdb
-
-        #Points within triangle 1
-        d0 = [1.0, 1.0]
-        d1 = [1.0, 2.0]
-        d2 = [3.0, 1.0]
-
-        #Point within triangle 2
-        d3 = [4.0, 3.0]
-
-        #Points on common edge
-        d4 = [2.5, 2.5]
-        d5 = [4.0, 1.0]
-
-        #Point on common vertex
-        d6 = [0., 5.]
-        
-        point_coords = [d0, d1, d2, d3, d4, d5, d6]
-
-        interp = Interpolate(vertices, triangles)
-
-        #Known values at vertices
-        #Functions are x+y, x+2y, 2x+y, x-y-5
-        f = [ [0., 0., 0., -5.],        # (0,0)
-              [5., 10., 5., -10.],      # (0,5)
-              [5., 5., 10.0, 0.],       # (5,0)
-              [10., 15., 15., -5.]]     # (5,5)
-
-        z = interp.interpolate(f, point_coords)
-        answer = [ [2., 3., 3., -5.],   # (1,1)
-                   [3., 5., 4., -6.],   # (1,2)
-                   [4., 5., 7., -3.],   # (3,1)
-                   [7., 10., 11., -4.], # (4,3)
-                   [5., 7.5, 7.5, -5.], # (2.5, 2.5)
-                   [5., 6., 9., -2.],   # (4,1)
-                   [5., 10., 5., -10.]]  # (0,5)
-
-        #print "***********"
-        #print "z",z
-        #print "answer",answer
-        #print "***********"
-
-        assert num.allclose(z, answer)
-
-
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-        
-    def test_interpolate_point_outside_of_mesh(self):
-        #Test linear interpolation of known values at vertices to
-        #new points inside a triangle
-        
-        a = [0.0, 0.0]
-        b = [0.0, 5.0]
-        c = [5.0, 0.0]
-        d = [5.0, 5.0]
-
-        vertices = [a, b, c, d]
-        triangles = [ [1,0,2], [2,3,1] ]   #bac, cdb
-
-        #Far away point
-        d7 = [-1., -1.]
-        
-        point_coords = [ d7]
-        interp = Interpolate(vertices, triangles)
-
-        #Known values at vertices
-        #Functions are x+y, x+2y, 2x+y, x-y-5
-        f = [ [0., 0., 0., -5.],        # (0,0)
-              [5., 10., 5., -10.],      # (0,5)
-              [5., 5., 10.0, 0.],       # (5,0)
-              [10., 15., 15., -5.]]     # (5,5)
-
-        z = interp.interpolate(f, point_coords) #, verbose=True)
-        answer = num.array([ [NAN, NAN, NAN, NAN]]) # (-1,-1)
-
-        #print "***********"
-        #print "z",z
-        #print "answer",answer
-        #print "***********"
-
-        #Should an error message be returned if points are outside
-        # of the mesh?
-        # A warning message is printed, if verbose is on.
-
-        for i in range(4):
-            self.failUnless( z[0,i] == answer[0,i], 'Fail!')
-        
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-
-        #print "z",z 
-        #print "answer",answer
-        
-        for i in range(4):
-            self.failUnless( z[0,i] == answer[0,i], 'Fail!')
-        
-        
-    def test_interpolate_attributes_to_pointsIV(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        point_coords = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [0.0, 2.0],
-                        [1.0, 1.0],
-                        [2.0, 1.0],
-                        [0.0, 0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [3.0, 1.0]]
-
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices),2*linear_function(vertices)])
-        f = num.transpose(f)
-        #print "f",f
-        z = interp.interpolate(f, point_coords)
-        answer = [linear_function(point_coords),
-                  2*linear_function(point_coords) ]
-        answer = num.transpose(answer)
-        #print "z",z
-        #print "answer",answer
-        assert num.allclose(z, answer)
-
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-    def test_interpolate_blocking(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        point_coords = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [0.0, 2.0],
-                        [1.0, 1.0],
-                        [2.0, 1.0],
-                        [0.0, 0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [3.0, 1.0]]
-
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices),2*linear_function(vertices)])
-        f = num.transpose(f)
-        #print "f",f
-        for blocking_max in range(len(point_coords)+2):
-        #if True:
-         #   blocking_max = 5
-            z = interp.interpolate(f, point_coords,
-                                   start_blocking_len=blocking_max)
-            answer = [linear_function(point_coords),
-                      2*linear_function(point_coords) ]
-            answer = num.transpose(answer)
-            #print "z",z
-            #print "answer",answer
-            assert num.allclose(z, answer)
-            
-        f = num.array([linear_function(vertices),2*linear_function(vertices),
-                       2*linear_function(vertices) - 100])
-        f = num.transpose(f)
-        #print "f",f
-        for blocking_max in range(len(point_coords)+2):
-        #if True:
-         #   blocking_max = 5
-            z = interp.interpolate(f, point_coords,
-                                   start_blocking_len=blocking_max)
-            answer = num.array([linear_function(point_coords),
-                                2*linear_function(point_coords) ,
-                                2*linear_function(point_coords)-100])
-            z = num.transpose(z)
-            #print "z",z
-            #print "answer",answer
-            assert num.allclose(z, answer)
-
-    def test_interpolate_geo_spatial(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        point_coords_absolute = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [0.0, 2.0],
-                        [1.0, 1.0],
-                        [2.0, 1.0],
-                        [0.0, 0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [3.0, 1.0]]
-
-        geo = Geo_reference(57,100, 500)
-        point_coords = geo.change_points_geo_ref(point_coords_absolute)
-        point_coords = Geospatial_data(point_coords,geo_reference = geo)
-        
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices),2*linear_function(vertices)])
-        f = num.transpose(f)
-        #print "f",f
-        for blocking_max in range(14):
-        #if True:
-         #   blocking_max = 5
-            z = interp.interpolate(f, point_coords,
-                                   start_blocking_len=blocking_max)
-            answer = [linear_function(point_coords.get_data_points( \
-                      absolute = True)),
-                      2*linear_function(point_coords.get_data_points( \
-                      absolute = True)) ]
-            answer = num.transpose(answer)
-            #print "z",z
-            #print "answer",answer
-            assert num.allclose(z, answer)
-            
-        f = num.array([linear_function(vertices),2*linear_function(vertices),
-                       2*linear_function(vertices) - 100])
-        f = num.transpose(f)
-        #print "f",f
-        for blocking_max in range(14):
-        #if True:
-         #   blocking_max = 5
-            z = interp.interpolate(f, point_coords,
-                                   start_blocking_len=blocking_max)
-            answer = num.array([linear_function(point_coords.get_data_points( \
-                                                              absolute = True)),
-                                                              2*linear_function(point_coords.get_data_points( \
-                                                              absolute = True)) ,
-                                                              2*linear_function(point_coords.get_data_points( \
-                                                              absolute = True))-100])
-            z = num.transpose(z)
-            #print "z",z
-            #print "answer",answer
-            assert num.allclose(z, answer)
-
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-        
-    def test_interpolate_geo_spatial(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-        point_coords_absolute = [[-2.0,  2.0],
-                                 [-1.0,  1.0],
-                                 [ 0.0,  2.0],
-                                 [ 1.0,  1.0],
-                                 [ 2.0,  1.0],
-                                 [ 0.0,  0.0],
-                                 [ 1.0,  0.0],
-                                 [ 0.0, -1.0],
-                                 [-0.2, -0.5],
-                                 [-0.9, -1.5],
-                                 [ 0.5, -1.9],
-                                 [ 3.0,  1.0]]
-
-        geo = Geo_reference(57, 100, 500)
-        point_coords = geo.change_points_geo_ref(point_coords_absolute)
-        point_coords = Geospatial_data(point_coords, geo_reference=geo)
-        
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices), 2*linear_function(vertices)])
-        f = num.transpose(f)
-        z = interp.interpolate_block(f, point_coords)
-        answer = [linear_function(point_coords.get_data_points(absolute=True)),
-                  2*linear_function(point_coords.get_data_points(absolute=True))
-                 ]
-        answer = num.transpose(answer)
-        msg = ('Expected z\n%s\nto be close to answer\n%s'
-               % (str(z), str(answer)))
-        assert num.allclose(z, answer), msg
-            
-        z = interp.interpolate(f, point_coords, start_blocking_len = 2)
-
-        msg = ('Expected z\n%s\nto be close to answer\n%s'
-               % (str(z), str(answer)))
-        assert num.allclose(z, answer)
-
-        
-    def test_interpolate_reuse_if_None(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        point_coords = [[-2.0,  2.0],
-                        [-1.0,  1.0],
-                        [ 0.0,  2.0],
-                        [ 1.0,  1.0],
-                        [ 2.0,  1.0],
-                        [ 0.0,  0.0],
-                        [ 1.0,  0.0],
-                        [ 0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [ 0.5, -1.9],
-                        [ 3.0,  1.0]]
-
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices),2*linear_function(vertices)])
-        f = num.transpose(f)
-        z = interp.interpolate(f, point_coords,
-                               start_blocking_len=20)
-        answer = [linear_function(point_coords),
-                  2*linear_function(point_coords) ]
-        answer = num.transpose(answer)
-        #print "z",z
-        #print "answer",answer
-        assert num.allclose(z, answer)
-        assert num.allclose(interp._A_can_be_reused, True)
-
-        z = interp.interpolate(f)
-        assert num.allclose(z, answer)
-        
-        # This causes blocking to occur. 
-        z = interp.interpolate(f, start_blocking_len=10)
-        assert num.allclose(z, answer)
-        assert num.allclose(interp._A_can_be_reused, False)
-
-        #A is recalculated
-        z = interp.interpolate(f)
-        assert num.allclose(z, answer)
-        assert num.allclose(interp._A_can_be_reused, True)
-        
-        interp = Interpolate(vertices, triangles)
-        #Must raise an exception, no points specified
-        try:
-            z = interp.interpolate(f)
-        except:
-            pass
-        
-    def xxtest_interpolate_reuse_if_same(self):
-
-        # This on tests that repeated identical interpolation
-        # points makes use of precomputed matrix (Ole)
-        # This is not really a test and is disabled for now
-        
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-
-        point_coords = [[-2.0,  2.0],
-                        [-1.0,  1.0],
-                        [ 0.0,  2.0],
-                        [ 1.0,  1.0],
-                        [ 2.0,  1.0],
-                        [ 0.0,  0.0],
-                        [ 1.0,  0.0],
-                        [ 0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [ 0.5, -1.9],
-                        [ 3.0,  1.0]]
-
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices), 2*linear_function(vertices)])
-        f = num.transpose(f)
-        z = interp.interpolate(f, point_coords)
-        answer = [linear_function(point_coords),
-                  2*linear_function(point_coords) ]
-        answer = num.transpose(answer)
-
-        assert num.allclose(z, answer)
-        assert num.allclose(interp._A_can_be_reused, True)
-
-
-        z = interp.interpolate(f)    # None
-        assert num.allclose(z, answer)        
-        z = interp.interpolate(f, point_coords) # Repeated (not really a test)        
-        assert num.allclose(z, answer)
-        
-
-
-    def test_interpolation_interface_time_only(self):
-
-        # Test spatio-temporal interpolation
-        # Test that spatio temporal function performs the correct
-        # interpolations in both time and space
-        
-
-
-        #Three timesteps
-        time = [1.0, 5.0, 6.0]
-        
-
-        #One quantity
-        Q = num.zeros( (3,6), num.float )
-
-        #Linear in time and space
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        
-        for i, t in enumerate(time):
-            Q[i, :] = t*linear_function(points)
-
-            
-        #Check basic interpolation of one quantity using averaging
-        #(no interpolation points or spatial info)
-        I = Interpolation_function(time, [mean(Q[0,:]),
-                                          mean(Q[1,:]),
-                                          mean(Q[2,:])])
-
-
-
-        #Check temporal interpolation
-        for i in [0,1,2]:
-            assert num.allclose(I(time[i]), mean(Q[i,:]))
-
-        #Midway    
-        assert num.allclose(I( (time[0] + time[1])/2 ),
-                            (I(time[0]) + I(time[1]))/2 )
-
-        assert num.allclose(I( (time[1] + time[2])/2 ),
-                            (I(time[1]) + I(time[2]))/2 )
-
-        assert num.allclose(I( (time[0] + time[2])/2 ),
-                            (I(time[0]) + I(time[2]))/2 )                 
-
-        #1/3
-        assert num.allclose(I( (time[0] + time[2])/3 ),
-                            (I(time[0]) + I(time[2]))/3 )                         
-
-
-        #Out of bounds checks
-        try:
-            I(time[0]-1) 
-        except:
-            pass
-        else:
-            raise Exception('Should raise exception')
-
-        try:
-            I(time[-1]+1) 
-        except:
-            pass
-        else:
-            raise Exception('Should raise exception')
-
-
-        
-
-    def test_interpolation_interface_spatial_only(self):
-        # Test spatio-temporal interpolation with constant time
-        
-        #Three timesteps
-        time = [1.0, 5.0, 6.0]
-               
-        #Setup mesh used to represent fitted function
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-
-        #New datapoints where interpolated values are sought
-        interpolation_points = [[ 0.0, 0.0],
-                                [ 0.5, 0.5],
-                                [ 0.7, 0.7],
-                                [ 1.0, 0.5],
-                                [ 2.0, 0.4],
-                                [ 2.8, 1.2]]
-
-
-        #One quantity linear in space
-        Q = linear_function(points)
-
-
-        #Check interpolation of one quantity using interpolaton points
-        I = Interpolation_function(time, Q,
-                                   vertex_coordinates = points,
-                                   triangles = triangles, 
-                                   interpolation_points = interpolation_points,
-                                   verbose = False)
-
-
-        answer = linear_function(interpolation_points)
-
-        t = time[0]
-        for j in range(50): #t in [1, 6]
-            for id in range(len(interpolation_points)):
-                assert num.allclose(I(t, id), answer[id])
-            t += 0.1    
-
-        try:    
-            I(1)
-        except:
-            pass
-        else:
-            raise Exception('Should raise exception')
-
-            
-    def test_interpolation_interface(self):
-        # Test spatio-temporal interpolation
-        # Test that spatio temporal function performs the correct
-        # interpolations in both time and space
-    
-        #Three timesteps
-        time = [1.0, 5.0, 6.0]    
-
-        #Setup mesh used to represent fitted function
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-
-        #New datapoints where interpolated values are sought
-        interpolation_points = [[ 0.0, 0.0],
-                                [ 0.5, 0.5],
-                                [ 0.7, 0.7],
-                                [ 1.0, 0.5],
-                                [ 2.0, 0.4],
-                                [ 2.8, 1.2]]
-
-        #One quantity
-        Q = num.zeros( (3,6), num.float )
-
-        #Linear in time and space
-        for i, t in enumerate(time):
-            Q[i, :] = t*linear_function(points)
-
-        #Check interpolation of one quantity using interpolaton points)
-        I = Interpolation_function(time, Q,
-                                   vertex_coordinates = points,
-                                   triangles = triangles, 
-                                   interpolation_points = interpolation_points,
-                                   verbose = False)
-
-        answer = linear_function(interpolation_points)
-        
-        t = time[0]
-        for j in range(50): #t in [1, 6]
-            for id in range(len(interpolation_points)):
-                assert num.allclose(I(t, id), t*answer[id])
-            t += 0.1    
-
-        try:    
-            I(1)
-        except:
-            pass
-        else:
-            raise Exception('Should raise exception')
-
-
-
-    def test_interpolation_interface_with_time_thinning(self):
-        # Test spatio-temporal interpolation
-        # Test that spatio temporal function performs the correct
-        # interpolations in both time and space
-    
-        # Eight timesteps
-        time = [1.0, 2.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0]    
-
-        # Setup mesh used to represent fitted function
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        # bac, bce, ecf, dbe
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-
-        # New datapoints where interpolated values are sought
-        interpolation_points = [[ 0.0, 0.0],
-                                [ 0.5, 0.5],
-                                [ 0.7, 0.7],
-                                [ 1.0, 0.5],
-                                [ 2.0, 0.4],
-                                [ 2.8, 1.2]]
-
-        # One quantity
-        Q = num.zeros((8,6), num.float)
-
-        # Linear in time and space
-        for i, t in enumerate(time):
-            Q[i, :] = t*linear_function(points)
-
-        # Check interpolation of one quantity using interpolaton points) using default
-        # time_thinning of 1
-        I = Interpolation_function(time, Q,
-                                   vertex_coordinates=points,
-                                   triangles=triangles, 
-                                   interpolation_points=interpolation_points,
-                                   verbose=False)
-
-        answer = linear_function(interpolation_points)
-
-        
-        t = time[0]
-        for j in range(90): #t in [1, 10]
-            for id in range(len(interpolation_points)):
-                assert num.allclose(I(t, id), t*answer[id])
-            t += 0.1    
-
-
-        # Now check time_thinning
-        I = Interpolation_function(time, Q,
-                                   vertex_coordinates=points,
-                                   triangles=triangles, 
-                                   interpolation_points=interpolation_points,
-                                   time_thinning=2,
-                                   verbose=False)
-
-
-        assert len(I.time) == 4
-        assert( num.allclose(I.time, [1.0, 4.0, 7.0, 9.0] ))    
-
-        answer = linear_function(interpolation_points)
-
-        t = time[0]
-        for j in range(80): #t in [1, 9]
-            for id in range(len(interpolation_points)):
-                assert num.allclose(I(t, id), t*answer[id])
-            t += 0.1    
-
-
-
-    def test_interpolation_precompute_points(self):
-        # looking at a discrete mesh
-        #
-    
-        #Three timesteps
-        time = [0.0, 60.0]    
-
-        #Setup mesh used to represent fitted function
-        points = [[ 15., -20.],
-                  [ 15.,  10.],
-                  [  0., -20.],
-                  [  0.,  10.],
-                  [  0., -20.],
-                  [ 15.,  10.]]
-        
-        triangles = [[0, 1, 2],
-                     [3, 4, 5]]
-
-        #New datapoints where interpolated values are sought
-        interpolation_points = [[ 1.,  0.], [0.,1.]]
-
-        #One quantity
-        Q = num.zeros( (2,6), num.float )
-
-        #Linear in time and space
-        for i, t in enumerate(time):
-            Q[i, :] = t*linear_function(points)
-        #print "Q", Q
-
-
-        
-        interp = Interpolate(points, triangles)
-        f = num.array([linear_function(points),2*linear_function(points)])
-        f = num.transpose(f)
-        #print "f",f
-        z = interp.interpolate(f, interpolation_points)
-        answer = [linear_function(interpolation_points),
-                  2*linear_function(interpolation_points) ]
-        answer = num.transpose(answer)
-        #print "z",z
-        #print "answer",answer
-        assert num.allclose(z, answer)
-
-
-        #Check interpolation of one quantity using interpolaton points)
-        I = Interpolation_function(time, Q,
-                                   vertex_coordinates = points,
-                                   triangles = triangles, 
-                                   interpolation_points = interpolation_points,
-                                   verbose = False)
-        
-        #print "I.precomputed_values", I.precomputed_values
-
-        msg = 'Interpolation failed'
-        assert num.allclose(I.precomputed_values['Attribute'][1], [60, 60]), msg
-        #self.failUnless( I.precomputed_values['Attribute'][1] == 60.0,
-        #                ' failed')
-        
-    def test_interpolation_function_outside_point(self):
-        # Test spatio-temporal interpolation
-        # Test that spatio temporal function performs the correct
-        # interpolations in both time and space
-    
-        # Three timesteps
-        time = [1.0, 5.0, 6.0]    
-
-        # Setup mesh used to represent fitted function
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-
-        # New datapoints where interpolated values are sought
-        interpolation_points = [[ 0.0, 0.0],
-                                [ 0.5, 0.5],
-                                [ 0.7, 0.7],
-                                [ 1.0, 0.5],
-                                [ 2.0, 0.4],
-                                [ 545354534, 4354354353]] # outside the mesh
-
-        # One quantity
-        Q = num.zeros( (3,6), num.float )
-
-        # Linear in time and space
-        for i, t in enumerate(time):
-            Q[i, :] = t*linear_function(points)
-
-        # Check interpolation of one quantity using interpolaton points)
-
-        I = Interpolation_function(time, Q,
-                                   vertex_coordinates = points,
-                                   triangles = triangles, 
-                                   interpolation_points = interpolation_points,
-                                   verbose = False)
-        
-        
-        assert num.alltrue(I.precomputed_values['Attribute'][:,4] != NAN)
-        assert num.sometrue(I.precomputed_values['Attribute'][:,5] == NAN)
-
-        #X = I.precomputed_values['Attribute'][1,:]
-        #print X
-        #print take(X, X == NAN)
-        #print where(X == NAN, range(len(X)), 0)        
-        
-        answer = linear_function(interpolation_points)
-          
-        t = time[0]
-        for j in range(50): #t in [1, 6]
-            for id in range(len(interpolation_points)-1):
-                assert num.allclose(I(t, id), t*answer[id])
-            t += 0.1
-            
-        # Now test the point outside the mesh
-        t = time[0]
-        for j in range(50): #t in [1, 6]
-            self.failUnless(I(t, 5) == NAN, 'Fail!')
-            t += 0.1  
-            
-        try:    
-            I(1)
-        except:
-            pass
-        else:
-            raise Exception('Should raise exception')
-
-
-    def test_interpolation_function_time(self):
-        #Test a long time series with an error in it (this did cause an
-        #error once)
-        
-
-        time = num.array(\
-        [0.00000000e+00, 5.00000000e-02, 1.00000000e-01,   1.50000000e-01,
-        2.00000000e-01,   2.50000000e-01,   3.00000000e-01,   3.50000000e-01,
-        4.00000000e-01,   4.50000000e-01,   5.00000000e-01,   5.50000000e-01,
-        6.00000000e-01,   6.50000000e-01,   7.00000000e-01,   7.50000000e-01,
-        8.00000000e-01,   8.50000000e-01,   9.00000000e-01,   9.50000000e-01,
-        1.00000000e-00,   1.05000000e+00,   1.10000000e+00,   1.15000000e+00,
-        1.20000000e+00,   1.25000000e+00,   1.30000000e+00,   1.35000000e+00,
-        1.40000000e+00,   1.45000000e+00,   1.50000000e+00,   1.55000000e+00,
-        1.60000000e+00,   1.65000000e+00,   1.70000000e+00,   1.75000000e+00,
-        1.80000000e+00,   1.85000000e+00,   1.90000000e+00,   1.95000000e+00,
-        2.00000000e+00,   2.05000000e+00,   2.10000000e+00,   2.15000000e+00,
-        2.20000000e+00,   2.25000000e+00,   2.30000000e+00,   2.35000000e+00,
-        2.40000000e+00,   2.45000000e+00,   2.50000000e+00,   2.55000000e+00,
-        2.60000000e+00,   2.65000000e+00,   2.70000000e+00,   2.75000000e+00,
-        2.80000000e+00,   2.85000000e+00,   2.90000000e+00,   2.95000000e+00,
-        3.00000000e+00,   3.05000000e+00,   9.96920997e+36,   3.15000000e+00,
-        3.20000000e+00,   3.25000000e+00,   3.30000000e+00,   3.35000000e+00,
-        3.40000000e+00,   3.45000000e+00,   3.50000000e+00,   3.55000000e+00,
-        3.60000000e+00,   3.65000000e+00,   3.70000000e+00,   3.75000000e+00,
-        3.80000000e+00,   3.85000000e+00,   3.90000000e+00,   3.95000000e+00,
-        4.00000000e+00,   4.05000000e+00,   4.10000000e+00,   4.15000000e+00,
-        4.20000000e+00,   4.25000000e+00,   4.30000000e+00,   4.35000000e+00,
-        4.40000000e+00,   4.45000000e+00,   4.50000000e+00,   4.55000000e+00,
-        4.60000000e+00,   4.65000000e+00,   4.70000000e+00,   4.75000000e+00,
-        4.80000000e+00,   4.85000000e+00,   4.90000000e+00,   4.95000000e+00,
-        5.00000000e+00,   5.05000000e+00,   5.10000000e+00,   5.15000000e+00,
-        5.20000000e+00,   5.25000000e+00,   5.30000000e+00,   5.35000000e+00,
-        5.40000000e+00,   5.45000000e+00,   5.50000000e+00,   5.55000000e+00,
-        5.60000000e+00,   5.65000000e+00,   5.70000000e+00,   5.75000000e+00,
-        5.80000000e+00,   5.85000000e+00,   5.90000000e+00,   5.95000000e+00,
-        6.00000000e+00,   6.05000000e+00,   6.10000000e+00,   6.15000000e+00,
-        6.20000000e+00,   6.25000000e+00,   6.30000000e+00,   6.35000000e+00,
-        6.40000000e+00,   6.45000000e+00,   6.50000000e+00,   6.55000000e+00,
-        6.60000000e+00,   6.65000000e+00,   6.70000000e+00,   6.75000000e+00,
-        6.80000000e+00,   6.85000000e+00,   6.90000000e+00,   6.95000000e+00,
-        7.00000000e+00,   7.05000000e+00,   7.10000000e+00,   7.15000000e+00,
-        7.20000000e+00,   7.25000000e+00,   7.30000000e+00,   7.35000000e+00,
-        7.40000000e+00,   7.45000000e+00,   7.50000000e+00,   7.55000000e+00,
-        7.60000000e+00,   7.65000000e+00,   7.70000000e+00,   7.75000000e+00,
-        7.80000000e+00,   7.85000000e+00,   7.90000000e+00,   7.95000000e+00,
-        8.00000000e+00,   8.05000000e+00,   8.10000000e+00,   8.15000000e+00,
-        8.20000000e+00,   8.25000000e+00,   8.30000000e+00,   8.35000000e+00,
-        8.40000000e+00,   8.45000000e+00,   8.50000000e+00,   8.55000000e+00,
-        8.60000000e+00,   8.65000000e+00,   8.70000000e+00,   8.75000000e+00,
-        8.80000000e+00,   8.85000000e+00,   8.90000000e+00,   8.95000000e+00,
-        9.00000000e+00,   9.05000000e+00,   9.10000000e+00,   9.15000000e+00,
-        9.20000000e+00,   9.25000000e+00,   9.30000000e+00,   9.35000000e+00,
-        9.40000000e+00,   9.45000000e+00,   9.50000000e+00,   9.55000000e+00,
-        9.60000000e+00,   9.65000000e+00,   9.70000000e+00,   9.75000000e+00,
-        9.80000000e+00,   9.85000000e+00,   9.90000000e+00,   9.95000000e+00,
-        1.00000000e+01,   1.00500000e+01,   1.01000000e+01,   1.01500000e+01,
-        1.02000000e+01,   1.02500000e+01,   1.03000000e+01,   1.03500000e+01,
-        1.04000000e+01,   1.04500000e+01,   1.05000000e+01,   1.05500000e+01,
-        1.06000000e+01,   1.06500000e+01,   1.07000000e+01,   1.07500000e+01,
-        1.08000000e+01,   1.08500000e+01,   1.09000000e+01,   1.09500000e+01,
-        1.10000000e+01,   1.10500000e+01,   1.11000000e+01,   1.11500000e+01,
-        1.12000000e+01,   1.12500000e+01,   1.13000000e+01,   1.13500000e+01,
-        1.14000000e+01,   1.14500000e+01,   1.15000000e+01,   1.15500000e+01,
-        1.16000000e+01,   1.16500000e+01,   1.17000000e+01,   1.17500000e+01,
-        1.18000000e+01,   1.18500000e+01,   1.19000000e+01,   1.19500000e+01,
-        1.20000000e+01,   1.20500000e+01,   1.21000000e+01,   1.21500000e+01,
-        1.22000000e+01,   1.22500000e+01,   1.23000000e+01,   1.23500000e+01,
-        1.24000000e+01,   1.24500000e+01,   1.25000000e+01,   1.25500000e+01,
-        1.26000000e+01,   1.26500000e+01,   1.27000000e+01,   1.27500000e+01,
-        1.28000000e+01,   1.28500000e+01,   1.29000000e+01,   1.29500000e+01,
-        1.30000000e+01,   1.30500000e+01,   1.31000000e+01,   1.31500000e+01,
-        1.32000000e+01,   1.32500000e+01,   1.33000000e+01,   1.33500000e+01,
-        1.34000000e+01,   1.34500000e+01,   1.35000000e+01,   1.35500000e+01,
-        1.36000000e+01,   1.36500000e+01,   1.37000000e+01,   1.37500000e+01,
-        1.38000000e+01,   1.38500000e+01,   1.39000000e+01,   1.39500000e+01,
-        1.40000000e+01,   1.40500000e+01,   1.41000000e+01,   1.41500000e+01,
-        1.42000000e+01,   1.42500000e+01,   1.43000000e+01,   1.43500000e+01,
-        1.44000000e+01,   1.44500000e+01,   1.45000000e+01,   1.45500000e+01,
-        1.46000000e+01,   1.46500000e+01,   1.47000000e+01,   1.47500000e+01,
-        1.48000000e+01,   1.48500000e+01,   1.49000000e+01,   1.49500000e+01,
-        1.50000000e+01,   1.50500000e+01,   1.51000000e+01,   1.51500000e+01,
-        1.52000000e+01,   1.52500000e+01,   1.53000000e+01,   1.53500000e+01,
-        1.54000000e+01,   1.54500000e+01,   1.55000000e+01,   1.55500000e+01,
-        1.56000000e+01,   1.56500000e+01,   1.57000000e+01,   1.57500000e+01,
-        1.58000000e+01,   1.58500000e+01,   1.59000000e+01,   1.59500000e+01,
-        1.60000000e+01,   1.60500000e+01,   1.61000000e+01,   1.61500000e+01,
-        1.62000000e+01,   1.62500000e+01,   1.63000000e+01,   1.63500000e+01,
-        1.64000000e+01,   1.64500000e+01,   1.65000000e+01,   1.65500000e+01,
-        1.66000000e+01,   1.66500000e+01,   1.67000000e+01,   1.67500000e+01,
-        1.68000000e+01,   1.68500000e+01,   1.69000000e+01,   1.69500000e+01,
-        1.70000000e+01,   1.70500000e+01,   1.71000000e+01,   1.71500000e+01,
-        1.72000000e+01,   1.72500000e+01,   1.73000000e+01,   1.73500000e+01,
-        1.74000000e+01,   1.74500000e+01,   1.75000000e+01,   1.75500000e+01,
-        1.76000000e+01,   1.76500000e+01,   1.77000000e+01,   1.77500000e+01,
-        1.78000000e+01,   1.78500000e+01,   1.79000000e+01,   1.79500000e+01,
-        1.80000000e+01,   1.80500000e+01,   1.81000000e+01,   1.81500000e+01,
-        1.82000000e+01,   1.82500000e+01,   1.83000000e+01,   1.83500000e+01,
-        1.84000000e+01,   1.84500000e+01,   1.85000000e+01,   1.85500000e+01,
-        1.86000000e+01,   1.86500000e+01,   1.87000000e+01,   1.87500000e+01,
-        1.88000000e+01,   1.88500000e+01,   1.89000000e+01,   1.89500000e+01,
-        1.90000000e+01,   1.90500000e+01,   1.91000000e+01,   1.91500000e+01,
-        1.92000000e+01,   1.92500000e+01,   1.93000000e+01,   1.93500000e+01,
-        1.94000000e+01,   1.94500000e+01,   1.95000000e+01,   1.95500000e+01,
-        1.96000000e+01,   1.96500000e+01,   1.97000000e+01,   1.97500000e+01,
-        1.98000000e+01,   1.98500000e+01,   1.99000000e+01,   1.99500000e+01,
-        2.00000000e+01,   2.00500000e+01,   2.01000000e+01,   2.01500000e+01,
-        2.02000000e+01,   2.02500000e+01,   2.03000000e+01,   2.03500000e+01,
-        2.04000000e+01,   2.04500000e+01,   2.05000000e+01,   2.05500000e+01,
-        2.06000000e+01,   2.06500000e+01,   2.07000000e+01,   2.07500000e+01,
-        2.08000000e+01,   2.08500000e+01,   2.09000000e+01,   2.09500000e+01,
-        2.10000000e+01,   2.10500000e+01,   2.11000000e+01,   2.11500000e+01,
-        2.12000000e+01,   2.12500000e+01,   2.13000000e+01,   2.13500000e+01,
-        2.14000000e+01,   2.14500000e+01,   2.15000000e+01,   2.15500000e+01,
-        2.16000000e+01,   2.16500000e+01,   2.17000000e+01,   2.17500000e+01,
-        2.18000000e+01,   2.18500000e+01,   2.19000000e+01,   2.19500000e+01,
-        2.20000000e+01,   2.20500000e+01,   2.21000000e+01,   2.21500000e+01,
-        2.22000000e+01,   2.22500000e+01,   2.23000000e+01,   2.23500000e+01,
-        2.24000000e+01,   2.24500000e+01,   2.25000000e+01])
-
-        #print 'Diff', time[1:] - time[:-1] 
-
-        #Setup mesh used to represent fitted function
-        a = [0.0, 0.0]
-        b = [0.0, 2.0]
-        c = [2.0, 0.0]
-        d = [0.0, 4.0]
-        e = [2.0, 2.0]
-        f = [4.0, 0.0]
-
-        points = [a, b, c, d, e, f]
-        #bac, bce, ecf, dbe
-        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
-
-
-        #New datapoints where interpolated values are sought
-        interpolation_points = [[ 0.0, 0.0],
-                                [ 0.5, 0.5],
-                                [ 0.7, 0.7],
-                                [ 1.0, 0.5],
-                                [ 2.0, 0.4],
-                                [ 545354534, 4354354353]] # outside the mesh
-
-        #One quantity
-        Q = num.zeros( (len(time),6), num.float )
-
-        #Linear in time and space
-        for i, t in enumerate(time):
-            Q[i, :] = t*linear_function(points)
-
-        #Check interpolation of one quantity using interpolaton points)
-        try:
-            I = Interpolation_function(time, Q,
-                                       vertex_coordinates = points,
-                                       triangles = triangles, 
-                                       interpolation_points = interpolation_points,
-                                       verbose = False)
-        except:
-            pass
-        else:
-            raise Exception('Should raise exception due to time being non-monotoneous')
-      
-
-    def test_points_outside_the_polygon(self):
-        a = [-1.0, 0.0]
-        b = [3.0, 4.0]
-        c = [4.0, 1.0]
-        d = [-3.0, 2.0] #3
-        e = [-1.0, -2.0]
-        f = [1.0, -2.0] #5
-
-        vertices = [a, b, c, d,e,f]
-        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
-
-        point_coords = [[-2.0, 2.0],
-                        [-1.0, 1.0],
-                        [9999.0, 9999.0], # point Outside poly
-                        [-9999.0, 1.0], # point Outside poly
-                        [2.0, 1.0],
-                        [0.0, 0.0],
-                        [1.0, 0.0],
-                        [0.0, -1.0],
-                        [-0.2, -0.5],
-                        [-0.9, -1.5],
-                        [0.5, -1.9],
-                        [999999, 9999999]] # point Outside poly
-        geo_data = Geospatial_data(data_points = point_coords)
-
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices),2*linear_function(vertices)])
-        f = num.transpose(f)
-        #print "f",f
-        z = interp.interpolate(f, geo_data)
-        #z = interp.interpolate(f, point_coords)
-        answer = [linear_function(point_coords),
-                  2*linear_function(point_coords) ]
-        answer = num.transpose(answer)
-        answer[2,:] = [NAN, NAN]
-        answer[3,:] = [NAN, NAN]
-        answer[11,:] = [NAN, NAN]
-        #print "z",z
-        #print "answer _ fixed",answer
-        assert num.allclose(z[0:1], answer[0:1])
-        assert num.allclose(z[4:10], answer[4:10])
-        for i in [2,3,11]:
-            self.failUnless( z[i,1] == answer[11,1], 'Fail!')
-            self.failUnless( z[i,0] == answer[11,0], 'Fail!')
-
-
-
-
-    def test_points_in_hole(self):
-
-        v0  = [0.0,     0.0]
-        v1  = [1.0/3.0, 0.0]
-        v2  = [2.0/3.0, 0.0]
-        v3  = [1.0,     0.0]
-        v4  = [0.0,     1.0/3.0]
-        v5  = [1.0/3.0, 1.0/3.0]
-        v6  = [2.0/3.0, 1.0/3.0]
-        v7  = [1.0,     1.0/3.0]
-        v8  = [0.0,     2.0/3.0]
-        v9  = [1.0/3.0, 2.0/3.0]
-        v10 = [2.0/3.0, 2.0/3.0]
-        v11 = [1.0,     2.0/3.0]
-        v12 = [0.0,     1.0]
-        v13 = [1.0/3.0, 1.0]
-        v14 = [2.0/3.0, 1.0]
-        v15 = [1.0,     1.0]
-
-
-        vertices = [v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15]
-
-        triangles = [[1,4,0], [1,5,4],    [2,5,1], [2,6,5],       [3,6,2],  [3,7,6],
-                     [5,8,4], [5,9,8],                            [7,10,6], [7,11,10],
-                     [9,12,8], [9,13,12], [10,13,9], [10,14,13], [11,14,10], [11,15,14]]
-
-
-
-
-        point_coords = [[0.25, 0.25],
-                        [0.25, 0.2],
-                        [10.0, 12.0], # point Outside poly
-                        [0.5, 0.5], # point in hole
-                        [0.5, 0.4], # point in hole
-                        [0.0, 0.0],
-                        [1.0, 0.0]]
-
-        geo_data = Geospatial_data(data_points = point_coords)
-
-        interp = Interpolate(vertices, triangles)
-        f = num.array([linear_function(vertices),2*linear_function(vertices)])
-        f = num.transpose(f)
-        #print "f",f
-        z = interp.interpolate(f, geo_data)
-        #z = interp.interpolate(f, point_coords)
-        answer = [linear_function(point_coords),
-                  2*linear_function(point_coords) ]
-        answer = num.transpose(answer)
-        answer[2,:] = [NAN, NAN]
-        answer[3,:] = [NAN, NAN]
-        answer[4,:] = [NAN, NAN]
-        #print "z",z
-        #print "answer _ fixed",answer
-        assert num.allclose(z[0:1], answer[0:1])
-        assert num.allclose(z[5:6], answer[5:6])
-        for i in [2,3,4]:
-            self.failUnless( z[i,1] == answer[2,1], 'Fail!')
-            self.failUnless( z[i,0] == answer[2,0], 'Fail!')
-
-
-    def test_interpolate_sww2csv(self):
-
-        def elevation_function(x, y):
-            return -x
-        
-        # Create mesh
-        mesh_file = tempfile.mktemp(".tsh")    
-        points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]]
-        m = Mesh()
-        m.add_vertices(points)
-        m.auto_segment()
-        m.generate_mesh(verbose=False)
-        m.export_mesh_file(mesh_file)
-        
-        # Create shallow water domain
-        domain = Domain(mesh_file)
-        os.remove(mesh_file)
-        
-        domain.default_order = 2
-
-        # This test was made before tight_slope_limiters were introduced
-        # Since were are testing interpolation values this is OK
-        domain.tight_slope_limiters = 0 
-
-        # Set some field values
-        domain.set_quantity('elevation', elevation_function)
-        domain.set_quantity('friction', 0.03)
-        domain.set_quantity('xmomentum', 3.0)
-        domain.set_quantity('ymomentum', 4.0)
-
-        ######################
-        # Boundary conditions
-        B = anuga.Transmissive_boundary(domain)
-        domain.set_boundary( {'exterior': B})
-
-        # This call mangles the stage values.
-        domain.distribute_to_vertices_and_edges()
-        domain.set_quantity('stage', 1.0)
-
-
-        domain.set_name('datatest' + str(time.time()))
-        domain.smooth = True
-        domain.reduction = mean
-
-        sww = SWW_file(domain)
-        sww.store_connectivity()
-        sww.store_timestep()
-        domain.set_quantity('stage', 10.0) # This is automatically limited
-        # So it will not be less than the elevation
-        domain.time = 2.
-        sww.store_timestep()
-
-        # Test the function
-        points = [[5.0,1.],[0.5,2.]]
-        depth_file = tempfile.mktemp(".csv") 
-        velocity_x_file = tempfile.mktemp(".csv") 
-        velocity_y_file = tempfile.mktemp(".csv") 
-        interpolate_sww2csv(sww.filename, points, depth_file,
-                            velocity_x_file,
-                            velocity_y_file,
-                            verbose=False)
-
-        depth_answers_array = [[0.0, 6.0, 1.5], [2.0, 15., 10.5]] 
-        velocity_x_answers_array = [[0.0, 3./6.0, 3./1.5],
-                                    [2.0, 3./15., 3/10.5]]
-        velocity_y_answers_array = [[0.0, 4./6.0, 4./1.5],
-                                    [2.0, 4./15., 4./10.5]]
-        depth_file_handle = file(depth_file)
-        depth_reader = csv.reader(depth_file_handle)
-        depth_reader.next()
-        velocity_x_file_handle = file(velocity_x_file)
-        velocity_x_reader = csv.reader(velocity_x_file_handle)
-        velocity_x_reader.next()
-        for depths, velocitys, depth_answers, velocity_answers in map(None,
-                                              depth_reader,
-                                              velocity_x_reader,
-                                              depth_answers_array,
-                                              velocity_x_answers_array):
-            for i in range(len(depths)):
-                #print "depths",depths[i] 
-                #print "depth_answers",depth_answers[i]
-                #print "velocitys",velocitys[i] 
-                #print "velocity_answers_array", velocity_answers[i]
-                msg = 'Interpolation failed'
-                assert num.allclose(float(depths[i]), depth_answers[i]), msg
-                assert num.allclose(float(velocitys[i]), velocity_answers[i]), msg
-
-        velocity_y_file_handle = file(velocity_y_file)
-        velocity_y_reader = csv.reader(velocity_y_file_handle)
-        velocity_y_reader.next()
-        for velocitys, velocity_answers in map(None,
-                                              velocity_y_reader,
-                                              velocity_y_answers_array):
-            for i in range(len(depths)):
-                #print "depths",depths[i] 
-                #print "depth_answers",depth_answers[i]
-                #print "velocitys",velocitys[i] 
-                #print "velocity_answers_array", velocity_answers[i]
-                msg = 'Interpolation failed'
-                assert num.allclose(float(depths[i]), depth_answers[i]), msg
-                assert num.allclose(float(velocitys[i]), velocity_answers[i]), msg
-                
-        # clean up
-        depth_file_handle.close()
-        velocity_y_file_handle.close()
-        velocity_x_file_handle.close()
-        #print "sww.filename",sww.filename 
-        os.remove(sww.filename)
-        os.remove(depth_file)
-        os.remove(velocity_x_file)
-        os.remove(velocity_y_file)
-
-        
-    def test_interpolate_one_point_many_triangles(self):
-        z0 = [2.0, 5.0]
-
-        v0 = [0.0, 0.0]
-        v1 = [1.0, 0.0]
-        v2 = [2.0, 0.0]
-        v3 = [3.0, 0.0]
-        v4 = [4.0, 0.0]
-        v5 = [5.0, 0.0]
-        v6 = [6.0, 0.0]
-        v7 = [0.0, 10.0]
-        v8 = [1.0, 10.0]
-        v9 = [2.0, 10.0]
-        v10= [3.0, 10.0]
-        v11= [4.0, 10.0]
-        v12= [5.0, 10.0]
-        v13= [6.0, 10.0]
-        
-        vertices = [z0,v0, v1, v2, v3,v4 ,v5, v6, v7, v8, v9, v10, v11,
-                    v12, v13]
-        triangles = [
-                      [0,1,2],
-                      [0,2,3],
-                      [0,3,4],
-                      [0,4,5],
-                      [0,5,6],
-                      [0,6,7],
-                      [0,9,8],
-                      [0,10,9],
-                      [0,11,10],
-                      [0,12,11],
-                      [0,13,12],
-                      [0,14,13]
-                      ]
-
-        d0 = [1.0, 1.0]
-        d1 = [1.0, 2.0]
-        d2 = [3.0, 1.0]
-        point_coords = [ d0, d1, d2]
-        try:
-            interp = Interpolate(vertices, triangles)
-        except RuntimeError:
-            self.failUnless(0 ==1,  'quad fails with 14 verts at the same \
-            position. Should be able to handle any number.')
-        f = linear_function(vertices)
-        z = interp.interpolate(f, point_coords)
-        answer = linear_function(point_coords)
-
-        #print "z",z 
-        #print "answer",answer 
-        assert num.allclose(z, answer)
-
-################################################################################
-
-if __name__ == "__main__":
-    suite = unittest.makeSuite(Test_Interpolate,'test')
-    runner = unittest.TextTestRunner() #verbosity=1)
-    runner.run(suite)
-
Index: unk/anuga_core/source/anuga/fit_interpolate/test_interpolate2d.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test_interpolate2d.py	(revision 9478)
+++ 	(revision )
@@ -1,469 +1,0 @@
-"""Test suite for interpolate2d.py
-
-Ole Nielsen, 2011
-"""
-
-__author__ = 'Ole Nielsen <ole.moller.nielsen@gmail.com>'
-__revision__ = '$Format:%H$'
-__date__ = '01/11/2011'
-__license__ = 'GPL v3'
-__copyright__ = 'Copyright 2012, Australia Indonesia Facility for '
-__copyright__ += 'Disaster Reduction'
-
-import unittest
-import numpy
-
-from interpolate2d import interpolate2d
-
-
-# Auxiliary functions for testing
-def nanallclose(x, y, rtol=1.0e-5, atol=1.0e-8):
-    """Numpy allclose function which allows NaN
-
-    Args:
-        * x, y: Either scalars or numpy arrays
-
-    Returns:
-        * True or False
-
-    Note:
-        Returns True if all non-nan elements pass.
-    """
-
-    xn = numpy.isnan(x)
-    yn = numpy.isnan(y)
-    if numpy.any(xn != yn):
-        # Presence of NaNs is not the same in x and y
-        return False
-
-    if numpy.all(xn):
-        # Everything is NaN.
-        # This will also take care of x and y being NaN scalars
-        return True
-
-    # Filter NaN's out
-    if numpy.any(xn):
-        x = x[-xn]
-        y = y[-yn]
-
-    # Compare non NaN's and return
-    return numpy.allclose(x, y, rtol=rtol, atol=atol)
-
-
-def axes2points(x, y):
-    """Generate all combinations of grid point coordinates from x and y axes
-
-    Args:
-        * x: x coordinates (array)
-        * y: y coordinates (array)
-
-    Returns:
-        * P: Nx2 array consisting of coordinates for all
-             grid points defined by x and y axes. The x coordinate
-             will vary the fastest to match the way 2D numpy
-             arrays are laid out by default ('C' order). That way,
-             the x and y coordinates will match a corresponding
-             2D array A when flattened (A.flat[:] or A.reshape(-1))
-
-    Note:
-        Example
-
-        x = [1, 2, 3]
-        y = [10, 20]
-
-        P = [[1, 10],
-             [2, 10],
-             [3, 10],
-             [1, 20],
-             [2, 20],
-             [3, 20]]
-    """
-
-    # Reverse y coordinates to have them start at bottom of array
-    y = numpy.flipud(y)
-
-    # Repeat x coordinates for each y (fastest varying)
-    X = numpy.kron(numpy.ones(len(y)), x)
-
-    # Repeat y coordinates for each x (slowest varying)
-    Y = numpy.kron(y, numpy.ones(len(x)))
-
-    # Check
-    N = len(X)
-    assert len(Y) == N
-
-    # Create Nx2 array of x and y coordinates
-    X = numpy.reshape(X, (N, 1))
-    Y = numpy.reshape(Y, (N, 1))
-    P = numpy.concatenate((X, Y), axis=1)
-
-    # Return
-    return P
-
-
-def linear_function(x, y):
-    """Auxiliary function for use with interpolation test
-    """
-
-    return x + y / 2.0
-
-
-class Test_interpolate(unittest.TestCase):
-
-    def test_linear_interpolation_basic(self):
-        """Interpolation library works for linear function - basic test
-        """
-
-        # Define pixel centers along each direction
-        x = [1.0, 2.0, 4.0]
-        y = [5.0, 9.0]
-
-        # Define ny by nx array with corresponding values
-        A = numpy.zeros((len(x), len(y)))
-
-        # Define values for each x, y pair as a linear function
-        for i in range(len(x)):
-            for j in range(len(y)):
-                A[i, j] = linear_function(x[i], y[j])
-
-        # Test first that original points are reproduced correctly
-        for i, xi in enumerate(x):
-            for j, eta in enumerate(y):
-                val = interpolate2d(x, y, A, [(xi, eta)], mode='linear')[0]
-                ref = linear_function(xi, eta)
-                assert numpy.allclose(val, ref, rtol=1e-12, atol=1e-12)
-
-        # Then test that genuinly interpolated points are correct
-        xis = numpy.linspace(x[0], x[-1], 10)
-        etas = numpy.linspace(y[0], y[-1], 10)
-        points = axes2points(xis, etas)
-
-        vals = interpolate2d(x, y, A, points, mode='linear')
-        refs = linear_function(points[:, 0], points[:, 1])
-        assert numpy.allclose(vals, refs, rtol=1e-12, atol=1e-12)
-
-    def test_constant_interpolation_basic(self):
-        """Interpolation library works for piecewise constant function
-        """
-
-        # Define pixel centers along each direction
-        x = numpy.array([1.0, 2.0, 4.0])
-        y = numpy.array([5.0, 9.0])
-
-        # Define ny by nx array with corresponding values
-        A = numpy.zeros((len(x), len(y)))
-
-        # Define values for each x, y pair as a linear function
-        for i in range(len(x)):
-            for j in range(len(y)):
-                A[i, j] = linear_function(x[i], y[j])
-
-        # Then test that interpolated points are always assigned value of
-        # closest neighbour
-        xis = numpy.linspace(x[0], x[-1], 10)
-        etas = numpy.linspace(y[0], y[-1], 10)
-        points = axes2points(xis, etas)
-
-        vals = interpolate2d(x, y, A, points, mode='constant')
-
-        # Find upper neighbours for each interpolation point
-        xi = points[:, 0]
-        eta = points[:, 1]
-        idx = numpy.searchsorted(x, xi, side='left')
-        idy = numpy.searchsorted(y, eta, side='left')
-
-        # Get the four neighbours for each interpolation point
-        x0 = x[idx - 1]
-        x1 = x[idx]
-        y0 = y[idy - 1]
-        y1 = y[idy]
-
-        z00 = A[idx - 1, idy - 1]
-        z01 = A[idx - 1, idy]
-        z10 = A[idx, idy - 1]
-        z11 = A[idx, idy]
-
-        # Location coefficients
-        alpha = (xi - x0) / (x1 - x0)
-        beta = (eta - y0) / (y1 - y0)
-
-        refs = numpy.zeros(len(vals))
-        for i in range(len(refs)):
-            if alpha[i] < 0.5 and beta[i] < 0.5:
-                refs[i] = z00[i]
-
-            if alpha[i] >= 0.5 and beta[i] < 0.5:
-                refs[i] = z10[i]
-
-            if alpha[i] < 0.5 and beta[i] >= 0.5:
-                refs[i] = z01[i]
-
-            if alpha[i] >= 0.5 and beta[i] >= 0.5:
-                refs[i] = z11[i]
-
-        assert numpy.allclose(vals, refs, rtol=1e-12, atol=1e-12)
-
-    def test_linear_interpolation_range(self):
-        """Interpolation library works for linear function - a range of cases
-        """
-
-        for x in [[1.0, 2.0, 4.0], [-20, -19, 0], numpy.arange(200) + 1000]:
-            for y in [[5.0, 9.0], [100, 200, 10000]]:
-
-                # Define ny by nx array with corresponding values
-                A = numpy.zeros((len(x), len(y)))
-
-                # Define values for each x, y pair as a linear function
-                for i in range(len(x)):
-                    for j in range(len(y)):
-                        A[i, j] = linear_function(x[i], y[j])
-
-                # Test that linearly interpolated points are correct
-                xis = numpy.linspace(x[0], x[-1], 100)
-                etas = numpy.linspace(y[0], y[-1], 100)
-                points = axes2points(xis, etas)
-
-                vals = interpolate2d(x, y, A, points, mode='linear')
-                refs = linear_function(points[:, 0], points[:, 1])
-                assert numpy.allclose(vals, refs, rtol=1e-12, atol=1e-12)
-
-    def test_linear_interpolation_nan_points(self):
-        """Interpolation library works with interpolation points being NaN
-
-        This is was the reason for bug reported in:
-        https://github.com/AIFDR/riab/issues/155
-        """
-
-        # Define pixel centers along each direction
-        x = [1.0, 2.0, 4.0]
-        y = [5.0, 9.0]
-
-        # Define ny by nx array with corresponding values
-        A = numpy.zeros((len(x), len(y)))
-
-        # Define values for each x, y pair as a linear function
-        for i in range(len(x)):
-            for j in range(len(y)):
-                A[i, j] = linear_function(x[i], y[j])
-
-        # Then test that interpolated points can contain NaN
-        xis = numpy.linspace(x[0], x[-1], 10)
-        etas = numpy.linspace(y[0], y[-1], 10)
-        
-        xis[6:7] = numpy.nan
-        etas[3] = numpy.nan
-        points = axes2points(xis, etas)
-        
-        vals = interpolate2d(x, y, A, points, mode='linear')
-        refs = linear_function(points[:, 0], points[:, 1])
-        
-        assert nanallclose(vals, refs, rtol=1e-12, atol=1e-12)
-
-    def test_linear_interpolation_nan_array(self):
-        """Interpolation library works (linear mode) with grid points being NaN
-        """
-
-        # Define pixel centers along each direction
-        x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
-        y = [4.0, 5.0, 7.0, 9.0, 11.0, 13.0]
-
-        # Define ny by nx array with corresponding values
-        A = numpy.zeros((len(x), len(y)))
-
-        # Define values for each x, y pair as a linear function
-        for i in range(len(x)):
-            for j in range(len(y)):
-                A[i, j] = linear_function(x[i], y[j])
-        A[2, 3] = numpy.nan  # (x=2.0, y=9.0): NaN
-
-        # Then test that interpolated points can contain NaN
-        xis = numpy.linspace(x[0], x[-1], 12)
-        etas = numpy.linspace(y[0], y[-1], 10)
-        points = axes2points(xis, etas)
-
-
-        vals = interpolate2d(x, y, A, points, mode='linear')
-        refs = linear_function(points[:, 0], points[:, 1])
-
-        # Set reference result with expected NaNs and compare
-        for i, (xi, eta) in enumerate(points):
-            if (1.0 < xi <= 3.0) and (7.0 < eta <= 11.0):
-                refs[i] = numpy.nan
-
-        assert nanallclose(vals, refs, rtol=1e-12, atol=1e-12)
-
-    def test_interpolation_random_array_and_nan(self):
-        """Interpolation library (constant and linear) works with NaN
-        """
-
-        # Define pixel centers along each direction
-        x = numpy.arange(20) * 1.0
-        y = numpy.arange(25) * 1.0
-
-        # Define ny by nx array with corresponding values
-        A = numpy.zeros((len(x), len(y)))
-
-        # Define arbitrary values for each x, y pair
-        numpy.random.seed(17)
-        A = numpy.random.random((len(x), len(y))) * 10
-
-        # Create islands of NaN
-        A[5, 13] = numpy.nan
-        A[6, 14] = A[6, 18] = numpy.nan
-        A[7, 14:18] = numpy.nan
-        A[8, 13:18] = numpy.nan
-        A[9, 12:19] = numpy.nan
-        A[10, 14:17] = numpy.nan
-        A[11, 15] = numpy.nan
-
-        A[15, 5:6] = numpy.nan
-
-        # Creat interpolation points
-        xis = numpy.linspace(x[0], x[-1], 39)   # Hit all mid points
-        etas = numpy.linspace(y[0], y[-1], 73)  # Hit thirds
-        points = axes2points(xis, etas)
-
-        for mode in ['linear', 'constant']:
-            vals = interpolate2d(x, y, A, points, mode=mode)
-
-            # Calculate reference result with expected NaNs and compare
-            i = j = 0
-            for k, (xi, eta) in enumerate(points):
-
-                # Find indices of nearest higher value in x and y
-                i = numpy.searchsorted(x, xi)
-                j = numpy.searchsorted(y, eta)
-
-                if i > 0 and j > 0:
-
-                    # Get four neigbours
-                    A00 = A[i - 1, j - 1]
-                    A01 = A[i - 1, j]
-                    A10 = A[i, j - 1]
-                    A11 = A[i, j]
-
-                    if numpy.allclose(xi, x[i]):
-                        alpha = 1.0
-                    else:
-                        alpha = 0.5
-
-                    if numpy.allclose(eta, y[j]):
-                        beta = 1.0
-                    else:
-                        beta = eta - y[j - 1]
-
-                    if mode == 'linear':
-                        if numpy.any(numpy.isnan([A00, A01, A10, A11])):
-                            ref = numpy.nan
-                        else:
-                            ref = (A00 * (1 - alpha) * (1 - beta) +
-                                   A01 * (1 - alpha) * beta +
-                                   A10 * alpha * (1 - beta) +
-                                   A11 * alpha * beta)
-                    elif mode == 'constant':
-                        assert alpha >= 0.5  # Only case in this test
-
-                        if beta < 0.5:
-                            ref = A10
-                        else:
-                            ref = A11
-                    else:
-                        msg = 'Unknown mode: %s' % mode
-                        raise Exception(msg)
-
-                    #print i, j, xi, eta, alpha, beta, vals[k], ref
-                    assert nanallclose(vals[k], ref, rtol=1e-12, atol=1e-12)
-
-    def test_linear_interpolation_outside_domain(self):
-        """Interpolation library sensibly handles values outside the domain
-        """
-
-        # Define pixel centers along each direction
-        x = [1.0, 2.0, 4.0]
-        y = [5.0, 9.0]
-
-        # Define ny by nx array with corresponding values
-        A = numpy.zeros((len(x), len(y)))
-
-        # Define values for each x, y pair as a linear function
-        for i in range(len(x)):
-            for j in range(len(y)):
-                A[i, j] = linear_function(x[i], y[j])
-
-        # Simple example first for debugging
-        xis = numpy.linspace(0.9, 4.0, 4)
-        etas = numpy.linspace(5, 9.1, 3)
-        points = axes2points(xis, etas)
-        refs = linear_function(points[:, 0], points[:, 1])
-
-        vals = interpolate2d(x, y, A, points, mode='linear',
-                             bounds_error=False)
-        msg = ('Length of interpolation points %i differs from length '
-               'of interpolated values %i' % (len(points), len(vals)))
-        assert len(points) == len(vals), msg
-        for i, (xi, eta) in enumerate(points):
-            if xi < x[0] or xi > x[-1] or eta < y[0] or eta > y[-1]:
-                assert numpy.isnan(vals[i])
-            else:
-                msg = ('Got %.15f for (%f, %f), expected %.15f'
-                       % (vals[i], xi, eta, refs[i]))
-                assert numpy.allclose(vals[i], refs[i],
-                                      rtol=1.0e-12, atol=1.0e-12), msg
-
-        # Try a range of combinations of points outside domain
-        # with error_bounds True
-        for lox in [x[0], x[0] - 1]:
-            for hix in [x[-1], x[-1] + 1]:
-                for loy in [y[0], y[0] - 1]:
-                    for hiy in [y[-1], y[-1] + 1]:
-
-                        # Then test that points outside domain can be handled
-                        xis = numpy.linspace(lox, hix, 4)
-                        etas = numpy.linspace(loy, hiy, 4)
-                        points = axes2points(xis, etas)
-
-                        if lox < x[0] or hix > x[-1] or \
-                                loy < x[0] or hiy > y[-1]:
-                            try:
-                                vals = interpolate2d(x, y, A, points,
-                                                     mode='linear',
-                                                     bounds_error=True)
-                            except Exception, e:
-                                pass
-                            else:
-                                msg = 'Should have raise bounds error'
-                                raise Exception(msg)
-
-        # Try a range of combinations of points outside domain with
-        # error_bounds False
-        for lox in [x[0], x[0] - 1, x[0] - 10]:
-            for hix in [x[-1], x[-1] + 1, x[-1] + 5]:
-                for loy in [y[0], y[0] - 1, y[0] - 10]:
-                    for hiy in [y[-1], y[-1] + 1, y[-1] + 10]:
-
-                        # Then test that points outside domain can be handled
-                        xis = numpy.linspace(lox, hix, 10)
-                        etas = numpy.linspace(loy, hiy, 10)
-                        points = axes2points(xis, etas)
-                        refs = linear_function(points[:, 0], points[:, 1])
-                        vals = interpolate2d(x, y, A, points,
-                                             mode='linear', bounds_error=False)
-
-                        assert len(points) == len(vals), msg
-                        for i, (xi, eta) in enumerate(points):
-                            if xi < x[0] or xi > x[-1] or\
-                                    eta < y[0] or eta > y[-1]:
-                                msg = 'Expected NaN for %f, %f' % (xi, eta)
-                                assert numpy.isnan(vals[i]), msg
-                            else:
-                                msg = ('Got %.15f for (%f, %f), expected '
-                                       '%.15f' % (vals[i], xi, eta, refs[i]))
-                                assert numpy.allclose(vals[i], refs[i],
-                                                      rtol=1.0e-12,
-                                                      atol=1.0e-12), msg
-
-
-if __name__ == '__main__':
-    suite = unittest.makeSuite(Test_interpolate, 'test')
-    runner = unittest.TextTestRunner(verbosity=2)
-    runner.run(suite)
Index: unk/anuga_core/source/anuga/fit_interpolate/test_search_functions.py
===================================================================
--- /trunk/anuga_core/source/anuga/fit_interpolate/test_search_functions.py	(revision 9478)
+++ 	(revision )
@@ -1,236 +1,0 @@
-#!/usr/bin/env python
-
-
-import unittest
-from anuga.pmesh.mesh_quadtree import MeshQuadtree
-
-from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
-from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular
-from anuga.geometry.quad import Cell
-from anuga.geometry.aabb import AABB
-from anuga.utilities.numerical_tools import ensure_numeric
-from anuga.geometry.polygon import is_inside_polygon, is_inside_triangle    
-
-import numpy as num
-
-class Test_search_functions(unittest.TestCase):
-    def setUp(self):
-        pass
-
-    def tearDown(self):
-        pass
-
-    def NOtest_that_C_extension_compiles(self):
-        FN = 'search_functions_ext.c'
-        try:
-            import search_functions_ext
-        except:
-            from compile import compile
-
-            try:
-                compile(FN)
-            except:
-                raise Exception('Could not compile %s' % FN)
-            else:
-                import search_functions_ext
-
-
-
-    def test_off_and_boundary(self):
-        """test_off: Test a point off the mesh
-        """
-
-        points, vertices, boundary = rectangular(1, 1, 1, 1)
-        mesh = Mesh(points, vertices, boundary)
-
-        #Test that points are arranged in a counter clock wise order
-        mesh.check_integrity()
-
-        root = MeshQuadtree(mesh)
-        root.set_last_triangle()
-
-        found, s0, s1, s2, k = root.search_fast([-0.2, 10.7])
-        assert found is False
-
-        found, s0, s1, s2, k = root.search_fast([0, 0])
-        assert found is True
-        
-        
-    def test_small(self):
-        """test_small: Two triangles
-        """
-
-        points, vertices, boundary = rectangular(1, 1, 1, 1)
-        mesh = Mesh(points, vertices, boundary)
-
-        #Test that points are arranged in a counter clock wise order
-        mesh.check_integrity()
-
-        root = MeshQuadtree(mesh)
-        root.set_last_triangle()
-
-        x = [0.2, 0.7]
-        found, s0, s1, s2, k = root.search_fast(x)
-        assert k == 1 # Triangle one
-        assert found is True        
-        
-    def test_bigger(self):
-        """test_bigger
-        
-        test larger mesh
-        """
-
-        points, vertices, boundary = rectangular(4, 4, 1, 1)
-        mesh = Mesh(points, vertices, boundary)
-
-        #Test that points are arranged in a counter clock wise order
-        mesh.check_integrity()
-
-        root = MeshQuadtree(mesh)
-        root.set_last_triangle()
-
-        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
-                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
-                  [10, 3]]:
-            
-            found, s0, s1, s2, k = root.search_fast(ensure_numeric(x))                                   
-                                         
-            if k >= 0:
-                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
-                assert is_inside_polygon(x, V)
-                assert found is True
-                #print k, x
-            else:
-                assert found is False                
-
-        
-
-    def test_large(self):
-        """test_larger mesh and different quad trees
-        """
-
-        points, vertices, boundary = rectangular(10, 12, 1, 1)
-        mesh = Mesh(points, vertices, boundary)
-
-        #Test that points are arranged in a counter clock wise order
-        mesh.check_integrity()
-
-        
-
-        root = MeshQuadtree(mesh)
-        root.set_last_triangle()
-        #print m, root.show()
-
-        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
-                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
-                  [10, 3]]:
-            
-            found, s0, s1, s2, k = root.search_fast(x)
-
-            if k >= 0:
-                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
-                assert is_inside_triangle(x, V, closed=True)
-                assert is_inside_polygon(x, V)
-                assert found is True
-            else:
-                assert found is False                
-
-        
-            if k == 0: return    
-    # NOTE PADARN: This function is no longer exposed
-    # have passed this test - but could expose
-    # c function if deemed neccesary.
-    def test_underlying_function(self):
-        """test_larger mesh and different quad trees
-        """
-        return
-        points, vertices, boundary = rectangular(2, 2, 1, 1)
-        mesh = Mesh(points, vertices, boundary)
-
-        root = MeshQuadtree(mesh)
-        root.set_last_triangle()
-
-        # One point
-        x = ensure_numeric([0.5, 0.5])
-
-        found, sigma0, sigma1, sigma2, k = \
-               root._search_triangles_of_vertices(root.search(x), x)
-
-        if k >= 0:
-            V = mesh.get_vertex_coordinates(k) # nodes for triangle k
-            assert is_inside_polygon(x, V)
-            assert found is True
-        else:
-            assert found is False                
-
-        
-
-        # More points    
-        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
-                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
-                  [10, 3]]:
-                
-            triangles = root.search(x)
-
-            #print x, candidate_vertices
-            found, sigma0, sigma1, sigma2, k = \
-                   root._search_triangles_of_vertices(triangles,
-                                                 ensure_numeric(x))
-            if k >= 0:
-                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
-                assert is_inside_polygon(x, V)
-                assert found is True
-            else:
-                assert found is False
-
-                
-
-    def expanding_search(self):
-        """test_larger mesh and different quad trees
-        """
-        
-        p0 = [2,1]
-        p1 = [4,1]
-        p2 = [4.,4]
-        p3 = [2,4]
-        p4 = [5,4]
-
-        p5 = [-1,-1]
-        p6 = [1,-1]
-        p7 = [1,1]
-        p8 = [-1,1]
-
-        points = [p0,p1,p2, p3,p4,p5,p6,p7,p8]
-        #
-        vertices = [[0,1,2],[0,2,3],[1,4,2],[5,6,7], [5,7,8]]
-        mesh = Mesh(points, vertices)
-
-        # Don't do this, want to control the max and mins
-        #root = build_quadtree(mesh, max_points_per_cell=4)
-    
-
-        root = Cell(-3, 9, -3, 9,
-                    max_points_per_cell = 4)
-        #Insert indices of all vertices
-        root.insert( range(mesh.number_of_nodes) )
-
-        #Build quad tree and return
-        root.split()
-        
-        # One point
-        #x = [3.5, 1.5]
-        x = [2.5, 1.5]
-        element_found, sigma0, sigma1, sigma2, k = root.search_fast(x)
-        # One point
-        x = [3.00005, 2.999994]
-        element_found, sigma0, sigma1, sigma2, k = root.search_fast(x)
-        assert element_found is True
-        assert k == 1
-        
-
-#-------------------------------------------------------------
-if __name__ == "__main__":
-    suite = unittest.makeSuite(Test_search_functions, 'test')
-    runner = unittest.TextTestRunner(verbosity=1)
-    runner.run(suite)
-    
