Pratiman, 29 July 2020
2 min read.Here is an example of generating a surface plot from WRF output file. I find this method to be more elegent.
import wrf
from netCDF4 import Dataset
import proplot as plot
import cartopy.crs as crs
# Download the dataset from https://www.unidata.ucar.edu/software/netcdf/examples/wrfout_v2_Lambert.nc
# Load the dataset
ncfile = Dataset('wrfout_v2_Lambert.nc')
# Get the variable
slp = wrf.getvar(ncfile, "slp")
# Get the latitude and longitude points
lats, lons = wrf.latlon_coords(slp)
# Get the cartopy mapping object
cart_proj = wrf.get_cartopy(slp)
#Plotting
fig, axs = plot.subplots(proj=cart_proj)
# Define extents
lat = [lats.min(), lats.max()]
lon = [lons.min(), lons.max()]
#format the plot
axs.format(
lonlim=lon, latlim=lat,
labels=True, innerborders=True
)
#Plot using contourf
m = axs.contourf(wrf.to_np(lons), wrf.to_np(lats), wrf.to_np(slp),
transform=crs.PlateCarree(), cmap='roma_r')
#Adding colorbar with label
cbar = fig.colorbar(m, loc='b', label='Sea Level Pressure (hPa)')
#Saving the figure
fig.savefig('slp.png')
import wrf
from netCDF4 import Dataset
import proplot as plot
import cartopy.crs as crs
ncfile = Dataset('wrfout_v2_Lambert.nc')
slp = wrf.getvar(ncfile, "slp")
lats, lons = wrf.latlon_coords(slp)
cart_proj = wrf.get_cartopy(slp)
proj
.fig, axs = plot.subplots(proj=cart_proj)
lat = [lats.min(), lats.max()]
lon = [lons.min(), lons.max()]
axs.format(
lonlim=lon, latlim=lat,
labels=True, innerborders=True
)
m = axs.contourf(wrf.to_np(lons), wrf.to_np(lats), wrf.to_np(slp),
transform=crs.PlateCarree(), cmap='roma_r')
cbar = fig.colorbar(m, loc='b', label='Sea Level Pressure (hPa)')
fig.savefig('slp.png')
If you do not like the LCC projection then you can change the following line:
fig, axs = plot.subplots(proj='cyl')