%matplotlib inline
from parcels import FieldSet, ParticleSet, JITParticle, AdvectionRK4
from datetime import timedelta, datetime

"""
Explanation: Parcels plotting methods
Disclaimer: This tutorial demonstrates the simple plotting functionality included in Parcels. For high quality analysis it is recommended to create your own code. This tutorial shows how to start a more advanced analysis. 
The show() method of the Particelset class is capable of plotting the particle locations and velocity fields in scalar and vector form. In this notebook, we demonstrate these capabilities using the GlobCurrent dataset. We begin by importing the relevant modules.
End of explanation
"""


filenames = {'U': "GlobCurrent_example_data/20*.nc",
             'V': "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)

"""
Explanation: We then instatiate a FieldSet with the velocity field data from GlobCurrent dataset.
End of explanation
"""


pset = ParticleSet.from_line(fieldset=fieldset, size=5, pclass=JITParticle,
                             start=(31, -31), finish=(32, -31), time=datetime(2002, 1, 1))

"""
Explanation: Next, we instantiate a ParticeSet composed of JITParticles:
End of explanation
"""


pset.show()

"""
Explanation: Given this ParticleSet, we can now explore the different features of the show() method. To start, let's simply call show() with no arguments.
End of explanation
"""


pset.execute(AdvectionRK4, runtime=timedelta(days=7), dt=timedelta(minutes=5))

"""
Explanation: Then, let's advect the particles starting on January 1, 2002 for a week.
End of explanation
"""


pset.show()

"""
Explanation: If we call show() again, we will see that the particles have been advected:
End of explanation
"""


pset.show(land=False)

"""
Explanation: To plot without the continents on the same plot, add land=False.
End of explanation
"""


pset.show(domain={'N':-31, 'S':-35, 'E':33, 'W':26})

"""
Explanation: To set the domain of the plot, we specify the domain argument. The format domain expects a dictionary with entries {'S', 'N', 'E', 'W'} for South, North, East and West extent, respectively. Note that the plotted domain is found by interpolating the user-specified domain onto the velocity grid. For instance,
End of explanation
"""


pset.show(field=fieldset.U)

"""
Explanation: We can also easily display a scalar contour plot of a single component of the velocity vector field. This is done by setting the field argument equal to the desired scalar velocity field.
End of explanation
"""


pset.show(field=fieldset.U, show_time=datetime(2002, 1, 10, 2))

"""
Explanation: To plot the scalar U velocity field at a different date and time, we set the argument show_time equal to a datetime or timedelta object or simply the number of seconds since the time origin. For instance, let's view the U field on January, 10, 2002 at 2 PM.
End of explanation
"""


pset.show(field=fieldset.U, show_time=datetime(2002, 1, 10, 2), with_particles=False)

"""
Explanation: Note that the particle locations do not change, but remain at the locations corresponding to  the end of the last integration. To remove them from the plot, we set the argument with_particles equal to False.
End of explanation
"""


pset.show(field='vector')

"""
Explanation: By setting the field argument equal to vector, we can display the velocity in full vector form.
End of explanation
"""


pset.show(field='vector', vmax=3.0, domain={'N':-31, 'S':-39, 'E':33, 'W':18})

"""
Explanation: The normalized vector field is colored by speed. To control the maximum speed value on the colorbar, set the vmax argument equal to the desired value.
End of explanation
"""


try:  # Within a try/pass for unit testing on machines without cartopy installed
    import cartopy
    pset.show(field='vector', vmax=3.0, domain={'N':-31, 'S':-39, 'E':33, 'W':18}, projection=cartopy.crs.Robinson())
except:
    pass

"""
Explanation: We can change the projection of the plot by providing one of the projections from cartopy. For example, to plot on a Robinson projection , we use projection=cartopy.crs.Robinson(). Note that not all projections support gridlines, so these may not be shown.
End of explanation
"""


pset.show(field='vector', vmax=3.0, domain={'N':-31, 'S':-39, 'E':33, 'W':18}, land=True, savefile='particles')

"""
Explanation: If we want to save the file rather than show it, we set the argument savefile equal to the 'path/to/save/file'.
End of explanation
"""


