文章目录

  • 效果图
  • 一、 python读取wrfout
  • 一、python绘制500hPa高度场
  • 三、输出nc文件

  • 资料:台风“菲特“fitow模拟结果文件,https://blog.csdn.net/nice_clever/article/details/127340492#comments_24201637
  • 必要python包:netCDF4、wrf-puthon

【anaconda安装wrf-python 】

conda install -c conda-forge wrf-python

本文主要介绍python对wrfout结果文件的初步后处理操作,以及基础绘图。
wrfout后处理包括:【读取wrfout文件、读取wrfout文件中变量metadata及数据、对高度场进行500hPa插值、输出nc文件】
基础绘图操作包括:【设置投影和范围、绘制等值线contour和等值线标值、副高区域填色contourf】
仅展示初步评估模拟的效果,若精美绘制需要进一步的设置、细化。


效果图

以下代码运行后输出plot():

一、 python读取wrfout

import numpy as npimport xarray as xrfrom netCDF4 import Datasetfrom wrf import getvar, ALL_TIMES, interplevelimport matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeature# ### 使用 cat 方法合并多个文件wrfin = Dataset('./wrfout_d01_2013-10-05_00_00_00')# print(wrfin)# 提取位势高度和压力场z = getvar(wrfin, 'z')# 提取WRF netCDF 变量 # model heightp = getvar(wrfin, 'pressure')# 单位hPa (29, 216, 216)# 计算 500 mb 位势高度ht_500mb = interplevel(z, p, 500.)ht_500mb = ht_500mb/10.# print(np.min(ht_500mb))# print(np.max(ht_500mb))lon = ht_500mb.XLONGlat = ht_500mb.XLATprint(ht_500mb.shape)

一、python绘制500hPa高度场

####################################################### ## PLOT####################################################### proj= ccrs.PlateCarree(central_longitude=180)proj_data = ccrs.PlateCarree()#LambertCylindrical() # 数据的投影方式leftlon, rightlon, lowerlat, upperlat = (115, 145, 12, 42)fig , ax= plt.subplots(1,1,figsize=(8,8),subplot_kw={'projection':proj})######## 调节绘图经纬度范围Region = [leftlon, rightlon, lowerlat, upperlat] #要绘制的范围 lon1,lon2,lat1,lat2ax.set_extent(Region, crs=proj_data) #经纬度范围,坐标参考系转换######## 添加地理信息ax.add_feature(cfeature.COASTLINE.with_scale('50m'),lw=0.5)# 添加海岸线######## 绘制等值线qs = ax.contourf(lon,lat,ht_500mb, zorder=0, levels=[576., 600.], colors='grey',extend='max', transform=proj_data)lvl = np.arange(560,600,2.0)cs = ax.contour(lon,lat,ht_500mb, zorder=0, levels=lvl, colors='k', transform=proj_data)######## 添加等值线标注ax.clabel(cs,cs.levels, fontsize=8, colors='k')plt.savefig('0620.png',dpi=200)plt.show()# ds = xr.Dataset({'ht_500mb':ht_500mb})# 将数据数组转为数据集# ds.to_netcdf('./ht_500mb_100520.nc', mode='w') # 使用相对路径也可以# ht_500mb.to_netcdf('./ht_500mb_100520.nc', mode='w')

三、输出nc文件

ds = xr.Dataset({'ht_500mb':ht_500mb})# 将数据数组转为数据集ds.to_netcdf('./ht_500mb_100520.nc', mode='w') # 使用相对路径也可以ht_500mb.to_netcdf('./ht_500mb_100520.nc', mode='w')