netcdf.reDef仅在matlab中看到变化

时间:2018-01-29 02:47:50

标签: matlab netcdf

更改netcdf文件中变量的名称时,我只在Matlab中看到更改。

特别是,我通过nccreatencwrite创建了一个带有matlab的netcdf文件:

cd $PATH

ncfile = 'test.nc';

nccreate(ncfile,'Var1','Dimensions',{'time',20},'DeflateLevel',5);

ncwrite(ncfile,'Var1',rand(1,20),1);
ncwriteatt(ncfile,'Var1','Units','[m/s]');

通过在Matlab(ncdisp)和shell(ncdump)内进行检查,结果是变量名称相同:

 ncdisp('test.nc') 
Source:
       $PATH/test.nc Format:
       netcdf4_classic Dimensions:
       time = 20 Variables:
Var1
       Size:       20x1
       Dimensions: time
       Datatype:   double
       Attributes:
                   Units = '[m/s]'
$ ncdump -h test.nc
netcdf test { dimensions:     time = 20 ; variables:  double Var1(time) ;
    Var1:Units = "[m/s]" ; }

但是,当我使用Matlab更改变量的名称时:

cd $PATH

% Open netCDF file.
ncid = netcdf.open('test.nc','NC_WRITE');


% Put file in define mode.=
netcdf.reDef(ncid)


% Get name of first variable
[varname, xtype, varDimIDs, varAtts] = netcdf.inqVar(ncid,0);

varname


% Rename the variable, using a capital letter to start the name.
netcdf.renameVar(ncid,0,'velocity')


% Verify that the name of the variable changed.
[varname, xtype, varDimIDs, varAtts] = netcdf.inqVar(ncid,0);

varname
varname =

    'Var1'


varname =

    'velocity'

现在,只有Matlab才能看到这种变化:

ncdisp('test.nc')
Source:
       $PATH/test.nc Format:
       netcdf4_classic Dimensions:
       time = 20 Variables:
velocity
       Size:       20x1
       Dimensions: time
       Datatype:   double
       Attributes:
                   Units = '[m/s]'
$ ncdump -h test.nc 
netcdf test { dimensions:     time = 20 ; variables:  double Var1(time) ;
    Var1:Units = "[m/s]" ; }

实际上ncdump仍然会看到一个名为Var1的变量。

有谁知道这个的原因?

1 个答案:

答案 0 :(得分:1)

您需要致电netcdf.syncnetcdf.close

# ...
netcdf.renameVar(ncid,0,'velocity')
netcdf.sync(ncid)

# ...
netcdf.renameVar(ncid,0,'velocity')
netcdf.close(ncid)

netcdf.sync之后,您仍然可以使用netcdf标识ncid(对示例进行更多更改)。 netcdf.close不是这种情况。

没有必要关闭matlab以使这些更改对其他程序可见。

仅在使用NetCDF 3文件[1]时才需要函数调用netcdf.reDefnetcdf.endDef

[1] https://www.unidata.ucar.edu/software/netcdf/workshops/2007/nc4features/Enddef.html