安装过程如下:
(1)从http://www.unidata.ucar.edu/downloads/netcdf/index.jsp官方网站下载源程序包。最新版为4.0.1,安装的是3.6.3,测试成功。
(2)假设源程序包保存在/home/fengwei/netcdf-3.6.3文件夹下,打算安装在/usr/local/netcdf路径里。进入root后, *** 作如下:
mkdir /usr/local/netcdf
cd /home/fengwei/netcdf-3.6.3
./configure --prefix=/usr/local/netcdf
make check
make
make install
(3)安厅猜败装完成后,/usr/local/netcdf/文件夹下存在4个文件,分别为bin,include,lib和share。
(4)针对感兴趣的某一nc文件,编写相应的fortran代码(如test.f90),其中应包括include
'netcdf.inc',并调用NetCDF给定的函数对nc文件进行读取和写入等 *** 作。
(5)编译fortran代码,以intel fortran编译器兆租为例,其他的编译器基本一致
ifort -c -I/usr/local/netcdf/include test.f90
扮颤 ifort -o test test.o -L/usr/local/netcdf/lib -lnetcdf
(6)运行
./test
#include <iostream>#include <netcdfcpp.h>
using namespace std
// We are reading 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(void)
{
// This is the array we will read.
int dataIn[NX][NY]
// Open the file. The ReadOnly parameter tells netCDF we want
// read-only access to the file.
NcFile dataFile("simple_xy.nc", NcFile::ReadOnly)
// You should always check whether a netCDF file open or creation
// constructor succeeded.
if (!dataFile.is_valid())
{
cout << "Couldn’t open file!\n"
return NC_ERR
}
// For other method calls, the default behavior of the C++ API is
// to exit with a message if there is an error. If that behavior
// is OK, there is no need to check 友圆return values in simple cases
// like the following.
// Retrieve the variable named "data"
Chapter 2: Example Programs 25
NcVar *data = dataFile.get_var("data")
// Read all the values from the "data" variable into memory.
data->get(&dataIn[0][0], NX, NY)
// Check the values.
for (int i = 0 i < 好慧塌NX i++)
for (int j = 0 j < NY j++)
if (dataIn[i][j] != i * NY + j)
return NC_ERR
// The netCDF file is automatically closed by the NcFile destructor
cout << "*** SUCCESS reading 碧正example file simple_xy.nc!" << endl
return 0
}
clear allfgrd='/data/analysis_ms/2008080100_da.nc' %文件路径
nc=netcdf.open(fgrd, 'NC_NOWRITE')%打开文件
start=[0,0,0]% 起点位置 [0,0,0]
count=[145,96,30]% 向后计数
vid=netcdf.inqVarID(nc,'EC'洞纤)% 获取变量名的ID
ec=netcdf.getVar (nc,vid,start,count)%读取变纳蔽仿量
netcdf.close(nc)% 关并枝闭文件
ec0=ec(:,:,1)%只取其中第一层
contourf(ec0') % 画等值线
说明:
这个数据在原来的Fortran中维数定义为(0:144,0:95,1:30),也就是说,其维数是145*96*30,分别为纬向、经向、垂直格点数
由于在matlab中认为nc文件总是从0开始的,所以从[0,0,0]作为起点,要读完所有的点,则要向后计数[145,96,30]个
如果读第一个点,则start=[0,0,0] count=[1,1,1]
如果读最后一个点则start=[144,96,29] count=[1,1,1]
如果读第一层的,则 start=[0,0,0] count=[145,96,1]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)