您的位置:首页 > 编程语言 > MATLAB

制作nc文件(Matlab)

2016-02-27 15:41 561 查看
首先看一个nc文件中包含哪些部分,例如一个标准的
FVCOM
输入文件
wind.nc


netcdf wind {
dimensions:
nele = 36858 ;
node = 18718 ;
time = UNLIMITED ; // (151 currently)
variables:
int iint(time) ;
iint:long_name = "internal mode iteration number" ;
float time(time) ;
time:long_name = "time" ;
time:units = "days since 0.0" ;
time:time_zone = "none" ;
float uwind_speed(time, nele) ;
uwind_speed:long_name = "Eastward Wind Speed" ;
uwind_speed:standard_name = "Wind Speed" ;
uwind_speed:units = "m/s" ;
uwind_speed:grid = "fvcom_grid" ;
uwind_speed:type = "data" ;
float vwind_speed(time, nele) ;
vwind_speed:long_name = "Northward Wind Speed" ;
vwind_speed:standard_name = "Wind Speed" ;
vwind_speed:units = "m/s" ;
vwind_speed:grid = "fvcom_grid" ;
vwind_speed:type = "data" ;

// global attributes:
:title = "wind.nc" ;
:institution = "School for Marine Science and Technology" ;
:source = "fvcom grid (unstructured) surface forcing" ;
:history = "FILE CREATED: Sun Jul 31 13:48:52 2011" ;
:references = "http://fvcom.smast.umassd.edu,http://codfish.smast.umassd.edu" ;
:Conventions = "CF-1.0" ;
:CoordinateSystem = "Cartesian" ;
:CoordinateProjection = "none" ;
}

nc
文件主要内容

可以看出
nc
文件包含两个主要部分:

dimensions
:各个变量维度大小

variables
:变量

dimensions

在上面文件中包含三个维度:
nele
node
time
。其中
nele
node
两个是固定长度的,而
time
则是
UNLIMITED
,代表其为任意长度。

variables

variables
下,每个变量形式为

float uwind_speed(time, nele)

括号包含了变量的维度,每个维度变量指定了该变量某个维度的长度。

netcdf
定义中数组是按行排列,因此循环是由右至左。而在
matlab
中,数组则是按列排列,因此储存的数组会将各个维度顺序交换,即
uwind_speed(nele, time)


生成
nc
文件方法

使用 matlab 的
netcdf
工具箱生成文件时需要按照以下顺序:

定义文件内维度与变量,包括

定义维度

定义变量

储存变量

以生成一个如上的
wind.nc
文件为例

定义维度

首先定义文件中维度:
nele
node
time


注意
time
长度是不固定的。

%% creat new netcdf file

ncid = netcdf.create('wind.nc','CLOBBER');

% definition
ele_dim = netcdf.defDim(ncid,'nele', eleNum);
node_dim = netcdf.defDim(ncid,'node', nodeNum);
time_dim = netcdf.defDim(ncid,'time', netcdf.getConstant('NC_UNLIMITED'));

定义维度之后便可根据维度定义变量。

注意,由于
time
维度是不定长度的,因此其必须作为变量最后一个维度(元素循环由左向右)

iint_var_id = netcdf.defVar(ncid,'init','int', time_dim);
time_var_id = netcdf.defVar(ncid,'time','float', time_dim);
u_var_id = netcdf.defVar(ncid,'uwind_speed','double',[ele_dim, time_dim]);
v_var_id = netcdf.defVar(ncid,'vwind_speed','double',[ele_dim, time_dim]);

% end definition
netcdf.endDef(ncid);
netcdf.close(ncid);

定义完变量之后便可向nc文件中储存数据了。

在这里需要注意的是,由于matlab 调用
c
语言的 NetCDF函数库,因此在其函数中数组下标是由0开始。(详见
help netcdf


% write data
ncid = netcdf.open('wind.nc', 'WRITE');
netcdf.putVar(ncid, time_var_id, 0, nstep, 1, time(1:nstep));

如上述代码中,
0
为起始序号,
nstep
为数据总数,
1
为储存数据间隔,
time
即使储存在matlab中变量名。

采用相同方法将
uwind_speed
vwind_speed
循环储存在文件内。

for itime = 1:nstep
...

netcdf.putVar(ncid, u_var_id, [0, itime-1], [eleNum, 1], u_interp);

...
netcdf.putVar(ncid, v_var_id, [0, itime-1], [eleNum, 1], v_interp);

fprintf('Processing: %f \n', itime/nstep);
end% for

在这里,
uwind_speed
vwind_speed
是多维数组,因此指定其起始位置也要采用一个向量
[0, itime-1]
0
代表
nele
维度起始序号;'itime-1' 则代表
time
维度起始序号)。
[eleNum, 1]
为这次要储存的数据占个维度个数,明显我们要储存一整个时间步的数据,所以长度分别为单元个数与时间步数
1
。最后
u_interp
v_interp
则是变量名。

结语

最终,我们来检查下生成的nc文件,使用
matlab
版本为 R2014b,
NetCDF
版本号为4.1.3。

Source:
/Users/mac/Documents/MATLAB/temp/forZhangNa/FVCOM_wind/wind.nc
Format:
classic
Dimensions:
nele = 21284
node = 10951
time = 3     (UNLIMITED)
Variables:
init
Size:       3x1
Dimensions: time
Datatype:   int32
time
Size:       3x1
Dimensions: time
Datatype:   single
uwind_speed
Size:       21284x3
Dimensions: nele,time
Datatype:   double
vwind_speed
Size:       21284x3
Dimensions: nele,time
Datatype:   double
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: