Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

开始使用

本文介绍如何使用 cedarkit 工具栈为 CMA-MESO 数据绘制中国区域 2 米温度填充图。

首先加载需要使用的包,包括:

  • 数据结构相关:pandas

  • 数据准备工具库:reki

  • 绘图工具库:cedarkit.plots

    • 绘图板:Panel

    • 底图布局:EastAsiaMapTemplate

    • 填充图样式:ContourStyle

    • 颜色表:get_ncl_colormap

    • 地图包:set_default_map_package

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
from cedarkit.plots.chart import Panel
from cedarkit.plots.domains import EastAsiaMapTemplate
from cedarkit.plots.colormap import get_ncl_colormap
from cedarkit.plots.map import set_default_map_loader_package

# 数据环境配置:使用本地数据目录(可用环境变量 CEDARKIT_NOTEBOOK_DATA_ROOT 覆盖)
from cedarkit_notebook.data import get_data_root, get_dataset_query
/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(
DATA_CLASS = "cmadaas"
STORAGE_BASE = str(get_data_root())

设置绘图的数据参数。使用动态日期绘制 CMA-MESO 最近可用时次 024 时效的 2 米温度场。

system_name = "CMA-MESO"
data_type = "cma_meso_3km/grib2/orig"
# 起报时次与预报时效固定为本地已下载数据(见 data/metadata/cma-meso-3km.yaml)
query = get_dataset_query("cma-meso-3km")
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_path
PosixPath('/home/wangdp/project/cedarkit/notebook-project/notebook-devel/repo/cedarkit-notebook-project/cedarkit-notebook/data/DATA/NAFP/GRAPES/RAFS/2026/20260725/Z_NAFP_C_BABJ_20260725000000_P_NWPC-GRAPES-3KM-ORIG-02400.grb2')

加载 2 米温度场。这里直接使用 ecCodes 内置的要素名 2t

t_2m_field = load_field_from_file(
    file_path,
    parameter="2t",
) - 273.15
t_2m_field
Loading...

配置

定义填充图层次

t_2m_level = [-24, -20, -16, -12, -8, -4, 0, 4, 8, 12, 16, 20, 24, 28, 32]

定义填充图颜色表。色表来自 cedarkit-plots 内置的 NCL 色表文件 BlAqGrYeOrReVi200.rgb。

color_index = [2, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122, 132, 142, 152]
t_2m_color_map = get_ncl_colormap("BlAqGrYeOrReVi200", index=color_index)

定义填充图样式

t_2m_style = ContourStyle(
    colors=t_2m_color_map,
    levels=t_2m_level,
    fill=True,
)

使用 CEMC 地图包

注:如果没有安装 cemc-meda-data 包,可以注释改行代码,直接使用默认地图包

set_default_map_loader_package("cedarkit.plots.map.cemc")
'cedarkit.plots.map.cemc'

绘图

创建中国区域底图布局

domain = EastAsiaMapTemplate()

创建绘图板,绘制等值线,设置标题,添加颜色条

panel = Panel(domain=domain)
panel.plot(t_2m_field, style=t_2m_style)
domain.set_title(
    panel=panel,
    graph_name="2m Temperature (C)",
    system_name=system_name,
    start_time=start_time,
    forecast_time=forecast_time,
)
domain.add_colorbar(panel=panel, style=t_2m_style)
panel.show()
<Figure size 3200x3200 with 2 Axes>