%*************************************************************************%
%程序目的:将nc文件批量转换为.tif文件并写出的方法
%2018年11月16日
%By jiujiezhe
%*************************************************************************%
clc;
clear all;
InPath = 'D:\360IExploreDownload\2010NC\';%批量处理的NC文件夹
OutPath = 'D:\360IExploreDownload\2010Tif\'; %输出路径
filelist=dir([InPath,'*.nc']); %指定批量数据的类型
a=filelist(1).name; %查看你要读取的文件的编号。filelist(1).name在window下为第一个标号数据
b=filelist(2).name; %查看你要读取的文件的编号。filelist(2).name在window下为第二个标号数据
k=length(filelist);
%循环开始
for i = 1:k
InFile = strcat(InPath,filelist(i).name);
ncdisp(InFile);%显示数据信息,变量
filename=[InPath,filelist(i).name];
preciData = ncread(filename,'precipitation');%读取降水量(根据需要读取自己想要的数据,根据前面的数据信息选择名称)
Lon = ncread(InFile,'lon'); %读取经度数据
Lat = ncread(InFile,'lat'); %读取纬度
GeoRef = georasterref('Rastersize',size(preciData),'Latlim',[double(min(Lat)),double(max(Lat))],'Lonlim',[double(min(Lon)),double(max(Lon))]);
preci_Tif = strcat(OutPath,filelist(i).name,'.tif');
geotiffwrite(preci_Tif,flip(preciData),GeoRef);%数据写进tif
end
%循环结束
disp('finished')