如何在ubuntu16.04上安装编译WRF4.5.2和WPS4.5.0

为了方便安装,这里已经搜集了所有教程中可能使用到的数据、软件和所需库,可点击百度网盘分享链接进行下载,这里下载完成后教程后面提到的下载可无需进行再次下载。
百度网盘分享链接:https://pan.baidu.com/s/1gdu_VTehi5VX6XXknZMCLw
提取码:rbpn

1. 环境测试

~/coding文件夹下创建文件夹WRF,进入该文件夹创建两个文件夹,分别为Build_WRFTESTS

1
2
3
cd ~/coding/WRF
mkdir Build_WRF TESTS
cd TESTS

运行一些测试来验证Fortran编译器和C编译器是否可用。

1
sudo apt-get install csh gfortran m4 build-essential
1
2
3
4
which gfortran
which cpp
which gcc
gcc --version

下载测试包Fortran and C Tests Tar File

将测试包解压

1
tar -xf Fortran_C_tests.tar

共有7项测试,请一次一次完成

Test #1:  TEST_1_fortran_only_fixed.f

在命令行输入以下内容:

gfortran TEST_1_fortran_only_fixed.f

现在输入:

./a.out

以下内容将出现在屏幕上:

SUCCESS test 1 fortran only fixed format

Test #2: TEST_2_fortran_only_free.f90

在命令行输入以下内容:

gfortran TEST_2_fortran_only_free.f90

然后输入:

./a.out

以下内容将出现在屏幕上:

Assume Fortran 2003: has FLUSH, ALLOCATABLE, derived type, and ISO C Binding
SUCCESS test 2 fortran only free format

Test #3: C: TEST_3_c_only.c

在命令行输入以下内容:

gcc TEST_3_c_only.c

然后输入:

./a.out

以下内容将出现在屏幕上:

SUCCESS test 3 c only

Test #4: : TEST_4_fortran+c_c.c, and TEST_4_fortran+x_f.f90

在命令行输入以下内容:

gcc -c -m64 TEST_4_fortran+c_c.c

然后输入:

gfortran -c -m64 TEST_4_fortran+c_f.f90

*然后输入:

gfortran -m64 TEST_4_fortran+c_f.o TEST_4_fortran+c_c.o

然后输入:

./a.out

以下内容将出现在屏幕上:

C function called by Fortran
Values are xx = 2.00 and ii = 1
SUCCESS test 4 fortran calling c

Test #5: csh 

在命令行输入以下内容:

./TEST_csh.csh

以下内容将出现在屏幕上:

SUCCESS csh test

Test #6: perl

在命令行输入以下内容:

./TEST_perl.pl

以下内容将出现在屏幕上:

SUCCESS perl test

Test #7: sh

 在命令行输入以下内容:

./TEST_sh.sh

以下内容将出现在屏幕上:

SUCCESS sh test

2. 环境设置

在~/.bashrc中添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
###################  WRF 4.5.2 + WPS 4.5.0  ############################

export DIR=/home/root123/coding/WRF/Build_WRF/LIBRARIES
export CC=gcc
export CXX=g++
export FC=gfortran
export FCFLAGS=-m64
export F77=gfortran
export FFLAGS=-m64
export LDFLAGS=-L$DIR/grib2/lib
export CPPFLAGS=-I$DIR/grib2/include
export CFLAGS=-m64
export JASPERLIB=$DIR/grib2/lib
export JASPERINC=$DIR/grib2/include

export PATH=$DIR/netcdf/bin:$PATH
export NETCDF=$DIR/netcdf/
export PATH=$DIR/mpich/bin:$DIR/mpich/lib:$DIR/mpich/include:$PATH

export PATH=$NETCDF:$NETCDF/bin:$PATH
export LIBS="-lnetcdf -lz"
export PATH=$NETCDF/lib/:$PATH
export LD_LIBRARY_PATH=$NETCDF/lib:${LD_LIBRARY_PATH}
export NETCDF_INC="$NETCDF/include"
export NETCDF_LIB="$NETCDF/lib"
export PATH=$NETCDF_LIB:$NETCDF_INC:$PATH

