声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1749|回复: 0

[编程技巧] matlab批量读取nc文件

[复制链接]
发表于 2015-10-16 08:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

转自气象家园论坛
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-19 19:43 , Processed in 0.069536 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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