NetCDF 格式小试一

2015-11-15  本文已影响623人  80s老人

最近需要跨平台使用同一组原始数据,研究了下Matlab本身的.mat格式,HDF5 还有就是NetCDF4。
其实NetCDF4就是一个多自变量的单值函数。用公式来说就是f(x,y,z,...)=value, x,y,z等在netCDF中叫做维(dimension),函数值value在NetCDF中叫做变量(Variables). 而计量单位(量纲)叫属性(Attributes)。以下为我的Matlab范例。

%使用matlab原生支持命令创建nc文件范例
%2015-11-15

%% 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('week01.nc','NC_NOCLOBBER')

%% 2定义Dimension
% dimid = netcdf.defDim(ncid,dimname,dimlen)
timedimID = netcdf.defDim(ncid,'time',292366);


%% 3.定义变量:
% varid = netcdf.defVar(ncid,varname,xtype,dimids)
I_p1_kal_ID = netcdf.defVar(ncid,'I_p1_kal','double',[timedimID]);
I_p2_kal_ID = netcdf.defVar(ncid,'I_p2_kal','double',[timedimID]);
P_p1_kal_ID = netcdf.defVar(ncid,'P_p1_kal','double',[timedimID]);
P_p2_kal_ID = netcdf.defVar(ncid,'P_p2_kal','double',[timedimID]);
V_p1_kal_ID = netcdf.defVar(ncid,'V_p1_kal','double',[timedimID]);
V_p2_kal_ID = netcdf.defVar(ncid,'V_p2_kal','double',[timedimID]);
pin_kal_ID =  netcdf.defVar(ncid,'pin_kal','double',  [timedimID]);
pout_kal_ID = netcdf.defVar(ncid,'pout_kal','double',[timedimID]);
q_kal_ID    = netcdf.defVar(ncid,'q_kal','double',   [timedimID]);
time_kal_ID = netcdf.defVar(ncid,'time_kal','double',[timedimID]);
w_p1_kal_ID = netcdf.defVar(ncid,'w_p1_kal','double',[timedimID]);
w_p2_kal_ID = netcdf.defVar(ncid,'w_p2_kal','double',[timedimID]);

%% 4 完成netCDF文件定义模式
netcdf.endDef(ncid)


%% 5 把数据写到netcdf的文件中
% netcdf.putVar(ncid,varid,data)
% netcdf.putVar(ncid,varid,start,data)
% netcdf.putVar(ncid,varid,start,count,data)
% netcdf.putVar(ncid,varid,start,count,stride,data)
netcdf.putVar(ncid,I_p1_kal_ID,I_p1_kal);
netcdf.putVar(ncid,I_p2_kal_ID,I_p2_kal);
netcdf.putVar(ncid,P_p1_kal_ID,P_p1_kal);
netcdf.putVar(ncid,P_p2_kal_ID,P_p2_kal);
netcdf.putVar(ncid,V_p1_kal_ID,V_p1_kal);
netcdf.putVar(ncid,V_p2_kal_ID,V_p2_kal);
netcdf.putVar(ncid,pin_kal_ID, pin_kal);
netcdf.putVar(ncid,pout_kal_ID,pout_kal);
netcdf.putVar(ncid,q_kal_ID,   q_kal);
netcdf.putVar(ncid,time_kal_ID,time_kal);
netcdf.putVar(ncid,w_p1_kal_ID,w_p1_kal);
netcdf.putVar(ncid,w_p2_kal_ID,w_p2_kal);


%% 6 关闭文件
netcdf.close(ncid);
上一篇下一篇

猜你喜欢

热点阅读