In [1]:
import cartopy
import matplotlib as mpl
import matplotlib.pyplot as plt
from owslib.wms import WebMapService
from siphon.catalog import get_latest_access_url
In [2]:
catalog = 'http://thredds-jumbo.unidata.ucar.edu/thredds/catalog/grib/NCEP/HRRR/CONUS_2p5km/catalog.xml'
serverurl = get_latest_access_url(catalog, 'WMS')
wms = WebMapService( serverurl, version='1.1.1')
In [3]:
#Listing all available layers...
layers = list(wms.contents)
for layer in layers:
print('Layer name: {}'.format(wms[layer].name))
In [4]:
temp = wms['Temperature_height_above_ground']
elevations = [elevation.strip() for elevation in temp.elevations]
print(elevations)
# only one elevation, so use it
elevation = elevations[0]
# have to guess the range
color_max = 315 # K
color_min = 273 # K
colorscalerange = '{},{}'.format(color_min,color_max)
In [5]:
times = [time.strip() for time in temp.timepositions]
print(times)
# get the first time - Forecast Hour 0
time = times[0]
In [6]:
# pick a projection - going with Miller for this example
# note that with Cartopy you are NOT limited to the projections avaliable through ncWMS
plt_proj = cartopy.crs.Miller()
fig, ax = plt.subplots(figsize=(12,12), subplot_kw={'projection': plt_proj})
# Colorbar goodness
cax = fig.add_axes([0.95, 0.3, 0.02, 0.42])
norm = plt.Normalize(vmin=color_min, vmax=color_max)
cmap = plt.cm.gist_heat
cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm, spacing='proportional', orientation='vertical')
cb.set_label('Temperature [K]')
# use bounding box info obtained from the ncWMS service to frame the image
extent = (temp.boundingBox[0], temp.boundingBox[2], temp.boundingBox[1], temp.boundingBox[3])
ax.set_extent(extent)
# ncWMS keywords (which includes the WMS keywords as well)
wms_kwargs = {'colorscalerange': colorscalerange,
'abovemaxcolor': 'transparent',
'belowmincolor': 'transparent',
'transparent': 'true',
'elevation': elevation,
'time': time}
# plot the layer using Cartopy's WMS interface
ax.add_wms(wms=serverurl, layers=[temp.name], wms_kwargs=wms_kwargs, cmap=cmap)
# add coastlines, country borders and state outlines
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS)
ax.add_feature(cartopy.feature.NaturalEarthFeature(
category='cultural',
name='admin_1_states_provinces_lines',
scale='50m',
facecolor='none'), linestyle=':')
Out[6]:
In [ ]: