import netCDF4 as nc
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
from salishsea_tools import (nc_tools)
%matplotlib inline
Download netCDF4? http://netcdf4-python.googlecode.com/svn/trunk/docs/netCDF4-module.html.
Need plt to plot
Need Axes3D to plot in 3d
Need animation to produce an animation
Download salishsea_tools? Is there an easier way to view variables in dataset?
Here, I'm using matplotlib inline, assuming that they will have downloaded FFMpegWriter.
data = nc.Dataset('/ocean/imachuca/MEOPAR/Ariane/results/soft_carpentry/ariane_trajectories_qualitative.nc','r');
Relocate data and adjust directory
nc_tools.show_variables(data)
[u'init_x', u'init_y', u'init_z', u'init_t', u'init_age', u'init_transp', u'final_x', u'final_y', u'final_z', u'final_t', u'final_age', u'final_transp', u'traj_lon', u'traj_lat', u'traj_depth', u'traj_time']
lon=data.variables['traj_lon']
lat=data.variables['traj_lat']
dep=data.variables['traj_depth']
x=data.variables['init_x']
y=data.variables['init_y']
lon.shape
(10, 1)
It is important to know the shape or length for when you reach the animation part. For example, for our animations, we will have 10 frames.
fig, ax = plt.subplots(1,1,figsize=(20,10))
ax.scatter(lon[:,:],lat[:,:],color='blue')
ax.set_title('2D')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_xlim([-124.6,-123.0])
ax.set_ylim([48.25,48.65])
(48.25, 48.65)
fig = plt.figure(figsize=(20,10))
ax = fig.gca(projection='3d')
ax.scatter(lon[:,:],lat[:,:],dep[:,0],color='blue')
ax.set_title('3D')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Depth')
ax.set_xlim([-124.6,-123.0])
ax.set_ylim([48.25,48.65])
ax.set_zlim([-100,0])
(-100, 0)
fig = plt.figure(figsize=(20,10))
ax = fig.gca(projection='3d')
ax.scatter(lon[:,:],lat[:,:],dep[:,:],color='blue')
ax.set_title('3D')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Depth')
ax.set_xlim([-124.6,-123.0])
ax.set_ylim([48.25,48.65])
ax.set_zlim([-100,0])
ax.view_init(elev=0, azim=-90)
#Empty map
fig, ax = plt.subplots(1,1,figsize=(20,10))
ax.set_title('2D')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_xlim([-124.6,-123.0])
ax.set_ylim([48.25,48.65])
#Initial image
def init():
return [fig, ax]
#Animated points
def animate(p):
ax.scatter(lon[p,:],lat[p,:],color='blue')
#The animation function
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=10)
#A line that makes it all work
mywriter = animation.FFMpegWriter()
#Save in current folder
anim.save('Software-Carpentry2D.mp4',writer=mywriter)
#Show as a pop-up window
#plt.show()
#Empty map
fig = plt.figure(figsize=(20,10))
ax = fig.gca(projection='3d')
ax.set_title('3D')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Depth')
ax.set_xlim([-124.6,-123.0])
ax.set_ylim([48.25,48.65])
ax.set_zlim([-100,0])
ax.view_init(elev=0, azim=-90)
#Initial image
def init():
return [fig, ax]
#Animated points
def animate(p):
ax.scatter(lon[p,:],lat[p,:],dep[p,:],color='blue')
#The animation function
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=10)
#A line that makes it all work
mywriter = animation.FFMpegWriter()
#Save in current folder
anim.save('Software-Carpentry3D.mp4',writer=mywriter)
#Show as a pop-up window
#plt.show()