source: anuga_core/source/anuga/pyvolution/test_combine_pts.py @ 3546

Last change on this file since 3546 was 3514, checked in by duncan, 19 years ago

Hi all,
I'm doing a change in the anuga structure, moving the code to

\anuga_core\source\anuga

After you have done an svn update, the PYTHONPATH has to be changed to;
PYTHONPATH = anuga_core/source/

This is part of changes required to make installation of anuga quicker and reducing the size of our sandpits.

If any imports are broken, try fixing them. With adding anuga. to them for example. If this seems to have really broken things, email/phone me.

Cheers
Duncan

File size: 9.4 KB
Line 
1#!/usr/bin/env python
2
3#TEST
4
5import unittest
6from math import sqrt
7
8
9#from least_squares import *
10from Numeric import allclose, array, transpose
11
12from anuga.coordinate_transforms.geo_reference import Geo_reference
13from combine_pts import *
14from load_mesh.loadASCII import import_points_file
15#from anuga.geospatial_data.geospatial_data import import_points_file
16
17class Test_combine_pts(unittest.TestCase):
18
19    def setUp(self):
20        pass
21
22    def tearDown(self):
23        pass
24
25
26    def test_combine_rectangular_points_files(self):
27        from load_mesh.loadASCII import export_points_file
28        import tempfile
29        import os
30
31
32        # create a fine .pts file
33        fine_dict = {}
34        fine_dict['pointlist']=[[0,1],[0,4],[4,4],[4,1]]
35        att_dict = {}
36        att_dict['elevation'] = [10,40,80,50]
37        att_dict['sonic'] = [1,4,8,5]
38        fine_dict['attributelist'] = att_dict
39        fine_dict['geo_reference'] = Geo_reference(56,2.0,1.0)
40       
41        fine_file = tempfile.mktemp('.pts')
42        export_points_file(fine_file, fine_dict)
43
44
45        # create a coarse .pts file
46        coarse_dict = {}
47        coarse_dict['pointlist']=[[0,1],[2,1],[0,-2]]
48        att_dict = {}
49        att_dict['elevation'] = [10,30,-20]
50        att_dict['sonic'] = [1,3,-2]
51        coarse_dict['attributelist'] = att_dict
52        coarse_dict['geo_reference'] = Geo_reference(56,4.0,3.0)
53       
54        coarse_file = tempfile.mktemp('.pts')
55        export_points_file(coarse_file, coarse_dict)
56       
57        out_file = tempfile.mktemp('.pts')
58
59        combine_rectangular_points_files(fine_file,coarse_file,out_file)
60
61        #clean up
62        os.remove(fine_file)
63        os.remove(coarse_file)
64
65        results = import_points_file(out_file,
66                                     delimiter = ',')
67        answer = [[2.0, 0.0],
68                  [0.0, 1.0],
69                  [0.0, 4.0],
70                  [4.0, 4.0],
71                  [4.0, 1.0]]
72        #print "results",results
73        #print "answer",answer
74       
75        self.failUnless(len(results['pointlist']) == len(answer),
76                        'final number of points wrong. failed.')
77       
78        assert allclose(results['pointlist'], answer)
79        assert allclose(results['attributelist']['sonic'], [ -2.,
80                                                             1.,  4.,
81                                                             8.,  5.])
82        assert allclose(results['attributelist']['elevation'],[ -20.,
83                                                               10.,  40.,
84                                                               80.,  50.])
85       
86        self.failUnless(results['geo_reference'] == fine_dict['geo_reference'],
87                        ' failed.')
88        #clean up
89        os.remove(out_file)
90
91    def test_combine_rectangular_points_filesII(self):
92        from load_mesh.loadASCII import export_points_file
93        import tempfile
94        import os
95
96        # create a fine .pts file
97        fine_dict = {}
98        fine_dict['pointlist']=[[0,1],[0,4],[4,4],[4,1],[3,1],[2,2],[1,3],[3,4]]
99        att_dict = {}
100        fine_dict['attributelist'] = att_dict
101        fine_dict['geo_reference'] = Geo_reference(56,2.0,1.0)
102       
103        fine_file = tempfile.mktemp('.pts')
104        export_points_file(fine_file, fine_dict)
105
106
107        # create a coarse .pts file
108        coarse_dict = {}
109        coarse_dict['pointlist']=[[0,1],[0,0],[0.5,0.5],[1,1],
110                                  [1.5,1.5],[2,1],[0,-2],[100,10],
111                                  [-20,4],[-50,5],[60,70]]
112        att_dict = {}
113        coarse_dict['attributelist'] = att_dict
114        coarse_dict['geo_reference'] = Geo_reference(56,4.0,3.0)
115       
116        coarse_file = tempfile.mktemp('.pts')
117        export_points_file(coarse_file, coarse_dict)
118       
119        out_file = tempfile.mktemp('.pts')
120
121        combine_rectangular_points_files(fine_file,coarse_file,out_file)
122
123        #clean up
124        os.remove(fine_file)
125        os.remove(coarse_file)
126
127        results = import_points_file(out_file,
128                                  delimiter = ',')
129        answer = [[2.0, 0.0],
130                  [102.,12.],
131                  [-18.,6.],
132                  [-48.,7.],
133                  [62.,72.],
134                  [0.0, 1.0],
135                  [0.0, 4.0],
136                  [4.0, 4.0],
137                  [4.0, 1.0],
138                  [3.,1.],
139                  [2.,2.],
140                  [1.,3.],
141                  [3.,4.]]
142        #print "results",results['pointlist']
143        #print "answer",answer
144        #print "len(results['pointlist']",len(results['pointlist'])
145        #print "len(answer)",len(answer)
146       
147        self.failUnless(len(results['pointlist']) == len(answer),
148                         'final number of points wrong. failed.')
149        assert allclose(results['pointlist'], answer)
150       
151        self.failUnless(results['geo_reference'] == fine_dict['geo_reference'],
152                        ' failed.')
153        #clean up
154        os.remove(out_file)
155
156    def test_combine_rectangular_points_files_errors(self):
157        from load_mesh.loadASCII import export_points_file
158        import tempfile
159        import os
160
161        # create a fine .pts file
162        fine_dict = {}
163        fine_dict['pointlist']=[[0,1],[0,4],[4,4],[4,1]]
164        att_dict = {}
165        att_dict['elevation'] = [1,4,8,5]
166        att_dict['pneumonic'] = [1,4,8,5]
167        fine_dict['attributelist'] = att_dict
168        fine_dict['geo_reference'] = Geo_reference(56,2.0,1.0)
169       
170        fine_file = tempfile.mktemp('.pts')
171        export_points_file(fine_file, fine_dict)
172
173
174        # create a coarse .pts file
175        coarse_dict = {}
176        coarse_dict['pointlist']=[[0,1],[2,1],[0,-2]]
177        att_dict = {}
178        att_dict['elevation'] = [1,3,-2]
179        att_dict['sonic'] = [1,3,-2]
180        coarse_dict['attributelist'] = att_dict
181        coarse_dict['geo_reference'] = Geo_reference(56,4.0,3.0)
182       
183        coarse_file = tempfile.mktemp('.pts')
184        export_points_file(coarse_file, coarse_dict)
185       
186        out_file = tempfile.mktemp('.pts')
187        try:
188            combine_rectangular_points_files(fine_file,coarse_file,out_file)
189        except AttributeError:
190            pass
191        else:
192            self.failUnless(0 == 1,
193                            'bad pts files did not raise error!')
194        #clean up
195        os.remove(fine_file)
196        os.remove(coarse_file)
197     
198    def test_reduce_points_to_mesh_extent(self):
199        from load_mesh.loadASCII import export_points_file, export_mesh_file
200        import tempfile
201        import os
202        x_origin = -45435345.
203        y_origin = 433432432.
204        # create a fine .pts file
205        fine_dict = {}
206        fine_dict['pointlist']=[[1.,1.],
207                                [1.,7.],
208                                [7.,1.],
209                                [11.,11.],
210                                [7.,8.],
211                                [8.,8.],
212                                [9.,8.]]
213        att_dict = {}
214        att_dict['elevation'] = [10,40,80,50,78,78,45]
215        att_dict['sonic'] = [1,4,8,5,56,34,213]
216        fine_dict['attributelist'] = att_dict
217        fine_dict['geo_reference'] = Geo_reference(56,x_origin,y_origin)
218       
219        points_file = tempfile.mktemp('.pts')
220        export_points_file(points_file, fine_dict)
221
222
223        # create a coarse .pts file
224        mesh = {}
225        mesh['vertices']=[[0,0],
226                          [0,3],
227                          [3,3],
228                          [3,0],
229                          [1,2]
230                          ]
231        mesh['vertex_attributes']=[[],
232                          [],
233                          [],
234                          [],
235                          []
236                          ]
237        mesh['geo_reference'] = Geo_reference(56,x_origin+7.,y_origin+7.)
238       
239        mesh_file = tempfile.mktemp('.tsh')
240        export_mesh_file(mesh_file, mesh)
241       
242        out_file = tempfile.mktemp('.pts')
243
244        reduce_points_to_mesh_extent(points_file,mesh_file,out_file)
245
246        results = import_points_file(out_file,
247                                  delimiter = ',')
248        answer = [
249            [7.,8.], 
250            [8.,8.],
251            [9.,8.]]
252        #print "results",results['pointlist']
253        #print "answer",answer
254       
255        self.failUnless(len(results['pointlist']) == len(answer),
256                         'final number of points wrong. failed.')
257        assert allclose(results['pointlist'],answer)
258
259        answer = [78., 78.,45.]
260       
261        self.failUnless(len(results['attributelist']['elevation']) == len(answer),
262                         'final number of points wrong. failed.')
263        assert allclose(results['attributelist']['elevation'], answer)
264       
265        #clean up
266        os.remove(points_file)
267        os.remove(mesh_file)
268       
269        #clean up
270        os.remove(out_file)
271
272        #FIXME do test for add points files
273       
274#-------------------------------------------------------------
275if __name__ == "__main__":
276    suite = unittest.makeSuite(Test_combine_pts,'test')
277    #suite = unittest.makeSuite(Test_combine_pts,'test_reduce_points_to_mesh_extent')
278    runner = unittest.TextTestRunner(verbosity=1)
279    runner.run(suite)
280
281
282
283
284
Note: See TracBrowser for help on using the repository browser.