zhanghouxing的个人博客分享 http://blog.sciencenet.cn/u/zhanghouxing

博文

.nc数据读取详细资料matlab2010a及后面的版本

已有 7027 次阅读 2013-8-13 20:23 |系统分类:科研笔记| nc数据

(1)先声明下,关于nc的一些介绍引用了文章,这是读取数据需要首先学习的东西

http://blog.sciencenet.cn/blog-653020-549865.html

(2)里面的代码适用于matlab2010a及后面的版本,前面的版本需要安装插件

(3)现在将代码添加在后面,附近中是具体的文档

 

%This program is use for extracting the data in the form of nc

%The version of the matlab is 2010a and it may not be use for other versions

%your data will be extracted

filepath='E:work气候情景AR4HADCM3_SRA1B_pr_c30b';

fiename='HADCM3_SRA1B_1_pr_2070-2099.nc';

file=strcat(filepath,fiename);

%*****************reading*************************

ncid=netcdf.open(file,'nc_nowrite');

[ndims,nvars,ngatts,unlimdimid]=netcdf.inq(ncid); % 返回nc和cdf文件的信息,其中ndims为维度信息,nvars为变量个数信息,ngatts和unlimdimid为整体特征;

%下面是获取每个维度(维度个数为ndims)的名称和长度

names_lens=cell(ndims,2);

for i=0:ndims-1  

[dimname,dimlen]=netcdf.inqDim(ncid,i); % 查询ndims中维度的名称和资料长度,包含0~ndims-1的维度特征; dimid=0

names_lens{i+1,1}=dimname;

names_lens{i+1,2}=dimlen;

end

%下面是查询每个变量的属性特征

var_inform=cell(nvars,6);

for j=0:nvars-1

[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,j); %查询nvars中名称和资料维度,包含0~nvars-1个变量特征;  

var_inform{j+1,1}=varname; %变量名称

var_inform{j+1,2}=xtype; %数据类型

var_inform{j+1,3}=dimids; %维度信息

var_inform{j+1,4}=natts;%通过varid识别的属性个数

  attnames=cell(1,natts);

  attvals=cell(1,natts);

  for m=0:natts-1

   attname=netcdf.inqAttName(ncid,j,m); %获取各变量的属性名

   attval = netcdf.getAtt(ncid,j,attname);%获取各变量各属性名对应的属性值

   attnames{1,m+1}=attname;

   attvals{1,m+1}=attval;

  end  

  att_names_vals=[attnames;attvals];

 

var_inform{j+1,5}=netcdf.inqVarID(ncid,varname);% 返回查询变量varname 在nc和cdf文件中的nvars的位置,是变量的代号;

var_inform{j+1,6}=att_names_vals;

end

%下面是提取每个变量的具体数据

var_data=var_inform(:,[1,5]);

for k=1:nvars

   var_data{k,3}= netcdf.getVar(ncid,var_data{k,2}); % 获取变量;返回具体变量的具体内容(开始度。。。结尾度)

end

netcdf.close(ncid);

%下面是提取降雨数据的具体方式(我自己用)

%Pcp_Jan=roundn(flipud(var_data{7,3}(:,:,1)')*86400,-3);%需要经度为横坐标,纬度为纵坐标,所以矩阵翻转;需要得到单位是mm/day.所有需要乘以86400(原来单位是kg/m^2/s;

h = waitbar(0,'Please wait...','name','running');

pcp_month=cell(1,12);

for time=1:12

   %%%%%%%%%%%添加进度条

   if time==12

           waitbar(time/12,h,'finished');

           pause(1);

           delete(h);

      else

          waitbar(time/12,h,strcat(num2str(round((time/12)*100)),'%'));

      end

   %%%%%%%%%%%添加进度条              

 

  pcp_month{time}=roundn(flipud(var_data{7,3}(:,:,time)')*86400,-3);

  xlswrite('HADCM3_SRA1B_1_pr_2070-2099.xls',Pcp_Jan,strcat('month',num2str(time)));

end

%其他人可能用到的方式

%start=[0,0,0]; %起点位置

%count=[96,73,1]; %向后计数

%Pcp_Jan=flipud(netcdf.getVar(ncid,6,start,count)');



https://blog.sciencenet.cn/blog-922140-716555.html

上一篇:grb文件的读取 (更新)
下一篇:服务于IPCC 第四次报告(AR4)的气候情景简介
收藏 IP: 210.77.68.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 22:30

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部