matlab将变量输出为nc文件,以对应经纬度的光照强度数据为例

%% 1创建nc文件
% cid = netcdf.create(filename, mode)
% mode:
% 'NC_NOCLOBBER':                Prevent overwriting of existing file with the same name.
% 'NC_SHARE':                        Allow synchronous file updates.
% 'NC_64BIT_OFFSET':        Allow easier creation of files and variables which are larger than two gigabytes.
ncid = netcdf.create('example.nc','NC_NOCLOBBER');
% 定义经度和纬度维度
lon_dim_id = netcdf.defDim(ncid,'lon',4320);
lat_dim_id = netcdf.defDim(ncid,'lat',2160);
%定义经度和纬度变量
lon_var_id = netcdf.defVar(ncid, 'lon', 'double', lon_dim_id);
lat_var_id = netcdf.defVar(ncid, 'lat', 'double', lat_dim_id);
% 定义经度和纬度变量属性
netcdf.putAtt(ncid, lon_var_id,'standard_name','longitude');
netcdf.putAtt(ncid, lat_var_id,'standard_name','latitude');
netcdf.putAtt(ncid, lon_var_id,'units','degrees_east');
netcdf.putAtt(ncid, lat_var_id,'units','degrees_north');
% 定义数据变量及其属性
var_id = netcdf.defVar(ncid, 'PAR','double', [lon_dim_id,lat_dim_id]);
netcdf.putAtt(ncid, var_id, 'long_name','2022-PAR');
netcdf.putAtt(ncid, var_id, 'units','uphotons m^-2 day^-1');
% 结束 NetCDF 文件定义
netcdf.endDef(ncid);
%生成经度和纬度数据
lon = linspace(-179.958328247070,179.958343505859,4320)';
lat = linspace(-89.9583358764648,89.9583358764648,2160)';
%将经度和纬度数据写入 NetCDF 文件
netcdf.putVar(ncid, lon_var_id,lon);
netcdf.putVar(ncid, lat_var_id,lat);
%生成数据并将其写入 NetCDF 文件rand([3601897)data
netcdf.putVar(ncid, var_id,PAR22);
%关闭 NetCDF 文件netcdf.close(ncid);
netcdf.close(ncid);

参考了文库博客以及论坛,我按照我的需求修改了一下,确实可行。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值