2021-06-01 21:35:01 +00:00
|
|
|
=================
|
|
|
|
Grids and Cartopy
|
|
|
|
=================
|
|
|
|
`Notebook <http://nbviewer.ipython.org/github/Unidata/python-awips/blob/master/examples/notebooks/Grids_and_Cartopy.ipynb>`_
|
2020-09-09 20:02:35 +00:00
|
|
|
A simple example of requesting and plotting AWIPS grids with Matplotlib
|
|
|
|
and Cartopy.
|
|
|
|
|
|
|
|
.. code:: ipython3
|
|
|
|
|
|
|
|
from awips.dataaccess import DataAccessLayer
|
|
|
|
import cartopy.crs as ccrs
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
|
|
|
|
%matplotlib inline
|
|
|
|
|
|
|
|
DataAccessLayer.changeEDEXHost("edex-cloud.unidata.ucar.edu")
|
|
|
|
request = DataAccessLayer.newDataRequest()
|
|
|
|
request.setDatatype("grid")
|
|
|
|
request.setLocationNames("RAP13")
|
|
|
|
request.setParameters("T")
|
|
|
|
request.setLevels("2.0FHAG")
|
|
|
|
cycles = DataAccessLayer.getAvailableTimes(request, True)
|
|
|
|
times = DataAccessLayer.getAvailableTimes(request)
|
|
|
|
fcstRun = DataAccessLayer.getForecastRun(cycles[-1], times)
|
|
|
|
response = DataAccessLayer.getGridData(request, [fcstRun[0]])
|
|
|
|
grid = response[0]
|
|
|
|
data = grid.getRawData()
|
|
|
|
lons, lats = grid.getLatLonCoords()
|
|
|
|
bbox = [lons.min(), lons.max(), lats.min(), lats.max()]
|
|
|
|
|
|
|
|
def make_map(bbox, projection=ccrs.PlateCarree()):
|
|
|
|
fig, ax = plt.subplots(figsize=(16, 9),
|
|
|
|
subplot_kw=dict(projection=projection))
|
|
|
|
ax.set_extent(bbox)
|
|
|
|
ax.coastlines(resolution='50m')
|
|
|
|
gl = ax.gridlines(draw_labels=True)
|
|
|
|
gl.top_labels = gl.right_labels = False
|
|
|
|
gl.xformatter = LONGITUDE_FORMATTER
|
|
|
|
gl.yformatter = LATITUDE_FORMATTER
|
|
|
|
return fig, ax
|
|
|
|
|
|
|
|
with pcolormesh
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code:: ipython3
|
|
|
|
|
|
|
|
cmap = plt.get_cmap('rainbow')
|
|
|
|
fig, ax = make_map(bbox=bbox)
|
|
|
|
cs = ax.pcolormesh(lons, lats, data, cmap=cmap)
|
|
|
|
cbar = fig.colorbar(cs, shrink=0.7, orientation='horizontal')
|
|
|
|
cbar.set_label(grid.getLocationName() +" "+ grid.getLevel() + " " \
|
|
|
|
+ grid.getParameter() + " (" + grid.getUnit() + ") " \
|
|
|
|
+ "valid " + str(grid.getDataTime().getRefTime()))
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-01 21:35:01 +00:00
|
|
|
.. image:: Grids_and_Cartopy_files/Grids_and_Cartopy_3_0.png
|
2020-09-09 20:02:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
with contourf
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code:: ipython3
|
|
|
|
|
|
|
|
fig2, ax2 = make_map(bbox=bbox)
|
|
|
|
cs2 = ax2.contourf(lons, lats, data, 80, cmap=cmap,
|
|
|
|
vmin=data.min(), vmax=data.max())
|
|
|
|
cbar2 = fig2.colorbar(cs2, shrink=0.7, orientation='horizontal')
|
|
|
|
cbar2.set_label(grid.getLocationName() +" "+ grid.getLevel() + " " \
|
|
|
|
+ grid.getParameter() + " (" + grid.getUnit()+ ") " \
|
|
|
|
+ "valid " + str(grid.getDataTime().getRefTime()))
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-01 21:35:01 +00:00
|
|
|
.. image:: Grids_and_Cartopy_files/Grids_and_Cartopy_5_0.png
|
2020-09-09 20:02:35 +00:00
|
|
|
|