前段时间有读者来信问再分析数据的气象要素廓线怎么绘制,近期小编可以腾出手做个简单示例
今天我们测试如何使用Python中的两个强大库——xarray与pynio,来读取ERA5(European Centre for Medium-Range Weather Forecasts Reanalysis 5th Generation)提供的GRIB(GRIdded Binary)格式数据,并绘制指定经纬度站点的风速廓线。ERA5以其高时空分辨率和全球覆盖范围,为科学研究和应用提供了丰富的气象信息。接下来,我们将通过一系列步骤详细展示这一过程。
由于可视化代码过长隐藏,可点击基于ERA5 GRIB数据的气象要素廓线与Hovmoller图绘制运行Fork查看 🔜🔜若没有成功加载可视化图,点击运行可以查看 ps:隐藏代码在【代码已被隐藏】所在行,点击所在行,可以看到该行的最右角,会出现个三角形,点击查看即可
xarray允许您指定不同的引擎来处理不同格式的数据。对于GRIB文件,可以使用pynio引擎直接读取
In [2]:
import xarray as xr
file_path = '/home/mw/input/era51824/ERA5-2023-08_pl.nc'
engine = 'pynio'
dataset = xr.open_dataset(file_path, engine=engine)
u_wind = dataset['u']
v_wind = dataset['v']
In [5]:
u_wind
Out[5]:
确定您感兴趣的经纬度坐标(例如,target_lat =40,target_lon = 120)。使用xarray的sel方法找到最接近该点的格点数据
In [6]:
target_lat, target_lon = 40, 120
nearest_point = dataset.sel(longitude=target_lon, latitude=target_lat, method='nearest')
nearest_point
Out[6]:
In [24]:
wind_speed
Out[24]:
计算风速并绘制垂直廓线(风速随高度的变化分布)
In [50]:
import matplotlib.pyplot as plt
import numpy as np
wind_speed = np.sqrt(nearest_point['u'] ** 2 + nearest_point['v'] ** 2)
fig, ax = plt.subplots(figsize=(10, 12))
# 绘制风速廓线
ax.plot(wind_speed[0].values, nearest_point['level'].values, 'b', linewidth=2) # 使用蓝色线条绘制风速曲线
# 设置轴标签
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('Pressure Level (hPa)')
plt.title(f'Wind Speed Profile at ({target_lat:.2f}, {target_lon:.2f})')
# 反转 y 轴(从大气顶部向下到地面)
ax.invert_yaxis()
# 添加网格线
ax.grid(True)
# 显示图形
plt.show()