Centos 7和Ubuntu安装 netcdf

netcdf4可以从源码编译,或者用yum直接安装预编译版本

1 从源码安装

1.1 安装netcdf

(1)安装依赖

安装zlib

$ # Build and install zlib
$ ZDIR=/usr/local
$ ./configure --prefix=${ZDIR}
$ make check
$ make install   # or sudo make install, if root permissions required

安装hdf5

$ # Build and install HDF5
$ H5DIR=/usr/local
$ ./configure --with-zlib=${ZDIR} --prefix=${H5DIR} --enable-hl
$ make check
$ make install   # or sudo make install, if root permissions required

安装完后,打开.bashrc

$ gedit ~/.bashrc

 在./bashrc文件中加入下面这句话,从而将${H5DIR}/lib加入环境变量LD_LIBRARY_PATH

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

安装netcdf-c

从github上(https://github.com/Unidata/netcdf-c)或者netcdf官网(https://www.unidata.ucar.edu/software/netcdf/)下载源码"netcdf-c-4.7.4.tar.gz"

安装方法:

$ # Build and install netCDF-4
$ ZDIR=/usr/local
$ H5DIR=/usr/local
$ NCDIR=/usr/local
$ CPPFLAGS='-I${H5DIR}/include -I${ZDIR}/include' LDFLAGS='-L${H5DIR}/lib -L${ZDIR}/lib' ./configure --prefix=${NCDIR}
$ make check
$ make install  # or sudo make install

1.2 安装netcdf C++

下载源码: https://github.com/Unidata/netcdf-cxx4

解压后进入文件夹,在终端依次输入

mkdir build
cd build
../configure
make
make check
make install

 

 

2 从linux软件仓库在线安装

2.1 Centos 7

Centos 7的仓库比较老,建议从源码安装

sudo yum install netcdf-devel
sudo yum install netcdf-cxx-devel

2.2 For ubuntu 19.10

sudo apt-get install libnetcdf-dev
sudo apt-get install libnetcdf-c++4-dev

4 测试

测试一个源码文件夹里面的examples子文件夹的例子,该文件为simple_xy_wr.cpp

/* This is part of the netCDF package.
   Copyright 2006 University Corporation for Atmospheric Research/Unidata.
   See COPYRIGHT file for conditions of use.

   This is a very simple example which writes a 2D array of
   sample data. To handle this in netCDF we create two shared
   dimensions, "x" and "y", and a netCDF variable, called "data".

   This example is part of the netCDF tutorial:
   http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial

   Full documentation of the netCDF C++ API can be found at:
   http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx

   $Id: simple_xy_wr.cpp,v 1.5 2010/02/11 22:36:43 russ Exp $
*/

#include <iostream>
#include <netcdf>
#include <vector>
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;

// We are writing 2D data, a 6 x 12 grid. 
static const int NX = 6;
static const int NY = 12;

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



int main()
{
  // This is the data array we will write. It will just be filled
  // with a progression of numbers for this example.
  int dataOut[NX][NY];
  
  // Create some pretend data. If this wasn't an example program, we
  // would have some real data to write, for example, model output.
  for(int i = 0; i < NX; i++)
    for(int j = 0; j < NY; j++)
      dataOut[i][j] = i * NY + j;
  
  // The default behavior of the C++ API is to throw an exception i
  // an error occurs. A try catch block is necessary.
   
  try
    {  
      // Create the file. The Replace parameter tells netCDF to overwrite
      // this file, if it already exists.
      NcFile dataFile("simple_xy.nc", NcFile::replace);
      
      // Create netCDF dimensions
      NcDim xDim = dataFile.addDim("x", NX);
      NcDim yDim = dataFile.addDim("y", NY);
      
      // Define the variable. The type of the variable in this case is
      // ncInt (32-bit integer).
      vector<NcDim> dims;
      dims.push_back(xDim);
      dims.push_back(yDim);
      NcVar data = dataFile.addVar("data", ncInt, dims);
   
      // Write the data to the file. Although netCDF supports
      // reading and writing subsets of data, in this case we write all
      // the data in one operation.
      data.putVar(dataOut);
      
      // The file will be automatically close when the NcFile object goes
      // out of scope. This frees up any internal netCDF resources
      // associated with the file, and flushes any buffers.
      
      //cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
      return 0; 
    }
  catch(NcException& e)
    {e.what();
      return NC_ERR;
    }
}

编译选项一定要加上-lnetcdf,注意第一个不是1,也不是IJK的I,而是lmn的l

如果是C++,需要加上-lnetcdf_c++4

 

使用g++编译:

g++ simple_xy_wr.cpp -lnetcdf_c++4 -lnetcdf

NB 环境变量

如果是从源码编译的,netcdf-cxx的库一般是默认安装在/usr/local/lib里面,使用netcdf的C++库的程序运行时可能会出现如下的错误

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

这是因为没有找到链接库,需要添加环境变量LD_LIBARARY_PATH。在~/.bashrc文件的最后加

LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH

6 参看netcdf文件的工具

netcdf文件是二进制文件,不能直接用gedit或vim等文本编辑器查看,Panoly是一个界面比较友好的可以打开netcdf文件的软件,下载地址为

https://www.giss.nasa.gov/tools/panoply/

ncdump可以直接查看文件信息

7 文档和学习资料

https://www.unidata.ucar.edu/software/netcdf/docs/index.html

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值