from matplotlib import pylab
import matplotlib.pyplot as plt
import netCDF4 as nc
import numpy as np
from salishsea_tools import tidetools
import seaborn as sns
from salishsea_tools import (nc_tools,viz_tools)
from salishsea_tools.nowcast import analyze, research_VENUS
from matplotlib import animation
import datetime
%matplotlib inline
sns.set_style('whitegrid')
Define date range
start = datetime.datetime(2015,7,1)
end = datetime.datetime(2015,7,8)
numdays = (end-start).days
dates = [start + datetime.timedelta(days=num)
for num in range(0, numdays+1)]
results_home = '/data/dlatorne/MEOPAR/SalishSea/nowcast/'
bathy = nc.Dataset('/data/nsoontie/MEOPAR/NEMO-forcing/grid/bathy_meter_SalishSea2.nc')
mesh = nc.Dataset('/data/nsoontie/MEOPAR/NEMO-forcing/grid/mesh_mask_SalishSea2.nc')
Load files over date range
#subset
ist=200; ien=350
jst=300; jen=500;
x=np.arange(ist,ien)
y=np.arange(jst,jen)
k=np.arange(0,40)
files = analyze.get_filenames(start,end,'1h','grid_U', results_home)
u, times = analyze.combine_files(files,'vozocrtx',k,y,x )
u=np.ma.masked_values(u,0)
e3u = mesh.variables['e3u'][0,:,y,x]
umask = mesh.variables['umask'][0,:,y,x]
depav_u = analyze.depth_average_mask(u,e3u,umask, 1)
print(u.shape)
depav_u=np.expand_dims(depav_u,1)
ubc = u-depav_u
del u, depav_u
(192, 40, 200, 150)
files = analyze.get_filenames(start,end,'1h','grid_V', results_home)
v, times = analyze.combine_files(files,'vomecrty',k,y,x )
e3v = mesh.variables['e3v'][0,:,y,x]
vmask = mesh.variables['vmask'][0,:,y,x]
depav_v = analyze.depth_average_mask(v,e3v,vmask, 1)
v=np.ma.masked_values(v,0)
print(v.shape)
depav_v=np.expand_dims(depav_v,1)
vbc = v-depav_v
del v, depav_v
(192, 40, 200, 150)
ubc, vbc = viz_tools.unstagger(ubc, vbc)
ke = 0.5*(ubc**2 +vbc**2)
SITES = research_VENUS.SITES
print(SITES)
fig,ax=plt.subplots(1,1,figsize=(10,10))
viz_tools.plot_coastline(ax,bathy)
ax.plot(SITES['VENUS']['East']['i'], SITES['VENUS']['East']['j'], 'o')
ax.plot(SITES['VENUS']['Central']['i'], SITES['VENUS']['Central']['j'],'o')
{'VENUS': {'Central': {'lon': -123.4261, 'i': 266, 'lat': 49.0401, 'depth': 300, 'j': 424}, 'East': {'lon': -123.3176, 'i': 283, 'lat': 49.0419, 'depth': 170, 'j': 416}, 'ddl': {'lon': -123.3400617, 'i': 284, 'lat': 49.0807167, 'depth': 150, 'j': 425}}, 'Vancouver': {'lon': -123.1207, 'lat': 49.2827}}
[<matplotlib.lines.Line2D at 0x7fc83de6c9e8>]
Testing out the size and colors before animating
def internal_tide(t):
ax1.clear()
#mesh
m=ax1.contourf(ke[t,0,:,:],np.arange(0,.31,.01),cmap=cmap)
#title and axxis
timestamp = times[t]
ax1.set_title(timestamp.strftime('%d-%b-%Y %H:%M'))
ax1.set_xlabel('x-postion')
ax1.set_ylabel('y-position')
return m
sns.set_context("notebook", rc={"lines.linewidth": .5})
print(ke.shape)
(192, 40, 199, 149)
smin, smax, dels = 0, 34, 1
cs = np.arange(smin,smax)
cmap=plt.get_cmap('hot')
st=5
fig= plt.figure( figsize=(5, 5))
ax1=fig.add_subplot(1,1,1)
t=0
m = internal_tide(0)
cbar = plt.colorbar(m, ax=ax1)
cbar.set_label('Baroclinc ke (m^2/s^2)')
#Setting up first frame and static content
fig= plt.figure( figsize=(5, 5))
ax1=fig.add_subplot(1,1,1)
mesh = internal_tide(0)
cbar = plt.colorbar(mesh, ax=ax1)
cbar.set_label('Baroclinc ke (m^2/s^2)')
#frmaes
framess=np.arange(1,ubc.shape[0])
#The animation function
anim = animation.FuncAnimation(fig, internal_tide,frames=framess, blit=True, repeat=False)
#A line that makes it all work
mywriter = animation.FFMpegWriter( fps=3, bitrate=10000)
#Save in current folder
anim.save('internal_wave_surface_ke_jul2015.mp4',writer=mywriter)