CentOS 7.6 系統上添加最新版 NetCDF 4.6.1


這里提供兩種方法來安裝 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 

 

三 參考文獻

1. 【CESM】1.2.0環境搭建擴充版

2. HDF5 Source Code

3. zlib download

4. curl release and download

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

8. CentOS 7 編譯安裝 WRF 3.8

9. 在linux下用fortran讀取netcdf文件(以WRF模式輸出的數據為例)

10. 延伸閱讀:NCL基礎講解(一)——NCL簡介

11. 延伸閱讀:NCL基礎講解(二)——NCL安裝與運行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM