1 | from Numeric import zeros, Float |
---|
2 | from Scientific.IO.NetCDF import NetCDFFile |
---|
3 | from Tkinter import Button, E, W |
---|
4 | from visualiser import Visualiser |
---|
5 | from vtk import vtkCellArray, vtkPoints, vtkPolyData |
---|
6 | |
---|
7 | class OfflineVisualiser(Visualiser): |
---|
8 | """A VTK-powered offline visualiser which runs in its own thread. |
---|
9 | """ |
---|
10 | def __init__(self, source): |
---|
11 | """The source parameter is assumed to be a NetCDF sww file. |
---|
12 | """ |
---|
13 | Visualiser.__init__(self, source) |
---|
14 | |
---|
15 | self.frame_number = 0 |
---|
16 | fin = NetCDFFile(self.source, 'r') |
---|
17 | self.max_frame_number = fin.variables['time'].shape[0] - 1 |
---|
18 | fin.close() |
---|
19 | |
---|
20 | def setup_grid(self): |
---|
21 | fin = NetCDFFile(self.source, 'r') |
---|
22 | self.vtk_cells = vtkCellArray() |
---|
23 | N_tri = fin.variables['volumes'].shape[0] |
---|
24 | for v in range(N_tri): |
---|
25 | self.vtk_cells.InsertNextCell(3) |
---|
26 | for i in range(3): |
---|
27 | self.vtk_cells.InsertCellPoint(fin.variables['volumes'][v][i]) |
---|
28 | fin.close() |
---|
29 | |
---|
30 | def update_height_quantity(self, quantityName, dynamic=True): |
---|
31 | fin = NetCDFFile(self.source, 'r') |
---|
32 | if(fin.variables.has_key(quantityName)): |
---|
33 | points = vtkPoints() |
---|
34 | if dynamic: |
---|
35 | N_vert = fin.variables[quantityName].shape[1] |
---|
36 | else: |
---|
37 | N_vert = len(fin.variables[quantityName]) |
---|
38 | x = fin.variables['x'] |
---|
39 | y = fin.variables['y'] |
---|
40 | q = fin.variables[quantityName] |
---|
41 | scale = self.height_zScales[quantityName] |
---|
42 | off = self.height_zScales[quantityName] |
---|
43 | if dynamic is True: |
---|
44 | for v in range(N_vert): |
---|
45 | points.InsertNextPoint(x[v], |
---|
46 | y[v], |
---|
47 | q[self.frame_number][v] \ |
---|
48 | * scale \ |
---|
49 | + off) |
---|
50 | else: |
---|
51 | for v in range(N_vert): |
---|
52 | points.InsertNextPoint(x[v], |
---|
53 | y[v], |
---|
54 | q[v] \ |
---|
55 | * scale \ |
---|
56 | + off) |
---|
57 | polydata = self.vtk_polyData[quantityName] = vtkPolyData() |
---|
58 | polydata.SetPoints(points) |
---|
59 | polydata.SetPolys(self.vtk_cells) |
---|
60 | else: |
---|
61 | self.height_quantities.remove(quantityName) |
---|
62 | fin.close() |
---|
63 | |
---|
64 | def setup_gui(self): |
---|
65 | Visualiser.setup_gui(self) |
---|
66 | self.tk_renderWidget.grid(row=0, column=0, columnspan=6) |
---|
67 | self.tk_quit.grid(row=2, column=0, columnspan=6, sticky=W+E) |
---|
68 | self.tk_restart = Button(self.tk_root, text="<<<", command=self.restart) |
---|
69 | self.tk_restart.grid(row=1, column=0, sticky=W+E) |
---|
70 | self.tk_back10 = Button(self.tk_root, text="<<", command=self.back10) |
---|
71 | self.tk_back10.grid(row=1, column=1, sticky=W+E) |
---|
72 | self.tk_back = Button(self.tk_root, text="<", command=self.back) |
---|
73 | self.tk_back.grid(row=1, column=2, sticky=W+E) |
---|
74 | self.tk_pauseResume = Button(self.tk_root, text="Pause", command=self.pauseResume) |
---|
75 | self.tk_pauseResume.grid(row=1, column=3, sticky=W+E) |
---|
76 | self.tk_forward = Button(self.tk_root, text=">", command=self.forward) |
---|
77 | self.tk_forward.grid(row=1, column=4, sticky=W+E) |
---|
78 | self.tk_forward10 = Button(self.tk_root, text=">>", command=self.forward10) |
---|
79 | self.tk_forward10.grid(row=1, column=5, sticky=W+E) |
---|
80 | |
---|
81 | def restart(self): |
---|
82 | self.frame_number = 0 |
---|
83 | self.redraw_quantities(True) |
---|
84 | |
---|
85 | def back10(self): |
---|
86 | if self.frame_number - 10 >= 0: |
---|
87 | self.frame_number -= 10 |
---|
88 | else: |
---|
89 | self.frame_number = 0 |
---|
90 | self.redraw_quantities(True) |
---|
91 | |
---|
92 | def back(self): |
---|
93 | if self.frame_number > 0: |
---|
94 | self.frame_number -= 1 |
---|
95 | self.redraw_quantities(True) |
---|
96 | |
---|
97 | def pauseResume(self): |
---|
98 | print "Pause/Resume" |
---|
99 | |
---|
100 | def forward(self): |
---|
101 | if self.frame_number < self.max_frame_number: |
---|
102 | self.frame_number += 1 |
---|
103 | self.redraw_quantities(True) |
---|
104 | |
---|
105 | def forward10(self): |
---|
106 | if self.frame_number + 10 <= self.max_frame_number: |
---|
107 | self.frame_number += 10 |
---|
108 | else: |
---|
109 | self.frame_number = self.max_frame_number |
---|
110 | self.redraw_quantities(True) |
---|