<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 A First Look at Cartopy (for Iris)
Course aims and objectives
The aim of the cartopy course is to introduce the cartopy library and highlight of some of its features.
The learning outcomes of the cartopy course are as follows. By the end of the course, you will be able to
Step1:  Cartopy's matplotlib interface is set up via the projection keyword when constructing a matplotlib Axes / SubAxes instance. The resulting axes instance has new methods, such as the coastlines() method, which are specific to drawing cartographic data
Step2:  A full list of Cartopy projections is available at http
Step3:  <b><font color="brown">SAMPLE SOLUTION
Step4:  Let's compare the Plate Carree projection to another projection from the projection list; being the Orthographic projection. We'll do that by plotting two subplots next to each other at the same time
Step5:  Transforming data
To draw cartographic data, we use the standard matplotlib plotting routines with an additional transform keyword argument. The value of the transform argument should be the cartopy coordinate reference system of the data being plotted.
First let's plot a line on a PlateCarree projection.
Step6:  Now let's try plotting the same line on an EquidistantConic projection.
Step7:  The above plot is not what we intended.
We have set up the axes to be in the Equidistant Conic projection, but we have not told Cartopy that the coordinates of the line are "in PlateCarree".
To do this, we use the transform keyword in the plt.plot function 
Step8:  Notice that the plotted line is bent 
Step9:  <div class="alert alert-block alert-warning">
    <b><font color='brown'>Suggested Activity 
Step10:  <div class="alert alert-block alert-warning">
    <b><font color='brown'>Suggested Activity 
Step11:  Adding features
We can add features from the Natural Earth database to maps we produce. Natural Earth datasets are downloaded and cached when they are first used, so you need an internet connection to use them, and you may encounter warnings when you first download them.
We add features to maps via the cartopy feature interface.
For example, let's add political borders to a map
Step12:  We can add graticule lines and tick labels to the map using the gridlines method (this currently is limited to just a few coordinate reference systems)
Step13:  We can control the specific tick values by using matplotlib's locator object, and the formatting can be controlled with matplotlib formatters
Step14:  Cartopy cannot currently label all types of projection, though more work is intended on this functionality in the future.
Exercise
The following snippet of code produces coordinate arrays and some data in a rotated pole coordinate system. The coordinate system for the x and y values, which is similar to that found in the some limited area models of Europe, has a projection "north pole" at 193.0 degrees longitude and 41.0 degrees latitude.
<ASSISTANT_TASK:>
Python Code:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
Explanation: A First Look at Cartopy (for Iris)
Course aims and objectives
The aim of the cartopy course is to introduce the cartopy library and highlight of some of its features.
The learning outcomes of the cartopy course are as follows. By the end of the course, you will be able to:
understand the functionality provided by the cartopy.crs module,
explain the purpose and usage of the projection and transform keyword arguments, and
add geolocated data, grid lines and Natural Earth features to a cartopy map.
Introduction: maps and projections
Cartopy is a Python package that provides easy creation of maps, using matplotlib, for the analysis and visualisation of geospatial data.
In order to create a map with cartopy and matplotlib, we typically need to import pyplot from matplotlib and cartopy's crs (coordinate reference system) submodule. These are typically imported as follows:
End of explanation
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.show()
Explanation: Cartopy's matplotlib interface is set up via the projection keyword when constructing a matplotlib Axes / SubAxes instance. The resulting axes instance has new methods, such as the coastlines() method, which are specific to drawing cartographic data:
End of explanation
#
# EDIT for user code ...
#
Explanation: A full list of Cartopy projections is available at http://scitools.org.uk/cartopy/docs/latest/crs/projections.html.
<div class="alert alert-block alert-warning">
    <b><font color='brown'>Suggested Activity :</font></b> Try modifying the above code to plot a different projection.
