forked from pvthinker/wave2d
/
script_read_netcdf.py
51 lines (39 loc) · 1.23 KB
/
script_read_netcdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import xarray as xr
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
file = 'history.nc'
variable = 'p'
xmin, xmax = 0, 4
ymin, ymax = 0, 2
ds = xr.open_dataset(file)
# This part is a fast adaptation of the window shape
ny, nx = ds[variable][0].shape
if nx == ny:
shape_window = (8,8)
else:
shape_window = (12,5)
fps = 10 #Set up the number of image / second (animation speed)
snapshots = ds[variable]
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure( figsize=shape_window )
a = snapshots[0]
im = plt.imshow(a.T, cmap='coolwarm', extent=[xmin,xmax,ymin,ymax])
plt.xlabel('X')
plt.ylabel('Y')
plt.clim(-1,1) #Set up here the limits of colorbar
plt.colorbar(label=variable)
def animate_func(i):
if i % fps == 0:
print( '.', end ='' )
im.set_array(snapshots[i])
return [im]
anim = animation.FuncAnimation(
fig,
animate_func,
frames = len(ds[variable]),
interval = 1000 / fps, # in ms
)
anim.save('animation.gif', writer='PillowWriter')
print('Done!')