What is NetCDF? NetCDF (Network Common Data Form) is a set of software libraries and self-describing, machine-independent data formats that support the creation, access, and sharing of array-oriented scientific data. The project homepage is hosted by the Unidata program at the University Corporation for Atmospheric Research (UCAR). They are also the chief source of netCDF software, standards development, updates, etc. The format is an open standard. NetCDF Classic and 64-bit Offset Format are an international standard of the Open Geospatial Consortium . Displaying netCDF data under ArcGIS 10 以 sst.mon.ltm.1981-2010.nc ( details )文件为例, SST ( Sea Surface Temperature )在 ArcGIS10.2 中显示如图 1 。 图 1 Matlab 操作 以 sst.mon.ltm.1981-2010.nc 文件为例, Matlab 操作读入及输出为 GeoTiff 文件。 Spatial Coverage: 1.0 degree latitude x 1.0 degree longitude global grid (360x180). 89.5N - 89.5S, 0.5E - 359.5E. Missing grids are 9.999e+20(ltm). 代码运行生产 Jan.~Dec. 全球 SST ,程序运行流畅。 附上代码及 测 试数据( NCTest.rar )。 See Also Matlab netcdf package ;
当出现此类问题的时候 光靠 gfortran -o test w02.f90 -I/usr/local/netcdf/include -L/usr/local/netcdf/lib -lnetcdf 不能解决的,可以尝试gfortran -o test w02.f90 -I/usr/local/netcdf/include -L/usr/local/netcdf/lib -lnetcdf -lnetcdff 或gfortran -o test w02.f90 -I/usr/local/netcdf/include -L/usr/local/netcdf/lib -lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lz 附上错误的提示,供以后参考 /tmp/ccKcBo4F.o: In function `MAIN__': w02.f90:(.text+0x39): undefined reference to `nf_create_' w02.f90:(.text+0x71): undefined reference to `nf_def_dim_' w02.f90:(.text+0xa9): undefined reference to `nf_def_dim_' w02.f90:(.text+0xfc): undefined reference to `nf_def_var_' w02.f90:(.text+0x115): undefined reference to `nf_enddef_' w02.f90:(.text+0x14e): undefined reference to `nf_close_' /tmp/ccKcBo4F.o: In function `write_data_0_': w02.f90:(.text+0x1a7): undefined reference to `nf_put_vara_int_' /tmp/ccKcBo4F.o: In function `check_err_': w02.f90:(.text+0x21d): undefined reference to `nf_strerror_' collect2: ld 返回 1
最近装netcdf, 发现网上的许多方法都不完善,许多库文件与头文件的位置没有加入默认的文件夹里,以至于安装netcdf总会提醒缺少这个库,那个库的。亲自动手,记录安装过程,以便查询 装netcdf 需要 zlib szip netcdf hdf5 curl mpich2 这些软件 其中zlib,szip是hdf5与netcdf需要的库文件,二者可以二选一。 (1) 下载这些源代码 (2) 安装zlib tar -zxvf zlib***.tar.gz cd zlib* mkdir /usr/local/zlib ./configure --prefix=/usr/local/zlib --libdir=/usr/lib/ --includedir=/usr/lib/ --sharedlibdir=/usr/share 其中 --libdir=/usr/lib/ --includedir=/usr/lib/一定写上,把库文件与头文件装在系统默认搜寻的地方,免得后面设置LD_LIBRARY_PATH ,如果不懂可用./configure --help 查看 make make check make install (3)安装szip tar -zxvf szip***.tar.gz cd szip* mkdir /usr/local/szip ./configure --prefix=/usr/local/szip --libdir=/usr/lib/ --includedir=/usr/lib/ --sharedstatedir=/usr/share --bindir=/usr/bin/ 其中--bindir=/usr/bin/表示把可执行的二进制的文件放入默认的地方,就不用设置PATH了 make make check make install (3) 安装curl tar -zxvf curl***.tar.gz cd curl* mkdir /usr/local/curl ./configure --prefix=/usr/local/curl --libdir=/usr/lib/ --includedir=/usr/include/ --sharedstatedir=/usr/share --bindir=/usr/bin/ make make check make install (4)安装mpich2 tar -zxvf mpich***.tar.gz cd mpich* mkdir /usr/local/mpich ./configure --prefix=/usr/local/mpich --libdir=/usr/lib/ --includedir=/usr/lib/ --sharedstatedir=/usr/share --bindir=/usr/bin/ make make check make install (5) 安装hdf5 tar -zxvf hdf5***.tar.gz cd hdf5* mkdir /usr/local/hdf5 ./configure --prefix=/usr/local/hdf5 --libdir=/usr/lib/ --includedir=/usr/lib/ --sharedstatedir=/usr/share --bindir=/usr/bin/ make make check make install (6)安装netcdf tar -zxvf netcdf***.tar.gz cd netcdf* mkdir /usr/local/netcdf ./configure --prefix=/usr/local/netcdf --libdir=/usr/lib/ --includedir=/usr/lib/ --sharedstatedir=/usr/share --bindir=/usr/bin/ 如果装了intel编译器,会提示math.h的错误,加上CC=icc make make check make install 完美收官!
1: NetCDF编译过程 cdnetcdf-4.1.3 ./configure --disable-dap --disable-netcdf-4 --prefix=/usr/local/netcdf ( 根据 http://hi.baidu.com/liuxiaogis/blog/item/6f662e38d5a03b3c96ddd81a.html 的说法,加上--disable-dap的原因是缺少一个‘curl’的lib, --disable-netcdf-4是报错中提出的解决办法 ) 但是连这一步都没有通过, ....................... checking whether ifort accepts -g... no checking for Fortran flag to compile .f90 files... unknown configure: error: Fortran could not compile .f90 files configure: error: ./configure failed for libcf 在 --prefix=/usr/local/netcdf之后加上FC=gfortran之后 ./configure --disable-dap --disable-netcdf-4--prefix=/usr/local/netcdfFC=gfortran 编译通过! 2: make 直接输入 make 出现 ....................... make : Nothing to be done for `all-am'. make : Leaving directory `/home/dinghao/netcdf-4.1.3/examples' make : Leaving directory `/home/dinghao/netcdf-4.1.3/examples' make : Entering directory `/home/dinghao/netcdf-4.1.3' make : Leaving directory `/home/dinghao/netcdf-4.1.3' make : Leaving directory `/home/dinghao/netcdf-4.1.3' 在/usr/local/netcdf/ 目录下没有出现任何东西!?confused! 3: make check 步骤 键入 make check 出现 ....................... ================== All 7 tests passed ================== make : Leaving directory `/home/dinghao/netcdf-4.1.3/examples/CXX' make : Leaving directory `/home/dinghao/netcdf-4.1.3/examples/CXX' make : Entering directory `/home/dinghao/netcdf-4.1.3/examples' make : Nothing to be done for `check-am'. make : Leaving directory `/home/dinghao/netcdf-4.1.3/examples' make : Leaving directory `/home/dinghao/netcdf-4.1.3/examples' make : Entering directory `/home/dinghao/netcdf-4.1.3' make : Leaving directory `/home/dinghao/netcdf-4.1.3' check 居然通过! 4: make install 步骤 键入 sudomake install 最后显示 ........................... make install-data-hook make : Entering directory `/home/dinghao/netcdf-4.1.3' +-------------------------------------------------------------+ | 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 : Leaving directory `/home/dinghao/netcdf-4.1.3' make : Leaving directory `/home/dinghao/netcdf-4.1.3' make : Leaving directory `/home/dinghao/netcdf-4.1.3' 说明安装成功! 5: 测试 下面是一个已编好的用来测试NetCDF的小程序simple_xy_wr(由刘情操提供 http://blog.sina.com.cn/s/blog_4b1d9e7b0100zg89.html ,特此鸣谢)或者用/netcdf-4.1.3/example/F90/里面的例子来测试: program simple_xy_wr use netcdf implicit none ! 定义数据文件的名称 character (len = *), parameter :: FILE_NAME = "simple_xy.nc" ! 写一个12*6的二维数据 integer, parameter :: NDIMS = 2 integer, parameter :: NX = 6, NY = 12 ! 当创建netCDF文件的时候,变量和维数都有一个对应的ID integer :: ncid, varid, dimids(NDIMS) integer :: x_dimid, y_dimid integer :: x, y ! 要保存到文件的数据数组 integer, dimension(:,:), allocatable :: data_out ! 为数据数组分配内存 allocate(data_out(NY, NX)) ! 随意往数据数组里写一些数据 do x = 1, NX do y = 1, NY data_out(y, x) = (x - 1) * NY + (y - 1) end do end do ! 创建netCDF文件,返回文件对应的ID,如果存在则覆盖,check子程序用来检验执行是否成功 call check( nf90_create(FILE_NAME, NF90_CLOBBER, ncid) ) ! 定义维数,返回一个对应的ID call check( nf90_def_dim(ncid, "x", NX, x_dimid) ) call check( nf90_def_dim(ncid, "y", NY, y_dimid) ) ! 把上面得到的ID写到一个存放ID的数组里,注意,在fortran中,数组是以列为主存放数据的 dimids = (/ y_dimid, x_dimid /) ! 定义变量,返回一个对应的ID call check( nf90_def_var(ncid, "data", NF90_INT, dimids, varid) ) ! 定义完成,关闭定义模式 call check( nf90_enddef(ncid) ) ! 写入数据 call check( nf90_put_var(ncid, varid, data_out) ) ! 关闭文件 call check( nf90_close(ncid) ) !提示写文件成功 print *, "*** SUCCESS writing example file simple_xy.nc! " contains subroutine check(status) integer, intent ( in) :: status if(status /= nf90_noerr) then print *, trim(nf90_strerror(status)) stop 2 end if end subroutine check end program simple_xy_wr 在主目录下键入 gfortran cel-test.f90 -I/usr/local/netcdf/include -L/usr/local/netcdf/lib -lnetcdff -o test ( cel-test.f90为程序名, test为编译后生成的 ) 编译通过! 然后运行test,键入 ./test 结果无法运行,出现 Attempting netcdf-4 operation on netcdf-3 file STOP 2 再次郁闷! 打开.zshrc, 即打开所用shell的配置文件 gedit .zshrc 在文件后面加上 export NETCDF=/usr/local/ export DYLD_LIBRARY_PATH=$NETCDF/lib:$DYLD_LIBRARY_PATH export LD_LIBRARY_PATH=$NETCDF/lib:$LD_LIBRARY_PATH export PATH=$NETCDF/bin:$PATH 重新启动shell,然后再编译,运行test,得到 0 12 24 36 *** SUCCESS writing example file simple_xy.nc! 大功告成! 备注:以下为各个步骤执行命令的解释: $ ./configure The configuration script will set up the Makefiles that will be used to build the NetCDF libraries and utilities. It will also set up the installation directory for the default location of /usr/local. If you would like to install the libraries in another location, use this configure command: $ ./configure --prefix=/your/desired/install/directory) $ make----Make the libraries When the configuration step completes successfully, you can build the libraries using ‘make’. $ make check-----Testing NetCDF on Linux* You can test your NetCDF libraries using ‘make check’. $ make install------Installing NetCDF on Linux* Install NetCDF libraries using ‘make install’. This will install the NetCDF libraries, include files, and utilities in the default location of /usr/local or the location specified in the configuration step with the --prefix= option References: 1:http://bbs.sciencenet.cn/home.php?mod=spaceuid=237238do=blogid=547593 2: http://blog.sina.com.cn/s/blog_4b1d9e7b0100zg89.html 3:http://blog.sina.com.cn/s/articlelist_1260232315_3_1.html