本节组合前面三节内容,介绍如何使用 cedarkit-plots 绘制 500 hPa 位势高度 + 850 hPa 风速叠加图。
首先导入需要使用的包。包括:
数据结构:numpy, pandas
cedarkit 工具套件
数据准备:reki
数据查找函数:
find_local_fileGRIB2文件要素加载函数:
load_field_from_file
气象可视化:cedarkit.plots
绘图板:
Panel底图布局:
EastAsiaMapDomain绘图样式:
ContourStyle,ContourLabelStyle,BarbStyle颜色表加载函数:
get_ncl_colormap
import numpy as np
import pandas as pd
from reki.data_finder import find_local_file
from reki.format.grib.eccodes import load_field_from_file
from cedarkit.plots.style import ContourStyle, ContourLabelStyle, BarbStyle
from cedarkit.plots.chart import Panel
from cedarkit.plots.domains import EastAsiaMapTemplate
from cedarkit.plots.colormap import get_ncl_colormap/home/wangdp/project/cedarkit/notebook-project/notebook-devel/repo/cedarkit-notebook-project/cedarkit-notebook/.venv/lib/python3.14/site-packages/gribapi/__init__.py:23: UserWarning: ecCodes 2.42.0 or higher is recommended. You are running version 2.34.1
warnings.warn(
# 数据环境配置:使用本地数据目录(可用环境变量 CEDARKIT_NOTEBOOK_DATA_ROOT 覆盖)
from cedarkit_notebook.data import get_data_root, get_dataset_query
DATA_CLASS = "cmadaas"
STORAGE_BASE = str(get_data_root())
设置绘图的数据参数,使用 CMA-GFS 最近可获取的时次 024 数据。
system_name = "CMA-GFS"
data_type = "cma_gfs_gmf/grib2/orig"
# 起报时次与预报时效固定为本地已下载数据(见 data/metadata/cma-gfs.yaml)
query = get_dataset_query("cma-gfs")
start_time = pd.to_datetime(query["start_time"], format="%Y%m%d%H")
forecast_time = pd.to_timedelta(query["forecast_time"])加载数据¶
获取 GRIB2 数据路径
file_path = find_local_file(
data_type,
start_time=start_time,
forecast_time=forecast_time,
data_class=DATA_CLASS,
storage_base=STORAGE_BASE
)
file_pathPosixPath('/home/wangdp/project/cedarkit/notebook-project/notebook-devel/repo/cedarkit-notebook-project/cedarkit-notebook/data/DATA/NAFP/NMC/GRAPES-GFS-GLB/2026/20260725/Z_NAFP_C_BABJ_20260725000000_P_NWPC-GRAPES-GFS-GLB-02400.grib2')加载 500 hPa 位势高度场。位势高度在 ecCodes 的内置要素名为 gh
h_500_field = load_field_from_file(
file_path,
parameter="gh",
level_type="pl",
level=500
) / 10.0
h_500_fieldLoading...
加载 850 hPa 风场。风场在 ecCodes 的内置要素名为 u 和 v
u_850_field = load_field_from_file(
file_path,
parameter="u",
level_type="pl",
level=850
)
v_850_field = load_field_from_file(
file_path,
parameter="v",
level_type="pl",
level=850
)
u_850_fieldLoading...
计算得到风速场
wind_speed_850_field = np.sqrt(u_850_field ** 2 + v_850_field ** 2)
wind_speed_850_fieldLoading...
配置¶
为 500 hPa 位势高度场定义等值线样式
h_contour_lev = np.linspace(500, 588, endpoint=True, num=23)
h_contour_colos = "blue"
h_linewidths = np.where(h_contour_lev == 588, 1.4, 0.7)
hgt_style = ContourStyle(
levels=h_contour_lev,
colors="blue",
linewidths=h_linewidths,
label=True,
label_style=ContourLabelStyle(
colors="black",
fontsize=8,
)
)为 850 hPa 风速定义填充图样式
wind_speed_contour_lev = np.arange(8, 26, 2)
color_index = np.arange(90, 236, 15) - 2
color_index[0] = -1
wind_speed_color_map = get_ncl_colormap(
"rainbow+white+gray", index=color_index
)
wind_speed_style = ContourStyle(
colors=wind_speed_color_map,
levels=wind_speed_contour_lev,
fill=True,
)为 850 hPa 风场定义风场图样式
wind_barb_style = BarbStyle(
barbcolor="red",
flagcolor="red",
linewidth=0.3,
)绘制¶
创建中国区域底图布局
domain = EastAsiaMapTemplate()绘制叠加图,设置标题和颜色条。
多次调用 Panel.plot() 方法实现图形的叠加绘制。
panel = Panel(domain=domain)
panel.plot(h_500_field, style=hgt_style)
panel.plot(wind_speed_850_field, style=wind_speed_style)
panel.plot([[u_850_field[::14, ::14], v_850_field[::14, ::14]]], style=wind_barb_style, layer=[0])
domain.set_title(
panel=panel,
graph_name="500 hPa HGT (10gpm) line + 850 hPa WIND (m/s) windbarb and shadow",
system_name=system_name,
start_time=start_time,
forecast_time=forecast_time,
)
domain.add_colorbar(panel=panel, style=wind_speed_style)
panel.show()