Skip to content

Commit d1a7f66

Browse files
committed
[WIP] Add convert data script and docs
1 parent 98ea5b2 commit d1a7f66

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed

docs/zh/examples/pangu_weather.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Pangu-Weather
2+
3+
=== "模型训练命令"
4+
5+
暂无
6+
7+
=== "模型评估命令"
8+
9+
暂无
10+
11+
=== "模型导出命令"
12+
13+
暂无
14+
15+
=== "模型推理命令"
16+
17+
``` sh
18+
# Download sample input data
19+
mkdir -p ./data
20+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/models/Pangu/input_surface.npy -P ./data
21+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/models/Pangu/input_upper.npy -P ./data
22+
23+
# Download pretrain model weight
24+
mkdir -p ./inference
25+
26+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/models/Pangu/pangu_weather_1.onnx -P ./inference
27+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/models/Pangu/pangu_weather_3.onnx -P ./inference
28+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/models/Pangu/pangu_weather_6.onnx -P ./inference
29+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/models/Pangu/pangu_weather_24.onnx -P ./inference
30+
31+
# 1h interval-time model inference
32+
python predict.py INFER.export_path=inference/pangu_weather_1
33+
# 3h interval-time model inference
34+
python predict.py INFER.export_path=inference/pangu_weather_3
35+
# 6h interval-time model inference
36+
python predict.py INFER.export_path=inference/pangu_weather_6
37+
# 24h interval-time model inference
38+
python predict.py INFER.export_path=inference/pangu_weather_24
39+
```
40+
41+
## 1. 背景简介
42+
43+
盘古气象大模型(Pangu-Weather)是首个精度超过传统数值预报方法的 AI 方法,其提供了 1 小时间隔、3 小时间隔、6 小时间隔、24 小时间隔的预训练模型。1 小时 - 7 天预测精度均高于传统数值方法(即欧洲气象中心的 operational IFS),同时预测速度提升 10000 倍,可秒级完成对全球气象的预测,包括位势、湿度、风速、温度、海平面气压等。盘古气象大模型的水平空间分辨率达到 0.25°×0.25° ,时间分辨率为 1 小时,覆盖 13 层垂直高度,可以精准地预测细粒度气象特征。
44+
45+
## 2. 模型原理
46+
47+
本章节仅对盘古气象大模型的原理进行简单地介绍,详细的理论推导请阅读 [Pangu-Weather: A 3D High-Resolution System for Fast and Accurate Global Weather Forecast](https://arxiv.org/pdf/2211.02556)
48+
49+
模型的总体结构如图所示:
50+
51+
待补充
52+
53+
模型使用预训练权重推理,接下来将介绍模型的推理过程。
54+
55+
## 3. 模型构建
56+
57+
在该案例中,实现了 PanguWeatherPredictor用于ONNX模型的推理:
58+
59+
``` py linenums="30" title="examples/pangu_weather/predict.py"
60+
--8<--
61+
examples/pangu_weather/predict.py:67:97
62+
--8<--
63+
```
64+
65+
``` yaml linenums="15" title="examples/pangu_weather/conf/pangu_weather.yaml"
66+
--8<--
67+
examples/pangu_weather/conf/pangu_weather.yaml:29:44
68+
--8<--
69+
```
70+
71+
其中,`input_file``input_surface_file` 分别代表网络模型输入的高空气象数据和地面气象。
72+
73+
## 4. 结果可视化
74+
75+
先将数据从 npy 转换为 NetCDF 格式,然后采用 ncvue 进行可视化
76+
77+
1. 安装相关依赖
78+
```python
79+
pip install cdsapi netCDF4 ncvue
80+
```
81+
82+
2. 使用脚本进行数据转换
83+
```python
84+
85+
```
86+
87+
3. 使用 ncvue 打开转换后的 NetCDF 文件
88+
89+
## 5. 完整代码
90+
91+
``` py linenums="1" title="examples/pangu_weather/predict.py"
92+
--8<--
93+
examples/pangu_weather/predict.py
94+
--8<--
95+
```
96+
97+
## 6. 结果展示
98+
99+
下图展示了模型的预测结果和真值结果。
100+
101+
待补充
102+
103+
## 7. 参考资料
104+
105+
- [Pangu-Weather: A 3D High-Resolution System for Fast and Accurate Global Weather Forecast](https://arxiv.org/pdf/2211.02556)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# ref: https://github.com/HaxyMoly/Pangu-Weather-ReadyToGo/blob/main/forecast_decode_functions.py
16+
17+
import os
18+
from os import path as osp
19+
from typing import Dict
20+
21+
import hydra
22+
import netCDF4 as nc
23+
import numpy as np
24+
25+
26+
def convert_surface_data_to_nc(
27+
surface_file: str, file_name: str, output_dir: str
28+
) -> None:
29+
surface_data = np.load(surface_file)
30+
mean_sea_level_pressure = surface_data[0]
31+
u_component_of_wind_10m = surface_data[1]
32+
v_component_of_wind_10m = surface_data[2]
33+
temperature_2m = surface_data[3]
34+
35+
with nc.Dataset(
36+
os.path.join(output_dir, file_name), "w", format="NETCDF4_CLASSIC"
37+
) as nc_file:
38+
# Create dimensions
39+
nc_file.createDimension("longitude", 1440)
40+
nc_file.createDimension("latitude", 721)
41+
42+
# Create variables
43+
nc_lon = nc_file.createVariable("longitude", np.float32, ("longitude",))
44+
nc_lat = nc_file.createVariable("latitude", np.float32, ("latitude",))
45+
nc_msl = nc_file.createVariable(
46+
"mean_sea_level_pressure", np.float32, ("latitude", "longitude")
47+
)
48+
nc_u10 = nc_file.createVariable(
49+
"u_component_of_wind_10m", np.float32, ("latitude", "longitude")
50+
)
51+
nc_v10 = nc_file.createVariable(
52+
"v_component_of_wind_10m", np.float32, ("latitude", "longitude")
53+
)
54+
nc_t2m = nc_file.createVariable(
55+
"temperature_2m", np.float32, ("latitude", "longitude")
56+
)
57+
58+
# Set variable attributes
59+
nc_lon.units = "degrees_east"
60+
nc_lat.units = "degrees_north"
61+
nc_msl.units = "Pa"
62+
nc_u10.units = "m/s"
63+
nc_v10.units = "m/s"
64+
nc_t2m.units = "K"
65+
66+
# Write data to variables
67+
nc_lon[:] = np.linspace(0.125, 359.875, 1440)
68+
nc_lat[:] = np.linspace(90, -90, 721)
69+
nc_msl[:] = mean_sea_level_pressure
70+
nc_u10[:] = u_component_of_wind_10m
71+
nc_v10[:] = v_component_of_wind_10m
72+
nc_t2m[:] = temperature_2m
73+
74+
75+
def convert_upper_data_to_nc(upper_file: str, file_name: str, output_dir: str) -> None:
76+
# Load the saved numpy arrays
77+
upper_data = np.load(upper_file)
78+
geopotential = upper_data[0]
79+
specific_humidity = upper_data[1]
80+
temperature = upper_data[2]
81+
u_component_of_wind = upper_data[3]
82+
v_component_of_wind = upper_data[4]
83+
84+
with nc.Dataset(
85+
os.path.join(output_dir, file_name), "w", format="NETCDF4_CLASSIC"
86+
) as nc_file:
87+
# Create dimensions
88+
nc_file.createDimension("longitude", 1440)
89+
nc_file.createDimension("latitude", 721)
90+
nc_file.createDimension("level", 13)
91+
92+
# Create variables
93+
nc_lon = nc_file.createVariable("longitude", np.float32, ("longitude",))
94+
nc_lat = nc_file.createVariable("latitude", np.float32, ("latitude",))
95+
nc_geopotential = nc_file.createVariable(
96+
"geopotential", np.float32, ("level", "latitude", "longitude")
97+
)
98+
nc_specific_humidity = nc_file.createVariable(
99+
"specific_humidity", np.float32, ("level", "latitude", "longitude")
100+
)
101+
nc_temperature = nc_file.createVariable(
102+
"temperature", np.float32, ("level", "latitude", "longitude")
103+
)
104+
nc_u_component_of_wind = nc_file.createVariable(
105+
"u_component_of_wind", np.float32, ("level", "latitude", "longitude")
106+
)
107+
nc_v_component_of_wind = nc_file.createVariable(
108+
"v_component_of_wind", np.float32, ("level", "latitude", "longitude")
109+
)
110+
111+
# Set variable attributes
112+
nc_lon.units = "degrees_east"
113+
nc_lat.units = "degrees_north"
114+
nc_geopotential.units = "m"
115+
nc_specific_humidity.units = "kg/kg"
116+
nc_temperature.units = "K"
117+
nc_u_component_of_wind.units = "m/s"
118+
nc_v_component_of_wind.units = "m/s"
119+
# Write data to variables
120+
nc_lon[:] = np.linspace(0.125, 359.875, 1440)
121+
nc_lat[:] = np.linspace(90, -90, 721)
122+
nc_geopotential[:] = geopotential
123+
nc_specific_humidity[:] = specific_humidity
124+
nc_temperature[:] = temperature
125+
nc_u_component_of_wind[:] = u_component_of_wind
126+
nc_v_component_of_wind[:] = v_component_of_wind
127+
128+
129+
@hydra.main(version_base=None, config_path="./conf", config_name="pangu_weather.yaml")
130+
def main(cfg: Dict):
131+
output_dir = cfg.output_dir
132+
133+
convert_surface_data_to_nc(
134+
osp.join(output_dir, "output_surface.npy"), "output_surface.nc", output_dir
135+
)
136+
convert_upper_data_to_nc(
137+
osp.join(output_dir, "output_upper.npy"), "output_upper.nc", output_dir
138+
)
139+
140+
141+
if __name__ == "__main__":
142+
main()

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ nav:
104104
- VelocityGAN: zh/examples/velocity_gan.md
105105
- TGCN: zh/examples/tgcn.md
106106
- IOPS: zh/examples/iops.md
107+
- Pang-Weather: zh/examples/pangu_weather.md
107108
- 化学科学(AI for Chemistry):
108109
- Moflow: zh/examples/moflow.md
109110
- IFM: zh/examples/ifm.md

0 commit comments

Comments
 (0)