1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | import unittest |
---|
5 | import copy |
---|
6 | from Numeric import zeros, array, allclose, Float |
---|
7 | from util import mean |
---|
8 | |
---|
9 | from data_manager import * |
---|
10 | from shallow_water import * |
---|
11 | from config import epsilon |
---|
12 | |
---|
13 | class Test_Data_Manager(unittest.TestCase): |
---|
14 | def setUp(self): |
---|
15 | import time |
---|
16 | from mesh_factory import rectangular |
---|
17 | |
---|
18 | |
---|
19 | #Create basic mesh |
---|
20 | points, vertices, boundary = rectangular(2, 2) |
---|
21 | |
---|
22 | #Create shallow water domain |
---|
23 | domain = Domain(points, vertices, boundary) |
---|
24 | domain.default_order=2 |
---|
25 | |
---|
26 | |
---|
27 | #Set some field values |
---|
28 | domain.set_quantity('elevation', lambda x,y: -x) |
---|
29 | domain.set_quantity('friction', 0.03) |
---|
30 | |
---|
31 | |
---|
32 | ###################### |
---|
33 | # Boundary conditions |
---|
34 | B = Transmissive_boundary(domain) |
---|
35 | domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
36 | |
---|
37 | |
---|
38 | ###################### |
---|
39 | #Initial condition - with jumps |
---|
40 | |
---|
41 | |
---|
42 | bed = domain.quantities['elevation'].vertex_values |
---|
43 | stage = zeros(bed.shape, Float) |
---|
44 | |
---|
45 | h = 0.3 |
---|
46 | for i in range(stage.shape[0]): |
---|
47 | if i % 2 == 0: |
---|
48 | stage[i,:] = bed[i,:] + h |
---|
49 | else: |
---|
50 | stage[i,:] = bed[i,:] |
---|
51 | |
---|
52 | domain.set_quantity('stage', stage) |
---|
53 | self.initial_stage = copy.copy(domain.quantities['stage'].vertex_values) |
---|
54 | |
---|
55 | domain.distribute_to_vertices_and_edges() |
---|
56 | |
---|
57 | |
---|
58 | self.domain = domain |
---|
59 | |
---|
60 | C = domain.get_vertex_coordinates() |
---|
61 | self.X = C[:,0:6:2].copy() |
---|
62 | self.Y = C[:,1:6:2].copy() |
---|
63 | |
---|
64 | self.F = bed |
---|
65 | |
---|
66 | |
---|
67 | def tearDown(self): |
---|
68 | pass |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | # def test_xya(self): |
---|
74 | # import os |
---|
75 | # from Numeric import concatenate |
---|
76 | |
---|
77 | # import time, os |
---|
78 | # from Numeric import array, zeros, allclose, Float, concatenate |
---|
79 | |
---|
80 | # domain = self.domain |
---|
81 | |
---|
82 | # domain.filename = 'datatest' + str(time.time()) |
---|
83 | # domain.format = 'xya' |
---|
84 | # domain.smooth = True |
---|
85 | |
---|
86 | # xya = get_dataobject(self.domain) |
---|
87 | # xya.store_all() |
---|
88 | |
---|
89 | |
---|
90 | # #Read back |
---|
91 | # file = open(xya.filename) |
---|
92 | # lFile = file.read().split('\n') |
---|
93 | # lFile = lFile[:-1] |
---|
94 | |
---|
95 | # file.close() |
---|
96 | # os.remove(xya.filename) |
---|
97 | |
---|
98 | # #Check contents |
---|
99 | # if domain.smooth: |
---|
100 | # self.failUnless(lFile[0] == '9 3 # <vertex #> <x> <y> [attributes]') |
---|
101 | # else: |
---|
102 | # self.failUnless(lFile[0] == '24 3 # <vertex #> <x> <y> [attributes]') |
---|
103 | |
---|
104 | # #Get smoothed field values with X and Y |
---|
105 | # X,Y,F,V = domain.get_vertex_values(xy=True, value_array='field_values', |
---|
106 | # indices = (0,1), precision = Float) |
---|
107 | |
---|
108 | |
---|
109 | # Q,V = domain.get_vertex_values(xy=False, value_array='conserved_quantities', |
---|
110 | # indices = (0,), precision = Float) |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | # for i, line in enumerate(lFile[1:]): |
---|
115 | # fields = line.split() |
---|
116 | |
---|
117 | # assert len(fields) == 5 |
---|
118 | |
---|
119 | # assert allclose(float(fields[0]), X[i]) |
---|
120 | # assert allclose(float(fields[1]), Y[i]) |
---|
121 | # assert allclose(float(fields[2]), F[i,0]) |
---|
122 | # assert allclose(float(fields[3]), Q[i,0]) |
---|
123 | # assert allclose(float(fields[4]), F[i,1]) |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | def test_sww_constant(self): |
---|
129 | """Test that constant sww information can be written correctly |
---|
130 | (non smooth) |
---|
131 | """ |
---|
132 | |
---|
133 | import time, os |
---|
134 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
135 | from Scientific.IO.NetCDF import NetCDFFile |
---|
136 | |
---|
137 | self.domain.filename = 'datatest' + str(id(self)) |
---|
138 | self.domain.format = 'sww' |
---|
139 | self.domain.smooth = False |
---|
140 | |
---|
141 | sww = get_dataobject(self.domain) |
---|
142 | sww.store_connectivity() |
---|
143 | |
---|
144 | #Check contents |
---|
145 | #Get NetCDF |
---|
146 | fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
147 | |
---|
148 | # Get the variables |
---|
149 | x = fid.variables['x'] |
---|
150 | y = fid.variables['y'] |
---|
151 | z = fid.variables['elevation'] |
---|
152 | |
---|
153 | volumes = fid.variables['volumes'] |
---|
154 | |
---|
155 | |
---|
156 | assert allclose (x[:], self.X.flat) |
---|
157 | assert allclose (y[:], self.Y.flat) |
---|
158 | assert allclose (z[:], self.F.flat) |
---|
159 | |
---|
160 | V = volumes |
---|
161 | |
---|
162 | P = len(self.domain) |
---|
163 | for k in range(P): |
---|
164 | assert V[k, 0] == 3*k |
---|
165 | assert V[k, 1] == 3*k+1 |
---|
166 | assert V[k, 2] == 3*k+2 |
---|
167 | |
---|
168 | |
---|
169 | fid.close() |
---|
170 | |
---|
171 | #Cleanup |
---|
172 | os.remove(sww.filename) |
---|
173 | |
---|
174 | |
---|
175 | def test_sww_constant_smooth(self): |
---|
176 | """Test that constant sww information can be written correctly |
---|
177 | (non smooth) |
---|
178 | """ |
---|
179 | |
---|
180 | import time, os |
---|
181 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
182 | from Scientific.IO.NetCDF import NetCDFFile |
---|
183 | |
---|
184 | self.domain.filename = 'datatest' + str(id(self)) |
---|
185 | self.domain.format = 'sww' |
---|
186 | self.domain.smooth = True |
---|
187 | |
---|
188 | sww = get_dataobject(self.domain) |
---|
189 | sww.store_connectivity() |
---|
190 | |
---|
191 | #Check contents |
---|
192 | #Get NetCDF |
---|
193 | fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
194 | |
---|
195 | # Get the variables |
---|
196 | x = fid.variables['x'] |
---|
197 | y = fid.variables['y'] |
---|
198 | z = fid.variables['elevation'] |
---|
199 | |
---|
200 | volumes = fid.variables['volumes'] |
---|
201 | |
---|
202 | X = x[:] |
---|
203 | Y = y[:] |
---|
204 | |
---|
205 | assert allclose([X[0], Y[0]], array([0.0, 0.0])) |
---|
206 | assert allclose([X[1], Y[1]], array([0.0, 0.5])) |
---|
207 | assert allclose([X[2], Y[2]], array([0.0, 1.0])) |
---|
208 | |
---|
209 | assert allclose([X[4], Y[4]], array([0.5, 0.5])) |
---|
210 | |
---|
211 | assert allclose([X[7], Y[7]], array([1.0, 0.5])) |
---|
212 | |
---|
213 | Z = z[:] |
---|
214 | assert Z[4] == -0.5 |
---|
215 | |
---|
216 | V = volumes |
---|
217 | assert V[2,0] == 4 |
---|
218 | assert V[2,1] == 5 |
---|
219 | assert V[2,2] == 1 |
---|
220 | |
---|
221 | assert V[4,0] == 6 |
---|
222 | assert V[4,1] == 7 |
---|
223 | assert V[4,2] == 3 |
---|
224 | |
---|
225 | |
---|
226 | fid.close() |
---|
227 | |
---|
228 | #Cleanup |
---|
229 | os.remove(sww.filename) |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | def test_sww_variable(self): |
---|
234 | """Test that sww information can be written correctly |
---|
235 | """ |
---|
236 | |
---|
237 | import time, os |
---|
238 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
239 | from Scientific.IO.NetCDF import NetCDFFile |
---|
240 | |
---|
241 | self.domain.filename = 'datatest' + str(id(self)) |
---|
242 | self.domain.format = 'sww' |
---|
243 | self.domain.smooth = True |
---|
244 | self.domain.reduction = mean |
---|
245 | |
---|
246 | sww = get_dataobject(self.domain) |
---|
247 | sww.store_connectivity() |
---|
248 | sww.store_timestep('stage') |
---|
249 | |
---|
250 | #Check contents |
---|
251 | #Get NetCDF |
---|
252 | fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
253 | |
---|
254 | |
---|
255 | # Get the variables |
---|
256 | x = fid.variables['x'] |
---|
257 | y = fid.variables['y'] |
---|
258 | z = fid.variables['elevation'] |
---|
259 | time = fid.variables['time'] |
---|
260 | stage = fid.variables['stage'] |
---|
261 | |
---|
262 | |
---|
263 | Q = self.domain.quantities['stage'] |
---|
264 | Q0 = Q.vertex_values[:,0] |
---|
265 | Q1 = Q.vertex_values[:,1] |
---|
266 | Q2 = Q.vertex_values[:,2] |
---|
267 | |
---|
268 | A = stage[0,:] |
---|
269 | #print A[0], (Q2[0,0] + Q1[1,0])/2 |
---|
270 | assert allclose(A[0], (Q2[0] + Q1[1])/2) |
---|
271 | assert allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3) |
---|
272 | assert allclose(A[2], Q0[3]) |
---|
273 | assert allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3) |
---|
274 | |
---|
275 | #Center point |
---|
276 | assert allclose(A[4], (Q1[0] + Q2[1] + Q0[2] +\ |
---|
277 | Q0[5] + Q2[6] + Q1[7])/6) |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | fid.close() |
---|
282 | |
---|
283 | #Cleanup |
---|
284 | os.remove(sww.filename) |
---|
285 | |
---|
286 | |
---|
287 | def test_sww_variable2(self): |
---|
288 | """Test that sww information can be written correctly |
---|
289 | multiple timesteps. Use average as reduction operator |
---|
290 | """ |
---|
291 | |
---|
292 | import time, os |
---|
293 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
294 | from Scientific.IO.NetCDF import NetCDFFile |
---|
295 | |
---|
296 | self.domain.filename = 'datatest' + str(id(self)) |
---|
297 | self.domain.format = 'sww' |
---|
298 | self.domain.smooth = True |
---|
299 | |
---|
300 | self.domain.reduction = mean |
---|
301 | |
---|
302 | sww = get_dataobject(self.domain) |
---|
303 | sww.store_connectivity() |
---|
304 | sww.store_timestep('stage') |
---|
305 | self.domain.evolve_to_end(finaltime = 0.01) |
---|
306 | sww.store_timestep('stage') |
---|
307 | |
---|
308 | |
---|
309 | #Check contents |
---|
310 | #Get NetCDF |
---|
311 | fid = NetCDFFile(sww.filename, 'r') #Open existing file for append |
---|
312 | |
---|
313 | # Get the variables |
---|
314 | x = fid.variables['x'] |
---|
315 | y = fid.variables['y'] |
---|
316 | z = fid.variables['elevation'] |
---|
317 | time = fid.variables['time'] |
---|
318 | stage = fid.variables['stage'] |
---|
319 | |
---|
320 | #Check values |
---|
321 | Q = self.domain.quantities['stage'] |
---|
322 | Q0 = Q.vertex_values[:,0] |
---|
323 | Q1 = Q.vertex_values[:,1] |
---|
324 | Q2 = Q.vertex_values[:,2] |
---|
325 | |
---|
326 | A = stage[1,:] |
---|
327 | assert allclose(A[0], (Q2[0] + Q1[1])/2) |
---|
328 | assert allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3) |
---|
329 | assert allclose(A[2], Q0[3]) |
---|
330 | assert allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3) |
---|
331 | |
---|
332 | #Center point |
---|
333 | assert allclose(A[4], (Q1[0] + Q2[1] + Q0[2] +\ |
---|
334 | Q0[5] + Q2[6] + Q1[7])/6) |
---|
335 | |
---|
336 | |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | fid.close() |
---|
341 | |
---|
342 | #Cleanup |
---|
343 | os.remove(sww.filename) |
---|
344 | |
---|
345 | def test_sww_variable3(self): |
---|
346 | """Test that sww information can be written correctly |
---|
347 | multiple timesteps using a different reduction operator (min) |
---|
348 | """ |
---|
349 | |
---|
350 | import time, os |
---|
351 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
352 | from Scientific.IO.NetCDF import NetCDFFile |
---|
353 | |
---|
354 | self.domain.filename = 'datatest' + str(id(self)) |
---|
355 | self.domain.format = 'sww' |
---|
356 | self.domain.smooth = True |
---|
357 | self.domain.reduction = min |
---|
358 | |
---|
359 | sww = get_dataobject(self.domain) |
---|
360 | sww.store_connectivity() |
---|
361 | sww.store_timestep('stage') |
---|
362 | |
---|
363 | self.domain.evolve_to_end(finaltime = 0.01) |
---|
364 | sww.store_timestep('stage') |
---|
365 | |
---|
366 | |
---|
367 | |
---|
368 | #Check contents |
---|
369 | #Get NetCDF |
---|
370 | fid = NetCDFFile(sww.filename, 'r') |
---|
371 | |
---|
372 | |
---|
373 | # Get the variables |
---|
374 | x = fid.variables['x'] |
---|
375 | y = fid.variables['y'] |
---|
376 | z = fid.variables['elevation'] |
---|
377 | time = fid.variables['time'] |
---|
378 | stage = fid.variables['stage'] |
---|
379 | |
---|
380 | #Check values |
---|
381 | #Check values |
---|
382 | Q = self.domain.quantities['stage'] |
---|
383 | Q0 = Q.vertex_values[:,0] |
---|
384 | Q1 = Q.vertex_values[:,1] |
---|
385 | Q2 = Q.vertex_values[:,2] |
---|
386 | |
---|
387 | A = stage[1,:] |
---|
388 | assert allclose(A[0], min(Q2[0], Q1[1])) |
---|
389 | assert allclose(A[1], min(Q0[1], Q1[3], Q2[2])) |
---|
390 | assert allclose(A[2], Q0[3]) |
---|
391 | assert allclose(A[3], min(Q0[0], Q1[5], Q2[4])) |
---|
392 | |
---|
393 | #Center point |
---|
394 | assert allclose(A[4], min(Q1[0], Q2[1], Q0[2],\ |
---|
395 | Q0[5], Q2[6], Q1[7])) |
---|
396 | |
---|
397 | |
---|
398 | fid.close() |
---|
399 | |
---|
400 | #Cleanup |
---|
401 | os.remove(sww.filename) |
---|
402 | |
---|
403 | |
---|
404 | def test_sync(self): |
---|
405 | """Test info stored at each timestep is as expected (incl initial condition) |
---|
406 | """ |
---|
407 | |
---|
408 | import time, os, config |
---|
409 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
410 | from Scientific.IO.NetCDF import NetCDFFile |
---|
411 | |
---|
412 | self.domain.filename = 'synctest' |
---|
413 | self.domain.format = 'sww' |
---|
414 | self.domain.smooth = False |
---|
415 | self.domain.store = True |
---|
416 | self.domain.beta_h = 0 |
---|
417 | |
---|
418 | #Evolution |
---|
419 | for t in self.domain.evolve(yieldstep = 1.0, finaltime = 4.0): |
---|
420 | stage = self.domain.quantities['stage'].vertex_values |
---|
421 | |
---|
422 | #Get NetCDF |
---|
423 | fid = NetCDFFile(self.domain.writer.filename, 'r') |
---|
424 | stage_file = fid.variables['stage'] |
---|
425 | |
---|
426 | if t == 0.0: |
---|
427 | assert allclose(stage, self.initial_stage) |
---|
428 | assert allclose(stage_file[:], stage.flat) |
---|
429 | else: |
---|
430 | assert not allclose(stage, self.initial_stage) |
---|
431 | assert not allclose(stage_file[:], stage.flat) |
---|
432 | |
---|
433 | fid.close() |
---|
434 | os.remove(self.domain.writer.filename) |
---|
435 | |
---|
436 | |
---|
437 | |
---|
438 | def test_sww_DSG(self): |
---|
439 | """Not a test, rather a look at the sww format |
---|
440 | """ |
---|
441 | |
---|
442 | import time, os |
---|
443 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
444 | from Scientific.IO.NetCDF import NetCDFFile |
---|
445 | |
---|
446 | self.domain.filename = 'datatest' + str(id(self)) |
---|
447 | self.domain.format = 'sww' |
---|
448 | self.domain.smooth = True |
---|
449 | self.domain.reduction = mean |
---|
450 | |
---|
451 | sww = get_dataobject(self.domain) |
---|
452 | sww.store_connectivity() |
---|
453 | sww.store_timestep('stage') |
---|
454 | |
---|
455 | #Check contents |
---|
456 | #Get NetCDF |
---|
457 | fid = NetCDFFile(sww.filename, 'r') |
---|
458 | |
---|
459 | # Get the variables |
---|
460 | x = fid.variables['x'] |
---|
461 | y = fid.variables['y'] |
---|
462 | z = fid.variables['elevation'] |
---|
463 | |
---|
464 | volumes = fid.variables['volumes'] |
---|
465 | time = fid.variables['time'] |
---|
466 | |
---|
467 | # 2D |
---|
468 | stage = fid.variables['stage'] |
---|
469 | |
---|
470 | X = x[:] |
---|
471 | Y = y[:] |
---|
472 | Z = z[:] |
---|
473 | V = volumes[:] |
---|
474 | T = time[:] |
---|
475 | S = stage[:,:] |
---|
476 | |
---|
477 | # print "****************************" |
---|
478 | # print "X ",X |
---|
479 | # print "****************************" |
---|
480 | # print "Y ",Y |
---|
481 | # print "****************************" |
---|
482 | # print "Z ",Z |
---|
483 | # print "****************************" |
---|
484 | # print "V ",V |
---|
485 | # print "****************************" |
---|
486 | # print "Time ",T |
---|
487 | # print "****************************" |
---|
488 | # print "Stage ",S |
---|
489 | # print "****************************" |
---|
490 | |
---|
491 | |
---|
492 | fid.close() |
---|
493 | |
---|
494 | #Cleanup |
---|
495 | os.remove(sww.filename) |
---|
496 | |
---|
497 | |
---|
498 | |
---|
499 | def test_dem2pts(self): |
---|
500 | """Test conversion from dem in ascii format to native NetCDF xya format |
---|
501 | """ |
---|
502 | |
---|
503 | import time, os |
---|
504 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
505 | from Scientific.IO.NetCDF import NetCDFFile |
---|
506 | |
---|
507 | #Write test asc file |
---|
508 | root = 'demtest' |
---|
509 | |
---|
510 | filename = root+'.asc' |
---|
511 | fid = open(filename, 'w') |
---|
512 | fid.write("""ncols 5 |
---|
513 | nrows 6 |
---|
514 | xllcorner 2000.5 |
---|
515 | yllcorner 3000.5 |
---|
516 | cellsize 25 |
---|
517 | NODATA_value -9999 |
---|
518 | """) |
---|
519 | #Create linear function |
---|
520 | |
---|
521 | ref_points = [] |
---|
522 | ref_elevation = [] |
---|
523 | for i in range(6): |
---|
524 | y = (6-i)*25.0 |
---|
525 | for j in range(5): |
---|
526 | x = j*25.0 |
---|
527 | z = x+2*y |
---|
528 | |
---|
529 | ref_points.append( [x,y] ) |
---|
530 | ref_elevation.append(z) |
---|
531 | fid.write('%f ' %z) |
---|
532 | fid.write('\n') |
---|
533 | |
---|
534 | fid.close() |
---|
535 | |
---|
536 | #Write prj file with metadata |
---|
537 | metafilename = root+'.prj' |
---|
538 | fid = open(metafilename, 'w') |
---|
539 | |
---|
540 | |
---|
541 | fid.write("""Projection UTM |
---|
542 | Zone 56 |
---|
543 | Datum WGS84 |
---|
544 | Zunits NO |
---|
545 | Units METERS |
---|
546 | Spheroid WGS84 |
---|
547 | Xshift 0.0000000000 |
---|
548 | Yshift 10000000.0000000000 |
---|
549 | Parameters |
---|
550 | """) |
---|
551 | fid.close() |
---|
552 | |
---|
553 | #Convert to NetCDF xya |
---|
554 | convert_dem_from_ascii2netcdf(root) |
---|
555 | dem2pts(root) |
---|
556 | |
---|
557 | #Check contents |
---|
558 | #Get NetCDF |
---|
559 | fid = NetCDFFile(root+'.pts', 'r') |
---|
560 | |
---|
561 | # Get the variables |
---|
562 | #print fid.variables.keys() |
---|
563 | points = fid.variables['points'] |
---|
564 | elevation = fid.variables['elevation'] |
---|
565 | |
---|
566 | #Check values |
---|
567 | |
---|
568 | #print points[:] |
---|
569 | #print ref_points |
---|
570 | assert allclose(points, ref_points) |
---|
571 | |
---|
572 | #print attributes[:] |
---|
573 | #print ref_elevation |
---|
574 | assert allclose(elevation, ref_elevation) |
---|
575 | |
---|
576 | #Cleanup |
---|
577 | fid.close() |
---|
578 | |
---|
579 | |
---|
580 | os.remove(root + '.pts') |
---|
581 | os.remove(root + '.dem') |
---|
582 | os.remove(root + '.asc') |
---|
583 | os.remove(root + '.prj') |
---|
584 | |
---|
585 | |
---|
586 | def test_ferret2sww(self): |
---|
587 | """Test that georeferencing etc works when converting from |
---|
588 | ferret format (lat/lon) to sww format (UTM) |
---|
589 | """ |
---|
590 | from Scientific.IO.NetCDF import NetCDFFile |
---|
591 | |
---|
592 | #The test file has |
---|
593 | # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
594 | # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
595 | # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ; |
---|
596 | # |
---|
597 | # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
598 | # Fourth value (index==3) is -6.50198 cm |
---|
599 | |
---|
600 | |
---|
601 | from coordinate_transforms.redfearn import redfearn |
---|
602 | |
---|
603 | fid = NetCDFFile('small_ha.nc') |
---|
604 | first_value = fid.variables['HA'][:][0,0,0] |
---|
605 | fourth_value = fid.variables['HA'][:][0,0,3] |
---|
606 | |
---|
607 | |
---|
608 | #Call conversion (with zero origin) |
---|
609 | ferret2sww('small', verbose=False, |
---|
610 | origin = (56, 0, 0)) |
---|
611 | |
---|
612 | |
---|
613 | #Work out the UTM coordinates for first point |
---|
614 | zone, e, n = redfearn(-34.5, 150.66667) |
---|
615 | #print zone, e, n |
---|
616 | |
---|
617 | #Read output file 'small.sww' |
---|
618 | fid = NetCDFFile('small.sww') |
---|
619 | |
---|
620 | x = fid.variables['x'][:] |
---|
621 | y = fid.variables['y'][:] |
---|
622 | |
---|
623 | #Check that first coordinate is correctly represented |
---|
624 | assert allclose(x[0], e) |
---|
625 | assert allclose(y[0], n) |
---|
626 | |
---|
627 | #Check first value |
---|
628 | stage = fid.variables['stage'][:] |
---|
629 | xmomentum = fid.variables['xmomentum'][:] |
---|
630 | ymomentum = fid.variables['ymomentum'][:] |
---|
631 | |
---|
632 | #print ymomentum |
---|
633 | |
---|
634 | assert allclose(stage[0,0], first_value/100) #Meters |
---|
635 | |
---|
636 | #Check fourth value |
---|
637 | assert allclose(stage[0,3], fourth_value/100) #Meters |
---|
638 | |
---|
639 | fid.close() |
---|
640 | |
---|
641 | #Cleanup |
---|
642 | import os |
---|
643 | os.remove('small.sww') |
---|
644 | |
---|
645 | |
---|
646 | |
---|
647 | def test_ferret2sww_2(self): |
---|
648 | """Test that georeferencing etc works when converting from |
---|
649 | ferret format (lat/lon) to sww format (UTM) |
---|
650 | """ |
---|
651 | from Scientific.IO.NetCDF import NetCDFFile |
---|
652 | |
---|
653 | #The test file has |
---|
654 | # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
655 | # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
656 | # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ; |
---|
657 | # |
---|
658 | # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
659 | # Fourth value (index==3) is -6.50198 cm |
---|
660 | |
---|
661 | |
---|
662 | from coordinate_transforms.redfearn import redfearn |
---|
663 | |
---|
664 | fid = NetCDFFile('small_ha.nc') |
---|
665 | |
---|
666 | #Pick a coordinate and a value |
---|
667 | |
---|
668 | time_index = 1 |
---|
669 | lat_index = 0 |
---|
670 | lon_index = 2 |
---|
671 | |
---|
672 | test_value = fid.variables['HA'][:][time_index, lat_index, lon_index] |
---|
673 | test_time = fid.variables['TIME'][:][time_index] |
---|
674 | test_lat = fid.variables['LAT'][:][lat_index] |
---|
675 | test_lon = fid.variables['LON'][:][lon_index] |
---|
676 | |
---|
677 | linear_point_index = lat_index*4 + lon_index |
---|
678 | fid.close() |
---|
679 | |
---|
680 | #Call conversion (with zero origin) |
---|
681 | ferret2sww('small', verbose=False, |
---|
682 | origin = (56, 0, 0)) |
---|
683 | |
---|
684 | |
---|
685 | #Work out the UTM coordinates for test point |
---|
686 | zone, e, n = redfearn(test_lat, test_lon) |
---|
687 | |
---|
688 | #Read output file 'small.sww' |
---|
689 | fid = NetCDFFile('small.sww') |
---|
690 | |
---|
691 | x = fid.variables['x'][:] |
---|
692 | y = fid.variables['y'][:] |
---|
693 | |
---|
694 | #Check that test coordinate is correctly represented |
---|
695 | assert allclose(x[linear_point_index], e) |
---|
696 | assert allclose(y[linear_point_index], n) |
---|
697 | |
---|
698 | #Check test value |
---|
699 | stage = fid.variables['stage'][:] |
---|
700 | |
---|
701 | assert allclose(stage[time_index, linear_point_index], test_value/100) |
---|
702 | |
---|
703 | fid.close() |
---|
704 | |
---|
705 | #Cleanup |
---|
706 | import os |
---|
707 | os.remove('small.sww') |
---|
708 | |
---|
709 | def test_ferret2sww3(self): |
---|
710 | """ |
---|
711 | """ |
---|
712 | from Scientific.IO.NetCDF import NetCDFFile |
---|
713 | |
---|
714 | #The test file has |
---|
715 | # LON = 150.66667, 150.83334, 151, 151.16667 |
---|
716 | # LAT = -34.5, -34.33333, -34.16667, -34 ; |
---|
717 | # ELEVATION = [-1 -2 -3 -4 |
---|
718 | # -5 -6 -7 -8 |
---|
719 | # ... |
---|
720 | # ... -16] |
---|
721 | # where the top left corner is -1m, |
---|
722 | # and the ll corner is -13.0m |
---|
723 | # |
---|
724 | # First value (index=0) in small_ha.nc is 0.3400644 cm, |
---|
725 | # Fourth value (index==3) is -6.50198 cm |
---|
726 | |
---|
727 | |
---|
728 | from coordinate_transforms.redfearn import redfearn |
---|
729 | |
---|
730 | fid1 = NetCDFFile('small_ha.nc') |
---|
731 | fid2 = NetCDFFile('small_e.nc') |
---|
732 | fid3 = NetCDFFile('small_va.nc') |
---|
733 | |
---|
734 | first_amp = fid1.variables['HA'][:][0,0,0] |
---|
735 | fourth_amp = fid1.variables['HA'][:][0,0,3] |
---|
736 | first_elevation = fid2.variables['ELEVATION'][0,0] |
---|
737 | fourth_elevation= fid2.variables['ELEVATION'][:][0,3] |
---|
738 | first_speed = fid3.variables['VA'][0,0] |
---|
739 | fourth_speed = fid3.variables['VA'][:][0,3] |
---|
740 | |
---|
741 | fid1.close() |
---|
742 | fid2.close() |
---|
743 | fid3.close() |
---|
744 | |
---|
745 | #Call conversion (with zero origin) |
---|
746 | ferret2sww('small', verbose=False, |
---|
747 | origin = (56, 0, 0)) |
---|
748 | |
---|
749 | |
---|
750 | #Work out the UTM coordinates for first point |
---|
751 | zone, e, n = redfearn(-34.5, 150.66667) |
---|
752 | #print zone, e, n |
---|
753 | |
---|
754 | #Read output file 'small.sww' |
---|
755 | fid = NetCDFFile('small.sww') |
---|
756 | |
---|
757 | x = fid.variables['x'][:] |
---|
758 | y = fid.variables['y'][:] |
---|
759 | |
---|
760 | |
---|
761 | #Check that first coordinate is correctly represented |
---|
762 | assert allclose(x[0], e) |
---|
763 | assert allclose(y[0], n) |
---|
764 | |
---|
765 | #Check first value |
---|
766 | stage = fid.variables['stage'][:] |
---|
767 | xmomentum = fid.variables['xmomentum'][:] |
---|
768 | ymomentum = fid.variables['ymomentum'][:] |
---|
769 | |
---|
770 | #print ymomentum |
---|
771 | first_height = first_amp/100 - first_elevation |
---|
772 | fourth_height = fourth_amp/100 - fourth_elevation |
---|
773 | |
---|
774 | first_momentum=first_speed*first_height/100 |
---|
775 | fourth_momentum=fourth_speed*fourth_height/100 |
---|
776 | |
---|
777 | assert allclose(ymomentum[0][0],first_momentum) #Meters |
---|
778 | assert allclose(ymomentum[0][1],fourth_momentum) #Meters |
---|
779 | |
---|
780 | fid.close() |
---|
781 | |
---|
782 | #Cleanup |
---|
783 | import os |
---|
784 | os.remove('small.sww') |
---|
785 | |
---|
786 | |
---|
787 | |
---|
788 | |
---|
789 | def test_sww_extent(self): |
---|
790 | """Not a test, rather a look at the sww format |
---|
791 | """ |
---|
792 | |
---|
793 | import time, os |
---|
794 | from Numeric import array, zeros, allclose, Float, concatenate |
---|
795 | from Scientific.IO.NetCDF import NetCDFFile |
---|
796 | |
---|
797 | self.domain.filename = 'datatest' + str(id(self)) |
---|
798 | self.domain.format = 'sww' |
---|
799 | self.domain.smooth = True |
---|
800 | self.domain.reduction = mean |
---|
801 | self.domain.set_datadir('.') |
---|
802 | |
---|
803 | |
---|
804 | sww = get_dataobject(self.domain) |
---|
805 | sww.store_connectivity() |
---|
806 | sww.store_timestep('stage') |
---|
807 | self.domain.time = 2. |
---|
808 | |
---|
809 | #Modify stage at second timestep |
---|
810 | stage = self.domain.quantities['stage'].vertex_values |
---|
811 | self.domain.set_quantity('stage', stage/2) |
---|
812 | |
---|
813 | sww.store_timestep('stage') |
---|
814 | |
---|
815 | file_and_extension_name = self.domain.filename + ".sww" |
---|
816 | #print "file_and_extension_name",file_and_extension_name |
---|
817 | [xmin, xmax, ymin, ymax, stagemin, stagemax] = \ |
---|
818 | extent_sww(file_and_extension_name ) |
---|
819 | |
---|
820 | assert allclose(xmin, 0.0) |
---|
821 | assert allclose(xmax, 1.0) |
---|
822 | assert allclose(ymin, 0.0) |
---|
823 | assert allclose(ymax, 1.0) |
---|
824 | assert allclose(stagemin, -0.85) |
---|
825 | assert allclose(stagemax, 0.15) |
---|
826 | |
---|
827 | |
---|
828 | #Cleanup |
---|
829 | os.remove(sww.filename) |
---|
830 | |
---|
831 | |
---|
832 | def test_ferret2sww_nz_origin(self): |
---|
833 | from Scientific.IO.NetCDF import NetCDFFile |
---|
834 | from coordinate_transforms.redfearn import redfearn |
---|
835 | |
---|
836 | #Call conversion (with nonzero origin) |
---|
837 | ferret2sww('small', verbose=False, |
---|
838 | origin = (56, 100000, 200000)) |
---|
839 | |
---|
840 | |
---|
841 | #Work out the UTM coordinates for first point |
---|
842 | zone, e, n = redfearn(-34.5, 150.66667) |
---|
843 | |
---|
844 | #Read output file 'small.sww' |
---|
845 | fid = NetCDFFile('small.sww', 'r') |
---|
846 | |
---|
847 | x = fid.variables['x'][:] |
---|
848 | y = fid.variables['y'][:] |
---|
849 | |
---|
850 | #Check that first coordinate is correctly represented |
---|
851 | assert allclose(x[0], e-100000) |
---|
852 | assert allclose(y[0], n-200000) |
---|
853 | |
---|
854 | fid.close() |
---|
855 | |
---|
856 | #Cleanup |
---|
857 | import os |
---|
858 | os.remove('small.sww') |
---|
859 | |
---|
860 | |
---|
861 | #------------------------------------------------------------- |
---|
862 | if __name__ == "__main__": |
---|
863 | suite = unittest.makeSuite(Test_Data_Manager,'test') |
---|
864 | runner = unittest.TextTestRunner() |
---|
865 | runner.run(suite) |
---|