加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

netcdf网格文件读取和写入之matlab程序

(2014-07-28 17:21:54)
标签:

matlab

netcdf

网格文件

分类: matlab

 

1,查看netcdf文件基本信息,可以看到各个变量的名称

>> ncdisp('freeair.nc')

Source:

           E:\FedoraShare\work\MohoInversion\matlabCode\bouguerCorrection\data\freeair.nc

Format:

           classic

Global Attributes:

           Conventions = 'COARDS/CF-1.0'

           title       = 'freeair.nc'

           history     = 'grdproject -Jm1 -R112/122/24.9849663108/35.0097358103 -I freeair.nc -Gfreeair.nc'

           GMT_version = '4.5.9 [64-bit]'

           node_offset = 1

Dimensions:

           lon = 600

           lat = 696

Variables:

    lon

           Size:       600x1

           Dimensions: lon

           Datatype:   double

           Attributes:

                       long_name    = 'longitude'

                       units        = 'degrees_east'

                       actual_range = [1.12e+02 1.22e+02]

    lat

           Size:       696x1

           Dimensions: lat

           Datatype:   double

           Attributes:

                       long_name    = 'latitude'

                       units        = 'degrees_north'

                       actual_range = [2.50e+01 3.50e+01]

    z 

           Size:       600x696

           Dimensions: lon,lat

           Datatype:   single

           Attributes:

                       long_name    = 'z'

                       _FillValue   = NaN

                       actual_range = [-6.64e+01  1.16e+02]

2,读取文件

%打开文件

ncid=netcdf.open('freeair.nc','NC_NOWRITE');

%返回变量id

latid=netcdf.inqVarID(ncid,'lat') ;

lonid=netcdf.inqVarID(ncid,'lon') ;

zid=netcdf.inqVarID(ncid,'z');

%读取变量

lat=netcdf.getVar(ncid,latid);

lon=netcdf.getVar(ncid,lonid);

z=netcdf.getVar(ncid,zid);

 

 

3,写入netcdf文件

%使用matlab原生支持命令创建nc文件范例
12-05-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('test.nc','NC_NOCLOBBER')

%% 2
定义Dimension

% dimid = netcdf.defDim(ncid,dimname,dimlen)

dimidx = netcdf.defDim(ncid,'lon',10);
dimidy = netcdf.defDim(ncid,'lat',10);

%% 3.
定义变量:

% varid = netcdf.defVar(ncid,varname,xtype,dimids)
varid = netcdf.defVar(ncid,'test','double',[dimidx dimidy]);

%% 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,varid,rand(10))


%% 6
关闭文件

netcdf.close(ncid);

 

参考网址:

http://www.ilovematlab.cn/thread-217430-1-1.html

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有