资源描述
NetCDF格式转换TIFF-以全球GIMMS3g NDVI数据为例
1. 全球GIMMS3g NDVI数据格式为.nc4. 通过Matlab中的 ncdisp即可查看数据内容。
2. 数据范围为全球尺度,[-90 90] [-180 180]
3. 每个GIMMS3g NDVI数据中包含十二幅栅格影像,时间尺度为每月两幅。
Matlab 代码如下
%%
clear;clc
%% set the input path for containing inputdata
InputPath = 'input\';
% read the nerCDF4 file
% initial image lat [-90 90] lon[-180 180]
= '*.nc4';
OutputPath=(['output\']);
if exist(OutputPath,'dir')==0 %when the dir exist, the value is 7,or the value is 0
mkdir(OutputPath); %build the new direct
end
Ifiles = dir([InputPath,]);
nIfiles = length(Ifiles);
%% add the reference information
% set the maximum and minimun latitude and longitude
latlim = [-90 90];
lonlim = [-180 180];
% define the geo-reference
R = georefcells(latlim,lonlim,[3600 7200],'ColumnsStartFrom','north');
for nf =1:nIfiles
I = Ifiles(nf).name;
I = I(end-6);
year = I(end-12:end-9);
year = str2double(year);
IData = ncread([InputPath,I],'ndvi');
% each .nc4 12 NDVI images in half year
if str2double(I) ==1
Imonth = 1;
else
Imonth = 7;
end
for i =1:12
ndvidata = IData(:,:,i);
ndvidata = ndvidata';
% resample the data to 0.05 degree
ndvi05deg=imresize(ndvidata,[3600 7200],'nearest');
ndvi05deg= double(ndvi05deg)/10000;
ndvi05deg(ndvi05deg(:,:)<= 0)=NaN;
if mod(i,2) == 0
ndviname = year*10000+Imonth*100+2;
ndviname = num2str(ndviname);
% save the mat file
save([OutputPath,ndviname,'.mat'],'ndvi05deg','-v7.3');
% save a TIFF image
geotiffwrite([OutputPath,ndviname],ndvi05deg,R);
%% convert the NDVI from half month to month
% choose the maxinum as the month ndvi vlaue
NDVImax=ndvi05deg;
C1=(NDVImax-NDVI1>=0);
C2=(NDVI1-NDVImax>0);
NDVImon=C1.*NDVImax+C2.*NDVI1;
% save the mat file
ndviMoname = year*100+Imonth;
ndviMoname = num2str(ndviMoname);
save([OutputPath,ndviMoname,'.mat'],'NDVImon','-v7.3');
% save a TIFF image
geotiffwrite([OutputPath,ndviMoname],NDVImon,R);
clear C1 C2 NDVI1 NDVImax NDVImon;
Imonth = Imonth+1;
else
ndviname = year*10000+Imonth*100+1;
ndviname = num2str(ndviname);
% save the mat file
save([OutputPath,ndviname,'.mat'],'ndvi05deg','-v7.3');
% save a TIFF image
geotiffwrite([OutputPath,ndviname],ndvi05deg,R);
NDVI1=ndvi05deg;
end
end
end
展开阅读全文