export WRF_DIR=~/coding/WRF/Build_WRF/WRFV4.5.2
export WRFIO_NCD_NO_LARGE_FILE_SUPPORT=1
export HDF5PATH=$NETCDF

更改完成后,打开终端输入source ~/.bashrc

3. 构建库

3.0 下载所需库

下载地址:Index of /wrf/OnLineTutorial/compile_tutorial/tar_files

需要下载的库如下

1
2
3
4
5
netcdf-4.1.3.tar.gz
mpich-3.0.4.tar.gz
zlib-1.2.7.tar.gz
libpng-1.2.50.tar.gz
jasper-1.900.1.tar.gz
1
2
3
cd Build_WRF
mkdir LIBRARIES
LIBRARIES

3.1 NetCDF

1
2
3
4
5
6
7
# 安装netcdf-c
tar xzvf netcdf-4.1.3.tar.gz #or just .tar if no .gz present
cd netcdf-4.1.3
./configure --prefix=$DIR/netcdf --disable-dap --disable-netcdf-4 --disable-shared
make
make install
cd ..

3.2 MPICH

1
2
3
4
5
6
tar xzvf mpich-3.0.4.tar.gz     #or just .tar if no .gz present
cd mpich-3.0.4
./configure --prefix=$DIR/mpich
make
make install
cd ..

3.3 zlib

1
2
3
4
5
6
tar xzvf zlib-1.2.11.tar.gz     #or just .tar if no .gz present
cd zlib-1.2.11
./configure --prefix=$DIR/grib2
make
make install
cd ..

3.4 libpng

1
2
3
4
5
6
tar xzvf libpng-1.2.50.tar.gz     #or just .tar if no .gz present
cd libpng-1.2.50
./configure --prefix=$DIR/grib2
make
make install
cd ..

3.5 Jasper

1
2
3
4
5
6
tar xzvf jasper-1.900.1.tar.gz     #or just .tar if no .gz present
cd jasper-1.900.1
./configure --prefix=$DIR/grib2
make
make install
cd ..

4. 库兼容性测试

下载测试包Fortran_C_NETCDF_MPI_tests.tar

Test #1: Fortran + C + NetCDF

1
2
3
4
5
6
cp ${NETCDF}/include/netcdf.inc .

gfortran -c 01_fortran+c+netcdf_f.f
gcc -c 01_fortran+c+netcdf_c.c
gfortran 01_fortran+c+netcdf_f.o 01_fortran+c+netcdf_c.o -L${NETCDF}/lib -lnetcdff -lnetcdf
./a.out

以下内容将出现在屏幕上:

C function called by Fortran
Values are xx = 2.00 and ii = 1
SUCCESS test 1 fortran + c + netcdf

Test #2: Fortran + C + NetCDF + MPI

1
2
3
4
5
6
cp ${NETCDF}/include/netcdf.inc .

mpif90 -c 02_fortran+c+netcdf+mpi_f.f
mpicc -c 02_fortran+c+netcdf+mpi_c.c
mpif90 02_fortran+c+netcdf+mpi_f.o 02_fortran+c+netcdf+mpi_c.o -L${NETCDF}/lib -lnetcdff -lnetcdf
mpirun ./a.out

以下内容将出现在屏幕上:

C function called by Fortran
Values are xx = 2.00 and ii = 1
status = 2
SUCCESS test 2 fortran + c + netcdf + mpi

5. 构建WRF

Build_WRF文件夹中解压WRFv4.5.2.tar.gz,解压后文件夹名为WRFV4.5.2,进入该文件夹。

编译前使用命令uname -a查看电脑详细信息。

1
2
cd WRFV4.5.2
./configure

这里注意使用./configure命令编译WRF时,从以下32、33、34中选择,推荐34;

1
32. (serial) 33. (smpar) 34. (dmpar) 35. (dm+sm) GNU (gfortran/gcc)

Compile for nesting?选择1=basic

出现如下则说明安装成功。

1
2
Configuration successful! 
------------------------------------------------------------------------
1
2
3
4
5
6
Testing for NetCDF, C and Fortran compiler

