happy 发表于 2015-10-16 08:36

matlab批量读取nc文件

读取单个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

= meshgrid(LatData,LonData);
contourf(Y-180.0,X,PrecipData(:,:,9)); %画9月份等值面图
shading flat; %去掉等值线
colorbar('SouthOutside','Position',); %添加颜色条 [左右,上下,长,宽]

load coast %加载全球海岸线,但是不显示出来
geoshow(lat,long); %显示出海岸线,lat和long是coast的属性
hold on;

set(gca,'LineWidth',1,'FontSize',10,'Ylim',[-90,90],'Xlim',[-180,180],'Position',...
   ,'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(); %指定批量数据的类型
a=filelist(1).name; %查看你要读取的文件的编号。filelist(1).name在window下为第一个标号数据
b=filelist(2).name; %查看你要读取的文件的编号。filelist(2).name在window下为第二个标号数据
k=length(filelist);
for s=1:k
filename=;
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;

转自气象家园论坛
页: [1]
查看完整版本: matlab批量读取nc文件