周五发了素材募集没几个响应呀,这期虽然没什么难度,对新手应该足够了。
对很多气象 Python 新手来说,“环流图”三个字往往意味着“一大堆黑框框命令 + 神秘莫测的 GrADS/NCL”。但其实,ERA5 已经把全球每一层、每一小时的气象场做成“云上好下载的 NetCDF”,而 Python 生态里又有 xarray、cartopy、metpy 等开箱即用的轮子。本教程想让你体会到三件事:
1.下载再分析资料可以像点外卖一样简单; 2.绘图可以像搭积木一样“加一行就多点东西”; 3.只要你掌握一个 ERA5 例子,换任何资料(MERRA-2、NCEP、CRA-40……)只需文件和变量名。 4.记得自己注册好EC账号
你说数错了?四天王里有五个人也很正常嘛
!conda install -c conda-forge xarray netCDF4 cartopy cmocean metpy
import cdsapi, datetime as dt
c = cdsapi.Client()
dates = [dt.date(2023,6,1)+dt.timedelta(h) for h in range(24)]
c.retrieve(
'reanalysis-era5-pressure-levels',
{
'product_type':'reanalysis',
'variable':['geopotential','u_component_of_wind','v_component_of_wind'],
'pressure_level':'750',
'year':'2023',
'month':'06',
'day':['01','02'],
'time': [t.strftime('%H:%M') for t in dates],
'format':'netcdf',
}, 'ERA5_20230601-02_750hPa.nc')
import xarray as xr, cartopy.crs as ccrs, cartopy.feature as cfeature
import matplotlib.pyplot as plt, metpy.calc as mpcalc, numpy as np
from matplotlib.patches import Rectangle
# 1. 读数据
ds = xr.open_dataset("/home/mw/input/era58091/ERA5-2023-08_pl.nc")
h = ds['z'].metpy.sel(level=750).isel(time=0) / 9.80665 # 位势高度转位势米
u = ds['u'].metpy.sel(level=750).isel(time=0)
v = ds['v'].metpy.sel(level=750).isel(time=0)
# 2. 坐标
lon = h.longitude
lat = h.latitude
# 3. 画图
fig, ax = plt.subplots(figsize=(8, 6),dpi=200,
subplot_kw={'projection': ccrs.PlateCarree()})
ax.set_extent([100, 140, 15, 60], crs=ccrs.PlateCarree())
# 底图
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax.add_feature(cfeature.BORDERS.with_scale('50m'))
ax.add_feature(cfeature.LAKES.with_scale('50m'), alpha=0.5)
# 填色图 750hPa 位势高度(色标)
cs = ax.contourf(lon, lat, h, levels=10,
cmap='BrBG', transform=ccrs.PlateCarree())
ax.contour(lon, lat, h, levels=10,color='k',
transform=ccrs.PlateCarree())
plt.colorbar(cs, shrink=0.5, label='Geopotential Height (gpm)')
# 风羽图(密度每10格画1个)
ax.barbs(lon.values[::10], lat.values[::10],
u.values[::10,::10] ,v.values[::10,::10],
transform=ccrs.PlateCarree())
ax.set_title('ERA5 750 hPa Height&Wind 2023-06-01 00UTC')
plt.show()


小结(把大象装进冰箱的三步法) 拿数据:用 CDS API 一句 retrieve,或者网页点点鼠标,就能把 500 hPa 高度、纬向风、经向风一键下载到本地; 读数据:xarray.open_dataset() 直读 NetCDF 出图:plt.axes(projection=ccrs.PlateCarree()) 起画布,contourf/ barbs/ streamplot 三板斧,想画位势高度等高线就多一行,想画温度平流再多一行——万变不离其模板。