netcdf读取NC数据,C语言+VS2017环境部署

netcdf读取NC数据,C语言+VS2017环境部署

简单说明

本来主要使用C#开发后台的,对C也只是简单会用,因C#调用各种第三方组件都多少存在一些无法解决的问题,尝试使用C来自己封装,所以编写的啰嗦一些,主要针对我这种小白,写的不对的地方,请大神告知。

前期准备

简单介绍环境软件介绍、下载地址等等。

环境、软件

使用环境、软件:

  1. Panoply:也是使用netcdf编写的,读取NC文件,用来查看数据,也可以使用其他的;
  2. vs:版本不要太古老,我用的是vs2017
  3. netcdf:我使用的是 4.6.1

下载链接:

  1. Panoplyhttps://www.giss.nasa.gov/tools/panoply/download/PanoplyWin-4.10.4.zip (官方地址)
  2. vshttps://visualstudio.microsoft.com/zh-hans/downloads/ (官方地址)
    http://msdn.itellyou.cn/ (资源比较全,不保证安全)
  3. netcdfhttps://www.unidata.ucar.edu/downloads/netcdf/ftp/netCDF4.6.1-NC3-32.exe (官方地址)
    https://www.unidata.ucar.edu/downloads/netcdf/index.jsp (其他选择)

环境搭建

环境配置

  1. 项目属性\VC++目录\包含目录 添加 C:\Program Files (x86)\netCDF 4.6.1\include (安装位置的include目录)
  2. 项目属性\VC++目录\库目录 添加 C:\Program Files (x86)\netCDF 4.6.1\lib (安装位置的lib目录)
  3. 项目属性\链接器\附加库目录 添加 C:\Program Files (x86)\netCDF 4.6.1\bin\netcdf.dll (安装位置的bin\netcdf.dll文件)
  4. 项目属性\链接器\输入\附加依赖项 添加 netcdf.lib

必须以Release方式编译
必须以Release方式编译
必须以Release方式编译

测试代码

test.c

#include <stido.h>
int ncid;
//获取字段数据
int getColData(char *colname, float *dataArray)
{
	int varid;
	nc_inq_varid(ncid, colname, &varid);
	nc_get_var_float(ncid, varid, dataArray);
	return 0;
}
//获取字段内容数量
int getColLen(char *colname)
{
	int dimid;
	size_t size;
	//获取字段维度
	nc_inq_dimid(ncid, colname, &dimid);
	nc_inq_dimlen(ncid, dimid, &size);
	return (int)size;
}
//获取nc数据一维数据
void getNcData(char *ncFilePath,char *Name)
{
	int varid,i,lon_length;
	float *lon_data = NULL;
	//打开nc文件
	nc_open(ncFilePath,0,&ncid);
	lon_length = getColLen(Name);
	//声明存储数据空间
	lon_data = (float *)malloc(lon_length * sizeof(float));
	if (NULL == lon_data)
	{
		return NULL;
	}
	getColData(Name, lon_data);
	for(i = 0; i < lon_length; i++)
	{
		printf("%.4f\t",lon_data[i]);
	}
	
}
int main()
{
	getNcData("d:\******.nc","longitude");
	system("pause");
	return 0;
}
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值