matlab批量读取nc文件
(2016-10-22 21:22:06)分类: 学习matlab |
http://bbs.06climate.com/forum.php?mod=viewthread&tid=11870&extra=&page=1
本帖子使用的MATLAB版本为2012b及以上。
读取单个nc文件的方法:
clc,clear;
ncid = netcdf.open('D:\precip.mon.1981-2010.ltm.nc','NOWRITE'); %打开nc文件
ncdisp('precip.mon.1981-2010.ltm.nc'); %在命令窗中显示nc文件的变量
PrecipData =
ncread('precip.mon.1981-2010.ltm.nc','precip');
%读入变量precip
TimeData =
ncread('precip.mon.1981-2010.ltm.nc','time');
%读入变量time
LonData =
ncread('precip.mon.1981-2010.ltm.nc','lon');
%读入变量lon
LatData =
ncread('precip.mon.1981-2010.ltm.nc','lat');
%读入变量lat
Valid_yr_countData =
ncread('precip.mon.1981-2010.ltm.nc','valid_yr_count');
%读入变量validprecip_yr_count
ClimatologyData =
ncread('precip.mon.1981-2010.ltm.nc','climatology_bounds');
%读入变量climatology_bounds
[X, Y] = meshgrid(LatData,LonData);
contourf(Y-180.0,X,PrecipData(:,:,9)); %画9月份等值面图
shading flat; %去掉等值线
colorbar('SouthOutside','Position',[0.142,0.03,0.75,0.04]); %添加颜色条 [左右,上下,长,宽]
load coast %加载全球海岸线,但是不显示出来
geoshow(lat,long); %显示出海岸线,lat和long是coast的属性
hold on;
set(gca,'LineWidth',1,'FontSize',10,'Ylim',[-90,90],'Xlim',[-180,180],'Position',[0.142,0.09,0.75,0.84]...
,'XTick',[-180:60:180],'XTicklabel',{'-180W','-120W','-60W','0','60E','120E','180E'}...
,'YTick',[-90:30:90],'YTicklabel',{'-90S','-60S','-30S','0','30N','60N','90N'});
%添加经纬度信息
hold off;
netcdf.close(ncid); %关闭nc文件
批量读取nc文件的方法:
clc; %清屏
clear; %清空
datadir='D:\data\降水数据\CPC Unified Gauge-Based Analysis of Daily Precipitation over CONUS\'; %指定批量数据所在的文件夹
filelist=dir([datadir,'*.nc']); %指定批量数据的类型
a=filelist(1).name; %查看你要读取的文件的编号。filelist(1).name在window下为第一个标号数据
b=filelist(2).name; %查看你要读取的文件的编号。filelist(2).name在window下为第二个标号数据
k=length(filelist);
for s=1:k
filename=[datadir,filelist(s).name];
ncid=netcdf.open(filename,'NC_NOWRITE');
ncdisp('D:\data\降水数据\CPC
Unified Gauge-Based Analysis of Daily Precipitation over
CONUS\precip.V1.0.1948.nc'); %在命令窗中显示nc文件的变量
%任意取其中一个来看数据中所包含的变量特征,以为下面读取数据变量做铺垫
% ncid =
netcdf.open('D:\data\降水数据\CPC Unified Gauge-Based Analysis of Daily
Precipitation over CONUS\precip.V1.0.1948.nc','NOWRITE');
%打开nc文件
% ncdisp('D:\data\降水数据\CPC
Unified Gauge-Based Analysis of Daily Precipitation over
CONUS\precip.V1.0.1948.nc'); %在命令窗中显示nc文件的变量
PrecipData =
ncread(filename,'precip'); %读入变量precip
TimeData =
ncread(filename,'time'); %读入变量time
LonData =
ncread(filename,'lon'); %读入变量lon
LatData =
ncread(filename,'lat'); %读入变量lat
netcdf.close(ncid);
% 关闭文件
end;
敬请大家批评指正,并在此基础上丰富内容
读取单个nc文件的方法:
clc,clear;
ncid = netcdf.open('D:\precip.mon.1981-2010.ltm.nc','NOWRITE'); %打开nc文件
ncdisp('precip.mon.1981-2010.ltm.nc'); %在命令窗中显示nc文件的变量
PrecipData
TimeData
LonData
LatData
Valid_yr_countData
ClimatologyData
[X, Y] = meshgrid(LatData,LonData);
contourf(Y-180.0,X,PrecipData(:,:,9)); %画9月份等值面图
shading flat; %去掉等值线
colorbar('SouthOutside','Position',[0.142,0.03,0.75,0.04]); %添加颜色条 [左右,上下,长,宽]
load coast %加载全球海岸线,但是不显示出来
geoshow(lat,long); %显示出海岸线,lat和long是coast的属性
hold on;
set(gca,'LineWidth',1,'FontSize',10,'Ylim',[-90,90],'Xlim',[-180,180],'Position',[0.142,0.09,0.75,0.84]...
hold off;
netcdf.close(ncid); %关闭nc文件
批量读取nc文件的方法:
clc;
clear; %清空
datadir='D:\data\降水数据\CPC Unified Gauge-Based Analysis of Daily Precipitation over CONUS\'; %指定批量数据所在的文件夹
filelist=dir([datadir,'*.nc']); %指定批量数据的类型
a=filelist(1).name; %查看你要读取的文件的编号。filelist(1).name在window下为第一个标号数据
b=filelist(2).name; %查看你要读取的文件的编号。filelist(2).name在window下为第二个标号数据
k=length(filelist);
for s=1:k
end;
敬请大家批评指正,并在此基础上丰富内容
前一篇:nc文件的读取