基于MOD09Q1数据批量计算NDVI
通过MRT处理好b01和b02波段后,分别存储至两个不同的文件夹(b01和b02)。
接下来打开在arcgis自带的python2.7中键入以下代码:(如果用pycharm和spyder的朋友需要把环境配置为python2.7并且安装arcpy包)
'''
import os,arcpy,datetime
from arcpy.sa import *
Red_path = r'C:\Users\adminstor\Desktop\b01'
NIR_path = r'C:\Users\adminstor\Desktop\b02'
path =r'C:\Users\adminstor\Desktop'
intermediateDataPath = path+"\\"+"IntermediateData"
resultDataPath = path+"\\"+"Result"
if os.path.exists(intermediateDataPath):
print "IntermediateData floder exists"
else:
arcpy.CreateFolder_management(path, "IntermediateData")
if os.path.exists(resultDataPath):
print "Result floder exists"
else:
arcpy.CreateFolder_management(path, "Result")
print "Under calculation......"
arcpy.env.workspace = Red_path
rasters = arcpy.ListRasters("*", 'tif')
for raster in rasters:
print(raster)
arcpy.env.workspace = intermediateDataPath
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("3D")
NIRraster = NIR_path +'\\'+raster[:-5]+"2.tif"
NDVIoutPath = resultDataPath+'\\'+ raster[:-16]+ "_NDVI.tif"
print(NIRraster)
arcpy.Float_3d(Red_path+'\\'+ raster, "floatRedBand.tif")
arcpy.Float_3d(NIRraster, "floatNIRBand.tif")
arcpy.Minus_3d("floatNIRBand.tif", "floatRedBand.tif", "outminus.tif")
arcpy.Plus_3d("floatNIRBand.tif", "floatRedBand.tif", "NDVIfenmu.tif")
arcpy.Divide_3d("outminus.tif", "NDVIfenmu.tif", NDVIoutPath)
print(raster+" has done")
print("All done")
for i in os.listdir(intermediateDataPath):
path_file = os.path.join(intermediateDataPath,i)
if os.path.isfile(path_file):
os.remove(path_file)
算好了以后就可以拿来批量裁剪后使用了,批量裁剪可以在arcgis中model building或arcpy中进行。
|