您的位置:首页 > 编程语言 > C语言/C++

Ubuntu Read and Write NetCDF file

2016-12-28 17:14 447 查看

我将之前的在Window下VS成功读取NETCDF的程序移植到Ubuntu下

Linux 配置NetCDF环境

(1)官方下载源程序包:http://www.unidata.ucar.edu/downloads/netcdf/index.jsp,我下载版本为netcdf-4.4.1.1

(2)打算装在/usr/local/netcdf目录下,$ mkdir /usr/local/netcdf

(3) cd /home/zyx/mytar/netcdf-4.4.1.1

./configure –prefix=/usr/local/netcdf

我运行到这遇到问题:Can not find hdf5 library.我通过查看config.log,缺少安装库文件libhdf5,我通过安装

sudo apt-get install libhdf5-dev 安装后执行以下命令:

make

make check

make install

最后安装成功后netcdf目录下:



经过了一段时间的折磨,终于我的程序可以在Linux环境下运行了,实现的过程真是一把鼻涕一把泪,不过我没有放弃,最终环境配好了。第一步虽然成功安装了NetCDF,但是netcdf的依赖库没有装,所以程序一直跑不起来,下面我简单的说一下安装步骤和注意事项:

(1)首先,我在网上找到netcdf安装 、zlib 、szip、hdf5、mpich2、curl系列软件安装,网址http://blog.sina.com.cn/s/blog_626185090101e1n5.html

下载源代码:hdf5-1.8.11.tar.gz

NetCDF-4 C++

netcdf-4.4.1.1.tar.gz

其他的自行下载即可。

(2)根据上面步骤安装,但HDF5安装不上,然后我查了查,注意zlib和hdf5要安装在一个目录下,可以查看netcdf-4.4.1.1.tar.gz包下的INSTALL文件,安装命令如下:

./configure –with-zlib=/usr/local/zlib –prefix=/usr/local/zlib

make

make check

sudo make install

注意:我一开始安装了1.8.17,出现如下错误

Warning! ***HDF5 library version mismatched error***
The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.8.11, library is 1.8.17
SUMMARY OF THE HDF5 CONFIGURATION
=================================

General Information:
-------------------
HDF5 Version: 1.8.17
Configured on: Wed Jan 11 10:30:07 CST 2017
Configured by: zyx@ubuntu
Configure mode: production
Host system: x86_64-unknown-linux-gnu
Uname information: Linux ubuntu 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
Byte sex: little-endian
Libraries: static, shared
Installation point: /usr/local/hdf5

Compiling Options:
------------------
Compilation Mode: production
C Compiler: /usr/bin/gcc
CFLAGS:
H5_CFLAGS: -std=c99 -pedantic -Wall -Wextra -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wfloat-equal -Wmissing-format-attribute -Wmissing-noreturn -Wpacked -Wdisabled-optimization -Wformat=2 -Wunreachable-code -Wendif-labels -Wdeclaration-after-statement -Wold-style-definition -Winvalid-pch -Wvariadic-macros -Wnonnull -Winit-self -Wmissing-in


Headers are 1.8.11, library is 1.8.17,头文件是1.8.11,库是1.8.17,我重新安装了1.8.11,然后重启Linxu,这次问题解决了。

(3)接下来安装netcdf,我是安装在/usr/local/netcdf,照着上面那个按的
mkdir /usr/local/netcdf
./configure --prefix=/usr/local/netcdf --libdir=/usr/lib/ --includedir=/usr/lib/ --sharedstatedir=/usr/share --bindir=/usr/bin/
make
make check
sudo make check
(4)安装C++接口库NetCDF-4 C++
cd /home/zyx/Downloads/netcdf-cxx4-4.3.0
./configure --prefix=/usr/local/cxx4
make
make check
sudo make install
(5)我运行环境是在linux集成开发环境Eclipse,程序代码:


#include <iostream>
#include <netcdf>
#include <vector>

using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;

// Return this in event of a problem.
static const int NSSS3 = 142989;
static const int NC_ERR = 2;

int main()
{
try
{
NcFile dataFile("SM_REPR_MIR_OSUDP2_20150702T142644_20150702T152005_622_001_1.nc", NcFile::read);

NcVar SSS3 = dataFile.getVar("SSS3");

float *sss3 = new float[NSSS3];
if (SSS3.isNull()) return NC2_ERR;
SSS3.getVar(sss3);

for (int i = 0; i < NSSS3; i++)
{
cout << "SSS3:"<< sss3[i] << endl;
}

delete sss3;//释放掉内存
return 0;
}
catch(NcException& e)
{
e.what();
return NC_ERR;
}
}


编译没问题,但连接时候出现如下错误:

error while loading shared libraries: libnetcdf_c++4.so.1: cannot open shared object file: No such file or directory


解决方案:

把库路径/usr/local/lib 加到 /etc/ld.so.conf ,然后运行下ldconfig,命令如下:

sudo vim /etc/ld.so.conf

sudo ldconfig

OK!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu Netcdf-C++