轉發:https://www.cnblogs.com/wang985850293/p/6576533.html
https://blog.csdn.net/toby54king/article/details/78711563
https://blog.csdn.net/qq_31793023/article/details/103350834
**************************************************************************************************
使用的netcdf的c++接口版本是netcdf-cxx4-4.2.tar.gz,下載地址:
http://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp
**************************************************************************************************
**************************************************************************************************
netcdf的C++接口是基於netcdf的C語言接口,所以在使用c++接口之前,我們需要安裝netcdf的c語言版本。
使用的netcdf的C語言版本是NetCDF-4.2.1,下載地址:http://www.unidata.ucar.edu/software/netcdf/docs/winbin.html
這個版本是二進制版本,直接點擊安裝即可,然后會得到include,lib和bin等幾個文件。
**************************************************************************************************
使用方法:
1.首先解壓這個壓縮文件;
2.將解壓后的文件中的cxx4文件夾中的netcdf文件改為netcdfcpp.h;
3.我們需要用的是cxx4文件夾中的所有的.h和.cpp文件;
**************************************************************************************************
注意:不使用hdf5的話,ncFile.cpp文件需要做改動:去掉紅色前面的NC_NETCDF4
case NcFile::newFile:
ncCheck(nc_create(filePath.c_str(), NC_NOCLOBBER, &myId),__FILE__,__LINE__);
break;
case NcFile::replace:
ncCheck(nc_create(filePath.c_str(), NC_CLOBBER, &myId),__FILE__,__LINE__);
break;
**************************************************************************************************
4.將所有的.h和.cpp文件添加到你的工程目錄下即可,分別添加到工程頭文件和源文件中,在VS項目屬性——VC++目錄——包含目錄,添加.h頭文件所在的位置即可。
5.將netcdf的c語言的頭文件netcdf.h,動態鏈接庫netcdf.dll和庫文件netcdf.lib添加到VS2010中。
如何添加動態鏈接庫:
a.將netcdf的c語言版本的include,lib和bin文件添加到工程目錄下;
b.VS中項目屬性——鏈接器——常規——附加庫目錄,這里添加你的bin文件中的netcdf.dll路徑;
c.VS中項目屬性——鏈接器——輸入——附加依賴項,這里添加netcdf.lib庫文件;
d.VS中項目屬性——VC++目錄——包含目錄,添加netcdf.h頭文件的位置;
e.VS中項目屬性——VC++目錄——庫目錄,添加netcdf.lib庫文件的位置。
測試成功與否:注意添加的頭文件netcdfcpp.h
1.首先創建***.nc文件:
#include <iostream>
#include "netcdfcpp.h"
#include <vector>
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main()
{
int dataOut[NX][NY];
for(int i = 0; i < NX; i++)
for(int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
try
{
NcFile dataFile("simple_xy.nc", NcFile::replace);
NcDim xDim = dataFile.addDim("x", NX);
NcDim yDim = dataFile.addDim("y", NY);
vector<NcDim> dims;
dims.push_back(xDim);
dims.push_back(yDim);
NcVar data = dataFile.addVar("data", ncInt, dims);
data.putVar(dataOut);
return 0;
}
catch(NcException& e)
{e.what();
return NC_ERR;
}
}
2.讀取剛創建的***.nc文件:
#include
#include "netcdfcpp.h"
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main()
{
try
{
int dataIn[NX][NY];
NcFile dataFile("simple_xy.nc", NcFile::read);
NcVar data=dataFile.getVar("data");
if(data.isNull()) return NC_ERR;
data.getVar(dataIn);
for (int i = 0; i < NX; i++)
for (int j = 0; j < NY; j++)
if (dataIn[i][j] != i * NY + j)
return NC_ERR;
return 0;
}catch(NcException& e)
{
e.what();
cout<<"FAILURE*************************************"<<endl;
return NC_ERR;
}
}