简体   繁体   中英

Post-processing wrf output in python

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 The two lines which gives the wrong results are:

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

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

Obviously my definition of the z component to contour

baseArray = np.fromfunction(lambda y,x: (1000.0 / (width + height)) * (y+x), (height, width), dtype = float)

is incorrect.

I will appreciate assistance to define my variable so that what is plotted reflects what is in the input file.

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")
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()

# 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()

The following is code which is close to what should work. With this code, I have a problem with the numpy arrays of my longitude (x) and latitude (y). I hope someone conversant with numpy can assist me. I have added numpy in the tags. This code gives me the error:

ValueError: operands could not be broadcast together with shapes (17,60,69) (17,60) 

What I know is that I have 69 points in x-direction and 60 in y-direction. My data file has 19 levels, and 9 time steps. I do not know where 17 comes from.

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")
lons = fh.variables["XLONG"][:]
lats = fh.variables["XLAT"][:]
data = fh.variables["T2"][:]
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(0.,90,10.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)

# draw meridians
meridians = np.arange(180.,360.,10.)
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

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

# draw filled contours.
cs = m.contourf(x,y,data)

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

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

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

Here are a couple of Python WRF processing packages worth looking into for plotting and analysis. They're both fairly straightforward to use:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM