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