这里提供两种方法来安装 netcdf:
一是 yum 安装,yum 安装时采用的是系统自带的 gfortran 编译器,版本较旧;
二是采用源码来 Makefile 编译安装的方式,它可以下载最新版的源码,并自行指定 Intel 或 PGI 编译器,适合自定义的需求。
O 系统环境
centOS 64位系统:CentOS Linux release 7.6.1810 (Core), 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
PGI 2019:PGI Compilers and Tools 19.4-0 64-bit target on x86-64 Linux -tp sandybridge
一 yum 安装 netcdf
在CentOS 下安装比较简单,两句命令即可:
[She@she-centos7 netcdf-cxx4-4.3.0]$ sudo yum install netcdf ...========================================================================== Package 架构 版本 源 大小 ========================================================================== 正在安装: netcdf x86_64 4.3.3.1-5.el7 epel 693 k 为依赖而安装: hdf5 x86_64 1.8.12-8.el7 epel 1.6 M 事务概要 ========================================================================== 安装 1 软件包 (+1 依赖软件包) ... 完毕! [She@she-centos7 netcdf-cxx4-4.3.0]$ sudo yum install netcdf-devel ... 依赖关系解决 ========================================================================== Package 架构 版本 源 大小 ========================================================================== 正在安装: netcdf-devel x86_64 4.3.3.1-5.el7 epel 67 k 为依赖而安装: hdf5-devel x86_64 1.8.12-8.el7 epel 642 k libcurl-devel x86_64 7.29.0-35.el7.centos base 299 k 事务概要 ========================================================================== 安装 1 软件包 (+2 依赖软件包) ... 完毕!
以上方法安装的 netcdf 版本为 4.3.3.1,是2015年的版本,比较老。
二 从源码编译安装 netcdf
下面采用编译安装的方式,保证每个模块都使用当前最新的版本。
(1). http://www.zlib.net/zlib-1.2.11.tar.gz
(2). ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4/szip-2.1.tar.gz
(3) https://curl.haxx.se/download/curl-7.64.1.tar.gz
(4). https://www.hdfgroup.org/downloads/hdf5/source-code/
(5). ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.6.1.tar.gz。
(6). ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-c-4.7.0.tar.gz
(7). https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-fortran-4.4.5.tar.gz
(8). https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-cxx4-4.3.0.tar.gz
截止netcdf 4.1.3版本,C lib和fortran lib没有分开,从4.2版本开始分开。这就导致了netcdf-fortran-4.2的安装麻烦了一点。
需要依次安装 zlib,hdf5,netcdf,netcdf-c,netcdf-fortran。
1 安装 zlib
$ wget http://www.zlib.net/zlib-1.2.11.tar.gz $ tar -xvf ./zlib-1.2.11.tar.gz $ cd ./zlib-1.2.11$ env CC="pgcc -fPIC" CFLAGS="-O3" CXX="pgc++ -fPIC" CXXFLAGS="-O3" ./configure \ --prefix=/usr/local/netcdf4 --sharedlibdir=/opt/pgi/linux86-64/19.4/lib ... ... Checking for shared library support... Building shared library libz.so.1.2.11 with pgcc -fPIC. Checking for size_t... Yes. Checking for off64_t... Yes. Checking for fseeko... Yes. Checking for strerror... Yes. Checking for unistd.h... Yes. Checking for stdarg.h... Yes. Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf(). Checking for vsnprintf() in stdio.h... Yes. Checking for return value of vsnprintf()... Yes. Checking for attribute(visibility) support... Yes. $ make
$ make check
$ sudo make install
$ echo "/usr/local/netcdf4/lib/" >> /etc/ld.so.conf $ make clean
2 安装 szip
$ wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4/szip-2.1.tar.gz $ tar -xvf szip-2.1.tar.gz $ cd ./szip-2.1 $ env CC="pgcc" CFLAGS="-O2 -fPIC" CXX="pgc++" CXXFLAGS="-O2 -fPIC" ./configure 、
--prefix=/usr/local/netcdf4 --libdir=/opt/pgi/linux86-64/19.4/lib $ make $ make check $ sudo make install $ make clean
3 安装 curl
$ wget https://curl.haxx.se/download/curl-7.64.1.tar.gz
$ tar -xvf ./curl-7.64.1.tar.gz
$ cd ./curl-7.64.1
$ env CC="pgcc" CFLAGS="-O2 -fPIC" CXX="pgc++" CXXFLAGS="-O2 -fPIC" ./configure \
--prefix=/usr/local/netcdf4 --libdir=/opt/pgi/linux86-64/19.4/lib ... SSPI: no (--enable-sspi) ca cert bundle: no ca cert path: ca fallback: LDAP: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS: no (--enable-ldaps) RTSP: enabled RTMP: no (--with-librtmp) Metalink: no (--with-libmetalink) PSL: no (libpsl not found) Alt-svc: no (--enable-alt-svc) HTTP2: disabled (--with-nghttp2) Protocols: DICT FILE FTP GOPHER HTTP IMAP POP3 RTSP SMTP TELNET TFTP Features: IPv6 UnixSockets libz AsynchDNS $ make
$ make check ...
PASS: check-easy PASS: check-multi ============================================================================ Testsuite summary for curl - ============================================================================ # TOTAL: 2 # PASS: 2 # SKIP: 0 # XFAIL: 0 # FAIL: 0 # XPASS: 0 # ERROR: 0 ============================================================================ make[4]: Leaving directory `/home/She/Software/LINUX/NetCDF/curl-7.64.1/docs/libcurl' make[3]: Leaving directory `/home/She/Software/LINUX/NetCDF/curl-7.64.1/docs/libcurl' make[2]: Leaving directory `/home/She/Software/LINUX/NetCDF/curl-7.64.1/docs/libcurl' make[1]: Leaving directory `/home/She/Software/LINUX/NetCDF/curl-7.64.1/docs/libcurl'
$ sudo make install
$ make clean
4 安装 hdf5
下载地址:在网页 https://www.hdfgroup.org/downloads/hdf5/source-code/ 中选择版本 1.10.5,下载 .tar.gz 格式的文件。
$ tar -xvf ./hdf5-1.10.5.tar.gz
$ cd ./hdf5-1.10.5
$ env CC="pgcc" CFLAGS="-O3 -fPIC" FC="pgfortran" FCFLAGS="-O3 -fPIC" CXX="pgc++" \
CXXFLAGS="-O3 -fPIC" CPPFLAGS=-I/opt/pgi/linux86-64/19.4/include \
LDFLAGS=-L/opt/pgi/linux86-64/19.4/lib ./configure \
--with-zlib=/usr/local/netcdf4 --with-szlib=/usr/local/netcdf4 \
--prefix=/usr/local/netcdf4 --libdir=/opt/pgi/linux86-64/19.4/lib \
--enable-fortran=yes
$ make$ make check
$ make install
$ make clean
5 安装netcdf
下载地址位于UCAR网站 https://www.unidata.ucar.edu/downloads/netcdf/index.jsp,也可以从UCAR的FTP直接下载 ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4,选择其中的*.tar.gz格式的文件,目前的最新版本如下:
(1) NetCDF-4.6.0: ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.6.1.tar.gz
(2) netcdf-c-4.7.0: ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-c-4.7.0.tar.gz
(3) netcdf-fortran-4.4.5: https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-fortran-4.4.5.tar.gz
(4) netcdf-cxx4-4.3.0: https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-cxx4-4.3.0.tar.gz
$ wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.6.1.tar.gz
$ tar -xvf ./netcdf-4.6.1.tar.gz
$ cd netcdf-4.6.1 $ env CC="pgcc" CFLAGS="-O3 -fPIC" FC="pgf90" FCFLAGS="-O3 -fPIC" CXX="pgc++" CXXFLAGS="-O3 -fPIC" \
CPPFLAGS=-I/usr/local/netcdf4/include LDFLAGS=-L/usr/local/netcdf4/lib ./configure \
--disable-dap --with-zlib=/usr/local/netcdf4 --with-szlib=/usr/local/netcdf4 \
--libdir=/opt/pgi/linux86-64/19.4/lib --prefix=/usr/local/netcdf4 $ make
$ make check ... ============================================================================ Testsuite summary for netCDF 4.6.1 ============================================================================ # TOTAL: 2 # PASS: 2 # SKIP: 0 # XFAIL: 0 # FAIL: 0 # XPASS: 0 # ERROR: 0 ============================================================================ $ sudo make install ... +-------------------------------------------------------------+ | Congratulations! You have successfully installed netCDF! | | | | You can use script "nc-config" to find out the relevant | | compiler options to build your application. Enter | | | | nc-config --help | | | | for additional information. | | | | CAUTION: | | | | If you have not already run "make check", then we strongly | | recommend you do so. It does not take very long. | | | | Before using netCDF to store important data, test your | | build with "make check". | | | | NetCDF is tested nightly on many platforms at Unidata | | but your platform is probably different in some ways. | | | | If any tests fail, please see the netCDF web site: | | http://www.unidata.ucar.edu/software/netcdf/ | | | | NetCDF is developed and maintained at the Unidata Program | | Center. Unidata provides a broad array of data and software | | tools for use in geoscience education and research. | | http://www.unidata.ucar.edu | +-------------------------------------------------------------+ $ make clean
6 安装 netcdf-c 库
$ wget https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-c-4.7.0.tar.gz
$ tar -xvf ./netcdf-c-4.7.0.tar.gz
$ cd netcdf-c-4.7.0 $ env CC="pgcc" CFLAGS="-O3 -fPIC" FC="pgf90" FCFLAGS="-O3 -fPIC" CXX="pgc++" CXXFLAGS="-O3 -fPIC" \
CPPFLAGS=-I/usr/local/netcdf4/include LDFLAGS=-L/usr/local/netcdf4/lib ./configure \
--disable-dap --with-zlib=/usr/local/netcdf4 --with-szlib=/usr/local/netcdf4 \
--libdir=/opt/pgi/linux86-64/19.4/lib --includedir=/opt/pgi/linux86-64/19.4/include \
--prefix=/usr/local/netcdf4
$ make
$ make check
$ sudo make install
$ make clean ...
============================== # General ------- NetCDF Version: 4.7.0 Configured On: Mon May 13 14:54:33 CST 2019 Host System: x86_64-pc-linux-gnu Build Directory: /home/She/Software/LINUX/NetCDF/netcdf-c-4.7.0 Install Prefix: /usr/local/netcdf4 # Compiling Options ----------------- C Compiler: /opt/pgi/linux86-64/19.4/bin/pgcc CFLAGS: -O3 -fPIC CPPFLAGS: -I/usr/local/netcdf4/include LDFLAGS: -L/usr/local/netcdf4/lib AM_CFLAGS: AM_CPPFLAGS: AM_LDFLAGS: Shared Library: yes Static Library: yes Extra libraries: -lsz -lhdf5_hl -lhdf5 -lm -ldl -lz # Features -------- NetCDF-2 API: yes HDF4 Support: no HDF5 Support: yes NetCDF-4 API: yes NC-4 Parallel Support: no PnetCDF Support: no DAP2 Support: no DAP4 Support: no Byte-Range Support: no Diskless Support: yes MMap Support: no JNA Support: no CDF5 Support: yes ERANGE Fill Support: no Relaxed Boundary Check: yes $ make check ... ============================================================================ Testsuite summary for netCDF 4.7.0 ============================================================================ # TOTAL: 3 # PASS: 3 # SKIP: 0 # XFAIL: 0 # FAIL: 0 # XPASS: 0 # ERROR: 0
$ sudo make install ... +-------------------------------------------------------------+ | Congratulations! You have successfully installed netCDF! | | | | You can use script "nc-config" to find out the relevant | | compiler options to build your application. Enter | | | | nc-config --help | | | | for additional information. | | | | CAUTION: | | | | If you have not already run "make check", then we strongly | | recommend you do so. It does not take very long. | | | | Before using netCDF to store important data, test your | | build with "make check". | | | | NetCDF is tested nightly on many platforms at Unidata | | but your platform is probably different in some ways. | | | | If any tests fail, please see the netCDF web site: | | http://www.unidata.ucar.edu/software/netcdf/ | | | | NetCDF is developed and maintained at the Unidata Program | | Center. Unidata provides a broad array of data and software | | tools for use in geoscience education and research. | | http://www.unidata.ucar.edu | +-------------------------------------------------------------+ $ make clean
7 添加 netcdf 的环境变量
$ vi ~/env_netcdf_LD.sh
#!/bin/bash
# setting for netcdf, edit by She
export LD_LIBRARY_PATH=/usr/local/netcdf4/lib:${LD_LIBRARY_PATH}
$ sudo cp ~/env_netcdf_LD.sh /etc/profile.d
$ source /etc/profile.d/env_netcdf_LD.sh
$ echo $LD_LIBRARY_PATH
/usr/local/netcdf4/lib:/usr/lib64/openmpi/lib
8 安装netcdf-fortran 库
$ wget https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-fortran-4.4.5.tar.gz $ tar -xvf ./netcdf-fortran-4.4.5.tar.gz $ cd netcdf-fortran-4.4.5 $ env CC="pgcc" CFLAGS="-O3 -fPIC" FC="pgf90" FCFLAGS="-O3 -fPIC" CXX="pgc++" CXXFLAGS="-O3 -fPIC" \.... ...
F90="pgf90" FCFLAGS="-O3 -fPIC" CPPFLAGS=-I/usr/local/netcdf4/include LDFLAGS=-L/usr/local/netcdf4/lib \
./configure --libdir=/opt/pgi/linux86-64/19.4/lib --includedir=/opt/pgi/linux86-64/19.4/include \
--prefix=/usr/local/netcdf4 $ make
$ make check
$ sudo make install
+-------------------------------------------------------------+
| Congratulations! You have successfully installed the netCDF |
| Fortran libraries. |
| |
| You can use script "nf-config" to find out the relevant |
| compiler options to build your application. Enter |
| |
| nf-config --help |
| |
| for additional information. |
| |
| CAUTION: |
| |
| If you have not already run "make check", then we strongly |
| recommend you do so. It does not take very long. |
| |
| Before using netCDF to store important data, test your |
| build with "make check". |
| |
| NetCDF is tested nightly on many platforms at Unidata |
| but your platform is probably different in some ways. |
| |
| If any tests fail, please see the netCDF web site: |
| http://www.unidata.ucar.edu/software/netcdf/ |
| |
| NetCDF is developed and maintained at the Unidata Program |
| Center. Unidata provides a broad array of data and software |
| tools for use in geoscience education and research. |
| http://www.unidata.ucar.edu |
+-------------------------------------------------------------+
$ make clean
在 make check 时,可能会提示ftst_rengrps.F文件的一个小错误,错误内容如下:
$ make check
...
... pgfortran -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I../fortran -I../fortran -I/usr/local/netcdf4/include -I../fortran -g -c -o ftst_rengrps.o ftst_rengrps.F PGF90-S-0034-Syntax error at or near identifier nclude (ftst_rengrps.F: 13) PGF90/x86-64 Linux 19.4-0: compilation completed with severe errors make[2]: *** [ftst_rengrps.o] Error 2 make[2]: Leaving directory `/home/xxx/Software/LINUX/NetCDF/netcdf-fortran-4.4.5/nf_test' make[1]: *** [check-am] Error 2 make[1]: Leaving directory `/home/xxx/Software/LINUX/NetCDF/netcdf-fortran-4.4.5/nf_test' make: *** [check-recursive] Error 1
解决办法是,在源路径中找到文件“ftst_rengrps.F”,在第13行的开头增加一个空格,重新编译即可。
$ cat /home/She/Software/LINUX/NetCDF/netcdf-fortran-4.4.5/nf_test/ftst_rengrps.F | grep nclude include "netcdf.inc" include "netcdf.inc" $ head -n 15 /home/She/Software/LINUX/NetCDF/netcdf-fortran-4.4.5/nf_test/ftst_rengrps.F
C This is part of the netCDF package. C Copyright 2006 University Corporation for Atmospheric Research/Unidata. C See COPYRIGHT file for conditions of use. C This program tests netCDF-4 variable functions from fortran. C $Id: ftst_rengrps.F,v 1.3 2010/02/03 14:35:21 ed Exp $ program ftst_rengrps C use typeSizes C use netcdf C use netcdf4_f03 implicit none include "netcdf.inc" (修改前:错误的句子,不符合固定格式的写法) include "netcdf.inc" (修改后:正确)
9 再次添加 netcdf 终版的环境变量
$ vi ~/env_netcdf_final.sh #!/bin/bash # setting for netcdf, edit by She export NETCDF=/usr/local/netcdf4 export PATH=$PATH:$NETCDF/bin:$NETCDF/include:$NETCDF/lib export MANPATH=$MANPATH:$NETCDF/share/man$ sudo cp ~/env_netcdf_final.sh /etc/profile.d $ source /etc/profile.d/env_netcdf_final.sh
三 参考文献
5. netcdf-4.4.1 FTP source download
6. netcdf-cxx4-4.3.0.tar.gz FTP source download
7. netcdf-fortran-4.4.4.tar.gz FTP source download
9. 在linux下用fortran读取netcdf文件(以WRF模式输出的数据为例)
10. 延伸阅读:NCL基础讲解(一)——NCL简介
11. 延伸阅读:NCL基础讲解(二)——NCL安装与运行