source: inundation/pyvolution/test_ermapper.py @ 2891

Last change on this file since 2891 was 1813, checked in by tdhu, 19 years ago

This is the first working version of the ermapper_girds.py that will write and read both ERMapper grids and binary data. Currently the reading of ERMapper grids only returns an array (not the spatial information that is read from the header file). Code also needs more checks on parameters such as data format (i.e. consistency between binary data format and header file)

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