source: branches/numpy/anuga/abstract_2d_finite_volumes/test_ermapper.py @ 6306

Last change on this file since 6306 was 6304, checked in by rwilson, 16 years ago

Initial commit of numpy changes. Still a long way to go.

File size: 6.5 KB
Line 
1#!/usr/bin/env python
2
3import unittest
4
5
6import ermapper_grids
7from os import remove
8
9import numpy as num
10
11
12class Test_ERMapper(unittest.TestCase):
13    def setUp(self):
14        pass
15
16    def tearDown(self):
17        pass
18
19    def test_write_grid(self):
20        header_filename = 'test_write_ermapper_grid.ers'
21        data_filename = 'test_write_ermapper_grid'
22       
23        original_grid = num.array([[0.0, 0.1, 1.0], [2.0, 3.0, 4.0]])
24
25        # Check that the function works when passing the filename without
26        # a '.ers' extension
27        ermapper_grids.write_ermapper_grid(data_filename, original_grid)
28        new_grid = ermapper_grids.read_ermapper_grid(data_filename)
29        assert num.allclose(original_grid, new_grid)
30
31        # Check that the function works when passing the filename with
32        # a '.ers' extension
33        ermapper_grids.write_ermapper_grid(header_filename, original_grid)
34        new_grid = ermapper_grids.read_ermapper_grid(header_filename)
35        assert num.allclose(original_grid, new_grid)
36
37        # Clean up created files
38        remove(data_filename)
39        remove(header_filename)
40       
41    def test_basic_single_line_grid(self):
42        # Setup test data
43        filename = 'test_write_ermapper_grid'
44        original_grid = num.array([0.0, 0.1, 1.0, 2.0, 3.0, 4.0])
45
46        # Write test data
47        ermapper_grids.write_ermapper_data(original_grid, filename, num.float64)
48
49        # Read in the test data
50        new_grid = ermapper_grids.read_ermapper_data(filename, num.float64)
51
52        # Check that the test data that has been read in matches the original data
53        assert num.allclose(original_grid, new_grid)
54
55        # Clean up created files
56        remove(filename)
57       
58    def test_basic_single_line_grid_default_format(self):
59        # Setup test data
60        filename = 'test_write_ermapper_grid'
61        original_grid = num.array([0.0, 0.1, 1.0, 2.0, 3.0, 4.0])
62
63        # Write test data
64        ermapper_grids.write_ermapper_data(original_grid, filename)
65
66        # Read in the test data
67        new_grid = ermapper_grids.read_ermapper_data(filename)
68
69        # Check that the test data that has been read in matches the original data
70        assert num.allclose(original_grid, new_grid)
71
72        # Clean up created files
73        remove(filename)
74       
75    def test_write_default_header(self):
76        data_filename = 'test_write_ermapper_grid'
77
78        # setup test data
79        original_grid = num.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])
80        # Write test data
81        ermapper_grids.write_ermapper_data(original_grid, data_filename)
82        # Write test header using all default values
83        header_filename = data_filename + '.ers'                             
84        ermapper_grids.write_ermapper_header(header_filename)
85
86        # Check that the read in values match the default values
87        header = ermapper_grids.read_ermapper_header(header_filename)
88
89        assert header['datum'] == '"GDA94"'
90        assert header['projection'] == '"GEOGRAPHIC"' 
91        assert header['coordinatetype'] == 'LL'
92        assert header['rotation'] == '0:0:0.0'
93        assert header['celltype'] == 'IEEE4ByteReal'
94        assert header['nullcellvalue'] == '-99999'
95        assert header['xdimension'] == '100'
96        assert header['registrationcellx'] == '0'
97        assert header['ydimension'] == '100'
98        assert header['registrationcelly'] == '2'
99        assert header['nroflines'] == '3'
100        assert header['nrofcellsperline'] == '4'
101        assert header['longitude'] == '0:0:0'
102        assert header['latitude'] == '0:0:0'
103        assert header['nrofbands'] == '1'
104        assert header['value'] == '"Default_Band"'
105
106        # Clean up created files
107        remove(data_filename)
108        remove(header_filename)
109       
110    def test_header_creation(self):
111        header = {}
112        # have some values that aren't defaults
113        header['nroflines'] = '2'
114        header['nrofcellsperline'] = '3'
115
116        header = ermapper_grids.create_default_header(header)
117
118
119        # default values
120        assert header['datum'] == '"GDA94"'
121        assert header['projection'] == '"GEOGRAPHIC"' 
122        assert header['coordinatetype'] == 'LL'
123        assert header['rotation'] == '0:0:0.0'
124        assert header['celltype'] == 'IEEE4ByteReal'
125        assert header['nullcellvalue'] == '-99999'
126        assert header['xdimension'] == '100'
127        assert header['registrationcellx'] == '0'
128        assert header['ydimension'] == '100'
129        assert header['longitude'] == '0:0:0'
130        assert header['latitude'] == '0:0:0'
131        assert header['nrofbands'] == '1'
132        assert header['value'] == '"Default_Band"'
133
134        # non-default values
135        assert header['nroflines'] == '2'
136        assert header['nrofcellsperline'] == '3'
137        assert header['registrationcelly'] == '1'
138
139
140    def test_write_non_default_header(self):
141        header_filename = 'test_write_ermapper_grid.ers'
142
143        # setup test data
144        header = {}
145        # have some values that aren't defaults
146        header['nroflines'] = '2'
147        header['nrofcellsperline'] = '3'
148       
149        # Write test header using non-default values                           
150        ermapper_grids.write_ermapper_header(header_filename, header)
151
152        # Check that the read in values match the default values
153        header = ermapper_grids.read_ermapper_header(header_filename)
154
155        # default values
156        assert header['datum'] == '"GDA94"'
157        assert header['projection'] == '"GEOGRAPHIC"' 
158        assert header['coordinatetype'] == 'LL'
159        assert header['rotation'] == '0:0:0.0'
160        assert header['celltype'] == 'IEEE4ByteReal'
161        assert header['nullcellvalue'] == '-99999'
162        assert header['xdimension'] == '100'
163        assert header['registrationcellx'] == '0'
164        assert header['ydimension'] == '100'
165        assert header['longitude'] == '0:0:0'
166        assert header['latitude'] == '0:0:0'
167        assert header['nrofbands'] == '1'
168        assert header['value'] == '"Default_Band"'
169
170        # non-default values
171        assert header['nroflines'] == '2'
172        assert header['nrofcellsperline'] == '3'
173        assert header['registrationcelly'] == '1'
174
175        # Clean up created files
176        remove(header_filename)       
177
178
179# def test_default_filenames
180# def test_write_header
181# def test_multi_band_grid
182
183       
184#-------------------------------------------------------------
185if __name__ == "__main__":
186    suite = unittest.makeSuite(Test_ERMapper,'test')
187    runner = unittest.TextTestRunner()
188    runner.run(suite)
189
190
191
192
Note: See TracBrowser for help on using the repository browser.