IDL编写NDVI计算函数,数值范围控制在[-1,1],实现在ENVI中BAND MATH中调用
时间: 2024-11-20 22:56:08 浏览: 41
在ENVI(Environmental Imaging Science and Technology Initiative)环境中,要编写一个用于计算归一化差值植被指数(Normalized Difference Vegetation Index,NDVI)的IDL(Interactive Data Language)函数,并限制其结果在[-1,1]范围内,你可以按照以下步骤进行:
```idl
; 定义NDVI函数
procedure ndvi(band1, band2, out_result)
{
; 确保输入是浮点数
if (typeof(band1) ne TFLOAT || typeof(band2) ne TFLOAT) then begin
print, "Error: Both input bands must be floating-point.";
return;
end
; 计算NDVI公式:(band_nir - band_red) / (band_nir + band_red)
local numerator = band1 - band2;
local denominator = band1 + band2;
; 检查分母是否为零,防止除以零错误
if (denominator == 0.0) then begin
set output_error, "Divide by zero error: Band values are not valid for NDVI calculation.";
return;
end
; 计算并归一化NDVI值
out_result = numerator / denominator;
; 将结果限制在[-1,1]区间内
out_result = max(min(out_result, 1.0), -1.0);
end
; 调用该函数,例如使用ENVI的数据集
local red_band = read_envi_band("path_to红光波段.tif")
local nir_band = read_envi_band("path_to近红外波段.tif")
; 创建新的output band来存储结果
local ndvi_output = create_new_bands(red_band.shape_info, TFLOAT)
; 计算NDVI
ndvi(red_band, nir_band, ndvi_output)
; 将结果保存到新文件
write_envi_band("path_to_ndvi_output.tif", ndvi_output)
阅读全文