1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | |
---|
5 | import unittest |
---|
6 | import copy |
---|
7 | from Numeric import zeros, array, allclose, Float |
---|
8 | from anuga.utilities.numerical_tools import mean |
---|
9 | import tempfile |
---|
10 | import os |
---|
11 | from Scientific.IO.NetCDF import NetCDFFile |
---|
12 | from struct import pack |
---|
13 | from sets import ImmutableSet |
---|
14 | |
---|
15 | from anuga.shallow_water import * |
---|
16 | from anuga.shallow_water.data_manager import * |
---|
17 | from anuga.config import epsilon |
---|
18 | from anuga.utilities.anuga_exceptions import ANUGAError |
---|
19 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
20 | from anuga.coordinate_transforms.redfearn import degminsec2decimal_degrees |
---|
21 | from anuga.abstract_2d_finite_volumes.util import file_function |
---|
22 | |
---|
23 | # This is needed to run the tests of local functions |
---|
24 | import data_manager |
---|
25 | from anuga.coordinate_transforms.redfearn import redfearn |
---|
26 | from anuga.coordinate_transforms.geo_reference import Geo_reference, \ |
---|
27 | Â Â Â DEFAULT_ZONE |
---|
28 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
29 | |
---|
30 | class Test_Data_Manager(unittest.TestCase): |
---|
31 | Â Â # Class variable |
---|
32 | Â Â verbose =Â False |
---|
33 | |
---|
34 |   def set_verbose(self): |
---|
35 | Â Â Â Â Test_Data_Manager.verbose =Â True |
---|
36 | Â Â Â Â |
---|
37 |   def setUp(self): |
---|
38 |     import time |
---|
39 |     from mesh_factory import rectangular |
---|
40 | |
---|
41 | Â Â Â Â |
---|
42 | Â Â Â Â self.verbose =Â Test_Data_Manager.verbose |
---|
43 | Â Â Â Â #Create basic mesh |
---|
44 |     points, vertices, boundary = rectangular(2, 2) |
---|
45 | |
---|
46 | Â Â Â Â #Create shallow water domain |
---|
47 |     domain = Domain(points, vertices, boundary) |
---|
48 | Â Â Â Â domain.default_order =Â 2 |
---|
49 | |
---|
50 | Â Â Â Â #Set some field values |
---|
51 |     domain.set_quantity('elevation', lambda x,y: -x) |
---|
52 |     domain.set_quantity('friction', 0.03) |
---|
53 | |
---|
54 | |
---|
55 | Â Â Â Â ###################### |
---|
56 | Â Â Â Â # Boundary conditions |
---|
57 | Â Â Â Â B =Â Transmissive_boundary(domain) |
---|
58 |     domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
59 | |
---|
60 | |
---|
61 | Â Â Â Â ###################### |
---|
62 | Â Â Â Â #Initial condition - with jumps |
---|
63 | Â Â Â Â bed =Â domain.quantities['elevation'].vertex_values |
---|
64 |     stage = zeros(bed.shape, Float) |
---|
65 | |
---|
66 | Â Â Â Â h =Â 0.3 |
---|
67 |     for i in range(stage.shape[0]): |
---|
68 |       if i % 2 == 0: |
---|
69 | Â Â Â Â Â Â Â Â stage[i,:]Â =Â bed[i,:]Â +Â h |
---|
70 | Â Â Â Â Â Â else: |
---|
71 | Â Â Â Â Â Â Â Â stage[i,:]Â =Â bed[i,:] |
---|
72 | |
---|
73 |     domain.set_quantity('stage', stage) |
---|
74 | |
---|
75 | |
---|
76 | Â Â Â Â domain.distribute_to_vertices_and_edges()Â Â Â Â Â Â Â Â |
---|
77 | Â Â Â Â self.initial_stage =Â copy.copy(domain.quantities['stage'].vertex_values) |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | Â Â Â Â self.domain =Â domain |
---|
82 | |
---|
83 | Â Â Â Â C =Â domain.get_vertex_coordinates() |
---|
84 | Â Â Â Â self.X =Â C[:,0:6:2].copy() |
---|
85 | Â Â Â Â self.Y =Â C[:,1:6:2].copy() |
---|
86 | |
---|
87 | Â Â Â Â self.F =Â bed |
---|
88 | |
---|
89 | Â Â Â Â #Write A testfile (not realistic. Values aren't realistic) |
---|
90 | Â Â Â Â self.test_MOST_file =Â 'most_small' |
---|
91 | |
---|
92 |     longitudes = [150.66667, 150.83334, 151., 151.16667] |
---|
93 |     latitudes = [-34.5, -34.33333, -34.16667, -34] |
---|
94 | |
---|
95 | Â Â Â Â long_name =Â 'LON' |
---|
96 | Â Â Â Â lat_name =Â 'LAT' |
---|
97 | |
---|
98 | Â Â Â Â nx =Â 4 |
---|
99 | Â Â Â Â ny =Â 4 |
---|
100 | Â Â Â Â six =Â 6 |
---|
101 | |
---|
102 | |
---|
103 |     for ext in ['_ha.nc', '_ua.nc', '_va.nc', '_e.nc']: |
---|
104 |       fid = NetCDFFile(self.test_MOST_file + ext, 'w') |
---|
105 | |
---|
106 | Â Â Â Â Â Â fid.createDimension(long_name,nx) |
---|
107 | Â Â Â Â Â Â fid.createVariable(long_name,'d',(long_name,)) |
---|
108 | Â Â Â Â Â Â fid.variables[long_name].point_spacing='uneven' |
---|
109 | Â Â Â Â Â Â fid.variables[long_name].units='degrees_east' |
---|
110 | Â Â Â Â Â Â fid.variables[long_name].assignValue(longitudes) |
---|
111 | |
---|
112 | Â Â Â Â Â Â fid.createDimension(lat_name,ny) |
---|
113 | Â Â Â Â Â Â fid.createVariable(lat_name,'d',(lat_name,)) |
---|
114 | Â Â Â Â Â Â fid.variables[lat_name].point_spacing='uneven' |
---|
115 | Â Â Â Â Â Â fid.variables[lat_name].units='degrees_north' |
---|
116 | Â Â Â Â Â Â fid.variables[lat_name].assignValue(latitudes) |
---|
117 | |
---|
118 | Â Â Â Â Â Â fid.createDimension('TIME',six) |
---|
119 | Â Â Â Â Â Â fid.createVariable('TIME','d',('TIME',)) |
---|
120 | Â Â Â Â Â Â fid.variables['TIME'].point_spacing='uneven' |
---|
121 | Â Â Â Â Â Â fid.variables['TIME'].units='seconds' |
---|
122 |       fid.variables['TIME'].assignValue([0.0, 0.1, 0.6, 1.1, 1.6, 2.1]) |
---|
123 | |
---|
124 | |
---|
125 | Â Â Â Â Â Â name =Â ext[1:3].upper() |
---|
126 |       if name == 'E.': name = 'ELEVATION' |
---|
127 |       fid.createVariable(name,'d',('TIME', lat_name, long_name)) |
---|
128 | Â Â Â Â Â Â fid.variables[name].units='CENTIMETERS' |
---|
129 | Â Â Â Â Â Â fid.variables[name].missing_value=-1.e+034 |
---|
130 | |
---|
131 |       fid.variables[name].assignValue([[[0.3400644, 0, -46.63519, -6.50198], |
---|
132 |                        [-0.1214216, 0, 0, 0], |
---|
133 |                        [0, 0, 0, 0], |
---|
134 |                        [0, 0, 0, 0]], |
---|
135 |                        [[0.3400644, 2.291054e-005, -23.33335, -6.50198], |
---|
136 |                        [-0.1213987, 4.581959e-005, -1.594838e-007, 1.421085e-012], |
---|
137 |                        [2.291054e-005, 4.582107e-005, 4.581715e-005, 1.854517e-009], |
---|
138 |                        [0, 2.291054e-005, 2.291054e-005, 0]], |
---|
139 |                        [[0.3400644, 0.0001374632, -23.31503, -6.50198], |
---|
140 |                        [-0.1212842, 0.0002756907, 0.006325484, 1.380492e-006], |
---|
141 |                        [0.0001374632, 0.0002749264, 0.0002742863, 6.665601e-008], |
---|
142 |                        [0, 0.0001374632, 0.0001374632, 0]], |
---|
143 |                        [[0.3400644, 0.0002520159, -23.29672, -6.50198], |
---|
144 |                        [-0.1211696, 0.0005075303, 0.01264618, 6.208276e-006], |
---|
145 |                        [0.0002520159, 0.0005040318, 0.0005027961, 2.23865e-007], |
---|
146 |                        [0, 0.0002520159, 0.0002520159, 0]], |
---|
147 |                        [[0.3400644, 0.0003665686, -23.27842, -6.50198], |
---|
148 |                        [-0.1210551, 0.0007413362, 0.01896192, 1.447638e-005], |
---|
149 |                        [0.0003665686, 0.0007331371, 0.0007313463, 4.734126e-007], |
---|
150 |                        [0, 0.0003665686, 0.0003665686, 0]], |
---|
151 |                        [[0.3400644, 0.0004811212, -23.26012, -6.50198], |
---|
152 |                        [-0.1209405, 0.0009771062, 0.02527271, 2.617787e-005], |
---|
153 |                        [0.0004811212, 0.0009622425, 0.0009599366, 8.152277e-007], |
---|
154 |                        [0, 0.0004811212, 0.0004811212, 0]]]) |
---|
155 | |
---|
156 | |
---|
157 | Â Â Â Â Â Â fid.close() |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 |   def tearDown(self): |
---|
163 |     import os |
---|
164 |     for ext in ['_ha.nc', '_ua.nc', '_va.nc', '_e.nc']: |
---|
165 | Â Â Â Â Â Â #print 'Trying to remove', self.test_MOST_file + ext |
---|
166 | Â Â Â Â Â Â os.remove(self.test_MOST_file +Â ext) |
---|
167 | |
---|
168 |   def test_sww_constant(self): |
---|
169 | Â Â Â Â """Test that constant sww information can be written correctly |
---|
170 | Â Â Â Â (non smooth) |
---|
171 | Â Â Â Â """ |
---|
172 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
173 | Â Â Â Â self.domain.format =Â 'sww'Â #Remove?? |
---|
174 | Â Â Â Â self.domain.smooth =Â False |
---|
175 | Â Â Â Â |
---|
176 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
177 | Â Â Â Â sww.store_connectivity() |
---|
178 | |
---|
179 |     fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
180 | |
---|
181 | Â Â Â Â # Get the variables |
---|
182 | Â Â Â Â x =Â fid.variables['x'] |
---|
183 | Â Â Â Â y =Â fid.variables['y'] |
---|
184 | Â Â Â Â z =Â fid.variables['elevation'] |
---|
185 | Â Â Â Â V =Â fid.variables['volumes'] |
---|
186 | |
---|
187 |     assert allclose (x[:], self.X.flat) |
---|
188 |     assert allclose (y[:], self.Y.flat) |
---|
189 |     assert allclose (z[:], self.F.flat) |
---|
190 | |
---|
191 | Â Â Â Â P =Â len(self.domain) |
---|
192 |     for k in range(P): |
---|
193 |       assert V[k, 0] == 3*k |
---|
194 |       assert V[k, 1] == 3*k+1 |
---|
195 |       assert V[k, 2] == 3*k+2 |
---|
196 | |
---|
197 | Â Â Â Â fid.close() |
---|
198 | Â Â Â Â os.remove(sww.filename) |
---|
199 | |
---|
200 |   def test_sww_header(self): |
---|
201 | Â Â Â Â """Test that constant sww information can be written correctly |
---|
202 | Â Â Â Â (non smooth) |
---|
203 | Â Â Â Â """ |
---|
204 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
205 | Â Â Â Â self.domain.format =Â 'sww'Â #Remove?? |
---|
206 | Â Â Â Â self.domain.smooth =Â False |
---|
207 | |
---|
208 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
209 | Â Â Â Â sww.store_connectivity() |
---|
210 | |
---|
211 | Â Â Â Â #Check contents |
---|
212 | Â Â Â Â #Get NetCDF |
---|
213 |     fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
214 | |
---|
215 | Â Â Â Â # Get the variables |
---|
216 | Â Â Â Â sww_revision =Â fid.revision_number |
---|
217 | Â Â Â Â try: |
---|
218 | Â Â Â Â Â Â revision_number =Â get_revision_number() |
---|
219 | Â Â Â Â except: |
---|
220 | Â Â Â Â Â Â revision_number =Â None |
---|
221 | Â Â Â Â Â Â |
---|
222 |     assert str(revision_number) == sww_revision |
---|
223 | Â Â Â Â fid.close() |
---|
224 | |
---|
225 | Â Â Â Â #print "sww.filename", sww.filename |
---|
226 | Â Â Â Â os.remove(sww.filename) |
---|
227 | |
---|
228 |   def test_sww_range(self): |
---|
229 | Â Â Â Â """Test that constant sww information can be written correctly |
---|
230 | Â Â Â Â (non smooth) |
---|
231 | Â Â Â Â """ |
---|
232 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
233 | Â Â Â Â self.domain.format =Â 'sww' |
---|
234 | Â Â Â Â self.domain.smooth =Â True |
---|
235 | |
---|
236 | Â Â Â Â self.domain.tight_slope_limiters =Â 0Â # Backwards compatibility |
---|
237 | Â Â Â Â |
---|
238 | Â Â Â Â sww =Â get_dataobject(self.domain)Â Â Â Â |
---|
239 | |
---|
240 |     for t in self.domain.evolve(yieldstep = 1, finaltime = 1): |
---|
241 | Â Â Â Â Â Â pass |
---|
242 | Â Â Â Â Â Â |
---|
243 | Â Â Â Â # Get NetCDF |
---|
244 |     fid = NetCDFFile(sww.filename, 'r') # Open existing file for append |
---|
245 | |
---|
246 | Â Â Â Â # Get the variables |
---|
247 |     range = fid.variables['stage_range'][:] |
---|
248 | Â Â Â Â #print range |
---|
249 |     assert allclose(range,[-0.93519, 0.15]) or\ |
---|
250 |         allclose(range,[-0.9352743, 0.15]) or\ |
---|
251 |         allclose(range,[-0.93522203, 0.15000001]) # Old slope limiters |
---|
252 | Â Â Â Â |
---|
253 |     range = fid.variables['xmomentum_range'][:] |
---|
254 | Â Â Â Â #print range |
---|
255 |     assert allclose(range,[0,0.4695096]) or \ |
---|
256 | Â Â Â Â Â Â Â Â allclose(range,[0,0.47790655])Â or\ |
---|
257 | Â Â Â Â Â Â Â Â allclose(range,[0,0.46941957])Â or\ |
---|
258 | Â Â Â Â Â Â Â Â allclose(range,[0,0.47769409]) |
---|
259 | |
---|
260 | Â Â Â Â |
---|
261 |     range = fid.variables['ymomentum_range'][:] |
---|
262 | Â Â Â Â #print range |
---|
263 |     assert allclose(range,[0,0.02174380]) or\ |
---|
264 | Â Â Â Â Â Â Â Â allclose(range,[0,0.02174439])Â or\ |
---|
265 | Â Â Â Â Â Â Â Â allclose(range,[0,0.02283983])Â or\ |
---|
266 | Â Â Â Â Â Â Â Â allclose(range,[0,0.0217342])Â or\ |
---|
267 | Â Â Â Â Â Â Â Â allclose(range,[0,0.0227564])Â # Old slope limiters |
---|
268 | Â Â Â Â |
---|
269 | Â Â Â Â fid.close() |
---|
270 | Â Â Â Â os.remove(sww.filename) |
---|
271 | |
---|
272 |   def test_sww_extrema(self): |
---|
273 | Â Â Â Â """Test that extrema of quantities can be retrieved at every vertex |
---|
274 | Â Â Â Â Extrema are updated at every *internal* timestep |
---|
275 | Â Â Â Â """ |
---|
276 | |
---|
277 | Â Â Â Â domain =Â self.domain |
---|
278 | Â Â Â Â |
---|
279 | Â Â Â Â domain.set_name('extrema_test'Â +Â str(id(self))) |
---|
280 | Â Â Â Â domain.format =Â 'sww' |
---|
281 | Â Â Â Â domain.smooth =Â True |
---|
282 | |
---|
283 |     assert domain.quantities_to_be_monitored is None |
---|
284 |     assert domain.monitor_polygon is None |
---|
285 |     assert domain.monitor_time_interval is None    |
---|
286 | Â Â Â Â |
---|
287 | Â Â Â Â domain.set_quantities_to_be_monitored(['xmomentum', |
---|
288 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'ymomentum', |
---|
289 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'stage-elevation']) |
---|
290 | |
---|
291 |     assert domain.monitor_polygon is None |
---|
292 |     assert domain.monitor_time_interval is None |
---|
293 | |
---|
294 | |
---|
295 | Â Â Â Â domain.set_quantities_to_be_monitored(['xmomentum', |
---|
296 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'ymomentum', |
---|
297 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'stage-elevation'], |
---|
298 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â polygon=domain.get_boundary_polygon(), |
---|
299 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â time_interval=[0,1]) |
---|
300 | Â Â Â Â |
---|
301 | Â Â Â Â |
---|
302 |     assert len(domain.quantities_to_be_monitored) == 3 |
---|
303 |     assert domain.quantities_to_be_monitored.has_key('stage-elevation') |
---|
304 |     assert domain.quantities_to_be_monitored.has_key('xmomentum')        |
---|
305 |     assert domain.quantities_to_be_monitored.has_key('ymomentum')    |
---|
306 | |
---|
307 | Â Â Â Â |
---|
308 | Â Â Â Â #domain.protect_against_isolated_degenerate_timesteps = True |
---|
309 | Â Â Â Â #domain.tight_slope_limiters = 1 |
---|
310 | Â Â Â Â domain.tight_slope_limiters =Â 0Â # Backwards compatibility |
---|
311 | Â Â Â Â |
---|
312 | Â Â Â Â sww =Â get_dataobject(domain) |
---|
313 | |
---|
314 |     for t in domain.evolve(yieldstep = 1, finaltime = 1): |
---|
315 | Â Â Â Â Â Â pass |
---|
316 | Â Â Â Â Â Â #print domain.timestepping_statistics() |
---|
317 | Â Â Â Â Â Â domain.quantity_statistics(precision =Â '%.8f')Â # Silent |
---|
318 | |
---|
319 | Â Â Â Â Â Â |
---|
320 | Â Â Â Â # Get NetCDF |
---|
321 |     fid = NetCDFFile(sww.filename, 'r') # Open existing file for append |
---|
322 | |
---|
323 | Â Â Â Â # Get the variables |
---|
324 | Â Â Â Â extrema =Â fid.variables['stage-elevation.extrema'][:] |
---|
325 |     assert allclose(extrema, [0.00, 0.30]) |
---|
326 | |
---|
327 | Â Â Â Â loc =Â fid.variables['stage-elevation.min_location'][:] |
---|
328 |     assert allclose(loc, [0.16666667, 0.33333333]) |
---|
329 | |
---|
330 | Â Â Â Â loc =Â fid.variables['stage-elevation.max_location'][:]Â Â Â Â |
---|
331 |     assert allclose(loc, [0.8333333, 0.16666667])    |
---|
332 | |
---|
333 | Â Â Â Â time =Â fid.variables['stage-elevation.max_time'][:] |
---|
334 |     assert allclose(time, 0.0)        |
---|
335 | |
---|
336 | Â Â Â Â extrema =Â fid.variables['xmomentum.extrema'][:] |
---|
337 |     assert allclose(extrema,[-0.06062178, 0.47873023]) or allclose(extrema, [-0.06062178, 0.47847986]) |
---|
338 | Â Â Â Â |
---|
339 | Â Â Â Â extrema =Â fid.variables['ymomentum.extrema'][:] |
---|
340 |     assert allclose(extrema,[0.00, 0.0625786]) or allclose(extrema,[0.00, 0.06062178]) |
---|
341 | |
---|
342 | Â Â Â Â time_interval =Â fid.variables['extrema.time_interval'][:] |
---|
343 |     assert allclose(time_interval, [0,1]) |
---|
344 | Â Â Â Â |
---|
345 | Â Â Â Â polygon =Â fid.variables['extrema.polygon'][:]Â Â Â Â |
---|
346 |     assert allclose(polygon, domain.get_boundary_polygon()) |
---|
347 | Â Â Â Â |
---|
348 | Â Â Â Â fid.close() |
---|
349 | Â Â Â Â #print "sww.filename", sww.filename |
---|
350 | Â Â Â Â os.remove(sww.filename) |
---|
351 | |
---|
352 | Â Â Â Â |
---|
353 | Â Â Â Â |
---|
354 |   def test_sww_constant_smooth(self): |
---|
355 | Â Â Â Â """Test that constant sww information can be written correctly |
---|
356 | Â Â Â Â (non smooth) |
---|
357 | Â Â Â Â """ |
---|
358 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
359 | Â Â Â Â self.domain.format =Â 'sww' |
---|
360 | Â Â Â Â self.domain.smooth =Â True |
---|
361 | |
---|
362 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
363 | Â Â Â Â sww.store_connectivity() |
---|
364 | |
---|
365 | Â Â Â Â #Check contents |
---|
366 | Â Â Â Â #Get NetCDF |
---|
367 |     fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
368 | |
---|
369 | Â Â Â Â # Get the variables |
---|
370 | Â Â Â Â X =Â fid.variables['x'][:] |
---|
371 | Â Â Â Â Y =Â fid.variables['y'][:] |
---|
372 | Â Â Â Â Z =Â fid.variables['elevation'][:] |
---|
373 | Â Â Â Â V =Â fid.variables['volumes'] |
---|
374 | |
---|
375 |     assert allclose([X[0], Y[0]], array([0.0, 0.0])) |
---|
376 |     assert allclose([X[1], Y[1]], array([0.0, 0.5])) |
---|
377 |     assert allclose([X[2], Y[2]], array([0.0, 1.0])) |
---|
378 |     assert allclose([X[4], Y[4]], array([0.5, 0.5])) |
---|
379 |     assert allclose([X[7], Y[7]], array([1.0, 0.5])) |
---|
380 | |
---|
381 |     assert Z[4] == -0.5 |
---|
382 | |
---|
383 |     assert V[2,0] == 4 |
---|
384 |     assert V[2,1] == 5 |
---|
385 |     assert V[2,2] == 1 |
---|
386 |     assert V[4,0] == 6 |
---|
387 |     assert V[4,1] == 7 |
---|
388 |     assert V[4,2] == 3 |
---|
389 | |
---|
390 | Â Â Â Â fid.close() |
---|
391 | Â Â Â Â os.remove(sww.filename) |
---|
392 | Â Â Â Â |
---|
393 | |
---|
394 |   def test_sww_variable(self): |
---|
395 | Â Â Â Â """Test that sww information can be written correctly |
---|
396 | Â Â Â Â """ |
---|
397 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
398 | Â Â Â Â self.domain.format =Â 'sww' |
---|
399 | Â Â Â Â self.domain.smooth =Â True |
---|
400 | Â Â Â Â self.domain.reduction =Â mean |
---|
401 | |
---|
402 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
403 | Â Â Â Â sww.store_connectivity() |
---|
404 | Â Â Â Â sww.store_timestep() |
---|
405 | |
---|
406 | Â Â Â Â #Check contents |
---|
407 | Â Â Â Â #Get NetCDF |
---|
408 |     fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
409 | |
---|
410 | |
---|
411 | Â Â Â Â # Get the variables |
---|
412 | Â Â Â Â time =Â fid.variables['time'] |
---|
413 | Â Â Â Â stage =Â fid.variables['stage'] |
---|
414 | |
---|
415 | Â Â Â Â Q =Â self.domain.quantities['stage'] |
---|
416 | Â Â Â Â Q0 =Â Q.vertex_values[:,0] |
---|
417 | Â Â Â Â Q1 =Â Q.vertex_values[:,1] |
---|
418 | Â Â Â Â Q2 =Â Q.vertex_values[:,2] |
---|
419 | |
---|
420 | Â Â Â Â A =Â stage[0,:] |
---|
421 | Â Â Â Â #print A[0], (Q2[0,0] + Q1[1,0])/2 |
---|
422 |     assert allclose(A[0], (Q2[0] + Q1[1])/2) |
---|
423 |     assert allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3) |
---|
424 |     assert allclose(A[2], Q0[3]) |
---|
425 |     assert allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3) |
---|
426 | |
---|
427 | Â Â Â Â #Center point |
---|
428 |     assert allclose(A[4], (Q1[0] + Q2[1] + Q0[2] +\ |
---|
429 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Q0[5]Â +Â Q2[6]Â +Â Q1[7])/6) |
---|
430 | Â Â Â Â |
---|
431 | Â Â Â Â fid.close() |
---|
432 | Â Â Â Â os.remove(sww.filename) |
---|
433 | |
---|
434 | |
---|
435 |   def test_sww_variable2(self): |
---|
436 | Â Â Â Â """Test that sww information can be written correctly |
---|
437 | Â Â Â Â multiple timesteps. Use average as reduction operator |
---|
438 | Â Â Â Â """ |
---|
439 | |
---|
440 |     import time, os |
---|
441 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
442 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
443 | |
---|
444 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
445 | Â Â Â Â self.domain.format =Â 'sww' |
---|
446 | Â Â Â Â self.domain.smooth =Â True |
---|
447 | |
---|
448 | Â Â Â Â self.domain.reduction =Â mean |
---|
449 | |
---|
450 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
451 | Â Â Â Â sww.store_connectivity() |
---|
452 | Â Â Â Â sww.store_timestep() |
---|
453 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
454 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
455 | Â Â Â Â sww.store_timestep() |
---|
456 | |
---|
457 | |
---|
458 | Â Â Â Â #Check contents |
---|
459 | Â Â Â Â #Get NetCDF |
---|
460 |     fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
461 | |
---|
462 | Â Â Â Â # Get the variables |
---|
463 | Â Â Â Â x =Â fid.variables['x'] |
---|
464 | Â Â Â Â y =Â fid.variables['y'] |
---|
465 | Â Â Â Â z =Â fid.variables['elevation'] |
---|
466 | Â Â Â Â time =Â fid.variables['time'] |
---|
467 | Â Â Â Â stage =Â fid.variables['stage'] |
---|
468 | |
---|
469 | Â Â Â Â #Check values |
---|
470 | Â Â Â Â Q =Â self.domain.quantities['stage'] |
---|
471 | Â Â Â Â Q0 =Â Q.vertex_values[:,0] |
---|
472 | Â Â Â Â Q1 =Â Q.vertex_values[:,1] |
---|
473 | Â Â Â Â Q2 =Â Q.vertex_values[:,2] |
---|
474 | |
---|
475 | Â Â Â Â A =Â stage[1,:] |
---|
476 |     assert allclose(A[0], (Q2[0] + Q1[1])/2) |
---|
477 |     assert allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3) |
---|
478 |     assert allclose(A[2], Q0[3]) |
---|
479 |     assert allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3) |
---|
480 | |
---|
481 | Â Â Â Â #Center point |
---|
482 |     assert allclose(A[4], (Q1[0] + Q2[1] + Q0[2] +\ |
---|
483 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Q0[5]Â +Â Q2[6]Â +Â Q1[7])/6) |
---|
484 | |
---|
485 | |
---|
486 | Â Â Â Â fid.close() |
---|
487 | |
---|
488 | Â Â Â Â #Cleanup |
---|
489 | Â Â Â Â os.remove(sww.filename) |
---|
490 | |
---|
491 |   def no_test_sww_variable3(self): |
---|
492 | Â Â Â Â """Test that sww information can be written correctly |
---|
493 | Â Â Â Â multiple timesteps using a different reduction operator (min) |
---|
494 | Â Â Â Â """ |
---|
495 | |
---|
496 | Â Â Â Â # Different reduction in sww files has been made obsolete. |
---|
497 | Â Â Â Â |
---|
498 |     import time, os |
---|
499 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
500 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
501 | |
---|
502 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
503 | Â Â Â Â self.domain.format =Â 'sww' |
---|
504 | Â Â Â Â self.domain.smooth =Â True |
---|
505 | Â Â Â Â self.domain.reduction =Â min |
---|
506 | |
---|
507 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
508 | Â Â Â Â sww.store_connectivity() |
---|
509 | Â Â Â Â sww.store_timestep() |
---|
510 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
511 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
512 | Â Â Â Â sww.store_timestep() |
---|
513 | |
---|
514 | |
---|
515 | Â Â Â Â #Check contents |
---|
516 | Â Â Â Â #Get NetCDF |
---|
517 |     fid = NetCDFFile(sww.filename, 'r') |
---|
518 | |
---|
519 | Â Â Â Â # Get the variables |
---|
520 | Â Â Â Â x =Â fid.variables['x'] |
---|
521 | Â Â Â Â y =Â fid.variables['y'] |
---|
522 | Â Â Â Â z =Â fid.variables['elevation'] |
---|
523 | Â Â Â Â time =Â fid.variables['time'] |
---|
524 | Â Â Â Â stage =Â fid.variables['stage'] |
---|
525 | |
---|
526 | Â Â Â Â #Check values |
---|
527 | Â Â Â Â Q =Â self.domain.quantities['stage'] |
---|
528 | Â Â Â Â Q0 =Â Q.vertex_values[:,0] |
---|
529 | Â Â Â Â Q1 =Â Q.vertex_values[:,1] |
---|
530 | Â Â Â Â Q2 =Â Q.vertex_values[:,2] |
---|
531 | |
---|
532 | Â Â Â Â A =Â stage[1,:] |
---|
533 |     assert allclose(A[0], min(Q2[0], Q1[1])) |
---|
534 |     assert allclose(A[1], min(Q0[1], Q1[3], Q2[2])) |
---|
535 |     assert allclose(A[2], Q0[3]) |
---|
536 |     assert allclose(A[3], min(Q0[0], Q1[5], Q2[4])) |
---|
537 | |
---|
538 | Â Â Â Â #Center point |
---|
539 |     assert allclose(A[4], min(Q1[0], Q2[1], Q0[2],\ |
---|
540 |                  Q0[5], Q2[6], Q1[7])) |
---|
541 | |
---|
542 | |
---|
543 | Â Â Â Â fid.close() |
---|
544 | |
---|
545 | Â Â Â Â #Cleanup |
---|
546 | Â Â Â Â os.remove(sww.filename) |
---|
547 | |
---|
548 | |
---|
549 |   def test_sync(self): |
---|
550 | Â Â Â Â """test_sync - Test info stored at each timestep is as expected (incl initial condition) |
---|
551 | Â Â Â Â """ |
---|
552 | |
---|
553 |     import time, os, config |
---|
554 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
555 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
556 | |
---|
557 | Â Â Â Â self.domain.set_name('synctest') |
---|
558 | Â Â Â Â self.domain.format =Â 'sww' |
---|
559 | Â Â Â Â self.domain.smooth =Â False |
---|
560 | Â Â Â Â self.domain.store =Â True |
---|
561 | Â Â Â Â self.domain.beta_h =Â 0 |
---|
562 | |
---|
563 | Â Â Â Â # In this case tight_slope_limiters as default |
---|
564 | Â Â Â Â # in conjunction with protection |
---|
565 | Â Â Â Â # against isolated degenerate timesteps works. |
---|
566 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
567 | Â Â Â Â #self.domain.protect_against_isolated_degenerate_timesteps = True |
---|
568 | |
---|
569 | Â Â Â Â #print 'tight_sl', self.domain.tight_slope_limiters |
---|
570 | Â Â Â Â |
---|
571 | |
---|
572 | Â Â Â Â #Evolution |
---|
573 |     for t in self.domain.evolve(yieldstep = 1.0, finaltime = 4.0): |
---|
574 | Â Â Â Â Â Â |
---|
575 | Â Â Â Â Â Â #########self.domain.write_time(track_speeds=True) |
---|
576 | Â Â Â Â Â Â stage =Â self.domain.quantities['stage'].vertex_values |
---|
577 | |
---|
578 | Â Â Â Â Â Â #Get NetCDF |
---|
579 |       fid = NetCDFFile(self.domain.writer.filename, 'r') |
---|
580 | Â Â Â Â Â Â stage_file =Â fid.variables['stage'] |
---|
581 | Â Â Â Â Â Â |
---|
582 |       if t == 0.0: |
---|
583 |         assert allclose(stage, self.initial_stage) |
---|
584 |         assert allclose(stage_file[:], stage.flat) |
---|
585 | Â Â Â Â Â Â else: |
---|
586 |         assert not allclose(stage, self.initial_stage) |
---|
587 |         assert not allclose(stage_file[:], stage.flat) |
---|
588 | |
---|
589 | Â Â Â Â Â Â fid.close() |
---|
590 | |
---|
591 | Â Â Â Â os.remove(self.domain.writer.filename) |
---|
592 | |
---|
593 | |
---|
594 |   def test_sww_minimum_storable_height(self): |
---|
595 | Â Â Â Â """Test that sww information can be written correctly |
---|
596 | Â Â Â Â multiple timesteps using a different reduction operator (min) |
---|
597 | Â Â Â Â """ |
---|
598 | |
---|
599 |     import time, os |
---|
600 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
601 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
602 | |
---|
603 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
604 | Â Â Â Â self.domain.format =Â 'sww' |
---|
605 | Â Â Â Â self.domain.smooth =Â True |
---|
606 | Â Â Â Â self.domain.reduction =Â min |
---|
607 | Â Â Â Â self.domain.minimum_storable_height =Â 100 |
---|
608 | |
---|
609 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
610 | Â Â Â Â sww.store_connectivity() |
---|
611 | Â Â Â Â sww.store_timestep() |
---|
612 | |
---|
613 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
614 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
615 | Â Â Â Â sww.store_timestep() |
---|
616 | |
---|
617 | |
---|
618 | Â Â Â Â #Check contents |
---|
619 | Â Â Â Â #Get NetCDF |
---|
620 |     fid = NetCDFFile(sww.filename, 'r') |
---|
621 | |
---|
622 | |
---|
623 | Â Â Â Â # Get the variables |
---|
624 | Â Â Â Â x =Â fid.variables['x'] |
---|
625 | Â Â Â Â y =Â fid.variables['y'] |
---|
626 | Â Â Â Â z =Â fid.variables['elevation'] |
---|
627 | Â Â Â Â time =Â fid.variables['time'] |
---|
628 | Â Â Â Â stage =Â fid.variables['stage'] |
---|
629 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'] |
---|
630 | Â Â Â Â ymomentum =Â fid.variables['ymomentum']Â Â Â Â |
---|
631 | |
---|
632 | Â Â Â Â #Check values |
---|
633 | Â Â Â Â Q =Â self.domain.quantities['stage'] |
---|
634 | Â Â Â Â Q0 =Â Q.vertex_values[:,0] |
---|
635 | Â Â Â Â Q1 =Â Q.vertex_values[:,1] |
---|
636 | Â Â Â Â Q2 =Â Q.vertex_values[:,2] |
---|
637 | |
---|
638 | Â Â Â Â A =Â stage[1,:] |
---|
639 |     assert allclose(stage[1,:], z[:]) |
---|
640 | |
---|
641 | |
---|
642 |     assert allclose(xmomentum, 0.0) |
---|
643 |     assert allclose(ymomentum, 0.0)    |
---|
644 | Â Â Â Â |
---|
645 | Â Â Â Â fid.close() |
---|
646 | |
---|
647 | Â Â Â Â #Cleanup |
---|
648 | Â Â Â Â os.remove(sww.filename) |
---|
649 | |
---|
650 | |
---|
651 |   def Not_a_test_sww_DSG(self): |
---|
652 | Â Â Â Â """Not a test, rather a look at the sww format |
---|
653 | Â Â Â Â """ |
---|
654 | |
---|
655 |     import time, os |
---|
656 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
657 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
658 | |
---|
659 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
660 | Â Â Â Â self.domain.format =Â 'sww' |
---|
661 | Â Â Â Â self.domain.smooth =Â True |
---|
662 | Â Â Â Â self.domain.reduction =Â mean |
---|
663 | |
---|
664 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
665 | Â Â Â Â sww.store_connectivity() |
---|
666 | Â Â Â Â sww.store_timestep() |
---|
667 | |
---|
668 | Â Â Â Â #Check contents |
---|
669 | Â Â Â Â #Get NetCDF |
---|
670 |     fid = NetCDFFile(sww.filename, 'r') |
---|
671 | |
---|
672 | Â Â Â Â # Get the variables |
---|
673 | Â Â Â Â x =Â fid.variables['x'] |
---|
674 | Â Â Â Â y =Â fid.variables['y'] |
---|
675 | Â Â Â Â z =Â fid.variables['elevation'] |
---|
676 | |
---|
677 | Â Â Â Â volumes =Â fid.variables['volumes'] |
---|
678 | Â Â Â Â time =Â fid.variables['time'] |
---|
679 | |
---|
680 | Â Â Â Â # 2D |
---|
681 | Â Â Â Â stage =Â fid.variables['stage'] |
---|
682 | |
---|
683 | Â Â Â Â X =Â x[:] |
---|
684 | Â Â Â Â Y =Â y[:] |
---|
685 | Â Â Â Â Z =Â z[:] |
---|
686 | Â Â Â Â V =Â volumes[:] |
---|
687 | Â Â Â Â T =Â time[:] |
---|
688 | Â Â Â Â S =Â stage[:,:] |
---|
689 | |
---|
690 | #Â Â Â Â Â print "****************************" |
---|
691 | #Â Â Â Â Â print "X ",X |
---|
692 | #Â Â Â Â Â print "****************************" |
---|
693 | #Â Â Â Â Â print "Y ",Y |
---|
694 | #Â Â Â Â Â print "****************************" |
---|
695 | #Â Â Â Â Â print "Z ",Z |
---|
696 | #Â Â Â Â Â print "****************************" |
---|
697 | #Â Â Â Â Â print "V ",V |
---|
698 | #Â Â Â Â Â print "****************************" |
---|
699 | #Â Â Â Â Â print "Time ",T |
---|
700 | #Â Â Â Â Â print "****************************" |
---|
701 | #Â Â Â Â Â print "Stage ",S |
---|
702 | #Â Â Â Â Â print "****************************" |
---|
703 | |
---|
704 | |
---|
705 | Â Â Â Â fid.close() |
---|
706 | |
---|
707 | Â Â Â Â #Cleanup |
---|
708 | Â Â Â Â os.remove(sww.filename) |
---|
709 | |
---|
710 | |
---|
711 | |
---|
712 |   def test_dem2pts_bounding_box_v2(self): |
---|
713 | Â Â Â Â """Test conversion from dem in ascii format to native NetCDF format |
---|
714 | Â Â Â Â """ |
---|
715 | |
---|
716 |     import time, os |
---|
717 |     from Numeric import array, zeros, allclose, Float, concatenate, ones |
---|
718 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
719 | |
---|
720 | Â Â Â Â #Write test asc file |
---|
721 | Â Â Â Â root =Â 'demtest' |
---|
722 | |
---|
723 | Â Â Â Â filename =Â root+'.asc' |
---|
724 |     fid = open(filename, 'w') |
---|
725 |     fid.write("""ncols     10 |
---|
726 | nrows     10 |
---|
727 | xllcorner   2000 |
---|
728 | yllcorner   3000 |
---|
729 | cellsize   1 |
---|
730 | NODATA_value -9999 |
---|
731 | """) |
---|
732 | Â Â Â Â #Create linear function |
---|
733 | Â Â Â Â ref_points =Â [] |
---|
734 | Â Â Â Â ref_elevation =Â [] |
---|
735 | Â Â Â Â x0 =Â 2000 |
---|
736 | Â Â Â Â y =Â 3010 |
---|
737 | Â Â Â Â yvec =Â range(10) |
---|
738 | Â Â Â Â xvec =Â range(10) |
---|
739 | Â Â Â Â z =Â -1 |
---|
740 |     for i in range(10): |
---|
741 | Â Â Â Â Â Â y =Â y -Â 1 |
---|
742 |       for j in range(10): |
---|
743 | Â Â Â Â Â Â Â Â x =Â x0 +Â xvec[j] |
---|
744 | Â Â Â Â Â Â Â Â z +=Â 1 |
---|
745 | Â Â Â Â Â Â Â Â ref_points.append ([x,y]) |
---|
746 | Â Â Â Â Â Â Â Â ref_elevation.append(z) |
---|
747 |         fid.write('%f ' %z) |
---|
748 | Â Â Â Â Â Â fid.write('\n') |
---|
749 | |
---|
750 | Â Â Â Â fid.close() |
---|
751 | |
---|
752 | Â Â Â Â #print 'sending pts', ref_points |
---|
753 | Â Â Â Â #print 'sending elev', ref_elevation |
---|
754 | |
---|
755 | Â Â Â Â #Write prj file with metadata |
---|
756 | Â Â Â Â metafilename =Â root+'.prj' |
---|
757 |     fid = open(metafilename, 'w') |
---|
758 | |
---|
759 | |
---|
760 | Â Â Â Â fid.write("""Projection UTM |
---|
761 | Zone 56 |
---|
762 | Datum WGS84 |
---|
763 | Zunits NO |
---|
764 | Units METERS |
---|
765 | Spheroid WGS84 |
---|
766 | Xshift 0.0000000000 |
---|
767 | Yshift 10000000.0000000000 |
---|
768 | Parameters |
---|
769 | """) |
---|
770 | Â Â Â Â fid.close() |
---|
771 | |
---|
772 | Â Â Â Â #Convert to NetCDF pts |
---|
773 | Â Â Â Â convert_dem_from_ascii2netcdf(root) |
---|
774 |     dem2pts(root, easting_min=2002.0, easting_max=2007.0, |
---|
775 |         northing_min=3003.0, northing_max=3006.0, |
---|
776 | Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
777 | |
---|
778 | Â Â Â Â #Check contents |
---|
779 | Â Â Â Â #Get NetCDF |
---|
780 |     fid = NetCDFFile(root+'.pts', 'r') |
---|
781 | |
---|
782 | Â Â Â Â # Get the variables |
---|
783 | Â Â Â Â #print fid.variables.keys() |
---|
784 | Â Â Â Â points =Â fid.variables['points'] |
---|
785 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
786 | |
---|
787 | Â Â Â Â #Check values |
---|
788 |     assert fid.xllcorner[0] == 2002.0 |
---|
789 |     assert fid.yllcorner[0] == 3003.0 |
---|
790 | |
---|
791 | Â Â Â Â #create new reference points |
---|
792 | Â Â Â Â newz =Â [] |
---|
793 | Â Â Â Â newz[0:5]Â =Â ref_elevation[32:38] |
---|
794 | Â Â Â Â newz[6:11]Â =Â ref_elevation[42:48] |
---|
795 | Â Â Â Â newz[12:17]Â =Â ref_elevation[52:58] |
---|
796 | Â Â Â Â newz[18:23]Â =Â ref_elevation[62:68] |
---|
797 | Â Â Â Â ref_elevation =Â [] |
---|
798 | Â Â Â Â ref_elevation =Â newz |
---|
799 | Â Â Â Â ref_points =Â [] |
---|
800 | Â Â Â Â x0 =Â 2002 |
---|
801 | Â Â Â Â y =Â 3007 |
---|
802 | Â Â Â Â yvec =Â range(4) |
---|
803 | Â Â Â Â xvec =Â range(6) |
---|
804 |     for i in range(4): |
---|
805 | Â Â Â Â Â Â y =Â y -Â 1 |
---|
806 | Â Â Â Â Â Â ynew =Â y -Â 3003.0 |
---|
807 |       for j in range(6): |
---|
808 | Â Â Â Â Â Â Â Â x =Â x0 +Â xvec[j] |
---|
809 | Â Â Â Â Â Â Â Â xnew =Â x -Â 2002.0 |
---|
810 | Â Â Â Â Â Â Â Â ref_points.append ([xnew,ynew])Â #Relative point values |
---|
811 | |
---|
812 |     assert allclose(points, ref_points) |
---|
813 | |
---|
814 |     assert allclose(elevation, ref_elevation) |
---|
815 | |
---|
816 | Â Â Â Â #Cleanup |
---|
817 | Â Â Â Â fid.close() |
---|
818 | |
---|
819 | |
---|
820 | Â Â Â Â os.remove(root +Â '.pts') |
---|
821 | Â Â Â Â os.remove(root +Â '.dem') |
---|
822 | Â Â Â Â os.remove(root +Â '.asc') |
---|
823 | Â Â Â Â os.remove(root +Â '.prj') |
---|
824 | |
---|
825 | |
---|
826 |   def test_dem2pts_bounding_box_removeNullvalues_v2(self): |
---|
827 | Â Â Â Â """Test conversion from dem in ascii format to native NetCDF format |
---|
828 | Â Â Â Â """ |
---|
829 | |
---|
830 |     import time, os |
---|
831 |     from Numeric import array, zeros, allclose, Float, concatenate, ones |
---|
832 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
833 | |
---|
834 | Â Â Â Â #Write test asc file |
---|
835 | Â Â Â Â root =Â 'demtest' |
---|
836 | |
---|
837 | Â Â Â Â filename =Â root+'.asc' |
---|
838 |     fid = open(filename, 'w') |
---|
839 |     fid.write("""ncols     10 |
---|
840 | nrows     10 |
---|
841 | xllcorner   2000 |
---|
842 | yllcorner   3000 |
---|
843 | cellsize   1 |
---|
844 | NODATA_value -9999 |
---|
845 | """) |
---|
846 | Â Â Â Â #Create linear function |
---|
847 | Â Â Â Â ref_points =Â [] |
---|
848 | Â Â Â Â ref_elevation =Â [] |
---|
849 | Â Â Â Â x0 =Â 2000 |
---|
850 | Â Â Â Â y =Â 3010 |
---|
851 | Â Â Â Â yvec =Â range(10) |
---|
852 | Â Â Â Â xvec =Â range(10) |
---|
853 | Â Â Â Â #z = range(100) |
---|
854 | Â Â Â Â z =Â zeros(100) |
---|
855 | Â Â Â Â NODATA_value =Â -9999 |
---|
856 | Â Â Â Â count =Â -1 |
---|
857 |     for i in range(10): |
---|
858 | Â Â Â Â Â Â y =Â y -Â 1 |
---|
859 |       for j in range(10): |
---|
860 | Â Â Â Â Â Â Â Â x =Â x0 +Â xvec[j] |
---|
861 | Â Â Â Â Â Â Â Â ref_points.append ([x,y]) |
---|
862 | Â Â Â Â Â Â Â Â count +=Â 1 |
---|
863 | Â Â Â Â Â Â Â Â z[count]Â =Â (4*i -Â 3*j)%13 |
---|
864 |         if j == 4: z[count] = NODATA_value #column inside clipping region |
---|
865 |         if j == 8: z[count] = NODATA_value #column outside clipping region |
---|
866 |         if i == 9: z[count] = NODATA_value #row outside clipping region |
---|
867 |         if i == 4 and j == 6: z[count] = NODATA_value #arbitrary point inside clipping region |
---|
868 | Â Â Â Â Â Â Â Â ref_elevation.append(Â z[count]Â ) |
---|
869 |         fid.write('%f ' %z[count]) |
---|
870 | Â Â Â Â Â Â fid.write('\n') |
---|
871 | |
---|
872 | Â Â Â Â fid.close() |
---|
873 | |
---|
874 | Â Â Â Â #print 'sending elev', ref_elevation |
---|
875 | |
---|
876 | Â Â Â Â #Write prj file with metadata |
---|
877 | Â Â Â Â metafilename =Â root+'.prj' |
---|
878 |     fid = open(metafilename, 'w') |
---|
879 | |
---|
880 | |
---|
881 | Â Â Â Â fid.write("""Projection UTM |
---|
882 | Zone 56 |
---|
883 | Datum WGS84 |
---|
884 | Zunits NO |
---|
885 | Units METERS |
---|
886 | Spheroid WGS84 |
---|
887 | Xshift 0.0000000000 |
---|
888 | Yshift 10000000.0000000000 |
---|
889 | Parameters |
---|
890 | """) |
---|
891 | Â Â Â Â fid.close() |
---|
892 | |
---|
893 | Â Â Â Â #Convert to NetCDF pts |
---|
894 | Â Â Â Â convert_dem_from_ascii2netcdf(root) |
---|
895 |     dem2pts(root, easting_min=2002.0, easting_max=2007.0, |
---|
896 |         northing_min=3003.0, northing_max=3006.0, |
---|
897 | Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
898 | |
---|
899 | Â Â Â Â #Check contents |
---|
900 | Â Â Â Â #Get NetCDF |
---|
901 |     fid = NetCDFFile(root+'.pts', 'r') |
---|
902 | |
---|
903 | Â Â Â Â # Get the variables |
---|
904 | Â Â Â Â #print fid.variables.keys() |
---|
905 | Â Â Â Â points =Â fid.variables['points'] |
---|
906 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
907 | |
---|
908 | Â Â Â Â #Check values |
---|
909 |     assert fid.xllcorner[0] == 2002.0 |
---|
910 |     assert fid.yllcorner[0] == 3003.0 |
---|
911 | |
---|
912 | Â Â Â Â #create new reference points |
---|
913 | Â Â Â Â newz =Â zeros(19) |
---|
914 | Â Â Â Â newz[0:2]Â =Â ref_elevation[32:34] |
---|
915 | Â Â Â Â newz[2:5]Â =Â ref_elevation[35:38] |
---|
916 | Â Â Â Â newz[5:7]Â =Â ref_elevation[42:44] |
---|
917 | Â Â Â Â newz[7]Â =Â ref_elevation[45] |
---|
918 | Â Â Â Â newz[8]Â =Â ref_elevation[47] |
---|
919 | Â Â Â Â newz[9:11]Â =Â ref_elevation[52:54] |
---|
920 | Â Â Â Â newz[11:14]Â =Â ref_elevation[55:58] |
---|
921 | Â Â Â Â newz[14:16]Â =Â ref_elevation[62:64] |
---|
922 | Â Â Â Â newz[16:19]Â =Â ref_elevation[65:68] |
---|
923 | |
---|
924 | |
---|
925 | Â Â Â Â ref_elevation =Â newz |
---|
926 | Â Â Â Â ref_points =Â [] |
---|
927 | Â Â Â Â new_ref_points =Â [] |
---|
928 | Â Â Â Â x0 =Â 2002 |
---|
929 | Â Â Â Â y =Â 3007 |
---|
930 | Â Â Â Â yvec =Â range(4) |
---|
931 | Â Â Â Â xvec =Â range(6) |
---|
932 |     for i in range(4): |
---|
933 | Â Â Â Â Â Â y =Â y -Â 1 |
---|
934 | Â Â Â Â Â Â ynew =Â y -Â 3003.0 |
---|
935 |       for j in range(6): |
---|
936 | Â Â Â Â Â Â Â Â x =Â x0 +Â xvec[j] |
---|
937 | Â Â Â Â Â Â Â Â xnew =Â x -Â 2002.0 |
---|
938 |         if j <> 2 and (i<>1 or j<>4): |
---|
939 | Â Â Â Â Â Â Â Â Â Â ref_points.append([x,y]) |
---|
940 | Â Â Â Â Â Â Â Â Â Â new_ref_points.append ([xnew,ynew]) |
---|
941 | |
---|
942 | |
---|
943 |     assert allclose(points, new_ref_points) |
---|
944 |     assert allclose(elevation, ref_elevation) |
---|
945 | |
---|
946 | Â Â Â Â #Cleanup |
---|
947 | Â Â Â Â fid.close() |
---|
948 | |
---|
949 | |
---|
950 | Â Â Â Â os.remove(root +Â '.pts') |
---|
951 | Â Â Â Â os.remove(root +Â '.dem') |
---|
952 | Â Â Â Â os.remove(root +Â '.asc') |
---|
953 | Â Â Â Â os.remove(root +Â '.prj') |
---|
954 | |
---|
955 | |
---|
956 |   def test_dem2pts_bounding_box_removeNullvalues_v3(self): |
---|
957 | Â Â Â Â """Test conversion from dem in ascii format to native NetCDF format |
---|
958 | Â Â Â Â Check missing values on clipping boundary |
---|
959 | Â Â Â Â """ |
---|
960 | |
---|
961 |     import time, os |
---|
962 |     from Numeric import array, zeros, allclose, Float, concatenate, ones |
---|
963 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
964 | |
---|
965 | Â Â Â Â #Write test asc file |
---|
966 | Â Â Â Â root =Â 'demtest' |
---|
967 | |
---|
968 | Â Â Â Â filename =Â root+'.asc' |
---|
969 |     fid = open(filename, 'w') |
---|
970 |     fid.write("""ncols     10 |
---|
971 | nrows     10 |
---|
972 | xllcorner   2000 |
---|
973 | yllcorner   3000 |
---|
974 | cellsize   1 |
---|
975 | NODATA_value -9999 |
---|
976 | """) |
---|
977 | Â Â Â Â #Create linear function |
---|
978 | Â Â Â Â ref_points =Â [] |
---|
979 | Â Â Â Â ref_elevation =Â [] |
---|
980 | Â Â Â Â x0 =Â 2000 |
---|
981 | Â Â Â Â y =Â 3010 |
---|
982 | Â Â Â Â yvec =Â range(10) |
---|
983 | Â Â Â Â xvec =Â range(10) |
---|
984 | Â Â Â Â #z = range(100) |
---|
985 | Â Â Â Â z =Â zeros(100) |
---|
986 | Â Â Â Â NODATA_value =Â -9999 |
---|
987 | Â Â Â Â count =Â -1 |
---|
988 |     for i in range(10): |
---|
989 | Â Â Â Â Â Â y =Â y -Â 1 |
---|
990 |       for j in range(10): |
---|
991 | Â Â Â Â Â Â Â Â x =Â x0 +Â xvec[j] |
---|
992 | Â Â Â Â Â Â Â Â ref_points.append ([x,y]) |
---|
993 | Â Â Â Â Â Â Â Â count +=Â 1 |
---|
994 | Â Â Â Â Â Â Â Â z[count]Â =Â (4*i -Â 3*j)%13 |
---|
995 |         if j == 4: z[count] = NODATA_value #column inside clipping region |
---|
996 |         if j == 8: z[count] = NODATA_value #column outside clipping region |
---|
997 |         if i == 6: z[count] = NODATA_value #row on clipping boundary |
---|
998 |         if i == 4 and j == 6: z[count] = NODATA_value #arbitrary point inside clipping region |
---|
999 | Â Â Â Â Â Â Â Â ref_elevation.append(Â z[count]Â ) |
---|
1000 |         fid.write('%f ' %z[count]) |
---|
1001 | Â Â Â Â Â Â fid.write('\n') |
---|
1002 | |
---|
1003 | Â Â Â Â fid.close() |
---|
1004 | |
---|
1005 | Â Â Â Â #print 'sending elev', ref_elevation |
---|
1006 | |
---|
1007 | Â Â Â Â #Write prj file with metadata |
---|
1008 | Â Â Â Â metafilename =Â root+'.prj' |
---|
1009 |     fid = open(metafilename, 'w') |
---|
1010 | |
---|
1011 | |
---|
1012 | Â Â Â Â fid.write("""Projection UTM |
---|
1013 | Zone 56 |
---|
1014 | Datum WGS84 |
---|
1015 | Zunits NO |
---|
1016 | Units METERS |
---|
1017 | Spheroid WGS84 |
---|
1018 | Xshift 0.0000000000 |
---|
1019 | Yshift 10000000.0000000000 |
---|
1020 | Parameters |
---|
1021 | """) |
---|
1022 | Â Â Â Â fid.close() |
---|
1023 | |
---|
1024 | Â Â Â Â #Convert to NetCDF pts |
---|
1025 | Â Â Â Â convert_dem_from_ascii2netcdf(root) |
---|
1026 |     dem2pts(root, easting_min=2002.0, easting_max=2007.0, |
---|
1027 |         northing_min=3003.0, northing_max=3006.0, |
---|
1028 | Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
1029 | |
---|
1030 | Â Â Â Â #Check contents |
---|
1031 | Â Â Â Â #Get NetCDF |
---|
1032 |     fid = NetCDFFile(root+'.pts', 'r') |
---|
1033 | |
---|
1034 | Â Â Â Â # Get the variables |
---|
1035 | Â Â Â Â #print fid.variables.keys() |
---|
1036 | Â Â Â Â points =Â fid.variables['points'] |
---|
1037 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
1038 | |
---|
1039 | Â Â Â Â #Check values |
---|
1040 |     assert fid.xllcorner[0] == 2002.0 |
---|
1041 |     assert fid.yllcorner[0] == 3003.0 |
---|
1042 | |
---|
1043 | Â Â Â Â #create new reference points |
---|
1044 | Â Â Â Â newz =Â zeros(14) |
---|
1045 | Â Â Â Â newz[0:2]Â =Â ref_elevation[32:34] |
---|
1046 | Â Â Â Â newz[2:5]Â =Â ref_elevation[35:38] |
---|
1047 | Â Â Â Â newz[5:7]Â =Â ref_elevation[42:44] |
---|
1048 | Â Â Â Â newz[7]Â =Â ref_elevation[45] |
---|
1049 | Â Â Â Â newz[8]Â =Â ref_elevation[47] |
---|
1050 | Â Â Â Â newz[9:11]Â =Â ref_elevation[52:54] |
---|
1051 | Â Â Â Â newz[11:14]Â =Â ref_elevation[55:58] |
---|
1052 | |
---|
1053 | |
---|
1054 | |
---|
1055 | Â Â Â Â ref_elevation =Â newz |
---|
1056 | Â Â Â Â ref_points =Â [] |
---|
1057 | Â Â Â Â new_ref_points =Â [] |
---|
1058 | Â Â Â Â x0 =Â 2002 |
---|
1059 | Â Â Â Â y =Â 3007 |
---|
1060 | Â Â Â Â yvec =Â range(4) |
---|
1061 | Â Â Â Â xvec =Â range(6) |
---|
1062 |     for i in range(4): |
---|
1063 | Â Â Â Â Â Â y =Â y -Â 1 |
---|
1064 | Â Â Â Â Â Â ynew =Â y -Â 3003.0 |
---|
1065 |       for j in range(6): |
---|
1066 | Â Â Â Â Â Â Â Â x =Â x0 +Â xvec[j] |
---|
1067 | Â Â Â Â Â Â Â Â xnew =Â x -Â 2002.0 |
---|
1068 |         if j <> 2 and (i<>1 or j<>4) and i<>3: |
---|
1069 | Â Â Â Â Â Â Â Â Â Â ref_points.append([x,y]) |
---|
1070 | Â Â Â Â Â Â Â Â Â Â new_ref_points.append ([xnew,ynew]) |
---|
1071 | |
---|
1072 | |
---|
1073 | Â Â Â Â #print points[:],points[:].shape |
---|
1074 | Â Â Â Â #print new_ref_points, len(new_ref_points) |
---|
1075 | |
---|
1076 |     assert allclose(elevation, ref_elevation) |
---|
1077 |     assert allclose(points, new_ref_points) |
---|
1078 | |
---|
1079 | |
---|
1080 | Â Â Â Â #Cleanup |
---|
1081 | Â Â Â Â fid.close() |
---|
1082 | |
---|
1083 | |
---|
1084 | Â Â Â Â os.remove(root +Â '.pts') |
---|
1085 | Â Â Â Â os.remove(root +Â '.dem') |
---|
1086 | Â Â Â Â os.remove(root +Â '.asc') |
---|
1087 | Â Â Â Â os.remove(root +Â '.prj') |
---|
1088 | |
---|
1089 | |
---|
1090 |   def test_hecras_cross_sections2pts(self): |
---|
1091 | Â Â Â Â """Test conversion from HECRAS cross sections in ascii format |
---|
1092 | Â Â Â Â to native NetCDF pts format |
---|
1093 | Â Â Â Â """ |
---|
1094 | |
---|
1095 |     import time, os |
---|
1096 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1097 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1098 | |
---|
1099 | Â Â Â Â #Write test asc file |
---|
1100 | Â Â Â Â root =Â 'hecrastest' |
---|
1101 | |
---|
1102 | Â Â Â Â filename =Â root+'.sdf' |
---|
1103 |     fid = open(filename, 'w') |
---|
1104 | Â Â Â Â fid.write(""" |
---|
1105 | # RAS export file created on Mon 15Aug2005 11:42 |
---|
1106 | # by HEC-RAS Version 3.1.1 |
---|
1107 | |
---|
1108 | BEGIN HEADER: |
---|
1109 | Â UNITS: METRIC |
---|
1110 | Â DTM TYPE: TIN |
---|
1111 | Â DTM: v:\1\cit\perth_topo\river_tin |
---|
1112 | Â STREAM LAYER: c:\\x_local\hecras\21_02_03\up_canning_cent3d.shp |
---|
1113 | Â CROSS-SECTION LAYER: c:\\x_local\hecras\21_02_03\up_can_xs3d.shp |
---|
1114 | Â MAP PROJECTION: UTM |
---|
1115 | Â PROJECTION ZONE: 50 |
---|
1116 | Â DATUM: AGD66 |
---|
1117 | Â VERTICAL DATUM: |
---|
1118 | Â NUMBER OF REACHES:Â 19 |
---|
1119 | Â NUMBER OF CROSS-SECTIONS:Â 2 |
---|
1120 | END HEADER: |
---|
1121 | |
---|
1122 | |
---|
1123 | BEGIN CROSS-SECTIONS: |
---|
1124 | |
---|
1125 | Â CROSS-SECTION: |
---|
1126 | Â Â STREAM ID:Southern-Wungong |
---|
1127 | Â Â REACH ID:Southern-Wungong |
---|
1128 | Â Â STATION:21410 |
---|
1129 | Â Â CUT LINE: |
---|
1130 | Â Â Â 407546.08 , 6437277.542 |
---|
1131 | Â Â Â 407329.32 , 6437489.482 |
---|
1132 | Â Â Â 407283.11 , 6437541.232 |
---|
1133 | Â Â SURFACE LINE: |
---|
1134 |    407546.08,  6437277.54,  52.14 |
---|
1135 |    407538.88,  6437284.58,  51.07 |
---|
1136 |    407531.68,  6437291.62,  50.56 |
---|
1137 |    407524.48,  6437298.66,  49.58 |
---|
1138 |    407517.28,  6437305.70,  49.09 |
---|
1139 |    407510.08,  6437312.74,  48.76 |
---|
1140 | Â END: |
---|
1141 | |
---|
1142 | Â CROSS-SECTION: |
---|
1143 | Â Â STREAM ID:Swan River |
---|
1144 | Â Â REACH ID:Swan Mouth |
---|
1145 | Â Â STATION:840.* |
---|
1146 | Â Â CUT LINE: |
---|
1147 | Â Â Â 381178.0855 , 6452559.0685 |
---|
1148 | Â Â Â 380485.4755 , 6453169.272 |
---|
1149 | Â Â SURFACE LINE: |
---|
1150 |    381178.09,  6452559.07,  4.17 |
---|
1151 |    381169.49,  6452566.64,  4.26 |
---|
1152 |    381157.78,  6452576.96,  4.34 |
---|
1153 |    381155.97,  6452578.56,  4.35 |
---|
1154 |    381143.72,  6452589.35,  4.43 |
---|
1155 |    381136.69,  6452595.54,  4.58 |
---|
1156 |    381114.74,  6452614.88,  4.41 |
---|
1157 |    381075.53,  6452649.43,  4.17 |
---|
1158 |    381071.47,  6452653.00,  3.99 |
---|
1159 |    381063.46,  6452660.06,  3.67 |
---|
1160 |    381054.41,  6452668.03,  3.67 |
---|
1161 | Â END: |
---|
1162 | END CROSS-SECTIONS: |
---|
1163 | """) |
---|
1164 | |
---|
1165 | Â Â Â Â fid.close() |
---|
1166 | |
---|
1167 | |
---|
1168 | Â Â Â Â #Convert to NetCDF pts |
---|
1169 | Â Â Â Â hecras_cross_sections2pts(root) |
---|
1170 | |
---|
1171 | Â Â Â Â #Check contents |
---|
1172 | Â Â Â Â #Get NetCDF |
---|
1173 |     fid = NetCDFFile(root+'.pts', 'r') |
---|
1174 | |
---|
1175 | Â Â Â Â # Get the variables |
---|
1176 | Â Â Â Â #print fid.variables.keys() |
---|
1177 | Â Â Â Â points =Â fid.variables['points'] |
---|
1178 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
1179 | |
---|
1180 | Â Â Â Â #Check values |
---|
1181 |     ref_points = [[407546.08, 6437277.54], |
---|
1182 |            [407538.88, 6437284.58], |
---|
1183 |            [407531.68, 6437291.62], |
---|
1184 |            [407524.48, 6437298.66], |
---|
1185 |            [407517.28, 6437305.70], |
---|
1186 |            [407510.08, 6437312.74]] |
---|
1187 | |
---|
1188 |     ref_points += [[381178.09, 6452559.07], |
---|
1189 |             [381169.49, 6452566.64], |
---|
1190 |             [381157.78, 6452576.96], |
---|
1191 |             [381155.97, 6452578.56], |
---|
1192 |             [381143.72, 6452589.35], |
---|
1193 |             [381136.69, 6452595.54], |
---|
1194 |             [381114.74, 6452614.88], |
---|
1195 |             [381075.53, 6452649.43], |
---|
1196 |             [381071.47, 6452653.00], |
---|
1197 |             [381063.46, 6452660.06], |
---|
1198 |             [381054.41, 6452668.03]] |
---|
1199 | |
---|
1200 | |
---|
1201 |     ref_elevation = [52.14, 51.07, 50.56, 49.58, 49.09, 48.76] |
---|
1202 |     ref_elevation += [4.17, 4.26, 4.34, 4.35, 4.43, 4.58, 4.41, 4.17, 3.99, 3.67, 3.67] |
---|
1203 | |
---|
1204 | Â Â Â Â #print points[:] |
---|
1205 | Â Â Â Â #print ref_points |
---|
1206 |     assert allclose(points, ref_points) |
---|
1207 | |
---|
1208 | Â Â Â Â #print attributes[:] |
---|
1209 | Â Â Â Â #print ref_elevation |
---|
1210 |     assert allclose(elevation, ref_elevation) |
---|
1211 | |
---|
1212 | Â Â Â Â #Cleanup |
---|
1213 | Â Â Â Â fid.close() |
---|
1214 | |
---|
1215 | |
---|
1216 | Â Â Â Â os.remove(root +Â '.sdf') |
---|
1217 | Â Â Â Â os.remove(root +Â '.pts') |
---|
1218 | |
---|
1219 | |
---|
1220 |   def test_sww2dem_asc_elevation_depth(self): |
---|
1221 | Â Â Â Â """ |
---|
1222 | Â Â Â Â test_sww2dem_asc_elevation_depth(self): |
---|
1223 | Â Â Â Â Test that sww information can be converted correctly to asc/prj |
---|
1224 | Â Â Â Â format readable by e.g. ArcView |
---|
1225 | Â Â Â Â """ |
---|
1226 | |
---|
1227 |     import time, os |
---|
1228 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1229 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1230 | |
---|
1231 | Â Â Â Â #Setup |
---|
1232 | Â Â Â Â self.domain.set_name('datatest') |
---|
1233 | |
---|
1234 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_elevation.prj' |
---|
1235 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_elevation.asc' |
---|
1236 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
1237 | |
---|
1238 | Â Â Â Â self.domain.set_datadir('.') |
---|
1239 | Â Â Â Â self.domain.format =Â 'sww' |
---|
1240 | Â Â Â Â self.domain.smooth =Â True |
---|
1241 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
1242 |     self.domain.set_quantity('stage', 1.0) |
---|
1243 | |
---|
1244 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
1245 | |
---|
1246 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
1247 | Â Â Â Â sww.store_connectivity() |
---|
1248 | Â Â Â Â sww.store_timestep() |
---|
1249 | |
---|
1250 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
1251 | |
---|
1252 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
1253 | Â Â Â Â sww.store_timestep() |
---|
1254 | |
---|
1255 | Â Â Â Â cellsize =Â 0.25 |
---|
1256 | Â Â Â Â #Check contents |
---|
1257 | Â Â Â Â #Get NetCDF |
---|
1258 | |
---|
1259 |     fid = NetCDFFile(sww.filename, 'r') |
---|
1260 | |
---|
1261 | Â Â Â Â # Get the variables |
---|
1262 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
1263 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
1264 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
1265 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
1266 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
1267 | |
---|
1268 | Â Â Â Â fid.close() |
---|
1269 | |
---|
1270 | Â Â Â Â #Export to ascii/prj files |
---|
1271 | Â Â Â Â sww2dem(self.domain.get_name(), |
---|
1272 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
1273 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1274 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1275 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1276 | |
---|
1277 | Â Â Â Â #Check prj (meta data) |
---|
1278 | Â Â Â Â prjid =Â open(prjfile) |
---|
1279 | Â Â Â Â lines =Â prjid.readlines() |
---|
1280 | Â Â Â Â prjid.close() |
---|
1281 | |
---|
1282 | Â Â Â Â L =Â lines[0].strip().split() |
---|
1283 |     assert L[0].strip().lower() == 'projection' |
---|
1284 |     assert L[1].strip().lower() == 'utm' |
---|
1285 | |
---|
1286 | Â Â Â Â L =Â lines[1].strip().split() |
---|
1287 |     assert L[0].strip().lower() == 'zone' |
---|
1288 |     assert L[1].strip().lower() == '56' |
---|
1289 | |
---|
1290 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1291 |     assert L[0].strip().lower() == 'datum' |
---|
1292 |     assert L[1].strip().lower() == 'wgs84' |
---|
1293 | |
---|
1294 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1295 |     assert L[0].strip().lower() == 'zunits' |
---|
1296 |     assert L[1].strip().lower() == 'no' |
---|
1297 | |
---|
1298 | Â Â Â Â L =Â lines[4].strip().split() |
---|
1299 |     assert L[0].strip().lower() == 'units' |
---|
1300 |     assert L[1].strip().lower() == 'meters' |
---|
1301 | |
---|
1302 | Â Â Â Â L =Â lines[5].strip().split() |
---|
1303 |     assert L[0].strip().lower() == 'spheroid' |
---|
1304 |     assert L[1].strip().lower() == 'wgs84' |
---|
1305 | |
---|
1306 | Â Â Â Â L =Â lines[6].strip().split() |
---|
1307 |     assert L[0].strip().lower() == 'xshift' |
---|
1308 |     assert L[1].strip().lower() == '500000' |
---|
1309 | |
---|
1310 | Â Â Â Â L =Â lines[7].strip().split() |
---|
1311 |     assert L[0].strip().lower() == 'yshift' |
---|
1312 |     assert L[1].strip().lower() == '10000000' |
---|
1313 | |
---|
1314 | Â Â Â Â L =Â lines[8].strip().split() |
---|
1315 |     assert L[0].strip().lower() == 'parameters' |
---|
1316 | |
---|
1317 | |
---|
1318 | Â Â Â Â #Check asc file |
---|
1319 | Â Â Â Â ascid =Â open(ascfile) |
---|
1320 | Â Â Â Â lines =Â ascid.readlines() |
---|
1321 | Â Â Â Â ascid.close() |
---|
1322 | |
---|
1323 | Â Â Â Â L =Â lines[0].strip().split() |
---|
1324 |     assert L[0].strip().lower() == 'ncols' |
---|
1325 |     assert L[1].strip().lower() == '5' |
---|
1326 | |
---|
1327 | Â Â Â Â L =Â lines[1].strip().split() |
---|
1328 |     assert L[0].strip().lower() == 'nrows' |
---|
1329 |     assert L[1].strip().lower() == '5' |
---|
1330 | |
---|
1331 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1332 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1333 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1334 | |
---|
1335 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1336 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1337 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1338 | |
---|
1339 | Â Â Â Â L =Â lines[4].strip().split() |
---|
1340 |     assert L[0].strip().lower() == 'cellsize' |
---|
1341 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
1342 | |
---|
1343 | Â Â Â Â L =Â lines[5].strip().split() |
---|
1344 |     assert L[0].strip() == 'NODATA_value' |
---|
1345 |     assert L[1].strip().lower() == '-9999' |
---|
1346 | |
---|
1347 | Â Â Â Â #Check grid values |
---|
1348 |     for j in range(5): |
---|
1349 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1350 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1351 |       for i in range(5): |
---|
1352 |         assert allclose(float(L[i]), -i*cellsize - y) |
---|
1353 | Â Â Â Â Â Â Â Â |
---|
1354 | Â Â Â Â #Cleanup |
---|
1355 | Â Â Â Â os.remove(prjfile) |
---|
1356 | Â Â Â Â os.remove(ascfile) |
---|
1357 | |
---|
1358 | Â Â Â Â #Export to ascii/prj files |
---|
1359 | Â Â Â Â sww2dem(self.domain.get_name(), |
---|
1360 | Â Â Â Â Â Â Â Â quantity =Â 'depth', |
---|
1361 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1362 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1363 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1364 | Â Â Â Â |
---|
1365 | Â Â Â Â #Check asc file |
---|
1366 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_depth.asc' |
---|
1367 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_depth.prj' |
---|
1368 | Â Â Â Â ascid =Â open(ascfile) |
---|
1369 | Â Â Â Â lines =Â ascid.readlines() |
---|
1370 | Â Â Â Â ascid.close() |
---|
1371 | |
---|
1372 | Â Â Â Â L =Â lines[0].strip().split() |
---|
1373 |     assert L[0].strip().lower() == 'ncols' |
---|
1374 |     assert L[1].strip().lower() == '5' |
---|
1375 | |
---|
1376 | Â Â Â Â L =Â lines[1].strip().split() |
---|
1377 |     assert L[0].strip().lower() == 'nrows' |
---|
1378 |     assert L[1].strip().lower() == '5' |
---|
1379 | |
---|
1380 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1381 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1382 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1383 | |
---|
1384 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1385 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1386 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1387 | |
---|
1388 | Â Â Â Â L =Â lines[4].strip().split() |
---|
1389 |     assert L[0].strip().lower() == 'cellsize' |
---|
1390 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
1391 | |
---|
1392 | Â Â Â Â L =Â lines[5].strip().split() |
---|
1393 |     assert L[0].strip() == 'NODATA_value' |
---|
1394 |     assert L[1].strip().lower() == '-9999' |
---|
1395 | |
---|
1396 | Â Â Â Â #Check grid values |
---|
1397 |     for j in range(5): |
---|
1398 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1399 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1400 |       for i in range(5): |
---|
1401 |         assert allclose(float(L[i]), 1 - (-i*cellsize - y)) |
---|
1402 | |
---|
1403 | |
---|
1404 | Â Â Â Â #Cleanup |
---|
1405 | Â Â Â Â os.remove(prjfile) |
---|
1406 | Â Â Â Â os.remove(ascfile) |
---|
1407 | Â Â Â Â os.remove(swwfile) |
---|
1408 | |
---|
1409 | |
---|
1410 |   def test_export_grid(self): |
---|
1411 | Â Â Â Â """ |
---|
1412 | Â Â Â Â test_export_grid(self): |
---|
1413 | Â Â Â Â Test that sww information can be converted correctly to asc/prj |
---|
1414 | Â Â Â Â format readable by e.g. ArcView |
---|
1415 | Â Â Â Â """ |
---|
1416 | |
---|
1417 |     import time, os |
---|
1418 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1419 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1420 | |
---|
1421 | Â Â Â Â try: |
---|
1422 | Â Â Â Â Â Â os.remove('teg*.sww') |
---|
1423 | Â Â Â Â except: |
---|
1424 | Â Â Â Â Â Â pass |
---|
1425 | |
---|
1426 | Â Â Â Â #Setup |
---|
1427 | Â Â Â Â self.domain.set_name('teg') |
---|
1428 | |
---|
1429 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_elevation.prj' |
---|
1430 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_elevation.asc' |
---|
1431 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
1432 | |
---|
1433 | Â Â Â Â self.domain.set_datadir('.') |
---|
1434 | Â Â Â Â self.domain.format =Â 'sww' |
---|
1435 | Â Â Â Â self.domain.smooth =Â True |
---|
1436 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
1437 |     self.domain.set_quantity('stage', 1.0) |
---|
1438 | |
---|
1439 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
1440 | |
---|
1441 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
1442 | Â Â Â Â sww.store_connectivity() |
---|
1443 | Â Â Â Â sww.store_timestep() |
---|
1444 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
1445 | Â Â Â Â sww.store_timestep() |
---|
1446 | |
---|
1447 | Â Â Â Â cellsize =Â 0.25 |
---|
1448 | Â Â Â Â #Check contents |
---|
1449 | Â Â Â Â #Get NetCDF |
---|
1450 | |
---|
1451 |     fid = NetCDFFile(sww.filename, 'r') |
---|
1452 | |
---|
1453 | Â Â Â Â # Get the variables |
---|
1454 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
1455 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
1456 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
1457 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
1458 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
1459 | |
---|
1460 | Â Â Â Â fid.close() |
---|
1461 | |
---|
1462 | Â Â Â Â #Export to ascii/prj files |
---|
1463 | Â Â Â Â export_grid(self.domain.get_name(), |
---|
1464 | Â Â Â Â Â Â Â Â quantities =Â 'elevation', |
---|
1465 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1466 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1467 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1468 | |
---|
1469 | Â Â Â Â #Check asc file |
---|
1470 | Â Â Â Â ascid =Â open(ascfile) |
---|
1471 | Â Â Â Â lines =Â ascid.readlines() |
---|
1472 | Â Â Â Â ascid.close() |
---|
1473 | |
---|
1474 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1475 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1476 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1477 | |
---|
1478 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1479 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1480 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1481 | |
---|
1482 | Â Â Â Â #Check grid values |
---|
1483 |     for j in range(5): |
---|
1484 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1485 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1486 |       for i in range(5): |
---|
1487 |         assert allclose(float(L[i]), -i*cellsize - y) |
---|
1488 | Â Â Â Â Â Â Â Â |
---|
1489 | Â Â Â Â #Cleanup |
---|
1490 | Â Â Â Â os.remove(prjfile) |
---|
1491 | Â Â Â Â os.remove(ascfile) |
---|
1492 | Â Â Â Â os.remove(swwfile) |
---|
1493 | |
---|
1494 |   def test_export_gridII(self): |
---|
1495 | Â Â Â Â """ |
---|
1496 | Â Â Â Â test_export_gridII(self): |
---|
1497 | Â Â Â Â Test that sww information can be converted correctly to asc/prj |
---|
1498 | Â Â Â Â format readable by e.g. ArcView |
---|
1499 | Â Â Â Â """ |
---|
1500 | |
---|
1501 |     import time, os |
---|
1502 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1503 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1504 | |
---|
1505 | Â Â Â Â try: |
---|
1506 | Â Â Â Â Â Â os.remove('teg*.sww') |
---|
1507 | Â Â Â Â except: |
---|
1508 | Â Â Â Â Â Â pass |
---|
1509 | |
---|
1510 | Â Â Â Â #Setup |
---|
1511 | Â Â Â Â self.domain.set_name('tegII') |
---|
1512 | |
---|
1513 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
1514 | |
---|
1515 | Â Â Â Â self.domain.set_datadir('.') |
---|
1516 | Â Â Â Â self.domain.format =Â 'sww' |
---|
1517 | Â Â Â Â self.domain.smooth =Â True |
---|
1518 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
1519 |     self.domain.set_quantity('stage', 1.0) |
---|
1520 | |
---|
1521 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
1522 | |
---|
1523 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
1524 | Â Â Â Â sww.store_connectivity() |
---|
1525 | Â Â Â Â sww.store_timestep()Â #'stage') |
---|
1526 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
1527 | Â Â Â Â sww.store_timestep()Â #'stage') |
---|
1528 | |
---|
1529 | Â Â Â Â cellsize =Â 0.25 |
---|
1530 | Â Â Â Â #Check contents |
---|
1531 | Â Â Â Â #Get NetCDF |
---|
1532 | |
---|
1533 |     fid = NetCDFFile(sww.filename, 'r') |
---|
1534 | |
---|
1535 | Â Â Â Â # Get the variables |
---|
1536 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
1537 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
1538 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
1539 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
1540 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
1541 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
1542 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:]Â Â Â Â |
---|
1543 | |
---|
1544 | Â Â Â Â #print 'stage', stage |
---|
1545 | Â Â Â Â #print 'xmom', xmomentum |
---|
1546 |     #print 'ymom', ymomentum    |
---|
1547 | |
---|
1548 | Â Â Â Â fid.close() |
---|
1549 | |
---|
1550 | Â Â Â Â #Export to ascii/prj files |
---|
1551 |     if True: |
---|
1552 | Â Â Â Â Â Â export_grid(self.domain.get_name(), |
---|
1553 |             quantities = ['elevation', 'depth'], |
---|
1554 | Â Â Â Â Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1555 | Â Â Â Â Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1556 | Â Â Â Â Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1557 | |
---|
1558 | Â Â Â Â else: |
---|
1559 | Â Â Â Â Â Â export_grid(self.domain.get_name(), |
---|
1560 | Â Â Â Â Â Â Â Â quantities =Â ['depth'], |
---|
1561 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1562 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1563 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1564 | |
---|
1565 | |
---|
1566 | Â Â Â Â Â Â export_grid(self.domain.get_name(), |
---|
1567 | Â Â Â Â Â Â Â Â quantities =Â ['elevation'], |
---|
1568 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1569 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1570 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1571 | |
---|
1572 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_elevation.prj' |
---|
1573 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_elevation.asc' |
---|
1574 | Â Â Â Â |
---|
1575 | Â Â Â Â #Check asc file |
---|
1576 | Â Â Â Â ascid =Â open(ascfile) |
---|
1577 | Â Â Â Â lines =Â ascid.readlines() |
---|
1578 | Â Â Â Â ascid.close() |
---|
1579 | |
---|
1580 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1581 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1582 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1583 | |
---|
1584 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1585 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1586 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1587 | |
---|
1588 | Â Â Â Â #print "ascfile", ascfile |
---|
1589 | Â Â Â Â #Check grid values |
---|
1590 |     for j in range(5): |
---|
1591 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1592 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1593 |       for i in range(5): |
---|
1594 |         #print " -i*cellsize - y", -i*cellsize - y |
---|
1595 | Â Â Â Â Â Â Â Â #print "float(L[i])", float(L[i]) |
---|
1596 |         assert allclose(float(L[i]), -i*cellsize - y) |
---|
1597 | |
---|
1598 | Â Â Â Â #Cleanup |
---|
1599 | Â Â Â Â os.remove(prjfile) |
---|
1600 | Â Â Â Â os.remove(ascfile) |
---|
1601 | Â Â Â Â |
---|
1602 | Â Â Â Â #Check asc file |
---|
1603 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_depth.asc' |
---|
1604 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_depth.prj' |
---|
1605 | Â Â Â Â ascid =Â open(ascfile) |
---|
1606 | Â Â Â Â lines =Â ascid.readlines() |
---|
1607 | Â Â Â Â ascid.close() |
---|
1608 | |
---|
1609 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1610 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1611 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1612 | |
---|
1613 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1614 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1615 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1616 | |
---|
1617 | Â Â Â Â #Check grid values |
---|
1618 |     for j in range(5): |
---|
1619 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1620 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1621 |       for i in range(5): |
---|
1622 |         #print " -i*cellsize - y", -i*cellsize - y |
---|
1623 | Â Â Â Â Â Â Â Â #print "float(L[i])", float(L[i])Â Â Â Â Â Â Â Â |
---|
1624 |         assert allclose(float(L[i]), 1 - (-i*cellsize - y)) |
---|
1625 | |
---|
1626 | Â Â Â Â #Cleanup |
---|
1627 | Â Â Â Â os.remove(prjfile) |
---|
1628 | Â Â Â Â os.remove(ascfile) |
---|
1629 | Â Â Â Â os.remove(swwfile) |
---|
1630 | |
---|
1631 | |
---|
1632 |   def test_export_gridIII(self): |
---|
1633 | Â Â Â Â """ |
---|
1634 | Â Â Â Â test_export_gridIII |
---|
1635 | Â Â Â Â Test that sww information can be converted correctly to asc/prj |
---|
1636 | Â Â Â Â format readable by e.g. ArcView |
---|
1637 | Â Â Â Â """ |
---|
1638 | |
---|
1639 |     import time, os |
---|
1640 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1641 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1642 | |
---|
1643 | Â Â Â Â try: |
---|
1644 | Â Â Â Â Â Â os.remove('teg*.sww') |
---|
1645 | Â Â Â Â except: |
---|
1646 | Â Â Â Â Â Â pass |
---|
1647 | |
---|
1648 | Â Â Â Â #Setup |
---|
1649 | Â Â Â Â |
---|
1650 | Â Â Â Â self.domain.set_name('tegIII') |
---|
1651 | |
---|
1652 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
1653 | |
---|
1654 | Â Â Â Â self.domain.set_datadir('.') |
---|
1655 | Â Â Â Â self.domain.format =Â 'sww' |
---|
1656 | Â Â Â Â self.domain.smooth =Â True |
---|
1657 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
1658 |     self.domain.set_quantity('stage', 1.0) |
---|
1659 | |
---|
1660 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
1661 | Â Â Â Â |
---|
1662 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
1663 | Â Â Â Â sww.store_connectivity() |
---|
1664 | Â Â Â Â sww.store_timestep()Â #'stage') |
---|
1665 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
1666 | Â Â Â Â sww.store_timestep()Â #'stage') |
---|
1667 | |
---|
1668 | Â Â Â Â cellsize =Â 0.25 |
---|
1669 | Â Â Â Â #Check contents |
---|
1670 | Â Â Â Â #Get NetCDF |
---|
1671 | |
---|
1672 |     fid = NetCDFFile(sww.filename, 'r') |
---|
1673 | |
---|
1674 | Â Â Â Â # Get the variables |
---|
1675 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
1676 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
1677 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
1678 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
1679 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
1680 | |
---|
1681 | Â Â Â Â fid.close() |
---|
1682 | |
---|
1683 | Â Â Â Â #Export to ascii/prj files |
---|
1684 | Â Â Â Â extra_name_out =Â 'yeah' |
---|
1685 |     if True: |
---|
1686 | Â Â Â Â Â Â export_grid(self.domain.get_name(), |
---|
1687 |             quantities = ['elevation', 'depth'], |
---|
1688 | Â Â Â Â Â Â Â Â Â Â Â Â extra_name_out =Â extra_name_out, |
---|
1689 | Â Â Â Â Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1690 | Â Â Â Â Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1691 | Â Â Â Â Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1692 | |
---|
1693 | Â Â Â Â else: |
---|
1694 | Â Â Â Â Â Â export_grid(self.domain.get_name(), |
---|
1695 | Â Â Â Â Â Â Â Â quantities =Â ['depth'], |
---|
1696 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1697 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1698 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1699 | |
---|
1700 | |
---|
1701 | Â Â Â Â Â Â export_grid(self.domain.get_name(), |
---|
1702 | Â Â Â Â Â Â Â Â quantities =Â ['elevation'], |
---|
1703 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1704 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1705 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1706 | |
---|
1707 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_elevation_yeah.prj' |
---|
1708 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_elevation_yeah.asc' |
---|
1709 | Â Â Â Â |
---|
1710 | Â Â Â Â #Check asc file |
---|
1711 | Â Â Â Â ascid =Â open(ascfile) |
---|
1712 | Â Â Â Â lines =Â ascid.readlines() |
---|
1713 | Â Â Â Â ascid.close() |
---|
1714 | |
---|
1715 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1716 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1717 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1718 | |
---|
1719 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1720 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1721 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1722 | |
---|
1723 | Â Â Â Â #print "ascfile", ascfile |
---|
1724 | Â Â Â Â #Check grid values |
---|
1725 |     for j in range(5): |
---|
1726 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1727 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1728 |       for i in range(5): |
---|
1729 |         #print " -i*cellsize - y", -i*cellsize - y |
---|
1730 | Â Â Â Â Â Â Â Â #print "float(L[i])", float(L[i]) |
---|
1731 |         assert allclose(float(L[i]), -i*cellsize - y) |
---|
1732 | Â Â Â Â Â Â Â Â |
---|
1733 | Â Â Â Â #Cleanup |
---|
1734 | Â Â Â Â os.remove(prjfile) |
---|
1735 | Â Â Â Â os.remove(ascfile) |
---|
1736 | Â Â Â Â |
---|
1737 | Â Â Â Â #Check asc file |
---|
1738 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_depth_yeah.asc' |
---|
1739 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_depth_yeah.prj' |
---|
1740 | Â Â Â Â ascid =Â open(ascfile) |
---|
1741 | Â Â Â Â lines =Â ascid.readlines() |
---|
1742 | Â Â Â Â ascid.close() |
---|
1743 | |
---|
1744 | Â Â Â Â L =Â lines[2].strip().split() |
---|
1745 |     assert L[0].strip().lower() == 'xllcorner' |
---|
1746 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
1747 | |
---|
1748 | Â Â Â Â L =Â lines[3].strip().split() |
---|
1749 |     assert L[0].strip().lower() == 'yllcorner' |
---|
1750 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
1751 | |
---|
1752 | Â Â Â Â #Check grid values |
---|
1753 |     for j in range(5): |
---|
1754 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1755 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1756 |       for i in range(5): |
---|
1757 |         assert allclose(float(L[i]), 1 - (-i*cellsize - y)) |
---|
1758 | |
---|
1759 | Â Â Â Â #Cleanup |
---|
1760 | Â Â Â Â os.remove(prjfile) |
---|
1761 | Â Â Â Â os.remove(ascfile) |
---|
1762 | Â Â Â Â os.remove(swwfile) |
---|
1763 | |
---|
1764 |   def test_export_grid_bad(self): |
---|
1765 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
1766 | Â Â Â Â format readable by e.g. ArcView |
---|
1767 | Â Â Â Â """ |
---|
1768 | |
---|
1769 | Â Â Â Â try: |
---|
1770 | Â Â Â Â Â Â export_grid('a_small_round-egg', |
---|
1771 |             quantities = ['elevation', 'depth'], |
---|
1772 | Â Â Â Â Â Â Â Â Â Â Â Â cellsize =Â 99, |
---|
1773 | Â Â Â Â Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1774 | Â Â Â Â Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1775 |     except IOError: |
---|
1776 | Â Â Â Â Â Â pass |
---|
1777 | Â Â Â Â else: |
---|
1778 |       self.failUnless(0 ==1, 'Bad input did not throw exception error!') |
---|
1779 | |
---|
1780 |   def test_export_grid_parallel(self): |
---|
1781 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
1782 | Â Â Â Â format readable by e.g. ArcView |
---|
1783 | Â Â Â Â """ |
---|
1784 | |
---|
1785 |     import time, os |
---|
1786 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1787 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1788 | |
---|
1789 | Â Â Â Â base_name =Â 'tegp' |
---|
1790 | Â Â Â Â #Setup |
---|
1791 | Â Â Â Â self.domain.set_name(base_name+'_P0_8') |
---|
1792 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
1793 | |
---|
1794 | Â Â Â Â self.domain.set_datadir('.') |
---|
1795 | Â Â Â Â self.domain.format =Â 'sww' |
---|
1796 | Â Â Â Â self.domain.smooth =Â True |
---|
1797 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
1798 |     self.domain.set_quantity('stage', 1.0) |
---|
1799 | |
---|
1800 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
1801 | |
---|
1802 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
1803 | Â Â Â Â sww.store_connectivity() |
---|
1804 | Â Â Â Â sww.store_timestep() |
---|
1805 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.0001) |
---|
1806 | Â Â Â Â #Setup |
---|
1807 | Â Â Â Â self.domain.set_name(base_name+'_P1_8') |
---|
1808 | Â Â Â Â swwfile2 =Â self.domain.get_name()Â +Â '.sww' |
---|
1809 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
1810 | Â Â Â Â sww.store_connectivity() |
---|
1811 | Â Â Â Â sww.store_timestep() |
---|
1812 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.0002) |
---|
1813 | Â Â Â Â sww.store_timestep() |
---|
1814 | |
---|
1815 | Â Â Â Â cellsize =Â 0.25 |
---|
1816 | Â Â Â Â #Check contents |
---|
1817 | Â Â Â Â #Get NetCDF |
---|
1818 | |
---|
1819 |     fid = NetCDFFile(sww.filename, 'r') |
---|
1820 | |
---|
1821 | Â Â Â Â # Get the variables |
---|
1822 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
1823 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
1824 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
1825 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
1826 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
1827 | |
---|
1828 | Â Â Â Â fid.close() |
---|
1829 | |
---|
1830 | Â Â Â Â #Export to ascii/prj files |
---|
1831 | Â Â Â Â extra_name_out =Â 'yeah' |
---|
1832 | Â Â Â Â export_grid(base_name, |
---|
1833 |           quantities = ['elevation', 'depth'], |
---|
1834 | Â Â Â Â Â Â Â Â Â Â extra_name_out =Â extra_name_out, |
---|
1835 | Â Â Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1836 | Â Â Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1837 | Â Â Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1838 | |
---|
1839 | Â Â Â Â prjfile =Â base_name +Â '_P0_8_elevation_yeah.prj' |
---|
1840 | Â Â Â Â ascfile =Â base_name +Â '_P0_8_elevation_yeah.asc'Â Â Â Â |
---|
1841 | Â Â Â Â #Check asc file |
---|
1842 | Â Â Â Â ascid =Â open(ascfile) |
---|
1843 | Â Â Â Â lines =Â ascid.readlines() |
---|
1844 | Â Â Â Â ascid.close() |
---|
1845 | Â Â Â Â #Check grid values |
---|
1846 |     for j in range(5): |
---|
1847 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1848 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1849 |       for i in range(5): |
---|
1850 |         #print " -i*cellsize - y", -i*cellsize - y |
---|
1851 | Â Â Â Â Â Â Â Â #print "float(L[i])", float(L[i]) |
---|
1852 |         assert allclose(float(L[i]), -i*cellsize - y)        |
---|
1853 | Â Â Â Â #Cleanup |
---|
1854 | Â Â Â Â os.remove(prjfile) |
---|
1855 | Â Â Â Â os.remove(ascfile) |
---|
1856 | |
---|
1857 | Â Â Â Â prjfile =Â base_name +Â '_P1_8_elevation_yeah.prj' |
---|
1858 | Â Â Â Â ascfile =Â base_name +Â '_P1_8_elevation_yeah.asc'Â Â Â Â |
---|
1859 | Â Â Â Â #Check asc file |
---|
1860 | Â Â Â Â ascid =Â open(ascfile) |
---|
1861 | Â Â Â Â lines =Â ascid.readlines() |
---|
1862 | Â Â Â Â ascid.close() |
---|
1863 | Â Â Â Â #Check grid values |
---|
1864 |     for j in range(5): |
---|
1865 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1866 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1867 |       for i in range(5): |
---|
1868 |         #print " -i*cellsize - y", -i*cellsize - y |
---|
1869 | Â Â Â Â Â Â Â Â #print "float(L[i])", float(L[i]) |
---|
1870 |         assert allclose(float(L[i]), -i*cellsize - y)        |
---|
1871 | Â Â Â Â #Cleanup |
---|
1872 | Â Â Â Â os.remove(prjfile) |
---|
1873 | Â Â Â Â os.remove(ascfile) |
---|
1874 | Â Â Â Â os.remove(swwfile) |
---|
1875 | |
---|
1876 | Â Â Â Â #Check asc file |
---|
1877 | Â Â Â Â ascfile =Â base_name +Â '_P0_8_depth_yeah.asc' |
---|
1878 | Â Â Â Â prjfile =Â base_name +Â '_P0_8_depth_yeah.prj' |
---|
1879 | Â Â Â Â ascid =Â open(ascfile) |
---|
1880 | Â Â Â Â lines =Â ascid.readlines() |
---|
1881 | Â Â Â Â ascid.close() |
---|
1882 | Â Â Â Â #Check grid values |
---|
1883 |     for j in range(5): |
---|
1884 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1885 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1886 |       for i in range(5): |
---|
1887 |         assert allclose(float(L[i]), 1 - (-i*cellsize - y)) |
---|
1888 | Â Â Â Â #Cleanup |
---|
1889 | Â Â Â Â os.remove(prjfile) |
---|
1890 | Â Â Â Â os.remove(ascfile) |
---|
1891 | |
---|
1892 | Â Â Â Â #Check asc file |
---|
1893 | Â Â Â Â ascfile =Â base_name +Â '_P1_8_depth_yeah.asc' |
---|
1894 | Â Â Â Â prjfile =Â base_name +Â '_P1_8_depth_yeah.prj' |
---|
1895 | Â Â Â Â ascid =Â open(ascfile) |
---|
1896 | Â Â Â Â lines =Â ascid.readlines() |
---|
1897 | Â Â Â Â ascid.close() |
---|
1898 | Â Â Â Â #Check grid values |
---|
1899 |     for j in range(5): |
---|
1900 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
1901 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
1902 |       for i in range(5): |
---|
1903 |         assert allclose(float(L[i]), 1 - (-i*cellsize - y)) |
---|
1904 | Â Â Â Â #Cleanup |
---|
1905 | Â Â Â Â os.remove(prjfile) |
---|
1906 | Â Â Â Â os.remove(ascfile) |
---|
1907 | Â Â Â Â os.remove(swwfile2) |
---|
1908 | |
---|
1909 |   def test_sww2dem_larger(self): |
---|
1910 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
1911 | Â Â Â Â format readable by e.g. ArcView. Here: |
---|
1912 | |
---|
1913 |     ncols     11 |
---|
1914 |     nrows     11 |
---|
1915 |     xllcorner   308500 |
---|
1916 |     yllcorner   6189000 |
---|
1917 |     cellsize   10.000000 |
---|
1918 |     NODATA_value -9999 |
---|
1919 | Â Â Â Â -100 -110 -120 -130 -140 -150 -160 -170 -180 -190 -200 |
---|
1920 | Â Â Â Â Â -90 -100 -110 -120 -130 -140 -150 -160 -170 -180 -190 |
---|
1921 | Â Â Â Â Â -80Â -90 -100 -110 -120 -130 -140 -150 -160 -170 -180 |
---|
1922 | Â Â Â Â Â -70Â -80Â -90 -100 -110 -120 -130 -140 -150 -160 -170 |
---|
1923 | Â Â Â Â Â -60Â -70Â -80Â -90 -100 -110 -120 -130 -140 -150 -160 |
---|
1924 | Â Â Â Â Â -50Â -60Â -70Â -80Â -90 -100 -110 -120 -130 -140 -150 |
---|
1925 | Â Â Â Â Â -40Â -50Â -60Â -70Â -80Â -90 -100 -110 -120 -130 -140 |
---|
1926 | Â Â Â Â Â -30Â -40Â -50Â -60Â -70Â -80Â -90 -100 -110 -120 -130 |
---|
1927 | Â Â Â Â Â -20Â -30Â -40Â -50Â -60Â -70Â -80Â -90 -100 -110 -120 |
---|
1928 | Â Â Â Â Â -10Â -20Â -30Â -40Â -50Â -60Â -70Â -80Â -90 -100 -110 |
---|
1929 | Â Â Â Â Â Â 0Â -10Â -20Â -30Â -40Â -50Â -60Â -70Â -80Â -90 -100 |
---|
1930 | |
---|
1931 | Â Â Â Â """ |
---|
1932 | |
---|
1933 |     import time, os |
---|
1934 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
1935 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
1936 | |
---|
1937 | Â Â Â Â #Setup |
---|
1938 | |
---|
1939 |     from mesh_factory import rectangular |
---|
1940 | |
---|
1941 | Â Â Â Â #Create basic mesh (100m x 100m) |
---|
1942 |     points, vertices, boundary = rectangular(2, 2, 100, 100) |
---|
1943 | |
---|
1944 | Â Â Â Â #Create shallow water domain |
---|
1945 |     domain = Domain(points, vertices, boundary) |
---|
1946 | Â Â Â Â domain.default_order =Â 2 |
---|
1947 | |
---|
1948 | Â Â Â Â domain.set_name('datatest') |
---|
1949 | |
---|
1950 | Â Â Â Â prjfile =Â domain.get_name()Â +Â '_elevation.prj' |
---|
1951 | Â Â Â Â ascfile =Â domain.get_name()Â +Â '_elevation.asc' |
---|
1952 | Â Â Â Â swwfile =Â domain.get_name()Â +Â '.sww' |
---|
1953 | |
---|
1954 | Â Â Â Â domain.set_datadir('.') |
---|
1955 | Â Â Â Â domain.format =Â 'sww' |
---|
1956 | Â Â Â Â domain.smooth =Â True |
---|
1957 |     domain.geo_reference = Geo_reference(56, 308500, 6189000) |
---|
1958 | |
---|
1959 | Â Â Â Â # |
---|
1960 |     domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
1961 |     domain.set_quantity('stage', 0) |
---|
1962 | |
---|
1963 | Â Â Â Â B =Â Transmissive_boundary(domain) |
---|
1964 |     domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
1965 | |
---|
1966 | |
---|
1967 | Â Â Â Â # |
---|
1968 | Â Â Â Â sww =Â get_dataobject(domain) |
---|
1969 | Â Â Â Â sww.store_connectivity() |
---|
1970 | Â Â Â Â sww.store_timestep() |
---|
1971 | Â Â Â Â |
---|
1972 | Â Â Â Â domain.tight_slope_limiters =Â 1 |
---|
1973 | Â Â Â Â domain.evolve_to_end(finaltime =Â 0.01) |
---|
1974 | Â Â Â Â sww.store_timestep() |
---|
1975 | |
---|
1976 | Â Â Â Â cellsize =Â 10Â #10m grid |
---|
1977 | |
---|
1978 | |
---|
1979 | Â Â Â Â #Check contents |
---|
1980 | Â Â Â Â #Get NetCDF |
---|
1981 | |
---|
1982 |     fid = NetCDFFile(sww.filename, 'r') |
---|
1983 | |
---|
1984 | Â Â Â Â # Get the variables |
---|
1985 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
1986 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
1987 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
1988 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
1989 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
1990 | |
---|
1991 | |
---|
1992 | Â Â Â Â #Export to ascii/prj files |
---|
1993 | Â Â Â Â sww2dem(domain.get_name(), |
---|
1994 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
1995 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
1996 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
1997 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
1998 | |
---|
1999 | |
---|
2000 | Â Â Â Â #Check prj (meta data) |
---|
2001 | Â Â Â Â prjid =Â open(prjfile) |
---|
2002 | Â Â Â Â lines =Â prjid.readlines() |
---|
2003 | Â Â Â Â prjid.close() |
---|
2004 | |
---|
2005 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2006 |     assert L[0].strip().lower() == 'projection' |
---|
2007 |     assert L[1].strip().lower() == 'utm' |
---|
2008 | |
---|
2009 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2010 |     assert L[0].strip().lower() == 'zone' |
---|
2011 |     assert L[1].strip().lower() == '56' |
---|
2012 | |
---|
2013 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2014 |     assert L[0].strip().lower() == 'datum' |
---|
2015 |     assert L[1].strip().lower() == 'wgs84' |
---|
2016 | |
---|
2017 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2018 |     assert L[0].strip().lower() == 'zunits' |
---|
2019 |     assert L[1].strip().lower() == 'no' |
---|
2020 | |
---|
2021 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2022 |     assert L[0].strip().lower() == 'units' |
---|
2023 |     assert L[1].strip().lower() == 'meters' |
---|
2024 | |
---|
2025 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2026 |     assert L[0].strip().lower() == 'spheroid' |
---|
2027 |     assert L[1].strip().lower() == 'wgs84' |
---|
2028 | |
---|
2029 | Â Â Â Â L =Â lines[6].strip().split() |
---|
2030 |     assert L[0].strip().lower() == 'xshift' |
---|
2031 |     assert L[1].strip().lower() == '500000' |
---|
2032 | |
---|
2033 | Â Â Â Â L =Â lines[7].strip().split() |
---|
2034 |     assert L[0].strip().lower() == 'yshift' |
---|
2035 |     assert L[1].strip().lower() == '10000000' |
---|
2036 | |
---|
2037 | Â Â Â Â L =Â lines[8].strip().split() |
---|
2038 |     assert L[0].strip().lower() == 'parameters' |
---|
2039 | |
---|
2040 | |
---|
2041 | Â Â Â Â #Check asc file |
---|
2042 | Â Â Â Â ascid =Â open(ascfile) |
---|
2043 | Â Â Â Â lines =Â ascid.readlines() |
---|
2044 | Â Â Â Â ascid.close() |
---|
2045 | |
---|
2046 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2047 |     assert L[0].strip().lower() == 'ncols' |
---|
2048 |     assert L[1].strip().lower() == '11' |
---|
2049 | |
---|
2050 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2051 |     assert L[0].strip().lower() == 'nrows' |
---|
2052 |     assert L[1].strip().lower() == '11' |
---|
2053 | |
---|
2054 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2055 |     assert L[0].strip().lower() == 'xllcorner' |
---|
2056 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
2057 | |
---|
2058 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2059 |     assert L[0].strip().lower() == 'yllcorner' |
---|
2060 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
2061 | |
---|
2062 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2063 |     assert L[0].strip().lower() == 'cellsize' |
---|
2064 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
2065 | |
---|
2066 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2067 |     assert L[0].strip() == 'NODATA_value' |
---|
2068 |     assert L[1].strip().lower() == '-9999' |
---|
2069 | |
---|
2070 | Â Â Â Â #Check grid values (FIXME: Use same strategy for other sww2dem tests) |
---|
2071 |     for i, line in enumerate(lines[6:]): |
---|
2072 |       for j, value in enumerate( line.split() ): |
---|
2073 | Â Â Â Â Â Â Â Â #assert allclose(float(value), -(10-i+j)*cellsize) |
---|
2074 |         assert float(value) == -(10-i+j)*cellsize |
---|
2075 | |
---|
2076 | |
---|
2077 | Â Â Â Â fid.close() |
---|
2078 | |
---|
2079 | Â Â Â Â #Cleanup |
---|
2080 | Â Â Â Â os.remove(prjfile) |
---|
2081 | Â Â Â Â os.remove(ascfile) |
---|
2082 | Â Â Â Â os.remove(swwfile) |
---|
2083 | |
---|
2084 | |
---|
2085 | |
---|
2086 | |
---|
2087 |   def test_sww2dem_boundingbox(self): |
---|
2088 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
2089 | Â Â Â Â format readable by e.g. ArcView. |
---|
2090 | Â Â Â Â This will test that mesh can be restricted by bounding box |
---|
2091 | |
---|
2092 | Â Â Â Â Original extent is 100m x 100m: |
---|
2093 | |
---|
2094 | Â Â Â Â Eastings:Â Â 308500 -Â 308600 |
---|
2095 | Â Â Â Â Northings: 6189000 - 6189100 |
---|
2096 | |
---|
2097 | Â Â Â Â Bounding box changes this to the 50m x 50m square defined by |
---|
2098 | |
---|
2099 | Â Â Â Â Eastings:Â Â 308530 -Â 308570 |
---|
2100 | Â Â Â Â Northings: 6189050 - 6189100 |
---|
2101 | |
---|
2102 | Â Â Â Â The cropped values should be |
---|
2103 | |
---|
2104 | Â Â Â Â Â -130 -140 -150 -160 -170 |
---|
2105 | Â Â Â Â Â -120 -130 -140 -150 -160 |
---|
2106 | Â Â Â Â Â -110 -120 -130 -140 -150 |
---|
2107 | Â Â Â Â Â -100 -110 -120 -130 -140 |
---|
2108 | Â Â Â Â Â -90 -100 -110 -120 -130 |
---|
2109 | Â Â Â Â Â -80Â -90 -100 -110 -120 |
---|
2110 | |
---|
2111 | Â Â Â Â and the new lower reference point should be |
---|
2112 | Â Â Â Â Eastings:Â Â 308530 |
---|
2113 | Â Â Â Â Northings: 6189050 |
---|
2114 | |
---|
2115 | Â Â Â Â Original dataset is the same as in test_sww2dem_larger() |
---|
2116 | |
---|
2117 | Â Â Â Â """ |
---|
2118 | |
---|
2119 |     import time, os |
---|
2120 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
2121 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2122 | |
---|
2123 | Â Â Â Â #Setup |
---|
2124 | |
---|
2125 |     from mesh_factory import rectangular |
---|
2126 | |
---|
2127 | Â Â Â Â #Create basic mesh (100m x 100m) |
---|
2128 |     points, vertices, boundary = rectangular(2, 2, 100, 100) |
---|
2129 | |
---|
2130 | Â Â Â Â #Create shallow water domain |
---|
2131 |     domain = Domain(points, vertices, boundary) |
---|
2132 | Â Â Â Â domain.default_order =Â 2 |
---|
2133 | |
---|
2134 | Â Â Â Â domain.set_name('datatest') |
---|
2135 | |
---|
2136 | Â Â Â Â prjfile =Â domain.get_name()Â +Â '_elevation.prj' |
---|
2137 | Â Â Â Â ascfile =Â domain.get_name()Â +Â '_elevation.asc' |
---|
2138 | Â Â Â Â swwfile =Â domain.get_name()Â +Â '.sww' |
---|
2139 | |
---|
2140 | Â Â Â Â domain.set_datadir('.') |
---|
2141 | Â Â Â Â domain.format =Â 'sww' |
---|
2142 | Â Â Â Â domain.smooth =Â True |
---|
2143 |     domain.geo_reference = Geo_reference(56, 308500, 6189000) |
---|
2144 | |
---|
2145 | Â Â Â Â # |
---|
2146 |     domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
2147 |     domain.set_quantity('stage', 0) |
---|
2148 | |
---|
2149 | Â Â Â Â B =Â Transmissive_boundary(domain) |
---|
2150 |     domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
2151 | |
---|
2152 | |
---|
2153 | Â Â Â Â # |
---|
2154 | Â Â Â Â sww =Â get_dataobject(domain) |
---|
2155 | Â Â Â Â sww.store_connectivity() |
---|
2156 | Â Â Â Â sww.store_timestep() |
---|
2157 | |
---|
2158 | Â Â Â Â #domain.tight_slope_limiters = 1 |
---|
2159 | Â Â Â Â domain.evolve_to_end(finaltime =Â 0.01) |
---|
2160 | Â Â Â Â sww.store_timestep() |
---|
2161 | |
---|
2162 | Â Â Â Â cellsize =Â 10Â #10m grid |
---|
2163 | |
---|
2164 | |
---|
2165 | Â Â Â Â #Check contents |
---|
2166 | Â Â Â Â #Get NetCDF |
---|
2167 | |
---|
2168 |     fid = NetCDFFile(sww.filename, 'r') |
---|
2169 | |
---|
2170 | Â Â Â Â # Get the variables |
---|
2171 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2172 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2173 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
2174 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
2175 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
2176 | |
---|
2177 | |
---|
2178 | Â Â Â Â #Export to ascii/prj files |
---|
2179 | Â Â Â Â sww2dem(domain.get_name(), |
---|
2180 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
2181 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
2182 | Â Â Â Â Â Â Â Â easting_min =Â 308530, |
---|
2183 | Â Â Â Â Â Â Â Â easting_max =Â 308570, |
---|
2184 | Â Â Â Â Â Â Â Â northing_min =Â 6189050, |
---|
2185 | Â Â Â Â Â Â Â Â northing_max =Â 6189100, |
---|
2186 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
2187 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
2188 | |
---|
2189 | Â Â Â Â fid.close() |
---|
2190 | |
---|
2191 | |
---|
2192 | Â Â Â Â #Check prj (meta data) |
---|
2193 | Â Â Â Â prjid =Â open(prjfile) |
---|
2194 | Â Â Â Â lines =Â prjid.readlines() |
---|
2195 | Â Â Â Â prjid.close() |
---|
2196 | |
---|
2197 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2198 |     assert L[0].strip().lower() == 'projection' |
---|
2199 |     assert L[1].strip().lower() == 'utm' |
---|
2200 | |
---|
2201 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2202 |     assert L[0].strip().lower() == 'zone' |
---|
2203 |     assert L[1].strip().lower() == '56' |
---|
2204 | |
---|
2205 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2206 |     assert L[0].strip().lower() == 'datum' |
---|
2207 |     assert L[1].strip().lower() == 'wgs84' |
---|
2208 | |
---|
2209 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2210 |     assert L[0].strip().lower() == 'zunits' |
---|
2211 |     assert L[1].strip().lower() == 'no' |
---|
2212 | |
---|
2213 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2214 |     assert L[0].strip().lower() == 'units' |
---|
2215 |     assert L[1].strip().lower() == 'meters' |
---|
2216 | |
---|
2217 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2218 |     assert L[0].strip().lower() == 'spheroid' |
---|
2219 |     assert L[1].strip().lower() == 'wgs84' |
---|
2220 | |
---|
2221 | Â Â Â Â L =Â lines[6].strip().split() |
---|
2222 |     assert L[0].strip().lower() == 'xshift' |
---|
2223 |     assert L[1].strip().lower() == '500000' |
---|
2224 | |
---|
2225 | Â Â Â Â L =Â lines[7].strip().split() |
---|
2226 |     assert L[0].strip().lower() == 'yshift' |
---|
2227 |     assert L[1].strip().lower() == '10000000' |
---|
2228 | |
---|
2229 | Â Â Â Â L =Â lines[8].strip().split() |
---|
2230 |     assert L[0].strip().lower() == 'parameters' |
---|
2231 | |
---|
2232 | |
---|
2233 | Â Â Â Â #Check asc file |
---|
2234 | Â Â Â Â ascid =Â open(ascfile) |
---|
2235 | Â Â Â Â lines =Â ascid.readlines() |
---|
2236 | Â Â Â Â ascid.close() |
---|
2237 | |
---|
2238 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2239 |     assert L[0].strip().lower() == 'ncols' |
---|
2240 |     assert L[1].strip().lower() == '5' |
---|
2241 | |
---|
2242 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2243 |     assert L[0].strip().lower() == 'nrows' |
---|
2244 |     assert L[1].strip().lower() == '6' |
---|
2245 | |
---|
2246 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2247 |     assert L[0].strip().lower() == 'xllcorner' |
---|
2248 |     assert allclose(float(L[1].strip().lower()), 308530) |
---|
2249 | |
---|
2250 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2251 |     assert L[0].strip().lower() == 'yllcorner' |
---|
2252 |     assert allclose(float(L[1].strip().lower()), 6189050) |
---|
2253 | |
---|
2254 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2255 |     assert L[0].strip().lower() == 'cellsize' |
---|
2256 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
2257 | |
---|
2258 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2259 |     assert L[0].strip() == 'NODATA_value' |
---|
2260 |     assert L[1].strip().lower() == '-9999' |
---|
2261 | |
---|
2262 | Â Â Â Â #Check grid values |
---|
2263 |     for i, line in enumerate(lines[6:]): |
---|
2264 |       for j, value in enumerate( line.split() ): |
---|
2265 | Â Â Â Â Â Â Â Â #assert float(value) == -(10-i+j)*cellsize |
---|
2266 |         assert float(value) == -(10-i+j+3)*cellsize |
---|
2267 | |
---|
2268 | |
---|
2269 | |
---|
2270 | Â Â Â Â #Cleanup |
---|
2271 | Â Â Â Â os.remove(prjfile) |
---|
2272 | Â Â Â Â os.remove(ascfile) |
---|
2273 | Â Â Â Â os.remove(swwfile) |
---|
2274 | |
---|
2275 | |
---|
2276 | |
---|
2277 |   def test_sww2dem_asc_stage_reduction(self): |
---|
2278 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
2279 | Â Â Â Â format readable by e.g. ArcView |
---|
2280 | |
---|
2281 | Â Â Â Â This tests the reduction of quantity stage using min |
---|
2282 | Â Â Â Â """ |
---|
2283 | |
---|
2284 |     import time, os |
---|
2285 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
2286 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2287 | |
---|
2288 | Â Â Â Â #Setup |
---|
2289 | Â Â Â Â self.domain.set_name('datatest') |
---|
2290 | |
---|
2291 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_stage.prj' |
---|
2292 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_stage.asc' |
---|
2293 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
2294 | |
---|
2295 | Â Â Â Â self.domain.set_datadir('.') |
---|
2296 | Â Â Â Â self.domain.format =Â 'sww' |
---|
2297 | Â Â Â Â self.domain.smooth =Â True |
---|
2298 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
2299 | |
---|
2300 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
2301 | |
---|
2302 | |
---|
2303 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
2304 | Â Â Â Â sww.store_connectivity() |
---|
2305 | Â Â Â Â sww.store_timestep() |
---|
2306 | |
---|
2307 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
2308 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
2309 | Â Â Â Â sww.store_timestep() |
---|
2310 | |
---|
2311 | Â Â Â Â cellsize =Â 0.25 |
---|
2312 | Â Â Â Â #Check contents |
---|
2313 | Â Â Â Â #Get NetCDF |
---|
2314 | |
---|
2315 |     fid = NetCDFFile(sww.filename, 'r') |
---|
2316 | |
---|
2317 | Â Â Â Â # Get the variables |
---|
2318 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2319 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2320 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
2321 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
2322 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
2323 | |
---|
2324 | |
---|
2325 | Â Â Â Â #Export to ascii/prj files |
---|
2326 | Â Â Â Â sww2dem(self.domain.get_name(), |
---|
2327 | Â Â Â Â Â Â Â Â quantity =Â 'stage', |
---|
2328 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
2329 | Â Â Â Â Â Â Â Â reduction =Â min, |
---|
2330 | Â Â Â Â Â Â Â Â format =Â 'asc', |
---|
2331 | Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
2332 | |
---|
2333 | |
---|
2334 | Â Â Â Â #Check asc file |
---|
2335 | Â Â Â Â ascid =Â open(ascfile) |
---|
2336 | Â Â Â Â lines =Â ascid.readlines() |
---|
2337 | Â Â Â Â ascid.close() |
---|
2338 | |
---|
2339 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2340 |     assert L[0].strip().lower() == 'ncols' |
---|
2341 |     assert L[1].strip().lower() == '5' |
---|
2342 | |
---|
2343 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2344 |     assert L[0].strip().lower() == 'nrows' |
---|
2345 |     assert L[1].strip().lower() == '5' |
---|
2346 | |
---|
2347 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2348 |     assert L[0].strip().lower() == 'xllcorner' |
---|
2349 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
2350 | |
---|
2351 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2352 |     assert L[0].strip().lower() == 'yllcorner' |
---|
2353 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
2354 | |
---|
2355 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2356 |     assert L[0].strip().lower() == 'cellsize' |
---|
2357 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
2358 | |
---|
2359 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2360 |     assert L[0].strip() == 'NODATA_value' |
---|
2361 |     assert L[1].strip().lower() == '-9999' |
---|
2362 | |
---|
2363 | |
---|
2364 | Â Â Â Â #Check grid values (where applicable) |
---|
2365 |     for j in range(5): |
---|
2366 |       if j%2 == 0: |
---|
2367 | Â Â Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
2368 | Â Â Â Â Â Â Â Â jj =Â 4-j |
---|
2369 |         for i in range(5): |
---|
2370 |           if i%2 == 0: |
---|
2371 | Â Â Â Â Â Â Â Â Â Â Â Â index =Â jj/2Â +Â i/2*3 |
---|
2372 | Â Â Â Â Â Â Â Â Â Â Â Â val0 =Â stage[0,index] |
---|
2373 | Â Â Â Â Â Â Â Â Â Â Â Â val1 =Â stage[1,index] |
---|
2374 | |
---|
2375 | Â Â Â Â Â Â Â Â Â Â Â Â #print i, j, index, ':', L[i], val0, val1 |
---|
2376 |             assert allclose(float(L[i]), min(val0, val1)) |
---|
2377 | |
---|
2378 | |
---|
2379 | Â Â Â Â fid.close() |
---|
2380 | |
---|
2381 | Â Â Â Â #Cleanup |
---|
2382 | Â Â Â Â os.remove(prjfile) |
---|
2383 | Â Â Â Â os.remove(ascfile) |
---|
2384 | Â Â Â Â os.remove(swwfile) |
---|
2385 | |
---|
2386 | |
---|
2387 | |
---|
2388 |   def test_sww2dem_asc_derived_quantity(self): |
---|
2389 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
2390 | Â Â Â Â format readable by e.g. ArcView |
---|
2391 | |
---|
2392 | Â Â Â Â This tests the use of derived quantities |
---|
2393 | Â Â Â Â """ |
---|
2394 | |
---|
2395 |     import time, os |
---|
2396 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
2397 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2398 | |
---|
2399 | Â Â Â Â #Setup |
---|
2400 | Â Â Â Â self.domain.set_name('datatest') |
---|
2401 | |
---|
2402 | Â Â Â Â prjfile =Â self.domain.get_name()Â +Â '_depth.prj' |
---|
2403 | Â Â Â Â ascfile =Â self.domain.get_name()Â +Â '_depth.asc' |
---|
2404 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
2405 | |
---|
2406 | Â Â Â Â self.domain.set_datadir('.') |
---|
2407 | Â Â Â Â self.domain.format =Â 'sww' |
---|
2408 | Â Â Â Â self.domain.smooth =Â True |
---|
2409 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
2410 |     self.domain.set_quantity('stage', 0.0) |
---|
2411 | |
---|
2412 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
2413 | |
---|
2414 | |
---|
2415 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
2416 | Â Â Â Â sww.store_connectivity() |
---|
2417 | Â Â Â Â sww.store_timestep() |
---|
2418 | |
---|
2419 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
2420 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
2421 | Â Â Â Â sww.store_timestep() |
---|
2422 | |
---|
2423 | Â Â Â Â cellsize =Â 0.25 |
---|
2424 | Â Â Â Â #Check contents |
---|
2425 | Â Â Â Â #Get NetCDF |
---|
2426 | |
---|
2427 |     fid = NetCDFFile(sww.filename, 'r') |
---|
2428 | |
---|
2429 | Â Â Â Â # Get the variables |
---|
2430 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2431 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2432 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
2433 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
2434 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
2435 | |
---|
2436 | |
---|
2437 | Â Â Â Â #Export to ascii/prj files |
---|
2438 | Â Â Â Â sww2dem(self.domain.get_name(), |
---|
2439 | Â Â Â Â Â Â Â Â basename_out =Â 'datatest_depth', |
---|
2440 | Â Â Â Â Â Â Â Â quantity =Â 'stage - elevation', |
---|
2441 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
2442 | Â Â Â Â Â Â Â Â reduction =Â min, |
---|
2443 | Â Â Â Â Â Â Â Â format =Â 'asc', |
---|
2444 | Â Â Â Â Â Â Â Â verbose =Â self.verbose) |
---|
2445 | |
---|
2446 | |
---|
2447 | Â Â Â Â #Check asc file |
---|
2448 | Â Â Â Â ascid =Â open(ascfile) |
---|
2449 | Â Â Â Â lines =Â ascid.readlines() |
---|
2450 | Â Â Â Â ascid.close() |
---|
2451 | |
---|
2452 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2453 |     assert L[0].strip().lower() == 'ncols' |
---|
2454 |     assert L[1].strip().lower() == '5' |
---|
2455 | |
---|
2456 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2457 |     assert L[0].strip().lower() == 'nrows' |
---|
2458 |     assert L[1].strip().lower() == '5' |
---|
2459 | |
---|
2460 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2461 |     assert L[0].strip().lower() == 'xllcorner' |
---|
2462 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
2463 | |
---|
2464 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2465 |     assert L[0].strip().lower() == 'yllcorner' |
---|
2466 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
2467 | |
---|
2468 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2469 |     assert L[0].strip().lower() == 'cellsize' |
---|
2470 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
2471 | |
---|
2472 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2473 |     assert L[0].strip() == 'NODATA_value' |
---|
2474 |     assert L[1].strip().lower() == '-9999' |
---|
2475 | |
---|
2476 | |
---|
2477 | Â Â Â Â #Check grid values (where applicable) |
---|
2478 |     for j in range(5): |
---|
2479 |       if j%2 == 0: |
---|
2480 | Â Â Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
2481 | Â Â Â Â Â Â Â Â jj =Â 4-j |
---|
2482 |         for i in range(5): |
---|
2483 |           if i%2 == 0: |
---|
2484 | Â Â Â Â Â Â Â Â Â Â Â Â index =Â jj/2Â +Â i/2*3 |
---|
2485 | Â Â Â Â Â Â Â Â Â Â Â Â val0 =Â stage[0,index]Â -Â z[index] |
---|
2486 | Â Â Â Â Â Â Â Â Â Â Â Â val1 =Â stage[1,index]Â -Â z[index] |
---|
2487 | |
---|
2488 | Â Â Â Â Â Â Â Â Â Â Â Â #print i, j, index, ':', L[i], val0, val1 |
---|
2489 |             assert allclose(float(L[i]), min(val0, val1)) |
---|
2490 | |
---|
2491 | |
---|
2492 | Â Â Â Â fid.close() |
---|
2493 | |
---|
2494 | Â Â Â Â #Cleanup |
---|
2495 | Â Â Â Â os.remove(prjfile) |
---|
2496 | Â Â Â Â os.remove(ascfile) |
---|
2497 | Â Â Â Â os.remove(swwfile) |
---|
2498 | |
---|
2499 | |
---|
2500 | |
---|
2501 | |
---|
2502 | |
---|
2503 |   def test_sww2dem_asc_missing_points(self): |
---|
2504 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
2505 | Â Â Â Â format readable by e.g. ArcView |
---|
2506 | |
---|
2507 | Â Â Â Â This test includes the writing of missing values |
---|
2508 | Â Â Â Â """ |
---|
2509 | |
---|
2510 |     import time, os |
---|
2511 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
2512 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2513 | |
---|
2514 | Â Â Â Â #Setup mesh not coinciding with rectangle. |
---|
2515 | Â Â Â Â #This will cause missing values to occur in gridded data |
---|
2516 | |
---|
2517 | |
---|
2518 |     points = [            [1.0, 1.0], |
---|
2519 |                [0.5, 0.5], [1.0, 0.5], |
---|
2520 |          [0.0, 0.0], [0.5, 0.0], [1.0, 0.0]] |
---|
2521 | |
---|
2522 |     vertices = [ [4,1,3], [5,2,4], [1,4,2], [2,0,1]] |
---|
2523 | |
---|
2524 | Â Â Â Â #Create shallow water domain |
---|
2525 |     domain = Domain(points, vertices) |
---|
2526 | Â Â Â Â domain.default_order=2 |
---|
2527 | |
---|
2528 | |
---|
2529 | Â Â Â Â #Set some field values |
---|
2530 |     domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
2531 |     domain.set_quantity('friction', 0.03) |
---|
2532 | |
---|
2533 | |
---|
2534 | Â Â Â Â ###################### |
---|
2535 | Â Â Â Â # Boundary conditions |
---|
2536 | Â Â Â Â B =Â Transmissive_boundary(domain) |
---|
2537 | Â Â Â Â domain.set_boundary(Â {'exterior':Â B}Â ) |
---|
2538 | |
---|
2539 | |
---|
2540 | Â Â Â Â ###################### |
---|
2541 | Â Â Â Â #Initial condition - with jumps |
---|
2542 | |
---|
2543 | Â Â Â Â bed =Â domain.quantities['elevation'].vertex_values |
---|
2544 |     stage = zeros(bed.shape, Float) |
---|
2545 | |
---|
2546 | Â Â Â Â h =Â 0.3 |
---|
2547 |     for i in range(stage.shape[0]): |
---|
2548 |       if i % 2 == 0: |
---|
2549 | Â Â Â Â Â Â Â Â stage[i,:]Â =Â bed[i,:]Â +Â h |
---|
2550 | Â Â Â Â Â Â else: |
---|
2551 | Â Â Â Â Â Â Â Â stage[i,:]Â =Â bed[i,:] |
---|
2552 | |
---|
2553 |     domain.set_quantity('stage', stage) |
---|
2554 | Â Â Â Â domain.distribute_to_vertices_and_edges() |
---|
2555 | |
---|
2556 | Â Â Â Â domain.set_name('datatest') |
---|
2557 | |
---|
2558 | Â Â Â Â prjfile =Â domain.get_name()Â +Â '_elevation.prj' |
---|
2559 | Â Â Â Â ascfile =Â domain.get_name()Â +Â '_elevation.asc' |
---|
2560 | Â Â Â Â swwfile =Â domain.get_name()Â +Â '.sww' |
---|
2561 | |
---|
2562 | Â Â Â Â domain.set_datadir('.') |
---|
2563 | Â Â Â Â domain.format =Â 'sww' |
---|
2564 | Â Â Â Â domain.smooth =Â True |
---|
2565 | |
---|
2566 | Â Â Â Â domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
2567 | |
---|
2568 | Â Â Â Â sww =Â get_dataobject(domain) |
---|
2569 | Â Â Â Â sww.store_connectivity() |
---|
2570 | Â Â Â Â sww.store_timestep() |
---|
2571 | |
---|
2572 | Â Â Â Â cellsize =Â 0.25 |
---|
2573 | Â Â Â Â #Check contents |
---|
2574 | Â Â Â Â #Get NetCDF |
---|
2575 | |
---|
2576 |     fid = NetCDFFile(swwfile, 'r') |
---|
2577 | |
---|
2578 | Â Â Â Â # Get the variables |
---|
2579 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2580 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2581 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
2582 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
2583 | |
---|
2584 | Â Â Â Â try: |
---|
2585 | Â Â Â Â Â Â geo_reference =Â Geo_reference(NetCDFObject=fid) |
---|
2586 |     except AttributeError, e: |
---|
2587 | Â Â Â Â Â Â geo_reference =Â Geo_reference(DEFAULT_ZONE,0,0) |
---|
2588 | |
---|
2589 | Â Â Â Â #Export to ascii/prj files |
---|
2590 | Â Â Â Â sww2dem(domain.get_name(), |
---|
2591 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
2592 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
2593 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
2594 | Â Â Â Â Â Â Â Â format =Â 'asc') |
---|
2595 | |
---|
2596 | |
---|
2597 | Â Â Â Â #Check asc file |
---|
2598 | Â Â Â Â ascid =Â open(ascfile) |
---|
2599 | Â Â Â Â lines =Â ascid.readlines() |
---|
2600 | Â Â Â Â ascid.close() |
---|
2601 | |
---|
2602 | Â Â Â Â L =Â lines[0].strip().split() |
---|
2603 |     assert L[0].strip().lower() == 'ncols' |
---|
2604 |     assert L[1].strip().lower() == '5' |
---|
2605 | |
---|
2606 | Â Â Â Â L =Â lines[1].strip().split() |
---|
2607 |     assert L[0].strip().lower() == 'nrows' |
---|
2608 |     assert L[1].strip().lower() == '5' |
---|
2609 | |
---|
2610 | Â Â Â Â L =Â lines[2].strip().split() |
---|
2611 |     assert L[0].strip().lower() == 'xllcorner' |
---|
2612 |     assert allclose(float(L[1].strip().lower()), 308500) |
---|
2613 | |
---|
2614 | Â Â Â Â L =Â lines[3].strip().split() |
---|
2615 |     assert L[0].strip().lower() == 'yllcorner' |
---|
2616 |     assert allclose(float(L[1].strip().lower()), 6189000) |
---|
2617 | |
---|
2618 | Â Â Â Â L =Â lines[4].strip().split() |
---|
2619 |     assert L[0].strip().lower() == 'cellsize' |
---|
2620 |     assert allclose(float(L[1].strip().lower()), cellsize) |
---|
2621 | |
---|
2622 | Â Â Â Â L =Â lines[5].strip().split() |
---|
2623 |     assert L[0].strip() == 'NODATA_value' |
---|
2624 |     assert L[1].strip().lower() == '-9999' |
---|
2625 | |
---|
2626 | Â Â Â Â #Check grid values |
---|
2627 |     for j in range(5): |
---|
2628 | Â Â Â Â Â Â L =Â lines[6+j].strip().split() |
---|
2629 |       assert len(L) == 5 |
---|
2630 | Â Â Â Â Â Â y =Â (4-j)Â *Â cellsize |
---|
2631 | |
---|
2632 |       for i in range(5): |
---|
2633 | Â Â Â Â Â Â Â Â #print i |
---|
2634 |         if i+j >= 4: |
---|
2635 |           assert allclose(float(L[i]), -i*cellsize - y) |
---|
2636 | Â Â Â Â Â Â Â Â else: |
---|
2637 | Â Â Â Â Â Â Â Â Â Â #Missing values |
---|
2638 |           assert allclose(float(L[i]), -9999) |
---|
2639 | |
---|
2640 | |
---|
2641 | |
---|
2642 | Â Â Â Â fid.close() |
---|
2643 | |
---|
2644 | Â Â Â Â #Cleanup |
---|
2645 | Â Â Â Â os.remove(prjfile) |
---|
2646 | Â Â Â Â os.remove(ascfile) |
---|
2647 | Â Â Â Â os.remove(swwfile) |
---|
2648 | |
---|
2649 |   def test_sww2ers_simple(self): |
---|
2650 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
2651 | Â Â Â Â format readable by e.g. ArcView |
---|
2652 | Â Â Â Â """ |
---|
2653 | |
---|
2654 |     import time, os |
---|
2655 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
2656 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2657 | |
---|
2658 | |
---|
2659 | Â Â Â Â NODATA_value =Â 1758323 |
---|
2660 | |
---|
2661 | Â Â Â Â #Setup |
---|
2662 | Â Â Â Â self.domain.set_name('datatest') |
---|
2663 | |
---|
2664 | Â Â Â Â headerfile =Â self.domain.get_name()Â +Â '.ers' |
---|
2665 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
2666 | |
---|
2667 | Â Â Â Â self.domain.set_datadir('.') |
---|
2668 | Â Â Â Â self.domain.format =Â 'sww' |
---|
2669 | Â Â Â Â self.domain.smooth =Â True |
---|
2670 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
2671 | |
---|
2672 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
2673 | |
---|
2674 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
2675 | Â Â Â Â sww.store_connectivity() |
---|
2676 | Â Â Â Â sww.store_timestep() |
---|
2677 | |
---|
2678 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
2679 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
2680 | Â Â Â Â sww.store_timestep() |
---|
2681 | |
---|
2682 | Â Â Â Â cellsize =Â 0.25 |
---|
2683 | Â Â Â Â #Check contents |
---|
2684 | Â Â Â Â #Get NetCDF |
---|
2685 | |
---|
2686 |     fid = NetCDFFile(sww.filename, 'r') |
---|
2687 | |
---|
2688 | Â Â Â Â # Get the variables |
---|
2689 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2690 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2691 | Â Â Â Â z =Â fid.variables['elevation'][:] |
---|
2692 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
2693 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
2694 | |
---|
2695 | |
---|
2696 | Â Â Â Â #Export to ers files |
---|
2697 | Â Â Â Â sww2dem(self.domain.get_name(), |
---|
2698 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
2699 | Â Â Â Â Â Â Â Â cellsize =Â cellsize, |
---|
2700 | Â Â Â Â Â Â Â Â NODATA_value =Â NODATA_value, |
---|
2701 | Â Â Â Â Â Â Â Â verbose =Â self.verbose, |
---|
2702 | Â Â Â Â Â Â Â Â format =Â 'ers') |
---|
2703 | |
---|
2704 | Â Â Â Â #Check header data |
---|
2705 |     from ermapper_grids import read_ermapper_header, read_ermapper_data |
---|
2706 | |
---|
2707 | Â Â Â Â header =Â read_ermapper_header(self.domain.get_name()Â +Â '_elevation.ers') |
---|
2708 | Â Â Â Â #print header |
---|
2709 |     assert header['projection'].lower() == '"utm-56"' |
---|
2710 |     assert header['datum'].lower() == '"wgs84"' |
---|
2711 |     assert header['units'].lower() == '"meters"' |
---|
2712 |     assert header['value'].lower() == '"elevation"' |
---|
2713 |     assert header['xdimension'] == '0.25' |
---|
2714 |     assert header['ydimension'] == '0.25' |
---|
2715 |     assert float(header['eastings']) == 308500.0  #xllcorner |
---|
2716 |     assert float(header['northings']) == 6189000.0 #yllcorner |
---|
2717 |     assert int(header['nroflines']) == 5 |
---|
2718 |     assert int(header['nrofcellsperline']) == 5 |
---|
2719 |     assert int(header['nullcellvalue']) == NODATA_value |
---|
2720 | Â Â Â Â #FIXME - there is more in the header |
---|
2721 | |
---|
2722 | |
---|
2723 | Â Â Â Â #Check grid data |
---|
2724 | Â Â Â Â grid =Â read_ermapper_data(self.domain.get_name()Â +Â '_elevation') |
---|
2725 | |
---|
2726 | Â Â Â Â #FIXME (Ole): Why is this the desired reference grid for -x-y? |
---|
2727 |     ref_grid = [NODATA_value, NODATA_value, NODATA_value, NODATA_value, NODATA_value, |
---|
2728 |           -1,  -1.25, -1.5, -1.75, -2.0, |
---|
2729 |           -0.75, -1.0, -1.25, -1.5, -1.75, |
---|
2730 |           -0.5, -0.75, -1.0, -1.25, -1.5, |
---|
2731 |           -0.25, -0.5, -0.75, -1.0, -1.25] |
---|
2732 | |
---|
2733 | |
---|
2734 | Â Â Â Â #print grid |
---|
2735 |     assert allclose(grid, ref_grid) |
---|
2736 | |
---|
2737 | Â Â Â Â fid.close() |
---|
2738 | |
---|
2739 | Â Â Â Â #Cleanup |
---|
2740 | Â Â Â Â #FIXME the file clean-up doesn't work (eg Permission Denied Error) |
---|
2741 | Â Â Â Â #Done (Ole) - it was because sww2ers didn't close it's sww file |
---|
2742 | Â Â Â Â os.remove(sww.filename) |
---|
2743 | Â Â Â Â os.remove(self.domain.get_name()Â +Â '_elevation') |
---|
2744 | Â Â Â Â os.remove(self.domain.get_name()Â +Â '_elevation.ers') |
---|
2745 | |
---|
2746 | |
---|
2747 | |
---|
2748 |   def test_sww2pts_centroids(self): |
---|
2749 | Â Â Â Â """Test that sww information can be converted correctly to pts data at specified coordinates |
---|
2750 | Â Â Â Â - in this case, the centroids. |
---|
2751 | Â Â Â Â """ |
---|
2752 | |
---|
2753 |     import time, os |
---|
2754 |     from Numeric import array, zeros, allclose, Float, concatenate, NewAxis |
---|
2755 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2756 | Â Â Â Â # Used for points that lie outside mesh |
---|
2757 | Â Â Â Â NODATA_value =Â 1758323 |
---|
2758 | |
---|
2759 | Â Â Â Â # Setup |
---|
2760 | Â Â Â Â self.domain.set_name('datatest') |
---|
2761 | |
---|
2762 | Â Â Â Â ptsfile =Â self.domain.get_name()Â +Â '_elevation.pts' |
---|
2763 | Â Â Â Â swwfile =Â self.domain.get_name()Â +Â '.sww' |
---|
2764 | |
---|
2765 | Â Â Â Â self.domain.set_datadir('.') |
---|
2766 | Â Â Â Â self.domain.format =Â 'sww' |
---|
2767 |     self.smooth = True #self.set_store_vertices_uniquely(False) |
---|
2768 |     self.domain.set_quantity('elevation', lambda x,y: -x-y) |
---|
2769 | |
---|
2770 | Â Â Â Â self.domain.geo_reference =Â Geo_reference(56,308500,6189000) |
---|
2771 | |
---|
2772 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
2773 | Â Â Â Â sww.store_connectivity() |
---|
2774 | Â Â Â Â sww.store_timestep() |
---|
2775 | |
---|
2776 | Â Â Â Â #self.domain.tight_slope_limiters = 1 |
---|
2777 | Â Â Â Â self.domain.evolve_to_end(finaltime =Â 0.01) |
---|
2778 | Â Â Â Â sww.store_timestep() |
---|
2779 | |
---|
2780 | Â Â Â Â # Check contents in NetCDF |
---|
2781 |     fid = NetCDFFile(sww.filename, 'r') |
---|
2782 | |
---|
2783 | Â Â Â Â # Get the variables |
---|
2784 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2785 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2786 | Â Â Â Â elevation =Â fid.variables['elevation'][:] |
---|
2787 | Â Â Â Â time =Â fid.variables['time'][:] |
---|
2788 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
2789 | |
---|
2790 | Â Â Â Â volumes =Â fid.variables['volumes'][:] |
---|
2791 | |
---|
2792 | |
---|
2793 |     # Invoke interpolation for vertex points    |
---|
2794 |     points = concatenate( (x[:,NewAxis],y[:,NewAxis]), axis=1 ) |
---|
2795 | Â Â Â Â sww2pts(self.domain.get_name(), |
---|
2796 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
2797 | Â Â Â Â Â Â Â Â data_points =Â points, |
---|
2798 | Â Â Â Â Â Â Â Â NODATA_value =Â NODATA_value, |
---|
2799 | Â Â Â Â Â Â Â Â verbose =Â self.verbose) |
---|
2800 | Â Â Â Â ref_point_values =Â elevation |
---|
2801 | Â Â Â Â point_values =Â Geospatial_data(ptsfile).get_attributes() |
---|
2802 | Â Â Â Â #print 'P', point_values |
---|
2803 |     #print 'Ref', ref_point_values    |
---|
2804 |     assert allclose(point_values, ref_point_values)    |
---|
2805 | |
---|
2806 | |
---|
2807 | |
---|
2808 | Â Â Â Â # Invoke interpolation for centroids |
---|
2809 | Â Â Â Â points =Â self.domain.get_centroid_coordinates() |
---|
2810 | Â Â Â Â #print points |
---|
2811 | Â Â Â Â sww2pts(self.domain.get_name(), |
---|
2812 | Â Â Â Â Â Â Â Â quantity =Â 'elevation', |
---|
2813 | Â Â Â Â Â Â Â Â data_points =Â points, |
---|
2814 | Â Â Â Â Â Â Â Â NODATA_value =Â NODATA_value, |
---|
2815 | Â Â Â Â Â Â Â Â verbose =Â self.verbose) |
---|
2816 |     ref_point_values = [-0.5, -0.5, -1, -1, -1, -1, -1.5, -1.5]  #At centroids |
---|
2817 | |
---|
2818 | Â Â Â Â |
---|
2819 | Â Â Â Â point_values =Â Geospatial_data(ptsfile).get_attributes() |
---|
2820 | Â Â Â Â #print 'P', point_values |
---|
2821 |     #print 'Ref', ref_point_values    |
---|
2822 |     assert allclose(point_values, ref_point_values)    |
---|
2823 | |
---|
2824 | |
---|
2825 | |
---|
2826 | Â Â Â Â fid.close() |
---|
2827 | |
---|
2828 | Â Â Â Â #Cleanup |
---|
2829 | Â Â Â Â os.remove(sww.filename) |
---|
2830 | Â Â Â Â os.remove(ptsfile) |
---|
2831 | |
---|
2832 | |
---|
2833 | |
---|
2834 | |
---|
2835 |   def test_ferret2sww1(self): |
---|
2836 | Â Â Â Â """Test that georeferencing etc works when converting from |
---|
2837 | Â Â Â Â ferret format (lat/lon) to sww format (UTM) |
---|
2838 | Â Â Â Â """ |
---|
2839 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2840 |     import os, sys |
---|
2841 | |
---|
2842 | Â Â Â Â #The test file has |
---|
2843 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
2844 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
2845 | Â Â Â Â # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ; |
---|
2846 | Â Â Â Â # |
---|
2847 | Â Â Â Â # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
2848 | Â Â Â Â # Fourth value (index==3) is -6.50198 cm |
---|
2849 | |
---|
2850 | |
---|
2851 | |
---|
2852 | Â Â Â Â #Read |
---|
2853 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
2854 | Â Â Â Â #fid = NetCDFFile(self.test_MOST_file) |
---|
2855 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '_ha.nc') |
---|
2856 | Â Â Â Â first_value =Â fid.variables['HA'][:][0,0,0] |
---|
2857 | Â Â Â Â fourth_value =Â fid.variables['HA'][:][0,0,3] |
---|
2858 | Â Â Â Â fid.close() |
---|
2859 | |
---|
2860 | |
---|
2861 | Â Â Â Â #Call conversion (with zero origin) |
---|
2862 | Â Â Â Â #ferret2sww('small', verbose=False, |
---|
2863 | Â Â Â Â #Â Â Â Â Â Â origin = (56, 0, 0)) |
---|
2864 |     ferret2sww(self.test_MOST_file, verbose=self.verbose, |
---|
2865 |           origin = (56, 0, 0)) |
---|
2866 | |
---|
2867 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
2868 |     zone, e, n = redfearn(-34.5, 150.66667) |
---|
2869 | Â Â Â Â #print zone, e, n |
---|
2870 | |
---|
2871 | Â Â Â Â #Read output file 'small.sww' |
---|
2872 | Â Â Â Â #fid = NetCDFFile('small.sww') |
---|
2873 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
2874 | |
---|
2875 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
2876 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
2877 | |
---|
2878 | Â Â Â Â #Check that first coordinate is correctly represented |
---|
2879 |     assert allclose(x[0], e) |
---|
2880 |     assert allclose(y[0], n) |
---|
2881 | |
---|
2882 | Â Â Â Â #Check first value |
---|
2883 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
2884 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
2885 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:] |
---|
2886 | |
---|
2887 | Â Â Â Â #print ymomentum |
---|
2888 | |
---|
2889 |     assert allclose(stage[0,0], first_value/100) #Meters |
---|
2890 | |
---|
2891 | Â Â Â Â #Check fourth value |
---|
2892 |     assert allclose(stage[0,3], fourth_value/100) #Meters |
---|
2893 | |
---|
2894 | Â Â Â Â fid.close() |
---|
2895 | |
---|
2896 | Â Â Â Â #Cleanup |
---|
2897 |     import os |
---|
2898 | Â Â Â Â os.remove(self.test_MOST_file +Â '.sww') |
---|
2899 | |
---|
2900 | |
---|
2901 | |
---|
2902 |   def test_ferret2sww_zscale(self): |
---|
2903 | Â Â Â Â """Test that zscale workse |
---|
2904 | Â Â Â Â """ |
---|
2905 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2906 |     import os, sys |
---|
2907 | |
---|
2908 | Â Â Â Â #The test file has |
---|
2909 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
2910 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
2911 | Â Â Â Â # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ; |
---|
2912 | Â Â Â Â # |
---|
2913 | Â Â Â Â # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
2914 | Â Â Â Â # Fourth value (index==3) is -6.50198 cm |
---|
2915 | |
---|
2916 | |
---|
2917 | Â Â Â Â #Read |
---|
2918 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
2919 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '_ha.nc') |
---|
2920 | Â Â Â Â first_value =Â fid.variables['HA'][:][0,0,0] |
---|
2921 | Â Â Â Â fourth_value =Â fid.variables['HA'][:][0,0,3] |
---|
2922 | Â Â Â Â fid.close() |
---|
2923 | |
---|
2924 | Â Â Â Â #Call conversion (with no scaling) |
---|
2925 |     ferret2sww(self.test_MOST_file, verbose=self.verbose, |
---|
2926 |           origin = (56, 0, 0)) |
---|
2927 | |
---|
2928 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
2929 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
2930 | |
---|
2931 | Â Â Â Â #Check values |
---|
2932 | Â Â Â Â stage_1 =Â fid.variables['stage'][:] |
---|
2933 | Â Â Â Â xmomentum_1 =Â fid.variables['xmomentum'][:] |
---|
2934 | Â Â Â Â ymomentum_1 =Â fid.variables['ymomentum'][:] |
---|
2935 | |
---|
2936 |     assert allclose(stage_1[0,0], first_value/100) #Meters |
---|
2937 |     assert allclose(stage_1[0,3], fourth_value/100) #Meters |
---|
2938 | |
---|
2939 | Â Â Â Â fid.close() |
---|
2940 | |
---|
2941 | Â Â Â Â #Call conversion (with scaling) |
---|
2942 | Â Â Â Â ferret2sww(self.test_MOST_file, |
---|
2943 | Â Â Â Â Â Â Â Â Â Â zscale =Â 5, |
---|
2944 | Â Â Â Â Â Â Â Â Â Â verbose=self.verbose, |
---|
2945 |           origin = (56, 0, 0)) |
---|
2946 | |
---|
2947 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
2948 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
2949 | |
---|
2950 | Â Â Â Â #Check values |
---|
2951 | Â Â Â Â stage_5 =Â fid.variables['stage'][:] |
---|
2952 | Â Â Â Â xmomentum_5 =Â fid.variables['xmomentum'][:] |
---|
2953 | Â Â Â Â ymomentum_5 =Â fid.variables['ymomentum'][:] |
---|
2954 | Â Â Â Â elevation =Â fid.variables['elevation'][:] |
---|
2955 | |
---|
2956 |     assert allclose(stage_5[0,0], 5*first_value/100) #Meters |
---|
2957 |     assert allclose(stage_5[0,3], 5*fourth_value/100) #Meters |
---|
2958 | |
---|
2959 |     assert allclose(5*stage_1, stage_5) |
---|
2960 | |
---|
2961 | Â Â Â Â # Momentum will also be changed due to new depth |
---|
2962 | |
---|
2963 | Â Â Â Â depth_1 =Â stage_1-elevation |
---|
2964 | Â Â Â Â depth_5 =Â stage_5-elevation |
---|
2965 | |
---|
2966 | |
---|
2967 |     for i in range(stage_1.shape[0]): |
---|
2968 |       for j in range(stage_1.shape[1]):      |
---|
2969 |         if depth_1[i,j] > epsilon: |
---|
2970 | |
---|
2971 | Â Â Â Â Â Â Â Â Â Â scale =Â depth_5[i,j]/depth_1[i,j] |
---|
2972 | Â Â Â Â Â Â Â Â Â Â ref_xmomentum =Â xmomentum_1[i,j]Â *Â scale |
---|
2973 | Â Â Â Â Â Â Â Â Â Â ref_ymomentum =Â ymomentum_1[i,j]Â *Â scale |
---|
2974 | Â Â Â Â Â Â Â Â Â Â |
---|
2975 | Â Â Â Â Â Â Â Â Â Â #print i, scale, xmomentum_1[i,j], xmomentum_5[i,j] |
---|
2976 | Â Â Â Â Â Â Â Â Â Â |
---|
2977 |           assert allclose(xmomentum_5[i,j], ref_xmomentum) |
---|
2978 |           assert allclose(ymomentum_5[i,j], ref_ymomentum) |
---|
2979 | Â Â Â Â Â Â Â Â Â Â |
---|
2980 | Â Â Â Â |
---|
2981 | |
---|
2982 | Â Â Â Â fid.close() |
---|
2983 | |
---|
2984 | |
---|
2985 | Â Â Â Â #Cleanup |
---|
2986 |     import os |
---|
2987 | Â Â Â Â os.remove(self.test_MOST_file +Â '.sww') |
---|
2988 | |
---|
2989 | |
---|
2990 | |
---|
2991 |   def test_ferret2sww_2(self): |
---|
2992 | Â Â Â Â """Test that georeferencing etc works when converting from |
---|
2993 | Â Â Â Â ferret format (lat/lon) to sww format (UTM) |
---|
2994 | Â Â Â Â """ |
---|
2995 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
2996 | |
---|
2997 | Â Â Â Â #The test file has |
---|
2998 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
2999 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
3000 | Â Â Â Â # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ; |
---|
3001 | Â Â Â Â # |
---|
3002 | Â Â Â Â # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
3003 | Â Â Â Â # Fourth value (index==3) is -6.50198 cm |
---|
3004 | |
---|
3005 | |
---|
3006 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3007 | |
---|
3008 | Â Â Â Â #fid = NetCDFFile('small_ha.nc') |
---|
3009 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '_ha.nc') |
---|
3010 | |
---|
3011 | Â Â Â Â #Pick a coordinate and a value |
---|
3012 | |
---|
3013 | Â Â Â Â time_index =Â 1 |
---|
3014 | Â Â Â Â lat_index =Â 0 |
---|
3015 | Â Â Â Â lon_index =Â 2 |
---|
3016 | |
---|
3017 |     test_value = fid.variables['HA'][:][time_index, lat_index, lon_index] |
---|
3018 | Â Â Â Â test_time =Â fid.variables['TIME'][:][time_index] |
---|
3019 | Â Â Â Â test_lat =Â fid.variables['LAT'][:][lat_index] |
---|
3020 | Â Â Â Â test_lon =Â fid.variables['LON'][:][lon_index] |
---|
3021 | |
---|
3022 | Â Â Â Â linear_point_index =Â lat_index*4Â +Â lon_index |
---|
3023 | Â Â Â Â fid.close() |
---|
3024 | |
---|
3025 | Â Â Â Â #Call conversion (with zero origin) |
---|
3026 |     ferret2sww(self.test_MOST_file, verbose=self.verbose, |
---|
3027 |           origin = (56, 0, 0)) |
---|
3028 | |
---|
3029 | |
---|
3030 | Â Â Â Â #Work out the UTM coordinates for test point |
---|
3031 |     zone, e, n = redfearn(test_lat, test_lon) |
---|
3032 | |
---|
3033 | Â Â Â Â #Read output file 'small.sww' |
---|
3034 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
3035 | |
---|
3036 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
3037 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
3038 | |
---|
3039 | Â Â Â Â #Check that test coordinate is correctly represented |
---|
3040 |     assert allclose(x[linear_point_index], e) |
---|
3041 |     assert allclose(y[linear_point_index], n) |
---|
3042 | |
---|
3043 | Â Â Â Â #Check test value |
---|
3044 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
3045 | |
---|
3046 |     assert allclose(stage[time_index, linear_point_index], test_value/100) |
---|
3047 | |
---|
3048 | Â Â Â Â fid.close() |
---|
3049 | |
---|
3050 | Â Â Â Â #Cleanup |
---|
3051 |     import os |
---|
3052 | Â Â Â Â os.remove(self.test_MOST_file +Â '.sww') |
---|
3053 | |
---|
3054 | |
---|
3055 |   def test_ferret2sww_lat_long(self): |
---|
3056 | Â Â Â Â # Test that min lat long works |
---|
3057 | |
---|
3058 | Â Â Â Â #The test file has |
---|
3059 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
3060 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
3061 | Â Â Â Â |
---|
3062 | Â Â Â Â #Read |
---|
3063 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3064 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '_ha.nc') |
---|
3065 | Â Â Â Â first_value =Â fid.variables['HA'][:][0,0,0] |
---|
3066 | Â Â Â Â fourth_value =Â fid.variables['HA'][:][0,0,3] |
---|
3067 | Â Â Â Â fid.close() |
---|
3068 | |
---|
3069 | |
---|
3070 | Â Â Â Â #Call conversion (with zero origin) |
---|
3071 | Â Â Â Â #ferret2sww('small', verbose=self.verbose, |
---|
3072 | Â Â Â Â #Â Â Â Â Â Â origin = (56, 0, 0)) |
---|
3073 |     ferret2sww(self.test_MOST_file, verbose=self.verbose, |
---|
3074 |           origin = (56, 0, 0), minlat=-34.5, maxlat=-34) |
---|
3075 | |
---|
3076 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
3077 |     zone, e, n = redfearn(-34.5, 150.66667) |
---|
3078 | Â Â Â Â #print zone, e, n |
---|
3079 | |
---|
3080 | Â Â Â Â #Read output file 'small.sww' |
---|
3081 | Â Â Â Â #fid = NetCDFFile('small.sww') |
---|
3082 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
3083 | |
---|
3084 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
3085 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
3086 | Â Â Â Â #Check that first coordinate is correctly represented |
---|
3087 |     assert 16 == len(x) |
---|
3088 | |
---|
3089 | Â Â Â Â fid.close() |
---|
3090 | |
---|
3091 | Â Â Â Â #Cleanup |
---|
3092 |     import os |
---|
3093 | Â Â Â Â os.remove(self.test_MOST_file +Â '.sww') |
---|
3094 | |
---|
3095 | |
---|
3096 |   def test_ferret2sww_lat_longII(self): |
---|
3097 | Â Â Â Â # Test that min lat long works |
---|
3098 | |
---|
3099 | Â Â Â Â #The test file has |
---|
3100 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
3101 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
3102 | Â Â Â Â |
---|
3103 | Â Â Â Â #Read |
---|
3104 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3105 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '_ha.nc') |
---|
3106 | Â Â Â Â first_value =Â fid.variables['HA'][:][0,0,0] |
---|
3107 | Â Â Â Â fourth_value =Â fid.variables['HA'][:][0,0,3] |
---|
3108 | Â Â Â Â fid.close() |
---|
3109 | |
---|
3110 | |
---|
3111 | Â Â Â Â #Call conversion (with zero origin) |
---|
3112 | Â Â Â Â #ferret2sww('small', verbose=False, |
---|
3113 | Â Â Â Â #Â Â Â Â Â Â origin = (56, 0, 0)) |
---|
3114 |     ferret2sww(self.test_MOST_file, verbose=False, |
---|
3115 |           origin = (56, 0, 0), minlat=-34.4, maxlat=-34.2) |
---|
3116 | |
---|
3117 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
3118 |     zone, e, n = redfearn(-34.5, 150.66667) |
---|
3119 | Â Â Â Â #print zone, e, n |
---|
3120 | |
---|
3121 | Â Â Â Â #Read output file 'small.sww' |
---|
3122 | Â Â Â Â #fid = NetCDFFile('small.sww') |
---|
3123 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
3124 | |
---|
3125 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
3126 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
3127 | Â Â Â Â #Check that first coordinate is correctly represented |
---|
3128 |     assert 12 == len(x) |
---|
3129 | |
---|
3130 | Â Â Â Â fid.close() |
---|
3131 | |
---|
3132 | Â Â Â Â #Cleanup |
---|
3133 |     import os |
---|
3134 | Â Â Â Â os.remove(self.test_MOST_file +Â '.sww') |
---|
3135 | Â Â Â Â |
---|
3136 |   def test_ferret2sww3(self): |
---|
3137 | Â Â Â Â """Elevation included |
---|
3138 | Â Â Â Â """ |
---|
3139 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
3140 | |
---|
3141 | Â Â Â Â #The test file has |
---|
3142 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
3143 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
3144 | Â Â Â Â # ELEVATION = [-1 -2 -3 -4 |
---|
3145 | Â Â Â Â #Â Â Â Â Â Â Â -5 -6 -7 -8 |
---|
3146 | Â Â Â Â #Â Â Â Â Â Â Â ... |
---|
3147 |     #       ...   -16] |
---|
3148 | Â Â Â Â # where the top left corner is -1m, |
---|
3149 | Â Â Â Â # and the ll corner is -13.0m |
---|
3150 | Â Â Â Â # |
---|
3151 | Â Â Â Â # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
3152 | Â Â Â Â # Fourth value (index==3) is -6.50198 cm |
---|
3153 | |
---|
3154 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3155 |     import os |
---|
3156 | Â Â Â Â fid1 =Â NetCDFFile('test_ha.nc','w') |
---|
3157 | Â Â Â Â fid2 =Â NetCDFFile('test_ua.nc','w') |
---|
3158 | Â Â Â Â fid3 =Â NetCDFFile('test_va.nc','w') |
---|
3159 | Â Â Â Â fid4 =Â NetCDFFile('test_e.nc','w') |
---|
3160 | |
---|
3161 | Â Â Â Â h1_list =Â [150.66667,150.83334,151.] |
---|
3162 | Â Â Â Â h2_list =Â [-34.5,-34.33333] |
---|
3163 | |
---|
3164 | Â Â Â Â long_name =Â 'LON' |
---|
3165 | Â Â Â Â lat_name =Â 'LAT' |
---|
3166 | Â Â Â Â time_name =Â 'TIME' |
---|
3167 | |
---|
3168 | Â Â Â Â nx =Â 3 |
---|
3169 | Â Â Â Â ny =Â 2 |
---|
3170 | |
---|
3171 |     for fid in [fid1,fid2,fid3]: |
---|
3172 | Â Â Â Â Â Â fid.createDimension(long_name,nx) |
---|
3173 | Â Â Â Â Â Â fid.createVariable(long_name,'d',(long_name,)) |
---|
3174 | Â Â Â Â Â Â fid.variables[long_name].point_spacing='uneven' |
---|
3175 | Â Â Â Â Â Â fid.variables[long_name].units='degrees_east' |
---|
3176 | Â Â Â Â Â Â fid.variables[long_name].assignValue(h1_list) |
---|
3177 | |
---|
3178 | Â Â Â Â Â Â fid.createDimension(lat_name,ny) |
---|
3179 | Â Â Â Â Â Â fid.createVariable(lat_name,'d',(lat_name,)) |
---|
3180 | Â Â Â Â Â Â fid.variables[lat_name].point_spacing='uneven' |
---|
3181 | Â Â Â Â Â Â fid.variables[lat_name].units='degrees_north' |
---|
3182 | Â Â Â Â Â Â fid.variables[lat_name].assignValue(h2_list) |
---|
3183 | |
---|
3184 | Â Â Â Â Â Â fid.createDimension(time_name,2) |
---|
3185 | Â Â Â Â Â Â fid.createVariable(time_name,'d',(time_name,)) |
---|
3186 | Â Â Â Â Â Â fid.variables[time_name].point_spacing='uneven' |
---|
3187 | Â Â Â Â Â Â fid.variables[time_name].units='seconds' |
---|
3188 | Â Â Â Â Â Â fid.variables[time_name].assignValue([0.,1.]) |
---|
3189 |       if fid == fid3: break |
---|
3190 | |
---|
3191 | |
---|
3192 |     for fid in [fid4]: |
---|
3193 | Â Â Â Â Â Â fid.createDimension(long_name,nx) |
---|
3194 | Â Â Â Â Â Â fid.createVariable(long_name,'d',(long_name,)) |
---|
3195 | Â Â Â Â Â Â fid.variables[long_name].point_spacing='uneven' |
---|
3196 | Â Â Â Â Â Â fid.variables[long_name].units='degrees_east' |
---|
3197 | Â Â Â Â Â Â fid.variables[long_name].assignValue(h1_list) |
---|
3198 | |
---|
3199 | Â Â Â Â Â Â fid.createDimension(lat_name,ny) |
---|
3200 | Â Â Â Â Â Â fid.createVariable(lat_name,'d',(lat_name,)) |
---|
3201 | Â Â Â Â Â Â fid.variables[lat_name].point_spacing='uneven' |
---|
3202 | Â Â Â Â Â Â fid.variables[lat_name].units='degrees_north' |
---|
3203 | Â Â Â Â Â Â fid.variables[lat_name].assignValue(h2_list) |
---|
3204 | |
---|
3205 | Â Â Â Â name =Â {} |
---|
3206 | Â Â Â Â name[fid1]='HA' |
---|
3207 | Â Â Â Â name[fid2]='UA' |
---|
3208 | Â Â Â Â name[fid3]='VA' |
---|
3209 | Â Â Â Â name[fid4]='ELEVATION' |
---|
3210 | |
---|
3211 | Â Â Â Â units =Â {} |
---|
3212 | Â Â Â Â units[fid1]='cm' |
---|
3213 | Â Â Â Â units[fid2]='cm/s' |
---|
3214 | Â Â Â Â units[fid3]='cm/s' |
---|
3215 | Â Â Â Â units[fid4]='m' |
---|
3216 | |
---|
3217 | Â Â Â Â values =Â {} |
---|
3218 |     values[fid1]=[[[5., 10.,15.], [13.,18.,23.]],[[50.,100.,150.],[130.,180.,230.]]] |
---|
3219 |     values[fid2]=[[[1., 2.,3.], [4.,5.,6.]],[[7.,8.,9.],[10.,11.,12.]]] |
---|
3220 |     values[fid3]=[[[13., 12.,11.], [10.,9.,8.]],[[7.,6.,5.],[4.,3.,2.]]] |
---|
3221 | Â Â Â Â values[fid4]=[[-3000,-3100,-3200],[-4000,-5000,-6000]] |
---|
3222 | |
---|
3223 |     for fid in [fid1,fid2,fid3]: |
---|
3224 | Â Â Â Â Â fid.createVariable(name[fid],'d',(time_name,lat_name,long_name)) |
---|
3225 | Â Â Â Â Â fid.variables[name[fid]].point_spacing='uneven' |
---|
3226 | Â Â Â Â Â fid.variables[name[fid]].units=units[fid] |
---|
3227 | Â Â Â Â Â fid.variables[name[fid]].assignValue(values[fid]) |
---|
3228 | Â Â Â Â Â fid.variables[name[fid]].missing_value =Â -99999999. |
---|
3229 |      if fid == fid3: break |
---|
3230 | |
---|
3231 |     for fid in [fid4]: |
---|
3232 | Â Â Â Â Â Â fid.createVariable(name[fid],'d',(lat_name,long_name)) |
---|
3233 | Â Â Â Â Â Â fid.variables[name[fid]].point_spacing='uneven' |
---|
3234 | Â Â Â Â Â Â fid.variables[name[fid]].units=units[fid] |
---|
3235 | Â Â Â Â Â Â fid.variables[name[fid]].assignValue(values[fid]) |
---|
3236 | Â Â Â Â Â Â fid.variables[name[fid]].missing_value =Â -99999999. |
---|
3237 | |
---|
3238 | |
---|
3239 | Â Â Â Â fid1.sync();Â fid1.close() |
---|
3240 | Â Â Â Â fid2.sync();Â fid2.close() |
---|
3241 | Â Â Â Â fid3.sync();Â fid3.close() |
---|
3242 | Â Â Â Â fid4.sync();Â fid4.close() |
---|
3243 | |
---|
3244 | Â Â Â Â fid1 =Â NetCDFFile('test_ha.nc','r') |
---|
3245 | Â Â Â Â fid2 =Â NetCDFFile('test_e.nc','r') |
---|
3246 | Â Â Â Â fid3 =Â NetCDFFile('test_va.nc','r') |
---|
3247 | |
---|
3248 | |
---|
3249 | Â Â Â Â first_amp =Â fid1.variables['HA'][:][0,0,0] |
---|
3250 | Â Â Â Â third_amp =Â fid1.variables['HA'][:][0,0,2] |
---|
3251 | Â Â Â Â first_elevation =Â fid2.variables['ELEVATION'][0,0] |
---|
3252 | Â Â Â Â third_elevation=Â fid2.variables['ELEVATION'][:][0,2] |
---|
3253 | Â Â Â Â first_speed =Â fid3.variables['VA'][0,0,0] |
---|
3254 | Â Â Â Â third_speed =Â fid3.variables['VA'][:][0,0,2] |
---|
3255 | |
---|
3256 | Â Â Â Â fid1.close() |
---|
3257 | Â Â Â Â fid2.close() |
---|
3258 | Â Â Â Â fid3.close() |
---|
3259 | |
---|
3260 | Â Â Â Â #Call conversion (with zero origin) |
---|
3261 |     ferret2sww('test', verbose=self.verbose, |
---|
3262 |           origin = (56, 0, 0), inverted_bathymetry=False) |
---|
3263 | |
---|
3264 | Â Â Â Â os.remove('test_va.nc') |
---|
3265 | Â Â Â Â os.remove('test_ua.nc') |
---|
3266 | Â Â Â Â os.remove('test_ha.nc') |
---|
3267 | Â Â Â Â os.remove('test_e.nc') |
---|
3268 | |
---|
3269 | Â Â Â Â #Read output file 'test.sww' |
---|
3270 | Â Â Â Â fid =Â NetCDFFile('test.sww') |
---|
3271 | |
---|
3272 | |
---|
3273 | Â Â Â Â #Check first value |
---|
3274 | Â Â Â Â elevation =Â fid.variables['elevation'][:] |
---|
3275 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
3276 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
3277 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:] |
---|
3278 | |
---|
3279 | Â Â Â Â #print ymomentum |
---|
3280 | Â Â Â Â first_height =Â first_amp/100Â -Â first_elevation |
---|
3281 | Â Â Â Â third_height =Â third_amp/100Â -Â third_elevation |
---|
3282 | Â Â Â Â first_momentum=first_speed*first_height/100 |
---|
3283 | Â Â Â Â third_momentum=third_speed*third_height/100 |
---|
3284 | |
---|
3285 |     assert allclose(ymomentum[0][0],first_momentum) #Meters |
---|
3286 |     assert allclose(ymomentum[0][2],third_momentum) #Meters |
---|
3287 | |
---|
3288 | Â Â Â Â fid.close() |
---|
3289 | |
---|
3290 | Â Â Â Â #Cleanup |
---|
3291 | Â Â Â Â os.remove('test.sww') |
---|
3292 | |
---|
3293 | |
---|
3294 | |
---|
3295 |   def test_ferret2sww4(self): |
---|
3296 | Â Â Â Â """Like previous but with augmented variable names as |
---|
3297 | Â Â Â Â in files produced by ferret as opposed to MOST |
---|
3298 | Â Â Â Â """ |
---|
3299 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
3300 | |
---|
3301 | Â Â Â Â #The test file has |
---|
3302 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
3303 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
3304 | Â Â Â Â # ELEVATION = [-1 -2 -3 -4 |
---|
3305 | Â Â Â Â #Â Â Â Â Â Â Â -5 -6 -7 -8 |
---|
3306 | Â Â Â Â #Â Â Â Â Â Â Â ... |
---|
3307 |     #       ...   -16] |
---|
3308 | Â Â Â Â # where the top left corner is -1m, |
---|
3309 | Â Â Â Â # and the ll corner is -13.0m |
---|
3310 | Â Â Â Â # |
---|
3311 | Â Â Â Â # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
3312 | Â Â Â Â # Fourth value (index==3) is -6.50198 cm |
---|
3313 | |
---|
3314 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3315 |     import os |
---|
3316 | Â Â Â Â fid1 =Â NetCDFFile('test_ha.nc','w') |
---|
3317 | Â Â Â Â fid2 =Â NetCDFFile('test_ua.nc','w') |
---|
3318 | Â Â Â Â fid3 =Â NetCDFFile('test_va.nc','w') |
---|
3319 | Â Â Â Â fid4 =Â NetCDFFile('test_e.nc','w') |
---|
3320 | |
---|
3321 | Â Â Â Â h1_list =Â [150.66667,150.83334,151.] |
---|
3322 | Â Â Â Â h2_list =Â [-34.5,-34.33333] |
---|
3323 | |
---|
3324 | #Â Â Â Â long_name = 'LON961_1261' |
---|
3325 | #Â Â Â Â lat_name = 'LAT481_841' |
---|
3326 | #Â Â Â Â time_name = 'TIME1' |
---|
3327 | |
---|
3328 | Â Â Â Â long_name =Â 'LON' |
---|
3329 | Â Â Â Â lat_name =Â 'LAT' |
---|
3330 | Â Â Â Â time_name =Â 'TIME' |
---|
3331 | |
---|
3332 | Â Â Â Â nx =Â 3 |
---|
3333 | Â Â Â Â ny =Â 2 |
---|
3334 | |
---|
3335 |     for fid in [fid1,fid2,fid3]: |
---|
3336 | Â Â Â Â Â Â fid.createDimension(long_name,nx) |
---|
3337 | Â Â Â Â Â Â fid.createVariable(long_name,'d',(long_name,)) |
---|
3338 | Â Â Â Â Â Â fid.variables[long_name].point_spacing='uneven' |
---|
3339 | Â Â Â Â Â Â fid.variables[long_name].units='degrees_east' |
---|
3340 | Â Â Â Â Â Â fid.variables[long_name].assignValue(h1_list) |
---|
3341 | |
---|
3342 | Â Â Â Â Â Â fid.createDimension(lat_name,ny) |
---|
3343 | Â Â Â Â Â Â fid.createVariable(lat_name,'d',(lat_name,)) |
---|
3344 | Â Â Â Â Â Â fid.variables[lat_name].point_spacing='uneven' |
---|
3345 | Â Â Â Â Â Â fid.variables[lat_name].units='degrees_north' |
---|
3346 | Â Â Â Â Â Â fid.variables[lat_name].assignValue(h2_list) |
---|
3347 | |
---|
3348 | Â Â Â Â Â Â fid.createDimension(time_name,2) |
---|
3349 | Â Â Â Â Â Â fid.createVariable(time_name,'d',(time_name,)) |
---|
3350 | Â Â Â Â Â Â fid.variables[time_name].point_spacing='uneven' |
---|
3351 | Â Â Â Â Â Â fid.variables[time_name].units='seconds' |
---|
3352 | Â Â Â Â Â Â fid.variables[time_name].assignValue([0.,1.]) |
---|
3353 |       if fid == fid3: break |
---|
3354 | |
---|
3355 | |
---|
3356 |     for fid in [fid4]: |
---|
3357 | Â Â Â Â Â Â fid.createDimension(long_name,nx) |
---|
3358 | Â Â Â Â Â Â fid.createVariable(long_name,'d',(long_name,)) |
---|
3359 | Â Â Â Â Â Â fid.variables[long_name].point_spacing='uneven' |
---|
3360 | Â Â Â Â Â Â fid.variables[long_name].units='degrees_east' |
---|
3361 | Â Â Â Â Â Â fid.variables[long_name].assignValue(h1_list) |
---|
3362 | |
---|
3363 | Â Â Â Â Â Â fid.createDimension(lat_name,ny) |
---|
3364 | Â Â Â Â Â Â fid.createVariable(lat_name,'d',(lat_name,)) |
---|
3365 | Â Â Â Â Â Â fid.variables[lat_name].point_spacing='uneven' |
---|
3366 | Â Â Â Â Â Â fid.variables[lat_name].units='degrees_north' |
---|
3367 | Â Â Â Â Â Â fid.variables[lat_name].assignValue(h2_list) |
---|
3368 | |
---|
3369 | Â Â Â Â name =Â {} |
---|
3370 | Â Â Â Â name[fid1]='HA' |
---|
3371 | Â Â Â Â name[fid2]='UA' |
---|
3372 | Â Â Â Â name[fid3]='VA' |
---|
3373 | Â Â Â Â name[fid4]='ELEVATION' |
---|
3374 | |
---|
3375 | Â Â Â Â units =Â {} |
---|
3376 | Â Â Â Â units[fid1]='cm' |
---|
3377 | Â Â Â Â units[fid2]='cm/s' |
---|
3378 | Â Â Â Â units[fid3]='cm/s' |
---|
3379 | Â Â Â Â units[fid4]='m' |
---|
3380 | |
---|
3381 | Â Â Â Â values =Â {} |
---|
3382 |     values[fid1]=[[[5., 10.,15.], [13.,18.,23.]],[[50.,100.,150.],[130.,180.,230.]]] |
---|
3383 |     values[fid2]=[[[1., 2.,3.], [4.,5.,6.]],[[7.,8.,9.],[10.,11.,12.]]] |
---|
3384 |     values[fid3]=[[[13., 12.,11.], [10.,9.,8.]],[[7.,6.,5.],[4.,3.,2.]]] |
---|
3385 | Â Â Â Â values[fid4]=[[-3000,-3100,-3200],[-4000,-5000,-6000]] |
---|
3386 | |
---|
3387 |     for fid in [fid1,fid2,fid3]: |
---|
3388 | Â Â Â Â Â fid.createVariable(name[fid],'d',(time_name,lat_name,long_name)) |
---|
3389 | Â Â Â Â Â fid.variables[name[fid]].point_spacing='uneven' |
---|
3390 | Â Â Â Â Â fid.variables[name[fid]].units=units[fid] |
---|
3391 | Â Â Â Â Â fid.variables[name[fid]].assignValue(values[fid]) |
---|
3392 | Â Â Â Â Â fid.variables[name[fid]].missing_value =Â -99999999. |
---|
3393 |      if fid == fid3: break |
---|
3394 | |
---|
3395 |     for fid in [fid4]: |
---|
3396 | Â Â Â Â Â Â fid.createVariable(name[fid],'d',(lat_name,long_name)) |
---|
3397 | Â Â Â Â Â Â fid.variables[name[fid]].point_spacing='uneven' |
---|
3398 | Â Â Â Â Â Â fid.variables[name[fid]].units=units[fid] |
---|
3399 | Â Â Â Â Â Â fid.variables[name[fid]].assignValue(values[fid]) |
---|
3400 | Â Â Â Â Â Â fid.variables[name[fid]].missing_value =Â -99999999. |
---|
3401 | |
---|
3402 | |
---|
3403 | Â Â Â Â fid1.sync();Â fid1.close() |
---|
3404 | Â Â Â Â fid2.sync();Â fid2.close() |
---|
3405 | Â Â Â Â fid3.sync();Â fid3.close() |
---|
3406 | Â Â Â Â fid4.sync();Â fid4.close() |
---|
3407 | |
---|
3408 | Â Â Â Â fid1 =Â NetCDFFile('test_ha.nc','r') |
---|
3409 | Â Â Â Â fid2 =Â NetCDFFile('test_e.nc','r') |
---|
3410 | Â Â Â Â fid3 =Â NetCDFFile('test_va.nc','r') |
---|
3411 | |
---|
3412 | |
---|
3413 | Â Â Â Â first_amp =Â fid1.variables['HA'][:][0,0,0] |
---|
3414 | Â Â Â Â third_amp =Â fid1.variables['HA'][:][0,0,2] |
---|
3415 | Â Â Â Â first_elevation =Â fid2.variables['ELEVATION'][0,0] |
---|
3416 | Â Â Â Â third_elevation=Â fid2.variables['ELEVATION'][:][0,2] |
---|
3417 | Â Â Â Â first_speed =Â fid3.variables['VA'][0,0,0] |
---|
3418 | Â Â Â Â third_speed =Â fid3.variables['VA'][:][0,0,2] |
---|
3419 | |
---|
3420 | Â Â Â Â fid1.close() |
---|
3421 | Â Â Â Â fid2.close() |
---|
3422 | Â Â Â Â fid3.close() |
---|
3423 | |
---|
3424 | Â Â Â Â #Call conversion (with zero origin) |
---|
3425 |     ferret2sww('test', verbose=self.verbose, origin = (56, 0, 0) |
---|
3426 |           , inverted_bathymetry=False) |
---|
3427 | |
---|
3428 | Â Â Â Â os.remove('test_va.nc') |
---|
3429 | Â Â Â Â os.remove('test_ua.nc') |
---|
3430 | Â Â Â Â os.remove('test_ha.nc') |
---|
3431 | Â Â Â Â os.remove('test_e.nc') |
---|
3432 | |
---|
3433 | Â Â Â Â #Read output file 'test.sww' |
---|
3434 | Â Â Â Â fid =Â NetCDFFile('test.sww') |
---|
3435 | |
---|
3436 | |
---|
3437 | Â Â Â Â #Check first value |
---|
3438 | Â Â Â Â elevation =Â fid.variables['elevation'][:] |
---|
3439 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
3440 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
3441 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:] |
---|
3442 | |
---|
3443 | Â Â Â Â #print ymomentum |
---|
3444 | Â Â Â Â first_height =Â first_amp/100Â -Â first_elevation |
---|
3445 | Â Â Â Â third_height =Â third_amp/100Â -Â third_elevation |
---|
3446 | Â Â Â Â first_momentum=first_speed*first_height/100 |
---|
3447 | Â Â Â Â third_momentum=third_speed*third_height/100 |
---|
3448 | |
---|
3449 |     assert allclose(ymomentum[0][0],first_momentum) #Meters |
---|
3450 |     assert allclose(ymomentum[0][2],third_momentum) #Meters |
---|
3451 | |
---|
3452 | Â Â Â Â fid.close() |
---|
3453 | |
---|
3454 | Â Â Â Â #Cleanup |
---|
3455 | Â Â Â Â os.remove('test.sww') |
---|
3456 | |
---|
3457 | |
---|
3458 | |
---|
3459 | |
---|
3460 |   def test_ferret2sww_nz_origin(self): |
---|
3461 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
3462 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3463 | |
---|
3464 | Â Â Â Â #Call conversion (with nonzero origin) |
---|
3465 |     ferret2sww(self.test_MOST_file, verbose=self.verbose, |
---|
3466 |           origin = (56, 100000, 200000)) |
---|
3467 | |
---|
3468 | |
---|
3469 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
3470 |     zone, e, n = redfearn(-34.5, 150.66667) |
---|
3471 | |
---|
3472 | Â Â Â Â #Read output file 'small.sww' |
---|
3473 | Â Â Â Â #fid = NetCDFFile('small.sww', 'r') |
---|
3474 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '.sww') |
---|
3475 | |
---|
3476 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
3477 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
3478 | |
---|
3479 | Â Â Â Â #Check that first coordinate is correctly represented |
---|
3480 |     assert allclose(x[0], e-100000) |
---|
3481 |     assert allclose(y[0], n-200000) |
---|
3482 | |
---|
3483 | Â Â Â Â fid.close() |
---|
3484 | |
---|
3485 | Â Â Â Â #Cleanup |
---|
3486 | Â Â Â Â os.remove(self.test_MOST_file +Â '.sww') |
---|
3487 | |
---|
3488 | |
---|
3489 |   def test_ferret2sww_lat_longII(self): |
---|
3490 | Â Â Â Â # Test that min lat long works |
---|
3491 | |
---|
3492 | Â Â Â Â #The test file has |
---|
3493 | Â Â Â Â # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
3494 | Â Â Â Â # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
3495 | Â Â Â Â |
---|
3496 | Â Â Â Â #Read |
---|
3497 |     from anuga.coordinate_transforms.redfearn import redfearn |
---|
3498 | Â Â Â Â fid =Â NetCDFFile(self.test_MOST_file +Â '_ha.nc') |
---|
3499 | Â Â Â Â first_value =Â fid.variables['HA'][:][0,0,0] |
---|
3500 | Â Â Â Â fourth_value =Â fid.variables['HA'][:][0,0,3] |
---|
3501 | Â Â Â Â fid.close() |
---|
3502 | |
---|
3503 | |
---|
3504 | Â Â Â Â #Call conversion (with zero origin) |
---|
3505 | Â Â Â Â #ferret2sww('small', verbose=self.verbose, |
---|
3506 | Â Â Â Â #Â Â Â Â Â Â origin = (56, 0, 0)) |
---|
3507 | Â Â Â Â try: |
---|
3508 |       ferret2sww(self.test_MOST_file, verbose=self.verbose, |
---|
3509 |           origin = (56, 0, 0), minlat=-34.5, maxlat=-35) |
---|
3510 |     except AssertionError: |
---|
3511 | Â Â Â Â Â Â pass |
---|
3512 | Â Â Â Â else: |
---|
3513 |       self.failUnless(0 ==1, 'Bad input did not throw exception error!') |
---|
3514 | |
---|
3515 |   def test_sww_extent(self): |
---|
3516 | Â Â Â Â """Not a test, rather a look at the sww format |
---|
3517 | Â Â Â Â """ |
---|
3518 | |
---|
3519 |     import time, os |
---|
3520 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
3521 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
3522 | |
---|
3523 | Â Â Â Â self.domain.set_name('datatest'Â +Â str(id(self))) |
---|
3524 | Â Â Â Â self.domain.format =Â 'sww' |
---|
3525 | Â Â Â Â self.domain.smooth =Â True |
---|
3526 | Â Â Â Â self.domain.reduction =Â mean |
---|
3527 | Â Â Â Â self.domain.set_datadir('.') |
---|
3528 | Â Â Â Â #self.domain.tight_slope_limiters = 1Â Â Â Â |
---|
3529 | |
---|
3530 | |
---|
3531 | Â Â Â Â sww =Â get_dataobject(self.domain) |
---|
3532 | Â Â Â Â sww.store_connectivity() |
---|
3533 | Â Â Â Â sww.store_timestep() |
---|
3534 | Â Â Â Â self.domain.time =Â 2. |
---|
3535 | |
---|
3536 | Â Â Â Â #Modify stage at second timestep |
---|
3537 | Â Â Â Â stage =Â self.domain.quantities['stage'].vertex_values |
---|
3538 |     self.domain.set_quantity('stage', stage/2) |
---|
3539 | |
---|
3540 | Â Â Â Â sww.store_timestep() |
---|
3541 | |
---|
3542 | Â Â Â Â file_and_extension_name =Â self.domain.get_name()Â +Â ".sww" |
---|
3543 | Â Â Â Â #print "file_and_extension_name",file_and_extension_name |
---|
3544 |     [xmin, xmax, ymin, ymax, stagemin, stagemax] = \ |
---|
3545 | Â Â Â Â Â Â Â Â extent_sww(file_and_extension_name ) |
---|
3546 | |
---|
3547 |     assert allclose(xmin, 0.0) |
---|
3548 |     assert allclose(xmax, 1.0) |
---|
3549 |     assert allclose(ymin, 0.0) |
---|
3550 |     assert allclose(ymax, 1.0) |
---|
3551 | |
---|
3552 | Â Â Â Â # FIXME (Ole): Revisit these numbers |
---|
3553 | Â Â Â Â #assert allclose(stagemin, -0.85), 'stagemin=%.4f' %stagemin |
---|
3554 | Â Â Â Â #assert allclose(stagemax, 0.15), 'stagemax=%.4f' %stagemax |
---|
3555 | |
---|
3556 | |
---|
3557 | Â Â Â Â #Cleanup |
---|
3558 | Â Â Â Â os.remove(sww.filename) |
---|
3559 | |
---|
3560 | |
---|
3561 | |
---|
3562 |   def test_sww2domain1(self): |
---|
3563 | Â Â Â Â ################################################ |
---|
3564 | Â Â Â Â #Create a test domain, and evolve and save it. |
---|
3565 | Â Â Â Â ################################################ |
---|
3566 |     from mesh_factory import rectangular |
---|
3567 |     from Numeric import array |
---|
3568 | |
---|
3569 | Â Â Â Â #Create basic mesh |
---|
3570 | |
---|
3571 | Â Â Â Â yiel=0.01 |
---|
3572 |     points, vertices, boundary = rectangular(10,10) |
---|
3573 | |
---|
3574 | Â Â Â Â #Create shallow water domain |
---|
3575 |     domain = Domain(points, vertices, boundary) |
---|
3576 | Â Â Â Â domain.geo_reference =Â Geo_reference(56,11,11) |
---|
3577 | Â Â Â Â domain.smooth =Â False |
---|
3578 | Â Â Â Â domain.store =Â True |
---|
3579 | Â Â Â Â domain.set_name('bedslope') |
---|
3580 | Â Â Â Â domain.default_order=2 |
---|
3581 | Â Â Â Â #Bed-slope and friction |
---|
3582 |     domain.set_quantity('elevation', lambda x,y: -x/3) |
---|
3583 |     domain.set_quantity('friction', 0.1) |
---|
3584 | Â Â Â Â # Boundary conditions |
---|
3585 |     from math import sin, pi |
---|
3586 | Â Â Â Â Br =Â Reflective_boundary(domain) |
---|
3587 | Â Â Â Â Bt =Â Transmissive_boundary(domain) |
---|
3588 | Â Â Â Â Bd =Â Dirichlet_boundary([0.2,0.,0.]) |
---|
3589 |     Bw = Time_boundary(domain=domain,f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0]) |
---|
3590 | |
---|
3591 | Â Â Â Â #domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br}) |
---|
3592 |     domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd}) |
---|
3593 | |
---|
3594 | Â Â Â Â domain.quantities_to_be_stored.extend(['xmomentum','ymomentum']) |
---|
3595 | Â Â Â Â #Initial condition |
---|
3596 | Â Â Â Â h =Â 0.05 |
---|
3597 | Â Â Â Â elevation =Â domain.quantities['elevation'].vertex_values |
---|
3598 |     domain.set_quantity('stage', elevation + h) |
---|
3599 | |
---|
3600 | Â Â Â Â domain.check_integrity() |
---|
3601 | Â Â Â Â #Evolution |
---|
3602 | Â Â Â Â #domain.tight_slope_limiters = 1 |
---|
3603 |     for t in domain.evolve(yieldstep = yiel, finaltime = 0.05): |
---|
3604 | Â Â Â Â Â Â #domain.write_time() |
---|
3605 | Â Â Â Â Â Â pass |
---|
3606 | |
---|
3607 | |
---|
3608 | Â Â Â Â ########################################## |
---|
3609 | Â Â Â Â #Import the example's file as a new domain |
---|
3610 | Â Â Â Â ########################################## |
---|
3611 |     from data_manager import sww2domain |
---|
3612 |     from Numeric import allclose |
---|
3613 |     import os |
---|
3614 | |
---|
3615 | Â Â Â Â filename =Â domain.datadir +Â os.sep +Â domain.get_name()Â +Â '.sww' |
---|
3616 | Â Â Â Â domain2 =Â sww2domain(filename,None,fail_if_NaN=False,verbose=self.verbose) |
---|
3617 | Â Â Â Â #points, vertices, boundary = rectangular(15,15) |
---|
3618 | Â Â Â Â #domain2.boundary = boundary |
---|
3619 | Â Â Â Â ################### |
---|
3620 | Â Â Â Â ##NOW TEST IT!!! |
---|
3621 | Â Â Â Â ################### |
---|
3622 | |
---|
3623 | Â Â Â Â os.remove(filename) |
---|
3624 | |
---|
3625 | Â Â Â Â bits =Â ['vertex_coordinates'] |
---|
3626 |     for quantity in ['elevation']+domain.quantities_to_be_stored: |
---|
3627 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_integral()'Â %quantity) |
---|
3628 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_values()'Â %quantity) |
---|
3629 | |
---|
3630 |     for bit in bits: |
---|
3631 | Â Â Â Â Â Â #print 'testing that domain.'+bit+' has been restored' |
---|
3632 | Â Â Â Â Â Â #print bit |
---|
3633 | Â Â Â Â Â Â #print 'done' |
---|
3634 |       assert allclose(eval('domain.'+bit),eval('domain2.'+bit)) |
---|
3635 | |
---|
3636 | Â Â Â Â ###################################### |
---|
3637 | Â Â Â Â #Now evolve them both, just to be sure |
---|
3638 | Â Â Â Â ######################################x |
---|
3639 | Â Â Â Â domain.time =Â 0. |
---|
3640 |     from time import sleep |
---|
3641 | |
---|
3642 | Â Â Â Â final =Â .1 |
---|
3643 |     domain.set_quantity('friction', 0.1) |
---|
3644 | Â Â Â Â domain.store =Â False |
---|
3645 |     domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd}) |
---|
3646 | |
---|
3647 | |
---|
3648 |     for t in domain.evolve(yieldstep = yiel, finaltime = final): |
---|
3649 | Â Â Â Â Â Â #domain.write_time() |
---|
3650 | Â Â Â Â Â Â pass |
---|
3651 | |
---|
3652 | Â Â Â Â final =Â final -Â (domain2.starttime-domain.starttime) |
---|
3653 | Â Â Â Â #BUT since domain1 gets time hacked back to 0: |
---|
3654 | Â Â Â Â final =Â final +Â (domain2.starttime-domain.starttime) |
---|
3655 | |
---|
3656 | Â Â Â Â domain2.smooth =Â False |
---|
3657 | Â Â Â Â domain2.store =Â False |
---|
3658 | Â Â Â Â domain2.default_order=2 |
---|
3659 |     domain2.set_quantity('friction', 0.1) |
---|
3660 | Â Â Â Â #Bed-slope and friction |
---|
3661 | Â Â Â Â # Boundary conditions |
---|
3662 | Â Â Â Â Bd2=Dirichlet_boundary([0.2,0.,0.]) |
---|
3663 | Â Â Â Â domain2.boundary =Â domain.boundary |
---|
3664 | Â Â Â Â #print 'domain2.boundary' |
---|
3665 | Â Â Â Â #print domain2.boundary |
---|
3666 |     domain2.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd}) |
---|
3667 | Â Â Â Â #domain2.set_boundary({'exterior': Bd}) |
---|
3668 | |
---|
3669 | Â Â Â Â domain2.check_integrity() |
---|
3670 | |
---|
3671 |     for t in domain2.evolve(yieldstep = yiel, finaltime = final): |
---|
3672 | Â Â Â Â Â Â #domain2.write_time() |
---|
3673 | Â Â Â Â Â Â pass |
---|
3674 | |
---|
3675 | Â Â Â Â ################### |
---|
3676 | Â Â Â Â ##NOW TEST IT!!! |
---|
3677 | Â Â Â Â ################## |
---|
3678 | |
---|
3679 | Â Â Â Â bits =Â ['vertex_coordinates'] |
---|
3680 | |
---|
3681 |     for quantity in ['elevation','stage', 'ymomentum','xmomentum']: |
---|
3682 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_integral()'Â %quantity) |
---|
3683 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_values()'Â %quantity) |
---|
3684 | |
---|
3685 | Â Â Â Â #print bits |
---|
3686 |     for bit in bits: |
---|
3687 | Â Â Â Â Â Â #print bit |
---|
3688 | Â Â Â Â Â Â #print eval('domain.'+bit) |
---|
3689 | Â Â Â Â Â Â #print eval('domain2.'+bit) |
---|
3690 | Â Â Â Â Â Â |
---|
3691 | Â Â Â Â Â Â #print eval('domain.'+bit+'-domain2.'+bit) |
---|
3692 | Â Â Â Â Â Â msg =Â 'Values in the two domains are different for 'Â +Â bit |
---|
3693 |       assert allclose(eval('domain.'+bit),eval('domain2.'+bit), |
---|
3694 |               rtol=1.e-5, atol=3.e-8), msg |
---|
3695 | |
---|
3696 | |
---|
3697 |   def DISABLEDtest_sww2domain2(self): |
---|
3698 | Â Â Â Â ################################################################## |
---|
3699 | Â Â Â Â #Same as previous test, but this checks how NaNs are handled. |
---|
3700 | Â Â Â Â ################################################################## |
---|
3701 | |
---|
3702 | |
---|
3703 |     from mesh_factory import rectangular |
---|
3704 |     from Numeric import array |
---|
3705 | |
---|
3706 | Â Â Â Â #Create basic mesh |
---|
3707 |     points, vertices, boundary = rectangular(2,2) |
---|
3708 | |
---|
3709 | Â Â Â Â #Create shallow water domain |
---|
3710 |     domain = Domain(points, vertices, boundary) |
---|
3711 | Â Â Â Â domain.smooth =Â False |
---|
3712 | Â Â Â Â domain.store =Â True |
---|
3713 | Â Â Â Â domain.set_name('test_file') |
---|
3714 | Â Â Â Â domain.set_datadir('.') |
---|
3715 | Â Â Â Â domain.default_order=2 |
---|
3716 | Â Â Â Â #domain.quantities_to_be_stored=['stage'] |
---|
3717 | |
---|
3718 |     domain.set_quantity('elevation', lambda x,y: -x/3) |
---|
3719 |     domain.set_quantity('friction', 0.1) |
---|
3720 | |
---|
3721 |     from math import sin, pi |
---|
3722 | Â Â Â Â Br =Â Reflective_boundary(domain) |
---|
3723 | Â Â Â Â Bt =Â Transmissive_boundary(domain) |
---|
3724 | Â Â Â Â Bd =Â Dirichlet_boundary([0.2,0.,0.]) |
---|
3725 | Â Â Â Â Bw =Â Time_boundary(domain=domain, |
---|
3726 |               f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0]) |
---|
3727 | |
---|
3728 |     domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br}) |
---|
3729 | |
---|
3730 | Â Â Â Â h =Â 0.05 |
---|
3731 | Â Â Â Â elevation =Â domain.quantities['elevation'].vertex_values |
---|
3732 |     domain.set_quantity('stage', elevation + h) |
---|
3733 | |
---|
3734 | Â Â Â Â domain.check_integrity() |
---|
3735 | |
---|
3736 |     for t in domain.evolve(yieldstep = 1, finaltime = 2.0): |
---|
3737 | Â Â Â Â Â Â pass |
---|
3738 | Â Â Â Â Â Â #domain.write_time() |
---|
3739 | |
---|
3740 | |
---|
3741 | |
---|
3742 | Â Â Â Â ################################## |
---|
3743 | Â Â Â Â #Import the file as a new domain |
---|
3744 | Â Â Â Â ################################## |
---|
3745 |     from data_manager import sww2domain |
---|
3746 |     from Numeric import allclose |
---|
3747 |     import os |
---|
3748 | |
---|
3749 | Â Â Â Â filename =Â domain.datadir +Â os.sep +Â domain.get_name()Â +Â '.sww' |
---|
3750 | |
---|
3751 | Â Â Â Â #Fail because NaNs are present |
---|
3752 | Â Â Â Â try: |
---|
3753 | Â Â Â Â Â Â domain2 =Â sww2domain(filename,boundary,fail_if_NaN=True,verbose=self.verbose) |
---|
3754 | Â Â Â Â except: |
---|
3755 | Â Â Â Â Â Â #Now import it, filling NaNs to be 0 |
---|
3756 | Â Â Â Â Â Â filler =Â 0 |
---|
3757 | Â Â Â Â Â Â domain2 =Â sww2domain(filename,None,fail_if_NaN=False,NaN_filler =Â filler,verbose=self.verbose) |
---|
3758 | |
---|
3759 | Â Â Â Â #Clean up |
---|
3760 | Â Â Â Â os.remove(filename) |
---|
3761 | |
---|
3762 | |
---|
3763 | Â Â Â Â bits =Â ['geo_reference.get_xllcorner()', |
---|
3764 | Â Â Â Â Â Â Â Â 'geo_reference.get_yllcorner()', |
---|
3765 | Â Â Â Â Â Â Â Â 'vertex_coordinates'] |
---|
3766 | |
---|
3767 |     for quantity in ['elevation']+domain.quantities_to_be_stored: |
---|
3768 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_integral()'Â %quantity) |
---|
3769 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_values()'Â %quantity) |
---|
3770 | |
---|
3771 |     for bit in bits: |
---|
3772 | Â Â Â Â #Â Â print 'testing that domain.'+bit+' has been restored' |
---|
3773 |       assert allclose(eval('domain.'+bit),eval('domain2.'+bit)) |
---|
3774 | |
---|
3775 | Â Â Â Â #print filler |
---|
3776 | Â Â Â Â #print max(max(domain2.get_quantity('xmomentum').get_values())) |
---|
3777 | Â Â Â Â |
---|
3778 |     assert max(max(domain2.get_quantity('xmomentum').get_values()))==filler |
---|
3779 |     assert min(min(domain2.get_quantity('xmomentum').get_values()))==filler |
---|
3780 |     assert max(max(domain2.get_quantity('ymomentum').get_values()))==filler |
---|
3781 |     assert min(min(domain2.get_quantity('ymomentum').get_values()))==filler |
---|
3782 | |
---|
3783 | |
---|
3784 | |
---|
3785 |   def test_weed(self): |
---|
3786 |     from data_manager import weed |
---|
3787 | |
---|
3788 | Â Â Â Â coordinates1 =Â [[0.,0.],[1.,0.],[1.,1.],[1.,0.],[2.,0.],[1.,1.]] |
---|
3789 | Â Â Â Â volumes1 =Â [[0,1,2],[3,4,5]] |
---|
3790 | Â Â Â Â boundary1=Â {(0,1):Â 'external',(1,2):Â 'not external',(2,0):Â 'external',(3,4):Â 'external',(4,5):Â 'external',(5,3):Â 'not external'} |
---|
3791 | Â Â Â Â coordinates2,volumes2,boundary2=weed(coordinates1,volumes1,boundary1) |
---|
3792 | |
---|
3793 | Â Â Â Â points2 =Â {(0.,0.):None,(1.,0.):None,(1.,1.):None,(2.,0.):None} |
---|
3794 | |
---|
3795 |     assert len(points2)==len(coordinates2) |
---|
3796 |     for i in range(len(coordinates2)): |
---|
3797 | Â Â Â Â Â Â coordinate =Â tuple(coordinates2[i]) |
---|
3798 |       assert points2.has_key(coordinate) |
---|
3799 | Â Â Â Â Â Â points2[coordinate]=i |
---|
3800 | |
---|
3801 |     for triangle in volumes1: |
---|
3802 |       for coordinate in triangle: |
---|
3803 |         assert coordinates2[points2[tuple(coordinates1[coordinate])]][0]==coordinates1[coordinate][0] |
---|
3804 |         assert coordinates2[points2[tuple(coordinates1[coordinate])]][1]==coordinates1[coordinate][1] |
---|
3805 | |
---|
3806 | |
---|
3807 | Â Â #FIXME This fails - smooth makes the comparism too hard for allclose |
---|
3808 |   def ztest_sww2domain3(self): |
---|
3809 | Â Â Â Â ################################################ |
---|
3810 | Â Â Â Â #DOMAIN.SMOOTH = TRUE !!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
3811 | Â Â Â Â ################################################ |
---|
3812 |     from mesh_factory import rectangular |
---|
3813 |     from Numeric import array |
---|
3814 | Â Â Â Â #Create basic mesh |
---|
3815 | |
---|
3816 | Â Â Â Â yiel=0.01 |
---|
3817 |     points, vertices, boundary = rectangular(10,10) |
---|
3818 | |
---|
3819 | Â Â Â Â #Create shallow water domain |
---|
3820 |     domain = Domain(points, vertices, boundary) |
---|
3821 | Â Â Â Â domain.geo_reference =Â Geo_reference(56,11,11) |
---|
3822 | Â Â Â Â domain.smooth =Â True |
---|
3823 | Â Â Â Â domain.store =Â True |
---|
3824 | Â Â Â Â domain.set_name('bedslope') |
---|
3825 | Â Â Â Â domain.default_order=2 |
---|
3826 | Â Â Â Â #Bed-slope and friction |
---|
3827 |     domain.set_quantity('elevation', lambda x,y: -x/3) |
---|
3828 |     domain.set_quantity('friction', 0.1) |
---|
3829 | Â Â Â Â # Boundary conditions |
---|
3830 |     from math import sin, pi |
---|
3831 | Â Â Â Â Br =Â Reflective_boundary(domain) |
---|
3832 | Â Â Â Â Bt =Â Transmissive_boundary(domain) |
---|
3833 | Â Â Â Â Bd =Â Dirichlet_boundary([0.2,0.,0.]) |
---|
3834 | Â Â Â Â Bw =Â Time_boundary(domain=domain, |
---|
3835 |               f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0]) |
---|
3836 | |
---|
3837 |     domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd}) |
---|
3838 | |
---|
3839 | Â Â Â Â domain.quantities_to_be_stored.extend(['xmomentum','ymomentum']) |
---|
3840 | Â Â Â Â #Initial condition |
---|
3841 | Â Â Â Â h =Â 0.05 |
---|
3842 | Â Â Â Â elevation =Â domain.quantities['elevation'].vertex_values |
---|
3843 |     domain.set_quantity('stage', elevation + h) |
---|
3844 | |
---|
3845 | |
---|
3846 | Â Â Â Â domain.check_integrity() |
---|
3847 | Â Â Â Â #Evolution |
---|
3848 |     for t in domain.evolve(yieldstep = yiel, finaltime = 0.05): |
---|
3849 | Â Â Â Â #Â Â domain.write_time() |
---|
3850 | Â Â Â Â Â Â pass |
---|
3851 | |
---|
3852 | |
---|
3853 | Â Â Â Â ########################################## |
---|
3854 | Â Â Â Â #Import the example's file as a new domain |
---|
3855 | Â Â Â Â ########################################## |
---|
3856 |     from data_manager import sww2domain |
---|
3857 |     from Numeric import allclose |
---|
3858 |     import os |
---|
3859 | |
---|
3860 | Â Â Â Â filename =Â domain.datadir +Â os.sep +Â domain.get_name()Â +Â '.sww' |
---|
3861 | Â Â Â Â domain2 =Â sww2domain(filename,None,fail_if_NaN=False,verbose=self.verbose) |
---|
3862 | Â Â Â Â #points, vertices, boundary = rectangular(15,15) |
---|
3863 | Â Â Â Â #domain2.boundary = boundary |
---|
3864 | Â Â Â Â ################### |
---|
3865 | Â Â Â Â ##NOW TEST IT!!! |
---|
3866 | Â Â Â Â ################### |
---|
3867 | |
---|
3868 | Â Â Â Â os.remove(domain.get_name()Â +Â '.sww') |
---|
3869 | |
---|
3870 | Â Â Â Â #FIXME smooth domain so that they can be compared |
---|
3871 | |
---|
3872 | |
---|
3873 | Â Â Â Â bits =Â []#'vertex_coordinates'] |
---|
3874 |     for quantity in ['elevation']+domain.quantities_to_be_stored: |
---|
3875 | Â Â Â Â Â Â bits.append('quantities["%s"].get_integral()'%quantity) |
---|
3876 | |
---|
3877 | |
---|
3878 |     for bit in bits: |
---|
3879 | Â Â Â Â Â Â #print 'testing that domain.'+bit+' has been restored' |
---|
3880 | Â Â Â Â Â Â #print bit |
---|
3881 | Â Â Â Â Â Â #print 'done' |
---|
3882 | Â Â Â Â Â Â #print ('domain.'+bit), eval('domain.'+bit) |
---|
3883 | Â Â Â Â Â Â #print ('domain2.'+bit), eval('domain2.'+bit) |
---|
3884 |       assert allclose(eval('domain.'+bit),eval('domain2.'+bit),rtol=1.0e-1,atol=1.e-3) |
---|
3885 | Â Â Â Â Â Â pass |
---|
3886 | |
---|
3887 | Â Â Â Â ###################################### |
---|
3888 | Â Â Â Â #Now evolve them both, just to be sure |
---|
3889 | Â Â Â Â ######################################x |
---|
3890 | Â Â Â Â domain.time =Â 0. |
---|
3891 |     from time import sleep |
---|
3892 | |
---|
3893 | Â Â Â Â final =Â .5 |
---|
3894 |     domain.set_quantity('friction', 0.1) |
---|
3895 | Â Â Â Â domain.store =Â False |
---|
3896 |     domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Br}) |
---|
3897 | |
---|
3898 |     for t in domain.evolve(yieldstep = yiel, finaltime = final): |
---|
3899 | Â Â Â Â Â Â #domain.write_time() |
---|
3900 | Â Â Â Â Â Â pass |
---|
3901 | |
---|
3902 | Â Â Â Â domain2.smooth =Â True |
---|
3903 | Â Â Â Â domain2.store =Â False |
---|
3904 | Â Â Â Â domain2.default_order=2 |
---|
3905 |     domain2.set_quantity('friction', 0.1) |
---|
3906 | Â Â Â Â #Bed-slope and friction |
---|
3907 | Â Â Â Â # Boundary conditions |
---|
3908 | Â Â Â Â Bd2=Dirichlet_boundary([0.2,0.,0.]) |
---|
3909 | Â Â Â Â Br2 =Â Reflective_boundary(domain2) |
---|
3910 | Â Â Â Â domain2.boundary =Â domain.boundary |
---|
3911 | Â Â Â Â #print 'domain2.boundary' |
---|
3912 | Â Â Â Â #print domain2.boundary |
---|
3913 |     domain2.set_boundary({'left': Bd2, 'right': Bd2, 'top': Bd2, 'bottom': Br2}) |
---|
3914 | Â Â Â Â #domain2.boundary = domain.boundary |
---|
3915 | Â Â Â Â #domain2.set_boundary({'exterior': Bd}) |
---|
3916 | |
---|
3917 | Â Â Â Â domain2.check_integrity() |
---|
3918 | |
---|
3919 |     for t in domain2.evolve(yieldstep = yiel, finaltime = final): |
---|
3920 | Â Â Â Â Â Â #domain2.write_time() |
---|
3921 | Â Â Â Â Â Â pass |
---|
3922 | |
---|
3923 | Â Â Â Â ################### |
---|
3924 | Â Â Â Â ##NOW TEST IT!!! |
---|
3925 | Â Â Â Â ################## |
---|
3926 | |
---|
3927 |     print '><><><><>>' |
---|
3928 | Â Â Â Â bits =Â [Â 'vertex_coordinates'] |
---|
3929 | |
---|
3930 |     for quantity in ['elevation','xmomentum','ymomentum']: |
---|
3931 | Â Â Â Â Â Â #bits.append('quantities["%s"].get_integral()'%quantity) |
---|
3932 | Â Â Â Â Â Â bits.append('get_quantity("%s").get_values()'Â %quantity) |
---|
3933 | |
---|
3934 |     for bit in bits: |
---|
3935 |       print bit |
---|
3936 |       assert allclose(eval('domain.'+bit),eval('domain2.'+bit)) |
---|
3937 | |
---|
3938 | |
---|
3939 |   def test_decimate_dem(self): |
---|
3940 | Â Â Â Â """Test decimation of dem file |
---|
3941 | Â Â Â Â """ |
---|
3942 | |
---|
3943 |     import os |
---|
3944 |     from Numeric import ones, allclose, Float, arange |
---|
3945 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
3946 | |
---|
3947 | Â Â Â Â #Write test dem file |
---|
3948 | Â Â Â Â root =Â 'decdemtest' |
---|
3949 | |
---|
3950 | Â Â Â Â filename =Â root +Â '.dem' |
---|
3951 |     fid = NetCDFFile(filename, 'w') |
---|
3952 | |
---|
3953 | Â Â Â Â fid.institution =Â 'Geoscience Australia' |
---|
3954 | Â Â Â Â fid.description =Â 'NetCDF DEM format for compact and portable 'Â +\ |
---|
3955 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'storage of spatial point data' |
---|
3956 | |
---|
3957 | Â Â Â Â nrows =Â 15 |
---|
3958 | Â Â Â Â ncols =Â 18 |
---|
3959 | |
---|
3960 | Â Â Â Â fid.ncols =Â ncols |
---|
3961 | Â Â Â Â fid.nrows =Â nrows |
---|
3962 | Â Â Â Â fid.xllcorner =Â 2000.5 |
---|
3963 | Â Â Â Â fid.yllcorner =Â 3000.5 |
---|
3964 | Â Â Â Â fid.cellsize =Â 25 |
---|
3965 | Â Â Â Â fid.NODATA_value =Â -9999 |
---|
3966 | |
---|
3967 | Â Â Â Â fid.zone =Â 56 |
---|
3968 | Â Â Â Â fid.false_easting =Â 0.0 |
---|
3969 | Â Â Â Â fid.false_northing =Â 0.0 |
---|
3970 | Â Â Â Â fid.projection =Â 'UTM' |
---|
3971 | Â Â Â Â fid.datum =Â 'WGS84' |
---|
3972 | Â Â Â Â fid.units =Â 'METERS' |
---|
3973 | |
---|
3974 |     fid.createDimension('number_of_points', nrows*ncols) |
---|
3975 | |
---|
3976 |     fid.createVariable('elevation', Float, ('number_of_points',)) |
---|
3977 | |
---|
3978 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
3979 | |
---|
3980 | Â Â Â Â elevation[:]Â =Â (arange(nrows*ncols)) |
---|
3981 | |
---|
3982 | Â Â Â Â fid.close() |
---|
3983 | |
---|
3984 | Â Â Â Â #generate the elevation values expected in the decimated file |
---|
3985 | Â Â Â Â ref_elevation =Â [(Â 0+Â 1+Â 2+Â 18+Â 19+Â 20+Â 36+Â 37+Â 38)Â /Â 9.0, |
---|
3986 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 4+Â 5+Â 6+Â 22+Â 23+Â 24+Â 40+Â 41+Â 42)Â /Â 9.0, |
---|
3987 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 8+Â 9+Â 10+Â 26+Â 27+Â 28+Â 44+Â 45+Â 46)Â /Â 9.0, |
---|
3988 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 12+Â 13+Â 14+Â 30+Â 31+Â 32+Â 48+Â 49+Â 50)Â /Â 9.0, |
---|
3989 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 72+Â 73+Â 74+Â 90+Â 91+Â 92+108+109+110)Â /Â 9.0, |
---|
3990 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 76+Â 77+Â 78+Â 94+Â 95+Â 96+112+113+114)Â /Â 9.0, |
---|
3991 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 80+Â 81+Â 82+Â 98+Â 99+100+116+117+118)Â /Â 9.0, |
---|
3992 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 84+Â 85+Â 86+102+103+104+120+121+122)Â /Â 9.0, |
---|
3993 | Â Â Â Â Â Â Â Â Â Â Â Â Â (144+145+146+162+163+164+180+181+182)Â /Â 9.0, |
---|
3994 | Â Â Â Â Â Â Â Â Â Â Â Â Â (148+149+150+166+167+168+184+185+186)Â /Â 9.0, |
---|
3995 | Â Â Â Â Â Â Â Â Â Â Â Â Â (152+153+154+170+171+172+188+189+190)Â /Â 9.0, |
---|
3996 | Â Â Â Â Â Â Â Â Â Â Â Â Â (156+157+158+174+175+176+192+193+194)Â /Â 9.0, |
---|
3997 | Â Â Â Â Â Â Â Â Â Â Â Â Â (216+217+218+234+235+236+252+253+254)Â /Â 9.0, |
---|
3998 | Â Â Â Â Â Â Â Â Â Â Â Â Â (220+221+222+238+239+240+256+257+258)Â /Â 9.0, |
---|
3999 | Â Â Â Â Â Â Â Â Â Â Â Â Â (224+225+226+242+243+244+260+261+262)Â /Â 9.0, |
---|
4000 | Â Â Â Â Â Â Â Â Â Â Â Â Â (228+229+230+246+247+248+264+265+266)Â /Â 9.0] |
---|
4001 | |
---|
4002 | Â Â Â Â #generate a stencil for computing the decimated values |
---|
4003 |     stencil = ones((3,3), Float) / 9.0 |
---|
4004 | |
---|
4005 |     decimate_dem(root, stencil=stencil, cellsize_new=100) |
---|
4006 | |
---|
4007 | Â Â Â Â #Open decimated NetCDF file |
---|
4008 |     fid = NetCDFFile(root + '_100.dem', 'r') |
---|
4009 | |
---|
4010 | Â Â Â Â # Get decimated elevation |
---|
4011 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
4012 | |
---|
4013 | Â Â Â Â #Check values |
---|
4014 |     assert allclose(elevation, ref_elevation) |
---|
4015 | |
---|
4016 | Â Â Â Â #Cleanup |
---|
4017 | Â Â Â Â fid.close() |
---|
4018 | |
---|
4019 | Â Â Â Â os.remove(root +Â '.dem') |
---|
4020 | Â Â Â Â os.remove(root +Â '_100.dem') |
---|
4021 | |
---|
4022 |   def test_decimate_dem_NODATA(self): |
---|
4023 | Â Â Â Â """Test decimation of dem file that includes NODATA values |
---|
4024 | Â Â Â Â """ |
---|
4025 | |
---|
4026 |     import os |
---|
4027 |     from Numeric import ones, allclose, Float, arange, reshape |
---|
4028 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
4029 | |
---|
4030 | Â Â Â Â #Write test dem file |
---|
4031 | Â Â Â Â root =Â 'decdemtest' |
---|
4032 | |
---|
4033 | Â Â Â Â filename =Â root +Â '.dem' |
---|
4034 |     fid = NetCDFFile(filename, 'w') |
---|
4035 | |
---|
4036 | Â Â Â Â fid.institution =Â 'Geoscience Australia' |
---|
4037 | Â Â Â Â fid.description =Â 'NetCDF DEM format for compact and portable 'Â +\ |
---|
4038 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'storage of spatial point data' |
---|
4039 | |
---|
4040 | Â Â Â Â nrows =Â 15 |
---|
4041 | Â Â Â Â ncols =Â 18 |
---|
4042 | Â Â Â Â NODATA_value =Â -9999 |
---|
4043 | |
---|
4044 | Â Â Â Â fid.ncols =Â ncols |
---|
4045 | Â Â Â Â fid.nrows =Â nrows |
---|
4046 | Â Â Â Â fid.xllcorner =Â 2000.5 |
---|
4047 | Â Â Â Â fid.yllcorner =Â 3000.5 |
---|
4048 | Â Â Â Â fid.cellsize =Â 25 |
---|
4049 | Â Â Â Â fid.NODATA_value =Â NODATA_value |
---|
4050 | |
---|
4051 | Â Â Â Â fid.zone =Â 56 |
---|
4052 | Â Â Â Â fid.false_easting =Â 0.0 |
---|
4053 | Â Â Â Â fid.false_northing =Â 0.0 |
---|
4054 | Â Â Â Â fid.projection =Â 'UTM' |
---|
4055 | Â Â Â Â fid.datum =Â 'WGS84' |
---|
4056 | Â Â Â Â fid.units =Â 'METERS' |
---|
4057 | |
---|
4058 |     fid.createDimension('number_of_points', nrows*ncols) |
---|
4059 | |
---|
4060 |     fid.createVariable('elevation', Float, ('number_of_points',)) |
---|
4061 | |
---|
4062 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
4063 | |
---|
4064 | Â Â Â Â #generate initial elevation values |
---|
4065 | Â Â Â Â elevation_tmp =Â (arange(nrows*ncols)) |
---|
4066 | Â Â Â Â #add some NODATA values |
---|
4067 | Â Â Â Â elevation_tmp[0]Â Â =Â NODATA_value |
---|
4068 | Â Â Â Â elevation_tmp[95]Â =Â NODATA_value |
---|
4069 | Â Â Â Â elevation_tmp[188]Â =Â NODATA_value |
---|
4070 | Â Â Â Â elevation_tmp[189]Â =Â NODATA_value |
---|
4071 | Â Â Â Â elevation_tmp[190]Â =Â NODATA_value |
---|
4072 | Â Â Â Â elevation_tmp[209]Â =Â NODATA_value |
---|
4073 | Â Â Â Â elevation_tmp[252]Â =Â NODATA_value |
---|
4074 | |
---|
4075 | Â Â Â Â elevation[:]Â =Â elevation_tmp |
---|
4076 | |
---|
4077 | Â Â Â Â fid.close() |
---|
4078 | |
---|
4079 | Â Â Â Â #generate the elevation values expected in the decimated file |
---|
4080 | Â Â Â Â ref_elevation =Â [NODATA_value, |
---|
4081 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 4+Â 5+Â 6+Â 22+Â 23+Â 24+Â 40+Â 41+Â 42)Â /Â 9.0, |
---|
4082 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 8+Â 9+Â 10+Â 26+Â 27+Â 28+Â 44+Â 45+Â 46)Â /Â 9.0, |
---|
4083 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 12+Â 13+Â 14+Â 30+Â 31+Â 32+Â 48+Â 49+Â 50)Â /Â 9.0, |
---|
4084 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 72+Â 73+Â 74+Â 90+Â 91+Â 92+108+109+110)Â /Â 9.0, |
---|
4085 | Â Â Â Â Â Â Â Â Â Â Â Â Â NODATA_value, |
---|
4086 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 80+Â 81+Â 82+Â 98+Â 99+100+116+117+118)Â /Â 9.0, |
---|
4087 | Â Â Â Â Â Â Â Â Â Â Â Â Â (Â 84+Â 85+Â 86+102+103+104+120+121+122)Â /Â 9.0, |
---|
4088 | Â Â Â Â Â Â Â Â Â Â Â Â Â (144+145+146+162+163+164+180+181+182)Â /Â 9.0, |
---|
4089 | Â Â Â Â Â Â Â Â Â Â Â Â Â (148+149+150+166+167+168+184+185+186)Â /Â 9.0, |
---|
4090 | Â Â Â Â Â Â Â Â Â Â Â Â Â NODATA_value, |
---|
4091 | Â Â Â Â Â Â Â Â Â Â Â Â Â (156+157+158+174+175+176+192+193+194)Â /Â 9.0, |
---|
4092 | Â Â Â Â Â Â Â Â Â Â Â Â Â NODATA_value, |
---|
4093 | Â Â Â Â Â Â Â Â Â Â Â Â Â (220+221+222+238+239+240+256+257+258)Â /Â 9.0, |
---|
4094 | Â Â Â Â Â Â Â Â Â Â Â Â Â (224+225+226+242+243+244+260+261+262)Â /Â 9.0, |
---|
4095 | Â Â Â Â Â Â Â Â Â Â Â Â Â (228+229+230+246+247+248+264+265+266)Â /Â 9.0] |
---|
4096 | |
---|
4097 | Â Â Â Â #generate a stencil for computing the decimated values |
---|
4098 |     stencil = ones((3,3), Float) / 9.0 |
---|
4099 | |
---|
4100 |     decimate_dem(root, stencil=stencil, cellsize_new=100) |
---|
4101 | |
---|
4102 | Â Â Â Â #Open decimated NetCDF file |
---|
4103 |     fid = NetCDFFile(root + '_100.dem', 'r') |
---|
4104 | |
---|
4105 | Â Â Â Â # Get decimated elevation |
---|
4106 | Â Â Â Â elevation =Â fid.variables['elevation'] |
---|
4107 | |
---|
4108 | Â Â Â Â #Check values |
---|
4109 |     assert allclose(elevation, ref_elevation) |
---|
4110 | |
---|
4111 | Â Â Â Â #Cleanup |
---|
4112 | Â Â Â Â fid.close() |
---|
4113 | |
---|
4114 | Â Â Â Â os.remove(root +Â '.dem') |
---|
4115 | Â Â Â Â os.remove(root +Â '_100.dem') |
---|
4116 | |
---|
4117 |   def xxxtestz_sww2ers_real(self): |
---|
4118 | Â Â Â Â """Test that sww information can be converted correctly to asc/prj |
---|
4119 | Â Â Â Â format readable by e.g. ArcView |
---|
4120 | Â Â Â Â """ |
---|
4121 | |
---|
4122 |     import time, os |
---|
4123 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
4124 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
4125 | |
---|
4126 | Â Â Â Â # the memory optimised least squares |
---|
4127 |     # cellsize = 20,  # this one seems to hang |
---|
4128 | Â Â Â Â #Â cellsize = 200000, # Ran 1 test in 269.703s |
---|
4129 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #Ran 1 test in 267.344s |
---|
4130 |     # cellsize = 20000, # Ran 1 test in 460.922s |
---|
4131 | Â Â Â Â #Â cellsize = 2000Â Â #Ran 1 test in 5340.250s |
---|
4132 | Â Â Â Â #Â cellsize = 200Â Â #this one seems to hang, building matirx A |
---|
4133 | |
---|
4134 | Â Â Â Â # not optimised |
---|
4135 | Â Â Â Â # seems to hang |
---|
4136 | Â Â Â Â #Â cellsize = 2000Â Â # Ran 1 test in 5334.563s |
---|
4137 | Â Â Â Â #Export to ascii/prj files |
---|
4138 | Â Â Â Â sww2dem('karratha_100m', |
---|
4139 | Â Â Â Â Â Â Â Â quantity =Â 'depth', |
---|
4140 | Â Â Â Â Â Â Â Â cellsize =Â 200000, |
---|
4141 | Â Â Â Â Â Â Â Â verbose =Â True) |
---|
4142 | |
---|
4143 |   def test_read_asc(self): |
---|
4144 | Â Â Â Â """Test conversion from dem in ascii format to native NetCDF format |
---|
4145 | Â Â Â Â """ |
---|
4146 | |
---|
4147 |     import time, os |
---|
4148 |     from Numeric import array, zeros, allclose, Float, concatenate |
---|
4149 |     from Scientific.IO.NetCDF import NetCDFFile |
---|
4150 | |
---|
4151 |     from data_manager import _read_asc |
---|
4152 | Â Â Â Â #Write test asc file |
---|
4153 | Â Â Â Â filename =Â tempfile.mktemp(".000") |
---|
4154 |     fid = open(filename, 'w') |
---|
4155 |     fid.write("""ncols     7 |
---|
4156 | nrows     4 |
---|
4157 | xllcorner   2000.5 |
---|
4158 | yllcorner   3000.5 |
---|
4159 | cellsize   25 |
---|
4160 | NODATA_value -9999 |
---|
4161 | Â Â 97.921Â Â 99.285Â Â 125.588Â Â 180.830Â Â 258.645Â Â 342.872Â Â 415.836 |
---|
4162 | Â Â 473.157Â Â 514.391Â Â 553.893Â Â 607.120Â Â 678.125Â Â 777.283Â Â 883.038 |
---|
4163 | Â Â 984.494Â 1040.349Â 1008.161Â Â 900.738Â Â 730.882Â Â 581.430Â Â 514.980 |
---|
4164 | Â Â 502.645Â Â 516.230Â Â 504.739Â Â 450.604Â Â 388.500Â Â 338.097Â Â 514.980 |
---|
4165 | """) |
---|
4166 | Â Â Â Â fid.close() |
---|
4167 |     bath_metadata, grid = _read_asc(filename, verbose=self.verbose) |
---|
4168 |     self.failUnless(bath_metadata['xllcorner'] == 2000.5, 'Failed') |
---|
4169 |     self.failUnless(bath_metadata['yllcorner'] == 3000.5, 'Failed') |
---|
4170 |     self.failUnless(bath_metadata['cellsize'] == 25, 'Failed') |
---|
4171 |     self.failUnless(bath_metadata['NODATA_value'] == -9999, 'Failed') |
---|
4172 |     self.failUnless(grid[0][0] == 97.921, 'Failed') |
---|
4173 |     self.failUnless(grid[3][6] == 514.980, 'Failed') |
---|
4174 | |
---|
4175 | Â Â Â Â os.remove(filename) |
---|
4176 | |
---|
4177 |   def test_asc_csiro2sww(self): |
---|
4178 |     import tempfile |
---|
4179 | |
---|
4180 | Â Â Â Â bath_dir =Â tempfile.mkdtemp() |
---|
4181 | Â Â Â Â bath_dir_filename =Â bath_dir +Â os.sep +'ba19940524.000' |
---|
4182 | Â Â Â Â #bath_dir = 'bath_data_manager_test' |
---|
4183 | Â Â Â Â #print "os.getcwd( )",os.getcwd( ) |
---|
4184 | Â Â Â Â elevation_dir =Â tempfile.mkdtemp() |
---|
4185 | Â Â Â Â #elevation_dir = 'elev_expanded' |
---|
4186 | Â Â Â Â elevation_dir_filename1 =Â elevation_dir +Â os.sep +'el19940524.000' |
---|
4187 | Â Â Â Â elevation_dir_filename2 =Â elevation_dir +Â os.sep +'el19940524.001' |
---|
4188 | |
---|
4189 |     fid = open(bath_dir_filename, 'w') |
---|
4190 |     fid.write(""" ncols       3 |
---|
4191 |  nrows       2 |
---|
4192 |  xllcorner  148.00000 |
---|
4193 |  yllcorner  -38.00000 |
---|
4194 |  cellsize    0.25 |
---|
4195 |  nodata_value  -9999.0 |
---|
4196 | Â Â 9000.000Â Â -1000.000Â Â 3000.0 |
---|
4197 | Â Â -1000.000Â Â 9000.000Â -1000.000 |
---|
4198 | """) |
---|
4199 | Â Â Â Â fid.close() |
---|
4200 | |
---|
4201 |     fid = open(elevation_dir_filename1, 'w') |
---|
4202 |     fid.write(""" ncols       3 |
---|
4203 |  nrows       2 |
---|
4204 |  xllcorner  148.00000 |
---|
4205 |  yllcorner  -38.00000 |
---|
4206 |  cellsize    0.25 |
---|
4207 |  nodata_value  -9999.0 |
---|
4208 | Â Â 9000.000Â Â 0.000Â Â 3000.0 |
---|
4209 | Â Â Â 0.000Â Â Â 9000.000Â Â Â 0.000 |
---|
4210 | """) |
---|
4211 | Â Â Â Â fid.close() |
---|
4212 | |
---|
4213 |     fid = open(elevation_dir_filename2, 'w') |
---|
4214 |     fid.write(""" ncols       3 |
---|
4215 |  nrows       2 |
---|
4216 |  xllcorner  148.00000 |
---|
4217 |  yllcorner  -38.00000 |
---|
4218 |  cellsize    0.25 |
---|
4219 |  nodata_value  -9999.0 |
---|
4220 | Â Â 9000.000Â Â 4000.000Â Â 4000.0 |
---|
4221 | Â Â 4000.000Â Â 9000.000Â Â 4000.000 |
---|
4222 | """) |
---|
4223 | Â Â Â Â fid.close() |
---|
4224 | |
---|
4225 | Â Â Â Â ucur_dir =Â tempfile.mkdtemp() |
---|
4226 | Â Â Â Â ucur_dir_filename1 =Â ucur_dir +Â os.sep +'uc19940524.000' |
---|
4227 | Â Â Â Â ucur_dir_filename2 =Â ucur_dir +Â os.sep +'uc19940524.001' |
---|
4228 | |
---|
4229 |     fid = open(ucur_dir_filename1, 'w') |
---|
4230 |     fid.write(""" ncols       3 |
---|
4231 |  nrows       2 |
---|
4232 |  xllcorner  148.00000 |
---|
4233 |  yllcorner  -38.00000 |
---|
4234 |  cellsize    0.25 |
---|
4235 |  nodata_value  -9999.0 |
---|
4236 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4237 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4238 | """) |
---|
4239 | Â Â Â Â fid.close() |
---|
4240 |     fid = open(ucur_dir_filename2, 'w') |
---|
4241 |     fid.write(""" ncols       3 |
---|
4242 |  nrows       2 |
---|
4243 |  xllcorner  148.00000 |
---|
4244 |  yllcorner  -38.00000 |
---|
4245 |  cellsize    0.25 |
---|
4246 |  nodata_value  -9999.0 |
---|
4247 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4248 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4249 | """) |
---|
4250 | Â Â Â Â fid.close() |
---|
4251 | |
---|
4252 | Â Â Â Â vcur_dir =Â tempfile.mkdtemp() |
---|
4253 | Â Â Â Â vcur_dir_filename1 =Â vcur_dir +Â os.sep +'vc19940524.000' |
---|
4254 | Â Â Â Â vcur_dir_filename2 =Â vcur_dir +Â os.sep +'vc19940524.001' |
---|
4255 | |
---|
4256 |     fid = open(vcur_dir_filename1, 'w') |
---|
4257 |     fid.write(""" ncols       3 |
---|
4258 |  nrows       2 |
---|
4259 |  xllcorner  148.00000 |
---|
4260 |  yllcorner  -38.00000 |
---|
4261 |  cellsize    0.25 |
---|
4262 |  nodata_value  -9999.0 |
---|
4263 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4264 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4265 | """) |
---|
4266 | Â Â Â Â fid.close() |
---|
4267 |     fid = open(vcur_dir_filename2, 'w') |
---|
4268 |     fid.write(""" ncols       3 |
---|
4269 |  nrows       2 |
---|
4270 |  xllcorner  148.00000 |
---|
4271 |  yllcorner  -38.00000 |
---|
4272 |  cellsize    0.25 |
---|
4273 |  nodata_value  -9999.0 |
---|
4274 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4275 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4276 | """) |
---|
4277 | Â Â Â Â fid.close() |
---|
4278 | |
---|
4279 | Â Â Â Â sww_file =Â 'a_test.sww' |
---|
4280 |     asc_csiro2sww(bath_dir,elevation_dir, ucur_dir, vcur_dir, sww_file, |
---|
4281 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
4282 | |
---|
4283 | Â Â Â Â # check the sww file |
---|
4284 | |
---|
4285 |     fid = NetCDFFile(sww_file, 'r')  #Open existing file for read |
---|
4286 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
4287 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
4288 | Â Â Â Â z =Â fid.variables['z'][:] |
---|
4289 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
4290 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
4291 | Â Â Â Â geo_ref =Â Geo_reference(NetCDFObject=fid) |
---|
4292 | Â Â Â Â #print "geo_ref",geo_ref |
---|
4293 | Â Â Â Â x_ref =Â geo_ref.get_xllcorner() |
---|
4294 | Â Â Â Â y_ref =Â geo_ref.get_yllcorner() |
---|
4295 |     self.failUnless(geo_ref.get_zone() == 55, 'Failed') |
---|
4296 |     assert allclose(x_ref, 587798.418) # (-38, 148) |
---|
4297 |     assert allclose(y_ref, 5793123.477)# (-38, 148.5) |
---|
4298 | |
---|
4299 | Â Â Â Â #Zone:Â Â 55 |
---|
4300 | Â Â Â Â #Easting:Â 588095.674Â Northing: 5821451.722 |
---|
4301 | Â Â Â Â #Latitude:Â Â -37Â 45 ' 0.00000 ''Â Longitude: 148 0 ' 0.00000 '' |
---|
4302 |     assert allclose((x[0],y[0]), (588095.674 - x_ref, 5821451.722 - y_ref)) |
---|
4303 | |
---|
4304 | Â Â Â Â #Zone:Â Â 55 |
---|
4305 | Â Â Â Â #Easting:Â 632145.632Â Northing: 5820863.269 |
---|
4306 | Â Â Â Â #Latitude:Â Â -37Â 45 ' 0.00000 ''Â Longitude: 148Â 30 ' 0.00000 '' |
---|
4307 |     assert allclose((x[2],y[2]), (632145.632 - x_ref, 5820863.269 - y_ref)) |
---|
4308 | |
---|
4309 | Â Â Â Â #Zone:Â Â 55 |
---|
4310 | Â Â Â Â #Easting:Â 609748.788Â Northing: 5793447.860 |
---|
4311 | Â Â Â Â #Latitude:Â Â -38Â 0 ' 0.00000 ''Â Longitude: 148Â 15 ' 0.00000 '' |
---|
4312 |     assert allclose((x[4],y[4]), (609748.788 - x_ref, 5793447.86 - y_ref)) |
---|
4313 | |
---|
4314 |     assert allclose(z[0],9000.0 ) |
---|
4315 |     assert allclose(stage[0][1],0.0 ) |
---|
4316 | |
---|
4317 | Â Â Â Â #(4000+1000)*60 |
---|
4318 |     assert allclose(xmomentum[1][1],300000.0 ) |
---|
4319 | |
---|
4320 | |
---|
4321 | Â Â Â Â fid.close() |
---|
4322 | |
---|
4323 | Â Â Â Â #tidy up |
---|
4324 | Â Â Â Â os.remove(bath_dir_filename) |
---|
4325 | Â Â Â Â os.rmdir(bath_dir) |
---|
4326 | |
---|
4327 | Â Â Â Â os.remove(elevation_dir_filename1) |
---|
4328 | Â Â Â Â os.remove(elevation_dir_filename2) |
---|
4329 | Â Â Â Â os.rmdir(elevation_dir) |
---|
4330 | |
---|
4331 | Â Â Â Â os.remove(ucur_dir_filename1) |
---|
4332 | Â Â Â Â os.remove(ucur_dir_filename2) |
---|
4333 | Â Â Â Â os.rmdir(ucur_dir) |
---|
4334 | |
---|
4335 | Â Â Â Â os.remove(vcur_dir_filename1) |
---|
4336 | Â Â Â Â os.remove(vcur_dir_filename2) |
---|
4337 | Â Â Â Â os.rmdir(vcur_dir) |
---|
4338 | |
---|
4339 | |
---|
4340 | Â Â Â Â # remove sww file |
---|
4341 | Â Â Â Â os.remove(sww_file) |
---|
4342 | |
---|
4343 |   def test_asc_csiro2sww2(self): |
---|
4344 |     import tempfile |
---|
4345 | |
---|
4346 | Â Â Â Â bath_dir =Â tempfile.mkdtemp() |
---|
4347 | Â Â Â Â bath_dir_filename =Â bath_dir +Â os.sep +'ba19940524.000' |
---|
4348 | Â Â Â Â #bath_dir = 'bath_data_manager_test' |
---|
4349 | Â Â Â Â #print "os.getcwd( )",os.getcwd( ) |
---|
4350 | Â Â Â Â elevation_dir =Â tempfile.mkdtemp() |
---|
4351 | Â Â Â Â #elevation_dir = 'elev_expanded' |
---|
4352 | Â Â Â Â elevation_dir_filename1 =Â elevation_dir +Â os.sep +'el19940524.000' |
---|
4353 | Â Â Â Â elevation_dir_filename2 =Â elevation_dir +Â os.sep +'el19940524.001' |
---|
4354 | |
---|
4355 |     fid = open(bath_dir_filename, 'w') |
---|
4356 |     fid.write(""" ncols       3 |
---|
4357 |  nrows       2 |
---|
4358 |  xllcorner  148.00000 |
---|
4359 |  yllcorner  -38.00000 |
---|
4360 |  cellsize    0.25 |
---|
4361 |  nodata_value  -9999.0 |
---|
4362 | Â Â 9000.000Â Â -1000.000Â Â 3000.0 |
---|
4363 | Â Â -1000.000Â Â 9000.000Â -1000.000 |
---|
4364 | """) |
---|
4365 | Â Â Â Â fid.close() |
---|
4366 | |
---|
4367 |     fid = open(elevation_dir_filename1, 'w') |
---|
4368 |     fid.write(""" ncols       3 |
---|
4369 |  nrows       2 |
---|
4370 |  xllcorner  148.00000 |
---|
4371 |  yllcorner  -38.00000 |
---|
4372 |  cellsize    0.25 |
---|
4373 |  nodata_value  -9999.0 |
---|
4374 | Â Â 9000.000Â Â 0.000Â Â 3000.0 |
---|
4375 | Â Â Â 0.000Â Â Â -9999.000Â Â Â -9999.000 |
---|
4376 | """) |
---|
4377 | Â Â Â Â fid.close() |
---|
4378 | |
---|
4379 |     fid = open(elevation_dir_filename2, 'w') |
---|
4380 |     fid.write(""" ncols       3 |
---|
4381 |  nrows       2 |
---|
4382 |  xllcorner  148.00000 |
---|
4383 |  yllcorner  -38.00000 |
---|
4384 |  cellsize    0.25 |
---|
4385 |  nodata_value  -9999.0 |
---|
4386 | Â Â 9000.000Â Â 4000.000Â Â 4000.0 |
---|
4387 | Â Â 4000.000Â Â 9000.000Â Â 4000.000 |
---|
4388 | """) |
---|
4389 | Â Â Â Â fid.close() |
---|
4390 | |
---|
4391 | Â Â Â Â ucur_dir =Â tempfile.mkdtemp() |
---|
4392 | Â Â Â Â ucur_dir_filename1 =Â ucur_dir +Â os.sep +'uc19940524.000' |
---|
4393 | Â Â Â Â ucur_dir_filename2 =Â ucur_dir +Â os.sep +'uc19940524.001' |
---|
4394 | |
---|
4395 |     fid = open(ucur_dir_filename1, 'w') |
---|
4396 |     fid.write(""" ncols       3 |
---|
4397 |  nrows       2 |
---|
4398 |  xllcorner  148.00000 |
---|
4399 |  yllcorner  -38.00000 |
---|
4400 |  cellsize    0.25 |
---|
4401 |  nodata_value  -9999.0 |
---|
4402 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4403 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4404 | """) |
---|
4405 | Â Â Â Â fid.close() |
---|
4406 |     fid = open(ucur_dir_filename2, 'w') |
---|
4407 |     fid.write(""" ncols       3 |
---|
4408 |  nrows       2 |
---|
4409 |  xllcorner  148.00000 |
---|
4410 |  yllcorner  -38.00000 |
---|
4411 |  cellsize    0.25 |
---|
4412 |  nodata_value  -9999.0 |
---|
4413 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4414 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4415 | """) |
---|
4416 | Â Â Â Â fid.close() |
---|
4417 | |
---|
4418 | Â Â Â Â vcur_dir =Â tempfile.mkdtemp() |
---|
4419 | Â Â Â Â vcur_dir_filename1 =Â vcur_dir +Â os.sep +'vc19940524.000' |
---|
4420 | Â Â Â Â vcur_dir_filename2 =Â vcur_dir +Â os.sep +'vc19940524.001' |
---|
4421 | |
---|
4422 |     fid = open(vcur_dir_filename1, 'w') |
---|
4423 |     fid.write(""" ncols       3 |
---|
4424 |  nrows       2 |
---|
4425 |  xllcorner  148.00000 |
---|
4426 |  yllcorner  -38.00000 |
---|
4427 |  cellsize    0.25 |
---|
4428 |  nodata_value  -9999.0 |
---|
4429 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4430 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4431 | """) |
---|
4432 | Â Â Â Â fid.close() |
---|
4433 |     fid = open(vcur_dir_filename2, 'w') |
---|
4434 |     fid.write(""" ncols       3 |
---|
4435 |  nrows       2 |
---|
4436 |  xllcorner  148.00000 |
---|
4437 |  yllcorner  -38.00000 |
---|
4438 |  cellsize    0.25 |
---|
4439 |  nodata_value  -9999.0 |
---|
4440 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4441 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4442 | """) |
---|
4443 | Â Â Â Â fid.close() |
---|
4444 | |
---|
4445 | Â Â Â Â try: |
---|
4446 |       asc_csiro2sww(bath_dir,elevation_dir, ucur_dir, |
---|
4447 |              vcur_dir, sww_file, |
---|
4448 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
4449 | Â Â Â Â except: |
---|
4450 | Â Â Â Â Â Â #tidy up |
---|
4451 | Â Â Â Â Â Â os.remove(bath_dir_filename) |
---|
4452 | Â Â Â Â Â Â os.rmdir(bath_dir) |
---|
4453 | |
---|
4454 | Â Â Â Â Â Â os.remove(elevation_dir_filename1) |
---|
4455 | Â Â Â Â Â Â os.remove(elevation_dir_filename2) |
---|
4456 | Â Â Â Â Â Â os.rmdir(elevation_dir) |
---|
4457 | |
---|
4458 | Â Â Â Â Â Â os.remove(ucur_dir_filename1) |
---|
4459 | Â Â Â Â Â Â os.remove(ucur_dir_filename2) |
---|
4460 | Â Â Â Â Â Â os.rmdir(ucur_dir) |
---|
4461 | |
---|
4462 | Â Â Â Â Â Â os.remove(vcur_dir_filename1) |
---|
4463 | Â Â Â Â Â Â os.remove(vcur_dir_filename2) |
---|
4464 | Â Â Â Â Â Â os.rmdir(vcur_dir) |
---|
4465 | Â Â Â Â else: |
---|
4466 | Â Â Â Â Â Â #tidy up |
---|
4467 | Â Â Â Â Â Â os.remove(bath_dir_filename) |
---|
4468 | Â Â Â Â Â Â os.rmdir(bath_dir) |
---|
4469 | |
---|
4470 | Â Â Â Â Â Â os.remove(elevation_dir_filename1) |
---|
4471 | Â Â Â Â Â Â os.remove(elevation_dir_filename2) |
---|
4472 | Â Â Â Â Â Â os.rmdir(elevation_dir) |
---|
4473 |       raise 'Should raise exception' |
---|
4474 | |
---|
4475 | Â Â Â Â Â Â os.remove(ucur_dir_filename1) |
---|
4476 | Â Â Â Â Â Â os.remove(ucur_dir_filename2) |
---|
4477 | Â Â Â Â Â Â os.rmdir(ucur_dir) |
---|
4478 | |
---|
4479 | Â Â Â Â Â Â os.remove(vcur_dir_filename1) |
---|
4480 | Â Â Â Â Â Â os.remove(vcur_dir_filename2) |
---|
4481 | Â Â Â Â Â Â os.rmdir(vcur_dir) |
---|
4482 | |
---|
4483 | |
---|
4484 | |
---|
4485 |   def test_asc_csiro2sww3(self): |
---|
4486 |     import tempfile |
---|
4487 | |
---|
4488 | Â Â Â Â bath_dir =Â tempfile.mkdtemp() |
---|
4489 | Â Â Â Â bath_dir_filename =Â bath_dir +Â os.sep +'ba19940524.000' |
---|
4490 | Â Â Â Â #bath_dir = 'bath_data_manager_test' |
---|
4491 | Â Â Â Â #print "os.getcwd( )",os.getcwd( ) |
---|
4492 | Â Â Â Â elevation_dir =Â tempfile.mkdtemp() |
---|
4493 | Â Â Â Â #elevation_dir = 'elev_expanded' |
---|
4494 | Â Â Â Â elevation_dir_filename1 =Â elevation_dir +Â os.sep +'el19940524.000' |
---|
4495 | Â Â Â Â elevation_dir_filename2 =Â elevation_dir +Â os.sep +'el19940524.001' |
---|
4496 | |
---|
4497 |     fid = open(bath_dir_filename, 'w') |
---|
4498 |     fid.write(""" ncols       3 |
---|
4499 |  nrows       2 |
---|
4500 |  xllcorner  148.00000 |
---|
4501 |  yllcorner  -38.00000 |
---|
4502 |  cellsize    0.25 |
---|
4503 |  nodata_value  -9999.0 |
---|
4504 | Â Â 9000.000Â Â -1000.000Â Â 3000.0 |
---|
4505 | Â Â -1000.000Â Â 9000.000Â -1000.000 |
---|
4506 | """) |
---|
4507 | Â Â Â Â fid.close() |
---|
4508 | |
---|
4509 |     fid = open(elevation_dir_filename1, 'w') |
---|
4510 |     fid.write(""" ncols       3 |
---|
4511 |  nrows       2 |
---|
4512 |  xllcorner  148.00000 |
---|
4513 |  yllcorner  -38.00000 |
---|
4514 |  cellsize    0.25 |
---|
4515 |  nodata_value  -9999.0 |
---|
4516 | Â Â 9000.000Â Â 0.000Â Â 3000.0 |
---|
4517 | Â Â Â 0.000Â Â Â -9999.000Â Â Â -9999.000 |
---|
4518 | """) |
---|
4519 | Â Â Â Â fid.close() |
---|
4520 | |
---|
4521 |     fid = open(elevation_dir_filename2, 'w') |
---|
4522 |     fid.write(""" ncols       3 |
---|
4523 |  nrows       2 |
---|
4524 |  xllcorner  148.00000 |
---|
4525 |  yllcorner  -38.00000 |
---|
4526 |  cellsize    0.25 |
---|
4527 |  nodata_value  -9999.0 |
---|
4528 | Â Â 9000.000Â Â 4000.000Â Â 4000.0 |
---|
4529 | Â Â 4000.000Â Â 9000.000Â Â 4000.000 |
---|
4530 | """) |
---|
4531 | Â Â Â Â fid.close() |
---|
4532 | |
---|
4533 | Â Â Â Â ucur_dir =Â tempfile.mkdtemp() |
---|
4534 | Â Â Â Â ucur_dir_filename1 =Â ucur_dir +Â os.sep +'uc19940524.000' |
---|
4535 | Â Â Â Â ucur_dir_filename2 =Â ucur_dir +Â os.sep +'uc19940524.001' |
---|
4536 | |
---|
4537 |     fid = open(ucur_dir_filename1, 'w') |
---|
4538 |     fid.write(""" ncols       3 |
---|
4539 |  nrows       2 |
---|
4540 |  xllcorner  148.00000 |
---|
4541 |  yllcorner  -38.00000 |
---|
4542 |  cellsize    0.25 |
---|
4543 |  nodata_value  -9999.0 |
---|
4544 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4545 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4546 | """) |
---|
4547 | Â Â Â Â fid.close() |
---|
4548 |     fid = open(ucur_dir_filename2, 'w') |
---|
4549 |     fid.write(""" ncols       3 |
---|
4550 |  nrows       2 |
---|
4551 |  xllcorner  148.00000 |
---|
4552 |  yllcorner  -38.00000 |
---|
4553 |  cellsize    0.25 |
---|
4554 |  nodata_value  -9999.0 |
---|
4555 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4556 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4557 | """) |
---|
4558 | Â Â Â Â fid.close() |
---|
4559 | |
---|
4560 | Â Â Â Â vcur_dir =Â tempfile.mkdtemp() |
---|
4561 | Â Â Â Â vcur_dir_filename1 =Â vcur_dir +Â os.sep +'vc19940524.000' |
---|
4562 | Â Â Â Â vcur_dir_filename2 =Â vcur_dir +Â os.sep +'vc19940524.001' |
---|
4563 | |
---|
4564 |     fid = open(vcur_dir_filename1, 'w') |
---|
4565 |     fid.write(""" ncols       3 |
---|
4566 |  nrows       2 |
---|
4567 |  xllcorner  148.00000 |
---|
4568 |  yllcorner  -38.00000 |
---|
4569 |  cellsize    0.25 |
---|
4570 |  nodata_value  -9999.0 |
---|
4571 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4572 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4573 | """) |
---|
4574 | Â Â Â Â fid.close() |
---|
4575 |     fid = open(vcur_dir_filename2, 'w') |
---|
4576 |     fid.write(""" ncols       3 |
---|
4577 |  nrows       2 |
---|
4578 |  xllcorner  148.00000 |
---|
4579 |  yllcorner  -38.00000 |
---|
4580 |  cellsize    0.25 |
---|
4581 |  nodata_value  -9999.0 |
---|
4582 | Â Â 90.000Â Â 60.000Â Â 30.0 |
---|
4583 | Â Â 10.000Â Â 10.000Â Â 10.000 |
---|
4584 | """) |
---|
4585 | Â Â Â Â fid.close() |
---|
4586 | |
---|
4587 | Â Â Â Â sww_file =Â 'a_test.sww' |
---|
4588 |     asc_csiro2sww(bath_dir,elevation_dir, ucur_dir, vcur_dir, |
---|
4589 |            sww_file, fail_on_NaN = False, elevation_NaN_filler = 0, |
---|
4590 | Â Â Â Â Â Â Â Â Â Â Â mean_stage =Â 100, |
---|
4591 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
4592 | |
---|
4593 | Â Â Â Â # check the sww file |
---|
4594 | |
---|
4595 |     fid = NetCDFFile(sww_file, 'r')  #Open existing file for read |
---|
4596 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
4597 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
4598 | Â Â Â Â z =Â fid.variables['z'][:] |
---|
4599 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
4600 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
4601 | Â Â Â Â geo_ref =Â Geo_reference(NetCDFObject=fid) |
---|
4602 | Â Â Â Â #print "geo_ref",geo_ref |
---|
4603 | Â Â Â Â x_ref =Â geo_ref.get_xllcorner() |
---|
4604 | Â Â Â Â y_ref =Â geo_ref.get_yllcorner() |
---|
4605 |     self.failUnless(geo_ref.get_zone() == 55, 'Failed') |
---|
4606 |     assert allclose(x_ref, 587798.418) # (-38, 148) |
---|
4607 |     assert allclose(y_ref, 5793123.477)# (-38, 148.5) |
---|
4608 | |
---|
4609 | Â Â Â Â #Zone:Â Â 55 |
---|
4610 | Â Â Â Â #Easting:Â 588095.674Â Northing: 5821451.722 |
---|
4611 | Â Â Â Â #Latitude:Â Â -37Â 45 ' 0.00000 ''Â Longitude: 148 0 ' 0.00000 '' |
---|
4612 |     assert allclose((x[0],y[0]), (588095.674 - x_ref, 5821451.722 - y_ref)) |
---|
4613 | |
---|
4614 | Â Â Â Â #Zone:Â Â 55 |
---|
4615 | Â Â Â Â #Easting:Â 632145.632Â Northing: 5820863.269 |
---|
4616 | Â Â Â Â #Latitude:Â Â -37Â 45 ' 0.00000 ''Â Longitude: 148Â 30 ' 0.00000 '' |
---|
4617 |     assert allclose((x[2],y[2]), (632145.632 - x_ref, 5820863.269 - y_ref)) |
---|
4618 | |
---|
4619 | Â Â Â Â #Zone:Â Â 55 |
---|
4620 | Â Â Â Â #Easting:Â 609748.788Â Northing: 5793447.860 |
---|
4621 | Â Â Â Â #Latitude:Â Â -38Â 0 ' 0.00000 ''Â Longitude: 148Â 15 ' 0.00000 '' |
---|
4622 |     assert allclose((x[4],y[4]), (609748.788 - x_ref, 5793447.86 - y_ref)) |
---|
4623 | |
---|
4624 |     assert allclose(z[0],9000.0 ) |
---|
4625 |     assert allclose(stage[0][4],100.0 ) |
---|
4626 |     assert allclose(stage[0][5],100.0 ) |
---|
4627 | |
---|
4628 | Â Â Â Â #(100.0 - 9000)*10 |
---|
4629 |     assert allclose(xmomentum[0][4], -89000.0 ) |
---|
4630 | |
---|
4631 | Â Â Â Â #(100.0 - -1000.000)*10 |
---|
4632 |     assert allclose(xmomentum[0][5], 11000.0 ) |
---|
4633 | |
---|
4634 | Â Â Â Â fid.close() |
---|
4635 | |
---|
4636 | Â Â Â Â #tidy up |
---|
4637 | Â Â Â Â os.remove(bath_dir_filename) |
---|
4638 | Â Â Â Â os.rmdir(bath_dir) |
---|
4639 | |
---|
4640 | Â Â Â Â os.remove(elevation_dir_filename1) |
---|
4641 | Â Â Â Â os.remove(elevation_dir_filename2) |
---|
4642 | Â Â Â Â os.rmdir(elevation_dir) |
---|
4643 | |
---|
4644 | Â Â Â Â os.remove(ucur_dir_filename1) |
---|
4645 | Â Â Â Â os.remove(ucur_dir_filename2) |
---|
4646 | Â Â Â Â os.rmdir(ucur_dir) |
---|
4647 | |
---|
4648 | Â Â Â Â os.remove(vcur_dir_filename1) |
---|
4649 | Â Â Â Â os.remove(vcur_dir_filename2) |
---|
4650 | Â Â Â Â os.rmdir(vcur_dir) |
---|
4651 | |
---|
4652 | Â Â Â Â # remove sww file |
---|
4653 | Â Â Â Â os.remove(sww_file) |
---|
4654 | |
---|
4655 | |
---|
4656 |   def test_asc_csiro2sww4(self): |
---|
4657 | Â Â Â Â """ |
---|
4658 | Â Â Â Â Test specifying the extent |
---|
4659 | Â Â Â Â """ |
---|
4660 | |
---|
4661 |     import tempfile |
---|
4662 | |
---|
4663 | Â Â Â Â bath_dir =Â tempfile.mkdtemp() |
---|
4664 | Â Â Â Â bath_dir_filename =Â bath_dir +Â os.sep +'ba19940524.000' |
---|
4665 | Â Â Â Â #bath_dir = 'bath_data_manager_test' |
---|
4666 | Â Â Â Â #print "os.getcwd( )",os.getcwd( ) |
---|
4667 | Â Â Â Â elevation_dir =Â tempfile.mkdtemp() |
---|
4668 | Â Â Â Â #elevation_dir = 'elev_expanded' |
---|
4669 | Â Â Â Â elevation_dir_filename1 =Â elevation_dir +Â os.sep +'el19940524.000' |
---|
4670 | Â Â Â Â elevation_dir_filename2 =Â elevation_dir +Â os.sep +'el19940524.001' |
---|
4671 | |
---|
4672 |     fid = open(bath_dir_filename, 'w') |
---|
4673 |     fid.write(""" ncols       4 |
---|
4674 |  nrows       4 |
---|
4675 |  xllcorner  148.00000 |
---|
4676 |  yllcorner  -38.00000 |
---|
4677 |  cellsize    0.25 |
---|
4678 |  nodata_value  -9999.0 |
---|
4679 | Â Â -9000.000Â Â -1000.000Â Â -3000.0 -2000.000 |
---|
4680 | Â Â -1000.000Â Â 9000.000Â -1000.000 -3000.000 |
---|
4681 | Â Â -4000.000Â Â 6000.000Â Â 2000.000 -5000.000 |
---|
4682 | Â Â -9000.000Â Â -1000.000Â Â -3000.0 -2000.000 |
---|
4683 | """) |
---|
4684 | Â Â Â Â fid.close() |
---|
4685 | |
---|
4686 |     fid = open(elevation_dir_filename1, 'w') |
---|
4687 |     fid.write(""" ncols       4 |
---|
4688 |  nrows       4 |
---|
4689 |  xllcorner  148.00000 |
---|
4690 |  yllcorner  -38.00000 |
---|
4691 |  cellsize    0.25 |
---|
4692 |  nodata_value  -9999.0 |
---|
4693 | Â Â -900.000Â Â -100.000Â Â -300.0 -200.000 |
---|
4694 | Â Â -100.000Â Â 900.000Â -100.000 -300.000 |
---|
4695 | Â Â -400.000Â Â 600.000Â Â 200.000 -500.000 |
---|
4696 | Â Â -900.000Â Â -100.000Â Â -300.0 -200.000 |
---|
4697 | """) |
---|
4698 | Â Â Â Â fid.close() |
---|
4699 | |
---|
4700 |     fid = open(elevation_dir_filename2, 'w') |
---|
4701 |     fid.write(""" ncols       4 |
---|
4702 |  nrows       4 |
---|
4703 |  xllcorner  148.00000 |
---|
4704 |  yllcorner  -38.00000 |
---|
4705 |  cellsize    0.25 |
---|
4706 |  nodata_value  -9999.0 |
---|
4707 | Â Â -990.000Â Â -110.000Â Â -330.0 -220.000 |
---|
4708 | Â Â -110.000Â Â 990.000Â -110.000 -330.000 |
---|
4709 | Â Â -440.000Â Â 660.000Â Â 220.000 -550.000 |
---|
4710 | Â Â -990.000Â Â -110.000Â Â -330.0 -220.000 |
---|
4711 | """) |
---|
4712 | Â Â Â Â fid.close() |
---|
4713 | |
---|
4714 | Â Â Â Â ucur_dir =Â tempfile.mkdtemp() |
---|
4715 | Â Â Â Â ucur_dir_filename1 =Â ucur_dir +Â os.sep +'uc19940524.000' |
---|
4716 | Â Â Â Â ucur_dir_filename2 =Â ucur_dir +Â os.sep +'uc19940524.001' |
---|
4717 | |
---|
4718 |     fid = open(ucur_dir_filename1, 'w') |
---|
4719 |     fid.write(""" ncols       4 |
---|
4720 |  nrows       4 |
---|
4721 |  xllcorner  148.00000 |
---|
4722 |  yllcorner  -38.00000 |
---|
4723 |  cellsize    0.25 |
---|
4724 |  nodata_value  -9999.0 |
---|
4725 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4726 | Â Â -10.000Â Â 90.000Â -10.000 -30.000 |
---|
4727 | Â Â -40.000Â Â 60.000Â Â 20.000 -50.000 |
---|
4728 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4729 | """) |
---|
4730 | Â Â Â Â fid.close() |
---|
4731 |     fid = open(ucur_dir_filename2, 'w') |
---|
4732 |     fid.write(""" ncols       4 |
---|
4733 |  nrows       4 |
---|
4734 |  xllcorner  148.00000 |
---|
4735 |  yllcorner  -38.00000 |
---|
4736 |  cellsize    0.25 |
---|
4737 |  nodata_value  -9999.0 |
---|
4738 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4739 | Â Â -10.000Â Â 99.000Â -11.000 -30.000 |
---|
4740 | Â Â -40.000Â Â 66.000Â Â 22.000 -50.000 |
---|
4741 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4742 | """) |
---|
4743 | Â Â Â Â fid.close() |
---|
4744 | |
---|
4745 | Â Â Â Â vcur_dir =Â tempfile.mkdtemp() |
---|
4746 | Â Â Â Â vcur_dir_filename1 =Â vcur_dir +Â os.sep +'vc19940524.000' |
---|
4747 | Â Â Â Â vcur_dir_filename2 =Â vcur_dir +Â os.sep +'vc19940524.001' |
---|
4748 | |
---|
4749 |     fid = open(vcur_dir_filename1, 'w') |
---|
4750 |     fid.write(""" ncols       4 |
---|
4751 |  nrows       4 |
---|
4752 |  xllcorner  148.00000 |
---|
4753 |  yllcorner  -38.00000 |
---|
4754 |  cellsize    0.25 |
---|
4755 |  nodata_value  -9999.0 |
---|
4756 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4757 | Â Â -10.000Â Â 80.000Â -20.000 -30.000 |
---|
4758 | Â Â -40.000Â Â 50.000Â Â 10.000 -50.000 |
---|
4759 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4760 | """) |
---|
4761 | Â Â Â Â fid.close() |
---|
4762 |     fid = open(vcur_dir_filename2, 'w') |
---|
4763 |     fid.write(""" ncols       4 |
---|
4764 |  nrows       4 |
---|
4765 |  xllcorner  148.00000 |
---|
4766 |  yllcorner  -38.00000 |
---|
4767 |  cellsize    0.25 |
---|
4768 |  nodata_value  -9999.0 |
---|
4769 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4770 | Â Â -10.000Â Â 88.000Â -22.000 -30.000 |
---|
4771 | Â Â -40.000Â Â 55.000Â Â 11.000 -50.000 |
---|
4772 | Â Â -90.000Â Â -10.000Â Â -30.0 -20.000 |
---|
4773 | """) |
---|
4774 | Â Â Â Â fid.close() |
---|
4775 | |
---|
4776 | Â Â Â Â sww_file =Â tempfile.mktemp(".sww") |
---|
4777 | Â Â Â Â #sww_file = 'a_test.sww' |
---|
4778 |     asc_csiro2sww(bath_dir,elevation_dir, ucur_dir, vcur_dir, |
---|
4779 |            sww_file, fail_on_NaN = False, elevation_NaN_filler = 0, |
---|
4780 | Â Â Â Â Â Â Â Â Â Â Â mean_stage =Â 100, |
---|
4781 |             minlat = -37.6, maxlat = -37.6, |
---|
4782 |          minlon = 148.3, maxlon = 148.3, |
---|
4783 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose |
---|
4784 | Â Â Â Â Â Â Â Â Â Â Â #,verbose = True |
---|
4785 | Â Â Â Â Â Â Â Â Â Â Â ) |
---|
4786 | |
---|
4787 | Â Â Â Â # check the sww file |
---|
4788 | |
---|
4789 |     fid = NetCDFFile(sww_file, 'r')  #Open existing file for read |
---|
4790 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
4791 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
4792 | Â Â Â Â z =Â fid.variables['z'][:] |
---|
4793 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
4794 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
4795 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:] |
---|
4796 | Â Â Â Â geo_ref =Â Geo_reference(NetCDFObject=fid) |
---|
4797 | Â Â Â Â #print "geo_ref",geo_ref |
---|
4798 | Â Â Â Â x_ref =Â geo_ref.get_xllcorner() |
---|
4799 | Â Â Â Â y_ref =Â geo_ref.get_yllcorner() |
---|
4800 |     self.failUnless(geo_ref.get_zone() == 55, 'Failed') |
---|
4801 | |
---|
4802 |     assert allclose(fid.starttime, 0.0) # (-37.45, 148.25) |
---|
4803 |     assert allclose(x_ref, 610120.388) # (-37.45, 148.25) |
---|
4804 |     assert allclose(y_ref, 5820863.269 )# (-37.45, 148.5) |
---|
4805 | |
---|
4806 | Â Â Â Â #Easting:Â 632145.632Â Northing: 5820863.269 |
---|
4807 | Â Â Â Â #Latitude:Â Â -37 45 ' 0.00000 ''Â Longitude: 148Â 30 ' 0.00000 '' |
---|
4808 | |
---|
4809 | Â Â Â Â #print "x",x |
---|
4810 | Â Â Â Â #print "y",y |
---|
4811 | Â Â Â Â self.failUnless(len(x)Â ==Â 4,'failed')Â # 2*2 |
---|
4812 | Â Â Â Â self.failUnless(len(x)Â ==Â 4,'failed')Â # 2*2 |
---|
4813 | |
---|
4814 | Â Â Â Â #Zone:Â Â 55 |
---|
4815 | Â Â Â Â #Easting:Â 632145.632Â Northing: 5820863.269 |
---|
4816 | Â Â Â Â #Latitude:Â Â -37 45 ' 0.00000 ''Â Longitude: 148Â 30 ' 0.00000 '' |
---|
4817 | Â Â Â Â # magic number - y is close enough for me. |
---|
4818 |     assert allclose(x[3], 632145.63 - x_ref) |
---|
4819 |     assert allclose(y[3], 5820863.269 - y_ref + 5.22155314684e-005) |
---|
4820 | |
---|
4821 |     assert allclose(z[0],9000.0 ) #z is elevation info |
---|
4822 | Â Â Â Â #print "z",z |
---|
4823 | Â Â Â Â # 2 time steps, 4 points |
---|
4824 |     self.failUnless(xmomentum.shape == (2,4), 'failed') |
---|
4825 |     self.failUnless(ymomentum.shape == (2,4), 'failed') |
---|
4826 | |
---|
4827 | Â Â Â Â #(100.0 - -1000.000)*10 |
---|
4828 | Â Â Â Â #assert allclose(xmomentum[0][5], 11000.0 ) |
---|
4829 | |
---|
4830 | Â Â Â Â fid.close() |
---|
4831 | |
---|
4832 | Â Â Â Â # is the sww file readable? |
---|
4833 | Â Â Â Â #Lets see if we can convert it to a dem! |
---|
4834 | Â Â Â Â # if you uncomment, remember to delete the file |
---|
4835 | Â Â Â Â #print "sww_file",sww_file |
---|
4836 | Â Â Â Â #dem_file = tempfile.mktemp(".dem") |
---|
4837 | Â Â Â Â domain =Â sww2domain(sww_file)Â ###, dem_file) |
---|
4838 | Â Â Â Â domain.check_integrity() |
---|
4839 | |
---|
4840 | Â Â Â Â #tidy up |
---|
4841 | Â Â Â Â os.remove(bath_dir_filename) |
---|
4842 | Â Â Â Â os.rmdir(bath_dir) |
---|
4843 | |
---|
4844 | Â Â Â Â os.remove(elevation_dir_filename1) |
---|
4845 | Â Â Â Â os.remove(elevation_dir_filename2) |
---|
4846 | Â Â Â Â os.rmdir(elevation_dir) |
---|
4847 | |
---|
4848 | Â Â Â Â os.remove(ucur_dir_filename1) |
---|
4849 | Â Â Â Â os.remove(ucur_dir_filename2) |
---|
4850 | Â Â Â Â os.rmdir(ucur_dir) |
---|
4851 | |
---|
4852 | Â Â Â Â os.remove(vcur_dir_filename1) |
---|
4853 | Â Â Â Â os.remove(vcur_dir_filename2) |
---|
4854 | Â Â Â Â os.rmdir(vcur_dir) |
---|
4855 | |
---|
4856 | |
---|
4857 | |
---|
4858 | |
---|
4859 | Â Â Â Â # remove sww file |
---|
4860 | Â Â Â Â os.remove(sww_file) |
---|
4861 | |
---|
4862 | |
---|
4863 |   def test_get_min_max_indexes(self): |
---|
4864 | Â Â Â Â latitudes =Â [3,2,1,0] |
---|
4865 | Â Â Â Â longitudes =Â [0,10,20,30] |
---|
4866 | |
---|
4867 | Â Â Â Â # k - lat |
---|
4868 | Â Â Â Â # l - lon |
---|
4869 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4870 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4871 | Â Â Â Â Â Â -10,4,-10,31) |
---|
4872 | |
---|
4873 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4874 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4875 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4876 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4877 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4878 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4879 |     self.failUnless(latitudes == latitudes_new and \ |
---|
4880 | Â Â Â Â Â Â Â Â Â Â Â Â longitudes ==Â longitudes_news, |
---|
4881 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4882 | |
---|
4883 | Â Â Â Â ## 2nd test |
---|
4884 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4885 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4886 | Â Â Â Â Â Â 0.5,2.5,5,25) |
---|
4887 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4888 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4889 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4890 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4891 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4892 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4893 | |
---|
4894 |     self.failUnless(latitudes == latitudes_new and \ |
---|
4895 | Â Â Â Â Â Â Â Â Â Â Â Â longitudes ==Â longitudes_news, |
---|
4896 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4897 | |
---|
4898 | Â Â Â Â ## 3rd test |
---|
4899 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes(\ |
---|
4900 | Â Â Â Â Â Â latitudes, |
---|
4901 | Â Â Â Â Â Â longitudes, |
---|
4902 | Â Â Â Â Â Â 1.1,1.9,12,17) |
---|
4903 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4904 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4905 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4906 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4907 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4908 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4909 | |
---|
4910 |     self.failUnless(latitudes_new == [2, 1] and \ |
---|
4911 |             longitudes_news == [10, 20], |
---|
4912 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4913 | |
---|
4914 | |
---|
4915 | Â Â Â Â ## 4th test |
---|
4916 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4917 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4918 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â -0.1,1.9,-2,17) |
---|
4919 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4920 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4921 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4922 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4923 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4924 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4925 | |
---|
4926 |     self.failUnless(latitudes_new == [2, 1, 0] and \ |
---|
4927 |             longitudes_news == [0, 10, 20], |
---|
4928 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4929 | Â Â Â Â ## 5th test |
---|
4930 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4931 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4932 | Â Â Â Â Â Â 0.1,1.9,2,17) |
---|
4933 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4934 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4935 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4936 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4937 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4938 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4939 | |
---|
4940 |     self.failUnless(latitudes_new == [2, 1, 0] and \ |
---|
4941 |             longitudes_news == [0, 10, 20], |
---|
4942 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4943 | |
---|
4944 | Â Â Â Â ## 6th test |
---|
4945 | |
---|
4946 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4947 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4948 | Â Â Â Â Â Â 1.5,4,18,32) |
---|
4949 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4950 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4951 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4952 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4953 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4954 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4955 | |
---|
4956 |     self.failUnless(latitudes_new == [3, 2, 1] and \ |
---|
4957 |             longitudes_news == [10, 20, 30], |
---|
4958 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4959 | |
---|
4960 | |
---|
4961 | Â Â Â Â ## 7th test |
---|
4962 | Â Â Â Â m2d =Â array([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) |
---|
4963 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4964 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4965 | Â Â Â Â Â Â 1.5,1.5,15,15) |
---|
4966 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4967 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4968 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4969 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4970 | Â Â Â Â m2d =Â m2d[kmin:kmax,lmin:lmax] |
---|
4971 | Â Â Â Â #print "m2d", m2d |
---|
4972 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4973 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4974 | |
---|
4975 |     self.failUnless(latitudes_new == [2, 1] and \ |
---|
4976 |             longitudes_news == [10, 20], |
---|
4977 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4978 | |
---|
4979 | Â Â Â Â self.failUnless(m2d ==Â [[5,6],[9,10]], |
---|
4980 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
4981 | |
---|
4982 |   def test_get_min_max_indexes_lat_ascending(self): |
---|
4983 | Â Â Â Â latitudes =Â [0,1,2,3] |
---|
4984 | Â Â Â Â longitudes =Â [0,10,20,30] |
---|
4985 | |
---|
4986 | Â Â Â Â # k - lat |
---|
4987 | Â Â Â Â # l - lon |
---|
4988 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
4989 | Â Â Â Â Â Â latitudes,longitudes, |
---|
4990 | Â Â Â Â Â Â -10,4,-10,31) |
---|
4991 | |
---|
4992 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
4993 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
4994 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
4995 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
4996 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
4997 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
4998 |     self.failUnless(latitudes == latitudes_new and \ |
---|
4999 | Â Â Â Â Â Â Â Â Â Â Â Â longitudes ==Â longitudes_news, |
---|
5000 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
5001 | |
---|
5002 | Â Â Â Â ## 3rd test |
---|
5003 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes(\ |
---|
5004 | Â Â Â Â Â Â latitudes, |
---|
5005 | Â Â Â Â Â Â longitudes, |
---|
5006 | Â Â Â Â Â Â 1.1,1.9,12,17) |
---|
5007 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
5008 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
5009 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
5010 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
5011 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
5012 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
5013 | |
---|
5014 |     self.failUnless(latitudes_new == [1, 2] and \ |
---|
5015 |             longitudes_news == [10, 20], |
---|
5016 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
5017 | |
---|
5018 |   def test_get_min_max_indexes2(self): |
---|
5019 | Â Â Â Â latitudes =Â [-30,-35,-40,-45] |
---|
5020 | Â Â Â Â longitudes =Â [148,149,150,151] |
---|
5021 | |
---|
5022 | Â Â Â Â m2d =Â array([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) |
---|
5023 | |
---|
5024 | Â Â Â Â # k - lat |
---|
5025 | Â Â Â Â # l - lon |
---|
5026 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
5027 | Â Â Â Â Â Â latitudes,longitudes, |
---|
5028 | Â Â Â Â Â Â -37,-27,147,149.5) |
---|
5029 | |
---|
5030 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
5031 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
5032 | Â Â Â Â #print "m2d", m2d |
---|
5033 | Â Â Â Â #print "latitudes", latitudes |
---|
5034 | Â Â Â Â #print "longitudes",longitudes |
---|
5035 | Â Â Â Â #print "latitudes[kmax]", latitudes[kmax] |
---|
5036 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
5037 | Â Â Â Â longitudes_new =Â longitudes[lmin:lmax] |
---|
5038 | Â Â Â Â m2d =Â m2d[kmin:kmax,lmin:lmax] |
---|
5039 | Â Â Â Â #print "m2d", m2d |
---|
5040 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
5041 | Â Â Â Â #print "longitudes_new",longitudes_new |
---|
5042 | |
---|
5043 |     self.failUnless(latitudes_new == [-30, -35, -40] and \ |
---|
5044 |             longitudes_new == [148, 149,150], |
---|
5045 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
5046 | Â Â Â Â self.failUnless(m2d ==Â [[0,1,2],[4,5,6],[8,9,10]], |
---|
5047 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
5048 | |
---|
5049 |   def test_get_min_max_indexes3(self): |
---|
5050 | Â Â Â Â latitudes =Â [-30,-35,-40,-45,-50,-55,-60] |
---|
5051 | Â Â Â Â longitudes =Â [148,149,150,151] |
---|
5052 | |
---|
5053 | Â Â Â Â # k - lat |
---|
5054 | Â Â Â Â # l - lon |
---|
5055 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
5056 | Â Â Â Â Â Â latitudes,longitudes, |
---|
5057 | Â Â Â Â Â Â -43,-37,148.5,149.5) |
---|
5058 | |
---|
5059 | |
---|
5060 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
5061 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
5062 | Â Â Â Â #print "latitudes", latitudes |
---|
5063 | Â Â Â Â #print "longitudes",longitudes |
---|
5064 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
5065 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
5066 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
5067 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
5068 | |
---|
5069 |     self.failUnless(latitudes_new == [-35, -40, -45] and \ |
---|
5070 |             longitudes_news == [148, 149,150], |
---|
5071 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
5072 | |
---|
5073 |   def test_get_min_max_indexes4(self): |
---|
5074 | Â Â Â Â latitudes =Â [-30,-35,-40,-45,-50,-55,-60] |
---|
5075 | Â Â Â Â longitudes =Â [148,149,150,151] |
---|
5076 | |
---|
5077 | Â Â Â Â # k - lat |
---|
5078 | Â Â Â Â # l - lon |
---|
5079 |     kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes( |
---|
5080 | Â Â Â Â Â Â latitudes,longitudes) |
---|
5081 | |
---|
5082 | Â Â Â Â #print "kmin",kmin;print "kmax",kmax |
---|
5083 | Â Â Â Â #print "lmin",lmin;print "lmax",lmax |
---|
5084 | Â Â Â Â #print "latitudes", latitudes |
---|
5085 | Â Â Â Â #print "longitudes",longitudes |
---|
5086 | Â Â Â Â latitudes_new =Â latitudes[kmin:kmax] |
---|
5087 | Â Â Â Â longitudes_news =Â longitudes[lmin:lmax] |
---|
5088 | Â Â Â Â #print "latitudes_new", latitudes_new |
---|
5089 | Â Â Â Â #print "longitudes_news",longitudes_news |
---|
5090 | |
---|
5091 |     self.failUnless(latitudes_new == latitudes and \ |
---|
5092 | Â Â Â Â Â Â Â Â Â Â Â Â longitudes_news ==Â longitudes, |
---|
5093 | Â Â Â Â Â Â Â Â Â Â Â Â Â 'failed') |
---|
5094 | |
---|
5095 |   def test_tsh2sww(self): |
---|
5096 |     import os |
---|
5097 |     import tempfile |
---|
5098 | |
---|
5099 | Â Â Â Â tsh_file =Â tempfile.mktemp(".tsh") |
---|
5100 |     file = open(tsh_file,"w") |
---|
5101 |     file.write("4 3 # <vertex #> <x> <y> [attributes]\n \ |
---|
5102 | 0 0.0 0.0 0.0 0.0 0.01 \n \ |
---|
5103 | 1 1.0 0.0 10.0 10.0 0.02 \n \ |
---|
5104 | 2 0.0 1.0 0.0 10.0 0.03 \n \ |
---|
5105 | 3 0.5 0.25 8.0 12.0 0.04 \n \ |
---|
5106 | # Vert att title \n \ |
---|
5107 | elevation \n \ |
---|
5108 | stage \n \ |
---|
5109 | friction \n \ |
---|
5110 | 2 # <triangle #> [<vertex #>] [<neigbouring triangle #>]Â \n\ |
---|
5111 | 0 0 3 2 -1Â -1Â 1 dsg\n\ |
---|
5112 | 1 0 1 3 -1Â 0 -1Â Â ole nielsen\n\ |
---|
5113 | 4 # <segment #> <vertex #>Â <vertex #> [boundary tag] \n\ |
---|
5114 | 0 1 0 2 \n\ |
---|
5115 | 1 0 2 3 \n\ |
---|
5116 | 2 2 3 \n\ |
---|
5117 | 3 3 1 1 \n\ |
---|
5118 | 3 0 # <x> <y> [attributes] ...Mesh Vertices... \n \ |
---|
5119 | 0 216.0 -86.0 \n \ |
---|
5120 | 1 160.0 -167.0 \n \ |
---|
5121 | 2 114.0 -91.0 \n \ |
---|
5122 | 3 # <vertex #> <vertex #> [boundary tag] ...Mesh Segments... \n \ |
---|
5123 | 0 0 1 0 \n \ |
---|
5124 | 1 1 2 0 \n \ |
---|
5125 | 2 2 0 0 \n \ |
---|
5126 | 0 # <x> <y> ...Mesh Holes... \n \ |
---|
5127 | 0 # <x> <y> <attribute>...Mesh Regions... \n \ |
---|
5128 | 0 # <x> <y> <attribute>...Mesh Regions, area... \n\ |
---|
5129 | #Geo reference \n \ |
---|
5130 | 56 \n \ |
---|
5131 | 140 \n \ |
---|
5132 | 120 \n") |
---|
5133 | Â Â Â Â file.close() |
---|
5134 | |
---|
5135 | Â Â Â Â #sww_file = tempfile.mktemp(".sww") |
---|
5136 | Â Â Â Â #print "sww_file",sww_file |
---|
5137 | Â Â Â Â #print "sww_file",tsh_file |
---|
5138 | Â Â Â Â tsh2sww(tsh_file, |
---|
5139 | Â Â Â Â Â Â Â Â verbose=self.verbose) |
---|
5140 | |
---|
5141 | Â Â Â Â os.remove(tsh_file) |
---|
5142 | Â Â Â Â os.remove(tsh_file[:-4]Â +Â '.sww') |
---|
5143 | Â Â Â Â |
---|
5144 | |
---|
5145 | |
---|
5146 | |
---|
5147 | ########## testing nbed class ################## |
---|
5148 |   def test_exposure_csv_loading(self): |
---|
5149 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5150 |     file = open(file_name,"w") |
---|
5151 |     file.write("LATITUDE, LONGITUDE ,sound , speed \n\ |
---|
5152 | 115.0, -21.0, splat, 0.0\n\ |
---|
5153 | 114.0, -21.7, pow, 10.0\n\ |
---|
5154 | 114.5, -21.4, bang, 40.0\n") |
---|
5155 | Â Â Â Â file.close() |
---|
5156 |     exposure = Exposure_csv(file_name, title_check_list = ['speed','sound']) |
---|
5157 | Â Â Â Â exposure.get_column("sound") |
---|
5158 | Â Â Â Â |
---|
5159 | Â Â Â Â self.failUnless(exposure._attribute_dic['sound'][2]==' bang', |
---|
5160 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5161 | Â Â Â Â self.failUnless(exposure._attribute_dic['speed'][2]==' 40.0', |
---|
5162 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5163 | Â Â Â Â |
---|
5164 | Â Â Â Â os.remove(file_name) |
---|
5165 | Â Â Â Â |
---|
5166 |   def test_exposure_csv_loadingII(self): |
---|
5167 | Â Â Â Â |
---|
5168 | |
---|
5169 | Â Â Â Â file_name =Â tempfile.mktemp(".txt") |
---|
5170 |     file = open(file_name,"w") |
---|
5171 |     file.write("LATITUDE, LONGITUDE ,sound , speed \n\ |
---|
5172 | 115.0, -21.0, splat, 0.0\n\ |
---|
5173 | 114.0, -21.7, pow, 10.0\n\ |
---|
5174 | 114.5, -21.4, bang, 40.0\n") |
---|
5175 | Â Â Â Â file.close() |
---|
5176 | Â Â Â Â exposure =Â Exposure_csv(file_name) |
---|
5177 | Â Â Â Â exposure.get_column("sound") |
---|
5178 | Â Â Â Â |
---|
5179 | Â Â Â Â self.failUnless(exposure._attribute_dic['sound'][2]==' bang', |
---|
5180 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5181 | Â Â Â Â self.failUnless(exposure._attribute_dic['speed'][2]==' 40.0', |
---|
5182 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5183 | Â Â Â Â |
---|
5184 | Â Â Â Â os.remove(file_name) |
---|
5185 | Â Â Â Â |
---|
5186 |   def test_exposure_csv_loading_title_check_list(self): |
---|
5187 | |
---|
5188 | Â Â Â Â # I can't get cvs.reader to close the exposure file |
---|
5189 |     # The hacks below are to get around this.    |
---|
5190 |     if sys.platform == 'win32': |
---|
5191 | Â Â Â Â Â Â file_name =Â tempfile.gettempdir()Â +Â \ |
---|
5192 | Â Â Â Â Â Â Â Â Â Â "test_exposure_csv_loading_title_check_list.csv" |
---|
5193 | Â Â Â Â else: |
---|
5194 | Â Â Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5195 |     file = open(file_name,"w") |
---|
5196 |     file.write("LATITUDE, LONGITUDE ,sound , speed \n\ |
---|
5197 | 115.0, -21.0, splat, 0.0\n\ |
---|
5198 | 114.0, -21.7, pow, 10.0\n\ |
---|
5199 | 114.5, -21.4, bang, 40.0\n") |
---|
5200 | Â Â Â Â file.close() |
---|
5201 | Â Â Â Â try: |
---|
5202 |       exposure = Exposure_csv(file_name, title_check_list = ['SOUND']) |
---|
5203 |     except IOError: |
---|
5204 | Â Â Â Â Â Â pass |
---|
5205 | Â Â Â Â else: |
---|
5206 |       self.failUnless(0 ==1, 'Assertion not thrown error!') |
---|
5207 | Â Â Â Â Â Â |
---|
5208 |     if not sys.platform == 'win32': |
---|
5209 | Â Â Â Â Â Â os.remove(file_name) |
---|
5210 | Â Â Â Â |
---|
5211 |   def test_exposure_csv_cmp(self): |
---|
5212 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5213 |     file = open(file_name,"w") |
---|
5214 |     file.write("LATITUDE, LONGITUDE ,sound , speed \n\ |
---|
5215 | 115.0, -21.0, splat, 0.0\n\ |
---|
5216 | 114.0, -21.7, pow, 10.0\n\ |
---|
5217 | 114.5, -21.4, bang, 40.0\n") |
---|
5218 | Â Â Â Â file.close() |
---|
5219 | Â Â Â Â |
---|
5220 | Â Â Â Â e1 =Â Exposure_csv(file_name) |
---|
5221 | Â Â Â Â e2 =Â Exposure_csv(file_name) |
---|
5222 | Â Â Â Â os.remove(file_name) |
---|
5223 | |
---|
5224 | Â Â Â Â self.failUnless(cmp(e1,e2)==0, |
---|
5225 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5226 | Â Â Â Â |
---|
5227 | Â Â Â Â self.failUnless(cmp(e1,"hey")==1, |
---|
5228 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5229 | Â Â Â Â |
---|
5230 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5231 |     file = open(file_name,"w") |
---|
5232 | Â Â Â Â # Note, this has less spaces in the title, |
---|
5233 | Â Â Â Â # the instances will be the same. |
---|
5234 | Â Â Â Â file.write("LATITUDE,LONGITUDE ,sound, speed \n\ |
---|
5235 | 115.0, -21.0, splat, 0.0\n\ |
---|
5236 | 114.0, -21.7, pow, 10.0\n\ |
---|
5237 | 114.5, -21.4, bang, 40.0\n") |
---|
5238 | Â Â Â Â file.close() |
---|
5239 | Â Â Â Â e3 =Â Exposure_csv(file_name) |
---|
5240 | Â Â Â Â os.remove(file_name) |
---|
5241 | |
---|
5242 | Â Â Â Â self.failUnless(cmp(e3,e2)==0, |
---|
5243 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5244 | Â Â Â Â |
---|
5245 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5246 |     file = open(file_name,"w") |
---|
5247 | Â Â Â Â # Note, 40 changed to 44 . |
---|
5248 | Â Â Â Â file.write("LATITUDE,LONGITUDE ,sound, speed \n\ |
---|
5249 | 115.0, -21.0, splat, 0.0\n\ |
---|
5250 | 114.0, -21.7, pow, 10.0\n\ |
---|
5251 | 114.5, -21.4, bang, 44.0\n") |
---|
5252 | Â Â Â Â file.close() |
---|
5253 | Â Â Â Â e4 =Â Exposure_csv(file_name) |
---|
5254 | Â Â Â Â os.remove(file_name) |
---|
5255 | Â Â Â Â #print "e4",e4._attribute_dic |
---|
5256 | Â Â Â Â #print "e2",e2._attribute_dic |
---|
5257 | Â Â Â Â self.failUnless(cmp(e4,e2)<>0, |
---|
5258 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5259 | Â Â Â Â |
---|
5260 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5261 |     file = open(file_name,"w") |
---|
5262 | Â Â Â Â # Note, the first two columns are swapped. |
---|
5263 | Â Â Â Â file.write("LONGITUDE,LATITUDE ,sound, speed \n\ |
---|
5264 | Â -21.0,115.0, splat, 0.0\n\ |
---|
5265 | Â -21.7,114.0, pow, 10.0\n\ |
---|
5266 | Â -21.4,114.5, bang, 40.0\n") |
---|
5267 | Â Â Â Â file.close() |
---|
5268 | Â Â Â Â e5 =Â Exposure_csv(file_name) |
---|
5269 | Â Â Â Â os.remove(file_name) |
---|
5270 | |
---|
5271 | Â Â Â Â self.failUnless(cmp(e3,e5)<>0, |
---|
5272 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5273 | Â Â Â Â |
---|
5274 |   def test_exposure_csv_saving(self): |
---|
5275 | Â Â Â Â |
---|
5276 | |
---|
5277 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5278 |     file = open(file_name,"w") |
---|
5279 |     file.write("LATITUDE, LONGITUDE ,sound , speed \n\ |
---|
5280 | 115.0, -21.0, splat, 0.0\n\ |
---|
5281 | 114.0, -21.7, pow, 10.0\n\ |
---|
5282 | 114.5, -21.4, bang, 40.0\n") |
---|
5283 | Â Â Â Â file.close() |
---|
5284 | Â Â Â Â e1 =Â Exposure_csv(file_name) |
---|
5285 | Â Â Â Â |
---|
5286 | Â Â Â Â file_name2 =Â tempfile.mktemp(".csv") |
---|
5287 | Â Â Â Â e1.save(file_name =Â file_name2) |
---|
5288 | Â Â Â Â e2 =Â Exposure_csv(file_name2) |
---|
5289 | Â Â Â Â |
---|
5290 | Â Â Â Â self.failUnless(cmp(e1,e2)==0, |
---|
5291 | Â Â Â Â Â Â Â Â Â Â Â Â 'FAILED!') |
---|
5292 | Â Â Â Â os.remove(file_name) |
---|
5293 | Â Â Â Â os.remove(file_name2) |
---|
5294 | |
---|
5295 |   def test_exposure_csv_get_location(self): |
---|
5296 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5297 |     file = open(file_name,"w") |
---|
5298 |     file.write("LONGITUDE , LATITUDE, sound , speed \n\ |
---|
5299 | 150.916666667, -34.5, splat, 0.0\n\ |
---|
5300 | 150.0, -34.0, pow, 10.0\n") |
---|
5301 | Â Â Â Â file.close() |
---|
5302 | Â Â Â Â e1 =Â Exposure_csv(file_name) |
---|
5303 | |
---|
5304 | Â Â Â Â gsd =Â e1.get_location() |
---|
5305 | Â Â Â Â |
---|
5306 | Â Â Â Â points =Â gsd.get_data_points(absolute=True) |
---|
5307 | Â Â Â Â |
---|
5308 |     assert allclose(points[0][0], 308728.009) |
---|
5309 |     assert allclose(points[0][1], 6180432.601) |
---|
5310 |     assert allclose(points[1][0], 222908.705) |
---|
5311 |     assert allclose(points[1][1], 6233785.284) |
---|
5312 | Â Â Â Â self.failUnless(gsd.get_geo_reference().get_zone()Â ==Â 56, |
---|
5313 | Â Â Â Â Â Â Â Â Â Â Â Â 'Bad zone error!') |
---|
5314 | |
---|
5315 | Â Â Â Â os.remove(file_name) |
---|
5316 | Â Â Â Â |
---|
5317 |   def test_exposure_csv_set_column_get_column(self): |
---|
5318 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5319 |     file = open(file_name,"w") |
---|
5320 |     file.write("LONGITUDE , LATITUDE, sound , speed \n\ |
---|
5321 | 150.916666667, -34.5, splat, 0.0\n\ |
---|
5322 | 150.0, -34.0, pow, 10.0\n") |
---|
5323 | Â Â Â Â file.close() |
---|
5324 | Â Â Â Â e1 =Â Exposure_csv(file_name)Â Â Â |
---|
5325 | Â Â Â Â os.remove(file_name) |
---|
5326 | |
---|
5327 | Â Â Â Â new_title =Â "feast" |
---|
5328 | Â Â Â Â new_values =Â ["chicken","soup"] |
---|
5329 |     e1.set_column(new_title, new_values) |
---|
5330 | Â Â Â Â returned_values =Â e1.get_column(new_title) |
---|
5331 | Â Â Â Â self.failUnless(returned_values ==Â new_values, |
---|
5332 | Â Â Â Â Â Â Â Â Â Â Â Â ' Error!') |
---|
5333 | Â Â Â Â |
---|
5334 | Â Â Â Â file_name2 =Â tempfile.mktemp(".csv") |
---|
5335 | Â Â Â Â e1.save(file_name =Â file_name2) |
---|
5336 | Â Â Â Â e2 =Â Exposure_csv(file_name2) |
---|
5337 | Â Â Â Â returned_values =Â e2.get_column(new_title) |
---|
5338 | Â Â Â Â self.failUnless(returned_values ==Â new_values, |
---|
5339 | Â Â Â Â Â Â Â Â Â Â Â Â ' Error!')Â Â Â Â |
---|
5340 | Â Â Â Â os.remove(file_name2) |
---|
5341 | |
---|
5342 |   def test_exposure_csv_set_column_get_column_error_checking(self): |
---|
5343 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5344 |     file = open(file_name,"w") |
---|
5345 |     file.write("LONGITUDE , LATITUDE, sound , speed \n\ |
---|
5346 | 150.916666667, -34.5, splat, 0.0\n\ |
---|
5347 | 150.0, -34.0, pow, 10.0\n") |
---|
5348 | Â Â Â Â file.close() |
---|
5349 | Â Â Â Â e1 =Â Exposure_csv(file_name)Â Â Â |
---|
5350 | Â Â Â Â os.remove(file_name) |
---|
5351 | |
---|
5352 | Â Â Â Â new_title =Â "sound" |
---|
5353 | Â Â Â Â new_values =Â [12.5,7.6] |
---|
5354 | Â Â Â Â try: |
---|
5355 |       e1.set_column(new_title, new_values) |
---|
5356 |     except TitleValueError: |
---|
5357 | Â Â Â Â Â Â pass |
---|
5358 | Â Â Â Â else: |
---|
5359 |       self.failUnless(0 ==1, 'Error not thrown error!') |
---|
5360 | Â Â Â Â Â Â |
---|
5361 |     e1.set_column(new_title, new_values, overwrite=True) |
---|
5362 | Â Â Â Â returned_values =Â e1.get_column(new_title) |
---|
5363 | Â Â Â Â self.failUnless(returned_values ==Â new_values, |
---|
5364 | Â Â Â Â Â Â Â Â Â Â Â Â ' Error!')Â Â Â Â |
---|
5365 | Â Â Â Â |
---|
5366 | Â Â Â Â new2_title =Â "short list" |
---|
5367 | Â Â Â Â new2_values =Â [12.5] |
---|
5368 | Â Â Â Â try: |
---|
5369 |       e1.set_column(new2_title, new2_values) |
---|
5370 |     except DataMissingValuesError: |
---|
5371 | Â Â Â Â Â Â pass |
---|
5372 | Â Â Â Â else: |
---|
5373 |       self.failUnless(0 ==1, 'Error not thrown error!') |
---|
5374 | Â Â Â Â Â Â |
---|
5375 | Â Â Â Â new2_title =Â "long list" |
---|
5376 |     new2_values = [12.5, 7,8] |
---|
5377 | Â Â Â Â try: |
---|
5378 |       e1.set_column(new2_title, new2_values) |
---|
5379 |     except DataMissingValuesError: |
---|
5380 | Â Â Â Â Â Â pass |
---|
5381 | Â Â Â Â else: |
---|
5382 |       self.failUnless(0 ==1, 'Error not thrown error!') |
---|
5383 | Â Â Â Â file_name2 =Â tempfile.mktemp(".csv") |
---|
5384 | Â Â Â Â e1.save(file_name =Â file_name2) |
---|
5385 | Â Â Â Â e2 =Â Exposure_csv(file_name2) |
---|
5386 | Â Â Â Â returned_values =Â e2.get_column(new_title) |
---|
5387 |     for returned, new in map(None, returned_values, new_values): |
---|
5388 |       self.failUnless(returned == str(new), ' Error!') |
---|
5389 | Â Â Â Â #self.failUnless(returned_values == new_values, ' Error!')Â Â Â Â |
---|
5390 | Â Â Â Â os.remove(file_name2) |
---|
5391 | Â Â Â Â |
---|
5392 | Â Â Â Â try: |
---|
5393 | Â Â Â Â Â Â e1.get_column("toe jam") |
---|
5394 |     except TitleValueError: |
---|
5395 | Â Â Â Â Â Â pass |
---|
5396 | Â Â Â Â else: |
---|
5397 |       self.failUnless(0 ==1, 'Error not thrown error!') |
---|
5398 | Â Â Â Â Â Â |
---|
5399 |   def test_exposure_csv_loading_x_y(self): |
---|
5400 | Â Â Â Â |
---|
5401 | |
---|
5402 | Â Â Â Â file_name =Â tempfile.mktemp(".csv") |
---|
5403 |     file = open(file_name,"w") |
---|
5404 |     file.write("x, y ,sound , speed \n\ |
---|
5405 | 115.0, 7, splat, 0.0\n\ |
---|
5406 | 114.0, 8.0, pow, 10.0\n\ |
---|
5407 | 114.5, 9., bang, 40.0\n") |
---|
5408 | Â Â Â Â file.close() |
---|
5409 |     e1 = Exposure_csv(file_name, is_x_y_locations=True) |
---|
5410 | Â Â Â Â gsd =Â e1.get_location() |
---|
5411 | Â Â Â Â |
---|
5412 | Â Â Â Â points =Â gsd.get_data_points(absolute=True) |
---|
5413 | Â Â Â Â |
---|
5414 |     assert allclose(points[0][0], 115) |
---|
5415 |     assert allclose(points[0][1], 7) |
---|
5416 |     assert allclose(points[1][0], 114) |
---|
5417 |     assert allclose(points[1][1], 8) |
---|
5418 |     assert allclose(points[2][0], 114.5) |
---|
5419 |     assert allclose(points[2][1], 9) |
---|
5420 | Â Â Â Â self.failUnless(gsd.get_geo_reference().get_zone()Â ==Â -1, |
---|
5421 | Â Â Â Â Â Â Â Â Â Â Â Â 'Bad zone error!') |
---|
5422 | |
---|
5423 | Â Â Â Â os.remove(file_name) |
---|
5424 | |
---|
5425 | Â Â Â Â Â Â |
---|
5426 |   def test_exposure_csv_loading_x_y2(self): |
---|
5427 | Â Â Â Â |
---|
5428 | Â Â Â Â csv_file =Â tempfile.mktemp(".csv") |
---|
5429 | Â Â Â Â fd =Â open(csv_file,'wb') |
---|
5430 | Â Â Â Â writer =Â csv.writer(fd) |
---|
5431 |     writer.writerow(['x','y','STR_VALUE','C_VALUE','ROOF_TYPE','WALLS', 'SHORE_DIST']) |
---|
5432 | Â Â Â Â writer.writerow([5.5,0.5,'199770','130000','Metal','Timber',20]) |
---|
5433 | Â Â Â Â writer.writerow([4.5,1.0,'150000','76000','Metal','Double Brick',20]) |
---|
5434 | Â Â Â Â writer.writerow([4.5,1.5,'150000','76000','Metal','Brick Veneer',20]) |
---|
5435 | Â Â Â Â fd.close() |
---|
5436 | |
---|
5437 | Â Â Â Â e1 =Â Exposure_csv(csv_file) |
---|
5438 | Â Â Â Â gsd =Â e1.get_location() |
---|
5439 | Â Â Â Â |
---|
5440 | Â Â Â Â points =Â gsd.get_data_points(absolute=True) |
---|
5441 |     assert allclose(points[0][0], 5.5) |
---|
5442 |     assert allclose(points[0][1], 0.5) |
---|
5443 |     assert allclose(points[1][0], 4.5) |
---|
5444 |     assert allclose(points[1][1], 1.0) |
---|
5445 |     assert allclose(points[2][0], 4.5) |
---|
5446 |     assert allclose(points[2][1], 1.5) |
---|
5447 | Â Â Â Â self.failUnless(gsd.get_geo_reference().get_zone()Â ==Â -1, |
---|
5448 | Â Â Â Â Â Â Â Â Â Â Â Â 'Bad zone error!') |
---|
5449 | |
---|
5450 | Â Â Â Â os.remove(csv_file) |
---|
5451 | |
---|
5452 | Â Â #### TESTS FOR URS 2 SWWÂ ###Â Â Â |
---|
5453 | Â Â |
---|
5454 |   def create_mux(self, points_num=None): |
---|
5455 | Â Â Â Â # write all the mux stuff. |
---|
5456 | Â Â Â Â time_step_count =Â 3 |
---|
5457 | Â Â Â Â time_step =Â 0.5 |
---|
5458 | Â Â Â Â |
---|
5459 |     longitudes = [150.66667, 150.83334, 151., 151.16667] |
---|
5460 |     latitudes = [-34.5, -34.33333, -34.16667, -34] |
---|
5461 | |
---|
5462 |     if points_num == None: |
---|
5463 | Â Â Â Â Â Â points_num =Â len(longitudes)Â *Â len(latitudes) |
---|
5464 | |
---|
5465 | Â Â Â Â lonlatdeps =Â [] |
---|
5466 | Â Â Â Â quantities =Â ['HA','UA','VA'] |
---|
5467 | Â Â Â Â mux_names =Â [WAVEHEIGHT_MUX_LABEL, |
---|
5468 | Â Â Â Â Â Â Â Â Â Â Â EAST_VELOCITY_LABEL, |
---|
5469 | Â Â Â Â Â Â Â Â Â Â Â NORTH_VELOCITY_LABEL] |
---|
5470 | Â Â Â Â quantities_init =Â [[],[],[]] |
---|
5471 | Â Â Â Â # urs binary is latitude fastest |
---|
5472 |     for i,lon in enumerate(longitudes): |
---|
5473 |       for j,lat in enumerate(latitudes): |
---|
5474 |         _ , e, n = redfearn(lat, lon) |
---|
5475 |         lonlatdeps.append([lon, lat, n]) |
---|
5476 | Â Â Â Â Â Â Â Â quantities_init[0].append(e)Â # HA |
---|
5477 | Â Â Â Â Â Â Â Â quantities_init[1].append(n )Â # UA |
---|
5478 | Â Â Â Â Â Â Â Â quantities_init[2].append(e)Â # VA |
---|
5479 | Â Â Â Â #print "lonlatdeps",lonlatdeps |
---|
5480 | |
---|
5481 |     file_handle, base_name = tempfile.mkstemp("") |
---|
5482 | Â Â Â Â os.close(file_handle) |
---|
5483 | Â Â Â Â os.remove(base_name) |
---|
5484 | Â Â Â Â |
---|
5485 | Â Â Â Â files =Â []Â Â Â Â |
---|
5486 |     for i,q in enumerate(quantities): |
---|
5487 | Â Â Â Â Â Â quantities_init[i]Â =Â ensure_numeric(quantities_init[i]) |
---|
5488 | Â Â Â Â Â Â #print "HA_init", HA_init |
---|
5489 |       q_time = zeros((time_step_count, points_num), Float) |
---|
5490 |       for time in range(time_step_count): |
---|
5491 | Â Â Â Â Â Â Â Â q_time[time,:]Â =Â quantities_init[i]Â #* time * 4 |
---|
5492 | Â Â Â Â Â Â |
---|
5493 | Â Â Â Â Â Â #Write C files |
---|
5494 | Â Â Â Â Â Â columns =Â 3Â # long, lat , depth |
---|
5495 |       file = base_name + mux_names[i] |
---|
5496 | Â Â Â Â Â Â #print "base_name file",file |
---|
5497 |       f = open(file, 'wb') |
---|
5498 | Â Â Â Â Â Â files.append(file) |
---|
5499 | Â Â Â Â Â Â f.write(pack('i',points_num)) |
---|
5500 | Â Â Â Â Â Â f.write(pack('i',time_step_count)) |
---|
5501 | Â Â Â Â Â Â f.write(pack('f',time_step)) |
---|
5502 | |
---|
5503 | Â Â Â Â Â Â #write lat/long info |
---|
5504 |       for lonlatdep in lonlatdeps: |
---|
5505 |         for float in lonlatdep: |
---|
5506 | Â Â Â Â Â Â Â Â Â Â f.write(pack('f',float)) |
---|
5507 | Â Â Â Â Â Â Â Â Â Â |
---|
5508 | Â Â Â Â Â Â # Write quantity info |
---|
5509 |       for time in range(time_step_count): |
---|
5510 |         for point_i in range(points_num): |
---|
5511 | Â Â Â Â Â Â Â Â Â Â f.write(pack('f',q_time[time,point_i])) |
---|
5512 | Â Â Â Â Â Â Â Â Â Â #print " mux_names[i]", mux_names[i] |
---|
5513 | Â Â Â Â Â Â Â Â Â Â #print "f.write(pack('f',q_time[time,i]))", q_time[time,point_i] |
---|
5514 | Â Â Â Â Â Â f.close() |
---|
5515 |     return base_name, files |
---|
5516 | Â Â |
---|
5517 |   def write_mux(self,lat_long_points, time_step_count, time_step, |
---|
5518 |          depth=None, ha=None, ua=None, va=None |
---|
5519 | Â Â Â Â Â Â Â Â Â ): |
---|
5520 | Â Â Â Â """ |
---|
5521 | Â Â Â Â This will write 3 non-gridded mux files, for testing. |
---|
5522 | Â Â Â Â If no quantities are passed in, |
---|
5523 | Â Â Â Â na and va quantities will be the Easting values. |
---|
5524 | Â Â Â Â Depth and ua will be the Northing value. |
---|
5525 | Â Â Â Â """ |
---|
5526 | Â Â Â Â #print "lat_long_points", lat_long_points |
---|
5527 | Â Â Â Â #print "time_step_count",time_step_count |
---|
5528 | Â Â Â Â #print "time_step", |
---|
5529 | Â Â Â Â |
---|
5530 | Â Â Â Â points_num =Â len(lat_long_points) |
---|
5531 | Â Â Â Â lonlatdeps =Â [] |
---|
5532 | Â Â Â Â quantities =Â ['HA','UA','VA'] |
---|
5533 | Â Â Â Â |
---|
5534 | Â Â Â Â mux_names =Â [WAVEHEIGHT_MUX_LABEL, |
---|
5535 | Â Â Â Â Â Â Â Â Â Â Â EAST_VELOCITY_LABEL, |
---|
5536 | Â Â Â Â Â Â Â Â Â Â Â NORTH_VELOCITY_LABEL] |
---|
5537 | Â Â Â Â quantities_init =Â [[],[],[]] |
---|
5538 | Â Â Â Â # urs binary is latitude fastest |
---|
5539 |     for point in lat_long_points: |
---|
5540 | Â Â Â Â Â Â lat =Â point[0] |
---|
5541 | Â Â Â Â Â Â lon =Â point[1] |
---|
5542 |       _ , e, n = redfearn(lat, lon) |
---|
5543 |       if depth is None: |
---|
5544 | Â Â Â Â Â Â Â Â this_depth =Â n |
---|
5545 | Â Â Â Â Â Â else: |
---|
5546 | Â Â Â Â Â Â Â Â this_depth =Â depth |
---|
5547 |       if ha is None: |
---|
5548 | Â Â Â Â Â Â Â Â this_ha =Â e |
---|
5549 | Â Â Â Â Â Â else: |
---|
5550 | Â Â Â Â Â Â Â Â this_ha =Â ha |
---|
5551 |       if ua is None: |
---|
5552 | Â Â Â Â Â Â Â Â this_ua =Â n |
---|
5553 | Â Â Â Â Â Â else: |
---|
5554 | Â Â Â Â Â Â Â Â this_ua =Â ua |
---|
5555 |       if va is None: |
---|
5556 |         this_va = e  |
---|
5557 | Â Â Â Â Â Â else: |
---|
5558 |         this_va = va     |
---|
5559 |       lonlatdeps.append([lon, lat, this_depth]) |
---|
5560 | Â Â Â Â Â Â quantities_init[0].append(this_ha)Â # HA |
---|
5561 | Â Â Â Â Â Â quantities_init[1].append(this_ua)Â # UA |
---|
5562 | Â Â Â Â Â Â quantities_init[2].append(this_va)Â # VA |
---|
5563 | Â Â Â Â Â Â Â Â |
---|
5564 |     file_handle, base_name = tempfile.mkstemp("") |
---|
5565 | Â Â Â Â os.close(file_handle) |
---|
5566 | Â Â Â Â os.remove(base_name) |
---|
5567 | Â Â Â Â |
---|
5568 | Â Â Â Â files =Â []Â Â Â Â |
---|
5569 |     for i,q in enumerate(quantities): |
---|
5570 | Â Â Â Â Â Â quantities_init[i]Â =Â ensure_numeric(quantities_init[i]) |
---|
5571 | Â Â Â Â Â Â #print "HA_init", HA_init |
---|
5572 |       q_time = zeros((time_step_count, points_num), Float) |
---|
5573 |       for time in range(time_step_count): |
---|
5574 | Â Â Â Â Â Â Â Â q_time[time,:]Â =Â quantities_init[i]Â #* time * 4 |
---|
5575 | Â Â Â Â Â Â |
---|
5576 | Â Â Â Â Â Â #Write C files |
---|
5577 | Â Â Â Â Â Â columns =Â 3Â # long, lat , depth |
---|
5578 |       file = base_name + mux_names[i] |
---|
5579 | Â Â Â Â Â Â #print "base_name file",file |
---|
5580 |       f = open(file, 'wb') |
---|
5581 | Â Â Â Â Â Â files.append(file) |
---|
5582 | Â Â Â Â Â Â f.write(pack('i',points_num)) |
---|
5583 | Â Â Â Â Â Â f.write(pack('i',time_step_count)) |
---|
5584 | Â Â Â Â Â Â f.write(pack('f',time_step)) |
---|
5585 | |
---|
5586 | Â Â Â Â Â Â #write lat/long info |
---|
5587 |       for lonlatdep in lonlatdeps: |
---|
5588 |         for float in lonlatdep: |
---|
5589 | Â Â Â Â Â Â Â Â Â Â f.write(pack('f',float)) |
---|
5590 | Â Â Â Â Â Â Â Â Â Â |
---|
5591 | Â Â Â Â Â Â # Write quantity info |
---|
5592 |       for time in range(time_step_count): |
---|
5593 |         for point_i in range(points_num): |
---|
5594 | Â Â Â Â Â Â Â Â Â Â f.write(pack('f',q_time[time,point_i])) |
---|
5595 | Â Â Â Â Â Â Â Â Â Â #print " mux_names[i]", mux_names[i] |
---|
5596 | Â Â Â Â Â Â Â Â Â Â #print "f.write(pack('f',q_time[time,i]))", q_time[time,point_i] |
---|
5597 | Â Â Â Â Â Â f.close() |
---|
5598 |     return base_name, files |
---|
5599 | Â Â Â Â |
---|
5600 | Â Â |
---|
5601 |   def delete_mux(self, files): |
---|
5602 |     for file in files: |
---|
5603 | Â Â Â Â Â Â os.remove(file) |
---|
5604 | Â Â Â Â Â Â |
---|
5605 |   def test_urs2sww_test_fail(self): |
---|
5606 | Â Â Â Â points_num =Â -100 |
---|
5607 | Â Â Â Â time_step_count =Â 45 |
---|
5608 | Â Â Â Â time_step =Â -7 |
---|
5609 |     file_handle, base_name = tempfile.mkstemp("")    |
---|
5610 | Â Â Â Â os.close(file_handle) |
---|
5611 | Â Â Â Â os.remove(base_name) |
---|
5612 | Â Â Â Â |
---|
5613 | Â Â Â Â files =Â [] |
---|
5614 | Â Â Â Â quantities =Â ['HA','UA','VA'] |
---|
5615 | Â Â Â Â |
---|
5616 | Â Â Â Â mux_names =Â [WAVEHEIGHT_MUX_LABEL, |
---|
5617 | Â Â Â Â Â Â Â Â Â Â Â EAST_VELOCITY_LABEL, |
---|
5618 | Â Â Â Â Â Â Â Â Â Â Â NORTH_VELOCITY_LABEL] |
---|
5619 |     for i,q in enumerate(quantities): |
---|
5620 | Â Â Â Â Â Â #Write C files |
---|
5621 | Â Â Â Â Â Â columns =Â 3Â # long, lat , depth |
---|
5622 |       file = base_name + mux_names[i] |
---|
5623 |       f = open(file, 'wb') |
---|
5624 | Â Â Â Â Â Â files.append(file) |
---|
5625 | Â Â Â Â Â Â f.write(pack('i',points_num)) |
---|
5626 | Â Â Â Â Â Â f.write(pack('i',time_step_count)) |
---|
5627 | Â Â Â Â Â Â f.write(pack('f',time_step)) |
---|
5628 | |
---|
5629 | Â Â Â Â Â Â f.close()Â Â |
---|
5630 | Â Â Â Â tide =Â 1 |
---|
5631 | Â Â Â Â try: |
---|
5632 |       urs2sww(base_name, remove_nc_files=True, mean_stage=tide, |
---|
5633 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose)Â Â Â Â |
---|
5634 |     except ANUGAError: |
---|
5635 | Â Â Â Â Â Â pass |
---|
5636 | Â Â Â Â else: |
---|
5637 | Â Â Â Â Â Â self.delete_mux(files) |
---|
5638 | Â Â Â Â Â Â msg =Â 'Should have raised exception' |
---|
5639 |       raise msg |
---|
5640 | Â Â Â Â sww_file =Â base_name +Â '.sww' |
---|
5641 | Â Â Â Â self.delete_mux(files) |
---|
5642 | Â Â Â Â |
---|
5643 |   def test_urs2sww_test_fail2(self): |
---|
5644 | Â Â Â Â base_name =Â 'Harry-high-pants' |
---|
5645 | Â Â Â Â try: |
---|
5646 | Â Â Â Â Â Â urs2sww(base_name)Â Â Â Â |
---|
5647 |     except IOError: |
---|
5648 | Â Â Â Â Â Â pass |
---|
5649 | Â Â Â Â else: |
---|
5650 | Â Â Â Â Â Â self.delete_mux(files) |
---|
5651 | Â Â Â Â Â Â msg =Â 'Should have raised exception' |
---|
5652 |       raise msg |
---|
5653 | Â Â Â Â Â Â |
---|
5654 |   def test_urs2sww(self): |
---|
5655 | Â Â Â Â tide =Â 1 |
---|
5656 |     base_name, files = self.create_mux() |
---|
5657 | Â Â Â Â urs2sww(base_name |
---|
5658 | Â Â Â Â Â Â Â Â #, origin=(0,0,0) |
---|
5659 |         , mean_stage=tide |
---|
5660 |         , remove_nc_files=True, |
---|
5661 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose |
---|
5662 | Â Â Â Â Â Â Â Â ) |
---|
5663 | Â Â Â Â sww_file =Â base_name +Â '.sww' |
---|
5664 | Â Â Â Â |
---|
5665 | Â Â Â Â #Let's interigate the sww file |
---|
5666 |     # Note, the sww info is not gridded. It is point data. |
---|
5667 | Â Â Â Â fid =Â NetCDFFile(sww_file) |
---|
5668 | |
---|
5669 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
5670 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
5671 | Â Â Â Â geo_reference =Â Geo_reference(NetCDFObject=fid) |
---|
5672 | |
---|
5673 | Â Â Â Â |
---|
5674 |     #Check that first coordinate is correctly represented    |
---|
5675 | Â Â Â Â #Work out the UTM coordinates for first point |
---|
5676 |     zone, e, n = redfearn(-34.5, 150.66667) |
---|
5677 | Â Â Â Â |
---|
5678 |     assert allclose(geo_reference.get_absolute([[x[0],y[0]]]), [e,n]) |
---|
5679 | |
---|
5680 | Â Â Â Â # Make x and y absolute |
---|
5681 |     points = geo_reference.get_absolute(map(None, x, y)) |
---|
5682 | Â Â Â Â points =Â ensure_numeric(points) |
---|
5683 | Â Â Â Â x =Â points[:,0] |
---|
5684 | Â Â Â Â y =Â points[:,1] |
---|
5685 | Â Â Â Â |
---|
5686 | Â Â Â Â #Check first value |
---|
5687 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
5688 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
5689 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:] |
---|
5690 | Â Â Â Â elevation =Â fid.variables['elevation'][:] |
---|
5691 |     assert allclose(stage[0,0], e +tide) #Meters |
---|
5692 | |
---|
5693 | Â Â Â Â #Check the momentums - ua |
---|
5694 | Â Â Â Â #momentum = velocity*(stage-elevation) |
---|
5695 | Â Â Â Â # elevation = - depth |
---|
5696 | Â Â Â Â #momentum = velocity_ua *(stage+depth) |
---|
5697 | Â Â Â Â # = n*(e+tide+n) based on how I'm writing these files |
---|
5698 | Â Â Â Â # |
---|
5699 | Â Â Â Â answer_x =Â n*(e+tide+n) |
---|
5700 | Â Â Â Â actual_x =Â xmomentum[0,0] |
---|
5701 | Â Â Â Â #print "answer_x",answer_x |
---|
5702 | Â Â Â Â #print "actual_x",actual_x |
---|
5703 |     assert allclose(answer_x, actual_x) #Meters |
---|
5704 | |
---|
5705 | Â Â Â Â #Check the momentums - va |
---|
5706 | Â Â Â Â #momentum = velocity*(stage-elevation) |
---|
5707 | Â Â Â Â # -(-elevation) since elevation is inverted in mux files |
---|
5708 | Â Â Â Â #momentum = velocity_va *(stage+elevation) |
---|
5709 | Â Â Â Â # = e*(e+tide+n) based on how I'm writing these files |
---|
5710 | Â Â Â Â answer_y =Â e*(e+tide+n)Â *Â -1Â # talking into account mux file format |
---|
5711 | Â Â Â Â actual_y =Â ymomentum[0,0] |
---|
5712 | Â Â Â Â #print "answer_y",answer_y |
---|
5713 | Â Â Â Â #print "actual_y",actual_y |
---|
5714 |     assert allclose(answer_y, actual_y) #Meters |
---|
5715 | Â Â Â Â |
---|
5716 |     assert allclose(answer_x, actual_x) #Meters |
---|
5717 | Â Â Â Â |
---|
5718 | Â Â Â Â # check the stage values, first time step. |
---|
5719 | Â Â Â Â # These arrays are equal since the Easting values were used as |
---|
5720 | Â Â Â Â # the stage |
---|
5721 |     assert allclose(stage[0], x +tide) #Meters |
---|
5722 | |
---|
5723 | Â Â Â Â # check the elevation values. |
---|
5724 | Â Â Â Â # -ve since urs measures depth, sww meshers height, |
---|
5725 | Â Â Â Â # these arrays are equal since the northing values were used as |
---|
5726 | Â Â Â Â # the elevation |
---|
5727 |     assert allclose(-elevation, y) #Meters |
---|
5728 | Â Â Â Â |
---|
5729 | Â Â Â Â fid.close() |
---|
5730 | Â Â Â Â self.delete_mux(files) |
---|
5731 | Â Â Â Â os.remove(sww_file) |
---|
5732 | Â Â Â Â |
---|
5733 |   def test_urs2sww_momentum(self): |
---|
5734 | Â Â Â Â tide =Â 1 |
---|
5735 | Â Â Â Â time_step_count =Â 3 |
---|
5736 | Â Â Â Â time_step =Â 2 |
---|
5737 | Â Â Â Â #lat_long_points =[(-21.5,114.5),(-21.5,115),(-21.,114.5), (-21.,115.)] |
---|
5738 | Â Â Â Â # This is gridded |
---|
5739 |     lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)] |
---|
5740 | Â Â Â Â depth=20 |
---|
5741 | Â Â Â Â ha=2 |
---|
5742 | Â Â Â Â ua=5 |
---|
5743 | Â Â Â Â va=-10Â #-ve added to take into account mux file format where south |
---|
5744 | Â Â Â Â Â Â Â Â # is positive. |
---|
5745 |     base_name, files = self.write_mux(lat_long_points, |
---|
5746 |                      time_step_count, time_step, |
---|
5747 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â depth=depth, |
---|
5748 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ha=ha, |
---|
5749 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ua=ua, |
---|
5750 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â va=va) |
---|
5751 | Â Â Â Â # write_mux(self,lat_long_points, time_step_count, time_step, |
---|
5752 | Â Â Â Â #Â Â Â Â Â depth=None, ha=None, ua=None, va=None |
---|
5753 | Â Â Â Â urs2sww(base_name |
---|
5754 | Â Â Â Â Â Â Â Â #, origin=(0,0,0) |
---|
5755 |         , mean_stage=tide |
---|
5756 |         , remove_nc_files=True, |
---|
5757 | Â Â Â Â Â Â Â Â Â Â Â verbose=self.verbose |
---|
5758 | Â Â Â Â Â Â Â Â ) |
---|
5759 | Â Â Â Â sww_file =Â base_name +Â '.sww' |
---|
5760 | Â Â Â Â |
---|
5761 | Â Â Â Â #Let's interigate the sww file |
---|
5762 |     # Note, the sww info is not gridded. It is point data. |
---|
5763 | Â Â Â Â fid =Â NetCDFFile(sww_file) |
---|
5764 | |
---|
5765 | Â Â Â Â x =Â fid.variables['x'][:] |
---|
5766 | Â Â Â Â y =Â fid.variables['y'][:] |
---|
5767 | Â Â Â Â geo_reference =Â Geo_reference(NetCDFObject=fid) |
---|
5768 | Â Â Â Â |
---|
5769 | Â Â Â Â #Check first value |
---|
5770 | Â Â Â Â stage =Â fid.variables['stage'][:] |
---|
5771 | Â Â Â Â xmomentum =Â fid.variables['xmomentum'][:] |
---|
5772 | Â Â Â Â ymomentum =Â fid.variables['ymomentum'][:] |
---|
5773 | Â Â Â Â elevation =Â fid.variables['elevation'][:] |
---|
5774 | Â Â Â Â #assert allclose(stage[0,0], e + tide)Â #Meters |
---|
5775 | Â Â Â Â #print "xmomentum", xmomentum |
---|
5776 | Â Â Â Â #print "ymomentum", ymomentum |
---|
5777 | Â Â Â Â #Check the momentums - ua |
---|
5778 | Â Â Â Â #momentum = velocity*water height |
---|
5779 | Â Â Â Â #water height = mux_depth + mux_height +tide |
---|
5780 | Â Â Â Â #water height = mux_depth + mux_height +tide |
---|
5781 | Â Â Â Â #momentum = velocity*(mux_depth + mux_height +tide) |
---|
5782 | Â Â Â Â # |
---|
5783 | Â Â Â Â |
---|
5784 | Â Â Â Â answer =Â 115 |
---|
5785 | Â Â Â Â actual =Â xmomentum[0,0] |
---|
5786 |     assert allclose(answer, actual) #Meters^2/ sec |
---|
5787 | Â Â Â Â answer =Â 230 |
---|
5788 | Â Â Â Â actual =Â ymomentum[0,0] |
---|
5789 | Â Â Â Â #print "answer",answer |
---|
5790 | Â Â Â Â #print "actual",actual |
---|
5791 |     assert allclose(answer, actual) #Meters^2/ sec |
---|
5792 | |
---|
5793 | Â Â Â Â # check the stage values, first time step. |
---|
5794 | Â Â Â Â # These arrays are equal since the Easting values were used as |
---|
5795 | Â Â Â Â # the stage |
---|
5796 | |
---|
5797 | Â Â Â Â #assert allclose(stage[0], x +tide)Â #Meters |
---|
5798 | |
---|
5799 | Â Â Â Â # check the elevation values. |
---|
5800 | Â Â Â Â # -ve since urs measures depth, sww meshers height, |
---|
5801 | Â Â Â Â # these arrays are equal since the northing values were used as |
---|
5802 | Â Â Â Â # the elevation |
---|
5803 | Â Â Â Â #assert allclose(-elevation, y)Â #Meters |
---|
5804 | Â Â Â Â |
---|
5805 | Â Â Â Â fid.close() |
---|
5806 | Â Â Â Â self.delete_mux(files) |
---|
5807 | Â Â Â Â os.remove(sww_file) |
---|
5808 | Â Â Â Â |
---|
5809 | Â |
---|
5810 |   def test_urs2sww_origin(self): |
---|
5811 | Â Â Â Â tide =Â 1 |
---|
5812 |     base_name, files = self.create_mux() |
---|
5813 | Â Â Â Â urs2sww(base_name |
---|
5814 |         , origin=(0,0,0) |
---|
5815 |         , mean_stage=tide |
---|
5816 |         , remove_nc_files=True, |
---|
5817 | Â Â Â Â Â Â Â Â Â Â |
---|