c++实现netcdf数据的存储与

转自:http://blog.sina.com.cn/s/blog_687960370101hiks.html

1.首先下载netcdf的c++接口版本,下载地址:https://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp

2.下载netcdf的C语言版本,下载地址:https://www.unidata.ucar.edu/software/netcdf/docs/winbin.html(注意32位和64位,该版本为exe版,直接安装会生成include,lib和bin几个文件)


使用方法:

1.解压netcdf-cxx的压缩包,会得到cxx、example、m4、man4及若干配置文件;

2.将配置文件中的config.h.in改为config.h;

3.我们一共需要用到config.h、ncvalues.h、netcdfcpp.h、ncvalues.cpp和netcdf.cpp这五个文件;

4.把这五个文件放入你的工程目录下,添加到工程头文件和源文件中,在VS项目属性—VC++目录—包含目录,添加config.h,netcdfcpp.h,ncvalues.h和netcdf.h头文件所在的位置;

5.将netcdf的c语言的头文件netcdf.h,动态链接库netcdf.dll和库文件netcdf.lib添加到VS中,VS中项目属性——链接器——常规——附加库目录,这里添加你的bin文件中的netcdf.dll路径,这里我虽然添加了但运行时报错缺少netcdf.dll文件,可以把它放在工程exe对应文件夹下,VS中项目属性——链接器——输入——附加依赖项,这里添加netcdf.lib库文件,VS中项目属性——VC++目录——包含目录,添加netcdf.h头文件的位置,VS中项目属性——VC++目录——库目录,添加netcdf.lib库文件的位置。

这样便完成了c++下netcdf库的配置。

可以用一下代码进行测试

这是写入数据,如果运行成功,在工程目录里会生成对应nc数据

#include <iostream>
#include <netcdfcpp.h>
using namespace std;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main(void)
{
   int dataOut[NX][NY];
   for(int i = 0; i < NX; i++) 
      for(int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
   NcFile dataFile("simple_xy.nc", NcFile::Replace);
   if (!dataFile.is_valid())
   {
      cout << "Couldn't open file!\n";
      return NC_ERR;
   }
   NcDim* xDim = dataFile.add_dim("x", NX);
   NcDim* yDim = dataFile.add_dim("y", NY);
   NcVar *data = dataFile.add_var("data", ncInt, xDim, yDim);
   data->put(&dataOut[0][0], NX, NY);
   cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
   return 0;
}

读取nc数据,制台窗口会显示0,1,2,3,4,....71数据,则成功

#include <iostream>
#include <netcdfcpp.h>
using namespace std;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main(void)
{
   int dataIn[NX][NY]; 
   NcFile dataFile("simple_xy.nc", NcFile::ReadOnly);
   if (!dataFile.is_valid())
   {
      cout << "Couldn't open file!\n";
      return NC_ERR;
   }
   NcVar *data = dataFile.get_var("data");
   data->get(&dataIn[0][0], NX, NY);
   for (int i = 0; i < NX; i++)
      for (int j = 0; j < NY; j++)
 {
 if (dataIn[i][j] != i * NY + j)
 return NC_ERR;
 cout<<dataIn[i][j]<<endl;
 }
   cout << "*** SUCCESS reading example file simple_xy.nc!" << endl;
   system("pause");
   return 0;
}



  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值