1 | #!/usr/bin/env python3 |
---|
2 | |
---|
3 | import matplotlib |
---|
4 | import sys |
---|
5 | from matplotlib import colors, ticker, cm |
---|
6 | from matplotlib.backends.backend_pdf import PdfPages |
---|
7 | from netCDF4 import Dataset |
---|
8 | import matplotlib.pyplot as plt |
---|
9 | import numpy as np |
---|
10 | import math |
---|
11 | from matplotlib import animation |
---|
12 | import matplotlib.gridspec as gridspec |
---|
13 | from matplotlib.lines import Line2D |
---|
14 | from matplotlib.collections import PatchCollection |
---|
15 | from matplotlib.patches import Polygon |
---|
16 | matplotlib.rcParams['text.usetex'] = True |
---|
17 | |
---|
18 | |
---|
19 | input_file_name = './corners_agt.059.nc' |
---|
20 | |
---|
21 | file_content = Dataset(input_file_name, mode='r') # load entire netCDF file |
---|
22 | print(file_content) |
---|
23 | time = file_content.variables['time'][:] # extract variable |
---|
24 | agx = file_content.variables['ag_x'][:,:] |
---|
25 | agy = file_content.variables['ag_y'][:,:] |
---|
26 | group = file_content.variables['ag_group'][:,:] |
---|
27 | file_content.close() # close netCDF file |
---|
28 | |
---|
29 | input_file_name = './topo.txt' |
---|
30 | f = open(input_file_name, mode='r') |
---|
31 | data = [] |
---|
32 | building_id = [] |
---|
33 | vertex_id = [] |
---|
34 | polygon = [] |
---|
35 | gons = [] |
---|
36 | p_last = 1 |
---|
37 | for line in f: |
---|
38 | line = line.strip() |
---|
39 | columns = line.split() |
---|
40 | if (int(columns[0]) > p_last): |
---|
41 | gons.append(polygon) |
---|
42 | polygon = [] |
---|
43 | polygon.append([float(columns[2]),float(columns[3])]) |
---|
44 | p_last = int(columns[0]) |
---|
45 | gons.append(polygon) |
---|
46 | nop = int(columns[0]) |
---|
47 | patches = [] |
---|
48 | |
---|
49 | for i in range(nop): |
---|
50 | poly = Polygon(gons[i-1], True) |
---|
51 | patches.append(poly) |
---|
52 | p = PatchCollection(patches,facecolors='grey') |
---|
53 | |
---|
54 | input_file_name = './topo_nav.txt' |
---|
55 | f = open(input_file_name, mode='r') |
---|
56 | t_x = [] |
---|
57 | t_y = [] |
---|
58 | |
---|
59 | l_count = 0 |
---|
60 | for line in f: |
---|
61 | l_count = l_count + 1 |
---|
62 | line = line.strip() |
---|
63 | columns_t = line.split() |
---|
64 | t_x.append(columns_t[2]) |
---|
65 | t_y.append(columns_t[3]) |
---|
66 | |
---|
67 | Nt = np.shape(time)[0] |
---|
68 | print(Nt) |
---|
69 | Nt = Nt -1 |
---|
70 | Nags = np.shape(agx[1,:]) |
---|
71 | Nx = 20 |
---|
72 | Ny = 20 |
---|
73 | |
---|
74 | x = np.linspace(0.0, 20.0, Nx) |
---|
75 | y = np.linspace(0.0, 20.0, Ny) |
---|
76 | X, Y = np.meshgrid(x, y) |
---|
77 | |
---|
78 | fig,ax = plt.subplots() |
---|
79 | fig.set_size_inches(7., 7.) |
---|
80 | xrange = [0, 20] |
---|
81 | yrange = [0, 20] |
---|
82 | colors = ['yellow','red','blue'] |
---|
83 | |
---|
84 | |
---|
85 | def animate(i): |
---|
86 | ax.clear() |
---|
87 | ax.add_collection(p) |
---|
88 | x = agx[i,:] |
---|
89 | y = agy[i,:] |
---|
90 | ax.set_xlim(*xrange) |
---|
91 | ax.set_ylim(*yrange) |
---|
92 | ax.scatter(x,y,s=3,c=group[i,:]) |
---|
93 | ax.scatter(t_x,t_y,s=1,c='red') |
---|
94 | plt.title(r'simulated time: %i s' % time[i]) |
---|
95 | k = i/Nt*100. |
---|
96 | print("Done: %6.2f percent\r" % k, file=sys.stdout, end=" ") |
---|
97 | return ax, |
---|
98 | |
---|
99 | anim = animation.FuncAnimation(fig, animate, frames=Nt, interval=60, blit=False,save_count=0) |
---|
100 | anim.save('ags.mp4', dpi=200, writer='ffmpeg') |
---|
101 | #anim.save('basic_animation.gif', fps=1, extra_args=['-vcodec', 'libx264']) |
---|
102 | |
---|
103 | #plt.show() |
---|