爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 16433|回复: 20

[参考资料] 转]matlab中读nc,grd的函数

[复制链接]

新浪微博达人勋

发表于 2012-2-25 16:48:47 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x


在matlab中读netCDF格式文件的一些函数
A.文件IO操作函数:
1.打开netCDF数据文件
ncid = netcdf.open(filename, mode)   mode:'NC_WRITE','NC_SHARE','NC_NOWRITE'

2.还原最近的netCDF文件定义
netcdf.abort(ncid)

3.关闭netCDF文件
netcdf.close(ncid)

4.创建新netCDF数据文件
ncid = 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.

5.完成netCDF文件定义模式
netcdf.endDef(ncid)
netcdf.endDef(ncid,h_minfree,v_align,v_minfree,r_align)

6.返回已知netCDF库常数的列表
val = netcdf.getConstantNames
如:
nc_constants = netcdf.getConstantNames
nc_constants =
    'NC2_ERR'
    'NC_64BIT_OFFSET'
    'NC_BYTE'
    'NC_CHAR'
    'NC_CLOBBER'
    'NC_DOUBLE'
    'NC_EBADDIM'
    'NC_EBADID'
    'NC_EBADNAME'
    'NC_EBADTYPE'
  ...

7.返回数值的命名常数
val = netcdf.getConstant(param_name)
param_name为netcdf.getConstantNames中的string类型的值。
如:
>> netcdf.getConstant('NC_MAX_VARS')
ans =
        8192
>> netcdf.getConstant('NC_SHORT')
ans =
     3

8.返回有关netCDF文件的信息
[ndims,nvars,ngatts,unlimdimid] = netcdf.inq(ncid)

9.返回netCDF的库版本信息
libvers = netcdf.inqLibVers

10.把打开netCDF文件换成定义模式
netcdf.reDef(ncid)
注:打开的文件可以通过此函数进入重新定义状态

11.更改默认netCDF的文件格式
oldFormat = netcdf.setDefaultFormat(newFormat)
newFormat可以取:
'NC_FORMAT_CLASSIC':Original netCDF file format'NC_FORMAT_64BIT'
64-bit offset format; relaxes limitations on creating very large files

12.填充模式设置netCDF
old_mode = netcdf.setFill(ncid,new_mode)

13.Synchronize netCDF file to disk 同步netCDF文件
netcdf.sync(ncid)
*******************************************************************************************
B.操作:
1.Create Dimensions函数:
dimid = netcdf.defDim(ncid,dimname,dimlen)

2.用ID返回Dimensions的名和长度的函数:
[dimname, dimlen] = netcdf.inqDim(ncid,dimid)

3.用名字返回Dimensions的ID号:
dimid = netcdf.inqDimID(ncid,dimname)

4.重命名Dimensions:
netcdf.renameDim(ncid,dimid,newName)

*******************************************************************************************
C.Variables变量操作:
1.Create 变量函数:
varid = netcdf.defVar(ncid,varname,xtype,dimids)

2.从netdcf变量中返回数据,其函数形式有
data = netcdf.getVar(ncid,varid)
data = netcdf.getVar(ncid,varid,start)
data = netcdf.getVar(ncid,varid,start,count)
data = netcdf.getVar(ncid,varid,start,count,stride)
data = netcdf.getVar(...,output_type)
output_type可以取值:'int','double','int16','short','single','int8','float','int32','uint8'

3.取得变量的信息
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varid)

4.从变量名知道变量的ID号
varid = netcdf.inqVarID(ncid,varname)

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)

6.重命名变量名
netcdf.renameVar(ncid,varid,newName)
*******************************************************************************************
D.Attributes操作:

1.把Attributes拷贝到新的位置
netcdf.copyAtt(ncid_in,varid_in,attname,ncid_out,varid_out)

2.删除一个Attributes:
netcdf.delAtt(ncid,varid,attName)

3.获得一个Attributes:
attrvalue = netcdf.getAtt(ncid,varid,attname)
attrvalue = netcdf.getAtt(ncid,varid,attname,output_datatype)

4.返回Attributes的信息(类型和长度)
[xtype,attlen] = netcdf.inqAtt(ncid,varid,attname)

5.返回Attributes的ID:
attnum = netcdf.inqAttID(ncid,varid,attname)

6.返回Attributes的名字
attname = netcdf.inqAttName(ncid,varid,attnum)

7.向netcdf文件中写入Attributes
netcdf.putAtt(ncid,varid,attrname,attrvalue)

8.重命名Attributes的名
netcdf.renameAtt(ncid,varid,oldName,newName)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

评分

参与人数 2金钱 +20 贡献 +3 收起 理由
mofangbao + 10 + 2
topmad + 10 + 1

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-2-25 17:35:28 | 显示全部楼层
好长啊....
密码修改失败请联系微信:mofangbao

新浪微博达人勋

0
早起挑战累计收入
发表于 2012-2-25 19:03:18 | 显示全部楼层
不错的东西,主持了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-2-26 11:29:57 | 显示全部楼层
密码修改失败请联系微信:mofangbao

新浪微博达人勋

0
早起挑战累计收入
发表于 2012-2-26 12:51:06 | 显示全部楼层
topmad 发表于 2012-2-26 11:29
清风,是支持了....

我们到山上当住持吧

。。。。。。。。。。。。。。。。。。。。。。。
这两天是咋么回事。。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-2-26 13:04:27 | 显示全部楼层
mofangbao 发表于 2012-2-26 12:51
。。。。。。。。。。。。。。。。。。。。。。。
这两天是咋么回事。。

这两天心思不在这上面呗.....
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-2-27 09:35:16 | 显示全部楼层
好东西!一直习惯于带插件的传统命令,看来得学学这些新的了!落伍了。。。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-3-2 09:43:50 | 显示全部楼层
东西!一直习惯于带插件的传统命令,看来得学学这些新的了!落伍了。。。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-3-3 20:00:38 | 显示全部楼层
不错,学习了,这两天正好要用,谢谢了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-3-18 09:30:17 | 显示全部楼层
感谢楼主分享~~
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表