适用于需要最大值合成方法的数据,植被数据等
folderPath = 'J:\PTJPL ET8218\驱动因子\FVC\month'; % 修改为你的文件夹路径
% 导入纬度数据
[aa, R] = geotiffread(fullfile(folderPath, 'FVC_1982_1.tif'));
info = geotiffinfo(fullfile(folderPath, 'FVC_1982_1.tif'));
% 定义年份范围
startYear = 1982;
endYear = 2018;
years = startYear:endYear;
% 获取所有栅格文件的列表
filePattern = fullfile(folderPath, 'FVC_*.tif');
rasterFiles = dir(filePattern);
% 初始化一个结构数组来存储每年的最大合成结果
annualMaxFVC = struct();
% 循环遍历每一年
for year = years
% 初始化一个列表来存储每月的FVC数据
monthlyFVC = [];
% 循环遍历每个月
for month = 1:12
% 构造文件名
fileName = sprintf('FVC_%d_%d.tif', year, month);
filePath = fullfile(folderPath, fileName);
% 检查文件是否存在
if isfile(filePath)
% 读取栅格数据
fvcData = imread(filePath);
% 如果是第一个月,初始化monthlyFVC矩阵
if isempty(monthlyFVC)
monthlyFVC = fvcData;
else
% 对于每个像元,更新最大FVC值
monthlyFVC = max(monthlyFVC, fvcData);
end
end
end
% 将每年的最大FVC值存储在结构数组中
annualMaxFVC(year - startYear + 1).year = year;
annualMaxFVC(year - startYear + 1).maxFVC = monthlyFVC;
% 保存每年的最大FVC值为新的tif文件
outputFileName = sprintf('FVC_Max_%d.tif', year);
outputFilePath = fullfile(folderPath, outputFileName);
% 将 double 类型的 FVC 值转换为 [0, 10000] 范围的 uint16 类型,以保持精度
monthlyFVC_scaled = uint16(monthlyFVC * 10000);
geotiffwrite(outputFilePath, monthlyFVC_scaled, R);
fprintf('Year %d processed and saved to %s\n', year, outputFilePath);
end