This installation of NetCDF is 64-bit
C compiler is 64-bit
Fortran compiler is 64-bit
It will build in 64-bit

编译 em_real

1
2
./compile em_real >& log.compile
cd ..

编译成功log.compile会出现如下类似内容。

1
2
3
4
5
6
7
8
--->                  Executables successfully built                  <---

-rwxrwxr-x 1 root123 root123 45400664 3月 21 10:49 main/ndown.exe
-rwxrwxr-x 1 root123 root123 45286000 3月 21 10:49 main/real.exe
-rwxrwxr-x 1 root123 root123 44823216 3月 21 10:49 main/tc.exe
-rwxrwxr-x 1 root123 root123 49968288 3月 21 10:48 main/wrf.exe

==========================================================================

编译成功后会在main文件夹内出现以下文件,同时也可根据是否生成exe文件来检查是否编译成功。

1
2
3
4
wrf.exe
real.exe
ndown.exe
tc.exe

6. 构建WPS

Build_WRF文件夹中解压WPS-master.zip,解压后文件夹更改为WPSV4.5.0,进入该文件夹。

1
2
3
cd WPSV4.5.0
./clean
./configure --prefix=$DIR/netcdf CC=mpicc CXX=mpicxx FC=mpif90
1
2
3
4
5
6
^M: Command not found.
^M: Command not found.
then^M: Command not found.
endif^M: Command not found.
^M: Command not found.
set: Syntax Error.

或执行./configure出现错误bash: ./configure: /bin/sh^M: 解释器错误: 没有那个文件或目录

解决办法

进入WPSV4.5.0文件夹内输入以下命令即可。

1
2
sed -i 's/\r$//' clean
sed -i 's/\r$//' configure

再输入

1
2
./clean
./configure --prefix=$DIR/netcdf CC=mpicc CXX=mpicxx FC=mpif90

编译WPS

1
2
sed -i 's/\r$//' compile
./compile >& log.compile

如果构建成功,WPSV4.5.0目录下应该存在3个可执行文件,链接到相应的src/目录。

1
2
3
geogrid.exe -> geogrid/src/geogrid.exe
ungrib.exe -> ungrib/src/ungrib.exe
metgrid.exe -> metgrid/src/metgrid.exe

7. 静态地理数据

  1. 选择 Download Highest Resolution of each Mandatory Field 下载静态地理数据,这些是运行WPS和WRF所需的所有强制字段,包含每个必填字段的最高分辨率这是最常用的字段,建议用于大多数应用。如对其他字段感兴趣,可以直接访问WPS V4地理静态数据下载页面进行查看和下载。

  2. 下载完成后,解压完成后的文件名也许为geog。将文件重命名为WPS_GEOG

  3. Build_WRF文件夹下新建文件夹WPS_Data,将解压后的文件夹WPS_GEOG移动到./WPS_Data/WPS_Geographical_Static_Data/目录下。

  4. 修改namelist.wps文件中&geogrid部分的geog_data_path变量,geog_data_path='/home/root123/coding/WRF/Build_WRF/WPS_Data/WPS_Geographical_Static_Data/WPS_GEOG'

    *为了确保不出错,最好将高分辨率(Highest Resolution)数据和全部数据(geog_complete)都下载好的数据放入上面创建的WPS_GEOG文件夹中。

8. 实时气象数据

数据下载地址:https://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/

也可使用wget命令下载GFS数据,以2024-03-12: 00时数据为例,该数据时间分辨率为6h(21600s)。

1
2
3
4
5
wget https://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.20240312/00/atmos/gfs.t00z.pgrb2.0p25.f000

wget https://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.20230312/00/atmos/gfs.t00z.pgrb2.0p25.f003

wget https://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.20230312/00/atmos/gfs.t00z.pgrb2.0p25.f006

Build_WRF文件夹下创建文件夹DATA,将下载的GFS数据移动到DATA文件夹下。

9. 运行WPS和WRF

9.1 运行WPS

进入WPSV4.5.0文件夹内,运行WPS。在运行之前,修改namelist.wps中的geog-data_path变量,注意一定要是绝对路径,不包含~