</div>
End of explanation
# %load solutions/cartopy_exercise_1
Explanation: <b><font color="brown">SAMPLE SOLUTION:</font></b>
Un-comment and execute the following, to view a possible code solution.
Then run it ...
End of explanation
# Make sure the figure is a decent size when plotted.
fig = plt.figure(figsize=(14, 7))
# Left plot.
ax1 = fig.add_subplot(1, 2, 1, projection=ccrs.PlateCarree())
ax1.coastlines()
# Right plot.
ax2 = fig.add_subplot(1, 2, 2, projection=ccrs.Orthographic())
ax2.coastlines()
# Show both subplots on the same figure.
plt.show()
Explanation: Let's compare the Plate Carree projection to another projection from the projection list; being the Orthographic projection. We'll do that by plotting two subplots next to each other at the same time:
End of explanation
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
x0, y0 = -50, -30
x1, y1 = 10, 55
plt.plot([x0, x1], [y0, y1], linewidth=4)
plt.show()
Explanation: Transforming data
To draw cartographic data, we use the standard matplotlib plotting routines with an additional transform keyword argument. The value of the transform argument should be the cartopy coordinate reference system of the data being plotted.
First let's plot a line on a PlateCarree projection.
End of explanation
proj = ccrs.EquidistantConic()
ax = plt.axes(projection=proj)
ax.coastlines()
plt.plot([x0, x1], [y0, y1], linewidth=4)
plt.show()
Explanation: Now let's try plotting the same line on an EquidistantConic projection.
End of explanation
ax = plt.axes(projection=proj)
ax.coastlines()
plt.plot([x0, x1], [y0, y1], linewidth=4, transform=ccrs.PlateCarree())
plt.show()
Explanation: The above plot is not what we intended.
We have set up the axes to be in the Equidistant Conic projection, but we have not told Cartopy that the coordinates of the line are "in PlateCarree".
To do this, we use the transform keyword in the plt.plot function :
End of explanation
ax = plt.axes(projection=proj)
ax.coastlines()
ax.set_global()
plt.plot([x0, x1], [y0, y1], linewidth=4, transform=ccrs.PlateCarree())
plt.show()
Explanation: Notice that the plotted line is bent :  It is a straight line in the coordinate system it is defined in, so that makes it a curved line on this map.
Also note that, unless we specify a map extent, the map zooms to contain just the plotted data.
A very simple alternative to that is to plot the 'full map', by calling the set_global method on the Axes, as in this case :
End of explanation
#
# edit space for user code ...
#
# SAMPLE SOLUTION
# %load solutions/cartopy_exercise_2
Explanation: <div class="alert alert-block alert-warning">
    <b><font color='brown'>Suggested Activity :</font></b> Try re-plotting the "failed plot" above, but adding a "set_global" call to show the full map extent.
What does this tell you about what was actually being plotted in that case ?
</div>
End of explanation
#
# edit space for user code ...
#
# SAMPLE SOLUTION
# %load solutions/cartopy_exercise_3
Explanation: <div class="alert alert-block alert-warning">
    <b><font color='brown'>Suggested Activity :</font></b> Try taking more control of the plot region,  
using the <a href="https://scitools.org.uk/cartopy/docs/latest/matplotlib/geoaxes.html?highlight=set_extent#cartopy.mpl.geoaxes.GeoAxes.set_extent"> Geoaxes.set_extents</a> method. <br>
( look this up ! )
What is the _coordinate system_ of the coordinate values which you pass into this method ?
</div>
End of explanation
import cartopy.feature as cfeat
fig = plt.figure(figsize=(14, 7))
ax = plt.axes(projection=ccrs.Miller())
ax.coastlines('50m')
# ax.add_feature(cfeat.BORDERS, edgecolor='b')
political_bdrys = cfeat.NaturalEarthFeature(category='cultural',
                                            name='admin_0_countries',
                                            scale='50m')
ax.add_feature(political_bdrys,
               edgecolor='b', facecolor='none',
               linestyle='--', zorder=-1)
plt.show()
Explanation: Adding features
We can add features from the Natural Earth database to maps we produce. Natural Earth datasets are downloaded and cached when they are first used, so you need an internet connection to use them, and you may encounter warnings when you first download them.
We add features to maps via the cartopy feature interface.
For example, let's add political borders to a map:
End of explanation
ax = plt.axes(projection=ccrs.Mercator())
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
plt.show()
Explanation: We can add graticule lines and tick labels to the map using the gridlines method (this currently is limited to just a few coordinate reference systems):
End of explanation
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LATITUDE_FORMATTER
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
gl.yformatter = LATITUDE_FORMATTER
plt.show()
Explanation: We can control the specific tick values by using matplotlib's locator object, and the formatting can be controlled with matplotlib formatters:
End of explanation
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
x = np.linspace(337, 377, 25)
y = np.linspace(-18.7, 25.3, 35)
x2d, y2d = np.meshgrid(x, y)
data = np.cos(np.deg2rad(y2d) * 4) + np.sin(np.deg2rad(x2d) * 4)
Explanation: Cartopy cannot currently label all types of projection, though more work is intended on this functionality in the future.
Exercise
The following snippet of code produces coordinate arrays and some data in a rotated pole coordinate system. The coordinate system for the x and y values, which is similar to that found in the some limited area models of Europe, has a projection "north pole" at 193.0 degrees longitude and 41.0 degrees latitude.
End of explanation
<END_TASK>