Skip to content

Commit 9da3565

Browse files
modify yinglong (#937)
* modify yinglong * refine
1 parent f3743db commit 9da3565

File tree

11 files changed

+127
-227
lines changed

11 files changed

+127
-227
lines changed

examples/yinglong/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# A Study of Data-driven Limited Area Model for Weather Forecasting
2+
3+
Recently, artificial intelligence-based models for forecasting global weather have been rapidly developed. Most of the global models are trained on reanalysis datasets with a spatial resolution of 0.25◦ × 0.25◦. However, study on artificial intelligence-based limited area weather forecasting models is still limited. In this study, an artificial intelligence-based limited area weather forecasting model (YingLong) is developed. YingLong utilizes a parallel structure of global and local blocks to capture multiscale meteorological features. Its predictability on surface temperature, humidity and wind speed is comparable to the predictability of the dynamical limited area model WRF-ARW, but with a much faster running speed. YingLong is also applied to investigate the issues related to the lateral boundary condition of artificial intelligence-based limited area models. The difference between artificial intelligence-based limited area models and dynamical limited area models is also discussed.
4+
5+
This code is the implementation of YingLong. We select the southeastern region of the United States, which is around the range of 80-110W, 30-42N, with 440 × 408 grid points in Lambert projection.
6+
7+
<div align=center>
8+
<img src="doc/fig_arch1.jpeg" width="70%" height="auto" >
9+
</div>
10+
11+
## Installation
12+
13+
### 1. Install PaddlePaddle
14+
15+
Please install the <font color="red"><b>2.6.0</b></font> or <font color="red"><b>develop</b></font> version of PaddlePaddle according to your environment on the official website of [PaddlePaddle](https://www.paddlepaddle.org.cn/en/install/quick?docurl=/documentation/docs/en/develop/install/pip/linux-pip_en.html).
16+
17+
For example, if your environment is linux and CUDA 11.2, you can install PaddlePaddle by the following command.
18+
19+
``` shell
20+
python -m pip install paddlepaddle-gpu==2.6.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
21+
```
22+
23+
After installation, run the following command to verify if PaddlePaddle has been successfully installed.
24+
25+
``` shell
26+
python -c "import paddle; paddle.utils.run_check()"
27+
```
28+
29+
If `"PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now."` appears, to verify that the installation was successful.
30+
31+
### 2. Install PaddleScience
32+
33+
Clone the code of PaddleScience from [here](https://github.com/PaddlePaddle/PaddleScience.git) and install requirements.
34+
35+
``` shell
36+
git clone -b develop https://github.com/PaddlePaddle/PaddleScience.git
37+
cd PaddleScience
38+
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
39+
export PYTHONPATH=$PWD
40+
```
41+
42+
## Example Usage
43+
44+
### 1. Download the data and model weights
45+
46+
``` shell
47+
cd examples/yinglong
48+
wget https://paddle-org.bj.bcebos.com/paddlescience/datasets/yinglong/western_valid_data.tar
49+
tar -xvf western_valid_data.tar
50+
wget https://paddle-org.bj.bcebos.com/paddlescience/datasets/yinglong/eastern_valid_data.tar
51+
tar -xvf eastern_valid_data.tar
52+
wget https://paddle-org.bj.bcebos.com/paddlescience/models/yinglong/inference.tar
53+
tar -xvf inference.tar
54+
```
55+
56+
### 2. Run the code
57+
58+
The following code runs the YingLong model, and the model output will be saved in `outputs_yinglong_eastern(western)/result.npy`.
59+
60+
``` shell
61+
# yinglong_eastern
62+
python predict.py -cn=yinglong_eastern.yaml
63+
# yinglong_western
64+
python predict.py -cn=yinglong_western.yaml
65+
```
66+
67+
We also visualized the predicted wind speed at 10 meters above ground level, with an initial field of 0:00 on January 1, 2022. Click [eastern](https://paddle-org.bj.bcebos.com/paddlescience/docs/Yinglong/result_eastern.gif)/[western](https://paddle-org.bj.bcebos.com/paddlescience/docs/Yinglong/result_western.gif) to view the prediction results.
68+
69+
## License
70+
71+
YingLong was released by Shanghai Zhangjiang Institute of Mathematics, Baidu inc.
72+
73+
The commercial use of these models is forbidden.

examples/yinglong1/conf/yinglong_12.yaml renamed to examples/yinglong/conf/yinglong_eastern.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ hydra:
22
run:
33
# dynamic output directory according to running time and override name
44
# dir: outputs_yinglong/${now:%Y-%m-%d}/${now:%H-%M-%S}/${hydra.job.override_dirname}
5-
dir: ./outputs_yinglong_12
5+
dir: ./outputs_yinglong_eastern
66
job:
77
name: ${mode} # name of logfile
88
chdir: false # keep current working directory unchanged
@@ -33,7 +33,7 @@ log_freq: 20
3333
# inference settings
3434
INFER:
3535
pretrained_model_path: null
36-
export_path: ./inference/yinglong_12
36+
export_path: inference/yinglong_eastern
3737
pdmodel_path: ${INFER.export_path}.pdmodel
3838
pdiparams_path: ${INFER.export_path}.pdiparams
3939
onnx_path: ${INFER.export_path}.onnx
@@ -43,13 +43,14 @@ INFER:
4343
ir_optim: false
4444
min_subgraph_size: 30
4545
gpu_mem: 100
46-
gpu_id: 0
46+
gpu_id: 3
4747
max_batch_size: 1
4848
num_cpu_threads: 10
4949
batch_size: 1
50-
mean_path: ./hrrr_example_24vars/stat/mean_crop.npy
51-
std_path: ./hrrr_example_24vars/stat/std_crop.npy
52-
input_file: ./hrrr_example_24vars/valid/2022/01/01.h5
50+
mean_path: ./eastern_valid_data/stat/mean_crop.npy
51+
std_path: ./eastern_valid_data/stat/std_crop.npy
52+
input_file: [./eastern_valid_data/valid/2022/01/01.h5, ./eastern_valid_data/valid/2022/01/02.h5, ./eastern_valid_data/valid/2022/01/03.h5]
5353
init_time: 2022/01/01/00
54-
nwp_file: ./hrrr_example_24vars/nwp_convert/2022/01/01/00.h5
55-
num_timestamps: 22
54+
nwp_file: [./eastern_valid_data/hrrr_nwp_h5/2022/01/01.h5, ./eastern_valid_data/hrrr_nwp_h5/2022/01/02.h5, ./eastern_valid_data/hrrr_nwp_h5/2022/01/03.h5]
55+
geo_file: ./eastern_valid_data/geo.h5
56+
num_timestamps: 48

examples/yinglong1/conf/yinglong_24.yaml renamed to examples/yinglong/conf/yinglong_western.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ hydra:
22
run:
33
# dynamic output directory according to running time and override name
44
# dir: outputs_yinglong/${now:%Y-%m-%d}/${now:%H-%M-%S}/${hydra.job.override_dirname}
5-
dir: ./outputs_yinglong_24
5+
dir: ./outputs_yinglong_western
66
job:
77
name: ${mode} # name of logfile
88
chdir: false # keep current working directory unchanged
@@ -33,7 +33,7 @@ log_freq: 20
3333
# inference settings
3434
INFER:
3535
pretrained_model_path: null
36-
export_path: ./inference/yinglong_24
36+
export_path: inference/yinglong_western
3737
pdmodel_path: ${INFER.export_path}.pdmodel
3838
pdiparams_path: ${INFER.export_path}.pdiparams
3939
onnx_path: ${INFER.export_path}.onnx
@@ -43,13 +43,14 @@ INFER:
4343
ir_optim: false
4444
min_subgraph_size: 30
4545
gpu_mem: 100
46-
gpu_id: 0
46+
gpu_id: 3
4747
max_batch_size: 1
4848
num_cpu_threads: 10
4949
batch_size: 1
50-
mean_path: ./hrrr_example_69vars/stat/mean_crop.npy
51-
std_path: ./hrrr_example_69vars/stat/std_crop.npy
52-
input_file: ./hrrr_example_69vars/valid/2022/01/01.h5
50+
mean_path: ./western_valid_data/stat/mean_crop.npy
51+
std_path: ./western_valid_data/stat/std_crop.npy
52+
input_file: [./western_valid_data/valid/2022/01/01.h5, ./western_valid_data/valid/2022/01/02.h5, ./western_valid_data/valid/2022/01/03.h5]
5353
init_time: 2022/01/01/00
54-
nwp_file: ./hrrr_example_69vars/nwp_convert/2022/01/01/00.h5
55-
num_timestamps: 22
54+
nwp_file: [./western_valid_data/hrrr_nwp_h5/2022/01/01.h5, ./western_valid_data/hrrr_nwp_h5/2022/01/02.h5, ./western_valid_data/hrrr_nwp_h5/2022/01/03.h5]
55+
geo_file: ./western_valid_data/geo.h5
56+
num_timestamps: 48

examples/yinglong/doc/fig_arch1.jpeg

98.1 KB
Loading

examples/yinglong1/plot.py renamed to examples/yinglong/plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import matplotlib
2424
import numpy as np
2525
import paddle
26-
from matplotlib import cm
26+
from matplotlib import colormaps as cm
2727
from matplotlib import pyplot as plt
2828
from matplotlib.legend_handler import HandlerBase
2929
from matplotlib.patches import Rectangle
@@ -102,7 +102,7 @@ def plot_weather(
102102
vmin,
103103
vmax,
104104
log_norm,
105-
cmap=cm.get_cmap("turbo", 1000),
105+
cmap=cm.get_cmap("turbo"),
106106
):
107107
ax.title.set_text(title_text)
108108
ax.set_yticks(yticks)

examples/yinglong1/predict_12layers.py renamed to examples/yinglong/predict.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,28 @@
2020
import paddle
2121
import pandas as pd
2222
from omegaconf import DictConfig
23+
from omegaconf import ListConfig
2324
from packaging import version
2425

25-
from examples.yinglong1.plot import save_plot_weather_from_dict
26-
from examples.yinglong1.predictor import YingLongPredictor
26+
from examples.yinglong.plot import save_plot_weather_from_dict
27+
from examples.yinglong.predictor import YingLongPredictor
2728
from ppsci.utils import logger
2829

2930

31+
def read_h5py(filename):
32+
"""Read hdf5 file."""
33+
if isinstance(filename, str):
34+
data = np.array(h5py.File(filename, "r")["fields"])
35+
elif isinstance(filename, ListConfig):
36+
data = [h5py.File(x, "r")["fields"] for x in filename]
37+
data = np.concatenate(data, axis=0)
38+
else:
39+
raise TypeError(
40+
f"The type of INFER.input_file must be str or list, but got {type(filename)}"
41+
)
42+
return data
43+
44+
3045
def inference(cfg: DictConfig):
3146
# log paddlepaddle's version
3247
if version.Version(paddle.__version__) != version.Version("0.0.0"):
@@ -50,15 +65,16 @@ def inference(cfg: DictConfig):
5065
# The variable name is 'z50', 'z500', 'z850', 'z1000', 't50', 't500', 't850', 'z1000',
5166
# 's50', 's500', 's850', 's1000', 'u50', 'u500', 'u850', 'u1000', 'v50', 'v500',
5267
# 'v850', 'v1000', 'mslp', 'u10', 'v10', 't2m'.
53-
input_file = h5py.File(cfg.INFER.input_file, "r")["fields"]
54-
nwp_file = h5py.File(cfg.INFER.nwp_file, "r")["fields"]
68+
input_data = read_h5py(cfg.INFER.input_file)
69+
nwp_data = read_h5py(cfg.INFER.nwp_file)
70+
geo_data = read_h5py(cfg.INFER.geo_file)
5571

5672
# input_data.shape: (1, 24, 440, 408)
57-
input_data = input_file[0:1]
73+
input_data_0 = input_data[0:1]
5874
# nwp_data.shape: # (num_timestamps, 24, 440, 408)
59-
nwp_data = nwp_file[0:num_timestamps]
75+
nwp_data = nwp_data[0:num_timestamps]
6076
# ground_truth.shape: (num_timestamps, 24, 440, 408)
61-
ground_truth = input_file[1 : num_timestamps + 1]
77+
ground_truth = input_data[1 : num_timestamps + 1]
6278

6379
# create time stamps
6480
cur_time = pd.to_datetime(cfg.INFER.init_time, format="%Y/%m/%d/%H")
@@ -68,7 +84,7 @@ def inference(cfg: DictConfig):
6884
time_stamps.append([cur_time])
6985

7086
# run predictor
71-
pred_data = predictor.predict(input_data, time_stamps, nwp_data)
87+
pred_data = predictor.predict(input_data_0, time_stamps, nwp_data, geo_data)
7288
pred_data = pred_data.squeeze(axis=1) # (num_timestamps, 24, 440, 408)
7389

7490
# save predict data
@@ -103,12 +119,14 @@ def inference(cfg: DictConfig):
103119
vmin=0,
104120
vmax=15,
105121
colorbar_label="m/s",
106-
num_timestamps=12, # only plot 12 timestamps
122+
num_timestamps=48,
107123
)
108124
logger.info(f"Save plot to {cfg.output_dir}")
109125

110126

111-
@hydra.main(version_base=None, config_path="./conf", config_name="yinglong_12.yaml")
127+
@hydra.main(
128+
version_base=None, config_path="./conf", config_name="yinglong_eastern.yaml"
129+
)
112130
def main(cfg: DictConfig):
113131
if cfg.mode == "infer":
114132
inference(cfg)

examples/yinglong1/predictor.py renamed to examples/yinglong/predictor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from omegaconf import DictConfig
2222

2323
from deploy.python_infer import base
24-
from examples.yinglong1.timefeatures import time_features
24+
from examples.yinglong.timefeatures import time_features
2525
from ppsci.utils import logger
2626

2727

@@ -57,6 +57,7 @@ def __init__(
5757
self.input_data_handle = self.predictor.get_input_handle(self.input_names[0])
5858
self.time_stamps_handle = self.predictor.get_input_handle(self.input_names[1])
5959
self.nwp_data_handle = self.predictor.get_input_handle(self.input_names[2])
60+
self.geo_data_handle = self.predictor.get_input_handle(self.input_names[3])
6061

6162
# get output names and data handles
6263
self.output_names = self.predictor.get_output_names()
@@ -95,6 +96,7 @@ def predict(
9596
input_data: np.ndarray,
9697
time_stamps: List[List[pd.Timestamp]],
9798
nwp_data: np.ndarray,
99+
geo_data: np.ndarray,
98100
batch_size: int = 1,
99101
) -> np.ndarray:
100102
"""Predicts the output of the yinglong model for the given input.
@@ -103,6 +105,7 @@ def predict(
103105
input_data (np.ndarray): Input data of shape (N, T, H, W).
104106
time_stamps (List[List[pd.Timestamp]]): Timestamps data.
105107
nwp_data (np.ndarray): NWP data.
108+
geo_data (np.ndarray): Geographic data.
106109
batch_size (int, optional): Batch size, now only support 1. Defaults to 1.
107110
108111
Returns:
@@ -118,6 +121,7 @@ def predict(
118121
self.input_names[0]: self.input_data_handle,
119122
self.input_names[1]: self.time_stamps_handle,
120123
self.input_names[2]: self.nwp_data_handle,
124+
self.input_names[3]: self.geo_data_handle,
121125
}
122126
# prepare output handle(s)
123127
output_handles = {self.output_names[0]: self.output_handle}
@@ -144,6 +148,7 @@ def predict(
144148
self.input_names[0]: input_data,
145149
self.input_names[1]: time_stamps,
146150
self.input_names[2]: nwp_data,
151+
self.input_names[3]: geo_data,
147152
}
148153

149154
# send batch input data to input handle(s)

examples/yinglong1/README.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

examples/yinglong1/doc/fig_arch1.jpg

-256 KB
Binary file not shown.

0 commit comments

Comments
 (0)