WRF模型等值线图绘制流程:WRF数据、相关库与主要步骤

[id_1[id_94126746]03758546[id_974184088]

WRF模型等值线图绘制流程:WRF数据、相关库与主要步骤

绘制 WRF 模型的等值线图需要用到特定的工具和库。首先需要使用 WRF 数据,同时还需要 wrf-python 库、Matplotlib 库或其他绘图库。以下是一般用于绘制 WRF 模型等值线图的步骤:

[id_[id_1272499531]90536876]

从 getvarWRF 输出文件中提取您希望绘制的变量,例如提取温度、提取降水量、提取风速等。

创建网格:若您的 WRF 输出数据是具有经纬度坐标的,那么您能够运用 latlon_coords 函数去创建网格,这样就可以在等值线图中进行绘制。

绘制等值线图:可以通过使用 Matplotlib 中的 contour 函数或者 contourf 函数来进行。contour 函数用于绘制轮廓线,而 contourf 函数则用于绘制填充轮廓线,具体该使用哪个函数取决于实际的需求。

Basemap 和 Cartopy:

在 Python 里,若要绘制 WRF(Weather Research and Forecasting)模型的地图,存在两个较为常用的库,分别是 Basemap 以及 Cartopy。这两个库均能够用来制作地图,然而它们各自有着不同的特点与用法。

Basemap:

Cartopy:

总体而言,若您需要更高级的地图制作功能,或者需要更强的地理数据处理能力,那么 Cartopy 或许是更优的选择。然而,倘若您仅仅需要简单的地图,Basemap 可能会更便于操作。依据您的需求以及个人的偏好,您能够选择使用其中一个库来对 WRF 模型进行地图可视化。

[id_1201112542]

Basemap:

from netCDF4 import Dataset
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
from mpl_toolkits.basemap import Basemap
from pylab import[id_1197162147]
import numpy as np
from[id_1823445491]import获取变量 getvar,对其进行二维平滑 smooth2d,获取基础地图 get_basemap,以及得到经纬度坐标 latlon_coords 并作用于 to_np
import numpy as np
mpl.rcParams['font.sans-serif'] = ['SimHei']

#choose the area
定义一个函数 select_and_return_index_2d,它接收一个二维数组 arr2d 以及最小值 min_val 和最大值 max_val 作为参数,用于选择并返回满足条件的元素的索引。
selected 等于 arr2d 中满足 (arr2d 大于等于 min_val) 并且 (arr2d 小于 max_val) 的元素;selected 也等于 arr2d 中满足 (arr2d 大于等于 min_val) 并且 (arr2d 小于等于 max_val) 的元素;selected 还等于 arr2d 中满足 (arr2d 大于 min_val) 并且 (arr2d 小于 max_val) 的元素;selected 同样等于 arr2d 中满足 (arr2d 大于 min_val) 并且 (arr2d 小于等于 max_val) 的元素。<= max_val)]
index 等于 np.where 满足两个条件的结果,一个条件是 arr2d 大于等于 min_val,另一个条件是 arr2d 小于 max_val,同时 arr2d 不等于特定值。<= max_val))
#   return selected, index

# Open the NetCDF file
ncfile = Dataset(G 盘下有 wrfout_d01_2023 年 7 月 1 日 6 时 0 分 0 秒的文件。)

# Get the sea level pressure
slp = getvar(ncfile, "slp")

使海平面气压趋于平稳,因为它在附近往往较为嘈杂。
# mountains
smooth_slp = smooth2d(slp, 3, cenweight=4)

获取纬度和经度的点。
lats 是通过 latlon_coords(slp)得到的;lons 也是通过 latlon_coords(slp)得到的。

# Get the basemap object
bm 等于获取基础地图(slp),其中左下角纬度为18, urcrnrlat=23,llcrnrlon=114, urcrnrlon=119,resolution='h')

levels=np.linspace(1040,1056,10)
# Create a figure
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111)

# Add geographic outlines
bm.drawcoastlines(linewidth=0.25)
bm.drawstates(linewidth=0.25)
bm.drawcountries(linewidth=0.25)
bm.drawlsmask ,其陆地颜色为“gray”,海洋颜色为“white”

将经纬度转换为 x 和 y。务必将经纬度进行转换。
通过 to_np 将 lons 转换为 numpy 数组,否则 basemap 会因未定义的情况而崩溃。
# RuntimeError.
x 和 y 等于通过 bm 函数将 to_np(lons)和 to_np(lats)进行处理后得到的结果

绘制轮廓线以及填充轮廓线。
bm 绘制 x 和 y 的轮廓图,使用 to_np(smooth_slp) 作为数据,水平设置为 levels,颜色设置为 colors 。"black")
bm 对 x 和 y 进行 contourf 操作,同时使用 to_np(smooth_slp),设置 levels 为 levels,颜色映射为 get_cmap( 。"jet"))

