WRF post-processing with Python

Miscellaneous questions you have about anything related to graphically displaying WRF output

WRF post-processing with Python

Postby wrf_usr » Tue May 14, 2019 4:16 am

Hello,
I am trying to use python to post-process WRF output. The various scripts I have seen are too complicated for my elementary level of python. I managed to put together bits of code from here and there. However in my final plot i am not able to capture the data properly.
I would appreciate any assistance.
My code is below
Unfortunately i was not able to attach the plots

from netCDF4 import Dataset
import numpy as np
from matplotlib.mlab import griddata

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from datetime import datetime, timedelta

fh = Dataset("wrfout_d01_2019-03-28_00:00:00", mode="r")
# fh = Dataset(my_file, mode="r")

lons = fh.variables["XLONG"][:]
lats = fh.variables["XLAT"][:]
temp = fh.variables["T2"][:]

temp_units = fh.variables['T2'].units

fh.close()

# define map extent
lllon,lllat,urlon,urlat = 17.25,-21.90,38.44,-04.07

width = 300
height = 200
dlon = (urlon-lllon) / width
dLat = (urlat-lllat) / height
baseArray = np.fromfunction(lambda y,x: (1000.0 / (width + height)) * (y+x), (height, width), dtype = float)
lons = np.arange(lllon, urlon, dlon)
lats = np.arange(lllat, urlat, dLat)
lons, lats = np.meshgrid(lons, lats)

fig = plt.figure()
#plt.title("The Plot")

# Set up Basemap instance
m = Basemap(
projection = 'merc',
llcrnrlon = lllon, llcrnrlat = lllat, urcrnrlon = urlon, urcrnrlat = urlat,
resolution='h')

# Plot Data
cs=m.pcolormesh(lons, lats, baseArray, shading='flat', latlon=True)
#cs=m.contourf(lons, lats, baseArray, shading='flat', latlon=True)

# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 5.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 5.), labels=[0,0,0,1], fontsize=10)

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()

# Add Colorbar
cb = plt.colorbar(cs ,shrink=1.0) #, extend='both')

# Add Title
plt.title("Temperature")

plt.savefig("Temp2s.png" , format="png", dpi=300, transparent=True)

plt.show()
**************************
After further googling and a bit of help from Stackoverflow (https://stackoverflow.com/questions/561 ... -in-python), here is my code which works, in case it may be useful to someone else (Note colorbar does not display correctly-no colors):

from netCDF4 import Dataset
import numpy as np
from matplotlib.mlab import griddata

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm
from datetime import datetime, timedelta

fh = Dataset("wrfout_d01_2019-03-28_00:00:00", mode="r")
# fh = Dataset(my_file, mode="r")

lons = fh.variables["XLONG"][0][0][:]
lats = fh.variables["XLAT"][0][:][0]
data = fh.variables["T2"][0][:]
data_units = fh.variables['T2'].units

fh.close()

# define map extent
lllon,lllat,urlon,urlat = 17.25,-21.90,38.44,-04.07

# Set up Basemap instance
m = Basemap(
projection = 'merc', \
llcrnrlon = lllon, llcrnrlat = lllat, \
urcrnrlon = urlon, urcrnrlat = urlat, resolution='h')

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# Add Grid Lines
# draw parallels.
parallels = np.arange(-20., 0., 5.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)

# draw meridians
meridians = np.arange(20., 40., 5.)
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

ny = data.shape[0]
nx = data.shape[1]
lons, lats = m.makegrid(nx, ny) # get lat/lons of ny by nx evenly space grid.

x, y = m(lons, lats) # compute map proj coordinates.

# draw filled contours.
cs = m.contourf(x,y,data, shading='flat')
cs = m.contour(x,y,data,levels = range(290,310,3),colors = 'blue')
plt.clabel(cs, fmt= '%.0f', inline = True)

# Add Colorbar
cbar = m.colorbar() # plt, location='bottom', pad="10%"
cbar.set_label(data_units)

# Add Title
plt.title("Surface Temperature")

plt.savefig("Temp2.png" , format="png", dpi=300, transparent=True)

plt.show()
wrf_usr
 
Posts: 6
Joined: Thu Feb 10, 2011 10:49 am

Return to Miscellaneous

Who is online

Users browsing this forum: No registered users and 4 guests