import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from salishsea_tools import stormtools
import netCDF4 as nc
from salishsea_tools import (
nc_tools,
viz_tools,)
%matplotlib inline
2006 December 14 - 15
tracers = nc.Dataset('/data/nsoontie/MEOPAR/SalishSea/results/storm-surges/tide_fix/dec2006/all_forcing/1hour/SalishSea_1h_20061214_20061215_grid_T.nc')
timesteps = tracers.variables['time_counter']
bathy = nc.Dataset('/ocean/imachuca/MEOPAR/NEMO-forcing/grid/bathy_meter_SalishSea2.nc')
salvar = tracers.variables['vosaline']
salvar.shape
(48, 40, 898, 398)
Salinity at the surface (depth=0)
sal0 = salvar[:,0,:,:]
For colourbar:
cmin = sal0.min()
cmax = sal0.max()
#Setting up a blank figure
fig, ax = plt.subplots(1, 1, figsize=(10, 8))
viz_tools.set_aspect(ax)
cmap = plt.get_cmap('Blues')
viz_tools.plot_land_mask(ax, bathy, color='burlywood')
#Making an initial image i.e. our first ssh reading
def init():
sal = np.ma.masked_values(sal0[0], 0)
mesh = ax.pcolormesh(sal,cmap=cmap)
mesh.set_clim(cmin,cmax)
cbar = fig.colorbar(mesh)
cbar.set_label('{label} [{units}]'.format(label=salvar.long_name.title(), units=salvar.units))
ax.set_xlabel('x Index')
ax.set_ylabel('y Index')
ax.set_title('t = 0.0 h')
#The full range of images that will make up the animation
def animate(t):
ssh = np.ma.masked_values(sal0[t], 0)
mesh = ax.pcolormesh(ssh,cmap=cmap)
mesh.set_clim(cmin,cmax)
ax.set_xlabel('x Index')
ax.set_ylabel('y Index')
timestamp = nc_tools.timestamp(tracers,t)
ax.set_title(timestamp)
#The animation function
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=48, interval=300, blit=False, repeat=False)
#A line that makes it all work
mywriter = animation.FFMpegWriter()
#Save in current folder
anim.save('SalinityDec_Animation.mp4',writer=mywriter,fps=1)
#Show as a pop-up window
#plt.show()
#Setting up a blank figure
fig, ax = plt.subplots(1, 1, figsize=(10, 8))
viz_tools.set_aspect(ax)
cmap = plt.get_cmap('ocean_r')
viz_tools.plot_land_mask(ax, bathy, color='burlywood')
#Making an initial image i.e. our first ssh reading
def init():
sal = np.ma.masked_values(sal0[0], 0)
mesh = ax.pcolormesh(sal,cmap=cmap)
mesh.set_clim(cmin,cmax)
cbar = fig.colorbar(mesh)
cbar.set_label('{label} [{units}]'.format(label=salvar.long_name.title(), units=salvar.units))
ax.set_xlabel('x Index')
ax.set_ylabel('y Index')
ax.set_title('t = 0.0 h')
#The full range of images that will make up the animation
def animate(t):
ssh = np.ma.masked_values(sal0[t], 0)
mesh = ax.pcolormesh(ssh,cmap=cmap)
mesh.set_clim(cmin,cmax)
ax.set_xlabel('x Index')
ax.set_ylabel('y Index')
timestamp = nc_tools.timestamp(tracers,t)
ax.set_title(timestamp)
#The animation function
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=48, interval=300, blit=False, repeat=False)
#A line that makes it all work
mywriter = animation.FFMpegWriter()
#Save in current folder
anim.save('SalinityDec2_Animation.mp4',writer=mywriter,fps=1)
#Show as a pop-up window
#plt.show()