简体   繁体   English

python中的后处理wrf输出

[英]Post-processing wrf output in python

I am trying to use python to post-process WRF output. 我正在尝试使用python来后处理WRF输出。 The various scripts I have seen are too complicated for my elementary level of python. 我见过的各种脚本对于我的初级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 显然我将z分量定义为轮廓

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). 使用此代码,我的经度(x)和纬度(y)的numpy数组有问题。 I hope someone conversant with numpy can assist me. 我希望熟悉numpy的人可以帮助我。 I have added numpy in the tags. 我在标签中添加了numpy。 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. 我所知道的是我在x方向上有69个点,在y方向上有60个点。 My data file has 19 levels, and 9 time steps. 我的数据文件有19个级别,9个时间步长。 I do not know where 17 comes from. 我不知道17来自哪里。

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. 以下是一些值得研究和分析的Python WRF处理包。 They're both fairly straightforward to use: 它们都非常简单易用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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