1
geog_data_path='/home/root123/coding/WRF/Build_WRF/WPS_Data/WPS_Geographical_Static_Data/WPS_GEOG

另外将namelist.wps文件中的内容替换为以下内容,以GFS文件gfs.t00z.pgrb2.0p25.f000为例。该文件为2024-03-12: 00时间点的气象数据,时间分辨率为6h(21600s)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
&share
wrf_core = 'ARW',
max_dom = 2,
start_date = '2024-03-12_00:00:00','2024-03-12_00:00:00',
end_date = '2024-03-12_00:00:00','2024-03-12_00:00:00',
interval_seconds = 21600,
io_form_geogrid = 2
/

&geogrid
parent_id = 1, 1,
parent_grid_ratio = 1, 3,
i_parent_start = 1, 10,
j_parent_start = 1, 20,
e_we = 150, 220,
e_sn = 130, 214,
geog_data_res = 'default','default',
dx = 15000,
dy = 15000,
map_proj = 'lambert',
ref_lat = 28.00,
ref_lon = 104.00,
truelat1 = 28.0,
truelat2 = 28.0,
stand_lon = 104.0,
geog_data_path = '/home/root123/coding/WRF/Build_WRF/WPS_Data/WPS_Geographical_Static_Data/WPS_GEOG'
/

&ungrib
out_format = 'WPS',
prefix = 'FILE',
/

&metgrid
fg_name = 'FILE'
/

修改完成后,开始运行程序。

  1. 执行geogrid.exe, 这一步将生成geo_em文件

    1
    2
    3
    cd WPSV4.5.0

    ./geogrid.exe >& log.geogrid

geogrid.exe执行成功后,会在geogrid.log 文件末尾出现以下内容。

1
2024-03-21 16:58:31.070 ---  *** Successful completion of program geogrid.exe ***
  1. 执行ungrib.exe,这一步将生成前缀为’FILE’的文件。

    1
    2
    3
    ./link_grib.csh ../DATA/gfs.t00z.pgrb2.0p25.f000
    ln -sf ungrib/Variable_Tables/Vtable.GFS Vtable
    ./ungrib.exe >& log.ungrib

    ungrib.exe执行成功后,会在ungrib.log文件末尾出现以下内容。

    1
    2
    3
    4
    2024-03-21 17:13:42.605 --- ****  Deleting temporary files created by ungrib...
    2024-03-21 17:13:42.605 --- Deleting file: ./PFILE:2024-03-12_00
    2024-03-21 17:13:42.756 --- **** Done deleting temporary files.
    2024-03-21 17:13:42.756 --- *** Successful completion of program ungrib.exe ***
  2. 执行metgrid.exe,这一步将生成每个时间段的前缀为met_em*的文件

    1
    ./metgrid.exe >& log.metgrid

metgrid.exe执行成功后,会在metgrid.log文件末尾出现以下内容。

1
2024-03-21 17:24:05.809 ---  *** Successful completion of program metgrid.exe ***

9.2 运行WRF

进入WRFV4.5.2/test/em_real/目录下。首先将刚才WPSV4.5.0目录下生成的前缀为met_em*的文件拷贝到WRFV4.5.2/test/em_real/目录下。

1
2
3
cp met_em* ../WRFV4.5.2/test/em_real/
cd ../WRFV4.5.2/test/em_real/
mpirun -np 1 ./real.exe

这将生成两个文件,分别为rsl.error.0000rsl.out.0000。如果在rsl.error.0000文件中看到SUCCESS,并且生成wrfbdy_d01wrfinput_d0*文件,证明运行成功,继续执行

1
mpirun -np 8 ./wrf.exe

再次检查rsl文件是否SUCCESS,并确保拥有预期所有的wrfout*文件。如果有,则运行成功。

参考文档

  1. 知乎-Ubuntu中配置WRF

  2. WRF-How to Compile WRF: The Complete Process


如何在ubuntu16.04上安装编译WRF4.5.2和WPS4.5.0
https://singyutang.github.io/2024/03/17/如何在ubuntu16-04上安装编译WRF4-5-2、WRF-hydro5-2-0和WPS4-5-0/
作者
SingyuTang
发布于
2024年3月17日
许可协议