|
改编自Matlab中文论坛https://ilovematlab.cn/thread-498179-1-1.html by 'rainbowag'.
提取netcdf数据中的NDVI,并写入geotiff文件,文件名为201401a.tif, 201401b.tif, ...
% ------------ four files in the directory to process ---------------
% ndvi3g_geo_v1_2014_0106.nc4
% ndvi3g_geo_v1_2014_0712.nc4
% ndvi3g_geo_v1_2015_0106.nc4
% ndvi3g_geo_v1_2015_0712.nc4
% ---------------- the code ----------------
clc; clear; close all
%% set the input path forcontaining inputdata
% read the nerCDF4 file
% initial image lat [-90 90] lon[-180 180]
filename = 'ndvi3g*2015*.nc4';
Ifiles = dir(filename);
nIfiles = length(Ifiles);
%% add the referenceinformation
% set the maximum andminimun latitude and longitude
latlim = [-90 90];
lonlim = [-180 180];
% define the geo-reference
R = georefcells(latlim,lonlim,[2160 4320],'ColumnsStartFrom','north');
for nf = 1:nIfiles
Ifilename = Ifiles(nf).name;
Ifiletime = Ifilename(end-6);
year = Ifilename(end-12:end-9);
year = str2double(year);
IData = ncread(Ifilename,'ndvi');
% each.nc4 file contain 12 NDVI images in half year
if str2double(Ifiletime)==1
Imonth = 1;
else
Imonth = 7;
end
if Imonth==1
for i = 1:2:12
ndvidata = IData(:,:,i);
ndvidata = ndvidata';
% name the files as yyyymma.tif or yyyymmb.tif
ndviname = [num2str(year),num2str((i+1)/2,'%02d'),'a.tif'];
% save a TIFF image
geotiffwrite(ndviname,ndvidata,R);
end
for i=2:2:12
ndvidata = IData(:,:,i);
ndvidata = ndvidata';
% name the files as yyyymma.tif or yyyymmb.tif
ndviname = [num2str(year),num2str(i/2,'%02d'),'b.tif'];
% save a TIFF image
geotiffwrite(ndviname,ndvidata,R);
end
elseif Imonth==7
for i = 1:2:12
ndvidata = IData(:,:,i);
ndvidata = ndvidata';
% name the files as yyyymma.tif or yyyymmb.tif
ndviname = [num2str(year),num2str((i+1)/2+6,'%02d'),'a.tif'];
% save a TIFF image
geotiffwrite(ndviname,ndvidata,R);
end
for i=2:2:12
ndvidata = IData(:,:,i);
ndvidata = ndvidata';
% name the files as yyyymma.tif or yyyymmb.tif
ndviname = [num2str(year),num2str(i/2+6,'%02d'),'b.tif'];
% save a TIFF image
geotiffwrite(ndviname,ndvidata,R);
end
end
end
%%
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-22 16:24
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社