Tutorial showing how to create Parcels in Agulhas animated gif¶

This brief tutorial shows how to recreate the animated gif showing particles in the Agulhas region south of Africa.

In [1]:
from parcels import FieldSet, ParticleSet, JITParticle, AdvectionRK4, ErrorCode
from datetime import timedelta
import numpy as np


Now load the Globcurrent fields from the GlobCurrent_example_data directory (note that unlike in the main Parcels tutorial we don't use a dictionary for the filenames here; as they are the same for all variables, we don't need to)

In [2]:
filenames = "GlobCurrent_example_data/20*.nc"
variables = {'U': 'eastward_eulerian_current_velocity',
'V': 'northward_eulerian_current_velocity'}
dimensions = {'lat': 'lat',
'lon': 'lon',
'time': 'time'}
fieldset = FieldSet.from_netcdf(filenames, variables, dimensions)

WARNING: Casting lon data to np.float32
WARNING: Casting lat data to np.float32
WARNING: Casting depth data to np.float32


Now create vectors of Longitude and Latitude starting locations on a regular mesh, and use these to initialise a ParticleSet object.

In [3]:
lons, lats = np.meshgrid(range(15, 35), range(-40, -30))
pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=lons, lat=lats)


Now we want to advect the particles. However, the Globcurrent data that we loaded in is only for a limited, regional domain and particles might be able to leave this domain. We therefore need to tell Parcels that particles that leave the domain need to be deleted. We do that using a Recovery Kernel, which will be invoked when a particle encounters an ErrorOutOfBounds error:

In [4]:
def DeleteParticle(particle, fieldset, time, dt):
particle.delete()


Now we can advect the particles. Note that we do this inside a for-loop, so we can save a plot every six hours (which is the value of runtime). See the plotting tutorial for more information on the pset.show() method.

In [5]:
for cnt in range(3):
# First plot the particles
pset.show(savefile='particles'+str(cnt).zfill(2), field='vector', land=True, vmax=2.0)

# Then advect the particles for 6 hours
runtime=timedelta(hours=6),  # runtime controls the interval of the plots
dt=timedelta(minutes=5),
recovery={ErrorCode.ErrorOutOfBounds: DeleteParticle})  # the recovery kernel

/Users/erik/miniconda2/envs/py2_parcels_cartopy/lib/python2.7/site-packages/cartopy/mpl/geoaxes.py:1744: RuntimeWarning: invalid value encountered in less
u, v = self.projection.transform_vectors(t, x, y, u, v)
/Users/erik/miniconda2/envs/py2_parcels_cartopy/lib/python2.7/site-packages/cartopy/mpl/geoaxes.py:1744: RuntimeWarning: invalid value encountered in greater
u, v = self.projection.transform_vectors(t, x, y, u, v)
INFO: Plot saved to particles00.png

This now has created 3 plots. Note that the original animated gif contained 20 plots, but to keep running of this notebook fast we have reduced the number here. Of course, it is trivial to increase the number of plots by changing the value in the range() in the cell above.