bm.drawparallels( np.arange(18.,24.,1.),   labels=[1,0,0,0], color='k', fontsize=12 )
bm.drawmeridians( np.arange(114.,120.,1.), labels=[0,0,0,1], color='k' , fontsize=12 )

# Add a color bar
plt.colorbar(shrink=1)

plt.title("Sea Level Pressure (hPa)")

plt.show()

Cartopy:

from netCDF4 import Dataset
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
import cartopy.crs as crs
import cartopy.feature as cfeature
import cmaps
from cartopy.mpl.gridliner importLONGITUDE_FORMATTER 以及 LATITUDE_FORMATTER
import matplotlib.ticker as mticker
import numpy as np
import cartopy.crs as ccrs

from wrf import使用 to_np 获取变量,然后进行 smooth2d 操作,接着通过 get_cartopy 获取相关内容,再利用 cartopy_xlim 进行相关处理。
cartopy_ylim 以及 latlon_coords

# Open the NetCDF file
ncfile = Dataset("G:\wrfout_d01_2023-07-01_06_00_00")

# Get the sea level pressure
slp = getvar(ncfile, "slp")

# Smooth the sea level pressure since it tends to be noisy near the
# mountains
smooth_slp = smooth2d(slp, 3, cenweight=4)

# Get the latitude and longitude points
lats, lons = latlon_coords(slp)

获取 cartopy 绘图对象。
cart_proj = get_cartopy(slp)
levels=np.linspace(1040,1056,10)

# Create a figure
fig = plt.figure(figsize=(12,6))
设置地理轴为 WRF 使用的投影。
ax 等于通过 plt.axes 方法创建的一个坐标轴对象,并且该坐标轴对象的投影设置为 cart_proj


制作平滑海平面的轮廓线和填充轮廓线。
# pressure.
plt 绘制等高线,等高线的经度坐标为 to_np(lons),纬度坐标为 to_np(lats),数值为 to_np(smooth_slp),等高线的级别为 levels,颜色为 colors。"black",
将坐标系统转换为 crs.PlateCarree() 。
mesh 等于 plt.contourf(to_np(lons)、to_np(lats)、to_np(smooth_slp),levels 为 levels 。
transform 等于 crs.PlateCarree()。
cmap 等于 cmaps.MPL_jet 。

ax.add_feature(cfeature.LAND)#添加陆地
ax 为图形对象,添加特征为海岸线(cfeature.COASTLINE),线宽(lw)设置为具体值(具体值未给出)。0.3)#添加海岸线
ax 添加了海洋这一特征。

# Add a color bar
plt.colorbar(ax=ax, shrink=) 后添加了一个颜色条,该颜色条与指定的轴 ax 相关联,并且进行了收缩处理。.98)

# 获取颜色条相关联的轴对象
# cax = colorbar.ax

# # 设置颜色条刻度标签的字体大小
cax.tick_params(labelsize=labelsize) # 将字体大小设置为之前设定的 5
# Set the map bounds
ax 设置 x 轴的范围为 cartopy_xlim 作用于 smooth_slp 的结果
ax 设置 y 轴的范围为通过 cartopy_ylim 函数处理后的 smooth_slp 的值。

mesh 等于 ax 的网格线(绘制标签为)True, linestyle='--', linewidth=0.6, alpha=0.5, x_inline=False, y_inline=False, color='k')
mesh.top_labels=False
mesh.right_labels=False
mesh 的 xformatter 等于 LONGITUDE_FORMATTER 。
mesh 的 yformatter 等于 LATITUDE_FORMATTER 。
mesh.xlocator 等于 mticker 的 FixedLocator 函数,传入的参数是 np.arange 函数生成的数组,这个数组的范围是从某个起始值到某个结束值,步长为 1 。114,120,1))
mesh.ylocator 等于 mticker 的 FixedLocator 函数,该函数的参数是 np.arange(18,24,1))
mesh.xlabel_style={'size':12}
mesh.ylabel_style={'size':12}

ax.set_extent([114,119,18,23],crs=ccrs.PlateCarree()) # 小范围

plt.title("Sea Level Pressure (hPa)")

plt.show()

你可能想看:
« 上一篇
下一篇 »

相关推荐

python爬虫教程PDF,Python爬虫书PDF

2026年09月02日

30阅读

外国教程python,python编程教学视频

2026年09月02日

25阅读

python简明教程沈洁元,python详细基础教程

2026年09月02日

21阅读

python库包教程,python 包

2026年09月02日

26阅读

python教程大全集,python教程600集

2026年09月02日

26阅读

python入门教程完整版,python零基础入门全套教程视频

2026年09月02日

22阅读