1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | #FIXME (Ole): I think we need a unit test of write_ASCII_triangulation |
---|
5 | # in order to quash bug in merimbula data |
---|
6 | # - to quash the bug need a raised error in pyvolution for reading xya files |
---|
7 | # when the title attribute information is not consistent with the data in the |
---|
8 | # file - DSG |
---|
9 | # also test_export_mesh_file tests write_ASCII_triangulation - DSG |
---|
10 | |
---|
11 | import tempfile |
---|
12 | import unittest |
---|
13 | |
---|
14 | import os |
---|
15 | import tempfile |
---|
16 | |
---|
17 | from os.path import splitext |
---|
18 | |
---|
19 | from Numeric import array, allclose |
---|
20 | |
---|
21 | from loadASCII import * |
---|
22 | from coordinate_transforms.geo_reference import Geo_reference |
---|
23 | |
---|
24 | class loadASCIITestCase(unittest.TestCase): |
---|
25 | def setUp(self): |
---|
26 | self.dict ={} |
---|
27 | self.dict['outline_segments'] = [(0, 1), (1, 2), (0, 2), (0, 3)] |
---|
28 | self.dict['outline_segment_tags'] = ['50', '40', '30', '20'] |
---|
29 | self.dict['holes'] = [(0.2, 0.6)] |
---|
30 | self.dict['point_attributes'] = [[5, 2], [4, 2], [3, 2], [2,2]] |
---|
31 | self.dict['regions'] = [(0.3, 0.3),(0.3, 0.4)] |
---|
32 | self.dict['region_tags'] = ['1.3', 'yeah'] |
---|
33 | self.dict['region_max_areas'] = [36.0,-7.1] |
---|
34 | self.dict['points'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), (1.0, 1.0)] |
---|
35 | self.dict['vertices'] = [(0.0, 0.0), (0.0, 4.0), |
---|
36 | (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] |
---|
37 | self.dict['triangles'] = [(3, 2, 4), (1, 0, 3), |
---|
38 | (3, 4,1), (2, 3, 0)] |
---|
39 | self.dict['segments'] = [(0, 1), (1, 4), (2, 0), |
---|
40 | (0, 3), (4, 2)] |
---|
41 | self.dict['triangle_tags'] = ['1.3', '1.3', |
---|
42 | '1.3', '1.3'] |
---|
43 | self.dict['vertex_attributes'] = [[1.2,2.], [1.2,2.], |
---|
44 | [1.2,2.], [1.2,2.], [1.2,3.]] |
---|
45 | self.dict['triangle_neighbors'] = [[-1, 2, 3], [3, 2, -1], |
---|
46 | [-1, 1, 0], [1, -1, 0]] |
---|
47 | self.dict['segment_tags'] = ['50', '40', '30', '20', '40'] |
---|
48 | self.dict['vertex_attribute_titles'] = ['bed elevation', 'height'] |
---|
49 | self.dict['geo_reference'] = Geo_reference(56,1.9,1.9) |
---|
50 | |
---|
51 | self.sparse_dict ={} |
---|
52 | self.sparse_dict['outline_segments'] = [] |
---|
53 | self.sparse_dict['outline_segment_tags'] = [] |
---|
54 | self.sparse_dict['holes'] = [] |
---|
55 | self.sparse_dict['points'] = [(0.0, 0.0),(9,8)] |
---|
56 | self.sparse_dict['point_attributes'] = [[],[]] # points don't have to have |
---|
57 | # attributes |
---|
58 | self.sparse_dict['regions'] = [] |
---|
59 | self.sparse_dict['region_tags'] = [] |
---|
60 | self.sparse_dict['region_max_areas'] = [] |
---|
61 | |
---|
62 | self.sparse_dict['vertices'] = [] |
---|
63 | self.sparse_dict['triangles'] = [] |
---|
64 | self.sparse_dict['segments'] = [] |
---|
65 | self.sparse_dict['triangle_tags'] = [] |
---|
66 | self.sparse_dict['vertex_attributes'] = [] |
---|
67 | self.sparse_dict['triangle_neighbors'] = [] |
---|
68 | self.sparse_dict['segment_tags'] = [] |
---|
69 | self.sparse_dict['vertex_attribute_titles'] = [] |
---|
70 | |
---|
71 | self.blank_dict ={} |
---|
72 | self.blank_dict['outline_segments'] = [] |
---|
73 | self.blank_dict['outline_segment_tags'] = [] |
---|
74 | self.blank_dict['holes'] = [] |
---|
75 | self.blank_dict['points'] = [] |
---|
76 | self.blank_dict['point_attributes'] = [] |
---|
77 | self.blank_dict['regions'] = [] |
---|
78 | self.blank_dict['region_tags'] = [] |
---|
79 | self.blank_dict['region_max_areas'] = [] |
---|
80 | self.blank_dict['vertices'] = [] |
---|
81 | self.blank_dict['triangles'] = [] |
---|
82 | self.blank_dict['segments'] = [] |
---|
83 | self.blank_dict['triangle_tags'] = [] |
---|
84 | self.blank_dict['vertex_attributes'] = [] |
---|
85 | self.blank_dict['triangle_neighbors'] = [] |
---|
86 | self.blank_dict['segment_tags'] = [] |
---|
87 | self.blank_dict['vertex_attribute_titles'] = [] |
---|
88 | |
---|
89 | self.tri_dict ={} |
---|
90 | self.tri_dict['outline_segments'] = [[0,1]] |
---|
91 | self.tri_dict['outline_segment_tags'] = [''] |
---|
92 | self.tri_dict['holes'] = [] |
---|
93 | self.tri_dict['points'] = [(9,8),(7,8)] |
---|
94 | self.tri_dict['point_attributes'] = [[],[]] |
---|
95 | self.tri_dict['regions'] = [] |
---|
96 | self.tri_dict['region_tags'] = [] |
---|
97 | self.tri_dict['region_max_areas'] = [] |
---|
98 | self.tri_dict['vertices'] = [[9,8],[7,8], [4,5]] |
---|
99 | self.tri_dict['triangles'] = [[0,1,2]] |
---|
100 | self.tri_dict['segments'] = [[0,1]] |
---|
101 | self.tri_dict['triangle_tags'] = [''] |
---|
102 | self.tri_dict['vertex_attributes'] = [[],[],[]] |
---|
103 | self.tri_dict['triangle_neighbors'] = [[0,0,0]] |
---|
104 | self.tri_dict['segment_tags'] = [''] |
---|
105 | self.tri_dict['vertex_attribute_titles'] = [] |
---|
106 | |
---|
107 | self.seg_dict ={} |
---|
108 | self.seg_dict['outline_segments'] = [[0,1]] |
---|
109 | self.seg_dict['outline_segment_tags'] = [''] |
---|
110 | self.seg_dict['holes'] = [] |
---|
111 | self.seg_dict['points'] = [(9,8),(7,8)] |
---|
112 | self.seg_dict['point_attributes'] = [[],[]] |
---|
113 | self.seg_dict['regions'] = [(5,4)] |
---|
114 | self.seg_dict['region_tags'] = [''] |
---|
115 | self.seg_dict['region_max_areas'] = [-999] |
---|
116 | self.seg_dict['vertices'] = [(9,8),(7,8)] |
---|
117 | self.seg_dict['triangles'] = [] |
---|
118 | self.seg_dict['segments'] = [[0,1]] |
---|
119 | self.seg_dict['triangle_tags'] = [] |
---|
120 | self.seg_dict['vertex_attributes'] = [[],[]] |
---|
121 | self.seg_dict['triangle_neighbors'] = [] |
---|
122 | self.seg_dict['segment_tags'] = [''] |
---|
123 | self.seg_dict['vertex_attribute_titles'] = [] |
---|
124 | |
---|
125 | self.reg_dict ={} |
---|
126 | self.reg_dict['outline_segments'] = [[0,1]] |
---|
127 | self.reg_dict['outline_segment_tags'] = [''] |
---|
128 | self.reg_dict['holes'] = [] |
---|
129 | self.reg_dict['points'] = [(9,8),(7,8)] |
---|
130 | self.reg_dict['point_attributes'] = [[],[]] |
---|
131 | self.reg_dict['regions'] = [(5,4)] |
---|
132 | self.reg_dict['region_tags'] = [''] |
---|
133 | self.reg_dict['region_max_areas'] = [] |
---|
134 | self.reg_dict['vertices'] = [(9,8),(7,8)] |
---|
135 | self.reg_dict['triangles'] = [] |
---|
136 | self.reg_dict['segments'] = [[0,1]] |
---|
137 | self.reg_dict['triangle_tags'] = [] |
---|
138 | self.reg_dict['vertex_attributes'] = [[],[]] |
---|
139 | self.reg_dict['triangle_neighbors'] = [] |
---|
140 | self.reg_dict['segment_tags'] = [''] |
---|
141 | self.reg_dict['vertex_attribute_titles'] = [] |
---|
142 | |
---|
143 | self.triangle_tags_dict ={} |
---|
144 | self.triangle_tags_dict['outline_segments'] = [(0, 1), (1, 2), (0, 2), (0, 3)] |
---|
145 | self.triangle_tags_dict['outline_segment_tags'] = ['50', '40', '30', '20'] |
---|
146 | self.triangle_tags_dict['holes'] = [(0.2, 0.6)] |
---|
147 | self.triangle_tags_dict['point_attributes'] = [[5, 2], [4, 2], [3, 2], [2,2]] |
---|
148 | self.triangle_tags_dict['regions'] = [(0.3, 0.3),(0.3, 0.4)] |
---|
149 | self.triangle_tags_dict['region_tags'] = ['1.3', 'yeah'] |
---|
150 | self.triangle_tags_dict['region_max_areas'] = [36.0,-7.1] |
---|
151 | self.triangle_tags_dict['points'] = [(0.0, 0.0), (0.0, 4.0), (4.0, 0.0), (1.0, 1.0)] |
---|
152 | self.triangle_tags_dict['vertices'] = [(0.0, 0.0), (0.0, 4.0), |
---|
153 | (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] |
---|
154 | self.triangle_tags_dict['triangles'] = [(3, 2, 4), (1, 0, 3), |
---|
155 | (3, 4,1), (2, 3, 0)] |
---|
156 | self.triangle_tags_dict['segments'] = [(0, 1), (1, 4), (2, 0), |
---|
157 | (0, 3), (4, 2)] |
---|
158 | self.triangle_tags_dict['triangle_tags'] = ['yeah', '1.3', |
---|
159 | '1.3', ''] |
---|
160 | self.triangle_tags_dict['vertex_attributes'] = [[1.2,2.], [1.2,2.], |
---|
161 | [1.2,2.], [1.2,2.], [1.2,3.]] |
---|
162 | self.triangle_tags_dict['triangle_neighbors'] = [[-1, 2, 3], [3, 2, -1], |
---|
163 | [-1, 1, 0], [1, -1, 0]] |
---|
164 | self.triangle_tags_dict['segment_tags'] = ['50', '40', '30', '20', '40'] |
---|
165 | self.triangle_tags_dict['vertex_attribute_titles'] = ['bed elevation', 'height'] |
---|
166 | self.triangle_tags_dict['geo_reference'] = Geo_reference(56,1.9,1.9) |
---|
167 | |
---|
168 | def tearDown(self): |
---|
169 | pass |
---|
170 | |
---|
171 | |
---|
172 | def test_import_mesh(self): |
---|
173 | |
---|
174 | dict = self.dict |
---|
175 | fileName = tempfile.mktemp(".txt") |
---|
176 | fd = open(fileName,'w') |
---|
177 | write_ASCII_outline(fd,dict) |
---|
178 | fd.close() |
---|
179 | loaded_dict = import_mesh(fileName) #FIXME function names are wacky |
---|
180 | os.remove(fileName) |
---|
181 | |
---|
182 | #gen_dict = |
---|
183 | #print "*********************" |
---|
184 | #print dict |
---|
185 | #print "**loaded_dict*******************" |
---|
186 | #print loaded_dict |
---|
187 | #print "*********************" |
---|
188 | |
---|
189 | |
---|
190 | self.failUnless(array(loaded_dict['points']) == |
---|
191 | array(dict['points']), |
---|
192 | 'test_import_mesh failed. Test 1') |
---|
193 | self.failUnless(array(loaded_dict['point_attributes']) == |
---|
194 | array(dict['point_attributes']), |
---|
195 | 'test_import_mesh failed. Test 2') |
---|
196 | self.failUnless(array(loaded_dict['outline_segments']) == |
---|
197 | array(dict['outline_segments']), |
---|
198 | 'test_import_mesh failed. Test 3') |
---|
199 | self.failUnless(array(loaded_dict['outline_segment_tags']) == |
---|
200 | array(dict['outline_segment_tags']), |
---|
201 | 'test_import_mesh failed. Test 4') |
---|
202 | self.failUnless(array(loaded_dict['regions']) == |
---|
203 | array(dict['regions']), |
---|
204 | 'test_import_mesh failed. Test 5') |
---|
205 | self.failUnless(array(loaded_dict['region_tags']) == |
---|
206 | array(dict['region_tags']), |
---|
207 | 'test_import_mesh failed. Test 5') |
---|
208 | self.failUnless(array(loaded_dict['region_max_areas']) == |
---|
209 | array(dict['region_max_areas']), |
---|
210 | 'test_import_mesh failed. Test 5') |
---|
211 | |
---|
212 | self.failUnless(array(loaded_dict['holes']) == |
---|
213 | array(dict['holes']), |
---|
214 | 'test_import_mesh failed. Test 6') |
---|
215 | |
---|
216 | def test_export_mesh_file(self): |
---|
217 | import os |
---|
218 | import tempfile |
---|
219 | |
---|
220 | meshDict = self.dict |
---|
221 | fileName = tempfile.mktemp(".tsh") |
---|
222 | export_mesh_file(fileName, meshDict) |
---|
223 | loadedDict = import_triangulation(fileName) |
---|
224 | |
---|
225 | #print "*(*( meshDict" |
---|
226 | #print meshDict |
---|
227 | #print "*(*( loadedDcit" |
---|
228 | #print loadedDict |
---|
229 | #print "*(*(" |
---|
230 | |
---|
231 | self.failUnless(array(meshDict['vertices']) == |
---|
232 | array(loadedDict['vertices']), |
---|
233 | 'test_export_mesh_file failed. Test 1') |
---|
234 | self.failUnless(array(meshDict['triangles']) == |
---|
235 | array(loadedDict['triangles']), |
---|
236 | 'test_export_mesh_file failed. Test 2') |
---|
237 | self.failUnless(array(meshDict['segments']) == |
---|
238 | array(loadedDict['segments']), |
---|
239 | 'test_export_mesh_file failed. Test 3') |
---|
240 | self.failUnless(array(meshDict['triangle_tags']) == |
---|
241 | array(loadedDict['triangle_tags']), |
---|
242 | 'test_export_mesh_file failed. Test 4') |
---|
243 | |
---|
244 | self.failUnless(meshDict['vertex_attributes'] == |
---|
245 | loadedDict['vertex_attributes'], |
---|
246 | 'test_export_mesh_file failed. Test 5') |
---|
247 | self.failUnless(array(meshDict['triangle_neighbors']) == |
---|
248 | array(loadedDict['triangle_neighbors']), |
---|
249 | 'test_export_mesh_file failed. Test 6') |
---|
250 | self.failUnless(array(meshDict['segment_tags']) == |
---|
251 | array(loadedDict['segment_tags']), |
---|
252 | 'test_export_mesh_file failed. Test 7') |
---|
253 | self.failUnless(array(meshDict['vertex_attribute_titles']) == |
---|
254 | array(loadedDict['vertex_attribute_titles']), |
---|
255 | 'test_export_mesh_file failed. Test 8') |
---|
256 | self.failUnless(array(meshDict['geo_reference']) == |
---|
257 | array(loadedDict['geo_reference']), |
---|
258 | 'test_export_mesh_file failed. Test 9') |
---|
259 | |
---|
260 | os.remove(fileName) |
---|
261 | |
---|
262 | def test_read_write_msh_file(self): |
---|
263 | dict = self.dict.copy() |
---|
264 | fileName = tempfile.mktemp(".txt") |
---|
265 | write_msh_file(fileName,dict) |
---|
266 | loaded_dict = read_msh_file(fileName) |
---|
267 | os.remove(fileName) |
---|
268 | dict = self.dict |
---|
269 | #print "*********************" |
---|
270 | #print dict |
---|
271 | #print "**loaded_dict*******************" |
---|
272 | #print loaded_dict |
---|
273 | #print "*********************" |
---|
274 | self.check_mesh_dicts(loaded_dict,dict,'test_read_write_msh_file') |
---|
275 | |
---|
276 | def test_read_write_msh_fileII(self): |
---|
277 | dict = self.sparse_dict.copy() |
---|
278 | fileName = tempfile.mktemp(".txt") |
---|
279 | write_msh_file(fileName,dict) |
---|
280 | loaded_dict = read_msh_file(fileName) |
---|
281 | os.remove(fileName) |
---|
282 | dict = self.sparse_dict |
---|
283 | #print "*********************" |
---|
284 | #print dict |
---|
285 | #print "**loaded_dict*******************" |
---|
286 | #print loaded_dict |
---|
287 | #print "*********************" |
---|
288 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileII') |
---|
289 | |
---|
290 | def test_read_write_msh_fileIII(self): |
---|
291 | dict = self.blank_dict.copy() |
---|
292 | fileName = tempfile.mktemp(".txt") |
---|
293 | write_msh_file(fileName,dict) |
---|
294 | loaded_dict = read_msh_file(fileName) |
---|
295 | os.remove(fileName) |
---|
296 | dict = self.blank_dict |
---|
297 | #print "*********************" |
---|
298 | #print dict |
---|
299 | #print "**loaded_dict*******************" |
---|
300 | #print loaded_dict |
---|
301 | #print "*********************" |
---|
302 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
303 | |
---|
304 | def test_read_write_msh_file4(self): |
---|
305 | dict = self.seg_dict.copy() |
---|
306 | fileName = tempfile.mktemp(".txt") |
---|
307 | write_msh_file(fileName,dict) |
---|
308 | loaded_dict = read_msh_file(fileName) |
---|
309 | os.remove(fileName) |
---|
310 | dict = self.seg_dict |
---|
311 | #print "*********************" |
---|
312 | #print dict |
---|
313 | #print "**loaded_dict*******************" |
---|
314 | #print loaded_dict |
---|
315 | #print "*********************" |
---|
316 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
317 | |
---|
318 | def test_read_write_msh_file5(self): |
---|
319 | dict = self.triangle_tags_dict.copy() |
---|
320 | fileName = tempfile.mktemp(".txt") |
---|
321 | write_msh_file(fileName,dict) |
---|
322 | loaded_dict = read_msh_file(fileName) |
---|
323 | os.remove(fileName) |
---|
324 | dict = self.seg_dict |
---|
325 | #print "*********************" |
---|
326 | #print dict |
---|
327 | #print "**loaded_dict*******************" |
---|
328 | #print loaded_dict |
---|
329 | #print "*********************" |
---|
330 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
331 | |
---|
332 | |
---|
333 | def test_read_write_msh_file5(self): |
---|
334 | dict = self.tri_dict.copy() |
---|
335 | fileName = tempfile.mktemp(".txt") |
---|
336 | write_msh_file(fileName,dict) |
---|
337 | loaded_dict = read_msh_file(fileName) |
---|
338 | os.remove(fileName) |
---|
339 | dict = self.tri_dict |
---|
340 | #print "*********************" |
---|
341 | #print dict |
---|
342 | #print "**loaded_dict*******************" |
---|
343 | #print loaded_dict |
---|
344 | #print "*********************" |
---|
345 | self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') |
---|
346 | |
---|
347 | def check_mesh_dicts(self, loaded_dict, dict, fail_string ): |
---|
348 | assert allclose(array(loaded_dict['points']), |
---|
349 | array(dict['points'])) |
---|
350 | |
---|
351 | assert allclose(array(loaded_dict['point_attributes']), |
---|
352 | array(dict['point_attributes'])) |
---|
353 | assert allclose(array(loaded_dict['outline_segments']), |
---|
354 | array(dict['outline_segments'])) |
---|
355 | |
---|
356 | self.failUnless(loaded_dict['outline_segment_tags'] == |
---|
357 | dict['outline_segment_tags'], |
---|
358 | fail_string + ' failed!! Test 4') |
---|
359 | |
---|
360 | |
---|
361 | assert allclose(array(loaded_dict['regions']), |
---|
362 | array(dict['regions'])) |
---|
363 | self.failUnless(loaded_dict['region_tags'] == |
---|
364 | dict['region_tags'], |
---|
365 | fail_string + ' failed!! Test 5') |
---|
366 | |
---|
367 | assert allclose(array(loaded_dict['region_max_areas']), |
---|
368 | array(dict['region_max_areas'])) |
---|
369 | |
---|
370 | assert allclose(array(loaded_dict['holes']), |
---|
371 | array(dict['holes'])) |
---|
372 | |
---|
373 | assert allclose(array(dict['vertices']), |
---|
374 | array(loaded_dict['vertices'])) |
---|
375 | |
---|
376 | assert allclose(array(dict['triangles']), |
---|
377 | array(loaded_dict['triangles'])) |
---|
378 | |
---|
379 | assert allclose(array(dict['segments']), |
---|
380 | array(loaded_dict['segments'])) |
---|
381 | for ob, ldob in map(None,dict['triangle_tags'], |
---|
382 | loaded_dict['triangle_tags']): |
---|
383 | self.failUnless(ob == ldob, |
---|
384 | fail_string + ' failed!! Test triangle_tags') |
---|
385 | self.failUnless(loaded_dict['vertex_attributes'] == |
---|
386 | dict['vertex_attributes'], |
---|
387 | fail_string + ' failed!! Test vertex_attributes') |
---|
388 | |
---|
389 | assert allclose(array(dict['triangle_neighbors']), |
---|
390 | array(loaded_dict['triangle_neighbors'])) |
---|
391 | |
---|
392 | for seg, ldseg in map(None,dict['segment_tags'], |
---|
393 | loaded_dict['segment_tags']): |
---|
394 | self.failUnless(seg == ldseg, |
---|
395 | fail_string + ' failed!! Test 8') |
---|
396 | try: |
---|
397 | assert allclose(array(dict['vertex_attribute_titles']), |
---|
398 | array(loaded_dict['vertex_attribute_titles'])) |
---|
399 | except TypeError: |
---|
400 | self.failUnless(array(loaded_dict['vertex_attribute_titles']) == |
---|
401 | array(dict['vertex_attribute_titles']), |
---|
402 | fail_string + ' failed!! Test 8') |
---|
403 | try: |
---|
404 | self.failUnless(loaded_dict['geo_reference'] == |
---|
405 | dict['geo_reference'] , |
---|
406 | fail_string + ' failed!! Test geo_reference') |
---|
407 | except KeyError: |
---|
408 | self.failUnless(not dict.has_key('geo_reference' and |
---|
409 | loaded_dict['geo_reference'] == None) , |
---|
410 | fail_string + ' failed!! Test geo_reference') |
---|
411 | |
---|
412 | def test_loadpts(self): |
---|
413 | |
---|
414 | from Scientific.IO.NetCDF import NetCDFFile |
---|
415 | |
---|
416 | fileName = tempfile.mktemp(".pts") |
---|
417 | # NetCDF file definition |
---|
418 | outfile = NetCDFFile(fileName, 'w') |
---|
419 | |
---|
420 | # dimension definitions |
---|
421 | outfile.createDimension('number_of_points', 3) |
---|
422 | outfile.createDimension('number_of_dimensions', 2) #This is 2d data |
---|
423 | |
---|
424 | # variable definitions |
---|
425 | outfile.createVariable('points', Float, ('number_of_points', |
---|
426 | 'number_of_dimensions')) |
---|
427 | outfile.createVariable('elevation', Float, ('number_of_points',)) |
---|
428 | |
---|
429 | # Get handles to the variables |
---|
430 | points = outfile.variables['points'] |
---|
431 | elevation = outfile.variables['elevation'] |
---|
432 | |
---|
433 | points[0, :] = [1.0,0.0] |
---|
434 | elevation[0] = 10.0 |
---|
435 | points[1, :] = [0.0,1.0] |
---|
436 | elevation[1] = 0.0 |
---|
437 | points[2, :] = [1.0,0.0] |
---|
438 | elevation[2] = 10.4 |
---|
439 | |
---|
440 | outfile.close() |
---|
441 | |
---|
442 | dict = load_points_file(fileName) |
---|
443 | os.remove(fileName) |
---|
444 | answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] |
---|
445 | #print "dict['pointlist']",dict['pointlist'] |
---|
446 | #print "answer",answer |
---|
447 | assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
448 | assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) |
---|
449 | |
---|
450 | |
---|
451 | def test_concatinate_attributelist(self): |
---|
452 | dic = {} |
---|
453 | dic['one'] = array([1,2]) |
---|
454 | dic['2'] = array([2,7]) |
---|
455 | dic['three'] = array([3,79]) |
---|
456 | dic['4'] = array([4,47]) |
---|
457 | dic['five'] = array([5,17]) |
---|
458 | titles, block = concatinate_attributelist(dic) |
---|
459 | #print "titles", titles |
---|
460 | #print "array", block |
---|
461 | self.failUnless(titles == ['4', '2', 'five', 'three', 'one'], |
---|
462 | 'test_concatinate_attributelist failed.') |
---|
463 | assert allclose(block, [[4,2,5,3,1],[47,7,17,79,2]]) |
---|
464 | |
---|
465 | |
---|
466 | def test_writepts(self): |
---|
467 | dict = {} |
---|
468 | att_dict = {} |
---|
469 | dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
470 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
471 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
472 | dict['attributelist'] = att_dict |
---|
473 | dict['geo_reference'] = Geo_reference(56,1.9,1.9) |
---|
474 | |
---|
475 | |
---|
476 | fileName = tempfile.mktemp(".pts") |
---|
477 | write_pts(fileName, dict) |
---|
478 | dict2 = load_points_file(fileName) |
---|
479 | #print "fileName",fileName |
---|
480 | os.remove(fileName) |
---|
481 | #print "dict2",dict2 |
---|
482 | |
---|
483 | assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
484 | assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) |
---|
485 | answer = [10.0, 0.0, 10.4] |
---|
486 | assert allclose(dict2['attributelist']['brightness'], answer) |
---|
487 | |
---|
488 | #print "dict['geo_reference'] ",dict['geo_reference'] |
---|
489 | #print "dict2['geo_reference']",dict2['geo_reference'] |
---|
490 | |
---|
491 | self.failUnless(dict['geo_reference'] == dict2['geo_reference'], |
---|
492 | 'test_writepts failed. Test geo_reference') |
---|
493 | |
---|
494 | |
---|
495 | def test_half_pts(self): |
---|
496 | dict = {} |
---|
497 | att_dict = {} |
---|
498 | dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
499 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
500 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
501 | dict['attributelist'] = att_dict |
---|
502 | out_dict = half_pts(dict) |
---|
503 | assert allclose(out_dict['pointlist'],[[1.0, 0.0],[1.0, 0.0]]) |
---|
504 | assert allclose(out_dict['attributelist']['elevation'], [10.0, 10.4]) |
---|
505 | assert allclose(out_dict['attributelist']['brightness'], [10.0, 10.4]) |
---|
506 | |
---|
507 | def test_extent_point_atts(self): |
---|
508 | |
---|
509 | dict = {} |
---|
510 | att_dict = {} |
---|
511 | dict['pointlist'] = array([[1.0, 10.0],[0.0, 1.0],[10.0, -10.0]]) |
---|
512 | att_dict['elevation'] = array([30.0, 0.0, 10.4]) |
---|
513 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
514 | dict['attributelist'] = att_dict |
---|
515 | out_dict = extent_point_atts(dict) |
---|
516 | |
---|
517 | #print "out_dict['pointlist']",out_dict #['pointlist'] |
---|
518 | assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0],[10.0,10.0],[0.0, 10.0]]) |
---|
519 | |
---|
520 | self.failUnless(dict['attributelist'] == {}, |
---|
521 | 'test_extent_point_atts failed. Test 1') |
---|
522 | |
---|
523 | def test_reduce_pts(self): |
---|
524 | dict = {} |
---|
525 | att_dict = {} |
---|
526 | dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
527 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
528 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
529 | dict['attributelist'] = att_dict |
---|
530 | |
---|
531 | inFileName = tempfile.mktemp(".pts") |
---|
532 | write_pts(inFileName, dict) |
---|
533 | |
---|
534 | outFileName = tempfile.mktemp(".pts") |
---|
535 | |
---|
536 | dict2 = reduce_pts(inFileName,outFileName, 1 ) |
---|
537 | os.remove(inFileName) |
---|
538 | |
---|
539 | dict2 = load_points_file(outFileName) |
---|
540 | os.remove(outFileName) |
---|
541 | #print "dict2",dict2 |
---|
542 | |
---|
543 | assert allclose(dict2['pointlist'],[[1.0, 0.0]]) |
---|
544 | assert allclose(dict2['attributelist']['elevation'], [10.0]) |
---|
545 | assert allclose(dict2['attributelist']['brightness'], [10.0]) |
---|
546 | |
---|
547 | def test_produce_half_point_files(self): |
---|
548 | dict = {} |
---|
549 | att_dict = {} |
---|
550 | dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
551 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
552 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
553 | dict['attributelist'] = att_dict |
---|
554 | |
---|
555 | inFileName = tempfile.mktemp(".pts") |
---|
556 | write_pts(inFileName, dict) |
---|
557 | |
---|
558 | outFileName = tempfile.mktemp(".pts") |
---|
559 | delimiter = '_p' |
---|
560 | outfiles = produce_half_point_files(inFileName, 1, |
---|
561 | delimiter, verbose = False ) |
---|
562 | os.remove(inFileName) |
---|
563 | root, ext = splitext(inFileName) |
---|
564 | outFileName = root + delimiter + ext |
---|
565 | #print "outFileName",outfiles |
---|
566 | dict2 = load_points_file(outfiles[1]) |
---|
567 | for file in outfiles: |
---|
568 | #print "del file",file |
---|
569 | os.remove(file) |
---|
570 | |
---|
571 | assert allclose(dict2['pointlist'],[[1.0, 0.0]]) |
---|
572 | assert allclose(dict2['attributelist']['elevation'], [10.0]) |
---|
573 | assert allclose(dict2['attributelist']['brightness'], [10.0]) |
---|
574 | |
---|
575 | def test_loadxy3(self): |
---|
576 | |
---|
577 | fileName = tempfile.mktemp(".xya") |
---|
578 | file = open(fileName,"w") |
---|
579 | file.write(" elevation , speed \n\ |
---|
580 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
581 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
582 | 1.0, 0.0, 10.4, 40.0\n") |
---|
583 | file.close() |
---|
584 | #print fileName |
---|
585 | dict = load_points_file(fileName,delimiter = ',') |
---|
586 | os.remove(fileName) |
---|
587 | assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
588 | assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) |
---|
589 | assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) |
---|
590 | |
---|
591 | def test_loadxy4(self): |
---|
592 | import os |
---|
593 | import tempfile |
---|
594 | |
---|
595 | fileName = tempfile.mktemp(".xya") |
---|
596 | file = open(fileName,"w") |
---|
597 | file.write(" elevation speed \n\ |
---|
598 | 1.0 0.0 10.0 0.0\n\ |
---|
599 | 0.0 1.0 0.0 10.0\n\ |
---|
600 | 1.0 0.0 10.4 40.0\n") |
---|
601 | file.close() |
---|
602 | #print fileName |
---|
603 | dict = load_points_file(fileName,delimiter = ' ') |
---|
604 | os.remove(fileName) |
---|
605 | assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
606 | assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) |
---|
607 | assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) |
---|
608 | |
---|
609 | def test_loadxy5(self): |
---|
610 | import os |
---|
611 | import tempfile |
---|
612 | |
---|
613 | fileName = tempfile.mktemp(".xya") |
---|
614 | file = open(fileName,"w") |
---|
615 | file.write(" elevation \n\ |
---|
616 | 1.0 0.0 10.0 0.0\n\ |
---|
617 | 0.0 1.0 0.0 10.0\n\ |
---|
618 | 1.0 0.0 10.4 40.0\n") |
---|
619 | file.close() |
---|
620 | #print fileName |
---|
621 | try: |
---|
622 | dict = load_points_file(fileName,delimiter = ' ') |
---|
623 | except TitleAmountError: |
---|
624 | pass |
---|
625 | else: |
---|
626 | self.failUnless(0 ==1, |
---|
627 | 'bad xya file did not raise error!') |
---|
628 | |
---|
629 | os.remove(fileName) |
---|
630 | |
---|
631 | |
---|
632 | #------------------------------------------------------------- |
---|
633 | if __name__ == "__main__": |
---|
634 | |
---|
635 | suite = unittest.makeSuite(loadASCIITestCase,'test') |
---|
636 | #suite = unittest.makeSuite(loadASCIITestCase,'test_writepts') |
---|
637 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
638 | runner.run(suite) |
---|
639 | |
---|