这里提供两种方法来安装 netcdf:
一是 yum 安装,yum 安装时采用的是系统自带的 gfortran 编译器,版本较旧;
二是采用源码来 Makefile 编译安装的方式,它可以下载最新版的源码,并自行指定 Intel 或 PGI 编译器,适合自定义的需求。
O 系统环境
centOS 64位系统:centos-release-7-3.1611.el7.centos.x86_64
PGI 2017:PGI Compilers and Tools 17.4-0 64-bit target on x86-64 Linux -tp sandybridge
一 yum 安装 netcdf
在CentOS 下安装比较简单,两句命令即可:
[[email protected] 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 依赖软件包) ... 完毕! [[email protected]-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). https://www.hdfgroup.org/downloads/hdf5/source-code/
(3). https://curl.haxx.se/download/curl-7.55.1.tar.gz
(4).
(5).
截止netcdf 4.1.3版本,C lib和fortran lib没有分开,从4.2版本开始分开。这就导致了netcdf-fortran-4.2的安装麻烦了一点。
需要依次安装 zlib,hdf5,netcdf,netcdf-c,netcdf-fortran。
1 安装 zlib
(1). 下载地址:http://www.zlib.net/zlib-1.2.11.tar.gz
$ cd zlib-1.2.11 $ ./configure --prefix=/usr/local/netcdf4 --sharedlibdir=/opt/pgi/linux86-64/17.4/lib Checking for gcc... Checking for shared library support... Building shared library libz.so.1.2.11 with gcc. 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 $ make clean
2 安装 hdf5
下载地址:在网页 https://www.hdfgroup.org/downloads/hdf5/source-code/ 中选择版本 1.10.1,下载 gz 格式的文件。
$ cd hdf5-1.10.1$ ./configure --with-zlib=/usr/local/netcdf4 --prefix=/usr/local/netcdf4$ make$ make check $ make install ... CC H5Zdeflate.lo CC H5Zfletcher32.lo CC H5Znbit.lo CC H5Zshuffle.lo CC H5Zscaleoffset.lo CC H5Zszip.lo CC H5Ztrans.lo CCLD libhdf5.la /bin/ld: /usr/local/netcdf4/lib/libz.a(inflate.o): relocation R_X86_64_32S against `zcfree‘ can not be used when making a shared object; recompile with -fPIC /usr/local/netcdf4/lib/libz.a: error adding symbols: Bad value collect2: error: ld returned 1 exit status make[2]: *** [libhdf5.la] 错误 1 make[2]: 离开目录“/home/She/Downloads/NetCDF/hdf5-1.10.1/src” make[1]: *** [all] 错误 2 make[1]: 离开目录“/home/She/Downloads/NetCDF/hdf5-1.10.1/src” make: *** [all-recursive] 错误 1
如果安装时没有提示上述错误,则跳过步骤“1.new” 和“2.new”,直接转至步骤3。
有关这个错误的原因及解决办法,可参见这里。
这个错误一般是64 位 电脑才会出现。
解决方法为重新安装 zlib 和 hdf5,即步骤“1.new” 和“2.new”:
cd zlib-1.2.11 //进入zlib目录 CFLAGS="-O3 -fPIC" ./configure //使用64位元的方法进行编译
1.new 重新安装 zlib
$ CFLAGS="-O3 -fPIC" ./configure --prefix=/usr/local/netcdf4 --sharedlibdir=/opt/pgi/linux86-64/17.4/lib $ make check $ sudo make install [sudo] password for She: $ make clean
2.new 重新安装 hdf5
$ cd hdf5-1.10.1/ $ ./configure --with-zlib=/usr/local/netcdf4 --prefix=/usr/local/netcdf4 $ sudo make $ sudo make check $ sudo make install $ sudo make clean
3 安装netcdf
$ CPPFLAGS=-I/usr/local/netcdf4/include LDFLAGS=-L/usr/local/netcdf4/lib \ ./configure --prefix=/usr/local/netcdf4 CC=pgcc FC=pgf90 ... ... # NetCDF C Configuration Summary ============================== # General ------- NetCDF Version: 4.4.1 Configured On: Wed Sep 27 13:34:01 CST 2017 Host System: x86_64-unknown-linux-gnu Build Directory: /home/She/Downloads/NetCDF/netcdf-4.4.1 Install Prefix: /usr/local/netcdf4 # Compiling Options ----------------- C Compiler: /opt/pgi/linux86-64/17.4/bin/pgcc CFLAGS: 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: -lhdf5_hl -lhdf5 -ldl -lm -lz -lcurl # Features -------- NetCDF-2 API: yes HDF4 Support: no NetCDF-4 API: yes NC-4 Parallel Support: no PNetCDF Support: no DAP Support: yes Diskless Support: yes MMap Support: no JNA Support: no $ make $ make check ... make check-TESTS make[2]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/h5_test” make[3]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/h5_test” PASS: tst_h_files PASS: tst_h_files2 PASS: tst_h_files4 PASS: tst_h_atts PASS: tst_h_atts3 PASS: tst_h_atts4 PASS: tst_h_vars PASS: tst_h_vars2 PASS: tst_h_vars3 PASS: tst_h_grps PASS: tst_h_compounds PASS: tst_h_compounds2 PASS: tst_h_wrt_cmp PASS: tst_h_rd_cmp PASS: tst_h_vl PASS: tst_h_opaques PASS: tst_h_strings PASS: tst_h_strings1 PASS: tst_h_strings2 PASS: tst_h_ints FAIL: tst_h_dimscales PASS: tst_h_dimscales1 PASS: tst_h_dimscales2 PASS: tst_h_dimscales3 PASS: tst_h_enums PASS: tst_h_dimscales4 ============================================================================ Testsuite summary for netCDF 4.4.1 ============================================================================ # TOTAL: 26 # PASS: 25 # SKIP: 0 # XFAIL: 0 # FAIL: 1 # XPASS: 0 # ERROR: 0 ============================================================================ See h5_test/test-suite.log Please report to support-[email protected] ============================================================================ make[3]: *** [test-suite.log] 错误 1 make[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/h5_test” make[2]: *** [check-TESTS] 错误 2 make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/h5_test” make[1]: *** [check-am] 错误 2 make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/h5_test” make: *** [check-recursive] 错误 1 $ sudo make install ... Making install in CDL make[2]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples/CDL” make[3]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples/CDL” make[3]: 对“install-exec-am”无需做任何事。 make[3]: 对“install-data-am”无需做任何事。 make[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples/CDL” make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples/CDL” make[2]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples” make[3]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples” make[3]: 对“install-exec-am”无需做任何事。 make[3]: 对“install-data-am”无需做任何事。 make[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples” make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1/examples” make[1]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1” make[2]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1” /usr/bin/mkdir -p ‘/usr/local/netcdf4/bin‘ /usr/bin/install -c nc-config ‘/usr/local/netcdf4/bin‘ /usr/bin/mkdir -p ‘/usr/local/netcdf4/lib/pkgconfig‘ /usr/bin/install -c -m 644 netcdf.pc ‘/usr/local/netcdf4/lib/pkgconfig‘ /usr/bin/mkdir -p ‘/usr/local/netcdf4/lib‘ /usr/bin/install -c -m 644 libnetcdf.settings ‘/usr/local/netcdf4/lib‘ make install-data-hook make[3]: 进入目录“/home/She/Downloads/NetCDF/netcdf-4.4.1” +-------------------------------------------------------------+ | 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[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1” make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-4.4.1” $ sudo make clean
4 安装 netcdf-c 库
[[email protected] netcdf-cxx4-4.3.0]$ CPPFLAGS=-I/usr/local/netcdf4/include LDFLAGS=-L/usr/local/netcdf4/lib \./configure --prefix=/usr/local/netcdf4 CC=pgcc FC=pgf90 [[email protected] netcdf-cxx4-4.3.0]$ make [[email protected]-centos7 netcdf-cxx4-4.3.0]$ make check ... ============================================================================ Testsuite summary for netCDF-cxx4 4.3.0 ============================================================================ # TOTAL: 7 # PASS: 7 # SKIP: 0 # XFAIL: 0 # FAIL: 0 # XPASS: 0 # ERROR: 0 ============================================================================ make[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0/examples” make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0/examples” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0/examples” Making check in docs make[1]: 进入目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0/docs” make[1]: 对“check”无需做任何事。 make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0/docs” make[1]: 进入目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0” [[email protected]-centos7 netcdf-cxx4-4.3.0]$ sudo make install ... +-------------------------------------------------------------+ | Congratulations! You have successfully installed the | | new netCDF-4 C++ Library | | | | 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". | | | | 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[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0” make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-cxx4-4.3.0” [[email protected]-centos7 netcdf-cxx4-4.3.0]$ make clean
5 添加 netcdf 的环境变量
[[email protected] NetCDF]$ vi /home/She/Downloads/NetCDF/env_netcdf_LD.sh #!/bin/bash # setting for netcdf, edit by She export LD_LIBRARY_PATH=/usr/local/netcdf4/lib:${LD_LIBRARY_PATH} [[email protected] NetCDF]$ sudo cp /home/She/Downloads/NetCDF/env_netcdf_LD.sh /etc/profile.d [sudo] password for She: [[email protected] NetCDF]$ source /etc/profile.d/env_netcdf_LD.sh [[email protected] NetCDF]$ echo $LD_LIBRARY_PATH /usr/local/netcdf4/lib:
6 安装netcdf-fortran 库
[[email protected] NetCDF] cd netcdf-fortran-4.4.4[[email protected] netcdf-fortran-4.4.4]$ CPPFLAGS=-I/usr/local/netcdf4/include \ LDFLAGS=-L/usr/local/netcdf4/lib ./configure --prefix=/usr/local/netcdf4 CC=pgcc FC=pgf90 [[email protected]-centos7 netcdf-fortran-4.4.4]$ make[[email protected]-centos7 netcdf-fortran-4.4.4]$ make check Making check in fortran make[1]: 进入目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/fortran” make check-am make[2]: 进入目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/fortran” make[2]: 对“check-am”无需做任何事。 make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/fortran” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/fortran” Making check in nf_test make[1]: 进入目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/nf_test” make nf_test tst_f77_v2 ftst_vars ftst_vars2 ftst_vars3 ftst_vars4 ftst_vars5 ftst_vars6 ftst_types ftst_types2 ftst_types3 ftst_groups ftst_path ftst_rengrps f90tst_vars tst_types tst_types2 f90tst_vars_vlen tst_f90_nc4 f90tst_grps f90tst_fill f90tst_fill2 f90tst_vars3 f90tst_vars4 f90tst_vars2 f90tst_path f90tst_rengrps ftst_v2 ftest tst_f90 make[2]: 进入目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/nf_test” pgf90 -I../fortran -I../fortran -g -c -o f03lib_f_interfaces.o f03lib_f_interfaces.f90 /opt/pgi/linux86-64/17.4/bin/pgf77 -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I/usr/local/netcdf4/include -I../fortran -I../fortran -g -c -o test_get.o test_get.F /opt/pgi/linux86-64/17.4/bin/pgf77 -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I/usr/local/netcdf4/include -I../fortran -I../fortran -g -c -o test_put.o test_put.F /opt/pgi/linux86-64/17.4/bin/pgf77 -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I/usr/local/netcdf4/include -I../fortran -I../fortran -g -c -o nf_error.o nf_error.F /opt/pgi/linux86-64/17.4/bin/pgf77 -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I/usr/local/netcdf4/include -I../fortran -I../fortran -g -c -o nf_test.o nf_test.F /opt/pgi/linux86-64/17.4/bin/pgf77 -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I/usr/local/netcdf4/include -I../fortran -I../fortran -g -c -o test_read.o test_read.F /opt/pgi/linux86-64/17.4/bin/pgf77 -DHAVE_CONFIG_H -I. -I.. -I../libsrc -I/usr/local/netcdf4/include -I../fortran -I../fortran -g -c -o test_write.o test_write.F PGFTN-S-0034-Syntax error at or near / (test_write.F: 167) PGFTN-S-0034-Syntax error at or near / (test_write.F: 197) 0 inform, 0 warnings, 2 severes, 0 fatal for test_nf_redef make[2]: *** [test_write.o] 错误 2 make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/nf_test” make[1]: *** [check-am] 错误 2 make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4/nf_test” make: *** [check-recursive] 错误 1 [[email protected]-centos7 netcdf-fortran-4.4.4]$ sudo make install [sudo] password for She: ... +-------------------------------------------------------------+ | 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[3]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4” make[2]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4” make[1]: 离开目录“/home/She/Downloads/NetCDF/netcdf-fortran-4.4.4” [[email protected]-centos7 netcdf-fortran-4.4.4]$ make clean
7 再次添加 netcdf 终版的环境变量
[[email protected] NetCDF]$ vi /home/She/Downloads/NetCDF/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 [[email protected]-centos7 NetCDF]$ sudo cp /home/She/Downloads/NetCDF/env_netcdf_final.sh /etc/profile.d [sudo] password for She: [[email protected]-centos7 NetCDF]$ 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安装与运行