1 | from Numeric import array, Float, zeros |
---|
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 = array(fin.variables['x'], Float) |
---|
39 | y = array(fin.variables['y'], Float) |
---|
40 | if dynamic is True: |
---|
41 | q = array(fin.variables[quantityName][self.frame_number], Float) |
---|
42 | else: |
---|
43 | q = array(fin.variables[quantityName], Float) |
---|
44 | |
---|
45 | q *= self.height_zScales[quantityName] |
---|
46 | q += self.height_offset[quantityName] |
---|
47 | |
---|
48 | for v in range(N_vert): |
---|
49 | points.InsertNextPoint(x[v], y[v], q[v]) |
---|
50 | polydata = self.vtk_polyData[quantityName] = vtkPolyData() |
---|
51 | polydata.SetPoints(points) |
---|
52 | polydata.SetPolys(self.vtk_cells) |
---|
53 | else: |
---|
54 | self.height_quantities.remove(quantityName) |
---|
55 | fin.close() |
---|
56 | |
---|
57 | def setup_gui(self): |
---|
58 | Visualiser.setup_gui(self) |
---|
59 | self.tk_renderWidget.grid(row=0, column=0, columnspan=6) |
---|
60 | self.tk_quit.grid(row=2, column=0, columnspan=6, sticky=W+E) |
---|
61 | self.tk_restart = Button(self.tk_root, text="<<<", command=self.restart) |
---|
62 | self.tk_restart.grid(row=1, column=0, sticky=W+E) |
---|
63 | self.tk_back10 = Button(self.tk_root, text="<<", command=self.back10) |
---|
64 | self.tk_back10.grid(row=1, column=1, sticky=W+E) |
---|
65 | self.tk_back = Button(self.tk_root, text="<", command=self.back) |
---|
66 | self.tk_back.grid(row=1, column=2, sticky=W+E) |
---|
67 | self.tk_pauseResume = Button(self.tk_root, text="Pause", command=self.pauseResume) |
---|
68 | self.tk_pauseResume.grid(row=1, column=3, sticky=W+E) |
---|
69 | self.tk_forward = Button(self.tk_root, text=">", command=self.forward) |
---|
70 | self.tk_forward.grid(row=1, column=4, sticky=W+E) |
---|
71 | self.tk_forward10 = Button(self.tk_root, text=">>", command=self.forward10) |
---|
72 | self.tk_forward10.grid(row=1, column=5, sticky=W+E) |
---|
73 | |
---|
74 | def restart(self): |
---|
75 | self.frame_number = 0 |
---|
76 | self.redraw_quantities(True) |
---|
77 | |
---|
78 | def back10(self): |
---|
79 | if self.frame_number - 10 >= 0: |
---|
80 | self.frame_number -= 10 |
---|
81 | else: |
---|
82 | self.frame_number = 0 |
---|
83 | self.redraw_quantities(True) |
---|
84 | |
---|
85 | def back(self): |
---|
86 | if self.frame_number > 0: |
---|
87 | self.frame_number -= 1 |
---|
88 | self.redraw_quantities(True) |
---|
89 | |
---|
90 | def pauseResume(self): |
---|
91 | print "Pause/Resume" |
---|
92 | |
---|
93 | def forward(self): |
---|
94 | if self.frame_number < self.max_frame_number: |
---|
95 | self.frame_number += 1 |
---|
96 | self.redraw_quantities(True) |
---|
97 | |
---|
98 | def forward10(self): |
---|
99 | if self.frame_number + 10 <= self.max_frame_number: |
---|
100 | self.frame_number += 10 |
---|
101 | else: |
---|
102 | self.frame_number = self.max_frame_number |
---|
103 | self.redraw_quantities(True) |
---|